def test_transfer_avatars_to_s3(self) -> None: bucket = create_s3_buckets(settings.S3_AVATAR_BUCKET)[0] self.login('hamlet') with get_test_image_file('img.png') as image_file: self.client_post("/json/users/me/avatar", {'file': image_file}) user = self.example_user("hamlet") transfer_avatars_to_s3(1) path_id = user_avatar_path(user) image_key = bucket.get_key(path_id) original_image_key = bucket.get_key(path_id + ".original") medium_image_key = bucket.get_key(path_id + "-medium.png") self.assertEqual(len(bucket.get_all_keys()), 3) self.assertEqual(image_key.get_contents_as_string(), open(avatar_disk_path(user), "rb").read()) self.assertEqual( original_image_key.get_contents_as_string(), open(avatar_disk_path(user, original=True), "rb").read()) self.assertEqual( medium_image_key.get_contents_as_string(), open(avatar_disk_path(user, medium=True), "rb").read())
def test_transfer_avatars_to_s3(self) -> None: bucket = create_s3_buckets(settings.S3_AVATAR_BUCKET)[0] self.login('hamlet') with get_test_image_file('img.png') as image_file: self.client_post("/json/users/me/avatar", {'file': image_file}) user = self.example_user("hamlet") with self.assertLogs(level="INFO"): transfer_avatars_to_s3(1) path_id = user_avatar_path(user) image_key = bucket.Object(path_id) original_image_key = bucket.Object(path_id + ".original") medium_image_key = bucket.Object(path_id + "-medium.png") self.assertEqual(len(list(bucket.objects.all())), 3) self.assertEqual(image_key.get()['Body'].read(), open(avatar_disk_path(user), "rb").read()) self.assertEqual( original_image_key.get()['Body'].read(), open(avatar_disk_path(user, original=True), "rb").read()) self.assertEqual( medium_image_key.get()['Body'].read(), open(avatar_disk_path(user, medium=True), "rb").read())
def test_add_bot_with_user_avatar(self) -> None: email = '*****@*****.**' realm = get_realm('zulip') self.login(self.example_email('hamlet')) self.assert_num_bots_equal(0) with get_test_image_file('img.png') as fp: self.create_bot(file=fp) profile = get_user(email, realm) # Make sure that avatar image that we've uploaded is same with avatar image in the server self.assertTrue(filecmp.cmp(fp.name, os.path.splitext(avatar_disk_path(profile))[0] + ".original")) self.assert_num_bots_equal(1) self.assertEqual(profile.avatar_source, UserProfile.AVATAR_FROM_USER) self.assertTrue(os.path.exists(avatar_disk_path(profile)))
def test_patch_bot_avatar(self): # type: () -> None self.login(self.example_email('hamlet')) bot_info = { 'full_name': 'The Bot of Hamlet', 'short_name': 'hambot', } result = self.client_post("/json/bots", bot_info) self.assert_json_success(result) bot_email = '*****@*****.**' bot_realm = get_realm('zulip') profile = get_user(bot_email, bot_realm) self.assertEqual(profile.avatar_source, UserProfile.AVATAR_FROM_GRAVATAR) # Try error case first (too many files): with get_test_image_file('img.png') as fp1, \ get_test_image_file('img.gif') as fp2: result = self.client_patch_multipart( '/json/bots/[email protected]', dict(file1=fp1, file2=fp2)) self.assert_json_error(result, 'You may only upload one file at a time') profile = get_user(bot_email, bot_realm) self.assertEqual(profile.avatar_version, 1) # HAPPY PATH with get_test_image_file('img.png') as fp: result = self.client_patch_multipart( '/json/bots/[email protected]', dict(file=fp)) profile = get_user(bot_email, bot_realm) self.assertEqual(profile.avatar_version, 2) # Make sure that avatar image that we've uploaded is same with avatar image in the server self.assertTrue( filecmp.cmp( fp.name, os.path.splitext(avatar_disk_path(profile))[0] + ".original")) self.assert_json_success(result) self.assertEqual(profile.avatar_source, UserProfile.AVATAR_FROM_USER) self.assertTrue(os.path.exists(avatar_disk_path(profile)))
def test_transfer_avatars_to_s3(self) -> None: bucket = create_s3_buckets(settings.S3_AVATAR_BUCKET)[0] self.login(self.example_email("hamlet")) with get_test_image_file('img.png') as image_file: self.client_post("/json/users/me/avatar", {'file': image_file}) user = self.example_user("hamlet") transfer_avatars_to_s3(1) path_id = user_avatar_path(user) image_key = bucket.get_key(path_id) original_image_key = bucket.get_key(path_id + ".original") medium_image_key = bucket.get_key(path_id + "-medium.png") self.assertEqual(len(bucket.get_all_keys()), 3) self.assertEqual(image_key.get_contents_as_string(), open(avatar_disk_path(user), "rb").read()) self.assertEqual(original_image_key.get_contents_as_string(), open(avatar_disk_path(user, original=True), "rb").read()) self.assertEqual(medium_image_key.get_contents_as_string(), open(avatar_disk_path(user, medium=True), "rb").read())
def test_patch_bot_avatar(self) -> None: self.login(self.example_email('hamlet')) bot_info = { 'full_name': 'The Bot of Hamlet', 'short_name': 'hambot', } result = self.client_post("/json/bots", bot_info) self.assert_json_success(result) bot_email = '*****@*****.**' bot_realm = get_realm('zulip') profile = get_user(bot_email, bot_realm) self.assertEqual(profile.avatar_source, UserProfile.AVATAR_FROM_GRAVATAR) # Try error case first (too many files): with get_test_image_file('img.png') as fp1, \ get_test_image_file('img.gif') as fp2: result = self.client_patch_multipart( '/json/bots/[email protected]', dict(file1=fp1, file2=fp2)) self.assert_json_error(result, 'You may only upload one file at a time') profile = get_user(bot_email, bot_realm) self.assertEqual(profile.avatar_version, 1) # HAPPY PATH with get_test_image_file('img.png') as fp: result = self.client_patch_multipart( '/json/bots/[email protected]', dict(file=fp)) profile = get_user(bot_email, bot_realm) self.assertEqual(profile.avatar_version, 2) # Make sure that avatar image that we've uploaded is same with avatar image in the server self.assertTrue(filecmp.cmp(fp.name, os.path.splitext(avatar_disk_path(profile))[0] + ".original")) self.assert_json_success(result) self.assertEqual(profile.avatar_source, UserProfile.AVATAR_FROM_USER) self.assertTrue(os.path.exists(avatar_disk_path(profile)))
def test_valid_avatars(self): # type: () -> None """ A PUT request to /json/users/me/avatar with a valid file should return a url and actually create an avatar. """ 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(self.example_email("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) 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)) # 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_medium_avatar_image( user_profile) 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
def test_valid_avatars(self): # type: () -> None """ A PUT request to /json/users/me/avatar with a valid file should return a url and actually create an avatar. """ 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(self.example_email("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) 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)) # 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_medium_avatar_image(user_profile) 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