Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

tests: Refactor tests of resizing animated images.

Alex Vandiverfc793c10fa6c
diff --git a/zerver/tests/test_upload.py b/zerver/tests/test_upload.py
index 47cfa077b49..0f8fe59d4aa 100644
--- a/zerver/tests/test_upload.py
+++ b/zerver/tests/test_upload.py
@@ -1303,46 +1303,30 @@ class EmojiTest(UploadSerializeMixin, ZulipTestCase):
with self.assertRaises(BadImageError):
resize_emoji(corrupted_img_data)
- # Test an image larger than max is resized
- animated_large_img_data = read_test_image_file("animated_large_img.gif")
- with patch("zerver.lib.upload.MAX_EMOJI_GIF_SIZE", 128):
+ animated_large_img_data = read_test_image_file(f"animated_large_img.gif")
+
+ def test_resize(size: int = 50) -> None:
resized_img_data, is_animated, still_img_data = resize_emoji(
animated_large_img_data, size=50
)
im = Image.open(io.BytesIO(resized_img_data))
- self.assertEqual((50, 50), im.size)
+ self.assertEqual((size, size), im.size)
self.assertTrue(is_animated)
assert still_img_data
still_image = Image.open(io.BytesIO(still_img_data))
self.assertEqual((50, 50), still_image.size)
+ # Test an image larger than max is resized
+ with patch("zerver.lib.upload.MAX_EMOJI_GIF_SIZE", 128):
+ test_resize()
+
# Test an image file larger than max is resized
- animated_large_img_data = read_test_image_file("animated_large_img.gif")
with patch("zerver.lib.upload.MAX_EMOJI_GIF_FILE_SIZE_BYTES", 3 * 1024 * 1024):
- resized_img_data, is_animated, still_img_data = resize_emoji(
- animated_large_img_data, size=50
- )
- im = Image.open(io.BytesIO(resized_img_data))
- self.assertEqual((50, 50), im.size)
- self.assertTrue(is_animated)
- assert still_img_data is not None
- still_image = Image.open(io.BytesIO(still_img_data))
- self.assertEqual((50, 50), still_image.size)
+ test_resize()
# Test an image smaller than max and smaller than file size max is not resized
- animated_large_img_data = read_test_image_file("animated_large_img.gif")
with patch("zerver.lib.upload.MAX_EMOJI_GIF_SIZE", 512):
- resized_img_data, is_animated, still_img_data = resize_emoji(
- animated_large_img_data, size=50
- )
- im = Image.open(io.BytesIO(resized_img_data))
- self.assertEqual((256, 256), im.size)
- self.assertTrue(is_animated)
-
- # We unconditionally resize the still_image
- assert still_img_data is not None
- still_image = Image.open(io.BytesIO(still_img_data))
- self.assertEqual((50, 50), still_image.size)
+ test_resize(size=256)
# Test a non-animated image which does need to be resized
still_large_img_data = read_test_image_file("still_large_img.gif")