From 6d9d58d0dbcf3dd8ecc73056a2092be283c30435 Mon Sep 17 00:00:00 2001 From: Vadim Stepanov Date: Thu, 7 Mar 2024 17:53:44 +0000 Subject: [PATCH] Add `id_ne` filter for integrations (internal API) (#4032) # What this PR does Adds a new multiple choice `id_ne` (ID not equal) filter to internal API integrations endpoint. ## Which issue(s) this PR closes 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. --- .../api/tests/test_alert_receive_channel.py | 31 +++++++++++++++++++ .../apps/api/views/alert_receive_channel.py | 6 ++++ 2 files changed, 37 insertions(+) diff --git a/engine/apps/api/tests/test_alert_receive_channel.py b/engine/apps/api/tests/test_alert_receive_channel.py index 3ad4a8e9..2b3dba1d 100644 --- a/engine/apps/api/tests/test_alert_receive_channel.py +++ b/engine/apps/api/tests/test_alert_receive_channel.py @@ -57,6 +57,37 @@ def test_get_alert_receive_channel_by_integration_ne( assert result["integration"] != AlertReceiveChannel.INTEGRATION_DIRECT_PAGING +@pytest.mark.django_db +def test_get_alert_receive_channel_by_id_ne( + make_organization_and_user_with_plugin_token, make_organization, make_user_auth_headers, make_alert_receive_channel +): + organization, user, token = make_organization_and_user_with_plugin_token() + + alert_receive_channel_1 = make_alert_receive_channel(organization) + alert_receive_channel_2 = make_alert_receive_channel(organization) + alert_receive_channel_3 = make_alert_receive_channel(organization) + + # integration in a different org + organization = make_organization() + alert_receive_channel_4 = make_alert_receive_channel(organization) + + client = APIClient() + url = ( + reverse("api-internal:alert_receive_channel-list") + + f"?id_ne={alert_receive_channel_1.public_primary_key}&id_ne={alert_receive_channel_2.public_primary_key}" + ) + response = client.get(url, **make_user_auth_headers(user, token)) + + assert response.status_code == status.HTTP_200_OK + assert len(response.json()["results"]) == 1 + assert response.json()["results"][0]["id"] == alert_receive_channel_3.public_primary_key + + # integration in a different org shouldn't work + url = reverse("api-internal:alert_receive_channel-list") + f"?id_ne={alert_receive_channel_4.public_primary_key}" + response = client.get(url, **make_user_auth_headers(user, token)) + assert response.status_code == status.HTTP_400_BAD_REQUEST + + @pytest.mark.django_db @pytest.mark.parametrize( "query_param,should_be_unpaginated", diff --git a/engine/apps/api/views/alert_receive_channel.py b/engine/apps/api/views/alert_receive_channel.py index 80b67025..50e48b29 100644 --- a/engine/apps/api/views/alert_receive_channel.py +++ b/engine/apps/api/views/alert_receive_channel.py @@ -68,6 +68,12 @@ class AlertReceiveChannelFilter(ByTeamModelFieldFilterMixin, filters.FilterSet): choices=AlertReceiveChannel.INTEGRATION_CHOICES, field_name="integration", exclude=True ) team = TeamModelMultipleChoiceFilter() + id_ne = filters.ModelMultipleChoiceFilter( + queryset=lambda request: request.auth.organization.alert_receive_channels.all(), + field_name="public_primary_key", + to_field_name="public_primary_key", + exclude=True, + ) class Meta: model = AlertReceiveChannel