Skip to content

Commit bc35a0c

Browse files
authored
feat: (AAP-28246) - only delete the event stream when no activations using it (#986)
1 parent 0bc13c3 commit bc35a0c

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

src/aap_eda/api/views/webhook.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,13 @@ def retrieve(self, request, *args, **kwargs):
9393
)
9494
def destroy(self, request, *args, **kwargs):
9595
webhook = self.get_object()
96+
97+
ref_count = webhook.activations.count()
98+
if ref_count > 0:
99+
raise api_exc.Conflict(
100+
f"Event stream '{webhook.name}' is being referenced by "
101+
f"{ref_count} activation(s) and cannot be deleted"
102+
)
96103
self.perform_destroy(webhook)
97104
return Response(status=status.HTTP_204_NO_CONTENT)
98105

tests/integration/api/test_webhook.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,25 @@ def test_delete_webhook(
301301
assert response.status_code == status.HTTP_204_NO_CONTENT
302302

303303

304+
@pytest.mark.django_db
305+
def test_delete_webhook_with_exception(
306+
admin_client: APIClient,
307+
default_activation: models.Activation,
308+
default_webhook: models.Webhook,
309+
):
310+
activation = default_activation
311+
activation.webhooks.add(default_webhook)
312+
313+
response = admin_client.delete(
314+
f"{api_url_v1}/webhooks/{default_webhook.id}/"
315+
)
316+
assert response.status_code == status.HTTP_409_CONFLICT
317+
assert (
318+
f"Event stream '{default_webhook.name}' is being referenced by "
319+
"1 activation(s) and cannot be deleted"
320+
) in response.data["detail"]
321+
322+
304323
@pytest.mark.django_db
305324
def test_post_webhook_with_bad_uuid(
306325
admin_client: APIClient,

0 commit comments

Comments
 (0)