|
23 | 23 | from vulnerabilities.models import Alias |
24 | 24 | from vulnerabilities.models import ApiUser |
25 | 25 | from vulnerabilities.models import CodeFixV2 |
| 26 | +from vulnerabilities.models import ImpactedPackage |
26 | 27 | from vulnerabilities.models import Package |
| 28 | +from vulnerabilities.models import PackageCommitPatch |
27 | 29 | from vulnerabilities.models import PackageV2 |
| 30 | +from vulnerabilities.models import Patch |
28 | 31 | from vulnerabilities.models import PipelineRun |
29 | 32 | from vulnerabilities.models import PipelineSchedule |
30 | 33 | from vulnerabilities.models import Vulnerability |
@@ -905,3 +908,154 @@ def test_get_all_vulnerable_purls(self): |
905 | 908 | response = self.client.get(url) |
906 | 909 | assert response.status_code == 200 |
907 | 910 | assert "pkg:pypi/sample@1.0.0" in response.data |
| 911 | + |
| 912 | + |
| 913 | +class PackageCommitPatchList(APITestCase): |
| 914 | + def setUp(self): |
| 915 | + self.advisory = AdvisoryV2.objects.create( |
| 916 | + datasource_id="test_source", |
| 917 | + advisory_id="TEST-2025-001", |
| 918 | + avid="test_source/TEST-2025-001", |
| 919 | + unique_content_id="a" * 64, |
| 920 | + url="https://example.com/advisory", |
| 921 | + date_collected="2025-07-01T00:00:00Z", |
| 922 | + ) |
| 923 | + |
| 924 | + self.affected_package = PackageV2.objects.from_purl(purl="pkg:github/torvalds/linux@1.0.0") |
| 925 | + self.fixed_package = PackageV2.objects.from_purl(purl="pkg:github/torvalds/linux@1.0.1") |
| 926 | + |
| 927 | + self.pkg_commit_patch1 = PackageCommitPatch.objects.create( |
| 928 | + commit_hash="2e1c42391ff2556387b3cb6308b24f6f65619feb", |
| 929 | + vcs_url="https://github.com/torvalds/linux", |
| 930 | + patch_text="From 2e1c42391ff2556387b3cb6308b24f6f65619feb Mon Sep 17 00:00:00 2001...", |
| 931 | + ) |
| 932 | + |
| 933 | + self.pkg_commit_patch2 = PackageCommitPatch.objects.create( |
| 934 | + commit_hash="99253eb750fda6a644d5188fb26c43bad8d5a745", |
| 935 | + vcs_url="https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git", |
| 936 | + patch_text="From 99253eb750fda6a644d5188fb26c43bad8d5a745 Mon Sep 17 00:00:00 2001...", |
| 937 | + ) |
| 938 | + |
| 939 | + self.pkg_commit_patch3 = PackageCommitPatch.objects.create( |
| 940 | + commit_hash="f043bfc98c193c284e2cd768fefabe18ac2fed9b", |
| 941 | + vcs_url="https://github.com/torvalds/linux", |
| 942 | + patch_text="From f043bfc98c193c284e2cd768fefabe18ac2fed9b Mon Sep 17 00:00:00 2001...", |
| 943 | + ) |
| 944 | + |
| 945 | + self.impacted_package1 = ImpactedPackage.objects.create( |
| 946 | + base_purl="pkg:github/torvalds/linux", |
| 947 | + advisory=self.advisory, |
| 948 | + ) |
| 949 | + |
| 950 | + self.impacted_package2 = ImpactedPackage.objects.create( |
| 951 | + base_purl="pkg:generic/git.kernel.org/pub/scm/linux/kernel", |
| 952 | + advisory=self.advisory, |
| 953 | + ) |
| 954 | + |
| 955 | + self.impacted_package1.fixed_by_package_commit_patches.add(self.pkg_commit_patch1) |
| 956 | + self.impacted_package1.introduced_by_package_commit_patches.add(self.pkg_commit_patch3) |
| 957 | + self.impacted_package2.fixed_by_package_commit_patches.add(self.pkg_commit_patch2) |
| 958 | + |
| 959 | + self.user = ApiUser.objects.create_api_user(username="e@mail.com") |
| 960 | + self.auth = f"Token {self.user.auth_token.key}" |
| 961 | + self.client = APIClient(enforce_csrf_checks=True) |
| 962 | + self.client.credentials(HTTP_AUTHORIZATION=self.auth) |
| 963 | + self.url = reverse("package-commit-patch-list") |
| 964 | + |
| 965 | + def test_package_commit_patches_list(self): |
| 966 | + response = self.client.get(self.url) |
| 967 | + assert response.status_code == 200 |
| 968 | + results = response.json().get("results", response.json()) |
| 969 | + assert len(results) == 3 |
| 970 | + patch_data = results[0] |
| 971 | + assert patch_data["vcs_url"] == self.pkg_commit_patch1.vcs_url |
| 972 | + assert patch_data["commit_hash"] == self.pkg_commit_patch1.commit_hash |
| 973 | + assert patch_data["fixed_in_advisories"] == [ |
| 974 | + {"avid": self.advisory.avid, "purl": self.impacted_package1.base_purl} |
| 975 | + ] |
| 976 | + assert patch_data["introduced_in_advisories"] == [] |
| 977 | + |
| 978 | + def test_filter_by_commit_hash(self): |
| 979 | + response = self.client.get(f"{self.url}?commit_hash={self.pkg_commit_patch1.commit_hash}") |
| 980 | + results = response.json().get("results", response.json()) |
| 981 | + assert len(results) == 1 |
| 982 | + |
| 983 | + response = self.client.get(f"{self.url}?commit_hash=test") |
| 984 | + results = response.json().get("results", response.json()) |
| 985 | + assert len(results) == 0 |
| 986 | + |
| 987 | + def test_filter_by_vcs_url(self): |
| 988 | + response = self.client.get(f"{self.url}?vcs_url={self.pkg_commit_patch1.vcs_url}") |
| 989 | + results = response.json().get("results", response.json()) |
| 990 | + assert len(results) == 2 |
| 991 | + |
| 992 | + response = self.client.get(f"{self.url}?vcs_url=test") |
| 993 | + results = response.json().get("results", response.json()) |
| 994 | + assert len(results) == 0 |
| 995 | + |
| 996 | + def test_filter_by_advisory_avid(self): |
| 997 | + response = self.client.get(f"{self.url}?advisory_avid={self.advisory.avid}") |
| 998 | + results = response.json().get("results", response.json()) |
| 999 | + assert len(results) == 3 |
| 1000 | + |
| 1001 | + response = self.client.get(f"{self.url}?advisory_avid=test_source/DOES-NOT-EXIST") |
| 1002 | + results = response.json().get("results", response.json()) |
| 1003 | + assert len(results) == 0 |
| 1004 | + |
| 1005 | + def test_filter_by_purl(self): |
| 1006 | + response = self.client.get(f"{self.url}?purl=pkg:github/torvalds/linux") |
| 1007 | + results = response.json().get("results", response.json()) |
| 1008 | + assert len(results) == 2 |
| 1009 | + assert any(r["id"] == self.pkg_commit_patch1.id for r in results) |
| 1010 | + |
| 1011 | + response = self.client.get(f"{self.url}?purl=pkg:github/aboutcode-org") |
| 1012 | + results = response.json().get("results", response.json()) |
| 1013 | + assert len(results) == 0 |
| 1014 | + |
| 1015 | + def test_filter_by_id(self): |
| 1016 | + response = self.client.get(f"{self.url}?id={self.pkg_commit_patch1.id}") |
| 1017 | + results = response.json().get("results", response.json()) |
| 1018 | + assert len(results) == 1 |
| 1019 | + assert results[0]["id"] == self.pkg_commit_patch1.id |
| 1020 | + |
| 1021 | + response = self.client.get(f"{self.url}?id=51646849") |
| 1022 | + results = response.json().get("results", response.json()) |
| 1023 | + assert len(results) == 0 |
| 1024 | + |
| 1025 | + |
| 1026 | +class PatchList(APITestCase): |
| 1027 | + def setUp(self): |
| 1028 | + self.advisory = AdvisoryV2.objects.create( |
| 1029 | + datasource_id="test_source", |
| 1030 | + advisory_id="TEST-2025-001", |
| 1031 | + avid="test_source/TEST-2025-001", |
| 1032 | + unique_content_id="a" * 64, |
| 1033 | + url="https://example.com/advisory", |
| 1034 | + date_collected="2025-07-01T00:00:00Z", |
| 1035 | + ) |
| 1036 | + |
| 1037 | + self.patch = Patch.objects.create( |
| 1038 | + patch_url="https://lore.kernel.org/patchwork/patch/1086060/", patch_text="some text" |
| 1039 | + ) |
| 1040 | + |
| 1041 | + self.advisory.patches.add(self.patch) |
| 1042 | + |
| 1043 | + self.user = ApiUser.objects.create_api_user(username="e@mail.com") |
| 1044 | + self.auth = f"Token {self.user.auth_token.key}" |
| 1045 | + self.client = APIClient(enforce_csrf_checks=True) |
| 1046 | + self.client.credentials(HTTP_AUTHORIZATION=self.auth) |
| 1047 | + self.url = reverse("patches-list") |
| 1048 | + |
| 1049 | + def test_patch_list(self): |
| 1050 | + response = self.client.get(self.url) |
| 1051 | + assert response.status_code == 200 |
| 1052 | + results = response.json().get("results", response.json()) |
| 1053 | + assert len(results) == 1 |
| 1054 | + assert results[0]["patch_url"] == self.patch.patch_url |
| 1055 | + assert results == [ |
| 1056 | + { |
| 1057 | + "id": 1, |
| 1058 | + "patch_url": "https://lore.kernel.org/patchwork/patch/1086060/", |
| 1059 | + "in_advisories": ["test_source/TEST-2025-001"], |
| 1060 | + } |
| 1061 | + ] |
0 commit comments