Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
tests: Check JSON serializability of test data with mock_queue_publish.
Alex Vandiverb1cac67c3138
diff --git a/zerver/tests/test_email_mirror.py b/zerver/tests/test_email_mirror.pyindex 5cf9b4b50a5..5dc05efeb04 100644--- a/zerver/tests/test_email_mirror.py+++ b/zerver/tests/test_email_mirror.py@@ -33,7 +33,7 @@ from zerver.lib.email_mirror_helpers import (from zerver.lib.email_notifications import convert_html_to_markdownfrom zerver.lib.send_email import FromAddressfrom zerver.lib.test_classes import ZulipTestCase-from zerver.lib.test_helpers import most_recent_message, most_recent_usermessage+from zerver.lib.test_helpers import mock_queue_publish, most_recent_message, most_recent_usermessagefrom zerver.models import (MissedMessageEmailAddress,Recipient,@@ -1174,9 +1174,7 @@ class TestEmailMirrorTornadoView(ZulipTestCase):user_message = most_recent_usermessage(user_profile)return create_missed_message_address(user_profile, user_message.message)- @mock.patch('zerver.lib.email_mirror.queue_json_publish')- def send_offline_message(self, to_address: str, sender: UserProfile,- mock_queue_json_publish: mock.Mock) -> HttpResponse:+ def send_offline_message(self, to_address: str, sender: UserProfile) -> HttpResponse:mail_template = self.fixture_data('simple.txt', type='email')mail = mail_template.format(stream_to_address=to_address, sender=sender.delivery_email)msg_base64 = base64.b64encode(mail.encode()).decode()@@ -1191,13 +1189,15 @@ class TestEmailMirrorTornadoView(ZulipTestCase):self.assertEqual(self.get_last_message().content,"This is a plain-text message for testing Zulip.")- mock_queue_json_publish.side_effect = check_queue_json_publishpost_data = {"rcpt_to": to_address,"msg_base64": msg_base64,"secret": settings.SHARED_SECRET,}- return self.client_post('/email_mirror_message', post_data)++ with mock_queue_publish('zerver.lib.email_mirror.queue_json_publish') as m:+ m.side_effect = check_queue_json_publish+ return self.client_post('/email_mirror_message', post_data)def test_success_stream(self) -> None:stream = get_stream("Denmark", get_realm("zulip"))diff --git a/zerver/tests/test_event_queue.py b/zerver/tests/test_event_queue.pyindex 1149e64b214..50497337c59 100644--- a/zerver/tests/test_event_queue.py+++ b/zerver/tests/test_event_queue.py@@ -7,7 +7,7 @@ from django.http import HttpRequest, HttpResponsefrom zerver.lib.actions import do_change_subscription_property, do_mute_topicfrom zerver.lib.test_classes import ZulipTestCase-from zerver.lib.test_helpers import POSTRequestMock+from zerver.lib.test_helpers import POSTRequestMock, mock_queue_publishfrom zerver.models import Recipient, Stream, Subscription, UserProfile, get_streamfrom zerver.tornado.event_queue import (ClientDescriptor,@@ -27,9 +27,9 @@ class MissedMessageNotificationsTest(ZulipTestCase):def check_will_notify(self, *args: Any, **kwargs: Any) -> Tuple[str, str]:email_notice = Nonemobile_notice = None- with mock.patch("zerver.tornado.event_queue.queue_json_publish") as mock_queue_publish:+ with mock_queue_publish("zerver.tornado.event_queue.queue_json_publish") as mock_queue_json_publish:notified = maybe_enqueue_notifications(*args, **kwargs)- for entry in mock_queue_publish.call_args_list:+ for entry in mock_queue_json_publish.call_args_list:args = entry[0]if args[0] == "missedmessage_mobile_notifications":mobile_notice = args[1]diff --git a/zerver/tests/test_link_embed.py b/zerver/tests/test_link_embed.pyindex 6beeaac5533..e2ba1d777cc 100644--- a/zerver/tests/test_link_embed.py+++ b/zerver/tests/test_link_embed.py@@ -9,7 +9,7 @@ from requests.exceptions import ConnectionErrorfrom zerver.lib.actions import queue_json_publishfrom zerver.lib.cache import NotFoundInCache, cache_set, preview_url_cache_keyfrom zerver.lib.test_classes import ZulipTestCase-from zerver.lib.test_helpers import MockPythonResponse+from zerver.lib.test_helpers import MockPythonResponse, mock_queue_publishfrom zerver.lib.url_preview.oembed import get_oembed_data, strip_cdatafrom zerver.lib.url_preview.parsers import GenericParser, OpenGraphParserfrom zerver.lib.url_preview.preview import get_link_embed_data, link_embed_data_from_cache@@ -284,7 +284,7 @@ class PreviewTestCase(ZulipTestCase):url = 'http://test.org/'mocked_response = mock.Mock(side_effect=self.create_mock_response(url))- with mock.patch('zerver.views.message_edit.queue_json_publish') as patched:+ with mock_queue_publish('zerver.views.message_edit.queue_json_publish') as patched:result = self.client_patch("/json/messages/" + str(msg_id), {'message_id': msg_id, 'content': url,})@@ -309,7 +309,7 @@ class PreviewTestCase(ZulipTestCase):def _send_message_with_test_org_url(self, sender: UserProfile, queue_should_run: bool=True,relative_url: bool=False) -> Message:url = 'http://test.org/'- with mock.patch('zerver.lib.actions.queue_json_publish') as patched:+ with mock_queue_publish('zerver.lib.actions.queue_json_publish') as patched:msg_id = self.send_personal_message(sender,self.example_user('cordelia'),@@ -352,7 +352,7 @@ class PreviewTestCase(ZulipTestCase):self.login_user(user)original_url = 'http://test.org/'edited_url = 'http://edited.org/'- with mock.patch('zerver.lib.actions.queue_json_publish') as patched:+ with mock_queue_publish('zerver.lib.actions.queue_json_publish') as patched:msg_id = self.send_stream_message(user, "Scotland",topic_name="foo", content=original_url)patched.assert_called_once()@@ -392,7 +392,7 @@ class PreviewTestCase(ZulipTestCase):'INFO:root:Time spent on get_link_embed_data for http://edited.org/: ' in info_logs.output[0])- with mock.patch('zerver.views.message_edit.queue_json_publish', wraps=wrapped_queue_json_publish) as patched:+ with mock_queue_publish('zerver.views.message_edit.queue_json_publish', wraps=wrapped_queue_json_publish):result = self.client_patch("/json/messages/" + str(msg_id), {'message_id': msg_id, 'content': edited_url,})@@ -432,7 +432,7 @@ class PreviewTestCase(ZulipTestCase):@override_settings(INLINE_URL_EMBED_PREVIEW=True)def test_inline_relative_url_embed_preview(self) -> None:# Relative urls should not be sent for url preview.- with mock.patch('zerver.lib.actions.queue_json_publish') as patched:+ with mock_queue_publish('zerver.lib.actions.queue_json_publish') as patched:self.send_personal_message(self.example_user('prospero'),self.example_user('cordelia'),@@ -494,7 +494,7 @@ class PreviewTestCase(ZulipTestCase):user = self.example_user('hamlet')self.login_user(user)url = 'http://test.org/audio.mp3'- with mock.patch('zerver.lib.actions.queue_json_publish') as patched:+ with mock_queue_publish('zerver.lib.actions.queue_json_publish') as patched:msg_id = self.send_stream_message(user, "Scotland", topic_name="foo", content=url)patched.assert_called_once()queue = patched.call_args[0][0]@@ -524,7 +524,7 @@ class PreviewTestCase(ZulipTestCase):user = self.example_user('hamlet')self.login_user(user)url = 'http://test.org/foo.html'- with mock.patch('zerver.lib.actions.queue_json_publish') as patched:+ with mock_queue_publish('zerver.lib.actions.queue_json_publish') as patched:msg_id = self.send_stream_message(user, "Scotland", topic_name="foo", content=url)patched.assert_called_once()queue = patched.call_args[0][0]@@ -555,7 +555,7 @@ class PreviewTestCase(ZulipTestCase):user = self.example_user('hamlet')self.login_user(user)url = 'http://test.org/foo.html'- with mock.patch('zerver.lib.actions.queue_json_publish') as patched:+ with mock_queue_publish('zerver.lib.actions.queue_json_publish') as patched:msg_id = self.send_stream_message(user, "Scotland", topic_name="foo", content=url)patched.assert_called_once()queue = patched.call_args[0][0]@@ -587,7 +587,7 @@ class PreviewTestCase(ZulipTestCase):user = self.example_user('hamlet')self.login_user(user)url = 'http://test.org/'- with mock.patch('zerver.lib.actions.queue_json_publish') as patched:+ with mock_queue_publish('zerver.lib.actions.queue_json_publish') as patched:msg_id = self.send_stream_message(user, "Scotland", topic_name="foo", content=url)patched.assert_called_once()queue = patched.call_args[0][0]@@ -614,7 +614,7 @@ class PreviewTestCase(ZulipTestCase):@override_settings(INLINE_URL_EMBED_PREVIEW=True)def test_valid_content_type_error_get_data(self) -> None:url = 'http://test.org/'- with mock.patch('zerver.lib.actions.queue_json_publish'):+ with mock_queue_publish('zerver.lib.actions.queue_json_publish'):msg_id = self.send_personal_message(self.example_user('hamlet'),self.example_user('cordelia'),@@ -649,7 +649,7 @@ class PreviewTestCase(ZulipTestCase):def test_invalid_url(self) -> None:url = 'http://test.org/'error_url = 'http://test.org/x'- with mock.patch('zerver.lib.actions.queue_json_publish'):+ with mock_queue_publish('zerver.lib.actions.queue_json_publish'):msg_id = self.send_personal_message(self.example_user('hamlet'),self.example_user('cordelia'),@@ -681,7 +681,7 @@ class PreviewTestCase(ZulipTestCase):@override_settings(INLINE_URL_EMBED_PREVIEW=True)def test_safe_oembed_html_url(self) -> None:url = 'http://test.org/'- with mock.patch('zerver.lib.actions.queue_json_publish'):+ with mock_queue_publish('zerver.lib.actions.queue_json_publish'):msg_id = self.send_personal_message(self.example_user('hamlet'),self.example_user('cordelia'),@@ -714,7 +714,7 @@ class PreviewTestCase(ZulipTestCase):@override_settings(INLINE_URL_EMBED_PREVIEW=True)def test_youtube_url_title_replaces_url(self) -> None:url = 'https://www.youtube.com/watch?v=eSJTXC7Ixgg'- with mock.patch('zerver.lib.actions.queue_json_publish'):+ with mock_queue_publish('zerver.lib.actions.queue_json_publish'):msg_id = self.send_personal_message(self.example_user('hamlet'),self.example_user('cordelia'),diff --git a/zerver/tests/test_logging_handlers.py b/zerver/tests/test_logging_handlers.pyindex b020beb511e..4e50fbd0244 100644--- a/zerver/tests/test_logging_handlers.py+++ b/zerver/tests/test_logging_handlers.py@@ -11,6 +11,7 @@ from django.http import HttpRequestfrom django.utils.log import AdminEmailHandlerfrom zerver.lib.test_classes import ZulipTestCase+from zerver.lib.test_helpers import mock_queue_publishfrom zerver.lib.types import ViewFuncTfrom zerver.logging_handlers import AdminNotifyHandler, HasRequest@@ -188,8 +189,8 @@ class AdminNotifyHandlerTest(ZulipTestCase):side_effect=Exception("queue error")):self.handler.emit(record)with self.settings(STAGING_ERROR_NOTIFICATIONS=False):- with patch('zerver.logging_handlers.queue_json_publish',- side_effect=Exception("queue error")):+ with mock_queue_publish('zerver.logging_handlers.queue_json_publish',+ side_effect=Exception("queue error")):self.handler.emit(record)# Test no exc_infodiff --git a/zerver/tests/test_message_edit_notifications.py b/zerver/tests/test_message_edit_notifications.pyindex 25ce6c1a290..9ad6b4171b9 100644--- a/zerver/tests/test_message_edit_notifications.py+++ b/zerver/tests/test_message_edit_notifications.py@@ -6,6 +6,7 @@ from django.utils.timezone import now as timezone_nowfrom zerver.lib.actions import get_clientfrom zerver.lib.push_notifications import get_apns_badge_count, get_apns_badge_count_futurefrom zerver.lib.test_classes import ZulipTestCase+from zerver.lib.test_helpers import mock_queue_publishfrom zerver.models import Subscription, UserPresencefrom zerver.tornado.event_queue import maybe_enqueue_notifications@@ -124,8 +125,7 @@ class EditMessageSideEffectsTest(ZulipTestCase):event=event,))- with mock.patch('zerver.tornado.event_queue.queue_json_publish') as m:- m.side_effect = fake_publish+ with mock_queue_publish('zerver.tornado.event_queue.queue_json_publish', side_effect=fake_publish) as m:maybe_enqueue_notifications(**enqueue_kwargs)self.assert_json_success(result)diff --git a/zerver/tests/test_push_notifications.py b/zerver/tests/test_push_notifications.pyindex c80d3e7c755..182140efd24 100644--- a/zerver/tests/test_push_notifications.py+++ b/zerver/tests/test_push_notifications.py@@ -58,6 +58,7 @@ from zerver.lib.remote_server import (from zerver.lib.request import JsonableErrorfrom zerver.lib.soft_deactivation import do_soft_deactivate_usersfrom zerver.lib.test_classes import ZulipTestCase+from zerver.lib.test_helpers import mock_queue_publishfrom zerver.models import (Message,PushDeviceToken,@@ -1994,7 +1995,7 @@ class TestClearOnRead(ZulipTestCase):flags=F('flags').bitor(UserMessage.flags.active_mobile_push_notification))- with mock.patch("zerver.lib.actions.queue_json_publish") as mock_publish:+ with mock_queue_publish("zerver.lib.actions.queue_json_publish") as mock_publish:do_mark_stream_messages_as_read(hamlet, self.client, stream)queue_items = [c[0][1] for c in mock_publish.call_args_list]groups = [item['message_ids'] for item in queue_items]diff --git a/zerver/tests/test_queue_worker.py b/zerver/tests/test_queue_worker.pyindex 79ccff0bbe2..45c45846b31 100644--- a/zerver/tests/test_queue_worker.py+++ b/zerver/tests/test_queue_worker.py@@ -2,7 +2,7 @@ import base64import osimport smtplibimport time-from typing import Any, Callable, Dict, List, Mapping, Tuple+from typing import Any, Callable, Dict, List, Mapping, Optional, Tuplefrom unittest.mock import MagicMock, patchimport orjson@@ -16,7 +16,7 @@ from zerver.lib.rate_limiter import RateLimiterLockingExceptionfrom zerver.lib.remote_server import PushNotificationBouncerRetryLaterErrorfrom zerver.lib.send_email import FromAddressfrom zerver.lib.test_classes import ZulipTestCase-from zerver.lib.test_helpers import simulated_queue_client+from zerver.lib.test_helpers import mock_queue_publish, simulated_queue_clientfrom zerver.models import PreregistrationUser, UserActivity, get_client, get_realm, get_streamfrom zerver.tornado.event_queue import build_offline_notificationfrom zerver.worker import queue_processors@@ -298,7 +298,7 @@ class WorkerTest(ZulipTestCase):fake_client.queue.append(('missedmessage_mobile_notifications', event_new))fake_client.queue.append(('missedmessage_mobile_notifications', event_remove))- with patch('zerver.lib.queue.queue_json_publish', side_effect=fake_publish), \+ with mock_queue_publish('zerver.lib.queue.queue_json_publish', side_effect=fake_publish), \self.assertLogs('zerver.worker.queue_processors', 'WARNING') as warn_logs:worker.start()self.assertEqual(mock_handle_new.call_count, 1 + MAX_REQUEST_RETRIES)@@ -413,7 +413,7 @@ class WorkerTest(ZulipTestCase):def fake_publish(queue_name: str,event: Dict[str, Any],- processor: Callable[[Any], None]) -> None:+ processor: Optional[Callable[[Any], None]]) -> None:fake_client.queue.append((queue_name, event))with simulated_queue_client(lambda: fake_client):@@ -421,8 +421,8 @@ class WorkerTest(ZulipTestCase):worker.setup()with patch('zerver.lib.send_email.build_email',side_effect=smtplib.SMTPServerDisconnected), \- patch('zerver.lib.queue.queue_json_publish',- side_effect=fake_publish), \+ mock_queue_publish('zerver.lib.queue.queue_json_publish',+ side_effect=fake_publish), \patch('logging.exception'):worker.start()@@ -436,7 +436,7 @@ class WorkerTest(ZulipTestCase):data = {'user_id': user_id, 'id': 'test_missed'}fake_client.queue.append(('signups', data))- def fake_publish(queue_name: str, event: Dict[str, Any], processor: Callable[[Any], None]) -> None:+ def fake_publish(queue_name: str, event: Dict[str, Any], processor: Optional[Callable[[Any], None]]) -> None:fake_client.queue.append((queue_name, event))fake_response = MagicMock()@@ -447,8 +447,8 @@ class WorkerTest(ZulipTestCase):worker.setup()with patch('zerver.worker.queue_processors.requests.post',return_value=fake_response), \- patch('zerver.lib.queue.queue_json_publish',- side_effect=fake_publish), \+ mock_queue_publish('zerver.lib.queue.queue_json_publish',+ side_effect=fake_publish), \patch('logging.info'), \self.settings(MAILCHIMP_API_KEY='one-two',PRODUCTION=True,diff --git a/zerver/tests/test_report.py b/zerver/tests/test_report.pyindex c41eefc643a..ffada6bb6bf 100644--- a/zerver/tests/test_report.py+++ b/zerver/tests/test_report.py@@ -5,6 +5,7 @@ import orjsonfrom django.test import override_settingsfrom zerver.lib.test_classes import ZulipTestCase+from zerver.lib.test_helpers import mock_queue_publishfrom zerver.lib.utils import statsd@@ -109,12 +110,11 @@ class TestReport(ZulipTestCase):more_info=dict(foo='bar', draft_content="**draft**"),))- publish_mock = mock.patch('zerver.views.report.queue_json_publish')subprocess_mock = mock.patch('zerver.views.report.subprocess.check_output',side_effect=KeyError('foo'),)- with publish_mock as m, subprocess_mock:+ with mock_queue_publish('zerver.views.report.queue_json_publish') as m, subprocess_mock:result = self.client_post("/json/report/error", params)self.assert_json_success(result)@@ -127,7 +127,7 @@ class TestReport(ZulipTestCase):# Teset with no more_infodel params['more_info']- with publish_mock as m, subprocess_mock:+ with mock_queue_publish('zerver.views.report.queue_json_publish') as m, subprocess_mock:result = self.client_post("/json/report/error", params)self.assert_json_success(result)diff --git a/zerver/tests/test_service_bot_system.py b/zerver/tests/test_service_bot_system.pyindex 97305cdf46c..eb190ef1186 100644--- a/zerver/tests/test_service_bot_system.py+++ b/zerver/tests/test_service_bot_system.py@@ -1,4 +1,4 @@-from typing import Any, Mapping, Union+from typing import Any, Callable, Dict, Optionalfrom unittest import mockimport orjson@@ -10,6 +10,7 @@ from zerver.lib.bot_config import ConfigError, load_bot_config_template, set_botfrom zerver.lib.bot_lib import EmbeddedBotEmptyRecipientsList, EmbeddedBotHandler, StateHandlerfrom zerver.lib.bot_storage import StateErrorfrom zerver.lib.test_classes import ZulipTestCase+from zerver.lib.test_helpers import patch_queue_publishfrom zerver.lib.validator import check_stringfrom zerver.models import Recipient, UserProfile, get_realm@@ -403,7 +404,7 @@ class TestServiceBotEventTriggers(ZulipTestCase):bot_type=UserProfile.OUTGOING_WEBHOOK_BOT,bot_owner=self.user_profile)- @mock.patch('zerver.lib.actions.queue_json_publish')+ @patch_queue_publish('zerver.lib.actions.queue_json_publish')def test_trigger_on_stream_mention_from_user(self, mock_queue_json_publish: mock.Mock) -> None:for bot_type, expected_queue_name in BOT_TYPE_TO_QUEUE_NAME.items():self.bot_profile.bot_type = bot_type@@ -416,7 +417,8 @@ class TestServiceBotEventTriggers(ZulipTestCase):def check_values_passed(queue_name: Any,- trigger_event: Union[Mapping[Any, Any], Any],+ trigger_event: Dict[str, Any],+ processor: Optional[Callable[[Any], None]] = None,) -> None:self.assertEqual(queue_name, expected_queue_name)self.assertEqual(trigger_event["message"]["content"], content)@@ -433,13 +435,13 @@ class TestServiceBotEventTriggers(ZulipTestCase):content)self.assertTrue(mock_queue_json_publish.called)- @mock.patch('zerver.lib.actions.queue_json_publish')+ @patch_queue_publish('zerver.lib.actions.queue_json_publish')def test_no_trigger_on_stream_message_without_mention(self, mock_queue_json_publish: mock.Mock) -> None:sender = self.user_profileself.send_stream_message(sender, "Denmark")self.assertFalse(mock_queue_json_publish.called)- @mock.patch('zerver.lib.actions.queue_json_publish')+ @patch_queue_publish('zerver.lib.actions.queue_json_publish')def test_no_trigger_on_stream_mention_from_bot(self, mock_queue_json_publish: mock.Mock) -> None:for bot_type in BOT_TYPE_TO_QUEUE_NAME:self.bot_profile.bot_type = bot_type@@ -451,7 +453,7 @@ class TestServiceBotEventTriggers(ZulipTestCase):'@**FooBot** foo bar!!!')self.assertFalse(mock_queue_json_publish.called)- @mock.patch('zerver.lib.actions.queue_json_publish')+ @patch_queue_publish('zerver.lib.actions.queue_json_publish')def test_trigger_on_personal_message_from_user(self, mock_queue_json_publish: mock.Mock) -> None:for bot_type, expected_queue_name in BOT_TYPE_TO_QUEUE_NAME.items():self.bot_profile.bot_type = bot_type@@ -462,7 +464,8 @@ class TestServiceBotEventTriggers(ZulipTestCase):def check_values_passed(queue_name: Any,- trigger_event: Union[Mapping[Any, Any], Any],+ trigger_event: Dict[str, Any],+ processor: Optional[Callable[[Any], None]] = None,) -> None:self.assertEqual(queue_name, expected_queue_name)self.assertEqual(trigger_event["user_profile_id"], self.bot_profile.id)@@ -479,7 +482,7 @@ class TestServiceBotEventTriggers(ZulipTestCase):self.send_personal_message(sender, recipient, 'test')self.assertTrue(mock_queue_json_publish.called)- @mock.patch('zerver.lib.actions.queue_json_publish')+ @patch_queue_publish('zerver.lib.actions.queue_json_publish')def test_no_trigger_on_personal_message_from_bot(self, mock_queue_json_publish: mock.Mock) -> None:for bot_type in BOT_TYPE_TO_QUEUE_NAME:self.bot_profile.bot_type = bot_type@@ -490,7 +493,7 @@ class TestServiceBotEventTriggers(ZulipTestCase):self.send_personal_message(sender, recipient)self.assertFalse(mock_queue_json_publish.called)- @mock.patch('zerver.lib.actions.queue_json_publish')+ @patch_queue_publish('zerver.lib.actions.queue_json_publish')def test_trigger_on_huddle_message_from_user(self, mock_queue_json_publish: mock.Mock) -> None:for bot_type, expected_queue_name in BOT_TYPE_TO_QUEUE_NAME.items():self.bot_profile.bot_type = bot_type@@ -505,7 +508,8 @@ class TestServiceBotEventTriggers(ZulipTestCase):def check_values_passed(queue_name: Any,- trigger_event: Union[Mapping[Any, Any], Any],+ trigger_event: Dict[str, Any],+ processor: Optional[Callable[[Any], None]] = None,) -> None:self.assertEqual(queue_name, expected_queue_name)self.assertIn(trigger_event["user_profile_id"], profile_ids)@@ -519,7 +523,7 @@ class TestServiceBotEventTriggers(ZulipTestCase):self.assertEqual(mock_queue_json_publish.call_count, 2)mock_queue_json_publish.reset_mock()- @mock.patch('zerver.lib.actions.queue_json_publish')+ @patch_queue_publish('zerver.lib.actions.queue_json_publish')def test_no_trigger_on_huddle_message_from_bot(self, mock_queue_json_publish: mock.Mock) -> None:for bot_type in BOT_TYPE_TO_QUEUE_NAME:self.bot_profile.bot_type = bot_type