From 417f9787de920da4398aaf0a5dd5ea92c491c9a8 Mon Sep 17 00:00:00 2001 From: Matias Bordese Date: Thu, 28 Nov 2024 18:11:00 -0300 Subject: [PATCH] fix: mobile app template preview use dynamic value (#5311) Related to https://github.com/grafana/support-escalations/issues/13689 --- .../api/tests/test_alert_receive_channel.py | 50 +++++++++++++++++++ engine/apps/mobile_app/alert_rendering.py | 2 +- 2 files changed, 51 insertions(+), 1 deletion(-) diff --git a/engine/apps/api/tests/test_alert_receive_channel.py b/engine/apps/api/tests/test_alert_receive_channel.py index ac040cde1c..d1314eb3a0 100644 --- a/engine/apps/api/tests/test_alert_receive_channel.py +++ b/engine/apps/api/tests/test_alert_receive_channel.py @@ -9,6 +9,7 @@ from apps.alerts.models import AlertReceiveChannel, EscalationPolicy from apps.api.permissions import LegacyAccessControlRole +from apps.base.messaging import load_backend from apps.labels.models import LabelKeyCache, LabelValueCache from common.exceptions import BacksyncIntegrationRequestError @@ -842,6 +843,55 @@ def test_alert_receive_channel_preview_template_dynamic_payload( assert response.data["preview"] == data["payload"]["foo"] +@pytest.mark.django_db +@pytest.mark.parametrize("template_name", ["title", "message"]) +@pytest.mark.parametrize("backend_path", ["apps.mobile_app.backend.MobileAppBackend"]) +def test_alert_receive_channel_preview_template_dynamic_payload_custom_backends( + make_organization_and_user_with_plugin_token, + make_user_auth_headers, + make_alert_receive_channel, + template_name, + backend_path, + make_alert_group, + make_alert, +): + organization, user, token = make_organization_and_user_with_plugin_token() + alert_receive_channel = make_alert_receive_channel(organization) + alert_group = make_alert_group(alert_receive_channel) + + make_alert(alert_group=alert_group, raw_request_data=alert_receive_channel.config.example_payload) + + client = APIClient() + url = reverse( + "api-internal:alert_receive_channel-preview-template", kwargs={"pk": alert_receive_channel.public_primary_key} + ) + + # load backend + backend = load_backend(backend_path, notification_channel_id=111) + notification_channel = backend.backend_id.lower() + + data = { + "template_body": "{{ payload.foo }}", + "template_name": f"{notification_channel}_{template_name}", + "payload": {"foo": "bar" if template_name != "image_url" else "http://example.com/image.jpg"}, + } + + with patch( + "apps.alerts.incident_appearance.templaters.alert_templater.get_messaging_backend_from_id" + ) as mock_get_backend: + mock_get_backend.return_value = backend + from common.api_helpers import mixins + + with patch.object(mixins, "NOTIFICATION_CHANNEL_OPTIONS", new=(notification_channel,)): + with patch.dict( + mixins.NOTIFICATION_CHANNEL_TO_TEMPLATER_MAP, {notification_channel: backend.get_templater_class()} + ): + response = client.post(url, data=data, format="json", **make_user_auth_headers(user, token)) + + assert response.status_code == status.HTTP_200_OK + assert response.data["preview"] == data["payload"]["foo"] + + @pytest.mark.django_db @pytest.mark.parametrize( "role,expected_status", diff --git a/engine/apps/mobile_app/alert_rendering.py b/engine/apps/mobile_app/alert_rendering.py index 3c67d8d342..3a05e18fed 100644 --- a/engine/apps/mobile_app/alert_rendering.py +++ b/engine/apps/mobile_app/alert_rendering.py @@ -24,7 +24,7 @@ def _validate_fcm_length_limit(value: typing.Optional[str]) -> str: class AlertMobileAppTemplater(AlertTemplater): def _render_for(self): - return "MOBILE_APP" + return "mobile_app" def _postformat(self, templated_alert: TemplatedAlert) -> TemplatedAlert: templated_alert.title = _validate_fcm_length_limit(templated_alert.title)