Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
tests: Switch from PIL to pyvips.
Alex Vandiver0070b5da78a7
diff --git a/zerver/tests/test_upload.py b/zerver/tests/test_upload.pyindex 8a0667f5702..24464edf11a 100644--- a/zerver/tests/test_upload.py+++ b/zerver/tests/test_upload.py@@ -1,4 +1,3 @@-import ioimport osimport reimport time@@ -8,9 +7,9 @@ from unittest.mock import patchfrom urllib.parse import quoteimport orjson+import pyvipsfrom django.conf import settingsfrom django.utils.timezone import now as timezone_now-from PIL import Imagefrom typing_extensions import overridefrom urllib3 import encode_multipart_formdatafrom urllib3.fields import RequestField@@ -1258,7 +1257,9 @@ class AvatarTest(UploadSerializeMixin, ZulipTestCase):if rfname is not None:response = self.client_get(url)data = response.getvalue()- self.assertEqual(Image.open(io.BytesIO(data)).size, (100, 100))+ avatar_image = pyvips.Image.new_from_buffer(data, "")+ self.assertEqual(avatar_image.height, 100)+ self.assertEqual(avatar_image.width, 100)# Verify that the medium-size avatar was createduser_profile = self.example_user("hamlet")@@ -1385,21 +1386,27 @@ class AvatarTest(UploadSerializeMixin, ZulipTestCase):class EmojiTest(UploadSerializeMixin, ZulipTestCase):- # While testing GIF resizing, we can't test if the final GIF has the same- # number of frames as the original one because PIL drops duplicate frames- # with a corresponding increase in the duration of the previous frame.def test_resize_emoji(self) -> None:# Test unequal width and height of animated GIF imageanimated_unequal_img_data = read_test_image_file("animated_unequal_img.gif")+ original_image = pyvips.Image.new_from_buffer(animated_unequal_img_data, "n=-1")resized_img_data, is_animated, still_img_data = resize_emoji(animated_unequal_img_data, "animated_unequal_img.gif", 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)+ emoji_image = pyvips.Image.new_from_buffer(resized_img_data, "n=-1")+ self.assertEqual(emoji_image.get("vips-loader"), "gifload_buffer")+ self.assertEqual(emoji_image.get_n_pages(), original_image.get_n_pages())+ self.assertEqual(emoji_image.get("page-height"), 50)+ self.assertEqual(emoji_image.height, 150)+ self.assertEqual(emoji_image.width, 50)++ still_image = pyvips.Image.new_from_buffer(still_img_data, "")+ self.assertEqual(still_image.get("vips-loader"), "pngload_buffer")+ self.assertEqual(still_image.get_n_pages(), 1)+ self.assertEqual(still_image.height, 50)+ self.assertEqual(still_image.width, 50)# Test corrupt image exceptioncorrupted_img_data = read_test_image_file("corrupt.gif")@@ -1407,15 +1414,23 @@ class EmojiTest(UploadSerializeMixin, ZulipTestCase):resize_emoji(corrupted_img_data, "corrupt.gif")animated_large_img_data = read_test_image_file("animated_large_img.gif")+ original_image = pyvips.Image.new_from_buffer(animated_large_img_data, "n=-1")resized_img_data, is_animated, still_img_data = resize_emoji(animated_large_img_data, "animated_large_img.gif", size=50)- im = Image.open(io.BytesIO(resized_img_data))- self.assertEqual((50, 50), im.size)+ emoji_image = pyvips.Image.new_from_buffer(resized_img_data, "n=-1")+ self.assertEqual(emoji_image.get("vips-loader"), "gifload_buffer")+ self.assertEqual(emoji_image.get_n_pages(), original_image.get_n_pages())+ self.assertEqual(emoji_image.get("page-height"), 50)+ self.assertEqual(emoji_image.height, 150)+ self.assertEqual(emoji_image.width, 50)self.assertTrue(is_animated)assert still_img_data- still_image = Image.open(io.BytesIO(still_img_data))- self.assertEqual((50, 50), still_image.size)+ still_image = pyvips.Image.new_from_buffer(still_img_data, "")+ self.assertEqual(still_image.get("vips-loader"), "pngload_buffer")+ self.assertEqual(still_image.get_n_pages(), 1)+ self.assertEqual(still_image.height, 50)+ self.assertEqual(still_image.width, 50)# Test an image file with too many bytes is not resizedwith patch("zerver.lib.thumbnail.MAX_EMOJI_GIF_FILE_SIZE_BYTES", 1024):@@ -1432,8 +1447,11 @@ class EmojiTest(UploadSerializeMixin, ZulipTestCase):resized_img_data, is_animated, no_still_data = resize_emoji(still_large_img_data, "still_large_img.gif", size=50)- im = Image.open(io.BytesIO(resized_img_data))- self.assertEqual((50, 50), im.size)+ emoji_image = pyvips.Image.new_from_buffer(resized_img_data, "n=-1")+ self.assertEqual(emoji_image.get("vips-loader"), "gifload_buffer")+ self.assertEqual(emoji_image.height, 50)+ self.assertEqual(emoji_image.width, 50)+ self.assertEqual(emoji_image.get_n_pages(), 1)self.assertFalse(is_animated)assert no_still_data is None@@ -1442,8 +1460,11 @@ class EmojiTest(UploadSerializeMixin, ZulipTestCase):resized_img_data, is_animated, no_still_data = resize_emoji(still_large_img_data, "img.jpg", size=50)- im = Image.open(io.BytesIO(resized_img_data))- self.assertEqual((50, 50), im.size)+ emoji_image = pyvips.Image.new_from_buffer(resized_img_data, "")+ self.assertEqual(emoji_image.get("vips-loader"), "jpegload_buffer")+ self.assertEqual(emoji_image.height, 50)+ self.assertEqual(emoji_image.width, 50)+ self.assertEqual(emoji_image.get_n_pages(), 1)self.assertFalse(is_animated)assert no_still_data is None@@ -1535,7 +1556,9 @@ class RealmIconTest(UploadSerializeMixin, ZulipTestCase):if rfname is not None:response = self.client_get(url)data = response.getvalue()- self.assertEqual(Image.open(io.BytesIO(data)).size, (100, 100))+ response_image = pyvips.Image.new_from_buffer(data, "")+ self.assertEqual(response_image.height, 100)+ self.assertEqual(response_image.width, 100)def test_invalid_icons(self) -> None:"""@@ -1719,7 +1742,9 @@ class RealmLogoTest(UploadSerializeMixin, ZulipTestCase):data = response.getvalue()# size should be 100 x 100 because thumbnail keeps aspect ratio# while trying to fit in a 800 x 100 box without losing part of the image- self.assertEqual(Image.open(io.BytesIO(data)).size, (100, 100))+ response_image = pyvips.Image.new_from_buffer(data, "")+ self.assertEqual(response_image.height, 100)+ self.assertEqual(response_image.width, 100)def test_invalid_logo_upload(self) -> None:"""diff --git a/zerver/tests/test_upload_local.py b/zerver/tests/test_upload_local.pyindex 12f698bfe5d..b7ab06d60bf 100644--- a/zerver/tests/test_upload_local.py+++ b/zerver/tests/test_upload_local.py@@ -3,8 +3,8 @@ import refrom io import BytesIO, StringIOfrom urllib.parse import urlsplit+import pyvipsfrom django.conf import settings-from PIL import Imageimport zerver.lib.uploadfrom zerver.lib.avatar_hash import user_avatar_path@@ -229,9 +229,9 @@ class LocalStorageTest(UploadSerializeMixin, ZulipTestCase):with open(file_path + ".original", "rb") as original_file:self.assertEqual(read_test_image_file("img.png"), original_file.read())- expected_size = (DEFAULT_EMOJI_SIZE, DEFAULT_EMOJI_SIZE)- with Image.open(file_path) as resized_image:- self.assertEqual(expected_size, resized_image.size)+ resized_emoji = pyvips.Image.new_from_file(file_path)+ self.assertEqual(DEFAULT_EMOJI_SIZE, resized_emoji.width)+ self.assertEqual(DEFAULT_EMOJI_SIZE, resized_emoji.height)def test_tarball_upload_and_deletion(self) -> None:user_profile = self.example_user("iago")diff --git a/zerver/tests/test_upload_s3.py b/zerver/tests/test_upload_s3.pyindex ab6bacc2fd0..ba934daf56d 100644--- a/zerver/tests/test_upload_s3.py+++ b/zerver/tests/test_upload_s3.py@@ -1,4 +1,3 @@-import ioimport osimport refrom io import BytesIO, StringIO@@ -6,8 +5,8 @@ from unittest.mock import patchfrom urllib.parse import urlsplitimport botocore.exceptions+import pyvipsfrom django.conf import settings-from PIL import Imageimport zerver.lib.uploadfrom zerver.actions.user_settings import do_delete_avatar_image@@ -414,9 +413,9 @@ class S3Test(ZulipTestCase):resized_path_id = os.path.join(str(user_profile.realm.id), "realm", "icon.png")resized_data = bucket.Object(resized_path_id).get()["Body"].read()- # while trying to fit in a 800 x 100 box without losing part of the image- resized_image = Image.open(io.BytesIO(resized_data)).size- self.assertEqual(resized_image, (DEFAULT_AVATAR_SIZE, DEFAULT_AVATAR_SIZE))+ resized_image = pyvips.Image.new_from_buffer(resized_data, "")+ self.assertEqual(DEFAULT_AVATAR_SIZE, resized_image.height)+ self.assertEqual(DEFAULT_AVATAR_SIZE, resized_image.width)@use_s3_backenddef _test_upload_logo_image(self, night: bool, file_name: str) -> None:@@ -436,8 +435,9 @@ class S3Test(ZulipTestCase):resized_path_id = os.path.join(str(user_profile.realm.id), "realm", f"{file_name}.png")resized_data = bucket.Object(resized_path_id).get()["Body"].read()- resized_image = Image.open(io.BytesIO(resized_data)).size- self.assertEqual(resized_image, (DEFAULT_AVATAR_SIZE, DEFAULT_AVATAR_SIZE))+ resized_image = pyvips.Image.new_from_buffer(resized_data, "")+ self.assertEqual(DEFAULT_AVATAR_SIZE, resized_image.height)+ self.assertEqual(DEFAULT_AVATAR_SIZE, resized_image.width)def test_upload_realm_logo_image(self) -> None:self._test_upload_logo_image(night=False, file_name="logo")@@ -489,8 +489,9 @@ class S3Test(ZulipTestCase):self.assertEqual(read_test_image_file("img.png"), original_key.get()["Body"].read())resized_data = bucket.Object(emoji_path).get()["Body"].read()- resized_image = Image.open(io.BytesIO(resized_data))- self.assertEqual(resized_image.size, (DEFAULT_EMOJI_SIZE, DEFAULT_EMOJI_SIZE))+ resized_image = pyvips.Image.new_from_buffer(resized_data, "")+ self.assertEqual(DEFAULT_EMOJI_SIZE, resized_image.height)+ self.assertEqual(DEFAULT_EMOJI_SIZE, resized_image.width)@use_s3_backenddef test_tarball_upload_and_deletion(self) -> None: