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.

test_upload: Uncomment subTest contexts.

Anders Kaseorgb729f00fc289
diff --git a/zerver/tests/test_upload.py b/zerver/tests/test_upload.py
index 62ab5f2d51b..883a2d60438 100644
--- a/zerver/tests/test_upload.py
+++ b/zerver/tests/test_upload.py
@@ -1135,44 +1135,45 @@ class AvatarTest(UploadSerializeMixin, ZulipTestCase):
"""
version = 2
for fname, rfname in self.correct_files:
- # TODO: use self.subTest once we're exclusively on python 3 by uncommenting the line below.
- # with self.subTest(fname=fname):
- self.login("hamlet")
- with get_test_image_file(fname) as fp:
- result = self.client_post("/json/users/me/avatar", {"file": fp})
+ with self.subTest(fname=fname):
+ self.login("hamlet")
+ with get_test_image_file(fname) as fp:
+ result = self.client_post("/json/users/me/avatar", {"file": fp})
+
+ self.assert_json_success(result)
+ self.assertIn("avatar_url", result.json())
+ base = "/user_avatars/"
+ url = result.json()["avatar_url"]
+ self.assertEqual(base, url[: len(base)])
+
+ if rfname is not None:
+ response = self.client_get(url)
+ assert isinstance(response, StreamingHttpResponse)
+ data = b"".join(response.streaming_content)
+ self.assertEqual(Image.open(io.BytesIO(data)).size, (100, 100))
+
+ # Verify that the medium-size avatar was created
+ user_profile = self.example_user("hamlet")
+ medium_avatar_disk_path = avatar_disk_path(user_profile, medium=True)
+ self.assertTrue(os.path.exists(medium_avatar_disk_path))
+
+ # Verify that ensure_medium_avatar_url does not overwrite this file if it exists
+ with mock.patch("zerver.lib.upload.write_local_file") as mock_write_local_file:
+ zerver.lib.upload.upload_backend.ensure_avatar_image(
+ user_profile, is_medium=True
+ )
+ self.assertFalse(mock_write_local_file.called)
- self.assert_json_success(result)
- self.assertIn("avatar_url", result.json())
- base = "/user_avatars/"
- url = result.json()["avatar_url"]
- self.assertEqual(base, url[: len(base)])
-
- if rfname is not None:
- response = self.client_get(url)
- assert isinstance(response, StreamingHttpResponse)
- data = b"".join(response.streaming_content)
- self.assertEqual(Image.open(io.BytesIO(data)).size, (100, 100))
-
- # Verify that the medium-size avatar was created
- user_profile = self.example_user("hamlet")
- medium_avatar_disk_path = avatar_disk_path(user_profile, medium=True)
- self.assertTrue(os.path.exists(medium_avatar_disk_path))
-
- # Verify that ensure_medium_avatar_url does not overwrite this file if it exists
- with mock.patch("zerver.lib.upload.write_local_file") as mock_write_local_file:
+ # Confirm that ensure_medium_avatar_url works to recreate
+ # medium size avatars from the original if needed
+ os.remove(medium_avatar_disk_path)
+ self.assertFalse(os.path.exists(medium_avatar_disk_path))
zerver.lib.upload.upload_backend.ensure_avatar_image(user_profile, is_medium=True)
- self.assertFalse(mock_write_local_file.called)
+ self.assertTrue(os.path.exists(medium_avatar_disk_path))
- # Confirm that ensure_medium_avatar_url works to recreate
- # medium size avatars from the original if needed
- os.remove(medium_avatar_disk_path)
- self.assertFalse(os.path.exists(medium_avatar_disk_path))
- zerver.lib.upload.upload_backend.ensure_avatar_image(user_profile, is_medium=True)
- self.assertTrue(os.path.exists(medium_avatar_disk_path))
-
- # Verify whether the avatar_version gets incremented with every new upload
- self.assertEqual(user_profile.avatar_version, version)
- version += 1
+ # Verify whether the avatar_version gets incremented with every new upload
+ self.assertEqual(user_profile.avatar_version, version)
+ version += 1
def test_copy_avatar_image(self) -> None:
self.login("hamlet")
@@ -1232,14 +1233,16 @@ class AvatarTest(UploadSerializeMixin, ZulipTestCase):
A PUT request to /json/users/me/avatar with an invalid file should fail.
"""
for fname in self.corrupt_files:
- # with self.subTest(fname=fname):
- self.login("hamlet")
- with get_test_image_file(fname) as fp:
- result = self.client_post("/json/users/me/avatar", {"file": fp})
+ with self.subTest(fname=fname):
+ self.login("hamlet")
+ with get_test_image_file(fname) as fp:
+ result = self.client_post("/json/users/me/avatar", {"file": fp})
- self.assert_json_error(result, "Could not decode image; did you upload an image file?")
- user_profile = self.example_user("hamlet")
- self.assertEqual(user_profile.avatar_version, 1)
+ self.assert_json_error(
+ result, "Could not decode image; did you upload an image file?"
+ )
+ user_profile = self.example_user("hamlet")
+ self.assertEqual(user_profile.avatar_version, 1)
def test_delete_avatar(self) -> None:
"""
@@ -1409,35 +1412,36 @@ class RealmIconTest(UploadSerializeMixin, ZulipTestCase):
and actually create an realm icon.
"""
for fname, rfname in self.correct_files:
- # TODO: use self.subTest once we're exclusively on python 3 by uncommenting the line below.
- # with self.subTest(fname=fname):
- self.login("iago")
- with get_test_image_file(fname) as fp:
- result = self.client_post("/json/realm/icon", {"file": fp})
- realm = get_realm("zulip")
- self.assert_json_success(result)
- self.assertIn("icon_url", result.json())
- base = f"/user_avatars/{realm.id}/realm/icon.png"
- url = result.json()["icon_url"]
- self.assertEqual(base, url[: len(base)])
-
- if rfname is not None:
- response = self.client_get(url)
- assert isinstance(response, StreamingHttpResponse)
- data = b"".join(response.streaming_content)
- self.assertEqual(Image.open(io.BytesIO(data)).size, (100, 100))
+ with self.subTest(fname=fname):
+ self.login("iago")
+ with get_test_image_file(fname) as fp:
+ result = self.client_post("/json/realm/icon", {"file": fp})
+ realm = get_realm("zulip")
+ self.assert_json_success(result)
+ self.assertIn("icon_url", result.json())
+ base = f"/user_avatars/{realm.id}/realm/icon.png"
+ url = result.json()["icon_url"]
+ self.assertEqual(base, url[: len(base)])
+
+ if rfname is not None:
+ response = self.client_get(url)
+ assert isinstance(response, StreamingHttpResponse)
+ data = b"".join(response.streaming_content)
+ self.assertEqual(Image.open(io.BytesIO(data)).size, (100, 100))
def test_invalid_icons(self) -> None:
"""
A PUT request to /json/realm/icon with an invalid file should fail.
"""
for fname in self.corrupt_files:
- # with self.subTest(fname=fname):
- self.login("iago")
- with get_test_image_file(fname) as fp:
- result = self.client_post("/json/realm/icon", {"file": fp})
+ with self.subTest(fname=fname):
+ self.login("iago")
+ with get_test_image_file(fname) as fp:
+ result = self.client_post("/json/realm/icon", {"file": fp})
- self.assert_json_error(result, "Could not decode image; did you upload an image file?")
+ self.assert_json_error(
+ result, "Could not decode image; did you upload an image file?"
+ )
def test_delete_icon(self) -> None:
"""
@@ -1582,38 +1586,39 @@ class RealmLogoTest(UploadSerializeMixin, ZulipTestCase):
and actually create an realm logo.
"""
for fname, rfname in self.correct_files:
- # TODO: use self.subTest once we're exclusively on python 3 by uncommenting the line below.
- # with self.subTest(fname=fname):
- self.login("iago")
- with get_test_image_file(fname) as fp:
- result = self.client_post(
- "/json/realm/logo", {"file": fp, "night": orjson.dumps(self.night).decode()}
- )
- realm = get_realm("zulip")
- self.assert_json_success(result)
- logo_url = get_realm_logo_url(realm, self.night)
-
- if rfname is not None:
- response = self.client_get(logo_url)
- assert isinstance(response, StreamingHttpResponse)
- data = b"".join(response.streaming_content)
- # 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))
+ with self.subTest(fname=fname):
+ self.login("iago")
+ with get_test_image_file(fname) as fp:
+ result = self.client_post(
+ "/json/realm/logo", {"file": fp, "night": orjson.dumps(self.night).decode()}
+ )
+ realm = get_realm("zulip")
+ self.assert_json_success(result)
+ logo_url = get_realm_logo_url(realm, self.night)
+
+ if rfname is not None:
+ response = self.client_get(logo_url)
+ assert isinstance(response, StreamingHttpResponse)
+ data = b"".join(response.streaming_content)
+ # 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))
def test_invalid_logo_upload(self) -> None:
"""
A PUT request to /json/realm/logo with an invalid file should fail.
"""
for fname in self.corrupt_files:
- # with self.subTest(fname=fname):
- self.login("iago")
- with get_test_image_file(fname) as fp:
- result = self.client_post(
- "/json/realm/logo", {"file": fp, "night": orjson.dumps(self.night).decode()}
- )
+ with self.subTest(fname=fname):
+ self.login("iago")
+ with get_test_image_file(fname) as fp:
+ result = self.client_post(
+ "/json/realm/logo", {"file": fp, "night": orjson.dumps(self.night).decode()}
+ )
- self.assert_json_error(result, "Could not decode image; did you upload an image file?")
+ self.assert_json_error(
+ result, "Could not decode image; did you upload an image file?"
+ )
def test_delete_logo(self) -> None:
"""