Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix saving source link template #898

Merged
merged 7 commits into from
Nov 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions engine/apps/api/serializers/alert_receive_channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
42 changes: 42 additions & 0 deletions engine/apps/api/tests/test_alert_receive_channel_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)