From 9b7dce64cd9fcc9299f4d314bd62cf8727d96eab Mon Sep 17 00:00:00 2001 From: Yulya Artyukhina Date: Wed, 13 Mar 2024 13:25:41 +0100 Subject: [PATCH] Delete connection on channel delete (#4048) # What this PR does Delete alert receive channel connections on alert receive channel delete Related to https://github.com/grafana/oncall-private/issues/2540 ## Checklist - [x] Unit, integration, and e2e (if applicable) tests updated - [x] Documentation added (or `pr:no public docs` PR label added if not required) - [x] Added the relevant release notes label (see labels prefixed w/ `release:`). These labels dictate how your PR will show up in the autogenerated release notes. --- .../alerts/models/alert_receive_channel.py | 3 ++ .../api/tests/test_alert_receive_channel.py | 53 +++++++++++++++++++ 2 files changed, 56 insertions(+) diff --git a/engine/apps/alerts/models/alert_receive_channel.py b/engine/apps/alerts/models/alert_receive_channel.py index c56f441d..53b07da4 100644 --- a/engine/apps/alerts/models/alert_receive_channel.py +++ b/engine/apps/alerts/models/alert_receive_channel.py @@ -777,6 +777,9 @@ def listen_for_alertreceivechannel_model_save( elif instance.deleted_at: if instance.is_alerting_integration: disconnect_integration_from_alerting_contact_points.apply_async((instance.pk,), countdown=5) + # delete alert receive channel connections + instance.connected_alert_receive_channels.all().delete() + instance.source_alert_receive_channels.all().delete() metrics_remove_deleted_integration_from_cache(instance) else: diff --git a/engine/apps/api/tests/test_alert_receive_channel.py b/engine/apps/api/tests/test_alert_receive_channel.py index 786190ba..ad6c7957 100644 --- a/engine/apps/api/tests/test_alert_receive_channel.py +++ b/engine/apps/api/tests/test_alert_receive_channel.py @@ -2213,3 +2213,56 @@ def test_connected_alert_receive_channels_delete( source_alert_receive_channel.connected_alert_receive_channels.first().connected_alert_receive_channel == connected_alert_receive_channel_2 ) + + +@pytest.mark.django_db +def test_delete_connection_on_channel_delete( + make_organization_and_user_with_plugin_token, + make_alert_receive_channel, + make_alert_receive_channel_connection, + make_user_auth_headers, +): + organization, user, token = make_organization_and_user_with_plugin_token() + + source_alert_receive_channel = make_alert_receive_channel(organization) + connected_alert_receive_channel_1 = make_alert_receive_channel(organization) + connected_alert_receive_channel_2 = make_alert_receive_channel(organization) + + make_alert_receive_channel_connection(source_alert_receive_channel, connected_alert_receive_channel_1) + make_alert_receive_channel_connection(source_alert_receive_channel, connected_alert_receive_channel_2) + + client = APIClient() + source_integration_url = reverse( + "api-internal:alert_receive_channel-connected-alert-receive-channels-get", + kwargs={ + "pk": source_alert_receive_channel.public_primary_key, + }, + ) + connected_integration_url = reverse( + "api-internal:alert_receive_channel-connected-alert-receive-channels-get", + kwargs={ + "pk": connected_alert_receive_channel_1.public_primary_key, + }, + ) + response = client.get(source_integration_url, **make_user_auth_headers(user, token)) + assert len(response.json()["connected_alert_receive_channels"]) == 2 + # delete connected integration + connected_alert_receive_channel_2.delete() + + response = client.get(source_integration_url, **make_user_auth_headers(user, token)) + assert len(response.json()["connected_alert_receive_channels"]) == 1 + assert ( + response.json()["connected_alert_receive_channels"][0]["alert_receive_channel"]["id"] + == connected_alert_receive_channel_1.public_primary_key + ) + # delete source integration + response = client.get(connected_integration_url, **make_user_auth_headers(user, token)) + assert len(response.json()["source_alert_receive_channels"]) == 1 + assert ( + response.json()["source_alert_receive_channels"][0]["alert_receive_channel"]["id"] + == source_alert_receive_channel.public_primary_key + ) + source_alert_receive_channel.delete() + + response = client.get(connected_integration_url, **make_user_auth_headers(user, token)) + assert len(response.json()["source_alert_receive_channels"]) == 0