mirror of
				https://github.com/zulip/zulip.git
				synced 2025-11-03 21:43:21 +00:00 
			
		
		
		
	tests: Use read_test_image_file.
Fixes a ResourceWarning from the unclosed file at test_upload.py:1954. Signed-off-by: Anders Kaseorg <anders@zulip.com>
This commit is contained in:
		
				
					committed by
					
						
						Tim Abbott
					
				
			
			
				
	
			
			
			
						parent
						
							031f4596ab
						
					
				
				
					commit
					4147da24dd
				
			@@ -65,8 +65,8 @@ from zerver.lib.storage import static_path
 | 
			
		||||
from zerver.lib.test_classes import ZulipTestCase
 | 
			
		||||
from zerver.lib.test_helpers import (
 | 
			
		||||
    create_s3_buckets,
 | 
			
		||||
    get_test_image_file,
 | 
			
		||||
    load_subdomain_token,
 | 
			
		||||
    read_test_image_file,
 | 
			
		||||
    use_s3_backend,
 | 
			
		||||
)
 | 
			
		||||
from zerver.lib.types import Validator
 | 
			
		||||
@@ -6228,8 +6228,7 @@ class TestZulipLDAPUserPopulator(ZulipLDAPTestCase):
 | 
			
		||||
    @use_s3_backend
 | 
			
		||||
    def test_update_user_avatar_for_s3(self) -> None:
 | 
			
		||||
        bucket = create_s3_buckets(settings.S3_AVATAR_BUCKET)[0]
 | 
			
		||||
        with get_test_image_file("img.png") as f:
 | 
			
		||||
            test_image_data = f.read()
 | 
			
		||||
        test_image_data = read_test_image_file("img.png")
 | 
			
		||||
        self.change_ldap_user_attr("hamlet", "jpegPhoto", test_image_data)
 | 
			
		||||
        with self.settings(AUTH_LDAP_USER_ATTR_MAP={"full_name": "cn", "avatar": "jpegPhoto"}):
 | 
			
		||||
            self.perform_ldap_sync(self.example_user("hamlet"))
 | 
			
		||||
 
 | 
			
		||||
@@ -43,7 +43,7 @@ from zerver.data_import.slack import (
 | 
			
		||||
)
 | 
			
		||||
from zerver.lib.import_realm import do_import_realm
 | 
			
		||||
from zerver.lib.test_classes import ZulipTestCase
 | 
			
		||||
from zerver.lib.test_helpers import get_test_image_file
 | 
			
		||||
from zerver.lib.test_helpers import read_test_image_file
 | 
			
		||||
from zerver.lib.topic import EXPORT_TOPIC_NAME
 | 
			
		||||
from zerver.models import Realm, RealmAuditLog, Recipient, UserProfile, get_realm
 | 
			
		||||
 | 
			
		||||
@@ -1103,8 +1103,7 @@ class SlackImporter(ZulipTestCase):
 | 
			
		||||
            {},
 | 
			
		||||
            team_info_fixture["team"],
 | 
			
		||||
        ]
 | 
			
		||||
        with get_test_image_file("img.png") as f:
 | 
			
		||||
            mock_requests_get.return_value.raw = BytesIO(f.read())
 | 
			
		||||
        mock_requests_get.return_value.raw = BytesIO(read_test_image_file("img.png"))
 | 
			
		||||
 | 
			
		||||
        with self.assertLogs(level="INFO"):
 | 
			
		||||
            do_convert_data(test_slack_zip_file, output_dir, token)
 | 
			
		||||
 
 | 
			
		||||
@@ -7,7 +7,12 @@ from moto import mock_s3
 | 
			
		||||
from zerver.lib.actions import check_add_realm_emoji
 | 
			
		||||
from zerver.lib.avatar_hash import user_avatar_path
 | 
			
		||||
from zerver.lib.test_classes import ZulipTestCase
 | 
			
		||||
from zerver.lib.test_helpers import avatar_disk_path, create_s3_buckets, get_test_image_file
 | 
			
		||||
from zerver.lib.test_helpers import (
 | 
			
		||||
    avatar_disk_path,
 | 
			
		||||
    create_s3_buckets,
 | 
			
		||||
    get_test_image_file,
 | 
			
		||||
    read_test_image_file,
 | 
			
		||||
)
 | 
			
		||||
