From 7950f82f273b63b781468136564d8e3eec6fade2 Mon Sep 17 00:00:00 2001 From: Harshit Bansal Date: Tue, 23 May 2017 06:41:30 +0000 Subject: [PATCH] realm_emoji: Make sure that a deactivated realm emoji can't be used. A deactivated realm emoji should neither be accepted further as a reaction nor its further occurences in a message be rendered as an emoji. However, all the old occurences should continue to render normally. --- static/js/markdown.js | 3 ++- zerver/lib/bugdown/__init__.py | 2 +- zerver/lib/emoji.py | 3 ++- zerver/tests/test_bugdown.py | 13 +++++++++++++ zerver/tests/test_reactions.py | 21 ++++++++++++++++++++- 5 files changed, 38 insertions(+), 4 deletions(-) diff --git a/static/js/markdown.js b/static/js/markdown.js index 890c8d2d79..dcb843ed7f 100644 --- a/static/js/markdown.js +++ b/static/js/markdown.js @@ -128,7 +128,8 @@ function handleUnicodeEmoji(unicode_emoji) { function handleEmoji(emoji_name) { var input_emoji = ':' + emoji_name + ":"; var emoji_url; - if (emoji.realm_emojis.hasOwnProperty(emoji_name)) { + if (emoji.realm_emojis.hasOwnProperty(emoji_name) && + emoji.realm_emojis[emoji_name].deactivated !== true) { emoji_url = emoji.realm_emojis[emoji_name].emoji_url; return '' + input_emoji + ' None - if emoji_name in set(realm.get_emoji().keys()): + realm_emojis = realm.get_emoji() + if emoji_name in realm_emojis and not realm_emojis[emoji_name]['deactivated']: return if emoji_name in name_to_codepoint: return diff --git a/zerver/tests/test_bugdown.py b/zerver/tests/test_bugdown.py index edc75f6f0f..2f42b1494a 100644 --- a/zerver/tests/test_bugdown.py +++ b/zerver/tests/test_bugdown.py @@ -33,6 +33,7 @@ from zerver.models import ( Message, Stream, Realm, + RealmEmoji, RealmFilter, Recipient, ) @@ -505,6 +506,18 @@ class BugdownTest(ZulipTestCase): converted = bugdown.convert(":test:", message_realm=realm, message=msg) self.assertEqual(converted, '

:test:

') + def test_deactivated_realm_emoji(self): + # type: () -> None + realm = get_realm('zulip') + check_add_realm_emoji(realm, "test", 'test.png') + emoji = RealmEmoji.objects.get(realm=realm, name='test') + emoji.deactivated = True + emoji.save(update_fields=['deactivated']) + + msg = Message(sender=self.example_user('hamlet')) + converted = bugdown.convert(":test:", message_realm=realm, message=msg) + self.assertEqual(converted, '

:test:

') + def test_unicode_emoji(self): # type: () -> None msg = u'\u2615' # ☕ diff --git a/zerver/tests/test_reactions.py b/zerver/tests/test_reactions.py index c22ac2c977..9c05d48be5 100644 --- a/zerver/tests/test_reactions.py +++ b/zerver/tests/test_reactions.py @@ -8,7 +8,7 @@ from six import string_types from zerver.lib.test_helpers import tornado_redirected_to_list, get_display_recipient, \ get_test_image_file from zerver.lib.test_classes import ZulipTestCase -from zerver.models import get_realm, Recipient, UserMessage +from zerver.models import get_realm, RealmEmoji, Recipient, UserMessage class ReactionEmojiTest(ZulipTestCase): def test_missing_emoji(self): @@ -41,6 +41,25 @@ class ReactionEmojiTest(ZulipTestCase): **self.api_auth(sender)) self.assert_json_error(result, "Emoji 'foo' does not exist") + def test_add_deactivated_realm_emoji(self): + # type: () -> None + """ + Sending deactivated realm emoji fails. + """ + email = "iago@zulip.com" + self.login(email) + with get_test_image_file('img.png') as fp1: + emoji_data = {'f1': fp1} + result = self.client_put_multipart('/json/realm/emoji/my_emoji', info=emoji_data) + self.assert_json_success(result) + emoji = RealmEmoji.objects.get(name="my_emoji") + emoji.deactivated = True + emoji.save(update_fields=['deactivated']) + sender = 'hamlet@zulip.com' + result = self.client_put('/api/v1/messages/1/emoji_reactions/my_emoji', + **self.api_auth(sender)) + self.assert_json_error(result, "Emoji 'my_emoji' does not exist") + def test_valid_emoji(self): # type: () -> None """