tests: Check JSON serializability of test data with mock_queue_publish.

This commit is contained in:
Alex Vandiver
2020-08-14 01:03:36 -07:00
committed by Tim Abbott
parent 660c2e5383
commit b1cac67c31
9 changed files with 57 additions and 51 deletions

View File

@@ -33,7 +33,7 @@ from zerver.lib.email_mirror_helpers import (
from zerver.lib.email_notifications import convert_html_to_markdown from zerver.lib.email_notifications import convert_html_to_markdown
from zerver.lib.send_email import FromAddress from zerver.lib.send_email import FromAddress
from zerver.lib.test_classes import ZulipTestCase from 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_usermessage
from zerver.models import ( from zerver.models import (
MissedMessageEmailAddress, MissedMessageEmailAddress,
Recipient, Recipient,
@@ -1174,9 +1174,7 @@ class TestEmailMirrorTornadoView(ZulipTestCase):
user_message = most_recent_usermessage(user_profile) user_message = most_recent_usermessage(user_profile)
return create_missed_message_address(user_profile, user_message.message) 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) -> HttpResponse:
def send_offline_message(self, to_address: str, sender: UserProfile,
mock_queue_json_publish: mock.Mock) -> HttpResponse:
mail_template = self.fixture_data('simple.txt', type='email') mail_template = self.fixture_data('simple.txt', type='email')
mail = mail_template.format(stream_to_address=to_address, sender=sender.delivery_email) mail = mail_template.format(stream_to_address=to_address, sender=sender.delivery_email)
msg_base64 = base64.b64encode(mail.encode()).decode() msg_base64 = base64.b64encode(mail.encode()).decode()
@@ -1191,12 +1189,14 @@ class TestEmailMirrorTornadoView(ZulipTestCase):
self.assertEqual(self.get_last_message().content, self.assertEqual(self.get_last_message().content,
"This is a plain-text message for testing Zulip.") "This is a plain-text message for testing Zulip.")
mock_queue_json_publish.side_effect = check_queue_json_publish
post_data = { post_data = {
"rcpt_to": to_address, "rcpt_to": to_address,
"msg_base64": msg_base64, "msg_base64": msg_base64,
"secret": settings.SHARED_SECRET, "secret": settings.SHARED_SECRET,
} }
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) return self.client_post('/email_mirror_message', post_data)
def test_success_stream(self) -> None: def test_success_stream(self) -> None:

View File

@@ -7,7 +7,7 @@ from django.http import HttpRequest, HttpResponse
from zerver.lib.actions import do_change_subscription_property, do_mute_topic from zerver.lib.actions import do_change_subscription_property, do_mute_topic
from zerver.lib.test_classes import ZulipTestCase from zerver.lib.test_classes import ZulipTestCase
from zerver.lib.test_helpers import POSTRequestMock from zerver.lib.test_helpers import POSTRequestMock, mock_queue_publish
from zerver.models import Recipient, Stream, Subscription, UserProfile, get_stream from zerver.models import Recipient, Stream, Subscription, UserProfile, get_stream
from zerver.tornado.event_queue import ( from zerver.tornado.event_queue import (
ClientDescriptor, ClientDescriptor,
@@ -27,9 +27,9 @@ class MissedMessageNotificationsTest(ZulipTestCase):
def check_will_notify(self, *args: Any, **kwargs: Any) -> Tuple[str, str]: def check_will_notify(self, *args: Any, **kwargs: Any) -> Tuple[str, str]:
email_notice = None email_notice = None
mobile_notice = None mobile_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) 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] args = entry[0]
if args[0] == "missedmessage_mobile_notifications": if args[0] == "missedmessage_mobile_notifications":
mobile_notice = args[1] mobile_notice = args[1]

View File

@@ -9,7 +9,7 @@ from requests.exceptions import ConnectionError
from zerver.lib.actions import queue_json_publish from zerver.lib.actions import queue_json_publish
from zerver.lib.cache import NotFoundInCache, cache_set, preview_url_cache_key from zerver.lib.cache import NotFoundInCache, cache_set, preview_url_cache_key
from zerver.lib.test_classes import ZulipTestCase from zerver.lib.test_classes import ZulipTestCase
from zerver.lib.test_helpers import MockPythonResponse from zerver.lib.test_helpers import MockPythonResponse, mock_queue_publish
from zerver.lib.url_preview.oembed import get_oembed_data, strip_cdata from zerver.lib.url_preview.oembed import get_oembed_data, strip_cdata
from zerver.lib.url_preview.parsers import GenericParser, OpenGraphParser from zerver.lib.url_preview.parsers import GenericParser, OpenGraphParser
from zerver.lib.url_preview.preview import get_link_embed_data, link_embed_data_from_cache from 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/' url = 'http://test.org/'
mocked_response = mock.Mock(side_effect=self.create_mock_response(url)) 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), { result = self.client_patch("/json/messages/" + str(msg_id), {
'message_id': msg_id, 'content': url, '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, def _send_message_with_test_org_url(self, sender: UserProfile, queue_should_run: bool=True,
relative_url: bool=False) -> Message: relative_url: bool=False) -> Message:
url = 'http://test.org/' 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( msg_id = self.send_personal_message(
sender, sender,
self.example_user('cordelia'), self.example_user('cordelia'),
@@ -352,7 +352,7 @@ class PreviewTestCase(ZulipTestCase):
self.login_user(user) self.login_user(user)
original_url = 'http://test.org/' original_url = 'http://test.org/'
edited_url = 'http://edited.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", msg_id = self.send_stream_message(user, "Scotland",
topic_name="foo", content=original_url) topic_name="foo", content=original_url)
patched.assert_called_once() 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] '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), { result = self.client_patch("/json/messages/" + str(msg_id), {
'message_id': msg_id, 'content': edited_url, 'message_id': msg_id, 'content': edited_url,
}) })
@@ -432,7 +432,7 @@ class PreviewTestCase(ZulipTestCase):
@override_settings(INLINE_URL_EMBED_PREVIEW=True) @override_settings(INLINE_URL_EMBED_PREVIEW=True)
def test_inline_relative_url_embed_preview(self) -> None: def test_inline_relative_url_embed_preview(self) -> None:
# Relative urls should not be sent for url preview. # 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.send_personal_message(
self.example_user('prospero'), self.example_user('prospero'),
self.example_user('cordelia'), self.example_user('cordelia'),
@@ -494,7 +494,7 @@ class PreviewTestCase(ZulipTestCase):
user = self.example_user('hamlet') user = self.example_user('hamlet')
self.login_user(user) self.login_user(user)
url = 'http://test.org/audio.mp3' 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) msg_id = self.send_stream_message(user, "Scotland", topic_name="foo", content=url)
patched.assert_called_once() patched.assert_called_once()
queue = patched.call_args[0][0] queue = patched.call_args[0][0]
@@ -524,7 +524,7 @@ class PreviewTestCase(ZulipTestCase):
user = self.example_user('hamlet') user = self.example_user('hamlet')
self.login_user(user) self.login_user(user)
url = 'http://test.org/foo.html' 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) msg_id = self.send_stream_message(user, "Scotland", topic_name="foo", content=url)
patched.assert_called_once() patched.assert_called_once()
queue = patched.call_args[0][0] queue = patched.call_args[0][0]
@@ -555,7 +555,7 @@ class PreviewTestCase(ZulipTestCase):
user = self.example_user('hamlet') user = self.example_user('hamlet')
self.login_user(user) self.login_user(user)
url = 'http://test.org/foo.html' 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) msg_id = self.send_stream_message(user, "Scotland", topic_name="foo", content=url)
patched.assert_called_once() patched.assert_called_once()
queue = patched.call_args[0][0] queue = patched.call_args[0][0]
@@ -587,7 +587,7 @@ class PreviewTestCase(ZulipTestCase):
user = self.example_user('hamlet') user = self.example_user('hamlet')
self.login_user(user) self.login_user(user)
url = 'http://test.org/' 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) msg_id = self.send_stream_message(user, "Scotland", topic_name="foo", content=url)
patched.assert_called_once() patched.assert_called_once()
queue = patched.call_args[0][0] queue = patched.call_args[0][0]
@@ -614,7 +614,7 @@ class PreviewTestCase(ZulipTestCase):
@override_settings(INLINE_URL_EMBED_PREVIEW=True) @override_settings(INLINE_URL_EMBED_PREVIEW=True)
def test_valid_content_type_error_get_data(self) -> None: def test_valid_content_type_error_get_data(self) -> None:
url = 'http://test.org/' 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( msg_id = self.send_personal_message(
self.example_user('hamlet'), self.example_user('hamlet'),
self.example_user('cordelia'), self.example_user('cordelia'),
@@ -649,7 +649,7 @@ class PreviewTestCase(ZulipTestCase):
def test_invalid_url(self) -> None: def test_invalid_url(self) -> None:
url = 'http://test.org/' url = 'http://test.org/'
error_url = 'http://test.org/x' 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( msg_id = self.send_personal_message(
self.example_user('hamlet'), self.example_user('hamlet'),
self.example_user('cordelia'), self.example_user('cordelia'),
@@ -681,7 +681,7 @@ class PreviewTestCase(ZulipTestCase):
@override_settings(INLINE_URL_EMBED_PREVIEW=True) @override_settings(INLINE_URL_EMBED_PREVIEW=True)
def test_safe_oembed_html_url(self) -> None: def test_safe_oembed_html_url(self) -> None:
url = 'http://test.org/' 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( msg_id = self.send_personal_message(
self.example_user('hamlet'), self.example_user('hamlet'),
self.example_user('cordelia'), self.example_user('cordelia'),
@@ -714,7 +714,7 @@ class PreviewTestCase(ZulipTestCase):
@override_settings(INLINE_URL_EMBED_PREVIEW=True) @override_settings(INLINE_URL_EMBED_PREVIEW=True)
def test_youtube_url_title_replaces_url(self) -> None: def test_youtube_url_title_replaces_url(self) -> None:
url = 'https://www.youtube.com/watch?v=eSJTXC7Ixgg' 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( msg_id = self.send_personal_message(
self.example_user('hamlet'), self.example_user('hamlet'),
self.example_user('cordelia'), self.example_user('cordelia'),

View File

@@ -11,6 +11,7 @@ from django.http import HttpRequest
from django.utils.log import AdminEmailHandler from django.utils.log import AdminEmailHandler
from zerver.lib.test_classes import ZulipTestCase from zerver.lib.test_classes import ZulipTestCase
from zerver.lib.test_helpers import mock_queue_publish
from zerver.lib.types import ViewFuncT from zerver.lib.types import ViewFuncT
from zerver.logging_handlers import AdminNotifyHandler, HasRequest from zerver.logging_handlers import AdminNotifyHandler, HasRequest
@@ -188,7 +189,7 @@ class AdminNotifyHandlerTest(ZulipTestCase):
side_effect=Exception("queue error")): side_effect=Exception("queue error")):
self.handler.emit(record) self.handler.emit(record)
with self.settings(STAGING_ERROR_NOTIFICATIONS=False): with self.settings(STAGING_ERROR_NOTIFICATIONS=False):
with patch('zerver.logging_handlers.queue_json_publish', with mock_queue_publish('zerver.logging_handlers.queue_json_publish',
side_effect=Exception("queue error")): side_effect=Exception("queue error")):
self.handler.emit(record) self.handler.emit(record)

View File

@@ -6,6 +6,7 @@ from django.utils.timezone import now as timezone_now
from zerver.lib.actions import get_client from zerver.lib.actions import get_client
from zerver.lib.push_notifications import get_apns_badge_count, get_apns_badge_count_future from zerver.lib.push_notifications import get_apns_badge_count, get_apns_badge_count_future
from zerver.lib.test_classes import ZulipTestCase from zerver.lib.test_classes import ZulipTestCase
from zerver.lib.test_helpers import mock_queue_publish
from zerver.models import Subscription, UserPresence from zerver.models import Subscription, UserPresence
from zerver.tornado.event_queue import maybe_enqueue_notifications from zerver.tornado.event_queue import maybe_enqueue_notifications
@@ -124,8 +125,7 @@ class EditMessageSideEffectsTest(ZulipTestCase):
event=event, event=event,
)) ))
with mock.patch('zerver.tornado.event_queue.queue_json_publish') as m: with mock_queue_publish('zerver.tornado.event_queue.queue_json_publish', side_effect=fake_publish) as m:
m.side_effect = fake_publish
maybe_enqueue_notifications(**enqueue_kwargs) maybe_enqueue_notifications(**enqueue_kwargs)
self.assert_json_success(result) self.assert_json_success(result)

View File

@@ -58,6 +58,7 @@ from zerver.lib.remote_server import (
from zerver.lib.request import JsonableError from zerver.lib.request import JsonableError
from zerver.lib.soft_deactivation import do_soft_deactivate_users from zerver.lib.soft_deactivation import do_soft_deactivate_users
from zerver.lib.test_classes import ZulipTestCase from zerver.lib.test_classes import ZulipTestCase
from zerver.lib.test_helpers import mock_queue_publish
from zerver.models import ( from zerver.models import (
Message, Message,
PushDeviceToken, PushDeviceToken,
@@ -1994,7 +1995,7 @@ class TestClearOnRead(ZulipTestCase):
flags=F('flags').bitor( flags=F('flags').bitor(
UserMessage.flags.active_mobile_push_notification)) 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) do_mark_stream_messages_as_read(hamlet, self.client, stream)
queue_items = [c[0][1] for c in mock_publish.call_args_list] queue_items = [c[0][1] for c in mock_publish.call_args_list]
groups = [item['message_ids'] for item in queue_items] groups = [item['message_ids'] for item in queue_items]

View File

@@ -2,7 +2,7 @@ import base64
import os import os
import smtplib import smtplib
import time import time
from typing import Any, Callable, Dict, List, Mapping, Tuple from typing import Any, Callable, Dict, List, Mapping, Optional, Tuple
from unittest.mock import MagicMock, patch from unittest.mock import MagicMock, patch
import orjson import orjson
@@ -16,7 +16,7 @@ from zerver.lib.rate_limiter import RateLimiterLockingException
from zerver.lib.remote_server import PushNotificationBouncerRetryLaterError from zerver.lib.remote_server import PushNotificationBouncerRetryLaterError
from zerver.lib.send_email import FromAddress from zerver.lib.send_email import FromAddress
from zerver.lib.test_classes import ZulipTestCase from 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_client
from zerver.models import PreregistrationUser, UserActivity, get_client, get_realm, get_stream from zerver.models import PreregistrationUser, UserActivity, get_client, get_realm, get_stream
from zerver.tornado.event_queue import build_offline_notification from zerver.tornado.event_queue import build_offline_notification
from zerver.worker import queue_processors from 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_new))
fake_client.queue.append(('missedmessage_mobile_notifications', event_remove)) 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: self.assertLogs('zerver.worker.queue_processors', 'WARNING') as warn_logs:
worker.start() worker.start()
self.assertEqual(mock_handle_new.call_count, 1 + MAX_REQUEST_RETRIES) self.assertEqual(mock_handle_new.call_count, 1 + MAX_REQUEST_RETRIES)
@@ -413,7 +413,7 @@ class WorkerTest(ZulipTestCase):
def fake_publish(queue_name: str, def fake_publish(queue_name: str,
event: Dict[str, Any], event: Dict[str, Any],
processor: Callable[[Any], None]) -> None: processor: Optional[Callable[[Any], None]]) -> None:
fake_client.queue.append((queue_name, event)) fake_client.queue.append((queue_name, event))
with simulated_queue_client(lambda: fake_client): with simulated_queue_client(lambda: fake_client):
@@ -421,7 +421,7 @@ class WorkerTest(ZulipTestCase):
worker.setup() worker.setup()
with patch('zerver.lib.send_email.build_email', with patch('zerver.lib.send_email.build_email',
side_effect=smtplib.SMTPServerDisconnected), \ side_effect=smtplib.SMTPServerDisconnected), \
patch('zerver.lib.queue.queue_json_publish', mock_queue_publish('zerver.lib.queue.queue_json_publish',
side_effect=fake_publish), \ side_effect=fake_publish), \
patch('logging.exception'): patch('logging.exception'):
worker.start() worker.start()
@@ -436,7 +436,7 @@ class WorkerTest(ZulipTestCase):
data = {'user_id': user_id, 'id': 'test_missed'} data = {'user_id': user_id, 'id': 'test_missed'}
fake_client.queue.append(('signups', data)) 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_client.queue.append((queue_name, event))
fake_response = MagicMock() fake_response = MagicMock()
@@ -447,7 +447,7 @@ class WorkerTest(ZulipTestCase):
worker.setup() worker.setup()
with patch('zerver.worker.queue_processors.requests.post', with patch('zerver.worker.queue_processors.requests.post',
return_value=fake_response), \ return_value=fake_response), \
patch('zerver.lib.queue.queue_json_publish', mock_queue_publish('zerver.lib.queue.queue_json_publish',
side_effect=fake_publish), \ side_effect=fake_publish), \
patch('logging.info'), \ patch('logging.info'), \
self.settings(MAILCHIMP_API_KEY='one-two', self.settings(MAILCHIMP_API_KEY='one-two',

View File

@@ -5,6 +5,7 @@ import orjson
from django.test import override_settings from django.test import override_settings
from zerver.lib.test_classes import ZulipTestCase from zerver.lib.test_classes import ZulipTestCase
from zerver.lib.test_helpers import mock_queue_publish
from zerver.lib.utils import statsd from zerver.lib.utils import statsd
@@ -109,12 +110,11 @@ class TestReport(ZulipTestCase):
more_info=dict(foo='bar', draft_content="**draft**"), more_info=dict(foo='bar', draft_content="**draft**"),
)) ))
publish_mock = mock.patch('zerver.views.report.queue_json_publish')
subprocess_mock = mock.patch( subprocess_mock = mock.patch(
'zerver.views.report.subprocess.check_output', 'zerver.views.report.subprocess.check_output',
side_effect=KeyError('foo'), 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) result = self.client_post("/json/report/error", params)
self.assert_json_success(result) self.assert_json_success(result)
@@ -127,7 +127,7 @@ class TestReport(ZulipTestCase):
# Teset with no more_info # Teset with no more_info
del params['more_info'] del 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) result = self.client_post("/json/report/error", params)
self.assert_json_success(result) self.assert_json_success(result)

