From 293e5e39cd49cfff545ad255c3ba4c6026a9b417 Mon Sep 17 00:00:00 2001 From: Michaela Wheeler Date: Mon, 6 Jan 2025 12:51:22 +1100 Subject: [PATCH] add tests for channel filter changes --- .../apps/alerts/tests/test_channel_filter.py | 98 ++++++++++++++++++- engine/apps/api/tests/test_alert_group.py | 42 ++++++++ 2 files changed, 139 insertions(+), 1 deletion(-) diff --git a/engine/apps/alerts/tests/test_channel_filter.py b/engine/apps/alerts/tests/test_channel_filter.py index ba19e68894..73ea00d160 100644 --- a/engine/apps/alerts/tests/test_channel_filter.py +++ b/engine/apps/alerts/tests/test_channel_filter.py @@ -1,6 +1,6 @@ import pytest -from apps.alerts.models import ChannelFilter +from apps.alerts.models import ChannelFilter, Alert @pytest.mark.django_db @@ -152,3 +152,99 @@ def test_channel_filter_using_filter_labels( assert ChannelFilter.select_filter(alert_receive_channel, {"title": "Test Title", "value": 5}, labels) == ( custom_channel_filter if should_match else default_channel_filter ) + +@pytest.mark.django_db +def test_channel_filter_team_update( + make_organization_and_user_with_plugin_token, + make_team, + make_escalation_chain, + make_alert_receive_channel, + make_channel_filter, +): + organization, user, token = make_organization_and_user_with_plugin_token() + team = make_team(organization) + + alert_receive_channel = make_alert_receive_channel(organization) + + escalation_chain = make_escalation_chain(organization=organization) + escalation_chain.team = team + + channel_filter = make_channel_filter(alert_receive_channel, escalation_chain=escalation_chain, is_default=True) + + channel_filter.update_team = True + + alert_group = Alert.create( + title="the title", + message="the message", + alert_receive_channel=alert_receive_channel, + raw_request_data={}, + integration_unique_data={}, + image_url=None, + link_to_upstream_details=None, + channel_filter=channel_filter + ).group + + assert list(alert_group.teams.all()) == [team] + +@pytest.mark.django_db +def test_channel_filter_no_team_set( + make_organization_and_user_with_plugin_token, + make_team, + make_escalation_chain, + make_alert_receive_channel, + make_channel_filter, +): + organization, user, token = make_organization_and_user_with_plugin_token() + team = make_team(organization) + + alert_receive_channel = make_alert_receive_channel(organization, team=team) + + escalation_chain = make_escalation_chain(organization=organization) + + channel_filter = make_channel_filter(alert_receive_channel, escalation_chain=escalation_chain, is_default=True) + + alert_group = Alert.create( + title="the title", + message="the message", + alert_receive_channel=alert_receive_channel, + raw_request_data={}, + integration_unique_data={}, + image_url=None, + link_to_upstream_details=None + ).group + + assert list(alert_group.teams.all()) == [team] + +@pytest.mark.django_db +def test_channel_filter_no_escalation_chain( + make_organization_and_user_with_plugin_token, + make_team, + make_escalation_chain, + make_alert_receive_channel, + make_channel_filter, +): + organization, user, token = make_organization_and_user_with_plugin_token() + team = make_team(organization) + + alert_receive_channel = make_alert_receive_channel(organization) + + escalation_chain = make_escalation_chain(organization=organization) + escalation_chain.team = team + + make_channel_filter(alert_receive_channel, escalation_chain=escalation_chain, is_default=True) + channel_filter = make_channel_filter(alert_receive_channel, is_default=True) + + channel_filter.update_team = True + + alert_group = Alert.create( + title="the title", + message="the message", + alert_receive_channel=alert_receive_channel, + raw_request_data={}, + integration_unique_data={}, + image_url=None, + link_to_upstream_details=None, + channel_filter=channel_filter + ).group + + assert list(alert_group.teams.all()) == [] \ No newline at end of file diff --git a/engine/apps/api/tests/test_alert_group.py b/engine/apps/api/tests/test_alert_group.py index 61a72e9334..be7348cfb2 100644 --- a/engine/apps/api/tests/test_alert_group.py +++ b/engine/apps/api/tests/test_alert_group.py @@ -2435,3 +2435,45 @@ def test_filter_default_started_at( ) assert response.status_code == status.HTTP_200_OK assert response.json()["pk"] == old_alert_group.public_primary_key + + + +@pytest.mark.django_db +def test_update_team( + make_organization_and_user_with_plugin_token, + make_alert_receive_channel, + make_alert_group, + make_alert, + make_user_auth_headers, + make_escalation_chain, + make_team, + make_channel_filter +): + organization, user, token = make_organization_and_user_with_plugin_token() + + team = make_team(organization) + + alert_receive_channel = make_alert_receive_channel(organization) + + escalation_chain = make_escalation_chain(organization=organization, team=team) + + channel_filter = make_channel_filter(alert_receive_channel, escalation_chain=escalation_chain, is_default=True, update_team=True) + + + alert_group = Alert.create( + title="the title", + message="the message", + alert_receive_channel=alert_receive_channel, + raw_request_data={}, + integration_unique_data={}, + image_url=None, + link_to_upstream_details=None + ).group + + client = APIClient() + url = reverse("api-internal:alertgroup-detail", kwargs={"pk": alert_group.public_primary_key}) + + response = client.get(url, **make_user_auth_headers(user, token)) + + assert response.status_code == status.HTTP_200_OK + assert response.json()['teams'][0]["id"] == team.public_primary_key