|
| 1 | +from django.test import TestCase |
| 2 | + |
| 3 | +from insights.charts.importer_panel import exploits_queryset |
| 4 | +from insights.charts.importer_panel import packages_queryset |
| 5 | +from vulnerabilities.models import AdvisoryExploit |
| 6 | +from vulnerabilities.models import AdvisoryV2 |
| 7 | +from vulnerabilities.models import ImpactedPackage |
| 8 | +from vulnerabilities.models import ImpactedPackageAffecting |
| 9 | +from vulnerabilities.models import PackageV2 |
| 10 | + |
| 11 | + |
| 12 | +def create_adv(avid, unique_id): |
| 13 | + return AdvisoryV2.objects.create( |
| 14 | + avid=f"github_osv/{avid}", |
| 15 | + datasource_id="github_osv", |
| 16 | + unique_content_id=unique_id, |
| 17 | + is_latest=True, |
| 18 | + pipeline_id="github_osv_pipeline", |
| 19 | + advisory_id=avid, |
| 20 | + url="https://example.com/" + avid, |
| 21 | + ) |
| 22 | + |
| 23 | + |
| 24 | +class TestImporterPanelQuerysets(TestCase): |
| 25 | + def test_packages_queryset(self): |
| 26 | + advisory_with_package = create_adv("GHSA-1", "1") |
| 27 | + pkg1 = PackageV2.objects.create(type="npm", name="test1", version="1.0.0") |
| 28 | + impact1 = ImpactedPackage.objects.create(advisory=advisory_with_package) |
| 29 | + ImpactedPackageAffecting.objects.create(impacted_package=impact1, package=pkg1) |
| 30 | + |
| 31 | + advisory_with_ghost_package = create_adv("GHSA-2", "2") |
| 32 | + pkg2 = PackageV2.objects.create(type="npm", name="test2", version="2.0.0", is_ghost=True) |
| 33 | + impact2 = ImpactedPackage.objects.create(advisory=advisory_with_ghost_package) |
| 34 | + ImpactedPackageAffecting.objects.create(impacted_package=impact2, package=pkg2) |
| 35 | + |
| 36 | + advisory_without_package = create_adv("GHSA-3", "3") |
| 37 | + |
| 38 | + qs = list(packages_queryset()) |
| 39 | + |
| 40 | + self.assertEqual(len(qs), 1) |
| 41 | + stats = qs[0] |
| 42 | + self.assertEqual(stats["datasource_id"], "github_osv") |
| 43 | + self.assertEqual(stats["total_advisories"], 3) |
| 44 | + self.assertEqual(stats["advisories_with_packages"], 2) # First two have packages |
| 45 | + self.assertEqual(stats["advisories_with_ghost_packages"], 1) |
| 46 | + |
| 47 | + def test_exploits_queryset(self): |
| 48 | + advisory_with_kev = create_adv("GHSA-1", "1") |
| 49 | + AdvisoryExploit.objects.create(advisory=advisory_with_kev, data_source="KEV") |
| 50 | + |
| 51 | + advisory_with_metasploit = create_adv("GHSA-2", "2") |
| 52 | + AdvisoryExploit.objects.create(advisory=advisory_with_metasploit, data_source="Metasploit") |
| 53 | + |
| 54 | + advisory_with_exploitdb = create_adv("GHSA-3", "3") |
| 55 | + AdvisoryExploit.objects.create(advisory=advisory_with_exploitdb, data_source="Exploit-DB") |
| 56 | + |
| 57 | + advisory_without_exploits = create_adv("GHSA-4", "4") |
| 58 | + |
| 59 | + qs = list(exploits_queryset()) |
| 60 | + |
| 61 | + self.assertEqual(len(qs), 1) |
| 62 | + stats = qs[0] |
| 63 | + self.assertEqual(stats["datasource_id"], "github_osv") |
| 64 | + |
| 65 | + # GHSA-4 shouldn't count in these |
| 66 | + self.assertEqual(stats["advisories_with_kev"], 1) |
| 67 | + self.assertEqual(stats["advisories_with_metasploit"], 1) |
| 68 | + self.assertEqual(stats["advisories_with_exploitdb"], 1) |
0 commit comments