|
11 | 11 | from django.db import connection |
12 | 12 | from django.db.migrations.executor import MigrationExecutor |
13 | 13 | from django.test import TestCase |
| 14 | +from django.utils import timezone |
| 15 | +from packageurl import PackageURL |
| 16 | +from univers.version_range import VersionRange |
14 | 17 |
|
15 | 18 | from vulnerabilities import severity_systems |
| 19 | +from vulnerabilities.importer import AdvisoryData |
| 20 | +from vulnerabilities.importer import AffectedPackage |
| 21 | +from vulnerabilities.importer import Reference |
16 | 22 |
|
17 | 23 |
|
18 | 24 | class TestMigrations(TestCase): |
@@ -610,3 +616,68 @@ def setUpBeforeMigration(self, apps): |
610 | 616 | def test_removal_of_duped_purls(self): |
611 | 617 | Package = apps.get_model("vulnerabilities", "Package") |
612 | 618 | assert Package.objects.count() == 1 |
| 619 | + |
| 620 | + |
| 621 | +class TestUpdateNpmPypaAdvisoryCreatedByField(TestMigrations): |
| 622 | + app_name = "vulnerabilities" |
| 623 | + migrate_from = "0062_package_is_ghost" |
| 624 | + migrate_to = "0063_update_npm_pypa_advisory_created_by" |
| 625 | + |
| 626 | + advisory_data1 = AdvisoryData( |
| 627 | + aliases=["CVE-2020-13371337"], |
| 628 | + summary="vulnerability description here", |
| 629 | + affected_packages=[ |
| 630 | + AffectedPackage( |
| 631 | + package=PackageURL(type="npm", name="dummy"), |
| 632 | + affected_version_range=VersionRange.from_string("vers:npm/>=1.0.0|<=2.0.0"), |
| 633 | + ) |
| 634 | + ], |
| 635 | + references=[Reference(url="https://example.com/with/more/info/CVE-2020-13371337")], |
| 636 | + date_published=timezone.now(), |
| 637 | + url="https://test.com", |
| 638 | + ) |
| 639 | + advisory_data2 = AdvisoryData( |
| 640 | + aliases=["CVE-2020-1337"], |
| 641 | + summary="vulnerability description here", |
| 642 | + affected_packages=[ |
| 643 | + AffectedPackage( |
| 644 | + package=PackageURL(type="pypi", name="dummy"), |
| 645 | + affected_version_range=VersionRange.from_string("vers:pypi/>=1.0.0|<=2.0.0"), |
| 646 | + ) |
| 647 | + ], |
| 648 | + references=[Reference(url="https://example.com/with/more/info/CVE-2020-1337")], |
| 649 | + date_published=timezone.now(), |
| 650 | + url="https://test2.com", |
| 651 | + ) |
| 652 | + |
| 653 | + def setUpBeforeMigration(self, apps): |
| 654 | + Advisory = apps.get_model("vulnerabilities", "Advisory") |
| 655 | + adv1 = Advisory.objects.create( |
| 656 | + aliases=self.advisory_data1.aliases, |
| 657 | + summary=self.advisory_data1.summary, |
| 658 | + affected_packages=[pkg.to_dict() for pkg in self.advisory_data1.affected_packages], |
| 659 | + references=[ref.to_dict() for ref in self.advisory_data1.references], |
| 660 | + url=self.advisory_data1.url, |
| 661 | + created_by="vulnerabilities.importers.npm.NpmImporter", |
| 662 | + date_collected=timezone.now(), |
| 663 | + ) |
| 664 | + |
| 665 | + adv2 = Advisory.objects.create( |
| 666 | + aliases=self.advisory_data2.aliases, |
| 667 | + summary=self.advisory_data2.summary, |
| 668 | + affected_packages=[pkg.to_dict() for pkg in self.advisory_data2.affected_packages], |
| 669 | + references=[ref.to_dict() for ref in self.advisory_data2.references], |
| 670 | + url=self.advisory_data2.url, |
| 671 | + created_by="vulnerabilities.importers.pypa.PyPaImporter", |
| 672 | + date_collected=timezone.now(), |
| 673 | + ) |
| 674 | + |
| 675 | + def test_removal_of_duped_purls(self): |
| 676 | + Advisory = apps.get_model("vulnerabilities", "Advisory") |
| 677 | + adv = Advisory.objects.all() |
| 678 | + |
| 679 | + assert adv.filter(created_by="vulnerabilities.importers.pypa.PyPaImporter").count() == 0 |
| 680 | + assert adv.filter(created_by="pypa_importer").count() == 1 |
| 681 | + |
| 682 | + assert adv.filter(created_by="vulnerabilities.importers.npm.NpmImporter").count() == 0 |
| 683 | + assert adv.filter(created_by="npm_importer").count() == 1 |
0 commit comments