2016-06-03 02:46:57 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
from __future__ import absolute_import
|
|
|
|
|
2016-11-07 21:49:36 +01:00
|
|
|
from zerver.lib.actions import get_realm_by_string_id, check_add_realm_emoji
|
2016-11-10 19:30:09 +01:00
|
|
|
from zerver.lib.test_classes import ZulipTestCase
|
2016-09-19 23:20:15 +02:00
|
|
|
from zerver.models import RealmEmoji
|
2016-06-03 02:46:57 +02:00
|
|
|
import ujson
|
|
|
|
|
2016-08-23 02:08:42 +02:00
|
|
|
class RealmEmojiTest(ZulipTestCase):
|
2016-06-03 02:46:57 +02:00
|
|
|
|
|
|
|
def test_list(self):
|
2016-06-04 19:54:34 +02:00
|
|
|
# type: () -> None
|
2016-06-03 02:46:57 +02:00
|
|
|
self.login("iago@zulip.com")
|
2016-11-07 21:49:36 +01:00
|
|
|
realm = get_realm_by_string_id('zulip')
|
2016-06-03 02:46:57 +02:00
|
|
|
check_add_realm_emoji(realm, "my_emoji", "https://example.com/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)
|
|
|
|
content = ujson.loads(result.content)
|
|
|
|
self.assertEqual(len(content["emoji"]), 1)
|
|
|
|
|
2016-12-20 15:41:30 +01:00
|
|
|
def test_list_no_author(self):
|
|
|
|
# type: () -> None
|
|
|
|
self.login("iago@zulip.com")
|
|
|
|
realm = get_realm_by_string_id('zulip')
|
|
|
|
RealmEmoji.objects.create(realm=realm, name='my_emojy', img_url='https://example.com/my_emoji')
|
|
|
|
result = self.client_get("/json/realm/emoji")
|
|
|
|
self.assert_json_success(result)
|
|
|
|
content = ujson.loads(result.content)
|
|
|
|
self.assertEqual(len(content["emoji"]), 1)
|
|
|
|
self.assertIsNone(content["emoji"]['my_emojy']['author'])
|
|
|
|
|
|
|
|
def test_list_admins_only(self):
|
|
|
|
# type: () -> None
|
|
|
|
self.login('othello@zulip.com')
|
|
|
|
realm = get_realm_by_string_id('zulip')
|
|
|
|
realm.add_emoji_by_admins_only = True
|
|
|
|
realm.save()
|
|
|
|
check_add_realm_emoji(realm, "my_emoji", "https://example.com/my_emoji")
|
|
|
|
result = self.client_get("/json/realm/emoji")
|
|
|
|
self.assert_json_success(result)
|
|
|
|
content = ujson.loads(result.content)
|
|
|
|
self.assertEqual(len(content["emoji"]), 1)
|
|
|
|
self.assertIsNone(content["emoji"]['my_emoji']['author'])
|
|
|
|
|
2016-06-03 02:46:57 +02:00
|
|
|
def test_upload(self):
|
2016-06-04 19:54:34 +02:00
|
|
|
# type: () -> None
|
2016-12-20 09:22:00 +01:00
|
|
|
email = "iago@zulip.com"
|
|
|
|
self.login(email)
|
2016-06-03 02:46:57 +02:00
|
|
|
data = {"name": "my_emoji", "url": "https://example.com/my_emoji"}
|
|
|
|
result = self.client_put("/json/realm/emoji", info=data)
|
|
|
|
self.assert_json_success(result)
|
|
|
|
self.assertEqual(200, result.status_code)
|
2016-12-20 09:22:00 +01:00
|
|
|
emoji = RealmEmoji.objects.get(name=data['name'])
|
|
|
|
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")
|
2016-06-03 02:46:57 +02:00
|
|
|
content = ujson.loads(result.content)
|
|
|
|
self.assert_json_success(result)
|
|
|
|
self.assertEqual(len(content["emoji"]), 1)
|
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
|
|
|
|
2016-11-07 21:49:36 +01:00
|
|
|
realm_emoji = RealmEmoji.objects.get(realm=get_realm_by_string_id('zulip'))
|
2016-09-19 23:20:15 +02:00
|
|
|
self.assertEqual(
|
|
|
|
str(realm_emoji),
|
|
|
|
'<RealmEmoji(zulip.com): my_emoji https://example.com/my_emoji>'
|
|
|
|
)
|
|
|
|
|
2016-06-03 02:46:57 +02:00
|
|
|
def test_upload_exception(self):
|
2016-06-04 19:54:34 +02:00
|
|
|
# type: () -> None
|
2016-06-03 02:46:57 +02:00
|
|
|
self.login("iago@zulip.com")
|
|
|
|
data = {"name": "my_em*/oji", "url": "https://example.com/my_emoji"}
|
|
|
|
result = self.client_put("/json/realm/emoji", info=data)
|
2016-06-04 19:54:34 +02:00
|
|
|
self.assert_json_error(result, 'Invalid characters in Emoji name')
|
2016-06-03 02:46:57 +02:00
|
|
|
|
2016-12-20 15:41:30 +01:00
|
|
|
def test_upload_admins_only(self):
|
|
|
|
# type: () -> None
|
|
|
|
self.login('othello@zulip.com')
|
|
|
|
realm = get_realm_by_string_id('zulip')
|
|
|
|
realm.add_emoji_by_admins_only = True
|
|
|
|
realm.save()
|
|
|
|
data = {"name": "my_emoji", "url": "https://example.com/my_emoji"}
|
|
|
|
result = self.client_put("/json/realm/emoji", info=data)
|
|
|
|
self.assert_json_error(result, 'Must be a realm administrator')
|
|
|
|
|
2016-06-03 02:46:57 +02:00
|
|
|
def test_delete(self):
|
2016-06-04 19:54:34 +02:00
|
|
|
# type: () -> None
|
2016-06-03 02:46:57 +02:00
|
|
|
self.login("iago@zulip.com")
|
2016-11-07 21:49:36 +01:00
|
|
|
realm = get_realm_by_string_id('zulip')
|
2016-06-03 02:46:57 +02:00
|
|
|
check_add_realm_emoji(realm, "my_emoji", "https://example.com/my_emoji")
|
|
|
|
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")
|
2016-06-03 02:46:57 +02:00
|
|
|
content = ujson.loads(result.content)
|
|
|
|
self.assert_json_success(result)
|
|
|
|
self.assertEqual(len(content["emoji"]), 0)
|
2016-12-20 15:41:30 +01:00
|
|
|
|
|
|
|
def test_delete_admins_only(self):
|
|
|
|
# type: () -> None
|
|
|
|
self.login('othello@zulip.com')
|
|
|
|
realm = get_realm_by_string_id('zulip')
|
|
|
|
realm.add_emoji_by_admins_only = True
|
|
|
|
realm.save()
|
|
|
|
check_add_realm_emoji(realm, "my_emoji", "https://example.com/my_emoji")
|
|
|
|
result = self.client_delete("/json/realm/emoji/my_emoji")
|
|
|
|
self.assert_json_error(result, 'Must be a realm administrator')
|