|
| 1 | +"""Vulnerability_Id type autodetection, the backfill helper path, and the uniqueness constraint.""" |
| 2 | +from django.db import IntegrityError, transaction |
| 3 | +from django.test import SimpleTestCase |
| 4 | + |
| 5 | +from dojo.finding.helper import save_vulnerability_ids |
| 6 | +from dojo.finding.vulnerability_id import resolve_vulnerability_id_type |
| 7 | +from dojo.models import Finding, Vulnerability_Id |
| 8 | +from unittests.dojo_test_case import DojoTestCase, versioned_fixtures |
| 9 | + |
| 10 | + |
| 11 | +class TestVulnerabilityIdTypeResolver(SimpleTestCase): |
| 12 | + |
| 13 | + def test_autodetect(self): |
| 14 | + self.assertEqual(resolve_vulnerability_id_type("CVE-2024-1234"), "CVE") |
| 15 | + self.assertEqual(resolve_vulnerability_id_type("GHSA-9v3m-xxxx"), "GHSA") |
| 16 | + self.assertEqual(resolve_vulnerability_id_type("RUSTSEC-2021-0001"), "RUSTSEC") |
| 17 | + self.assertEqual(resolve_vulnerability_id_type("ALINUX2-SA-2021"), "ALINUX2") |
| 18 | + self.assertEqual(resolve_vulnerability_id_type("cve-2024-1"), "CVE") |
| 19 | + # No non-numeric prefix -> None |
| 20 | + self.assertIsNone(resolve_vulnerability_id_type("1234")) |
| 21 | + self.assertIsNone(resolve_vulnerability_id_type("")) |
| 22 | + self.assertIsNone(resolve_vulnerability_id_type(None)) |
| 23 | + |
| 24 | + |
| 25 | +@versioned_fixtures |
| 26 | +class TestVulnerabilityIdType(DojoTestCase): |
| 27 | + fixtures = ["dojo_testdata.json"] |
| 28 | + |
| 29 | + def setUp(self): |
| 30 | + self.finding = Finding.objects.get(id=2) |
| 31 | + Vulnerability_Id.objects.filter(finding=self.finding).delete() |
| 32 | + |
| 33 | + def test_save_sets_type(self): |
| 34 | + row = Vulnerability_Id.objects.create(finding=self.finding, vulnerability_id="CVE-2024-1234") |
| 35 | + self.assertEqual(row.vulnerability_id_type, "CVE") |
| 36 | + # bare number -> no type |
| 37 | + row2 = Vulnerability_Id.objects.create(finding=self.finding, vulnerability_id="12345") |
| 38 | + self.assertIsNone(row2.vulnerability_id_type) |
| 39 | + |
| 40 | + def test_bulk_save_sets_type(self): |
| 41 | + # save_vulnerability_ids uses bulk_create, which sets the type at construction |
| 42 | + save_vulnerability_ids(self.finding, ["CVE-2024-1", "GHSA-aaaa-bbbb"]) |
| 43 | + types = dict( |
| 44 | + Vulnerability_Id.objects.filter(finding=self.finding).values_list("vulnerability_id", "vulnerability_id_type"), |
| 45 | + ) |
| 46 | + self.assertEqual(types, {"CVE-2024-1": "CVE", "GHSA-aaaa-bbbb": "GHSA"}) |
| 47 | + |
| 48 | + def test_unique_constraint(self): |
| 49 | + Vulnerability_Id.objects.create(finding=self.finding, vulnerability_id="CVE-2024-1234") |
| 50 | + with self.assertRaises(IntegrityError), transaction.atomic(): |
| 51 | + Vulnerability_Id.objects.create(finding=self.finding, vulnerability_id="CVE-2024-1234") |
0 commit comments