Skip to content

Commit bf048be

Browse files
committed
Fix failing tests
Signed-off-by: Tushar Goel <[email protected]>
1 parent 0aface0 commit bf048be

File tree

2 files changed

+51
-51
lines changed

2 files changed

+51
-51
lines changed

vulnerabilities/models.py

Lines changed: 33 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -317,28 +317,40 @@ class Meta:
317317
ordering = ["vulnerability", "reference"]
318318

319319

320+
def purl_to_dict(purl: PackageURL):
321+
"""
322+
Return a dict of purl components suitable for use in a queryset.
323+
We need to have specific empty values for using in querysets because of our peculiar model structure.
324+
325+
For example::
326+
>>> purl_to_dict(PackageURL.from_string("pkg:generic/postgres"))
327+
{'type': 'generic', 'namespace': '', 'name': 'postgres', 'version': '', 'qualifiers': {}, 'subpath': ''}
328+
>>> purl_to_dict(PackageURL.from_string("pkg:generic/postgres/[email protected]?foo=bar#baz"))
329+
{'type': 'generic', 'namespace': 'postgres', 'name': 'postgres', 'version': '1.2', 'qualifiers': {'foo': 'bar'}, 'subpath': 'baz'}
330+
"""
331+
if isinstance(purl, str):
332+
purl = PackageURL.from_string(purl)
333+
334+
return dict(
335+
type=purl.type,
336+
namespace=purl.namespace or "",
337+
name=purl.name,
338+
version=purl.version or "",
339+
qualifiers=purl.qualifiers or {},
340+
subpath=purl.subpath or "",
341+
)
342+
343+
320344
class PackageQuerySet(BaseQuerySet, PackageURLQuerySet):
321345
def get_or_create_from_purl(self, purl: PackageURL):
322346
"""
323347
Return an existing or new Package (created if neeed) given a
324348
``purl`` PackageURL.
325349
"""
326-
purl_fields = without_empty_values(purl.to_dict(encode=True))
327-
328-
# when there are 2 packages one with qualifiers and one without
329-
# qualifiers, having all other fields same, this raises MultipleObjectsReturned
330-
# so we are filling out the fields with empty value to avoid this
331-
# for field in PackageURL._fields:
332-
# # name, type, and version are required fields
333-
# if field not in purl_fields:
334-
# if field == "namespace":
335-
# purl_fields[field] = ""
336-
# if field == "qualifiers":
337-
# purl_fields[field] = {}
338-
# if field == "subpath":
339-
# purl_fields[field] = ""
340-
341-
package, _ = Package.objects.get_or_create(**purl_fields)
350+
if isinstance(purl, str):
351+
purl = PackageURL.from_string(purl)
352+
353+
package, _ = Package.objects.get_or_create(**purl_to_dict(purl=purl))
342354
return package
343355

344356
def for_package_url_object(self, purl):
@@ -347,15 +359,12 @@ def for_package_url_object(self, purl):
347359
``purl`` string is validated and transformed into filtering lookups. If
348360
this is a PackageURL object it is reused as-is.
349361
"""
350-
if isinstance(purl, PackageURL):
351-
lookups = without_empty_values(purl.to_dict(encode=True))
352-
return self.filter(**lookups)
353-
354-
elif isinstance(purl, str):
355-
return self.for_package_url(purl, encode=False)
356-
357-
else:
362+
if not purl:
358363
return self.none()
364+
if isinstance(purl, str):
365+
purl = PackageURL.from_string(purl)
366+
lookups = without_empty_values(purl.to_dict(encode=True))
367+
return self.filter(**lookups)
359368

360369
def affected(self):
361370
"""

vulnerabilities/tests/test_postgresql.py

Lines changed: 18 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,13 @@
77
# See https://aboutcode.org for more information about nexB OSS projects.
88
#
99

10-
import json
1110
import os
1211

13-
from django.test import TestCase
14-
from packageurl import PackageURL
12+
import pytest
1513

16-
from vulnerabilities import severity_systems
17-
from vulnerabilities.import_runner import process_advisories
18-
from vulnerabilities.importer import AdvisoryData
19-
from vulnerabilities.importer import Reference
20-
from vulnerabilities.importer import VulnerabilitySeverity
2114
from vulnerabilities.importers.postgresql import to_advisories
22-
from vulnerabilities.improve_runner import ImproveRunner
23-
from vulnerabilities.improve_runner import process_inferences
24-
from vulnerabilities.improvers.default import DefaultImprover
15+
from vulnerabilities.models import Package
2516
from vulnerabilities.tests import util_tests
26-
from vulnerabilities.utils import AffectedPackage
2717

2818
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
2919
TEST_DATA = os.path.join(
@@ -32,18 +22,19 @@
3222
)
3323

3424

35-
class TestPostgreSQLImporter(TestCase):
36-
def test_to_advisories(self):
37-
with open(os.path.join(TEST_DATA, "advisories.html")) as f:
38-
raw_data = f.read()
39-
advisories = to_advisories(raw_data)
40-
result = [data.to_dict() for data in advisories]
41-
expected_file = os.path.join(TEST_DATA, f"parse-advisory-postgresql-expected.json")
42-
util_tests.check_results_against_json(result, expected_file)
43-
44-
def test_run_default_improver(self):
45-
with open(os.path.join(TEST_DATA, "improver-data.json")) as f:
46-
raw_data = json.load(f)
47-
advisories = [AdvisoryData.from_dict(data) for data in raw_data]
48-
process_advisories(advisories, "postgresql")
49-
ImproveRunner(DefaultImprover).run()
25+
def test_to_advisories():
26+
with open(os.path.join(TEST_DATA, "advisories.html")) as f:
27+
raw_data = f.read()
28+
advisories = to_advisories(raw_data)
29+
result = [data.to_dict() for data in advisories]
30+
expected_file = os.path.join(TEST_DATA, f"parse-advisory-postgresql-expected.json")
31+
util_tests.check_results_against_json(result, expected_file)
32+
33+
34+
@pytest.mark.django_db
35+
def test_get_or_create_from_purl():
36+
p1 = "pkg:generic/postgres"
37+
p2 = "pkg:generic/postgres?foo=bar"
38+
res1 = Package.objects.get_or_create_from_purl(p1)
39+
res2 = Package.objects.get_or_create_from_purl(p2)
40+
res3 = Package.objects.get_or_create_from_purl(p1)

0 commit comments

Comments
 (0)