2020-05-26 07:16:25 +02:00
|
|
|
from unittest import mock
|
2018-03-11 18:55:20 +01:00
|
|
|
|
2020-06-11 00:54:34 +02:00
|
|
|
from zerver.lib.actions import check_add_realm_emoji, do_create_realm, do_create_user
|
2016-11-10 19:30:09 +01:00
|
|
|
from zerver.lib.test_classes import ZulipTestCase
|
2019-02-02 23:53:44 +01:00
|
|
|
from zerver.lib.test_helpers import get_test_image_file
|
2019-05-04 04:47:44 +02:00
|
|
|
from zerver.models import Realm, RealmEmoji, UserProfile, get_realm
|
2016-06-03 02:46:57 +02:00
|
|
|
|
2020-06-11 00:54:34 +02:00
|
|
|
|
2016-08-23 02:08:42 +02:00
|
|
|
class RealmEmojiTest(ZulipTestCase):
|
2016-06-03 02:46:57 +02:00
|
|
|
|
2018-03-11 18:55:20 +01:00
|
|
|
def create_test_emoji(self, name: str, author: UserProfile) -> RealmEmoji:
|
|
|
|
with get_test_image_file('img.png') as img_file:
|
|
|
|
realm_emoji = check_add_realm_emoji(realm=author.realm,
|
|
|
|
name=name,
|
|
|
|
author=author,
|
|
|
|
image_file=img_file)
|
|
|
|
if realm_emoji is None:
|
|
|
|
raise Exception("Error creating test emoji.") # nocoverage
|
|
|
|
return realm_emoji
|
|
|
|
|
|
|
|
def create_test_emoji_with_no_author(self, name: str, realm: Realm) -> RealmEmoji:
|
|
|
|
realm_emoji = RealmEmoji.objects.create(realm=realm, name=name)
|
|
|
|
return realm_emoji
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_list(self) -> None:
|
2018-03-11 18:55:20 +01:00
|
|
|
emoji_author = self.example_user('iago')
|
2020-03-06 18:40:46 +01:00
|
|
|
self.login_user(emoji_author)
|
2018-03-11 18:55:20 +01:00
|
|
|
self.create_test_emoji('my_emoji', emoji_author)
|
|
|
|
|
2016-07-28 00:38:45 +02:00
|
|
|
result = self.client_get("/json/realm/emoji")
|
2016-06-03 02:46:57 +02:00
|
|
|
self.assert_json_success(result)
|
|
|
|
self.assertEqual(200, result.status_code)
|
2017-08-16 09:51:32 +02:00
|
|
|
self.assertEqual(len(result.json()["emoji"]), 2)
|
2016-06-03 02:46:57 +02:00
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_list_no_author(self) -> None:
|
2020-03-06 18:40:46 +01:00
|
|
|
self.login('iago')
|
2017-01-04 05:30:48 +01:00
|
|
|
realm = get_realm('zulip')
|
2018-03-11 18:55:20 +01:00
|
|
|
realm_emoji = self.create_test_emoji_with_no_author('my_emoji', realm)
|
|
|
|
|
2016-12-20 15:41:30 +01:00
|
|
|
result = self.client_get("/json/realm/emoji")
|
|
|
|
self.assert_json_success(result)
|
2017-08-17 08:43:38 +02:00
|
|
|
content = result.json()
|
2017-07-14 14:56:53 +02:00
|
|
|
self.assertEqual(len(content["emoji"]), 2)
|
2018-03-11 18:55:20 +01:00
|
|
|
test_emoji = content["emoji"][str(realm_emoji.id)]
|
2020-05-29 11:11:48 +02:00
|
|
|
self.assertIsNone(test_emoji['author_id'])
|
2016-12-20 15:41:30 +01:00
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_list_admins_only(self) -> None:
|
2018-03-11 18:55:20 +01:00
|
|
|
# Test that realm emoji list is public and realm emojis
|
|
|
|
# having no author are also there in the list.
|
2020-03-06 18:40:46 +01:00
|
|
|
self.login('othello')
|
2017-01-04 05:30:48 +01:00
|
|
|
realm = get_realm('zulip')
|
2016-12-20 15:41:30 +01:00
|
|
|
realm.add_emoji_by_admins_only = True
|
|
|
|
realm.save()
|
2018-03-11 18:55:20 +01:00
|
|
|
realm_emoji = self.create_test_emoji_with_no_author('my_emoji', realm)
|
|
|
|
|
2016-12-20 15:41:30 +01:00
|
|
|
result = self.client_get("/json/realm/emoji")
|
|
|
|
self.assert_json_success(result)
|
2017-08-17 08:43:38 +02:00
|
|
|
content = result.json()
|
2017-07-14 14:56:53 +02:00
|
|
|
self.assertEqual(len(content["emoji"]), 2)
|
2018-03-11 18:55:20 +01:00
|
|
|
test_emoji = content["emoji"][str(realm_emoji.id)]
|
2020-05-29 11:11:48 +02:00
|
|
|
self.assertIsNone(test_emoji['author_id'])
|
2016-12-20 15:41:30 +01:00
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_upload(self) -> None:
|
2020-03-06 18:40:46 +01:00
|
|
|
user = self.example_user('iago')
|
|
|
|
email = user.email
|
|
|
|
self.login_user(user)
|
2017-03-13 05:45:50 +01:00
|
|
|
with get_test_image_file('img.png') as fp1:
|
|
|
|
emoji_data = {'f1': fp1}
|
2017-07-05 19:13:11 +02:00
|
|
|
result = self.client_post('/json/realm/emoji/my_emoji', info=emoji_data)
|
2016-06-03 02:46:57 +02:00
|
|
|
self.assert_json_success(result)
|
|
|
|
self.assertEqual(200, result.status_code)
|
2018-03-11 18:55:20 +01:00
|
|
|
realm_emoji = RealmEmoji.objects.get(name="my_emoji")
|
|
|
|
self.assertEqual(realm_emoji.author.email, email)
|
2016-06-03 02:46:57 +02:00
|
|
|
|
2016-07-28 00:38:45 +02:00
|
|
|
result = self.client_get("/json/realm/emoji")
|
2017-08-17 08:43:38 +02:00
|
|
|
content = result.json()
|
2016-06-03 02:46:57 +02:00
|
|
|
self.assert_json_success(result)
|
2017-07-14 14:56:53 +02:00
|
|
|
self.assertEqual(len(content["emoji"]), 2)
|
2018-03-11 18:55:20 +01:00
|
|
|
test_emoji = content["emoji"][str(realm_emoji.id)]
|
2020-05-29 11:11:48 +02:00
|
|
|
self.assertIn('author_id', test_emoji)
|
|
|
|
author = UserProfile.objects.get(id = test_emoji['author_id'])
|
|
|
|
self.assertEqual(author.email, email)
|
2016-06-03 02:46:57 +02:00
|
|
|
|
2018-03-11 18:55:20 +01:00
|
|
|
def test_realm_emoji_repr(self) -> None:
|
|
|
|
realm_emoji = RealmEmoji.objects.get(name='green_tick')
|
|
|
|
file_name = str(realm_emoji.id) + '.png'
|
2016-09-19 23:20:15 +02:00
|
|
|
self.assertEqual(
|
|
|
|
str(realm_emoji),
|
python: Use trailing commas consistently.
Automatically generated by the following script, based on the output
of lint with flake8-comma:
import re
import sys
last_filename = None
last_row = None
lines = []
for msg in sys.stdin:
m = re.match(
r"\x1b\[35mflake8 \|\x1b\[0m \x1b\[1;31m(.+):(\d+):(\d+): (\w+)", msg
)
if m:
filename, row_str, col_str, err = m.groups()
row, col = int(row_str), int(col_str)
if filename == last_filename:
assert last_row != row
else:
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
with open(filename) as f:
lines = f.readlines()
last_filename = filename
last_row = row
line = lines[row - 1]
if err in ["C812", "C815"]:
lines[row - 1] = line[: col - 1] + "," + line[col - 1 :]
elif err in ["C819"]:
assert line[col - 2] == ","
lines[row - 1] = line[: col - 2] + line[col - 1 :].lstrip(" ")
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-10 05:23:40 +02:00
|
|
|
f'<RealmEmoji(zulip): {realm_emoji.id} green_tick False {file_name}>',
|
2016-09-19 23:20:15 +02:00
|
|
|
)
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_upload_exception(self) -> None:
|
2020-03-06 18:40:46 +01:00
|
|
|
self.login('iago')
|
2017-03-13 05:45:50 +01:00
|
|
|
with get_test_image_file('img.png') as fp1:
|
|
|
|
emoji_data = {'f1': fp1}
|
2017-07-05 19:13:11 +02:00
|
|
|
result = self.client_post('/json/realm/emoji/my_em*oji', info=emoji_data)
|
2017-05-02 23:13:40 +02:00
|
|
|
self.assert_json_error(result, 'Invalid characters in emoji name')
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_upload_uppercase_exception(self) -> None:
|
2020-03-06 18:40:46 +01:00
|
|
|
self.login('iago')
|
2017-05-02 23:13:40 +02:00
|
|
|
with get_test_image_file('img.png') as fp1:
|
|
|
|
emoji_data = {'f1': fp1}
|
2017-07-05 19:13:11 +02:00
|
|
|
result = self.client_post('/json/realm/emoji/my_EMoji', info=emoji_data)
|
2017-01-17 08:38:25 +01:00
|
|
|
self.assert_json_error(result, 'Invalid characters in emoji name')
|
2016-06-03 02:46:57 +02:00
|
|
|
|
2020-04-08 21:49:55 +02:00
|
|
|
def test_missing_name_exception(self) -> None:
|
|
|
|
self.login('iago')
|
|
|
|
with get_test_image_file('img.png') as fp1:
|
|
|
|
emoji_data = {'f1': fp1}
|
|
|
|
result = self.client_post('/json/realm/emoji/', info=emoji_data)
|
|
|
|
self.assert_json_error(result, 'Emoji name is missing')
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_upload_admins_only(self) -> None:
|
2020-03-06 18:40:46 +01:00
|
|
|
self.login('othello')
|
2017-01-04 05:30:48 +01:00
|
|
|
realm = get_realm('zulip')
|
2016-12-20 15:41:30 +01:00
|
|
|
realm.add_emoji_by_admins_only = True
|
|
|
|
realm.save()
|
2017-03-13 05:45:50 +01:00
|
|
|
with get_test_image_file('img.png') as fp1:
|
|
|
|
emoji_data = {'f1': fp1}
|
2017-07-05 19:13:11 +02:00
|
|
|
result = self.client_post('/json/realm/emoji/my_emoji', info=emoji_data)
|
2018-03-08 01:47:17 +01:00
|
|
|
self.assert_json_error(result, 'Must be an organization administrator')
|
2016-12-20 15:41:30 +01:00
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_upload_anyone(self) -> None:
|
2020-03-06 18:40:46 +01:00
|
|
|
self.login('othello')
|
2017-07-22 23:26:38 +02:00
|
|
|
realm = get_realm('zulip')
|
|
|
|
realm.add_emoji_by_admins_only = False
|
|
|
|
realm.save()
|
|
|
|
with get_test_image_file('img.png') as fp1:
|
|
|
|
emoji_data = {'f1': fp1}
|
|
|
|
result = self.client_post('/json/realm/emoji/my_emoji', info=emoji_data)
|
|
|
|
self.assert_json_success(result)
|
|
|
|
|
2018-06-08 15:30:44 +02:00
|
|
|
def test_emoji_upload_by_guest_user(self) -> None:
|
2020-03-06 18:40:46 +01:00
|
|
|
self.login('polonius')
|
2018-06-08 15:30:44 +02:00
|
|
|
with get_test_image_file('img.png') as fp1:
|
|
|
|
emoji_data = {'f1': fp1}
|
|
|
|
result = self.client_post('/json/realm/emoji/my_emoji', info=emoji_data)
|
|
|
|
self.assert_json_error(result, 'Not allowed for guest users')
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_delete(self) -> None:
|
2018-03-11 18:55:20 +01:00
|
|
|
emoji_author = self.example_user('iago')
|
2020-03-06 18:40:46 +01:00
|
|
|
self.login_user(emoji_author)
|
2018-03-11 18:55:20 +01:00
|
|
|
realm_emoji = self.create_test_emoji('my_emoji', emoji_author)
|
|
|
|
result = self.client_delete('/json/realm/emoji/my_emoji')
|
2016-06-03 02:46:57 +02:00
|
|
|
self.assert_json_success(result)
|
|
|
|
|
2016-07-28 00:38:45 +02:00
|
|
|
result = self.client_get("/json/realm/emoji")
|
2017-08-16 09:51:32 +02:00
|
|
|
emojis = result.json()["emoji"]
|
2016-06-03 02:46:57 +02:00
|
|
|
self.assert_json_success(result)
|
2017-05-22 17:08:37 +02:00
|
|
|
# We only mark an emoji as deactivated instead of
|
|
|
|
# removing it from the database.
|
2017-07-14 14:56:53 +02:00
|
|
|
self.assertEqual(len(emojis), 2)
|
2018-03-11 18:55:20 +01:00
|
|
|
test_emoji = emojis[str(realm_emoji.id)]
|
|
|
|
self.assertEqual(test_emoji["deactivated"], True)
|
2016-12-20 15:41:30 +01:00
|
|
|
|
2018-03-11 18:55:20 +01:00
|
|
|
def test_delete_no_author(self) -> None:
|
2020-03-06 18:40:46 +01:00
|
|
|
self.login('iago')
|
2017-01-04 05:30:48 +01:00
|
|
|
realm = get_realm('zulip')
|
2018-03-11 18:55:20 +01:00
|
|
|
self.create_test_emoji_with_no_author('my_emoji', realm)
|
|
|
|
result = self.client_delete('/json/realm/emoji/my_emoji')
|
|
|
|
self.assert_json_success(result)
|
|
|
|
|
|
|
|
def test_delete_admins_only(self) -> None:
|
|
|
|
emoji_author = self.example_user('othello')
|
2020-03-06 18:40:46 +01:00
|
|
|
self.login_user(emoji_author)
|
2018-03-11 18:55:20 +01:00
|
|
|
realm = get_realm('zulip')
|
2016-12-20 15:41:30 +01:00
|
|
|
realm.add_emoji_by_admins_only = True
|
|
|
|
realm.save()
|
2018-03-11 18:55:20 +01:00
|
|
|
self.create_test_emoji_with_no_author("my_emoji", realm)
|
2016-12-20 15:41:30 +01:00
|
|
|
result = self.client_delete("/json/realm/emoji/my_emoji")
|
2018-03-08 01:47:17 +01:00
|
|
|
self.assert_json_error(result, 'Must be an organization administrator')
|
2017-01-17 08:48:07 +01:00
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_delete_admin_or_author(self) -> None:
|
2017-05-18 21:53:33 +02:00
|
|
|
# If any user in a realm can upload the emoji then the user who
|
|
|
|
# uploaded it as well as the admin should be able to delete it.
|
2018-03-11 18:55:20 +01:00
|
|
|
emoji_author = self.example_user('othello')
|
2017-05-18 21:53:33 +02:00
|
|
|
realm = get_realm('zulip')
|
|
|
|
realm.add_emoji_by_admins_only = False
|
|
|
|
realm.save()
|
2018-03-11 18:55:20 +01:00
|
|
|
|
|
|
|
self.create_test_emoji('my_emoji_1', emoji_author)
|
2020-03-06 18:40:46 +01:00
|
|
|
self.login_user(emoji_author)
|
2017-05-22 17:08:37 +02:00
|
|
|
result = self.client_delete("/json/realm/emoji/my_emoji_1")
|
2017-05-18 21:53:33 +02:00
|
|
|
self.assert_json_success(result)
|
|
|
|
self.logout()
|
|
|
|
|
2018-03-11 18:55:20 +01:00
|
|
|
self.create_test_emoji('my_emoji_2', emoji_author)
|
2020-03-06 18:40:46 +01:00
|
|
|
self.login('iago')
|
2017-05-22 17:08:37 +02:00
|
|
|
result = self.client_delete("/json/realm/emoji/my_emoji_2")
|
2017-05-18 21:53:33 +02:00
|
|
|
self.assert_json_success(result)
|
|
|
|
self.logout()
|
|
|
|
|
2018-03-11 18:55:20 +01:00
|
|
|
self.create_test_emoji('my_emoji_3', emoji_author)
|
2020-03-06 18:40:46 +01:00
|
|
|
self.login('cordelia')
|
2017-05-22 17:08:37 +02:00
|
|
|
result = self.client_delete("/json/realm/emoji/my_emoji_3")
|
2018-03-08 01:47:17 +01:00
|
|
|
self.assert_json_error(result, 'Must be an organization administrator or emoji author')
|
2017-05-18 21:53:33 +02:00
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_delete_exception(self) -> None:
|
2020-03-06 18:40:46 +01:00
|
|
|
self.login('iago')
|
2017-01-17 08:48:07 +01:00
|
|
|
result = self.client_delete("/json/realm/emoji/invalid_emoji")
|
|
|
|
self.assert_json_error(result, "Emoji 'invalid_emoji' does not exist")
|
2017-03-13 05:45:50 +01:00
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_multiple_upload(self) -> None:
|
2020-03-06 18:40:46 +01:00
|
|
|
self.login('iago')
|
2017-03-13 05:45:50 +01:00
|
|
|
with get_test_image_file('img.png') as fp1, get_test_image_file('img.png') as fp2:
|
2017-07-05 19:13:11 +02:00
|
|
|
result = self.client_post('/json/realm/emoji/my_emoji', {'f1': fp1, 'f2': fp2})
|
2017-03-13 05:45:50 +01:00
|
|
|
self.assert_json_error(result, 'You must upload exactly one file.')
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_emoji_upload_file_size_error(self) -> None:
|
2020-03-06 18:40:46 +01:00
|
|
|
self.login('iago')
|
2017-03-13 05:45:50 +01:00
|
|
|
with get_test_image_file('img.png') as fp:
|
|
|
|
with self.settings(MAX_EMOJI_FILE_SIZE=0):
|
2017-07-05 19:13:11 +02:00
|
|
|
result = self.client_post('/json/realm/emoji/my_emoji', {'file': fp})
|
2020-06-15 23:22:24 +02:00
|
|
|
self.assert_json_error(result, 'Uploaded file is larger than the allowed limit of 0 MiB')
|
2017-03-13 05:45:50 +01:00
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_upload_already_existed_emoji(self) -> None:
|
2020-03-06 18:40:46 +01:00
|
|
|
self.login('iago')
|
2017-03-13 05:45:50 +01:00
|
|
|
with get_test_image_file('img.png') as fp1:
|
|
|
|
emoji_data = {'f1': fp1}
|
2017-07-22 21:14:20 +02:00
|
|
|
result = self.client_post('/json/realm/emoji/green_tick', info=emoji_data)
|
2018-03-11 18:55:20 +01:00
|
|
|
self.assert_json_error(result, 'A custom emoji with this name already exists.')
|
|
|
|
|
|
|
|
def test_reupload(self) -> None:
|
|
|
|
# An user should be able to reupload an emoji with same name.
|
2020-03-06 18:40:46 +01:00
|
|
|
self.login('iago')
|
2018-03-11 18:55:20 +01:00
|
|
|
with get_test_image_file('img.png') as fp1:
|
|
|
|
emoji_data = {'f1': fp1}
|
|
|
|
result = self.client_post('/json/realm/emoji/my_emoji', info=emoji_data)
|
|
|
|
self.assert_json_success(result)
|
|
|
|
|
|
|
|
result = self.client_delete("/json/realm/emoji/my_emoji")
|
|
|
|
self.assert_json_success(result)
|
|
|
|
|
|
|
|
with get_test_image_file('img.png') as fp1:
|
|
|
|
emoji_data = {'f1': fp1}
|
|
|
|
result = self.client_post('/json/realm/emoji/my_emoji', info=emoji_data)
|
|
|
|
self.assert_json_success(result)
|
|
|
|
|
|
|
|
result = self.client_get("/json/realm/emoji")
|
|
|
|
emojis = result.json()["emoji"]
|
|
|
|
self.assert_json_success(result)
|
|
|
|
self.assertEqual(len(emojis), 3)
|
|
|
|
|
|
|
|
def test_failed_file_upload(self) -> None:
|
2020-03-06 18:40:46 +01:00
|
|
|
self.login('iago')
|
2018-03-11 18:55:20 +01:00
|
|
|
with mock.patch('zerver.lib.upload.write_local_file', side_effect=Exception()):
|
|
|
|
with get_test_image_file('img.png') as fp1:
|
|
|
|
emoji_data = {'f1': fp1}
|
|
|
|
result = self.client_post('/json/realm/emoji/my_emoji', info=emoji_data)
|
|
|
|
self.assert_json_error(result, "Image file upload failed.")
|
2018-03-18 17:33:10 +01:00
|
|
|
|
|
|
|
def test_check_admin_realm_emoji(self) -> None:
|
|
|
|
# Test that an user A is able to remove a realm emoji uploaded by him
|
|
|
|
# and having same name as a deactivated realm emoji uploaded by some
|
|
|
|
# other user B.
|
|
|
|
emoji_author_1 = self.example_user('cordelia')
|
|
|
|
self.create_test_emoji('test_emoji', emoji_author_1)
|
2020-03-06 18:40:46 +01:00
|
|
|
self.login_user(emoji_author_1)
|
2018-03-18 17:33:10 +01:00
|
|
|
result = self.client_delete('/json/realm/emoji/test_emoji')
|
|
|
|
self.assert_json_success(result)
|
|
|
|
|
|
|
|
emoji_author_2 = self.example_user('othello')
|
|
|
|
self.create_test_emoji('test_emoji', emoji_author_2)
|
2020-03-06 18:40:46 +01:00
|
|
|
self.login_user(emoji_author_2)
|
2018-03-18 17:33:10 +01:00
|
|
|
result = self.client_delete('/json/realm/emoji/test_emoji')
|
|
|
|
self.assert_json_success(result)
|
|
|
|
|
|
|
|
def test_check_admin_different_realm_emoji(self) -> None:
|
|
|
|
# Test that two different realm emojis in two different realms but
|
|
|
|
# having same name can be administered independently.
|
|
|
|
realm_1 = do_create_realm('test_realm', 'test_realm')
|
2020-07-16 14:10:43 +02:00
|
|
|
emoji_author_1 = do_create_user(
|
|
|
|
'abc@example.com',
|
|
|
|
password='abc',
|
|
|
|
realm=realm_1,
|
|
|
|
full_name='abc',
|
|
|
|
)
|
2018-03-18 17:33:10 +01:00
|
|
|
self.create_test_emoji('test_emoji', emoji_author_1)
|
|
|
|
|
|
|
|
emoji_author_2 = self.example_user('othello')
|
|
|
|
self.create_test_emoji('test_emoji', emoji_author_2)
|
2020-03-06 18:40:46 +01:00
|
|
|
self.login_user(emoji_author_2)
|
2018-03-18 17:33:10 +01:00
|
|
|
result = self.client_delete('/json/realm/emoji/test_emoji')
|
|
|
|
self.assert_json_success(result)
|