from zerver.lib.transfer import (
 | 
			
		||||
    transfer_avatars_to_s3,
 | 
			
		||||
    transfer_emoji_to_s3,
 | 
			
		||||
@@ -98,8 +103,7 @@ class TransferUploadsToS3Test(ZulipTestCase):
 | 
			
		||||
        original_key = bucket.Object(emoji_path + ".original")
 | 
			
		||||
        resized_key = bucket.Object(emoji_path)
 | 
			
		||||
 | 
			
		||||
        with get_test_image_file("img.png") as image_file:
 | 
			
		||||
            image_data = image_file.read()
 | 
			
		||||
        image_data = read_test_image_file("img.png")
 | 
			
		||||
        resized_image_data, is_animated, still_image_data = resize_emoji(image_data)
 | 
			
		||||
 | 
			
		||||
        self.assertEqual(is_animated, False)
 | 
			
		||||
@@ -133,8 +137,7 @@ class TransferUploadsToS3Test(ZulipTestCase):
 | 
			
		||||
            )
 | 
			
		||||
        )
 | 
			
		||||
 | 
			
		||||
        with get_test_image_file("animated_img.gif") as image_file:
 | 
			
		||||
            image_data = image_file.read()
 | 
			
		||||
        image_data = read_test_image_file("animated_img.gif")
 | 
			
		||||
        resized_image_data, is_animated, still_image_data = resize_emoji(image_data)
 | 
			
		||||
 | 
			
		||||
        self.assertEqual(is_animated, True)
 | 
			
		||||
 
 | 
			
		||||
@@ -40,6 +40,7 @@ from zerver.lib.test_helpers import (
 | 
			
		||||
    create_s3_buckets,
 | 
			
		||||
    get_test_image_file,
 | 
			
		||||
    queries_captured,
 | 
			
		||||
    read_test_image_file,
 | 
			
		||||
    use_s3_backend,
 | 
			
		||||
)
 | 
			
		||||
