|
20 | 20 | from vulnerabilities.models import VulnerabilityAnalysis |
21 | 21 | from vulnerabilities.tests import make_vulnerability |
22 | 22 | from vulnerabilities.tests import make_vulnerability_analysis |
| 23 | +from vulnerabilities.triage.engine import apply_preset_for_vulnerabilities |
| 24 | +from vulnerabilities.triage.models import AnalysisPreset |
| 25 | +from vulnerabilities.triage.tests import make_analysis_preset |
23 | 26 |
|
24 | 27 |
|
25 | 28 | class VulnerabilitiesAPITestCase(MaxQueryMixin, TestCase): |
@@ -154,6 +157,49 @@ def test_api_vulnerability_analysis_detail_endpoint(self): |
154 | 157 | self.assertEqual(str(analysis1.uuid), response.data["uuid"]) |
155 | 158 | self.assertTrue(response.data["is_reachable"]) |
156 | 159 |
|
| 160 | + def test_api_vulnerability_analysis_detail_endpoint_applied_by_preset_and_authors(self): |
| 161 | + self.client.login(username="super_user", password="secret") |
| 162 | + |
| 163 | + human_analysis = make_vulnerability_analysis( |
| 164 | + self.product_package1, |
| 165 | + self.vulnerability1, |
| 166 | + created_by=self.super_user, |
| 167 | + last_modified_by=self.super_user, |
| 168 | + ) |
| 169 | + detail_url = reverse("api_v2:vulnerabilityanalysis-detail", args=[human_analysis.uuid]) |
| 170 | + response = self.client.get(detail_url) |
| 171 | + self.assertIsNone(response.data["applied_by_preset"]) |
| 172 | + self.assertEqual(self.super_user.username, response.data["created_by"]) |
| 173 | + self.assertEqual(self.super_user.username, response.data["last_modified_by"]) |
| 174 | + |
| 175 | + preset = make_analysis_preset(self.dataspace, state=AnalysisPreset.State.NOT_AFFECTED) |
| 176 | + apply_preset_for_vulnerabilities(preset, self.product1, [self.vulnerability2.pk]) |
| 177 | + auto_analysis = VulnerabilityAnalysis.objects.get(vulnerability=self.vulnerability2) |
| 178 | + detail_url = reverse("api_v2:vulnerabilityanalysis-detail", args=[auto_analysis.uuid]) |
| 179 | + |
| 180 | + response = self.client.get(detail_url) |
| 181 | + self.assertEqual(preset.name, response.data["applied_by_preset"]) |
| 182 | + self.assertIsNone(response.data["created_by"]) |
| 183 | + self.assertIsNone(response.data["last_modified_by"]) |
| 184 | + |
| 185 | + def test_api_vulnerability_analysis_list_endpoint_filters_applied_by_preset(self): |
| 186 | + self.client.login(username="super_user", password="secret") |
| 187 | + make_vulnerability_analysis(self.product_package1, self.vulnerability1) |
| 188 | + preset = make_analysis_preset(self.dataspace, state=AnalysisPreset.State.NOT_AFFECTED) |
| 189 | + apply_preset_for_vulnerabilities(preset, self.product1, [self.vulnerability2.pk]) |
| 190 | + |
| 191 | + data = {"applied_by_preset__isnull": "true"} |
| 192 | + response = self.client.get(self.analysis_list_url, data) |
| 193 | + self.assertEqual(1, response.data["count"]) |
| 194 | + self.assertContains(response, self.vulnerability1.advisory_id) |
| 195 | + self.assertNotContains(response, self.vulnerability2.advisory_id) |
| 196 | + |
| 197 | + data = {"applied_by_preset__isnull": "false"} |
| 198 | + response = self.client.get(self.analysis_list_url, data) |
| 199 | + self.assertEqual(1, response.data["count"]) |
| 200 | + self.assertNotContains(response, self.vulnerability1.advisory_id) |
| 201 | + self.assertContains(response, self.vulnerability2.advisory_id) |
| 202 | + |
157 | 203 | def test_api_vulnerability_analysis_endpoint_create(self): |
158 | 204 | self.client.login(username="super_user", password="secret") |
159 | 205 | response = self.client.post(self.analysis_list_url) |
|
0 commit comments