tests: Extract upload_image helpers from test_markdown_thumbnail.

These are pretty general and can be useful utils for other tests.
This commit is contained in:
Mateusz Mandera 2024-10-24 00:35:02 +02:00 committed by Tim Abbott
parent b593f6a881
commit a6b0385229
2 changed files with 20 additions and 14 deletions

View File

@ -72,6 +72,7 @@ from zerver.lib.test_console_output import (
from zerver.lib.test_helpers import ( from zerver.lib.test_helpers import (
cache_tries_captured, cache_tries_captured,
find_key_by_email, find_key_by_email,
get_test_image_file,
instrument_url, instrument_url,
queries_captured, queries_captured,
) )
@ -2199,6 +2200,20 @@ class ZulipTestCase(ZulipTestCaseMixin, TestCase):
) )
return message_id return message_id
def upload_image(self, image_name: str) -> str:
with get_test_image_file(image_name) as image_file:
response = self.assert_json_success(
self.client_post("/json/user_uploads", {"file": image_file})
)
return re.sub(r"/user_uploads/", "", response["url"])
def upload_and_thumbnail_image(self, image_name: str) -> str:
with self.captureOnCommitCallbacks(execute=True):
# Running captureOnCommitCallbacks includes inserting into
# the Rabbitmq queue, which in testing means we
# immediately run the worker for it, producing the thumbnails.
return self.upload_image(image_name)
def get_row_ids_in_all_tables() -> Iterator[tuple[str, set[int]]]: def get_row_ids_in_all_tables() -> Iterator[tuple[str, set[int]]]:
all_models = apps.get_models(include_auto_created=True) all_models = apps.get_models(include_auto_created=True)

View File

@ -2,6 +2,7 @@ import re
from unittest.mock import patch from unittest.mock import patch
import pyvips import pyvips
from typing_extensions import override
from zerver.actions.message_delete import do_delete_messages from zerver.actions.message_delete import do_delete_messages
from zerver.actions.message_send import check_message, do_send_messages from zerver.actions.message_send import check_message, do_send_messages
@ -9,7 +10,7 @@ from zerver.lib.addressee import Addressee
from zerver.lib.camo import get_camo_url from zerver.lib.camo import get_camo_url
from zerver.lib.markdown import render_message_markdown from zerver.lib.markdown import render_message_markdown
from zerver.lib.test_classes import ZulipTestCase from zerver.lib.test_classes import ZulipTestCase
from zerver.lib.test_helpers import get_test_image_file, read_test_image_file from zerver.lib.test_helpers import read_test_image_file
from zerver.lib.thumbnail import ThumbnailFormat from zerver.lib.thumbnail import ThumbnailFormat
from zerver.lib.upload import upload_message_attachment from zerver.lib.upload import upload_message_attachment
from zerver.models import ( from zerver.models import (
@ -25,20 +26,10 @@ from zerver.worker.thumbnail import ensure_thumbnails
class MarkdownThumbnailTest(ZulipTestCase): class MarkdownThumbnailTest(ZulipTestCase):
def upload_image(self, image_name: str) -> str: @override
def setUp(self) -> None:
self.login("othello") self.login("othello")
with get_test_image_file(image_name) as image_file: super().setUp()
response = self.assert_json_success(
self.client_post("/json/user_uploads", {"file": image_file})
)
return re.sub(r"/user_uploads/", "", response["url"])
def upload_and_thumbnail_image(self, image_name: str) -> str:
with self.captureOnCommitCallbacks(execute=True):
# Running captureOnCommitCallbacks includes inserting into
# the Rabbitmq queue, which in testing means we
# immediately run the worker for it, producing the thumbnails.
return self.upload_image(image_name)
def assert_message_content_is( def assert_message_content_is(
self, message_id: int, rendered_content: str, user_name: str = "othello" self, message_id: int, rendered_content: str, user_name: str = "othello"