test_event_queue: Make subscription management code readable.

This test refactor makes the subscription/stream settings changes use standard
APIs and thus be easier to follow (and more robust to subtle re-fetching bugs).

This is a follow-up to #9181.
This commit is contained in:
Shubham Dhama
2018-08-09 01:09:19 +05:30
committed by Tim Abbott
parent 7b92b836a7
commit 4d07e485ff

View File

@@ -5,10 +5,10 @@ import ujson
from django.http import HttpRequest, HttpResponse
from typing import Any, Callable, Dict, Tuple
from zerver.lib.actions import do_mute_topic
from zerver.lib.actions import do_mute_topic, do_change_subscription_property
from zerver.lib.test_classes import ZulipTestCase
from zerver.lib.test_helpers import POSTRequestMock
from zerver.models import Recipient, Subscription, UserProfile, get_stream
from zerver.models import Recipient, Stream, Subscription, UserProfile, get_stream
from zerver.tornado.event_queue import maybe_enqueue_notifications, \
allocate_client_descriptor, process_message_event, clear_client_event_queues_for_testing, \
get_client_descriptor, missedmessage_hook
@@ -175,6 +175,11 @@ class MissedMessageNotificationsTest(ZulipTestCase):
email = user_profile.email
self.login(email)
def change_subscription_properties(user_profile: UserProfile, stream: Stream, sub: Subscription,
properties: Dict[str, bool]) -> None:
for property_name, value in properties.items():
do_change_subscription_property(user_profile, sub, stream, property_name, value)
result = self.tornado_call(get_events, user_profile,
{"apply_markdown": ujson.dumps(True),
"client_gravatar": ujson.dumps(True),
@@ -234,12 +239,12 @@ class MissedMessageNotificationsTest(ZulipTestCase):
False, "Denmark", False, True,
{'email_notified': True, 'push_notified': True}))
# Clear the event queue, now repeat with stream message with stream_push_notify
stream = get_stream("Denmark", user_profile.realm)
sub = Subscription.objects.get(user_profile=user_profile, recipient__type=Recipient.STREAM,
recipient__type_id=stream.id)
sub.push_notifications = True
sub.save()
# Clear the event queue, now repeat with stream message with stream_push_notify
change_subscription_properties(user_profile, stream, sub, {'push_notifications': True})
client_descriptor.event_queue.pop()
self.assertTrue(client_descriptor.event_queue.empty())
msg_id = self.send_stream_message(self.example_email("iago"), "Denmark",
@@ -255,12 +260,9 @@ class MissedMessageNotificationsTest(ZulipTestCase):
{'email_notified': False, 'push_notified': False}))
# Clear the event queue, now repeat with stream message with stream_email_notify
stream = get_stream("Denmark", user_profile.realm)
sub = Subscription.objects.get(user_profile=user_profile, recipient__type=Recipient.STREAM,
recipient__type_id=stream.id)
sub.email_notifications = True
sub.push_notifications = False
sub.save()
change_subscription_properties(user_profile, stream, sub,
{'push_notifications': False,
'email_notifications': True})
client_descriptor.event_queue.pop()
self.assertTrue(client_descriptor.event_queue.empty())
msg_id = self.send_stream_message(self.example_email("iago"), "Denmark",
@@ -277,9 +279,9 @@ class MissedMessageNotificationsTest(ZulipTestCase):
# Clear the event queue, now repeat with stream message with stream_push_notify
# on a muted topic, which we should not push notify for
sub.email_notifications = False
sub.push_notifications = True
sub.save()
change_subscription_properties(user_profile, stream, sub,
{'push_notifications': True,
'email_notifications': False})
client_descriptor.event_queue.pop()
self.assertTrue(client_descriptor.event_queue.empty())
do_mute_topic(user_profile, stream, sub.recipient, "mutingtest")
@@ -297,13 +299,12 @@ class MissedMessageNotificationsTest(ZulipTestCase):
# Clear the event queue, now repeat with stream message with stream_email_notify
# on a muted stream, which we should not email notify for
sub.email_notifications = True
sub.push_notifications = False
sub.save()
change_subscription_properties(user_profile, stream, sub,
{'push_notifications': False,
'email_notifications': True})
client_descriptor.event_queue.pop()
self.assertTrue(client_descriptor.event_queue.empty())
sub.in_home_view = False
sub.save()
change_subscription_properties(user_profile, stream, sub, {'in_home_view': False})
msg_id = self.send_stream_message(self.example_email("iago"), "Denmark",
content="what's up everyone?")
with mock.patch("zerver.tornado.event_queue.maybe_enqueue_notifications") as mock_enqueue:
@@ -317,6 +318,6 @@ class MissedMessageNotificationsTest(ZulipTestCase):
{'email_notified': False, 'push_notified': False}))
# Clean up the state we just changed (not necessary unless we add more test code below)
sub.push_notifications = True
sub.in_home_view = True
sub.save()
change_subscription_properties(user_profile, stream, sub,
{'push_notifications': True,
'in_home_view': True})