fix: mobile app template preview use dynamic value (#5311)
Related to https://github.com/grafana/support-escalations/issues/13689
This commit is contained in:
parent
bb4875f8a5
commit
417f9787de
2 changed files with 51 additions and 1 deletions
|
|
@ -9,6 +9,7 @@ from rest_framework.test import APIClient
|
|||
|
||||
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",
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue