Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
push_notif: Don't log for messages moved to inaccessible streams.
Aman Agrawalf8aa3ffff2ca
diff --git a/zerver/lib/push_notifications.py b/zerver/lib/push_notifications.pyindex dac5b439435..8d96135e972 100644--- a/zerver/lib/push_notifications.py+++ b/zerver/lib/push_notifications.py@@ -1750,6 +1750,12 @@ def handle_push_notification(user_profile_id: int, missed_message: dict[str, Any# If the cause is a race with the message being deleted,# that's normal and we have no need to log an error.return+ if Message.objects.filter(id=missed_message["message_id"]).exists():+ # If the message exists but is no longer accessible to+ # the user, this is likely because the message was moved+ # to a channel the user doesn't have access to. This is+ # a normal race and we have no need to log an error.+ returnlogging.info("Unexpected message access failure handling push notifications: %s %s",user_profile.id,diff --git a/zerver/tests/test_handle_push_notification.py b/zerver/tests/test_handle_push_notification.pyindex 518b1d7f0e2..ed90ef8e278 100644--- a/zerver/tests/test_handle_push_notification.py+++ b/zerver/tests/test_handle_push_notification.py@@ -11,6 +11,7 @@ from typing_extensions import overridefrom analytics.models import RealmCountfrom zerver.actions.message_delete import do_delete_messages+from zerver.actions.message_edit import check_update_messagefrom zerver.actions.user_groups import add_subgroups_to_user_group, check_add_user_groupfrom zerver.actions.user_settings import do_change_user_settingfrom zerver.actions.user_topics import do_set_user_topic_visibility_policy@@ -475,6 +476,51 @@ class HandlePushNotificationTest(PushNotificationTestCase):],)+ def test_message_moved_to_inaccessible_stream(self) -> None:+ """Simulates the race where a message is moved to a private stream+ the user doesn't have access to before handling push notifications."""+ user_profile = self.example_user("hamlet")+ realm = user_profile.realm+ admin = self.example_user("desdemona")++ # Send a message in a public stream where hamlet is subscribed.+ stream = get_stream("Denmark", realm)+ self.subscribe(user_profile, stream.name)+ self.subscribe(admin, stream.name)+ message_id = self.send_stream_message(admin, stream.name, topic_name="test topic")+ missed_message = {+ "message_id": message_id,+ "trigger": NotificationTriggers.STREAM_PUSH,+ }++ # Move the message to a private stream hamlet is not subscribed to.+ private_stream = self.make_stream("private stream", invite_only=True)+ self.subscribe(admin, private_stream.name)+ check_update_message(+ user_profile=admin,+ message_id=message_id,+ stream_id=private_stream.id,+ propagate_mode="change_all",+ send_notification_to_old_thread=False,+ send_notification_to_new_thread=False,+ content=None,+ )++ # This mock.patch() should be assertNoLogs once that feature+ # is added to Python.+ with (+ mock.patch("zerver.lib.push_notifications.uses_notification_bouncer") as mock_check,+ mock.patch("logging.info") as mock_logging_info,+ mock.patch(+ "zerver.lib.push_notifications.push_notifications_configured", return_value=True+ ) as mock_push_notifications,+ ):+ handle_push_notification(user_profile.id, missed_message)+ mock_push_notifications.assert_called_once()+ # Check we didn't proceed through and didn't log anything.+ mock_check.assert_not_called()+ mock_logging_info.assert_not_called()+def test_send_notifications_to_bouncer(self) -> None:self.setup_apns_tokens()self.setup_fcm_tokens()