View File

@@ -1,4 +1,4 @@
from typing import Any, Mapping, Union from typing import Any, Callable, Dict, Optional
from unittest import mock from unittest import mock
import orjson import orjson
@@ -10,6 +10,7 @@ from zerver.lib.bot_config import ConfigError, load_bot_config_template, set_bot
from zerver.lib.bot_lib import EmbeddedBotEmptyRecipientsList, EmbeddedBotHandler, StateHandler from zerver.lib.bot_lib import EmbeddedBotEmptyRecipientsList, EmbeddedBotHandler, StateHandler
from zerver.lib.bot_storage import StateError from zerver.lib.bot_storage import StateError
from zerver.lib.test_classes import ZulipTestCase from zerver.lib.test_classes import ZulipTestCase
from zerver.lib.test_helpers import patch_queue_publish
from zerver.lib.validator import check_string from zerver.lib.validator import check_string
from zerver.models import Recipient, UserProfile, get_realm from zerver.models import Recipient, UserProfile, get_realm
@@ -403,7 +404,7 @@ class TestServiceBotEventTriggers(ZulipTestCase):
bot_type=UserProfile.OUTGOING_WEBHOOK_BOT, bot_type=UserProfile.OUTGOING_WEBHOOK_BOT,
bot_owner=self.user_profile) 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: 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(): for bot_type, expected_queue_name in BOT_TYPE_TO_QUEUE_NAME.items():
self.bot_profile.bot_type = bot_type self.bot_profile.bot_type = bot_type
@@ -416,7 +417,8 @@ class TestServiceBotEventTriggers(ZulipTestCase):
def check_values_passed( def check_values_passed(
queue_name: Any, queue_name: Any,
trigger_event: Union[Mapping[Any, Any], Any], trigger_event: Dict[str, Any],
processor: Optional[Callable[[Any], None]] = None,
) -> None: ) -> None:
self.assertEqual(queue_name, expected_queue_name) self.assertEqual(queue_name, expected_queue_name)
self.assertEqual(trigger_event["message"]["content"], content) self.assertEqual(trigger_event["message"]["content"], content)
@@ -433,13 +435,13 @@ class TestServiceBotEventTriggers(ZulipTestCase):
content) content)
self.assertTrue(mock_queue_json_publish.called) 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: def test_no_trigger_on_stream_message_without_mention(self, mock_queue_json_publish: mock.Mock) -> None:
sender = self.user_profile sender = self.user_profile
self.send_stream_message(sender, "Denmark") self.send_stream_message(sender, "Denmark")
self.assertFalse(mock_queue_json_publish.called) 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: 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: for bot_type in BOT_TYPE_TO_QUEUE_NAME:
self.bot_profile.bot_type = bot_type self.bot_profile.bot_type = bot_type
@@ -451,7 +453,7 @@ class TestServiceBotEventTriggers(ZulipTestCase):
'@**FooBot** foo bar!!!') '@**FooBot** foo bar!!!')
self.assertFalse(mock_queue_json_publish.called) 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: 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(): for bot_type, expected_queue_name in BOT_TYPE_TO_QUEUE_NAME.items():
self.bot_profile.bot_type = bot_type self.bot_profile.bot_type = bot_type
@@ -462,7 +464,8 @@ class TestServiceBotEventTriggers(ZulipTestCase):
def check_values_passed( def check_values_passed(
queue_name: Any, queue_name: Any,
trigger_event: Union[Mapping[Any, Any], Any], trigger_event: Dict[str, Any],
processor: Optional[Callable[[Any], None]] = None,
) -> None: ) -> None:
self.assertEqual(queue_name, expected_queue_name) self.assertEqual(queue_name, expected_queue_name)
self.assertEqual(trigger_event["user_profile_id"], self.bot_profile.id) 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.send_personal_message(sender, recipient, 'test')
self.assertTrue(mock_queue_json_publish.called) 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: 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: for bot_type in BOT_TYPE_TO_QUEUE_NAME:
self.bot_profile.bot_type = bot_type self.bot_profile.bot_type = bot_type
@@ -490,7 +493,7 @@ class TestServiceBotEventTriggers(ZulipTestCase):
self.send_personal_message(sender, recipient) self.send_personal_message(sender, recipient)
self.assertFalse(mock_queue_json_publish.called) 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: 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(): for bot_type, expected_queue_name in BOT_TYPE_TO_QUEUE_NAME.items():
self.bot_profile.bot_type = bot_type self.bot_profile.bot_type = bot_type
@@ -505,7 +508,8 @@ class TestServiceBotEventTriggers(ZulipTestCase):
def check_values_passed( def check_values_passed(
queue_name: Any, queue_name: Any,
trigger_event: Union[Mapping[Any, Any], Any], trigger_event: Dict[str, Any],
processor: Optional[Callable[[Any], None]] = None,
) -> None: ) -> None:
self.assertEqual(queue_name, expected_queue_name) self.assertEqual(queue_name, expected_queue_name)
self.assertIn(trigger_event["user_profile_id"], profile_ids) 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) self.assertEqual(mock_queue_json_publish.call_count, 2)
mock_queue_json_publish.reset_mock() 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: 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: for bot_type in BOT_TYPE_TO_QUEUE_NAME:
self.bot_profile.bot_type = bot_type self.bot_profile.bot_type = bot_type