from zerver.lib.upload import (
 | 
			
		||||
@@ -1302,8 +1303,7 @@ class EmojiTest(UploadSerializeMixin, ZulipTestCase):
 | 
			
		||||
    # 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 image
 | 
			
		||||
        with get_test_image_file("animated_unequal_img.gif") as f:
 | 
			
		||||
            animated_unequal_img_data = f.read()
 | 
			
		||||
        animated_unequal_img_data = read_test_image_file("animated_unequal_img.gif")
 | 
			
		||||
        resized_img_data, is_animated, still_img_data = resize_emoji(
 | 
			
		||||
            animated_unequal_img_data, size=50
 | 
			
		||||
        )
 | 
			
		||||
@@ -1315,14 +1315,12 @@ class EmojiTest(UploadSerializeMixin, ZulipTestCase):
 | 
			
		||||
        self.assertEqual((50, 50), still_image.size)
 | 
			
		||||
 | 
			
		||||
        # Test corrupt image exception
 | 
			
		||||
        with get_test_image_file("corrupt.gif") as f:
 | 
			
		||||
            corrupted_img_data = f.read()
 | 
			
		||||
        corrupted_img_data = read_test_image_file("corrupt.gif")
 | 
			
		||||
        with self.assertRaises(BadImageError):
 | 
			
		||||
            resize_emoji(corrupted_img_data)
 | 
			
		||||
 | 
			
		||||
        # Test an image larger than max is resized
 | 
			
		||||
        with get_test_image_file("animated_large_img.gif") as f:
 | 
			
		||||
            animated_large_img_data = f.read()
 | 
			
		||||
        animated_large_img_data = read_test_image_file("animated_large_img.gif")
 | 
			
		||||
        with patch("zerver.lib.upload.MAX_EMOJI_GIF_SIZE", 128):
 | 
			
		||||
            resized_img_data, is_animated, still_img_data = resize_emoji(
 | 
			
		||||
                animated_large_img_data, size=50
 | 
			
		||||
@@ -1335,8 +1333,7 @@ class EmojiTest(UploadSerializeMixin, ZulipTestCase):
 | 
			
		||||
            self.assertEqual((50, 50), still_image.size)
 | 
			
		||||
 | 
			
		||||
        # Test an image file larger than max is resized
 | 
			
		||||
        with get_test_image_file("animated_large_img.gif") as f:
 | 
			
		||||
            animated_large_img_data = f.read()
 | 
			
		||||
        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
 | 
			
		||||
@@ -1349,8 +1346,7 @@ class EmojiTest(UploadSerializeMixin, ZulipTestCase):
 | 
			
		||||
            self.assertEqual((50, 50), still_image.size)
 | 
			
		||||
 | 
			
		||||
        # Test an image smaller than max and smaller than file size max is not resized
 | 
			
		||||
        with get_test_image_file("animated_large_img.gif") as f:
 | 
			
		||||
            animated_large_img_data = f.read()
 | 
			
		||||
        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
 | 
			
		||||
@@ -1723,8 +1719,7 @@ class LocalStorageTest(UploadSerializeMixin, ZulipTestCase):
 | 
			
		||||
        user_profile = self.example_user("hamlet")
 | 
			
		||||
        file_path = user_avatar_path(user_profile)
 | 
			
		||||
 | 
			
		||||
        with get_test_image_file("img.png") as image_file:
 | 
			
		||||
            write_local_file("avatars", file_path + ".original", image_file.read())
 | 
			
		||||
        write_local_file("avatars", file_path + ".original", read_test_image_file("img.png"))
 | 
			
		||||
 | 
			
		||||
        image_path = os.path.join(settings.LOCAL_UPLOADS_DIR, "avatars", file_path + ".original")
 | 
			
		||||
        with open(image_path, "rb") as f:
 | 
			
		||||
@@ -1756,10 +1751,8 @@ class LocalStorageTest(UploadSerializeMixin, ZulipTestCase):
 | 
			
		||||
 | 
			
		||||
        assert settings.LOCAL_UPLOADS_DIR is not None
 | 
			
		||||
        file_path = os.path.join(settings.LOCAL_UPLOADS_DIR, "avatars", emoji_path)
 | 
			
		||||
        with get_test_image_file("img.png") as image_file, open(
 | 
			
		||||
            file_path + ".original", "rb"
 | 
			
		||||
        ) as original_file:
 | 
			
		||||
            self.assertEqual(image_file.read(), original_file.read())
 | 
			
		||||
        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:
 | 
			
		||||
@@ -1951,8 +1944,7 @@ class S3Test(ZulipTestCase):
 | 
			
		||||
            zerver.lib.upload.upload_backend.upload_avatar_image(
 | 
			
		||||
                image_file, user_profile, user_profile
 | 
			
		||||
            )
 | 
			
		||||
        with open(get_test_image_file("img.png").name, "rb") as f:
 | 
			
		||||
            test_image_data = f.read()
 | 
			
		||||
        test_image_data = read_test_image_file("img.png")
 | 
			
		||||
        test_medium_image_data = resize_avatar(test_image_data, MEDIUM_AVATAR_SIZE)
 | 
			
		||||
 | 
			
		||||
        original_image_key = bucket.Object(original_image_path_id)
 | 
			
		||||
@@ -2057,8 +2049,7 @@ class S3Test(ZulipTestCase):
 | 
			
		||||
 | 
			
		||||
        original_path_id = os.path.join(str(user_profile.realm.id), "realm", "icon.original")
 | 
			
		||||
        original_key = bucket.Object(original_path_id)
 | 
			
		||||
        with get_test_image_file("img.png") as image_file:
 | 
			
		||||
            self.assertEqual(image_file.read(), original_key.get()["Body"].read())
 | 
			
		||||
        self.assertEqual(read_test_image_file("img.png"), original_key.get()["Body"].read())
 | 
			
		||||
 | 
			
		||||
        resized_path_id = os.path.join(str(user_profile.realm.id), "realm", "icon.png")
 | 
			
		||||
        resized_data = bucket.Object(resized_path_id).get()["Body"].read()
 | 
			
		||||
@@ -2080,8 +2071,7 @@ class S3Test(ZulipTestCase):
 | 
			
		||||
            str(user_profile.realm.id), "realm", f"{file_name}.original"
 | 
			
		||||
        )
 | 
			
		||||
        original_key = bucket.Object(original_path_id)
 | 
			
		||||
        with get_test_image_file("img.png") as image_file:
 | 
			
		||||
            self.assertEqual(image_file.read(), original_key.get()["Body"].read())
 | 
			
		||||
        self.assertEqual(read_test_image_file("img.png"), original_key.get()["Body"].read())
 | 
			
		||||
 | 
			
		||||
        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()
 | 
			
		||||
@@ -2108,8 +2098,7 @@ class S3Test(ZulipTestCase):
 | 
			
		||||
            emoji_file_name=emoji_name,
 | 
			
		||||
        )
 | 
			
		||||
        original_key = bucket.Object(emoji_path + ".original")
 | 
			
		||||
        with get_test_image_file("img.png") as image_file:
 | 
			
		||||
            self.assertEqual(image_file.read(), original_key.get()["Body"].read())
 | 
			
		||||
        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))
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user