2018-03-28 21:42:06 +02:00
|
|
|
import time
|
2021-05-31 10:52:32 +02:00
|
|
|
from typing import Any, Callable, Collection, Dict, List, Optional, Tuple
|
2020-06-11 00:54:34 +02:00
|
|
|
from unittest import mock
|
2017-09-27 20:27:04 +02:00
|
|
|
|
2020-08-07 01:09:47 +02:00
|
|
|
import orjson
|
2017-09-27 20:55:58 +02:00
|
|
|
from django.http import HttpRequest, HttpResponse
|
2017-09-27 20:27:04 +02:00
|
|
|
|
2020-06-11 00:54:34 +02:00
|
|
|
from zerver.lib.actions import do_change_subscription_property, do_mute_topic
|
2017-09-27 20:27:04 +02:00
|
|
|
from zerver.lib.test_classes import ZulipTestCase
|
2021-02-07 21:34:01 +01:00
|
|
|
from zerver.lib.test_helpers import HostRequestMock, mock_queue_publish
|
2018-08-08 21:39:19 +02:00
|
|
|
from zerver.models import Recipient, Stream, Subscription, UserProfile, get_stream
|
2020-06-11 00:54:34 +02:00
|
|
|
from zerver.tornado.event_queue import (
|
|
|
|
ClientDescriptor,
|
|
|
|
allocate_client_descriptor,
|
|
|
|
get_client_descriptor,
|
|
|
|
maybe_enqueue_notifications,
|
|
|
|
missedmessage_hook,
|
|
|
|
persistent_queue_filename,
|
|
|
|
)
|
|
|
|
from zerver.tornado.views import cleanup_event_queue, get_events
|
|
|
|
|
2017-09-27 20:27:04 +02:00
|
|
|
|
|
|
|
class MissedMessageNotificationsTest(ZulipTestCase):
|
|
|
|
"""Tests the logic for when missed-message notifications
|
|
|
|
should be triggered, based on user settings"""
|
2020-04-22 01:45:30 +02:00
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def check_will_notify(self, *args: Any, **kwargs: Any) -> Tuple[str, str]:
|
2017-09-27 20:27:04 +02:00
|
|
|
email_notice = None
|
|
|
|
mobile_notice = None
|
2021-02-12 08:19:30 +01:00
|
|
|
with mock_queue_publish(
|
|
|
|
"zerver.tornado.event_queue.queue_json_publish"
|
|
|
|
) as mock_queue_json_publish:
|
2017-09-27 20:27:04 +02:00
|
|
|
notified = maybe_enqueue_notifications(*args, **kwargs)
|
2020-08-14 10:03:36 +02:00
|
|
|
for entry in mock_queue_json_publish.call_args_list:
|
2017-09-27 20:27:04 +02:00
|
|
|
args = entry[0]
|
|
|
|
if args[0] == "missedmessage_mobile_notifications":
|
|
|
|
mobile_notice = args[1]
|
|
|
|
if args[0] == "missedmessage_emails":
|
|
|
|
email_notice = args[1]
|
|
|
|
|
|
|
|
# Now verify the return value matches the queue actions
|
|
|
|
if email_notice:
|
2021-02-12 08:20:45 +01:00
|
|
|
self.assertTrue(notified["email_notified"])
|
2017-09-27 20:27:04 +02:00
|
|
|
else:
|
2021-02-12 08:20:45 +01:00
|
|
|
self.assertFalse(notified.get("email_notified", False))
|
2017-09-27 20:27:04 +02:00
|
|
|
if mobile_notice:
|
2021-02-12 08:20:45 +01:00
|
|
|
self.assertTrue(notified["push_notified"])
|
2017-09-27 20:27:04 +02:00
|
|
|
else:
|
2021-02-12 08:20:45 +01:00
|
|
|
self.assertFalse(notified.get("push_notified", False))
|
2017-09-27 20:27:04 +02:00
|
|
|
return email_notice, mobile_notice
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_enqueue_notifications(self) -> None:
|
2017-09-27 20:27:04 +02:00
|
|
|
user_profile = self.example_user("hamlet")
|
|
|
|
message_id = 32
|
|
|
|
|
|
|
|
# Boring message doesn't send a notice
|
|
|
|
email_notice, mobile_notice = self.check_will_notify(
|
2021-02-12 08:19:30 +01:00
|
|
|
user_profile.id,
|
|
|
|
message_id,
|
|
|
|
private_message=False,
|
|
|
|
mentioned=False,
|
|
|
|
wildcard_mention_notify=False,
|
|
|
|
stream_push_notify=False,
|
|
|
|
stream_email_notify=False,
|
|
|
|
stream_name=None,
|
2021-05-24 06:11:59 +02:00
|
|
|
online_push_enabled=False,
|
2021-02-12 08:19:30 +01:00
|
|
|
idle=True,
|
|
|
|
already_notified={},
|
|
|
|
)
|
2017-09-27 20:27:04 +02:00
|
|
|
self.assertTrue(email_notice is None)
|
|
|
|
self.assertTrue(mobile_notice is None)
|
|
|
|
|
|
|
|
# Private message sends a notice
|
|
|
|
email_notice, mobile_notice = self.check_will_notify(
|
2021-02-12 08:19:30 +01:00
|
|
|
user_profile.id,
|
|
|
|
message_id,
|
|
|
|
private_message=True,
|
|
|
|
mentioned=False,
|
|
|
|
wildcard_mention_notify=False,
|
|
|
|
stream_push_notify=False,
|
|
|
|
stream_email_notify=True,
|
|
|
|
stream_name=None,
|
2021-05-24 06:11:59 +02:00
|
|
|
online_push_enabled=False,
|
2021-02-12 08:19:30 +01:00
|
|
|
idle=True,
|
|
|
|
already_notified={},
|
|
|
|
)
|
2017-09-27 20:27:04 +02:00
|
|
|
self.assertTrue(email_notice is not None)
|
|
|
|
self.assertTrue(mobile_notice is not None)
|
|
|
|
|
2017-10-18 06:54:03 +02:00
|
|
|
# Private message won't double-send either notice if we've
|
|
|
|
# already sent notices before.
|
|
|
|
email_notice, mobile_notice = self.check_will_notify(
|
2021-02-12 08:19:30 +01:00
|
|
|
user_profile.id,
|
|
|
|
message_id,
|
|
|
|
private_message=True,
|
|
|
|
mentioned=False,
|
|
|
|
wildcard_mention_notify=False,
|
|
|
|
stream_push_notify=False,
|
|
|
|
stream_email_notify=False,
|
|
|
|
stream_name=None,
|
2021-05-24 06:11:59 +02:00
|
|
|
online_push_enabled=False,
|
2021-02-12 08:19:30 +01:00
|
|
|
idle=True,
|
|
|
|
already_notified={
|
2021-02-12 08:20:45 +01:00
|
|
|
"push_notified": True,
|
|
|
|
"email_notified": False,
|
2021-02-12 08:19:30 +01:00
|
|
|
},
|
|
|
|
)
|
2017-10-18 06:54:03 +02:00
|
|
|
self.assertTrue(email_notice is not None)
|
|
|
|
self.assertTrue(mobile_notice is None)
|
|
|
|
|
|
|
|
email_notice, mobile_notice = self.check_will_notify(
|
2021-02-12 08:19:30 +01:00
|
|
|
user_profile.id,
|
|
|
|
message_id,
|
|
|
|
private_message=True,
|
|
|
|
mentioned=False,
|
|
|
|
wildcard_mention_notify=False,
|
|
|
|
stream_push_notify=False,
|
|
|
|
stream_email_notify=False,
|
|
|
|
stream_name=None,
|
2021-05-24 06:11:59 +02:00
|
|
|
online_push_enabled=False,
|
2021-02-12 08:19:30 +01:00
|
|
|
idle=True,
|
|
|
|
already_notified={
|
2021-02-12 08:20:45 +01:00
|
|
|
"push_notified": False,
|
|
|
|
"email_notified": True,
|
2021-02-12 08:19:30 +01:00
|
|
|
},
|
|
|
|
)
|
2017-10-18 06:54:03 +02:00
|
|
|
self.assertTrue(email_notice is None)
|
|
|
|
self.assertTrue(mobile_notice is not None)
|
|
|
|
|
2017-09-27 20:27:04 +02:00
|
|
|
# Mention sends a notice
|
|
|
|
email_notice, mobile_notice = self.check_will_notify(
|
2021-02-12 08:19:30 +01:00
|
|
|
user_profile.id,
|
|
|
|
message_id,
|
|
|
|
private_message=False,
|
|
|
|
mentioned=True,
|
|
|
|
wildcard_mention_notify=False,
|
|
|
|
stream_push_notify=False,
|
|
|
|
stream_email_notify=False,
|
|
|
|
stream_name=None,
|
2021-05-24 06:11:59 +02:00
|
|
|
online_push_enabled=False,
|
2021-02-12 08:19:30 +01:00
|
|
|
idle=True,
|
|
|
|
already_notified={},
|
|
|
|
)
|
2019-08-26 04:40:07 +02:00
|
|
|
self.assertTrue(email_notice is not None)
|
|
|
|
self.assertTrue(mobile_notice is not None)
|
|
|
|
|
2019-09-03 23:27:45 +02:00
|
|
|
# Wildcard mention triggers both email and push notices (Like a
|
|
|
|
# direct mention, whether the notice is actually delivered is
|
|
|
|
# determined later, in the email/push notification code)
|
2019-08-26 04:40:07 +02:00
|
|
|
email_notice, mobile_notice = self.check_will_notify(
|
2021-02-12 08:19:30 +01:00
|
|
|
user_profile.id,
|
|
|
|
message_id,
|
|
|
|
private_message=False,
|
|
|
|
mentioned=False,
|
|
|
|
wildcard_mention_notify=True,
|
|
|
|
stream_push_notify=False,
|
|
|
|
stream_email_notify=False,
|
|
|
|
stream_name=None,
|
2021-05-24 06:11:59 +02:00
|
|
|
online_push_enabled=False,
|
2021-02-12 08:19:30 +01:00
|
|
|
idle=True,
|
|
|
|
already_notified={},
|
|
|
|
)
|
2017-09-27 20:27:04 +02:00
|
|
|
self.assertTrue(email_notice is not None)
|
|
|
|
self.assertTrue(mobile_notice is not None)
|
|
|
|
|
|
|
|
# stream_push_notify pushes but doesn't email
|
|
|
|
email_notice, mobile_notice = self.check_will_notify(
|
2021-02-12 08:19:30 +01:00
|
|
|
user_profile.id,
|
|
|
|
message_id,
|
|
|
|
private_message=False,
|
|
|
|
mentioned=False,
|
|
|
|
wildcard_mention_notify=False,
|
|
|
|
stream_push_notify=True,
|
|
|
|
stream_email_notify=False,
|
|
|
|
stream_name="Denmark",
|
2021-05-24 06:11:59 +02:00
|
|
|
online_push_enabled=False,
|
2021-02-12 08:19:30 +01:00
|
|
|
idle=True,
|
|
|
|
already_notified={},
|
|
|
|
)
|
2017-09-27 20:27:04 +02:00
|
|
|
self.assertTrue(email_notice is None)
|
|
|
|
self.assertTrue(mobile_notice is not None)
|
|
|
|
|
2017-11-21 07:24:24 +01:00
|
|
|
# stream_email_notify emails but doesn't push
|
|
|
|
email_notice, mobile_notice = self.check_will_notify(
|
2021-02-12 08:19:30 +01:00
|
|
|
user_profile.id,
|
|
|
|
message_id,
|
|
|
|
private_message=False,
|
|
|
|
mentioned=False,
|
|
|
|
wildcard_mention_notify=False,
|
|
|
|
stream_push_notify=False,
|
|
|
|
stream_email_notify=True,
|
|
|
|
stream_name="Denmark",
|
2021-05-24 06:11:59 +02:00
|
|
|
online_push_enabled=False,
|
2021-02-12 08:19:30 +01:00
|
|
|
idle=True,
|
|
|
|
already_notified={},
|
|
|
|
)
|
2017-11-21 07:24:24 +01:00
|
|
|
self.assertTrue(email_notice is not None)
|
|
|
|
self.assertTrue(mobile_notice is None)
|
|
|
|
|
2017-09-27 20:27:04 +02:00
|
|
|
# Private message doesn't send a notice if not idle
|
|
|
|
email_notice, mobile_notice = self.check_will_notify(
|
2021-02-12 08:19:30 +01:00
|
|
|
user_profile.id,
|
|
|
|
message_id,
|
|
|
|
private_message=True,
|
|
|
|
mentioned=False,
|
|
|
|
wildcard_mention_notify=False,
|
|
|
|
stream_push_notify=False,
|
|
|
|
stream_email_notify=True,
|
|
|
|
stream_name=None,
|
2021-05-24 06:11:59 +02:00
|
|
|
online_push_enabled=False,
|
2021-02-12 08:19:30 +01:00
|
|
|
idle=False,
|
|
|
|
already_notified={},
|
|
|
|
)
|
2019-08-26 04:40:07 +02:00
|
|
|
self.assertTrue(email_notice is None)
|
|
|
|
self.assertTrue(mobile_notice is None)
|
|
|
|
|
|
|
|
# Mention doesn't send a notice if not idle
|
|
|
|
email_notice, mobile_notice = self.check_will_notify(
|
2021-02-12 08:19:30 +01:00
|
|
|
user_profile.id,
|
|
|
|
message_id,
|
|
|
|
private_message=False,
|
|
|
|
mentioned=True,
|
|
|
|
wildcard_mention_notify=False,
|
|
|
|
stream_push_notify=False,
|
|
|
|
stream_email_notify=False,
|
|
|
|
stream_name=None,
|
2021-05-24 06:11:59 +02:00
|
|
|
online_push_enabled=False,
|
2021-02-12 08:19:30 +01:00
|
|
|
idle=False,
|
|
|
|
already_notified={},
|
|
|
|
)
|
2019-08-26 04:40:07 +02:00
|
|
|
self.assertTrue(email_notice is None)
|
|
|
|
self.assertTrue(mobile_notice is None)
|
|
|
|
|
|
|
|
# Wildcard mention doesn't send a notice if not idle
|
|
|
|
email_notice, mobile_notice = self.check_will_notify(
|
2021-02-12 08:19:30 +01:00
|
|
|
user_profile.id,
|
|
|
|
message_id,
|
|
|
|
private_message=False,
|
|
|
|
mentioned=False,
|
|
|
|
wildcard_mention_notify=True,
|
|
|
|
stream_push_notify=False,
|
|
|
|
stream_email_notify=False,
|
|
|
|
stream_name=None,
|
2021-05-24 06:11:59 +02:00
|
|
|
online_push_enabled=False,
|
2021-02-12 08:19:30 +01:00
|
|
|
idle=False,
|
|
|
|
already_notified={},
|
|
|
|
)
|
2017-09-27 20:27:04 +02:00
|
|
|
self.assertTrue(email_notice is None)
|
|
|
|
self.assertTrue(mobile_notice is None)
|
|
|
|
|
2021-05-24 06:11:59 +02:00
|
|
|
# Private message sends push but not email if not idle but online_push_enabled
|
2017-09-27 20:27:04 +02:00
|
|
|
email_notice, mobile_notice = self.check_will_notify(
|
2021-02-12 08:19:30 +01:00
|
|
|
user_profile.id,
|
|
|
|
message_id,
|
|
|
|
private_message=True,
|
|
|
|
mentioned=False,
|
|
|
|
wildcard_mention_notify=False,
|
|
|
|
stream_push_notify=False,
|
|
|
|
stream_email_notify=True,
|
|
|
|
stream_name=None,
|
2021-05-24 06:11:59 +02:00
|
|
|
online_push_enabled=True,
|
2021-02-12 08:19:30 +01:00
|
|
|
idle=False,
|
|
|
|
already_notified={},
|
|
|
|
)
|
2017-09-27 20:27:04 +02:00
|
|
|
self.assertTrue(email_notice is None)
|
|
|
|
self.assertTrue(mobile_notice is not None)
|
2017-09-27 20:55:58 +02:00
|
|
|
|
2021-05-24 06:11:59 +02:00
|
|
|
# Stream message sends push but not email if not idle but online_push_enabled
|
2019-04-18 03:58:25 +02:00
|
|
|
email_notice, mobile_notice = self.check_will_notify(
|
2021-02-12 08:19:30 +01:00
|
|
|
user_profile.id,
|
|
|
|
message_id,
|
|
|
|
private_message=False,
|
|
|
|
mentioned=False,
|
|
|
|
wildcard_mention_notify=False,
|
|
|
|
stream_push_notify=True,
|
|
|
|
stream_email_notify=True,
|
|
|
|
stream_name="Denmark",
|
2021-05-24 06:11:59 +02:00
|
|
|
online_push_enabled=True,
|
2021-02-12 08:19:30 +01:00
|
|
|
idle=False,
|
|
|
|
already_notified={},
|
|
|
|
)
|
2019-04-18 03:58:25 +02:00
|
|
|
self.assertTrue(email_notice is None)
|
|
|
|
self.assertTrue(mobile_notice is not None)
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
def tornado_call(
|
|
|
|
self,
|
|
|
|
view_func: Callable[[HttpRequest, UserProfile], HttpResponse],
|
|
|
|
user_profile: UserProfile,
|
|
|
|
post_data: Dict[str, Any],
|
|
|
|
) -> HttpResponse:
|
2021-02-07 21:34:01 +01:00
|
|
|
request = HostRequestMock(post_data, user_profile)
|
2017-09-27 20:55:58 +02:00
|
|
|
return view_func(request, user_profile)
|
|
|
|
|
2018-03-28 21:42:06 +02:00
|
|
|
def test_stream_watchers(self) -> None:
|
2021-02-12 08:19:30 +01:00
|
|
|
"""
|
2018-03-28 21:42:06 +02:00
|
|
|
We used to have a bug with stream_watchers, where we set their flags to
|
|
|
|
None.
|
2021-02-12 08:19:30 +01:00
|
|
|
"""
|
2021-02-12 08:20:45 +01:00
|
|
|
cordelia = self.example_user("cordelia")
|
|
|
|
hamlet = self.example_user("hamlet")
|
2018-03-28 21:42:06 +02:00
|
|
|
realm = hamlet.realm
|
2021-02-12 08:20:45 +01:00
|
|
|
stream_name = "Denmark"
|
2018-03-28 21:42:06 +02:00
|
|
|
|
|
|
|
self.unsubscribe(hamlet, stream_name)
|
|
|
|
|
|
|
|
queue_data = dict(
|
|
|
|
all_public_streams=True,
|
|
|
|
apply_markdown=True,
|
|
|
|
client_gravatar=True,
|
2021-02-12 08:20:45 +01:00
|
|
|
client_type_name="home grown API program",
|
|
|
|
event_types=["message"],
|
2018-03-28 21:42:06 +02:00
|
|
|
last_connection_time=time.time(),
|
|
|
|
queue_timeout=0,
|
|
|
|
realm_id=realm.id,
|
|
|
|
user_profile_id=hamlet.id,
|
|
|
|
)
|
|
|
|
|
|
|
|
client = allocate_client_descriptor(queue_data)
|
|
|
|
|
2020-03-07 11:43:05 +01:00
|
|
|
self.send_stream_message(cordelia, stream_name)
|
2018-03-28 21:42:06 +02:00
|
|
|
|
2021-05-17 05:41:32 +02:00
|
|
|
self.assert_length(client.event_queue.contents(), 1)
|
2018-03-28 21:42:06 +02:00
|
|
|
|
|
|
|
# This next line of code should silently succeed and basically do
|
|
|
|
# nothing under the covers. This test is here to prevent a bug
|
|
|
|
# from re-appearing.
|
|
|
|
missedmessage_hook(
|
|
|
|
user_profile_id=hamlet.id,
|
|
|
|
client=client,
|
|
|
|
last_for_client=True,
|
|
|
|
)
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_end_to_end_missedmessage_hook(self) -> None:
|
2017-09-27 20:55:58 +02:00
|
|
|
"""Tests what arguments missedmessage_hook passes into maybe_enqueue_notifications.
|
|
|
|
Combined with the previous test, this ensures that the missedmessage_hook is correct"""
|
2021-02-12 08:20:45 +01:00
|
|
|
user_profile = self.example_user("hamlet")
|
2019-12-11 01:41:20 +01:00
|
|
|
|
|
|
|
user_profile.enable_online_push_notifications = False
|
|
|
|
user_profile.save()
|
|
|
|
|
2019-09-03 23:27:45 +02:00
|
|
|
# Fetch the Denmark stream for testing
|
|
|
|
stream = get_stream("Denmark", user_profile.realm)
|
2021-02-12 08:19:30 +01:00
|
|
|
sub = Subscription.objects.get(
|
|
|
|
user_profile=user_profile,
|
|
|
|
recipient__type=Recipient.STREAM,
|
|
|
|
recipient__type_id=stream.id,
|
|
|
|
)
|
2019-09-03 23:27:45 +02:00
|
|
|
|
2020-03-06 18:40:46 +01:00
|
|
|
self.login_user(user_profile)
|
2017-09-27 20:55:58 +02:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
def change_subscription_properties(
|
|
|
|
user_profile: UserProfile,
|
|
|
|
stream: Stream,
|
|
|
|
sub: Subscription,
|
|
|
|
properties: Dict[str, bool],
|
|
|
|
) -> None:
|
2018-08-08 21:39:19 +02:00
|
|
|
for property_name, value in properties.items():
|
2021-04-08 02:41:57 +02:00
|
|
|
do_change_subscription_property(
|
|
|
|
user_profile, sub, stream, property_name, value, acting_user=None
|
|
|
|
)
|
2018-08-08 21:39:19 +02:00
|
|
|
|
2019-08-26 04:35:47 +02:00
|
|
|
def allocate_event_queue() -> ClientDescriptor:
|
2021-02-12 08:19:30 +01:00
|
|
|
result = self.tornado_call(
|
|
|
|
get_events,
|
|
|
|
user_profile,
|
|
|
|
{
|
|
|
|
"apply_markdown": orjson.dumps(True).decode(),
|
|
|
|
"client_gravatar": orjson.dumps(True).decode(),
|
|
|
|
"event_types": orjson.dumps(["message"]).decode(),
|
|
|
|
"user_client": "website",
|
|
|
|
"dont_block": orjson.dumps(True).decode(),
|
|
|
|
},
|
|
|
|
)
|
2019-08-26 04:35:47 +02:00
|
|
|
self.assert_json_success(result)
|
2020-08-07 01:09:47 +02:00
|
|
|
queue_id = orjson.loads(result.content)["queue_id"]
|
2019-08-26 04:35:47 +02:00
|
|
|
return get_client_descriptor(queue_id)
|
|
|
|
|
|
|
|
def destroy_event_queue(queue_id: str) -> None:
|
2021-02-12 08:19:30 +01:00
|
|
|
result = self.tornado_call(cleanup_event_queue, user_profile, {"queue_id": queue_id})
|
2019-08-26 04:35:47 +02:00
|
|
|
self.assert_json_success(result)
|
|
|
|
|
2021-05-31 10:52:32 +02:00
|
|
|
def assert_maybe_enqueue_notifications_call_args(
|
|
|
|
args_list: Collection[Any],
|
|
|
|
user_profile_id: int,
|
|
|
|
message_id: int,
|
|
|
|
private_message: bool,
|
|
|
|
mentioned: bool,
|
|
|
|
wildcard_mention_notify: bool,
|
|
|
|
stream_push_notify: bool,
|
|
|
|
stream_email_notify: bool,
|
|
|
|
stream_name: Optional[str],
|
|
|
|
online_push_enabled: bool,
|
|
|
|
idle: bool,
|
|
|
|
already_notified: Dict[str, bool],
|
|
|
|
) -> None:
|
|
|
|
self.assertEqual(
|
|
|
|
args_list,
|
|
|
|
(
|
|
|
|
user_profile_id,
|
|
|
|
message_id,
|
|
|
|
private_message,
|
|
|
|
mentioned,
|
|
|
|
wildcard_mention_notify,
|
|
|
|
stream_push_notify,
|
|
|
|
stream_email_notify,
|
|
|
|
stream_name,
|
|
|
|
online_push_enabled,
|
|
|
|
idle,
|
|
|
|
already_notified,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
2019-08-26 04:35:47 +02:00
|
|
|
client_descriptor = allocate_event_queue()
|
2017-09-27 20:55:58 +02:00
|
|
|
with mock.patch("zerver.tornado.event_queue.maybe_enqueue_notifications") as mock_enqueue:
|
|
|
|
# To test the missed_message hook, we first need to send a message
|
2020-03-07 11:43:05 +01:00
|
|
|
msg_id = self.send_stream_message(self.example_user("iago"), "Denmark")
|
2017-09-27 20:55:58 +02:00
|
|
|
|
|
|
|
# Verify that nothing happens if you call it as not the
|
|
|
|
# "last client descriptor", in which case the function
|
|
|
|
# short-circuits, since the `missedmessage_hook` handler
|
|
|
|
# for garbage-collection is only for the user's last queue.
|
|
|
|
missedmessage_hook(user_profile.id, client_descriptor, False)
|
|
|
|
mock_enqueue.assert_not_called()
|
|
|
|
|
|
|
|
# Now verify that we called the appropriate enqueue function
|
|
|
|
missedmessage_hook(user_profile.id, client_descriptor, True)
|
|
|
|
mock_enqueue.assert_called_once()
|
|
|
|
args_list = mock_enqueue.call_args_list[0][0]
|
|
|
|
|
2021-05-31 10:52:32 +02:00
|
|
|
assert_maybe_enqueue_notifications_call_args(
|
|
|
|
args_list=args_list,
|
|
|
|
user_profile_id=user_profile.id,
|
|
|
|
message_id=msg_id,
|
|
|
|
private_message=False,
|
|
|
|
mentioned=False,
|
|
|
|
wildcard_mention_notify=False,
|
|
|
|
stream_push_notify=False,
|
|
|
|
stream_email_notify=False,
|
|
|
|
stream_name="Denmark",
|
|
|
|
online_push_enabled=False,
|
|
|
|
idle=True,
|
|
|
|
already_notified={"email_notified": False, "push_notified": False},
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
2019-08-26 04:35:47 +02:00
|
|
|
destroy_event_queue(client_descriptor.event_queue.id)
|
2017-09-27 20:55:58 +02:00
|
|
|
|
2019-09-03 23:27:45 +02:00
|
|
|
# Test the hook with a private message; this should trigger notifications
|
2019-08-26 04:35:47 +02:00
|
|
|
client_descriptor = allocate_event_queue()
|
2017-09-27 20:55:58 +02:00
|
|
|
self.assertTrue(client_descriptor.event_queue.empty())
|
2020-03-07 11:43:05 +01:00
|
|
|
msg_id = self.send_personal_message(self.example_user("iago"), user_profile)
|
2017-09-27 20:55:58 +02:00
|
|
|
with mock.patch("zerver.tornado.event_queue.maybe_enqueue_notifications") as mock_enqueue:
|
|
|
|
missedmessage_hook(user_profile.id, client_descriptor, True)
|
|
|
|
mock_enqueue.assert_called_once()
|
|
|
|
args_list = mock_enqueue.call_args_list[0][0]
|
|
|
|
|
2021-05-31 10:52:32 +02:00
|
|
|
assert_maybe_enqueue_notifications_call_args(
|
|
|
|
args_list=args_list,
|
|
|
|
user_profile_id=user_profile.id,
|
|
|
|
message_id=msg_id,
|
|
|
|
private_message=True,
|
|
|
|
mentioned=False,
|
|
|
|
wildcard_mention_notify=False,
|
|
|
|
stream_push_notify=False,
|
|
|
|
stream_email_notify=False,
|
|
|
|
stream_name=None,
|
|
|
|
online_push_enabled=False,
|
|
|
|
idle=True,
|
|
|
|
already_notified={"email_notified": True, "push_notified": True},
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
2019-08-26 04:35:47 +02:00
|
|
|
destroy_event_queue(client_descriptor.event_queue.id)
|
2017-09-27 20:55:58 +02:00
|
|
|
|
2019-09-03 23:27:45 +02:00
|
|
|
# Test the hook with a mention; this should trigger notifications
|
2019-08-26 04:35:47 +02:00
|
|
|
client_descriptor = allocate_event_queue()
|
2017-09-27 20:55:58 +02:00
|
|
|
self.assertTrue(client_descriptor.event_queue.empty())
|
2021-02-12 08:19:30 +01:00
|
|
|
msg_id = self.send_stream_message(
|
|
|
|
self.example_user("iago"), "Denmark", content="@**King Hamlet** what's up?"
|
|
|
|
)
|
2017-09-27 20:55:58 +02:00
|
|
|
with mock.patch("zerver.tornado.event_queue.maybe_enqueue_notifications") as mock_enqueue:
|
|
|
|
missedmessage_hook(user_profile.id, client_descriptor, True)
|
|
|
|
mock_enqueue.assert_called_once()
|
|
|
|
args_list = mock_enqueue.call_args_list[0][0]
|
|
|
|
|
2021-05-31 10:52:32 +02:00
|
|
|
assert_maybe_enqueue_notifications_call_args(
|
|
|
|
args_list=args_list,
|
|
|
|
user_profile_id=user_profile.id,
|
|
|
|
message_id=msg_id,
|
|
|
|
private_message=False,
|
|
|
|
mentioned=True,
|
|
|
|
wildcard_mention_notify=False,
|
|
|
|
stream_push_notify=False,
|
|
|
|
stream_email_notify=False,
|
|
|
|
stream_name="Denmark",
|
|
|
|
online_push_enabled=False,
|
|
|
|
idle=True,
|
|
|
|
already_notified={"email_notified": True, "push_notified": True},
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
2019-08-26 04:40:07 +02:00
|
|
|
destroy_event_queue(client_descriptor.event_queue.id)
|
|
|
|
|
2019-09-03 23:27:45 +02:00
|
|
|
# Test the hook with a wildcard mention; this should trigger notifications
|
2019-08-26 04:40:07 +02:00
|
|
|
client_descriptor = allocate_event_queue()
|
|
|
|
self.assertTrue(client_descriptor.event_queue.empty())
|
2021-02-12 08:19:30 +01:00
|
|
|
msg_id = self.send_stream_message(
|
|
|
|
self.example_user("iago"), "Denmark", content="@**all** what's up?"
|
|
|
|
)
|
2019-08-26 04:40:07 +02:00
|
|
|
with mock.patch("zerver.tornado.event_queue.maybe_enqueue_notifications") as mock_enqueue:
|
|
|
|
missedmessage_hook(user_profile.id, client_descriptor, True)
|
|
|
|
mock_enqueue.assert_called_once()
|
|
|
|
args_list = mock_enqueue.call_args_list[0][0]
|
|
|
|
|
2021-05-31 10:52:32 +02:00
|
|
|
assert_maybe_enqueue_notifications_call_args(
|
|
|
|
args_list=args_list,
|
|
|
|
user_profile_id=user_profile.id,
|
|
|
|
message_id=msg_id,
|
|
|
|
private_message=False,
|
|
|
|
mentioned=False,
|
|
|
|
wildcard_mention_notify=True,
|
|
|
|
stream_push_notify=False,
|
|
|
|
stream_email_notify=False,
|
|
|
|
stream_name="Denmark",
|
|
|
|
online_push_enabled=False,
|
|
|
|
idle=True,
|
|
|
|
already_notified={"email_notified": True, "push_notified": True},
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
2019-08-26 04:35:47 +02:00
|
|
|
destroy_event_queue(client_descriptor.event_queue.id)
|
|
|
|
|
2019-09-03 23:27:45 +02:00
|
|
|
# Test the hook with a wildcard mention sent by the user
|
|
|
|
# themself using a human client; should not notify.
|
|
|
|
client_descriptor = allocate_event_queue()
|
|
|
|
self.assertTrue(client_descriptor.event_queue.empty())
|
2021-02-12 08:19:30 +01:00
|
|
|
msg_id = self.send_stream_message(
|
|
|
|
self.example_user("hamlet"),
|
|
|
|
"Denmark",
|
|
|
|
content="@**all** what's up?",
|
|
|
|
sending_client_name="website",
|
|
|
|
)
|
2019-09-03 23:27:45 +02:00
|
|
|
with mock.patch("zerver.tornado.event_queue.maybe_enqueue_notifications") as mock_enqueue:
|
|
|
|
missedmessage_hook(user_profile.id, client_descriptor, True)
|
|
|
|
mock_enqueue.assert_called_once()
|
|
|
|
args_list = mock_enqueue.call_args_list[0][0]
|
|
|
|
|
2021-05-31 10:52:32 +02:00
|
|
|
assert_maybe_enqueue_notifications_call_args(
|
|
|
|
args_list=args_list,
|
|
|
|
user_profile_id=user_profile.id,
|
|
|
|
message_id=msg_id,
|
|
|
|
private_message=False,
|
|
|
|
mentioned=False,
|
|
|
|
wildcard_mention_notify=False,
|
|
|
|
stream_push_notify=False,
|
|
|
|
stream_email_notify=False,
|
|
|
|
stream_name="Denmark",
|
|
|
|
online_push_enabled=False,
|
|
|
|
idle=True,
|
|
|
|
already_notified={"email_notified": False, "push_notified": False},
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
2019-09-03 23:27:45 +02:00
|
|
|
destroy_event_queue(client_descriptor.event_queue.id)
|
|
|
|
|
|
|
|
# Wildcard mentions in muted streams don't notify.
|
2021-02-12 08:20:45 +01:00
|
|
|
change_subscription_properties(user_profile, stream, sub, {"is_muted": True})
|
2019-09-03 23:27:45 +02:00
|
|
|
client_descriptor = allocate_event_queue()
|
|
|
|
self.assertTrue(client_descriptor.event_queue.empty())
|
2021-02-12 08:19:30 +01:00
|
|
|
msg_id = self.send_stream_message(
|
|
|
|
self.example_user("iago"), "Denmark", content="@**all** what's up?"
|
|
|
|
)
|
2019-09-03 23:27:45 +02:00
|
|
|
with mock.patch("zerver.tornado.event_queue.maybe_enqueue_notifications") as mock_enqueue:
|
|
|
|
missedmessage_hook(user_profile.id, client_descriptor, True)
|
|
|
|
mock_enqueue.assert_called_once()
|
|
|
|
args_list = mock_enqueue.call_args_list[0][0]
|
|
|
|
|
2021-05-31 10:52:32 +02:00
|
|
|
assert_maybe_enqueue_notifications_call_args(
|
|
|
|
args_list=args_list,
|
|
|
|
user_profile_id=user_profile.id,
|
|
|
|
message_id=msg_id,
|
|
|
|
private_message=False,
|
|
|
|
mentioned=False,
|
|
|
|
wildcard_mention_notify=False,
|
|
|
|
stream_push_notify=False,
|
|
|
|
stream_email_notify=False,
|
|
|
|
stream_name="Denmark",
|
|
|
|
online_push_enabled=False,
|
|
|
|
idle=True,
|
|
|
|
already_notified={"email_notified": False, "push_notified": False},
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
2019-09-03 23:27:45 +02:00
|
|
|
destroy_event_queue(client_descriptor.event_queue.id)
|
2021-02-12 08:20:45 +01:00
|
|
|
change_subscription_properties(user_profile, stream, sub, {"is_muted": False})
|
2019-09-03 23:27:45 +02:00
|
|
|
|
|
|
|
# With wildcard_mentions_notify=False, we treat the user as not mentioned.
|
|
|
|
user_profile.wildcard_mentions_notify = False
|
|
|
|
user_profile.save()
|
|
|
|
client_descriptor = allocate_event_queue()
|
|
|
|
self.assertTrue(client_descriptor.event_queue.empty())
|
2021-02-12 08:19:30 +01:00
|
|
|
msg_id = self.send_stream_message(
|
|
|
|
self.example_user("iago"), "Denmark", content="@**all** what's up?"
|
|
|
|
)
|
2019-09-03 23:27:45 +02:00
|
|
|
with mock.patch("zerver.tornado.event_queue.maybe_enqueue_notifications") as mock_enqueue:
|
|
|
|
missedmessage_hook(user_profile.id, client_descriptor, True)
|
|
|
|
mock_enqueue.assert_called_once()
|
|
|
|
args_list = mock_enqueue.call_args_list[0][0]
|
|
|
|
|
2021-05-31 10:52:32 +02:00
|
|
|
assert_maybe_enqueue_notifications_call_args(
|
|
|
|
args_list=args_list,
|
|
|
|
user_profile_id=user_profile.id,
|
|
|
|
message_id=msg_id,
|
|
|
|
private_message=False,
|
|
|
|
mentioned=False,
|
|
|
|
wildcard_mention_notify=False,
|
|
|
|
stream_push_notify=False,
|
|
|
|
stream_email_notify=False,
|
|
|
|
stream_name="Denmark",
|
|
|
|
online_push_enabled=False,
|
|
|
|
idle=True,
|
|
|
|
already_notified={"email_notified": False, "push_notified": False},
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
2019-09-03 23:27:45 +02:00
|
|
|
destroy_event_queue(client_descriptor.event_queue.id)
|
|
|
|
user_profile.wildcard_mentions_notify = True
|
|
|
|
user_profile.save()
|
2019-08-26 04:35:47 +02:00
|
|
|
|
2019-11-26 02:37:12 +01:00
|
|
|
# If wildcard_mentions_notify=True for a stream and False for a user, we treat the user
|
|
|
|
# as mentioned for that stream.
|
|
|
|
user_profile.wildcard_mentions_notify = False
|
|
|
|
sub.wildcard_mentions_notify = True
|
|
|
|
user_profile.save()
|
|
|
|
sub.save()
|
|
|
|
client_descriptor = allocate_event_queue()
|
|
|
|
self.assertTrue(client_descriptor.event_queue.empty())
|
2021-02-12 08:19:30 +01:00
|
|
|
msg_id = self.send_stream_message(
|
|
|
|
self.example_user("iago"), "Denmark", content="@**all** what's up?"
|
|
|
|
)
|
2019-11-26 02:37:12 +01:00
|
|
|
with mock.patch("zerver.tornado.event_queue.maybe_enqueue_notifications") as mock_enqueue:
|
|
|
|
missedmessage_hook(user_profile.id, client_descriptor, True)
|
|
|
|
mock_enqueue.assert_called_once()
|
|
|
|
args_list = mock_enqueue.call_args_list[0][0]
|
|
|
|
|
2021-05-31 10:52:32 +02:00
|
|
|
assert_maybe_enqueue_notifications_call_args(
|
|
|
|
args_list=args_list,
|
|
|
|
user_profile_id=user_profile.id,
|
|
|
|
message_id=msg_id,
|
|
|
|
private_message=False,
|
|
|
|
mentioned=False,
|
|
|
|
wildcard_mention_notify=True,
|
|
|
|
stream_push_notify=False,
|
|
|
|
stream_email_notify=False,
|
|
|
|
stream_name="Denmark",
|
|
|
|
online_push_enabled=False,
|
|
|
|
idle=True,
|
|
|
|
already_notified={"email_notified": True, "push_notified": True},
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
2019-11-26 02:37:12 +01:00
|
|
|
destroy_event_queue(client_descriptor.event_queue.id)
|
|
|
|
user_profile.wildcard_mentions_notify = True
|
|
|
|
sub.wildcard_mentions_notify = None
|
|
|
|
user_profile.save()
|
|
|
|
sub.save()
|
|
|
|
|
2019-08-26 04:35:47 +02:00
|
|
|
# Test the hook with a stream message with stream_push_notify
|
2021-02-12 08:20:45 +01:00
|
|
|
change_subscription_properties(user_profile, stream, sub, {"push_notifications": True})
|
2019-08-26 04:35:47 +02:00
|
|
|
client_descriptor = allocate_event_queue()
|
2017-09-28 00:04:32 +02:00
|
|
|
self.assertTrue(client_descriptor.event_queue.empty())
|
2021-02-12 08:19:30 +01:00
|
|
|
msg_id = self.send_stream_message(
|
|
|
|
self.example_user("iago"), "Denmark", content="what's up everyone?"
|
|
|
|
)
|
2017-09-28 00:04:32 +02:00
|
|
|
with mock.patch("zerver.tornado.event_queue.maybe_enqueue_notifications") as mock_enqueue:
|
|
|
|
missedmessage_hook(user_profile.id, client_descriptor, True)
|
|
|
|
mock_enqueue.assert_called_once()
|
|
|
|
args_list = mock_enqueue.call_args_list[0][0]
|
|
|
|
|
2021-05-31 10:52:32 +02:00
|
|
|
assert_maybe_enqueue_notifications_call_args(
|
|
|
|
args_list=args_list,
|
|
|
|
user_profile_id=user_profile.id,
|
|
|
|
message_id=msg_id,
|
|
|
|
private_message=False,
|
|
|
|
mentioned=False,
|
|
|
|
wildcard_mention_notify=False,
|
|
|
|
stream_push_notify=True,
|
|
|
|
stream_email_notify=False,
|
|
|
|
stream_name="Denmark",
|
|
|
|
online_push_enabled=False,
|
|
|
|
idle=True,
|
|
|
|
already_notified={"email_notified": False, "push_notified": False},
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
2019-08-26 04:35:47 +02:00
|
|
|
destroy_event_queue(client_descriptor.event_queue.id)
|
2017-11-21 07:24:24 +01:00
|
|
|
|
2019-08-26 04:35:47 +02:00
|
|
|
# Test the hook with a stream message with stream_email_notify
|
|
|
|
client_descriptor = allocate_event_queue()
|
2021-02-12 08:19:30 +01:00
|
|
|
change_subscription_properties(
|
2021-02-12 08:20:45 +01:00
|
|
|
user_profile, stream, sub, {"push_notifications": False, "email_notifications": True}
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
2017-11-21 07:24:24 +01:00
|
|
|
self.assertTrue(client_descriptor.event_queue.empty())
|
2021-02-12 08:19:30 +01:00
|
|
|
msg_id = self.send_stream_message(
|
|
|
|
self.example_user("iago"), "Denmark", content="what's up everyone?"
|
|
|
|
)
|
2017-11-21 07:24:24 +01:00
|
|
|
with mock.patch("zerver.tornado.event_queue.maybe_enqueue_notifications") as mock_enqueue:
|
|
|
|
missedmessage_hook(user_profile.id, client_descriptor, True)
|
|
|
|
mock_enqueue.assert_called_once()
|
|
|
|
args_list = mock_enqueue.call_args_list[0][0]
|
|
|
|
|
2021-05-31 10:52:32 +02:00
|
|
|
assert_maybe_enqueue_notifications_call_args(
|
|
|
|
args_list=args_list,
|
|
|
|
user_profile_id=user_profile.id,
|
|
|
|
message_id=msg_id,
|
|
|
|
private_message=False,
|
|
|
|
mentioned=False,
|
|
|
|
wildcard_mention_notify=False,
|
|
|
|
stream_push_notify=False,
|
|
|
|
stream_email_notify=True,
|
|
|
|
stream_name="Denmark",
|
|
|
|
online_push_enabled=False,
|
|
|
|
idle=True,
|
|
|
|
already_notified={"email_notified": False, "push_notified": False},
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
2019-08-26 04:35:47 +02:00
|
|
|
destroy_event_queue(client_descriptor.event_queue.id)
|
2017-09-28 00:04:32 +02:00
|
|
|
|
2019-08-26 04:35:47 +02:00
|
|
|
# Test the hook with stream message with stream_push_notify on
|
|
|
|
# a muted topic, which we should not push notify for
|
|
|
|
client_descriptor = allocate_event_queue()
|
2021-02-12 08:19:30 +01:00
|
|
|
change_subscription_properties(
|
2021-02-12 08:20:45 +01:00
|
|
|
user_profile, stream, sub, {"push_notifications": True, "email_notifications": False}
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
2019-08-26 04:35:47 +02:00
|
|
|
|
2017-10-18 07:49:03 +02:00
|
|
|
self.assertTrue(client_descriptor.event_queue.empty())
|
2020-10-16 17:29:05 +02:00
|
|
|
do_mute_topic(user_profile, stream, "mutingtest")
|
2021-02-12 08:19:30 +01:00
|
|
|
msg_id = self.send_stream_message(
|
|
|
|
self.example_user("iago"),
|
|
|
|
"Denmark",
|
|
|
|
content="what's up everyone?",
|
|
|
|
topic_name="mutingtest",
|
|
|
|
)
|
2017-10-18 07:49:03 +02:00
|
|
|
with mock.patch("zerver.tornado.event_queue.maybe_enqueue_notifications") as mock_enqueue:
|
|
|
|
missedmessage_hook(user_profile.id, client_descriptor, True)
|
|
|
|
mock_enqueue.assert_called_once()
|
|
|
|
args_list = mock_enqueue.call_args_list[0][0]
|
|
|
|
|
2021-05-31 10:52:32 +02:00
|
|
|
assert_maybe_enqueue_notifications_call_args(
|
|
|
|
args_list=args_list,
|
|
|
|
user_profile_id=user_profile.id,
|
|
|
|
message_id=msg_id,
|
|
|
|
private_message=False,
|
|
|
|
mentioned=False,
|
|
|
|
wildcard_mention_notify=False,
|
|
|
|
stream_push_notify=False,
|
|
|
|
stream_email_notify=False,
|
|
|
|
stream_name="Denmark",
|
|
|
|
online_push_enabled=False,
|
|
|
|
idle=True,
|
|
|
|
already_notified={"email_notified": False, "push_notified": False},
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
2019-08-26 04:35:47 +02:00
|
|
|
destroy_event_queue(client_descriptor.event_queue.id)
|
2017-10-18 07:49:03 +02:00
|
|
|
|
2019-08-26 04:35:47 +02:00
|
|
|
# Test the hook with stream message with stream_email_notify on
|
|
|
|
# a muted stream, which we should not push notify for
|
|
|
|
client_descriptor = allocate_event_queue()
|
2021-02-12 08:19:30 +01:00
|
|
|
change_subscription_properties(
|
2021-02-12 08:20:45 +01:00
|
|
|
user_profile, stream, sub, {"push_notifications": False, "email_notifications": True}
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
2019-08-26 04:35:47 +02:00
|
|
|
|
2017-10-18 07:38:54 +02:00
|
|
|
self.assertTrue(client_descriptor.event_queue.empty())
|
2021-02-12 08:20:45 +01:00
|
|
|
change_subscription_properties(user_profile, stream, sub, {"is_muted": True})
|
2021-02-12 08:19:30 +01:00
|
|
|
msg_id = self.send_stream_message(
|
|
|
|
self.example_user("iago"), "Denmark", content="what's up everyone?"
|
|
|
|
)
|
2017-10-18 07:38:54 +02:00
|
|
|
with mock.patch("zerver.tornado.event_queue.maybe_enqueue_notifications") as mock_enqueue:
|
|
|
|
missedmessage_hook(user_profile.id, client_descriptor, True)
|
|
|
|
mock_enqueue.assert_called_once()
|
|
|
|
args_list = mock_enqueue.call_args_list[0][0]
|
|
|
|
|
2021-05-31 10:52:32 +02:00
|
|
|
assert_maybe_enqueue_notifications_call_args(
|
|
|
|
args_list=args_list,
|
|
|
|
user_profile_id=user_profile.id,
|
|
|
|
message_id=msg_id,
|
|
|
|
private_message=False,
|
|
|
|
mentioned=False,
|
|
|
|
wildcard_mention_notify=False,
|
|
|
|
stream_push_notify=False,
|
|
|
|
stream_email_notify=False,
|
|
|
|
stream_name="Denmark",
|
|
|
|
online_push_enabled=False,
|
|
|
|
idle=True,
|
|
|
|
already_notified={"email_notified": False, "push_notified": False},
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
2019-08-26 04:35:47 +02:00
|
|
|
destroy_event_queue(client_descriptor.event_queue.id)
|
2017-10-18 07:38:54 +02:00
|
|
|
|
|
|
|
# Clean up the state we just changed (not necessary unless we add more test code below)
|
2021-02-12 08:19:30 +01:00
|
|
|
change_subscription_properties(
|
2021-02-12 08:20:45 +01:00
|
|
|
user_profile, stream, sub, {"push_notifications": True, "is_muted": False}
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
|
|
|
|
2018-10-04 00:22:39 +02:00
|
|
|
|
|
|
|
class FileReloadLogicTest(ZulipTestCase):
|
|
|
|
def test_persistent_queue_filename(self) -> None:
|
2021-02-12 08:19:30 +01:00
|
|
|
with self.settings(
|
|
|
|
JSON_PERSISTENT_QUEUE_FILENAME_PATTERN="/home/zulip/tornado/event_queues%s.json"
|
|
|
|
):
|
|
|
|
self.assertEqual(
|
|
|
|
persistent_queue_filename(9800), "/home/zulip/tornado/event_queues.json"
|
|
|
|
)
|
|
|
|
self.assertEqual(
|
|
|
|
persistent_queue_filename(9800, last=True),
|
|
|
|
"/home/zulip/tornado/event_queues.json.last",
|
|
|
|
)
|
|
|
|
with self.settings(
|
|
|
|
JSON_PERSISTENT_QUEUE_FILENAME_PATTERN="/home/zulip/tornado/event_queues%s.json",
|
|
|
|
TORNADO_PROCESSES=4,
|
|
|
|
):
|
|
|
|
self.assertEqual(
|
|
|
|
persistent_queue_filename(9800), "/home/zulip/tornado/event_queues.9800.json"
|
|
|
|
)
|
|
|
|
self.assertEqual(
|
|
|
|
persistent_queue_filename(9800, last=True),
|
|
|
|
"/home/zulip/tornado/event_queues.9800.last.json",
|
|
|
|
)
|
|
|
|
|
2019-08-02 21:50:27 +02:00
|
|
|
|
2020-12-22 10:43:34 +01:00
|
|
|
class PruneInternalDataTest(ZulipTestCase):
|
|
|
|
def test_prune_internal_data(self) -> None:
|
|
|
|
user_profile = self.example_user("hamlet")
|
|
|
|
queue_data = dict(
|
|
|
|
all_public_streams=True,
|
|
|
|
apply_markdown=True,
|
|
|
|
client_gravatar=True,
|
|
|
|
client_type_name="website",
|
|
|
|
event_types=["message"],
|
|
|
|
last_connection_time=time.time(),
|
|
|
|
queue_timeout=600,
|
|
|
|
realm_id=user_profile.realm.id,
|
|
|
|
user_profile_id=user_profile.id,
|
|
|
|
)
|
|
|
|
client = allocate_client_descriptor(queue_data)
|
|
|
|
self.assertTrue(client.event_queue.empty())
|
|
|
|
|
|
|
|
self.send_stream_message(
|
|
|
|
self.example_user("iago"), "Denmark", content="@**King Hamlet** what's up?"
|
|
|
|
)
|
|
|
|
self.send_stream_message(
|
|
|
|
self.example_user("iago"), "Denmark", content="@**all** what's up?"
|
|
|
|
)
|
|
|
|
self.send_personal_message(self.example_user("iago"), user_profile)
|
|
|
|
|
|
|
|
events = client.event_queue.contents()
|
2021-05-17 05:41:32 +02:00
|
|
|
self.assert_length(events, 3)
|
2020-12-22 10:43:34 +01:00
|
|
|
self.assertFalse("internal_data" in events[0])
|
|
|
|
self.assertFalse("internal_data" in events[1])
|
|
|
|
self.assertFalse("internal_data" in events[2])
|
|
|
|
|
|
|
|
events = client.event_queue.contents(include_internal_data=True)
|
|
|
|
self.assertTrue("internal_data" in events[0])
|
|
|
|
self.assertTrue("internal_data" in events[1])
|
|
|
|
self.assertTrue("internal_data" in events[2])
|
|
|
|
|
|
|
|
|
2019-08-02 21:50:27 +02:00
|
|
|
class EventQueueTest(ZulipTestCase):
|
2019-08-02 22:23:05 +02:00
|
|
|
def get_client_descriptor(self) -> ClientDescriptor:
|
2021-02-12 08:20:45 +01:00
|
|
|
hamlet = self.example_user("hamlet")
|
2019-08-02 22:23:05 +02:00
|
|
|
realm = hamlet.realm
|
|
|
|
queue_data = dict(
|
|
|
|
all_public_streams=False,
|
|
|
|
apply_markdown=False,
|
|
|
|
client_gravatar=True,
|
2021-02-12 08:20:45 +01:00
|
|
|
client_type_name="website",
|
2019-08-02 22:23:05 +02:00
|
|
|
event_types=None,
|
|
|
|
last_connection_time=time.time(),
|
|
|
|
queue_timeout=0,
|
|
|
|
realm_id=realm.id,
|
|
|
|
user_profile_id=hamlet.id,
|
|
|
|
)
|
|
|
|
|
|
|
|
client = allocate_client_descriptor(queue_data)
|
|
|
|
return client
|
|
|
|
|
|
|
|
def verify_to_dict_end_to_end(self, client: ClientDescriptor) -> None:
|
|
|
|
client_dict = client.to_dict()
|
|
|
|
new_client = ClientDescriptor.from_dict(client_dict)
|
|
|
|
self.assertEqual(client.to_dict(), new_client.to_dict())
|
|
|
|
|
2019-08-06 00:08:56 +02:00
|
|
|
client_dict = client.to_dict()
|
2021-02-12 08:20:45 +01:00
|
|
|
del client_dict["event_queue"]["newest_pruned_id"]
|
2019-08-06 00:08:56 +02:00
|
|
|
new_client = ClientDescriptor.from_dict(client_dict)
|
|
|
|
self.assertEqual(client_dict, new_client.to_dict())
|
|
|
|
|
2019-08-02 21:50:27 +02:00
|
|
|
def test_one_event(self) -> None:
|
2019-08-02 22:23:05 +02:00
|
|
|
client = self.get_client_descriptor()
|
|
|
|
queue = client.event_queue
|
2020-06-26 13:40:19 +02:00
|
|
|
in_dict = dict(
|
|
|
|
type="arbitrary",
|
|
|
|
x="foo",
|
|
|
|
y=42,
|
|
|
|
z=False,
|
|
|
|
timestamp="1",
|
|
|
|
)
|
|
|
|
out_dict = dict(
|
|
|
|
id=0,
|
|
|
|
**in_dict,
|
|
|
|
)
|
|
|
|
queue.push(in_dict)
|
2019-08-02 21:50:27 +02:00
|
|
|
self.assertFalse(queue.empty())
|
2019-08-02 22:23:05 +02:00
|
|
|
self.verify_to_dict_end_to_end(client)
|
2020-06-26 13:40:19 +02:00
|
|
|
self.assertEqual(queue.contents(), [out_dict])
|
2019-08-02 22:23:05 +02:00
|
|
|
self.verify_to_dict_end_to_end(client)
|
2019-08-02 21:50:27 +02:00
|
|
|
|
|
|
|
def test_event_collapsing(self) -> None:
|
2019-08-02 22:23:05 +02:00
|
|
|
client = self.get_client_descriptor()
|
|
|
|
queue = client.event_queue
|
2019-08-02 21:50:27 +02:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
"""
|
2020-06-26 14:14:36 +02:00
|
|
|
The update_message_flags events are special, because
|
|
|
|
they can be collapsed together. Given two umfe's, we:
|
|
|
|
* use the latest timestamp
|
|
|
|
* concatenate the messages
|
2021-02-12 08:20:45 +01:00
|
|
|
"""
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2020-06-26 14:14:36 +02:00
|
|
|
def umfe(timestamp: int, messages: List[int]) -> Dict[str, Any]:
|
|
|
|
return dict(
|
2021-02-12 08:20:45 +01:00
|
|
|
type="update_message_flags",
|
|
|
|
operation="add",
|
|
|
|
flag="read",
|
2020-06-26 14:14:36 +02:00
|
|
|
all=False,
|
|
|
|
timestamp=timestamp,
|
|
|
|
messages=messages,
|
|
|
|
)
|
|
|
|
|
|
|
|
events = [
|
|
|
|
umfe(timestamp=1, messages=[101]),
|
|
|
|
umfe(timestamp=2, messages=[201, 202]),
|
2021-02-12 08:20:45 +01:00
|
|
|
dict(type="unknown"),
|
|
|
|
dict(type="restart", server_generation="1"),
|
2020-06-26 14:14:36 +02:00
|
|
|
umfe(timestamp=3, messages=[301, 302, 303]),
|
2021-02-12 08:20:45 +01:00
|
|
|
dict(type="restart", server_generation="2"),
|
2020-06-26 14:14:36 +02:00
|
|
|
umfe(timestamp=4, messages=[401, 402, 403, 404]),
|
|
|
|
]
|
|
|
|
|
|
|
|
for event in events:
|
|
|
|
queue.push(event)
|
2019-08-02 22:23:05 +02:00
|
|
|
|
|
|
|
self.verify_to_dict_end_to_end(client)
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
self.assertEqual(
|
|
|
|
queue.contents(),
|
|
|
|
[
|
2021-02-12 08:20:45 +01:00
|
|
|
dict(id=2, type="unknown"),
|
|
|
|
dict(id=5, type="restart", server_generation="2"),
|
2021-02-12 08:19:30 +01:00
|
|
|
dict(
|
|
|
|
id=6,
|
2021-02-12 08:20:45 +01:00
|
|
|
type="update_message_flags",
|
|
|
|
operation="add",
|
|
|
|
flag="read",
|
2021-02-12 08:19:30 +01:00
|
|
|
all=False,
|
|
|
|
timestamp=4,
|
|
|
|
messages=[101, 201, 202, 301, 302, 303, 401, 402, 403, 404],
|
|
|
|
),
|
|
|
|
],
|
|
|
|
)
|
2019-08-02 22:23:05 +02:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
"""
|
2020-06-26 14:14:36 +02:00
|
|
|
Note that calling queue.contents() has the side
|
|
|
|
effect that we will no longer be able to collapse
|
|
|
|
the previous events, so the next event will just
|
|
|
|
get added to the queue, rather than collapsed.
|
2021-02-12 08:20:45 +01:00
|
|
|
"""
|
2020-06-26 14:14:36 +02:00
|
|
|
queue.push(
|
|
|
|
umfe(timestamp=5, messages=[501, 502, 503, 504, 505]),
|
|
|
|
)
|
2021-02-12 08:19:30 +01:00
|
|
|
self.assertEqual(
|
|
|
|
queue.contents(),
|
|
|
|
[
|
2021-02-12 08:20:45 +01:00
|
|
|
dict(id=2, type="unknown"),
|
|
|
|
dict(id=5, type="restart", server_generation="2"),
|
2021-02-12 08:19:30 +01:00
|
|
|
dict(
|
|
|
|
id=6,
|
2021-02-12 08:20:45 +01:00
|
|
|
type="update_message_flags",
|
|
|
|
operation="add",
|
|
|
|
flag="read",
|
2021-02-12 08:19:30 +01:00
|
|
|
all=False,
|
|
|
|
timestamp=4,
|
|
|
|
messages=[101, 201, 202, 301, 302, 303, 401, 402, 403, 404],
|
|
|
|
),
|
|
|
|
dict(
|
|
|
|
id=7,
|
2021-02-12 08:20:45 +01:00
|
|
|
type="update_message_flags",
|
|
|
|
operation="add",
|
|
|
|
flag="read",
|
2021-02-12 08:19:30 +01:00
|
|
|
all=False,
|
|
|
|
timestamp=5,
|
|
|
|
messages=[501, 502, 503, 504, 505],
|
|
|
|
),
|
|
|
|
],
|
|
|
|
)
|
2019-08-02 21:50:27 +02:00
|
|
|
|
|
|
|
def test_flag_add_collapsing(self) -> None:
|
2019-08-02 22:23:05 +02:00
|
|
|
client = self.get_client_descriptor()
|
|
|
|
queue = client.event_queue
|
2021-02-12 08:19:30 +01:00
|
|
|
queue.push(
|
|
|
|
{
|
|
|
|
"type": "update_message_flags",
|
|
|
|
"flag": "read",
|
|
|
|
"operation": "add",
|
|
|
|
"all": False,
|
|
|
|
"messages": [1, 2, 3, 4],
|
|
|
|
"timestamp": "1",
|
|
|
|
}
|
|
|
|
)
|
2019-08-02 22:23:05 +02:00
|
|
|
self.verify_to_dict_end_to_end(client)
|
2021-02-12 08:19:30 +01:00
|
|
|
queue.push(
|
|
|
|
{
|
|
|
|
"type": "update_message_flags",
|
|
|
|
"flag": "read",
|
|
|
|
"all": False,
|
|
|
|
"operation": "add",
|
|
|
|
"messages": [5, 6],
|
|
|
|
"timestamp": "1",
|
|
|
|
}
|
|
|
|
)
|
|
|
|
self.verify_to_dict_end_to_end(client)
|
|
|
|
self.assertEqual(
|
|
|
|
queue.contents(),
|
|
|
|
[
|
|
|
|
{
|
2021-02-12 08:20:45 +01:00
|
|
|
"id": 1,
|
|
|
|
"type": "update_message_flags",
|
2019-08-02 21:50:27 +02:00
|
|
|
"all": False,
|
2021-02-12 08:19:30 +01:00
|
|
|
"flag": "read",
|
2019-08-02 21:50:27 +02:00
|
|
|
"operation": "add",
|
2021-02-12 08:19:30 +01:00
|
|
|
"messages": [1, 2, 3, 4, 5, 6],
|
|
|
|
"timestamp": "1",
|
|
|
|
}
|
|
|
|
],
|
|
|
|
)
|
2019-08-02 22:23:05 +02:00
|
|
|
self.verify_to_dict_end_to_end(client)
|
2019-08-02 21:50:27 +02:00
|
|
|
|
|
|
|
def test_flag_remove_collapsing(self) -> None:
|
2019-08-02 22:23:05 +02:00
|
|
|
client = self.get_client_descriptor()
|
|
|
|
queue = client.event_queue
|
2021-02-12 08:19:30 +01:00
|
|
|
queue.push(
|
|
|
|
{
|
|
|
|
"type": "update_message_flags",
|
|
|
|
"flag": "collapsed",
|
|
|
|
"operation": "remove",
|
|
|
|
"all": False,
|
|
|
|
"messages": [1, 2, 3, 4],
|
|
|
|
"timestamp": "1",
|
|
|
|
}
|
|
|
|
)
|
2019-08-02 22:23:05 +02:00
|
|
|
self.verify_to_dict_end_to_end(client)
|
2021-02-12 08:19:30 +01:00
|
|
|
queue.push(
|
|
|
|
{
|
|
|
|
"type": "update_message_flags",
|
|
|
|
"flag": "collapsed",
|
|
|
|
"all": False,
|
|
|
|
"operation": "remove",
|
|
|
|
"messages": [5, 6],
|
|
|
|
"timestamp": "1",
|
|
|
|
}
|
|
|
|
)
|
|
|
|
self.verify_to_dict_end_to_end(client)
|
|
|
|
self.assertEqual(
|
|
|
|
queue.contents(),
|
|
|
|
[
|
|
|
|
{
|
2021-02-12 08:20:45 +01:00
|
|
|
"id": 1,
|
|
|
|
"type": "update_message_flags",
|
2019-08-02 21:50:27 +02:00
|
|
|
"all": False,
|
2021-02-12 08:19:30 +01:00
|
|
|
"flag": "collapsed",
|
2019-08-02 21:50:27 +02:00
|
|
|
"operation": "remove",
|
2021-02-12 08:19:30 +01:00
|
|
|
"messages": [1, 2, 3, 4, 5, 6],
|
|
|
|
"timestamp": "1",
|
|
|
|
}
|
|
|
|
],
|
|
|
|
)
|
2019-08-02 22:23:05 +02:00
|
|
|
self.verify_to_dict_end_to_end(client)
|
2019-08-02 21:50:27 +02:00
|
|
|
|
|
|
|
def test_collapse_event(self) -> None:
|
2021-02-12 08:19:30 +01:00
|
|
|
"""
|
2020-06-26 16:11:38 +02:00
|
|
|
This mostly focues on the internals of
|
|
|
|
how we store "virtual_events" that we
|
|
|
|
can collapse if subsequent events are
|
|
|
|
of the same form. See the code in
|
|
|
|
EventQueue.push for more context.
|
2021-02-12 08:19:30 +01:00
|
|
|
"""
|
2019-08-02 22:23:05 +02:00
|
|
|
client = self.get_client_descriptor()
|
|
|
|
queue = client.event_queue
|
2021-02-12 08:19:30 +01:00
|
|
|
queue.push({"type": "restart", "server_generation": 1, "timestamp": "1"})
|
2020-06-26 16:11:38 +02:00
|
|
|
# Verify the server_generation event is stored as a virtual event
|
2021-02-12 08:19:30 +01:00
|
|
|
self.assertEqual(
|
|
|
|
queue.virtual_events,
|
2021-02-12 08:20:45 +01:00
|
|
|
{"restart": {"id": 0, "type": "restart", "server_generation": 1, "timestamp": "1"}},
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
2019-08-02 22:23:05 +02:00
|
|
|
# And we can reconstruct newest_pruned_id etc.
|
|
|
|
self.verify_to_dict_end_to_end(client)
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
queue.push({"type": "unknown", "timestamp": "1"})
|
2021-02-12 08:20:45 +01:00
|
|
|
self.assertEqual(list(queue.queue), [{"id": 1, "type": "unknown", "timestamp": "1"}])
|
2021-02-12 08:19:30 +01:00
|
|
|
self.assertEqual(
|
|
|
|
queue.virtual_events,
|
2021-02-12 08:20:45 +01:00
|
|
|
{"restart": {"id": 0, "type": "restart", "server_generation": 1, "timestamp": "1"}},
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
2019-08-02 22:23:05 +02:00
|
|
|
# And we can still reconstruct newest_pruned_id etc. correctly
|
|
|
|
self.verify_to_dict_end_to_end(client)
|
|
|
|
|
|
|
|
# Verify virtual events are converted to real events by .contents()
|
2021-02-12 08:19:30 +01:00
|
|
|
self.assertEqual(
|
|
|
|
queue.contents(),
|
|
|
|
[
|
2021-02-12 08:20:45 +01:00
|
|
|
{"id": 0, "type": "restart", "server_generation": 1, "timestamp": "1"},
|
|
|
|
{"id": 1, "type": "unknown", "timestamp": "1"},
|
2021-02-12 08:19:30 +01:00
|
|
|
],
|
|
|
|
)
|
2019-08-02 22:23:05 +02:00
|
|
|
|
|
|
|
# And now verify to_dict after pruning
|
|
|
|
queue.prune(0)
|
|
|
|
self.verify_to_dict_end_to_end(client)
|
|
|
|
|
|
|
|
queue.prune(1)
|
|
|
|
self.verify_to_dict_end_to_end(client)
|