Skip to content

Commit b22dfb1

Browse files
Jino-TMaffooch
andauthored
Add test to ensure duplicate findings are deleted in the proper order (#14256)
* added testing to ensure duplicate findings are deleted in the correct order * fix ruff issues * Update unittests/test_duplication_loops.py --------- Co-authored-by: Cody Maffucci <46459665+Maffooch@users.noreply.github.com>
1 parent 8856a94 commit b22dfb1

1 file changed

Lines changed: 34 additions & 1 deletion

File tree

unittests/test_duplication_loops.py

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55

66
from dojo.finding.deduplication import set_duplicate
77
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
910

1011
from .dojo_test_case import DojoTestCase, versioned_fixtures
1112

@@ -374,6 +375,38 @@ def test_list_relations_for_three_reverse(self):
374375
self.assertEqual(self.finding_c.duplicate_finding_set().count(), 2)
375376
self.assertEqual(self.finding_b.duplicate_finding_set().count(), 2)
376377

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+
377410
def test_delete_all_engagements(self):
378411
# make sure there is no exception when deleting all engagements
379412
for engagement in Engagement.objects.all().order_by("id"):

0 commit comments

Comments
 (0)