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

@@ -9,7 +9,7 @@ from requests.exceptions import ConnectionError
from zerver.lib.actions import queue_json_publish
from zerver.lib.cache import NotFoundInCache, cache_set, preview_url_cache_key
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.parsers import GenericParser, OpenGraphParser
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/'
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'),