|
5 | 5 |
|
6 | 6 | from dojo.finding.deduplication import set_duplicate |
7 | 7 | from dojo.management.commands.fix_loop_duplicates import fix_loop_duplicates |
8 | | -from dojo.models import Engagement, Finding, Product, User, copy_model_util |
| 8 | +from dojo.models import Engagement, Finding, Product, System_Settings, User, copy_model_util |
| 9 | +from dojo.tasks import _async_dupe_delete_impl # noqa: PLC2701 |
9 | 10 |
|
10 | 11 | from .dojo_test_case import DojoTestCase, versioned_fixtures |
11 | 12 |
|
@@ -374,6 +375,38 @@ def test_list_relations_for_three_reverse(self): |
374 | 375 | self.assertEqual(self.finding_c.duplicate_finding_set().count(), 2) |
375 | 376 | self.assertEqual(self.finding_b.duplicate_finding_set().count(), 2) |
376 | 377 |
|
| 378 | + # Test that Delete Duplicate Findings & Maximum Duplicate is correctly deleting olding finding first based off of finding date value |
| 379 | + def test_delete_duplicate_order(self): |
| 380 | + # Turn on delete duplicates and set the maximum dedupe value to 1 |
| 381 | + system_settings = System_Settings.objects.get() |
| 382 | + system_settings.delete_duplicates = True |
| 383 | + system_settings.max_dupes = 1 |
| 384 | + system_settings.save() |
| 385 | + |
| 386 | + # Add dates to finding b and c |
| 387 | + self.finding_b.date = "2024-01-01" |
| 388 | + self.finding_c.date = "2023-01-01" |
| 389 | + # Make findings b and c duplicates of a |
| 390 | + set_duplicate(self.finding_b, self.finding_a) |
| 391 | + set_duplicate(self.finding_c, self.finding_a) |
| 392 | + |
| 393 | + # Perform delete dedupe logic |
| 394 | + _async_dupe_delete_impl() |
| 395 | + |
| 396 | + # Finding C (2023) should be deleted |
| 397 | + self.assertFalse( |
| 398 | + Finding.objects.filter(id=self.finding_c.id).exists(), |
| 399 | + "The oldest duplicate (Finding c) should have been deleted.", |
| 400 | + ) |
| 401 | + # Finding b (2024) should exist |
| 402 | + self.assertTrue( |
| 403 | + Finding.objects.filter(id=self.finding_b.id).exists(), |
| 404 | + "The newest duplicate (Finding B) should still exist.", |
| 405 | + ) |
| 406 | + # Finding A should now only have 1 duplicate in its set |
| 407 | + self.finding_a.refresh_from_db() |
| 408 | + self.assertEqual(self.finding_a.duplicate_finding_set().count(), 1) |
| 409 | + |
377 | 410 | def test_delete_all_engagements(self): |
378 | 411 | # make sure there is no exception when deleting all engagements |
379 | 412 | for engagement in Engagement.objects.all().order_by("id"): |
|
0 commit comments