From e4465cb8bae97b62184129452cdb1376917fe2e2 Mon Sep 17 00:00:00 2001 From: Innokentii Konstantinov Date: Thu, 24 Nov 2022 13:47:46 +0800 Subject: [PATCH 1/7] Add test for update integration templates --- .../test_alert_receive_channel_template.py | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) 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 a4a10ccf5b..0048751ad0 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,39 @@ 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): + return f"{template}_updated" + + 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() + new_templates_data = {} + for k, v in existing_templates_data: + new_templates_data[k] = template_update_func(v) + + response = client.put(url, format="json", data=new_templates_data, **make_user_auth_headers(user, token)) + + updated_templates_data = response.json() + assert len(existing_templates_data) == len(updated_templates_data) + for k, v in existing_templates_data: + assert updated_templates_data[k] == template_update_func(v) From a70a391fd399634498b923433580161699798976 Mon Sep 17 00:00:00 2001 From: Innokentii Konstantinov Date: Thu, 24 Nov 2022 13:57:47 +0800 Subject: [PATCH 2/7] Fix tests --- .../apps/api/tests/test_alert_receive_channel_template.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) 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 0048751ad0..dfb0ee66fe 100644 --- a/engine/apps/api/tests/test_alert_receive_channel_template.py +++ b/engine/apps/api/tests/test_alert_receive_channel_template.py @@ -295,12 +295,12 @@ def template_update_func(template): assert response.status_code == status.HTTP_200_OK existing_templates_data = response.json() new_templates_data = {} - for k, v in existing_templates_data: - new_templates_data[k] = template_update_func(v) + 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() assert len(existing_templates_data) == len(updated_templates_data) - for k, v in existing_templates_data: - assert updated_templates_data[k] == template_update_func(v) + for template_name, prev_template_value in existing_templates_data.items(): + assert updated_templates_data[template_name] == template_update_func(prev_template_value) From 99f5aa079e34982a6489aa1e6d9854b72a69e36b Mon Sep 17 00:00:00 2001 From: Innokentii Konstantinov Date: Thu, 24 Nov 2022 14:16:51 +0800 Subject: [PATCH 3/7] Fix test --- engine/apps/api/tests/test_alert_receive_channel_template.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 dfb0ee66fe..25435c939b 100644 --- a/engine/apps/api/tests/test_alert_receive_channel_template.py +++ b/engine/apps/api/tests/test_alert_receive_channel_template.py @@ -277,7 +277,7 @@ def test_update_alert_receive_channel_templates( make_alert_receive_channel, ): def template_update_func(template): - return f"{template}_updated" + return "https://grafana.com" organization, user, token = make_organization_and_user_with_plugin_token(role=Role.ADMIN) alert_receive_channel = make_alert_receive_channel( From 61e444c9f4b2dd15755d232a52644d361f1c69f0 Mon Sep 17 00:00:00 2001 From: Innokentii Konstantinov Date: Thu, 24 Nov 2022 14:29:43 +0800 Subject: [PATCH 4/7] Fix saving source link template --- engine/apps/api/serializers/alert_receive_channel.py | 4 ++-- engine/apps/api/tests/test_alert_receive_channel_template.py | 1 + 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/engine/apps/api/serializers/alert_receive_channel.py b/engine/apps/api/serializers/alert_receive_channel.py index 58d6f349aa..918235818a 100644 --- a/engine/apps/api/serializers/alert_receive_channel.py +++ b/engine/apps/api/serializers/alert_receive_channel.py @@ -443,9 +443,9 @@ def get_source_link_template(self, obj): 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 25435c939b..b8313c048d 100644 --- a/engine/apps/api/tests/test_alert_receive_channel_template.py +++ b/engine/apps/api/tests/test_alert_receive_channel_template.py @@ -277,6 +277,7 @@ def test_update_alert_receive_channel_templates( 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) From 3a8da9fa57bab80a42d1d330ac234cdc78dcc394 Mon Sep 17 00:00:00 2001 From: Innokentii Konstantinov Date: Thu, 24 Nov 2022 14:44:29 +0800 Subject: [PATCH 5/7] Fix test --- engine/apps/api/tests/test_alert_receive_channel_template.py | 3 +++ 1 file changed, 3 insertions(+) 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 b8313c048d..00e6e66ee0 100644 --- a/engine/apps/api/tests/test_alert_receive_channel_template.py +++ b/engine/apps/api/tests/test_alert_receive_channel_template.py @@ -295,6 +295,9 @@ def template_update_func(template): 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"] new_templates_data = {} for template_name, template_value in existing_templates_data.items(): new_templates_data[template_name] = template_update_func(template_value) From 515b1a5230058a4ce5bf9ee28ce1352091d45297 Mon Sep 17 00:00:00 2001 From: Innokentii Konstantinov Date: Thu, 24 Nov 2022 14:49:29 +0800 Subject: [PATCH 6/7] Fix test --- engine/apps/api/tests/test_alert_receive_channel_template.py | 1 - 1 file changed, 1 deletion(-) 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 00e6e66ee0..62338fcfef 100644 --- a/engine/apps/api/tests/test_alert_receive_channel_template.py +++ b/engine/apps/api/tests/test_alert_receive_channel_template.py @@ -305,6 +305,5 @@ def template_update_func(template): response = client.put(url, format="json", data=new_templates_data, **make_user_auth_headers(user, token)) updated_templates_data = response.json() - assert len(existing_templates_data) == len(updated_templates_data) for template_name, prev_template_value in existing_templates_data.items(): assert updated_templates_data[template_name] == template_update_func(prev_template_value) From 7d7cfb533a72cc1374f80c46adea1fef36ef5fa4 Mon Sep 17 00:00:00 2001 From: Innokentii Konstantinov Date: Thu, 24 Nov 2022 15:01:00 +0800 Subject: [PATCH 7/7] Fix tests --- engine/apps/api/tests/test_alert_receive_channel_template.py | 3 +++ 1 file changed, 3 insertions(+) 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 62338fcfef..163308107f 100644 --- a/engine/apps/api/tests/test_alert_receive_channel_template.py +++ b/engine/apps/api/tests/test_alert_receive_channel_template.py @@ -295,9 +295,12 @@ def template_update_func(template): 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)