diff --git a/dojo/url/api/views.py b/dojo/url/api/views.py index 816e30b60a5..caebd867bec 100644 --- a/dojo/url/api/views.py +++ b/dojo/url/api/views.py @@ -36,3 +36,6 @@ def get_queryset(self) -> QuerySet[URL]: return URL.objects.annotate( active_findings=Coalesce(active_finding_subquery, Value(0)), ) + + def perform_destroy(self, instance): + instance.location.delete() diff --git a/unittests/test_rest_framework.py b/unittests/test_rest_framework.py index cd76daa0627..fc8f2e588f8 100644 --- a/unittests/test_rest_framework.py +++ b/unittests/test_rest_framework.py @@ -1612,6 +1612,19 @@ def test_update_object_not_authorized(self): response = self.client.put(relative_url, self.payload) self.assertEqual(403, response.status_code, response.content[:1000]) + def test_delete_removes_location(self): + """Verify that deleting a URL via the API also deletes the associated Location.""" + url_obj = URL.objects.get(pk=self.delete_id) + location_id = url_obj.location_id + self.assertTrue(Location.objects.filter(pk=location_id).exists()) + + relative_url = f"{self.url}{location_id}/" + response = self.client.delete(relative_url) + self.assertEqual(204, response.status_code, response.content[:1000]) + + self.assertFalse(Location.objects.filter(pk=location_id).exists()) + self.assertFalse(URL.objects.filter(pk=self.delete_id).exists()) + @versioned_fixtures class EngagementTest(BaseClass.RelatedObjectsTest, BaseClass.BaseClassTest):