reactions.py: Don't check for valid emoji name while removing reaction.

On receiving a request for deleting a reaction, just check if such
a reaction exists or not. If it exists then just delete the reaction
otherwise send an error message that such a reaction doesn't exist.
It doesn't make sense to check whether an emoji name is valid or not.
This commit is contained in:
Harshit Bansal
2017-10-08 13:42:41 +00:00
parent 71eee35bce
commit 3acaa79336
2 changed files with 35 additions and 12 deletions

View File

@@ -2,6 +2,7 @@
import ujson
from typing import Any, Mapping, List
from unittest import mock
from six import string_types
from zerver.lib.emoji import emoji_name_to_emoji_code
@@ -32,16 +33,6 @@ class ReactionEmojiTest(ZulipTestCase):
**self.api_auth(sender))
self.assert_json_error(result, "Emoji 'foo' does not exist")
def test_remove_invalid_emoji(self):
# type: () -> None
"""
Removing invalid emoji fails
"""
sender = self.example_email("hamlet")
result = self.client_delete('/api/v1/messages/1/emoji_reactions/foo',
**self.api_auth(sender))
self.assert_json_error(result, "Emoji 'foo' does not exist")
def test_add_deactivated_realm_emoji(self):
# type: () -> None
"""
@@ -265,6 +256,40 @@ class ReactionTest(ZulipTestCase):
**self.api_auth(reaction_sender))
self.assert_json_error(second, "Reaction does not exist")
def test_remove_existing_reaction_with_renamed_emoji(self):
# type: () -> None
"""
Removes an old existing reaction but the name of emoji got changed during
various emoji infra changes.
"""
sender = self.example_email("hamlet")
result = self.client_put('/api/v1/messages/1/emoji_reactions/smile',
**self.api_auth(sender))
self.assert_json_success(result)
with mock.patch('zerver.lib.emoji.name_to_codepoint', name_to_codepoint={}):
result = self.client_delete('/api/v1/messages/1/emoji_reactions/smile',
**self.api_auth(sender))
self.assert_json_success(result)
def test_remove_existing_reaction_with_deactivated_realm_emoji(self):
# type: () -> None
"""
Removes an old existing reaction but the realm emoji used there has been deactivated.
"""
sender = self.example_email("hamlet")
result = self.client_put('/api/v1/messages/1/emoji_reactions/green_tick',
**self.api_auth(sender))
self.assert_json_success(result)
# Deactivate realm emoji.
emoji = RealmEmoji.objects.get(name="green_tick")
emoji.deactivated = True
emoji.save(update_fields=['deactivated'])
result = self.client_delete('/api/v1/messages/1/emoji_reactions/green_tick',
**self.api_auth(sender))
self.assert_json_success(result)
class ReactionEventTest(ZulipTestCase):
def test_add_event(self):

View File

@@ -51,8 +51,6 @@ def remove_reaction_backend(request, user_profile, message_id, emoji_name):
# cannot see the message (e.g. for messages to private streams).
message = access_message(user_profile, message_id)[0]
check_valid_emoji(message.sender.realm, emoji_name)
# We could probably just make this check be a try/except for the
# IntegrityError from it already existing, but this is a bit cleaner.
if not Reaction.objects.filter(user_profile=user_profile,