def test_transfer_emoji_to_s3(self) -> None: bucket = create_s3_buckets(settings.S3_AVATAR_BUCKET)[0] othello = self.example_user('othello') RealmEmoji.objects.all().delete() emoji_name = "emoji.png" image_file = get_test_image_file("img.png") 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, ) transfer_emoji_to_s3(1) self.assertEqual(len(bucket.get_all_keys()), 2) original_key = bucket.get_key(emoji_path + ".original") resized_key = bucket.get_key(emoji_path) image_file.seek(0) image_data = image_file.read() resized_image_data = resize_emoji(image_data) self.assertEqual(image_data, original_key.get_contents_as_string()) self.assertEqual(resized_image_data, resized_key.get_contents_as_string())
def test_transfer_emoji_to_s3(self) -> None: bucket = create_s3_buckets(settings.S3_AVATAR_BUCKET)[0] othello = self.example_user('othello') RealmEmoji.objects.all().delete() emoji_name = "emoji.png" with get_test_image_file("img.png") 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.assertEqual(len(list(bucket.objects.all())), 2) 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() resized_image_data = resize_emoji(image_data) self.assertEqual(image_data, original_key.get()['Body'].read()) self.assertEqual(resized_image_data, resized_key.get()['Body'].read())
def test_resize_emoji(self) -> None: # Test unequal width and height of animated GIF image animated_unequal_img_data = open(get_test_image_file('animated_unequal_img.gif').name, 'rb').read() with self.assertRaises(JsonableError): resize_emoji(animated_unequal_img_data) # Test for large image (128x128) animated_large_img_data = open(get_test_image_file('animated_large_img.gif').name, 'rb').read() with self.assertRaises(JsonableError): resize_emoji(animated_large_img_data) # Test for no resize case animated_img_data = open(get_test_image_file('animated_img.gif').name, 'rb').read() self.assertEqual(animated_img_data, resize_emoji(animated_img_data)) # Test corrupt image exception corrupted_img_data = open(get_test_image_file('corrupt.gif').name, 'rb').read() with self.assertRaises(BadImageError): resize_emoji(corrupted_img_data)
def test_transfer_emoji_to_s3(self) -> None: bucket = create_s3_buckets(settings.S3_AVATAR_BUCKET)[0] othello = self.example_user("othello") RealmEmoji.objects.all().delete() emoji_name = "emoji.png" with get_test_image_file("img.png") 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()), 2) original_key = bucket.Object(emoji_path + ".original") resized_key = bucket.Object(emoji_path) 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) self.assertEqual(still_image_data, None) self.assertEqual(image_data, original_key.get()["Body"].read()) self.assertEqual(resized_image_data, resized_key.get()["Body"].read()) emoji_name = "emoji2.png" 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], ) ) 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) 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())