Repository: zulip/zulip

Issue:
Adjust API endpoint for sending test notifications.
As noted in https://chat.zulip.org/#narrow/stream/378-api-design/topic/.2323997.20Endpoint.20for.20test.20notification/near/1696957:

> To summarize from [this message](https://chat.zulip.org/#narrow/stream/378-api-design/topic/.2323997.20Endpoint.20for.20test.20notification/near/1690721) on 2023-11-28 and following, there are two changes we want to include in the 8.0 release if possible:

> - Change the notification payload's "event" field from "test-by-device-token" to just "test". (More on this [here](https://chat.zulip.org/#narrow/stream/378-api-design/topic/.2323997.20Endpoint.20for.20test.20notification/near/1690777).)
> - Add a "realm_name" field to the payload, with the realm's name.



Discussion:

Resolving patch:
diff --git a/version.py b/version.py
--- a/version.py
+++ b/version.py
@@ -33,7 +33,7 @@
 # Changes should be accompanied by documentation explaining what the
 # new level means in api_docs/changelog.md, as well as "**Changes**"
 # entries in the endpoint's documentation in `zulip.yaml`.
-API_FEATURE_LEVEL = 233
+API_FEATURE_LEVEL = 234
 
 # Bump the minor PROVISION_VERSION to indicate that folks should provision
 # only when going from an old version of the code to a newer version. Bump
diff --git a/zerver/lib/push_notifications.py b/zerver/lib/push_notifications.py
--- a/zerver/lib/push_notifications.py
+++ b/zerver/lib/push_notifications.py
@@ -908,6 +908,7 @@ def get_base_payload(user_profile: UserProfile) -> Dict[str, Any]:
     data["server"] = settings.EXTERNAL_HOST
     data["realm_id"] = user_profile.realm.id
     data["realm_uri"] = user_profile.realm.uri
+    data["realm_name"] = user_profile.realm.name
     data["user_id"] = user_profile.id
 
     return data
@@ -1398,7 +1399,7 @@ def send_test_push_notification_directly_to_devices(
     remote: Optional["RemoteZulipServer"] = None,
 ) -> None:
     payload = copy.deepcopy(base_payload)
-    payload["event"] = "test-by-device-token"
+    payload["event"] = "test"
 
     apple_devices = [device for device in devices if device.kind == PushDeviceToken.APNS]
     android_devices = [device for device in devices if device.kind == PushDeviceToken.GCM]
@@ -1408,10 +1409,13 @@ def send_test_push_notification_directly_to_devices(
     android_payload = copy.deepcopy(payload)
 
     realm_uri = base_payload["realm_uri"]
+    realm_name = base_payload["realm_name"]
     apns_data = {
         "alert": {
             "title": _("Test notification"),
-            "body": _("This is a test notification from {realm_uri}.").format(realm_uri=realm_uri),
+            "body": _("This is a test notification from {realm_name} ({realm_uri}).").format(
+                realm_name=realm_name, realm_uri=realm_uri
+            ),
         },
         "sound": "default",
         "custom": {"zulip": apple_payload},


Test patch:
diff --git a/zerver/tests/test_push_notifications.py b/zerver/tests/test_push_notifications.py
--- a/zerver/tests/test_push_notifications.py
+++ b/zerver/tests/test_push_notifications.py
@@ -196,9 +196,10 @@ def test_send_test_push_notification_api_no_bouncer_config(self) -> None:
         expected_android_payload = {
             "server": "testserver",
             "realm_id": user.realm_id,
+            "realm_name": "Zulip Dev",
             "realm_uri": "http://zulip.testserver",
             "user_id": user.id,
-            "event": "test-by-device-token",
+            "event": "test",
             "time": datetime_to_timestamp(time_now),
         }
         expected_gcm_options = {"priority": "high"}
@@ -219,16 +220,17 @@ def test_send_test_push_notification_api_no_bouncer_config(self) -> None:
         expected_apple_payload = {
             "alert": {
                 "title": "Test notification",
-                "body": "This is a test notification from http://zulip.testserver.",
+                "body": "This is a test notification from Zulip Dev (http://zulip.testserver).",
             },
             "sound": "default",
             "custom": {
                 "zulip": {
                     "server": "testserver",
                     "realm_id": user.realm_id,
+                    "realm_name": "Zulip Dev",
                     "realm_uri": "http://zulip.testserver",
                     "user_id": user.id,
-                    "event": "test-by-device-token",
+                    "event": "test",
                 }
             },
         }
@@ -301,9 +303,10 @@ def test_send_test_push_notification_api_with_bouncer_config(self) -> None:
         expected_payload = {
             "server": "testserver",
             "realm_id": user.realm_id,
+            "realm_name": "Zulip Dev",
             "realm_uri": "http://zulip.testserver",
             "user_id": user.id,
-            "event": "test-by-device-token",
+            "event": "test",
             "time": datetime_to_timestamp(time_now),
         }
         expected_gcm_options = {"priority": "high"}
@@ -2389,6 +2392,7 @@ def test_send_remove_notifications_to_bouncer(self) -> None:
                         "zulip": {
                             "server": "testserver",
                             "realm_id": self.sender.realm.id,
+                            "realm_name": self.sender.realm.name,
                             "realm_uri": "http://zulip.testserver",
                             "user_id": self.user_profile.id,
                             "event": "remove",
@@ -2399,6 +2403,7 @@ def test_send_remove_notifications_to_bouncer(self) -> None:
                 {
                     "server": "testserver",
                     "realm_id": self.sender.realm.id,
+                    "realm_name": self.sender.realm.name,
                     "realm_uri": "http://zulip.testserver",
                     "user_id": self.user_profile.id,
                     "event": "remove",
@@ -2452,6 +2457,7 @@ def test_non_bouncer_push_remove(self) -> None:
                 {
                     "server": "testserver",
                     "realm_id": self.sender.realm.id,
+                    "realm_name": self.sender.realm.name,
                     "realm_uri": "http://zulip.testserver",
                     "user_id": self.user_profile.id,
                     "event": "remove",
@@ -2469,6 +2475,7 @@ def test_non_bouncer_push_remove(self) -> None:
                         "zulip": {
                             "server": "testserver",
                             "realm_id": self.sender.realm.id,
+                            "realm_name": self.sender.realm.name,
                             "realm_uri": "http://zulip.testserver",
                             "user_id": self.user_profile.id,
                             "event": "remove",
@@ -2948,6 +2955,7 @@ def test_get_message_payload_apns_personal_message(self) -> None:
                     "sender_id": self.sender.id,
                     "server": settings.EXTERNAL_HOST,
                     "realm_id": self.sender.realm.id,
+                    "realm_name": self.sender.realm.name,
                     "realm_uri": self.sender.realm.uri,
                     "user_id": user_profile.id,
                     "time": datetime_to_timestamp(message.date_sent),
@@ -2991,6 +2999,7 @@ def test_get_message_payload_apns_huddle_message(
                     "sender_id": self.sender.id,
                     "server": settings.EXTERNAL_HOST,
                     "realm_id": self.sender.realm.id,
+                    "realm_name": self.sender.realm.name,
                     "realm_uri": self.sender.realm.uri,
                     "user_id": user_profile.id,
                     "time": datetime_to_timestamp(message.date_sent),
@@ -3023,6 +3032,7 @@ def _test_get_message_payload_apns_stream_message(self, trigger: str) -> None:
                     "topic": message.topic_name(),
                     "server": settings.EXTERNAL_HOST,
                     "realm_id": self.sender.realm.id,
+                    "realm_name": self.sender.realm.name,
                     "realm_uri": self.sender.realm.uri,
                     "user_id": self.sender.id,
                     "time": datetime_to_timestamp(message.date_sent),
@@ -3061,6 +3071,7 @@ def test_get_message_payload_apns_stream_mention(self) -> None:
                     "topic": message.topic_name(),
                     "server": settings.EXTERNAL_HOST,
                     "realm_id": self.sender.realm.id,
+                    "realm_name": self.sender.realm.name,
                     "realm_uri": self.sender.realm.uri,
                     "user_id": user_profile.id,
                     "time": datetime_to_timestamp(message.date_sent),
@@ -3098,6 +3109,7 @@ def test_get_message_payload_apns_user_group_mention(self) -> None:
                     "topic": message.topic_name(),
                     "server": settings.EXTERNAL_HOST,
                     "realm_id": self.sender.realm.id,
+                    "realm_name": self.sender.realm.name,
                     "realm_uri": self.sender.realm.uri,
                     "user_id": user_profile.id,
                     "mentioned_user_group_id": user_group.id,
@@ -3136,6 +3148,7 @@ def _test_get_message_payload_apns_wildcard_mention(self, trigger: str) -> None:
                     "topic": message.topic_name(),
                     "server": settings.EXTERNAL_HOST,
                     "realm_id": self.sender.realm.id,
+                    "realm_name": self.sender.realm.name,
                     "realm_uri": self.sender.realm.uri,
                     "user_id": user_profile.id,
                     "time": datetime_to_timestamp(message.date_sent),
@@ -3197,6 +3210,7 @@ def test_get_message_payload_apns_redacted_content(self) -> None:
                     "sender_id": self.sender.id,
                     "server": settings.EXTERNAL_HOST,
                     "realm_id": self.sender.realm.id,
+                    "realm_name": self.sender.realm.name,
                     "realm_uri": self.sender.realm.uri,
                     "user_id": user_profile.id,
                     "time": datetime_to_timestamp(message.date_sent),
@@ -3246,6 +3260,7 @@ def test_get_message_payload_apns_stream_message_from_inaccessible_user(self) ->
                     "topic": message.topic_name(),
                     "server": settings.EXTERNAL_HOST,
                     "realm_id": hamlet.realm.id,
+                    "realm_name": hamlet.realm.name,
                     "realm_uri": hamlet.realm.uri,
                     "user_id": polonius.id,
                     "time": datetime_to_timestamp(message.date_sent),
@@ -3284,6 +3299,7 @@ def _test_get_message_payload_gcm_stream_message(
             "content_truncated": truncate_content,
             "server": settings.EXTERNAL_HOST,
             "realm_id": hamlet.realm.id,
+            "realm_name": hamlet.realm.name,
             "realm_uri": hamlet.realm.uri,
             "sender_id": hamlet.id,
             "sender_email": hamlet.email,
@@ -3342,6 +3358,7 @@ def test_get_message_payload_gcm_direct_message(self) -> None:
                 "content_truncated": False,
                 "server": settings.EXTERNAL_HOST,
                 "realm_id": hamlet.realm.id,
+                "realm_name": hamlet.realm.name,
                 "realm_uri": hamlet.realm.uri,
                 "sender_id": hamlet.id,
                 "sender_email": hamlet.email,
@@ -3374,6 +3391,7 @@ def test_get_message_payload_gcm_redacted_content(self) -> None:
                 "content_truncated": False,
                 "server": settings.EXTERNAL_HOST,
                 "realm_id": hamlet.realm.id,
+                "realm_name": hamlet.realm.name,
                 "realm_uri": hamlet.realm.uri,
                 "sender_id": hamlet.id,
                 "sender_email": hamlet.email,
@@ -3423,6 +3441,7 @@ def test_get_message_payload_gcm_stream_message_from_inaccessible_user(self) ->
                 "content_truncated": False,
                 "server": settings.EXTERNAL_HOST,
                 "realm_id": hamlet.realm.id,
+                "realm_name": hamlet.realm.name,
                 "realm_uri": hamlet.realm.uri,
                 "sender_id": hamlet.id,
                 "sender_email": f"user{hamlet.id}@zulip.testserver",