2021-08-12 10:19:53 +02:00
|
|
|
import os
|
2020-06-11 00:54:34 +02:00
|
|
|
from unittest.mock import Mock, patch
|
2019-02-14 13:42:04 +01:00
|
|
|
|
2020-06-11 00:54:34 +02:00
|
|
|
from django.conf import settings
|
2018-12-07 17:52:01 +01:00
|
|
|
from moto import mock_s3
|
2019-02-14 13:42:04 +01:00
|
|
|
|
|
|
|
from zerver.lib.actions import check_add_realm_emoji
|
|
|
|
from zerver.lib.avatar_hash import user_avatar_path
|
2020-06-11 00:54:34 +02:00
|
|
|
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.transfer import (
|
|
|
|
transfer_avatars_to_s3,
|
|
|
|
transfer_emoji_to_s3,
|
|
|
|
transfer_message_files_to_s3,
|
|
|
|
transfer_uploads_to_s3,
|
|
|
|
)
|
|
|
|
from zerver.lib.upload import resize_emoji, upload_message_file
|
|
|
|
from zerver.models import Attachment, RealmEmoji
|
|
|
|
|
2019-02-14 13:42:04 +01:00
|
|
|
|
|
|
|
class TransferUploadsToS3Test(ZulipTestCase):
|
|
|
|
@patch("zerver.lib.transfer.transfer_avatars_to_s3")
|
|
|
|
@patch("zerver.lib.transfer.transfer_message_files_to_s3")
|
|
|
|
@patch("zerver.lib.transfer.transfer_emoji_to_s3")
|
|
|
|
def test_transfer_uploads_to_s3(self, m3: Mock, m2: Mock, m1: Mock) -> None:
|
|
|
|
transfer_uploads_to_s3(4)
|
|
|
|
|
|
|
|
m1.assert_called_with(4)
|
|
|
|
m2.assert_called_with(4)
|
|
|
|
m3.assert_called_with(4)
|
|
|
|
|
2018-12-07 17:52:01 +01:00
|
|
|
@mock_s3
|
2019-02-14 13:42:04 +01:00
|
|
|
def test_transfer_avatars_to_s3(self) -> None:
|
|
|
|
bucket = create_s3_buckets(settings.S3_AVATAR_BUCKET)[0]
|
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
self.login("hamlet")
|
|
|
|
with get_test_image_file("img.png") as image_file:
|
|
|
|
self.client_post("/json/users/me/avatar", {"file": image_file})
|
2019-02-14 13:42:04 +01:00
|
|
|
|
|
|
|
user = self.example_user("hamlet")
|
|
|
|
|
2020-07-24 18:10:01 +02:00
|
|
|
with self.assertLogs(level="INFO"):
|
|
|
|
transfer_avatars_to_s3(1)
|
2019-02-14 13:42:04 +01:00
|
|
|
|
|
|
|
path_id = user_avatar_path(user)
|
2018-12-07 17:52:01 +01:00
|
|
|
image_key = bucket.Object(path_id)
|
|
|
|
original_image_key = bucket.Object(path_id + ".original")
|
|
|
|
medium_image_key = bucket.Object(path_id + "-medium.png")
|
2019-02-14 13:42:04 +01:00
|
|
|
|
2021-05-17 05:41:32 +02:00
|
|
|
self.assert_length(list(bucket.objects.all()), 3)
|
2020-10-24 09:33:54 +02:00
|
|
|
with open(avatar_disk_path(user), "rb") as f:
|
2021-02-12 08:20:45 +01:00
|
|
|
self.assertEqual(image_key.get()["Body"].read(), f.read())
|
2020-10-24 09:33:54 +02:00
|
|
|
with open(avatar_disk_path(user, original=True), "rb") as f:
|
2021-02-12 08:20:45 +01:00
|
|
|
self.assertEqual(original_image_key.get()["Body"].read(), f.read())
|
2020-10-24 09:33:54 +02:00
|
|
|
with open(avatar_disk_path(user, medium=True), "rb") as f:
|
2021-02-12 08:20:45 +01:00
|
|
|
self.assertEqual(medium_image_key.get()["Body"].read(), f.read())
|
2019-02-14 13:42:04 +01:00
|
|
|
|
2018-12-07 17:52:01 +01:00
|
|
|
@mock_s3
|
2019-02-14 13:42:04 +01:00
|
|
|
def test_transfer_message_files(self) -> None:
|
|
|
|
bucket = create_s3_buckets(settings.S3_AUTH_UPLOADS_BUCKET)[0]
|
2021-02-12 08:20:45 +01:00
|
|
|
hamlet = self.example_user("hamlet")
|
|
|
|
othello = self.example_user("othello")
|
2019-02-14 13:42:04 +01:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
upload_message_file("dummy1.txt", len(b"zulip1!"), "text/plain", b"zulip1!", hamlet)
|
|
|
|
upload_message_file("dummy2.txt", len(b"zulip2!"), "text/plain", b"zulip2!", othello)
|
2019-02-14 13:42:04 +01:00
|
|
|
|
2020-07-24 18:10:01 +02:00
|
|
|
with self.assertLogs(level="INFO"):
|
|
|
|
transfer_message_files_to_s3(1)
|
2019-02-14 13:42:04 +01:00
|
|
|
|
2020-08-18 03:27:10 +02:00
|
|
|
attachments = Attachment.objects.all().order_by("id")
|
2019-02-14 13:42:04 +01:00
|
|
|
|
2021-05-17 05:41:32 +02:00
|
|
|
self.assert_length(list(bucket.objects.all()), 2)
|
2021-02-12 08:20:45 +01:00
|
|
|
self.assertEqual(bucket.Object(attachments[0].path_id).get()["Body"].read(), b"zulip1!")
|
|
|
|
self.assertEqual(bucket.Object(attachments[1].path_id).get()["Body"].read(), b"zulip2!")
|
2019-02-14 13:42:04 +01:00
|
|
|
|
2018-12-07 17:52:01 +01:00
|
|
|
@mock_s3
|
2019-02-14 13:42:04 +01:00
|
|
|
def test_transfer_emoji_to_s3(self) -> None:
|
|
|
|
bucket = create_s3_buckets(settings.S3_AVATAR_BUCKET)[0]
|
2021-02-12 08:20:45 +01:00
|
|
|
othello = self.example_user("othello")
|
2019-02-14 13:42:04 +01:00
|
|
|
RealmEmoji.objects.all().delete()
|
|
|
|
|
|
|
|
emoji_name = "emoji.png"
|
|
|
|
|
2020-10-24 09:33:54 +02:00
|
|
|
with get_test_image_file("img.png") as image_file:
|
|
|
|
emoji = check_add_realm_emoji(othello.realm, emoji_name, othello, image_file)
|
2019-02-14 13:42:04 +01:00
|
|
|
if not emoji:
|
|
|
|
raise AssertionError("Unable to add emoji.")
|
|
|
|
|
|
|
|
emoji_path = RealmEmoji.PATH_ID_TEMPLATE.format(
|
|
|
|
realm_id=othello.realm_id,
|
|
|
|
emoji_file_name=emoji.file_name,
|
|
|
|
)
|
|
|
|
|
2020-07-24 18:10:01 +02:00
|
|
|
with self.assertLogs(level="INFO"):
|
|
|
|
transfer_emoji_to_s3(1)
|
2019-02-14 13:42:04 +01:00
|
|
|
|
2021-05-17 05:41:32 +02:00
|
|
|
self.assert_length(list(bucket.objects.all()), 2)
|
2018-12-07 17:52:01 +01:00
|
|
|
original_key = bucket.Object(emoji_path + ".original")
|
|
|
|
resized_key = bucket.Object(emoji_path)
|
2019-02-14 13:42:04 +01:00
|
|
|
|
2020-10-24 09:33:54 +02:00
|
|
|
with get_test_image_file("img.png") as image_file:
|
|
|
|
image_data = image_file.read()
|
2021-08-12 10:19:53 +02:00
|
|
|
resized_image_data, is_animated, still_image_data = resize_emoji(image_data)
|
2019-02-14 13:42:04 +01:00
|
|
|
|
2021-08-12 10:19:53 +02:00
|
|
|
self.assertEqual(is_animated, False)
|
|
|
|
self.assertEqual(still_image_data, None)
|
2021-02-12 08:20:45 +01:00
|
|
|
self.assertEqual(image_data, original_key.get()["Body"].read())
|
|
|
|
self.assertEqual(resized_image_data, resized_key.get()["Body"].read())
|
2021-08-12 10:19:53 +02:00
|
|
|
|
|
|
|
with get_test_image_file("animated_img.gif") as image_file:
|
|
|
|
emoji = check_add_realm_emoji(othello.realm, emoji_name, othello, image_file)
|
|
|
|
if not emoji:
|
|
|
|
raise AssertionError("Unable to add emoji.")
|
|
|
|
|
|
|
|
emoji_path = RealmEmoji.PATH_ID_TEMPLATE.format(
|
|
|
|
realm_id=othello.realm_id,
|
|
|
|
emoji_file_name=emoji.file_name,
|
|
|
|
)
|
|
|
|
|
|
|
|
with self.assertLogs(level="INFO"):
|
|
|
|
transfer_emoji_to_s3(1)
|
|
|
|
|
|
|
|
self.assert_length(list(bucket.objects.all()), 5)
|
|
|
|
original_key = bucket.Object(emoji_path + ".original")
|
|
|
|
resized_key = bucket.Object(emoji_path)
|
|
|
|
assert emoji.file_name
|
|
|
|
still_key = bucket.Object(
|
|
|
|
RealmEmoji.STILL_PATH_ID_TEMPLATE.format(
|
|
|
|
realm_id=othello.realm_id,
|
|
|
|
emoji_filename_without_extension=os.path.splitext(emoji.file_name)[0],
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
with get_test_image_file("animated_img.gif") as image_file:
|
|
|
|
image_data = image_file.read()
|
|
|
|
resized_image_data, is_animated, still_image_data = resize_emoji(image_data)
|
|
|
|
|
|
|
|
self.assertEqual(is_animated, True)
|
|
|
|
self.assertEqual(type(still_image_data), bytes)
|
|
|
|
self.assertEqual(image_data, original_key.get()["Body"].read())
|
|
|
|
self.assertEqual(resized_image_data, resized_key.get()["Body"].read())
|
|
|
|
self.assertEqual(still_image_data, still_key.get()["Body"].read())
|