|
23 | 23 | from vulnerabilities.importer import PackageCommitPatchData |
24 | 24 | from vulnerabilities.models import AdvisorySet |
25 | 25 | from vulnerabilities.models import AdvisorySetMember |
| 26 | +from vulnerabilities.models import AdvisoryToDoV2 |
26 | 27 | from vulnerabilities.models import ImpactedPackage |
27 | 28 | from vulnerabilities.models import ImpactedPackageAffecting |
28 | 29 | from vulnerabilities.models import PackageV2 |
| 30 | +from vulnerabilities.pipelines.v2_improvers.group_advisories_for_packages import ( |
| 31 | + GroupAdvisoriesForPackages, |
| 32 | +) |
29 | 33 | from vulnerabilities.pipes.advisory import insert_advisory_v2 |
30 | 34 | from vulnerabilities.tests.pipelines import TestLogger |
31 | 35 |
|
@@ -241,7 +245,7 @@ def test_advisories_post(self): |
241 | 245 | self.assertEqual(response.status_code, status.HTTP_200_OK) |
242 | 246 | self.assertEqual(len(response.data["results"]), 100) |
243 | 247 | advisory = response.data["results"][0] |
244 | | - self.assertEqual(advisory["advisory_id"], "GHSA-12341") |
| 248 | + self.assertContains(response, "GHSA-12341") |
245 | 249 |
|
246 | 250 |
|
247 | 251 | class APIV3TestCaseOneAdvisoryMultiplePackages(APITestCase): |
@@ -606,3 +610,134 @@ def test_returns_distinct_types_and_caches_response(self): |
606 | 610 | assert response.json() == ["npm", "pypi"] |
607 | 611 |
|
608 | 612 | assert cache.get("package_types") == ["npm", "pypi"] |
| 613 | + |
| 614 | + |
| 615 | +class APIV3TestCaseAdvisoryTODO(APITestCase): |
| 616 | + def setUp(self): |
| 617 | + logger = TestLogger() |
| 618 | + from vulnerabilities.importer import AdvisoryDataV2 |
| 619 | + from vulnerabilities.importer import AffectedPackageV2 |
| 620 | + |
| 621 | + affected_packages1 = [ |
| 622 | + ( |
| 623 | + AffectedPackageV2( |
| 624 | + package=PackageURL(type="pypi", name=f"sample"), |
| 625 | + fixed_version_range=PypiVersionRange.from_string("vers:pypi/=1.0.0"), |
| 626 | + ) |
| 627 | + ) |
| 628 | + ] |
| 629 | + |
| 630 | + advisory1 = AdvisoryDataV2( |
| 631 | + advisory_id="GHSA-4321", |
| 632 | + aliases=["CVE-2021-4321"], |
| 633 | + summary="Sample advisory", |
| 634 | + affected_packages=affected_packages1, |
| 635 | + url="https://example.com/advisory", |
| 636 | + original_advisory_text="Sample advisory text", |
| 637 | + ) |
| 638 | + advisory_obj1 = insert_advisory_v2(advisory1, "ghsa_importer", logger.write, "ghsa", 100) |
| 639 | + cur = timezone.now() |
| 640 | + advisory_obj1._all_impacts_unfurled_at = cur |
| 641 | + advisory_obj1.save() |
| 642 | + |
| 643 | + affected_packages = [ |
| 644 | + ( |
| 645 | + AffectedPackageV2( |
| 646 | + package=PackageURL(type="pypi", name=f"sample"), |
| 647 | + affected_version_range=PypiVersionRange.from_string("vers:pypi/=1.0.0"), |
| 648 | + ) |
| 649 | + ) |
| 650 | + ] |
| 651 | + |
| 652 | + advisory = AdvisoryDataV2( |
| 653 | + advisory_id="GHSA-1234", |
| 654 | + aliases=["CVE-2021-1234"], |
| 655 | + summary="Sample advisory", |
| 656 | + affected_packages=affected_packages, |
| 657 | + url="https://example.com/advisory", |
| 658 | + original_advisory_text="Sample advisory text", |
| 659 | + ) |
| 660 | + |
| 661 | + advisory_obj = insert_advisory_v2(advisory, "ghsa_importer", logger.write, "ghsa", 100) |
| 662 | + cur = timezone.now() |
| 663 | + advisory_obj._all_impacts_unfurled_at = cur |
| 664 | + advisory_obj.save() |
| 665 | + |
| 666 | + todo = AdvisoryToDoV2.objects.create( |
| 667 | + related_advisories_id="abrakadabra", |
| 668 | + alias="GHSA-1234", |
| 669 | + advisories_count=1, |
| 670 | + issue_type="CONFLICTING_AFFECTED_PACKAGES", |
| 671 | + ) |
| 672 | + todo.advisories.add(advisory_obj) |
| 673 | + |
| 674 | + GroupAdvisoriesForPackages().execute() |
| 675 | + |
| 676 | + self.client = APIClient(enforce_csrf_checks=True) |
| 677 | + |
| 678 | + self.allow_request_patcher = patch( |
| 679 | + "vulnerabilities.throttling.PermissionBasedUserRateThrottle.allow_request", |
| 680 | + return_value=True, |
| 681 | + ) |
| 682 | + self.allow_request_patcher.start() |
| 683 | + self.addCleanup(self.allow_request_patcher.stop) |
| 684 | + |
| 685 | + self.anon_patcher = patch( |
| 686 | + "rest_framework.throttling.AnonRateThrottle.allow_request", |
| 687 | + return_value=True, |
| 688 | + ) |
| 689 | + self.anon_patcher.start() |
| 690 | + self.addCleanup(self.anon_patcher.stop) |
| 691 | + |
| 692 | + def test_get_todo_count_in_package_endpoint(self): |
| 693 | + url = reverse("package-v3-list") |
| 694 | + |
| 695 | + with self.assertNumQueries(13): |
| 696 | + response = self.client.post( |
| 697 | + url, |
| 698 | + data={ |
| 699 | + "purls": ["pkg:pypi/sample@1.0.0"], |
| 700 | + "details": True, |
| 701 | + }, |
| 702 | + format="json", |
| 703 | + HTTP_USER_AGENT="VCIO_API_AGENT", |
| 704 | + ) |
| 705 | + |
| 706 | + self.assertEqual(response.status_code, status.HTTP_200_OK) |
| 707 | + |
| 708 | + results = response.data["results"] |
| 709 | + self.assertEqual(results[0]["affected_by_vulnerabilities"][0]["todo_count"], 1) |
| 710 | + |
| 711 | + def test_get_todo_count_in_affected_by_advisory_endpoint(self): |
| 712 | + url = reverse("affected-by-advisories-list") |
| 713 | + with self.assertNumQueries(11): |
| 714 | + response = self.client.get( |
| 715 | + url, |
| 716 | + data={ |
| 717 | + "purl": "pkg:pypi/sample@1.0.0", |
| 718 | + }, |
| 719 | + HTTP_USER_AGENT="VCIO_API_AGENT", |
| 720 | + ) |
| 721 | + |
| 722 | + self.assertEqual(response.status_code, status.HTTP_200_OK) |
| 723 | + |
| 724 | + results = response.data["results"] |
| 725 | + self.assertEqual(results[0]["todo_count"], 1) |
| 726 | + |
| 727 | + def test_get_todo_count_in_advisory_endpoint(self): |
| 728 | + url = reverse("fixing-advisories-list") |
| 729 | + |
| 730 | + with self.assertNumQueries(10): |
| 731 | + response = self.client.get( |
| 732 | + url, |
| 733 | + data={ |
| 734 | + "purl": "pkg:pypi/sample@1.0.0", |
| 735 | + }, |
| 736 | + HTTP_USER_AGENT="VCIO_API_AGENT", |
| 737 | + ) |
| 738 | + |
| 739 | + self.assertEqual(response.status_code, status.HTTP_200_OK) |
| 740 | + |
| 741 | + results = response.data["results"] |
| 742 | + print(results) |
| 743 | + self.assertEqual(results[0]["todo_count"], 0) |
0 commit comments