2016-06-03 02:46:57 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2017-01-04 05:30:48 +01:00
|
|
|
from zerver.lib.actions import get_realm, check_add_realm_emoji
|
2016-11-10 19:30:09 +01:00
|
|
|
from zerver.lib.test_classes import ZulipTestCase
|
2017-07-11 20:03:03 +02:00
|
|
|
from zerver.lib.test_helpers import get_test_image_file, get_user
|
2016-09-19 23:20:15 +02:00
|
|
|
from zerver.models import RealmEmoji
|
2016-06-03 02:46:57 +02:00
|
|
|
|
2016-08-23 02:08:42 +02:00
|
|
|
class RealmEmojiTest(ZulipTestCase):
|
2016-06-03 02:46:57 +02:00
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_list(self) -> None:
|
2017-05-24 05:08:49 +02:00
|
|
|
email = self.example_email('iago')
|
|
|
|
self.login(email)
|
2017-01-04 05:30:48 +01:00
|
|
|
realm = get_realm('zulip')
|
2017-03-13 05:45:50 +01:00
|
|
|
check_add_realm_emoji(realm, "my_emoji", "my_emoji")
|
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:
|
2017-05-24 05:08:49 +02:00
|
|
|
email = self.example_email('iago')
|
|
|
|
self.login(email)
|
2017-01-04 05:30:48 +01:00
|
|
|
realm = get_realm('zulip')
|
2017-07-22 21:10:45 +02:00
|
|
|
RealmEmoji.objects.create(realm=realm, name='my_emoji', file_name='my_emoji')
|
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)
|
2017-07-22 21:10:45 +02:00
|
|
|
self.assertIsNone(content["emoji"]['my_emoji']['author'])
|
2016-12-20 15:41:30 +01:00
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_list_admins_only(self) -> None:
|
2017-05-24 05:08:49 +02:00
|
|
|
email = self.example_email('othello')
|
|
|
|
self.login(email)
|
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-07-22 21:10:45 +02:00
|
|
|
check_add_realm_emoji(realm, 'my_emoji', 'my_emoji')
|
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)
|
2017-07-22 21:10:45 +02:00
|
|
|
self.assertIsNone(content["emoji"]['my_emoji']['author'])
|
2016-12-20 15:41:30 +01:00
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_upload(self) -> None:
|
2017-05-24 05:08:49 +02:00
|
|
|
email = self.example_email('iago')
|
2016-12-20 09:22:00 +01:00
|
|
|
self.login(email)
|
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)
|
2017-01-17 08:38:25 +01:00
|
|
|
emoji = RealmEmoji.objects.get(name="my_emoji")
|
2016-12-20 09:22:00 +01:00
|
|
|
self.assertEqual(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)
|
2016-12-20 10:51:28 +01:00
|
|
|
self.assertIn('author', content["emoji"]['my_emoji'])
|
|
|
|
self.assertEqual(
|
|
|
|
content["emoji"]['my_emoji']['author']['email'], email)
|
2016-06-03 02:46:57 +02:00
|
|
|
|
2017-07-14 14:56:53 +02:00
|
|
|
realm_emoji = RealmEmoji.objects.get(name='my_emoji')
|
2016-09-19 23:20:15 +02:00
|
|
|
self.assertEqual(
|
|
|
|
str(realm_emoji),
|
2017-03-13 05:45:50 +01:00
|
|
|
'<RealmEmoji(zulip): my_emoji my_emoji.png>'
|
2016-09-19 23:20:15 +02:00
|
|
|
)
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_upload_exception(self) -> None:
|
2017-05-24 05:08:49 +02:00
|
|
|
email = self.example_email('iago')
|
|
|
|
self.login(email)
|
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:
|
2017-05-24 05:08:49 +02:00
|
|
|
email = self.example_email('iago')
|
|
|
|
self.login(email)
|
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
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_upload_admins_only(self) -> None:
|
2017-05-24 05:08:49 +02:00
|
|
|
email = self.example_email('othello')
|
|
|
|
self.login(email)
|
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)
|
2016-12-20 15:41:30 +01:00
|
|
|
self.assert_json_error(result, 'Must be a realm administrator')
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_upload_anyone(self) -> None:
|
2017-07-22 23:26:38 +02:00
|
|
|
email = self.example_email('othello')
|
|
|
|
self.login(email)
|
|
|
|
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)
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_delete(self) -> None:
|
2017-05-24 05:08:49 +02:00
|
|
|
email = self.example_email('iago')
|
|
|
|
self.login(email)
|
2017-01-04 05:30:48 +01:00
|
|
|
realm = get_realm('zulip')
|
2017-03-13 05:45:50 +01:00
|
|
|
check_add_realm_emoji(realm, "my_emoji", "my_emoji.png")
|
2016-06-03 02:46:57 +02:00
|
|
|
result = self.client_delete("/json/realm/emoji/my_emoji")
|
|
|
|
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)
|
2017-05-22 17:08:37 +02:00
|
|
|
self.assertEqual(emojis["my_emoji"]["deactivated"], True)
|
2016-12-20 15:41:30 +01:00
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_delete_admins_only(self) -> None:
|
2017-05-24 05:08:49 +02:00
|
|
|
email = self.example_email('othello')
|
|
|
|
self.login(email)
|
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
|
|
|
check_add_realm_emoji(realm, "my_emoji", "my_emoji.png")
|
2016-12-20 15:41:30 +01:00
|
|
|
result = self.client_delete("/json/realm/emoji/my_emoji")
|
|
|
|
self.assert_json_error(result, 'Must be a realm 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.
|
|
|
|
realm = get_realm('zulip')
|
2017-07-11 20:03:03 +02:00
|
|
|
author = get_user('othello@zulip.com', realm)
|
2017-05-18 21:53:33 +02:00
|
|
|
realm.add_emoji_by_admins_only = False
|
|
|
|
realm.save()
|
2017-05-22 17:08:37 +02:00
|
|
|
check_add_realm_emoji(realm, "my_emoji_1", "my_emoji.png", author)
|
2017-05-18 21:53:33 +02:00
|
|
|
self.login('othello@zulip.com')
|
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()
|
|
|
|
|
2017-05-22 17:08:37 +02:00
|
|
|
check_add_realm_emoji(realm, "my_emoji_2", "my_emoji.png", author)
|
2017-05-18 21:53:33 +02:00
|
|
|
self.login('iago@zulip.com')
|
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()
|
|
|
|
|
2017-05-22 17:08:37 +02:00
|
|
|
check_add_realm_emoji(realm, "my_emoji_3", "my_emoji.png", author)
|
2017-05-18 21:53:33 +02:00
|
|
|
self.login('cordelia@zulip.com')
|
2017-05-22 17:08:37 +02:00
|
|
|
result = self.client_delete("/json/realm/emoji/my_emoji_3")
|
2017-05-18 21:53:33 +02:00
|
|
|
self.assert_json_error(result, 'Must be a realm administrator or emoji author')
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_delete_exception(self) -> None:
|
2017-05-24 05:08:49 +02:00
|
|
|
email = self.example_email('iago')
|
|
|
|
self.login(email)
|
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:
|
2017-05-24 05:08:49 +02:00
|
|
|
email = self.example_email('iago')
|
|
|
|
self.login(email)
|
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:
|
2017-05-24 05:08:49 +02:00
|
|
|
email = self.example_email('iago')
|
|
|
|
self.login(email)
|
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})
|
2017-03-13 05:45:50 +01:00
|
|
|
self.assert_json_error(result, 'Uploaded file is larger than the allowed limit of 0 MB')
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_upload_already_existed_emoji(self) -> None:
|
2017-05-24 05:08:49 +02:00
|
|
|
email = self.example_email('iago')
|
|
|
|
self.login(email)
|
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)
|
2017-03-13 05:45:50 +01:00
|
|
|
self.assert_json_error(result, 'Realm emoji with this Realm and Name already exists.')
|