|
1 | 1 | import logging |
| 2 | +from unittest import mock |
2 | 3 |
|
3 | 4 | from django.utils import timezone |
4 | 5 |
|
|
12 | 13 |
|
13 | 14 |
|
14 | 15 | class TestDojoCloseOld(DojoTestCase): |
| 16 | + # Regression: close_old_findings fetched every Finding column (incl. description) via |
| 17 | + # findings.values() just to collect is_mitigated/hash_code/unique_id_from_tool. |
| 18 | + def test_close_old_findings_only_fetches_needed_columns(self): |
| 19 | + scan_type = "Acunetix Scan" |
| 20 | + user, _ = User.objects.get_or_create(username="admin") |
| 21 | + product_type, _ = Product_Type.objects.get_or_create(name="closeold") |
| 22 | + environment, _ = Development_Environment.objects.get_or_create(name="Development") |
| 23 | + product, _ = Product.objects.get_or_create( |
| 24 | + name="TestDojoCloseOldColumns", |
| 25 | + description="Test", |
| 26 | + prod_type=product_type, |
| 27 | + ) |
| 28 | + engagement, _ = Engagement.objects.get_or_create( |
| 29 | + name="Close Old Needed Columns", |
| 30 | + product=product, |
| 31 | + target_start=timezone.now(), |
| 32 | + target_end=timezone.now(), |
| 33 | + ) |
| 34 | + import_options = { |
| 35 | + "user": user, |
| 36 | + "lead": user, |
| 37 | + "scan_date": None, |
| 38 | + "environment": environment, |
| 39 | + "active": True, |
| 40 | + "verified": False, |
| 41 | + "engagement": engagement, |
| 42 | + "scan_type": scan_type, |
| 43 | + } |
| 44 | + # Seed an older test with 4 findings that the close-old pass should mitigate |
| 45 | + with (get_unit_tests_scans_path("acunetix") / "many_findings.xml").open(encoding="utf-8") as many_findings_scan: |
| 46 | + importer = DefaultImporter(close_old_findings=False, **import_options) |
| 47 | + _, _, len_new_findings, _, _, _, _ = importer.process_scan(many_findings_scan) |
| 48 | + self.assertEqual(4, len_new_findings) |
| 49 | + # Import a report with a single finding, without closing, to get the new test |
| 50 | + with (get_unit_tests_scans_path("acunetix") / "one_finding.xml").open(encoding="utf-8") as single_finding_scan: |
| 51 | + importer = DefaultImporter(close_old_findings=False, **import_options) |
| 52 | + test, _, len_new_findings, _, _, _, _ = importer.process_scan(single_finding_scan) |
| 53 | + self.assertEqual(1, len_new_findings) |
| 54 | + # Call close_old_findings directly with the new test's queryset and spy on values() |
| 55 | + importer = DefaultImporter(close_old_findings=True, **import_options) |
| 56 | + importer.test = test |
| 57 | + findings_queryset = test.finding_set.all() |
| 58 | + with mock.patch.object(findings_queryset, "values", wraps=findings_queryset.values) as values_spy: |
| 59 | + closed_findings = importer.close_old_findings(findings_queryset) |
| 60 | + values_spy.assert_called_once_with( |
| 61 | + "is_mitigated", "hash_code", "unique_id_from_tool", |
| 62 | + ) |
| 63 | + # Control: narrowing the columns must not change which findings get closed |
| 64 | + # (the 4 findings from the older test; dedupe is off, so the overlapping |
| 65 | + # finding closes too — same semantics as test_close_old_same_engagement) |
| 66 | + self.assertEqual( |
| 67 | + 4, len(closed_findings), |
| 68 | + msg=f"expected 4 old findings closed, got {len(closed_findings)}", |
| 69 | + ) |
| 70 | + |
15 | 71 | def test_close_old_same_engagement(self): |
16 | 72 | scan_type = "Acunetix Scan" |
17 | 73 | user, _ = User.objects.get_or_create(username="admin") |
|
0 commit comments