Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

test_e2ee_push_notifications: Assert database query count.

Prakhar Pratyush2588ac42904f
diff --git a/zerver/tests/test_e2ee_push_notifications.py b/zerver/tests/test_e2ee_push_notifications.py
index 83b45c6115e..7e32c41c239 100644
--- a/zerver/tests/test_e2ee_push_notifications.py
+++ b/zerver/tests/test_e2ee_push_notifications.py
@@ -1,4 +1,5 @@
from datetime import datetime, timezone
+from typing import Any
from unittest import mock
import responses
@@ -29,6 +30,7 @@ from zerver.lib.timestamp import datetime_to_timestamp
from zerver.models import PushDevice, UserMessage
from zerver.models.realms import get_realm
from zerver.models.scheduled_jobs import NotificationTriggers
+from zerver.models.streams import get_stream
from zilencer.lib.push_notifications import SentPushNotificationResult
from zilencer.models import RemoteRealm, RemoteRealmCount
@@ -38,10 +40,74 @@ class SendPushNotificationTest(E2EEPushNotificationTestCase):
def test_success_cloud(self) -> None:
hamlet = self.example_user("hamlet")
aaron = self.example_user("aaron")
+ iago = self.example_user("iago")
registered_device_apple, registered_device_android = (
self.register_push_devices_for_notification()
)
+
+ def test_end_to_end(missed_message: dict[str, Any], *, db_query_count: int) -> None:
+ self.assertEqual(RealmCount.objects.count(), 0)
+
+ with (
+ self.mock_fcm() as mock_fcm_messaging,
+ self.mock_apns() as send_notification,
+ self.assertLogs("zerver.lib.push_notifications", level="INFO") as zerver_logger,
+ self.assertLogs("zilencer.lib.push_notifications", level="INFO") as zilencer_logger,
+ mock.patch("time.perf_counter", side_effect=[10.0, 15.0]),
+ ):
+ mock_fcm_messaging.send_each.return_value = self.make_fcm_success_response()
+ send_notification.return_value.is_successful = True
+
+ with self.assert_database_query_count(db_query_count):
+ handle_push_notification(hamlet.id, missed_message)
+
+ mock_fcm_messaging.send_each.assert_called_once()
+ send_notification.assert_called_once()
+
+ self.assertEqual(
+ "INFO:zerver.lib.push_notifications:"
+ f"Sending push notifications to mobile clients for user {hamlet.id}",
+ zerver_logger.output[0],
+ )
+ self.assertEqual(
+ "INFO:zerver.lib.push_notifications:"
+ f"Skipping legacy push notifications for user {hamlet.id} because there are no registered devices",
+ zerver_logger.output[1],
+ )
+ self.assertEqual(
+ "INFO:zerver.lib.push_notifications:"
+ f"APNs: Success sending to (push_account_id={registered_device_apple.push_account_id}, device={registered_device_apple.token})",
+ zerver_logger.output[2],
+ )
+ self.assertEqual(
+ "INFO:zilencer.lib.push_notifications:"
+ f"FCM: Sent message with ID: 0 to (push_account_id={registered_device_android.push_account_id}, device={registered_device_android.token})",
+ zilencer_logger.output[0],
+ )
+ self.assertEqual(
+ "INFO:zerver.lib.push_notifications:"
+ f"Sent E2EE mobile push notifications for user {hamlet.id}: 1 via FCM, 1 via APNs in 5.000s",
+ zerver_logger.output[3],
+ )
+
+ realm_count_dict = (
+ RealmCount.objects.filter(property="mobile_pushes_sent::day")
+ .values("subgroup", "value")
+ .last()
+ )
+ self.assertEqual(realm_count_dict, dict(subgroup=None, value=2))
+ # Reset
+ RealmCount.objects.all().delete()
+
+ # 1:1 DM
+ # query count : source
+ # * 1 : `get_user_profile_by_id`
+ # * 2 : `access_message_and_usermessage` (Fetch Message + UserMessage)
+ # * 1 : update fetched user_message flag
+ # * 2 : fetch PushDeviceToken + PushDevice
+ # * 1 : `get_display_recipient` in `get_message_payload`
+ # * 2 : fetch RemotePushDevice + update RealmCount
message_id = self.send_personal_message(
from_user=aaron, to_user=hamlet, skip_capture_on_commit_callbacks=True
)
@@ -49,56 +115,47 @@ class SendPushNotificationTest(E2EEPushNotificationTestCase):
"message_id": message_id,
"trigger": NotificationTriggers.DIRECT_MESSAGE,
}
+ test_end_to_end(missed_message, db_query_count=9)
- self.assertEqual(RealmCount.objects.count(), 0)
-
- with (
- self.mock_fcm() as mock_fcm_messaging,
- self.mock_apns() as send_notification,
- self.assertLogs("zerver.lib.push_notifications", level="INFO") as zerver_logger,
- self.assertLogs("zilencer.lib.push_notifications", level="INFO") as zilencer_logger,
- mock.patch("time.perf_counter", side_effect=[10.0, 15.0]),
- ):
- mock_fcm_messaging.send_each.return_value = self.make_fcm_success_response()
- send_notification.return_value.is_successful = True
-
- handle_push_notification(hamlet.id, missed_message)
-
- mock_fcm_messaging.send_each.assert_called_once()
- send_notification.assert_called_once()
-
- self.assertEqual(
- "INFO:zerver.lib.push_notifications:"
- f"Sending push notifications to mobile clients for user {hamlet.id}",
- zerver_logger.output[0],
- )
- self.assertEqual(
- "INFO:zerver.lib.push_notifications:"
- f"Skipping legacy push notifications for user {hamlet.id} because there are no registered devices",
- zerver_logger.output[1],
- )
- self.assertEqual(
- "INFO:zerver.lib.push_notifications:"
- f"APNs: Success sending to (push_account_id={registered_device_apple.push_account_id}, device={registered_device_apple.token})",
- zerver_logger.output[2],
- )
- self.assertEqual(
- "INFO:zilencer.lib.push_notifications:"
- f"FCM: Sent message with ID: 0 to (push_account_id={registered_device_android.push_account_id}, device={registered_device_android.token})",
- zilencer_logger.output[0],
- )
- self.assertEqual(
- "INFO:zerver.lib.push_notifications:"
- f"Sent E2EE mobile push notifications for user {hamlet.id}: 1 via FCM, 1 via APNs in 5.000s",
- zerver_logger.output[3],
- )
-
- realm_count_dict = (
- RealmCount.objects.filter(property="mobile_pushes_sent::day")
- .values("subgroup", "value")
- .last()
- )
- self.assertEqual(realm_count_dict, dict(subgroup=None, value=2))
+ # Group DM
+ message_id = self.send_group_direct_message(
+ iago, [hamlet, aaron, iago], skip_capture_on_commit_callbacks=True
+ )
+ missed_message = {
+ "message_id": message_id,
+ "trigger": NotificationTriggers.DIRECT_MESSAGE,
+ }
+ test_end_to_end(missed_message, db_query_count=9)
+
+ # Channel message
+ # 2 extra queries than 1:1 DM
+ # 1 : fetch Stream in `access_message_and_usermessage` codepath
+ # 1 : query NamedUserGroup in `check_can_access_user` codepath
+ # 1 : fetch Stream in `get_message_payload` (TODO: we can avoid this)
+ # -1 : `get_display_recipient` not needed
+ message_id = self.send_stream_message(
+ aaron, "Verona", skip_capture_on_commit_callbacks=True
+ )
+ missed_message = {"message_id": message_id, "trigger": NotificationTriggers.STREAM_PUSH}
+ test_end_to_end(missed_message, db_query_count=11)
+
+ # Channel message: private channel + user-group mention
+ # 3 extra queries than prev:
+ # 1 : query Subscription in `access_message_and_usermessage` codepath (needed for private channel)
+ # 2 : `get_mentioned_user_group` b/c content includes user-group mention.
+ channel = get_stream("core team", iago.realm)
+ user_group = check_add_user_group(
+ iago.realm, "test_group", [iago, aaron, hamlet], acting_user=iago
+ )
+ message_id = self.send_stream_message(
+ iago, channel.name, f"@*{user_group.name}*", skip_capture_on_commit_callbacks=True
+ )
+ missed_message = {
+ "message_id": message_id,
+ "trigger": NotificationTriggers.MENTION,
+ "mentioned_user_group_id": user_group.id,
+ }
+ test_end_to_end(missed_message, db_query_count=14)
def test_no_registered_device(self) -> None:
aaron = self.example_user("aaron")