Skip to content

Commit b127bb1

Browse files
authored
when deleting a URL via API, perform_delete should call delete on the… (#14612)
* when deleting a URL via API, perform_delete should call delete on the abstractlocation instance (cascades down) * add tests for it
1 parent 4a3ee14 commit b127bb1

2 files changed

Lines changed: 16 additions & 0 deletions

File tree

dojo/url/api/views.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,6 @@ def get_queryset(self) -> QuerySet[URL]:
3636
return URL.objects.annotate(
3737
active_findings=Coalesce(active_finding_subquery, Value(0)),
3838
)
39+
40+
def perform_destroy(self, instance):
41+
instance.location.delete()

unittests/test_rest_framework.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1612,6 +1612,19 @@ def test_update_object_not_authorized(self):
16121612
response = self.client.put(relative_url, self.payload)
16131613
self.assertEqual(403, response.status_code, response.content[:1000])
16141614

1615+
def test_delete_removes_location(self):
1616+
"""Verify that deleting a URL via the API also deletes the associated Location."""
1617+
url_obj = URL.objects.get(pk=self.delete_id)
1618+
location_id = url_obj.location_id
1619+
self.assertTrue(Location.objects.filter(pk=location_id).exists())
1620+
1621+
relative_url = f"{self.url}{location_id}/"
1622+
response = self.client.delete(relative_url)
1623+
self.assertEqual(204, response.status_code, response.content[:1000])
1624+
1625+
self.assertFalse(Location.objects.filter(pk=location_id).exists())
1626+
self.assertFalse(URL.objects.filter(pk=self.delete_id).exists())
1627+
16151628

16161629
@versioned_fixtures
16171630
class EngagementTest(BaseClass.RelatedObjectsTest, BaseClass.BaseClassTest):

0 commit comments

Comments
 (0)