Skip to content

Commit

Permalink
fix: mobile app template preview use dynamic value (#5311)
Browse files Browse the repository at this point in the history
  • Loading branch information
matiasb authored Nov 28, 2024
1 parent bb4875f commit 417f978
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
50 changes: 50 additions & 0 deletions engine/apps/api/tests/test_alert_receive_channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion engine/apps/mobile_app/alert_rendering.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 417f978

Please sign in to comment.