Skip to content

Commit 0e5fa85

Browse files
committed
add unittests and grype
1 parent 673d420 commit 0e5fa85

5 files changed

Lines changed: 655 additions & 0 deletions

File tree

dojo/tools/anchore_grype/parser.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,9 @@ def get_findings(self, file, test):
121121
finding_description += f"\n**Package URL:** {artifact_purl}"
122122

123123
finding_mitigation = None
124+
fix_available = False
124125
if vuln_fix_versions:
126+
fix_available = True
125127
finding_mitigation = "Upgrade to version:"
126128
if len(vuln_fix_versions) == 1:
127129
finding_mitigation += f" {vuln_fix_versions[0]}"
@@ -200,6 +202,7 @@ def get_findings(self, file, test):
200202
dynamic_finding=False,
201203
nb_occurences=1,
202204
file_path=file_path,
205+
fix_available=fix_available,
203206
)
204207
dupes[dupe_key].unsaved_vulnerability_ids = vulnerability_ids
205208

unittests/scans/anchore_grype/fix_available.json

Lines changed: 315 additions & 0 deletions
Large diffs are not rendered by default.

unittests/scans/anchore_grype/fix_not_available.json

Lines changed: 313 additions & 0 deletions
Large diffs are not rendered by default.

unittests/test_import_reimport.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,8 @@ def __init__(self, *args, **kwargs):
102102
self.scan_type_gitlab_dast = "GitLab DAST Report"
103103

104104
self.anchore_grype_file_name = get_unit_tests_scans_path("anchore_grype") / "check_all_fields.json"
105+
self.anchore_grype_file_name_fix_not_available = get_unit_tests_scans_path("anchore_grype") / "fix_not_available.json"
106+
self.anchore_grype_file_name_fix_available = get_unit_tests_scans_path("anchore_grype") / "fix_available.json"
105107
self.anchore_grype_scan_type = "Anchore Grype"
106108

107109
self.checkmarx_one_open_and_false_positive = get_unit_tests_scans_path("checkmarx_one") / "one-open-one-false-positive.json"
@@ -1692,6 +1694,27 @@ def test_import_reimport_vulnerability_ids(self):
16921694
self.assertEqual("GHSA-v6rh-hp5x-86rv", findings[3].vulnerability_ids[0])
16931695
self.assertEqual("CVE-2021-44420", findings[3].vulnerability_ids[1])
16941696

1697+
def test_import_reimport_fix_available(self):
1698+
import0 = self.import_scan_with_params(self.anchore_grype_file_name_fix_not_available, scan_type=self.anchore_grype_scan_type)
1699+
test_id = import0["test"]
1700+
test = Test.objects.get(id=test_id)
1701+
findings = Finding.objects.filter(test=test)
1702+
self.assertEqual(1, len(findings))
1703+
self.assertEqual(False, findings[0].fix_available)
1704+
test_type = Test_Type.objects.get(name=self.anchore_grype_scan_type)
1705+
reimport_test = Test(
1706+
engagement=test.engagement,
1707+
test_type=test_type,
1708+
scan_type=self.anchore_grype_scan_type,
1709+
target_start=datetime.now(timezone.get_current_timezone()),
1710+
target_end=datetime.now(timezone.get_current_timezone()),
1711+
)
1712+
reimport_test.save()
1713+
self.reimport_scan_with_params(reimport_test.id, self.anchore_grype_file_name_fix_available, scan_type=self.anchore_grype_scan_type)
1714+
findings = Finding.objects.filter(test=reimport_test)
1715+
self.assertEqual(1, len(findings))
1716+
self.assertEqual(True, findings[0].fix_available)
1717+
16951718
def test_import_history_reactivated_and_untouched_findings_do_not_mix(self):
16961719
import0 = self.import_scan_with_params(self.generic_import_1, scan_type=self.scan_type_generic)
16971720
test_id = import0["test"]

unittests/tools/test_anchore_grype_parser.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,7 @@ def test_grype_issue_9618(self):
265265
parser = AnchoreGrypeParser()
266266
findings = parser.get_findings(testfile, Test())
267267
self.assertEqual(35, len(findings))
268+
self.assertEqual(findings[0].fix_available, True)
268269

269270
def test_grype_issue_9942(self):
270271
with (get_unit_tests_scans_path("anchore_grype") / "issue_9942.json").open(encoding="utf-8") as testfile:

0 commit comments

Comments
 (0)