Skip to content

Commit

Permalink
Address review comments
Browse files Browse the repository at this point in the history
Signed-off-by: Tushar Goel <tushar.goel.dav@gmail.com>
  • Loading branch information
TG1999 committed Aug 7, 2023
1 parent 0937a62 commit b34edf8
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 12 deletions.
6 changes: 6 additions & 0 deletions packagedb/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,12 @@ def filter_by_checksums(self, request, *args, **kwargs):

qs = Package.objects.filter(q)
paginated_qs = self.paginate_queryset(qs)
get_enhanced_package_data = request.query_params.get('get_enhanced_package_data', False)
if get_enhanced_package_data:
enhanced_package_data = [get_enhanced_package(package=package) for package in qs]
enhanced_package_data = [package for package in enhanced_package_data if package]
paginated_response = self.get_paginated_response(enhanced_package_data)
return paginated_response
serializer = PackageAPISerializer(paginated_qs, many=True, context={'request': request})
return self.get_paginated_response(serializer.data)

Expand Down
9 changes: 0 additions & 9 deletions packagedb/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,15 +161,6 @@ class Meta:


class PackageAPISerializer(HyperlinkedModelSerializer):

def to_representation(self, instance):
data = super().to_representation(instance)
request = self.context["request"]
if hasattr(request, "query_params") and request.query_params.get("enhanced", False):
from packagedb.api import get_enhanced_package
data["enhanced_package"] = get_enhanced_package(instance)
return data

dependencies = DependentPackageSerializer(many=True)
parties = PartySerializer(many=True)
resources = HyperlinkedIdentityField(view_name='api:package-resources', lookup_field='uuid')
Expand Down
26 changes: 23 additions & 3 deletions packagedb/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,14 +265,31 @@ def setUp(self):
self.package3 = Package.objects.create(**self.package_data3)
self.package3.refresh_from_db()

self.package_data4= {
'type': 'jar',
'namespace': 'sample',
'name': 'Baz',
'version': '90.123',
'qualifiers': '',
'subpath': '',
'download_url': 'http://anothersample.com',
'filename': 'Baz.zip',
'sha1': 'testsha1-4',
'md5': 'testmd5-3',
'size': 100,
'package_content': 5,
}
self.package4 = Package.objects.create(**self.package_data4)
self.package4.refresh_from_db()

self.test_url = 'http://testserver/api/packages/{}/'

self.client = APIClient()

def test_package_api_list_endpoint(self):
response = self.client.get('/api/packages/')
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(3, response.data.get('count'))
self.assertEqual(4, response.data.get('count'))

def test_package_api_list_endpoint_filter(self):
for key, value in self.package_data.items():
Expand Down Expand Up @@ -448,14 +465,17 @@ def test_package_api_filter_by_checksums(self):
'testsha1',
'testsha1-2',
'testsha1-3',
'testsha1-4',
]
data = {
'sha1': sha1s
}
enhanced_response = self.client.post('/api/packages/filter_by_checksums/?get_enhanced_package_data=true', data=data)
self.assertEqual(1, len(enhanced_response.data['results']))
response = self.client.post('/api/packages/filter_by_checksums/', data=data)
self.assertEqual(3, response.data['count'])
self.assertEqual(4, response.data['count'])
expected = self.get_test_loc('api/package-filter_by_checksums-expected.json')
self.check_expected_results(response.data['results'], expected, fields_to_remove=["url", "uuid", "resources"], regen=False)
self.check_expected_results(response.data['results'], expected, fields_to_remove=["url", "uuid", "resources"], regen=True)


class PackageApiReindexingTestCase(JsonBasedTesting, TestCase):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,5 +136,51 @@
"datasource_id":null,
"file_references":[],
"dependencies":[]
},
{
"filename":"Baz.zip",
"package_sets":[],
"package_content":"binary",
"purl":"pkg:jar/sample/baz@90.123",
"type":"jar",
"namespace":"sample",
"name":"baz",
"version":"90.123",
"qualifiers":"",
"subpath":"",
"primary_language":null,
"description":null,
"release_date":null,
"parties":[],
"keywords":[],
"homepage_url":null,
"download_url":"http://anothersample.com",
"bug_tracking_url":null,
"code_view_url":null,
"vcs_url":null,
"repository_homepage_url":null,
"repository_download_url":null,
"api_data_url":null,
"size":100,
"md5":"testmd5-3",
"sha1":"testsha1-4",
"sha256":null,
"sha512":null,
"copyright":null,
"holder":null,
"declared_license_expression":null,
"declared_license_expression_spdx":null,
"license_detections":[],
"other_license_expression":null,
"other_license_expression_spdx":null,
"other_license_detections":[],
"extracted_license_statement":null,
"notice_text":null,
"source_packages":[],
"extra_data":{},
"package_uid":"pkg:jar/sample/baz@90.123?uuid=fixed-uid-done-for-testing-5642512d1758",
"datasource_id":null,
"file_references":[],
"dependencies":[]
}
]

0 comments on commit b34edf8

Please sign in to comment.