diff --git a/engine/apps/api/serializers/alert_receive_channel.py b/engine/apps/api/serializers/alert_receive_channel.py index 58d6f349..91823581 100644 --- a/engine/apps/api/serializers/alert_receive_channel.py +++ b/engine/apps/api/serializers/alert_receive_channel.py @@ -443,9 +443,9 @@ class AlertReceiveChannelTemplatesSerializer(EagerLoadingMixin, serializers.Mode def set_source_link_template(self, value): default_template = AlertReceiveChannel.INTEGRATION_TO_DEFAULT_SOURCE_LINK_TEMPLATE[self.instance.integration] if default_template is None or default_template.strip() != value.strip(): - self.instance.source_link = value.strip() + self.instance.source_link_template = value.strip() elif default_template is not None and default_template.strip() == value.strip(): - self.instance.source_link = None + self.instance.source_link_template = None def get_grouping_id_template(self, obj): default_template = AlertReceiveChannel.INTEGRATION_TO_DEFAULT_GROUPING_ID_TEMPLATE[obj.integration] diff --git a/engine/apps/api/tests/test_alert_receive_channel_template.py b/engine/apps/api/tests/test_alert_receive_channel_template.py index a4a10ccf..16330810 100644 --- a/engine/apps/api/tests/test_alert_receive_channel_template.py +++ b/engine/apps/api/tests/test_alert_receive_channel_template.py @@ -268,3 +268,45 @@ def test_preview_alert_receive_channel_backend_templater( assert response.status_code == status.HTTP_200_OK assert response.json() == {"preview": "title: alert!"} + + +@pytest.mark.django_db +def test_update_alert_receive_channel_templates( + make_organization_and_user_with_plugin_token, + make_user_auth_headers, + make_alert_receive_channel, +): + def template_update_func(template): + # set url here to pass *_url templates validation + return "https://grafana.com" + + organization, user, token = make_organization_and_user_with_plugin_token(role=Role.ADMIN) + alert_receive_channel = make_alert_receive_channel( + organization, + messaging_backends_templates={"TESTONLY": {"title": "the-title", "message": "the-message", "image_url": "url"}}, + ) + client = APIClient() + + url = reverse( + "api-internal:alert_receive_channel_template-detail", kwargs={"pk": alert_receive_channel.public_primary_key} + ) + + response = client.get(url, format="json", **make_user_auth_headers(user, token)) + + assert response.status_code == status.HTTP_200_OK + existing_templates_data = response.json() + + # leave only templates-related fields + del existing_templates_data["id"] + del existing_templates_data["verbal_name"] + del existing_templates_data["payload_example"] + + new_templates_data = {} + for template_name, template_value in existing_templates_data.items(): + new_templates_data[template_name] = template_update_func(template_value) + + response = client.put(url, format="json", data=new_templates_data, **make_user_auth_headers(user, token)) + + updated_templates_data = response.json() + for template_name, prev_template_value in existing_templates_data.items(): + assert updated_templates_data[template_name] == template_update_func(prev_template_value)