Convert realm emoji upload to use PUT properly.

This commit is contained in:
Tim Abbott 2017-01-16 23:38:25 -08:00
parent 09b04f2aec
commit f3b9abee14
4 changed files with 29 additions and 20 deletions

View File

@ -58,8 +58,8 @@ class ReactionEmojiTest(ZulipTestCase):
"""
sender = 'hamlet@zulip.com'
emoji_name = 'my_emoji'
emoji_data = {'name': emoji_name, 'url': 'https://example.com/my_emoji'}
result = self.client_put('/json/realm/emoji', info=emoji_data,
emoji_data = {'url': 'https://example.com/my_emoji'}
result = self.client_put('/json/realm/emoji/my_emoji', info=emoji_data,
**self.api_auth(sender))
self.assert_json_success(result)
self.assertEqual(200, result.status_code)

View File

@ -47,11 +47,11 @@ class RealmEmojiTest(ZulipTestCase):
# type: () -> None
email = "iago@zulip.com"
self.login(email)
data = {"name": "my_emoji", "url": "https://example.com/my_emoji"}
result = self.client_put("/json/realm/emoji", info=data)
data = {"url": "https://example.com/my_emoji"}
result = self.client_put("/json/realm/emoji/my_emoji", data)
self.assert_json_success(result)
self.assertEqual(200, result.status_code)
emoji = RealmEmoji.objects.get(name=data['name'])
emoji = RealmEmoji.objects.get(name="my_emoji")
self.assertEqual(emoji.author.email, email)
result = self.client_get("/json/realm/emoji")
@ -71,9 +71,9 @@ class RealmEmojiTest(ZulipTestCase):
def test_upload_exception(self):
# type: () -> None
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)
self.assert_json_error(result, 'Invalid characters in Emoji name')
data = {"url": "https://example.com/my_emoji"}
result = self.client_put("/json/realm/emoji/my_em*oji", info=data)
self.assert_json_error(result, 'Invalid characters in emoji name')
def test_upload_admins_only(self):
# type: () -> None
@ -81,8 +81,8 @@ class RealmEmojiTest(ZulipTestCase):
realm = get_realm('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)
data = {"url": "https://example.com/my_emoji"}
result = self.client_put("/json/realm/emoji/my_emoji", info=data)
self.assert_json_error(result, 'Must be a realm administrator')
def test_delete(self):

View File

@ -1,11 +1,14 @@
from __future__ import absolute_import
import re
from django.http import HttpRequest, HttpResponse
from django.core.exceptions import ValidationError
from django.utils.translation import ugettext as _
from typing import Text
from zerver.models import UserProfile
from zerver.lib.request import JsonableError
from zerver.lib.request import JsonableError, REQ, has_request_variables
from zerver.lib.response import json_success, json_error
from zerver.lib.actions import check_add_realm_emoji, do_remove_realm_emoji
@ -14,6 +17,12 @@ def check_emoji_admin(user_profile):
if user_profile.realm.add_emoji_by_admins_only and not user_profile.is_realm_admin:
raise JsonableError(_("Must be a realm administrator"))
def check_valid_emoji_name(emoji_name):
# type: (Text) -> None
if re.match('^[0-9a-zA-Z.\-_]+(?<![.\-_])$', emoji_name):
return
raise JsonableError(_("Invalid characters in emoji name"))
def list_emoji(request, user_profile):
# type: (HttpRequest, UserProfile) -> HttpResponse
@ -21,13 +30,13 @@ def list_emoji(request, user_profile):
# emoji is public.
return json_success({'emoji': user_profile.realm.get_emoji()})
def upload_emoji(request, user_profile):
# type: (HttpRequest, UserProfile) -> HttpResponse
@has_request_variables
def upload_emoji(request, user_profile, emoji_name=None, url=REQ()):
# type: (HttpRequest, UserProfile, Text, Text) -> HttpResponse
check_valid_emoji_name(emoji_name)
check_emoji_admin(user_profile)
emoji_name = request.POST.get('name', None)
emoji_url = request.POST.get('url', None)
try:
check_add_realm_emoji(user_profile.realm, emoji_name, emoji_url, author=user_profile)
check_add_realm_emoji(user_profile.realm, emoji_name, url, author=user_profile)
except ValidationError as e:
return json_error(e.messages[0])
return json_success()

View File

@ -173,10 +173,10 @@ v1_api_and_json_patterns = [
# realm/emoji -> zerver.views.realm_emoji
url(r'^realm/emoji$', rest_dispatch,
{'GET': 'zerver.views.realm_emoji.list_emoji',
'PUT': 'zerver.views.realm_emoji.upload_emoji'}),
url(r'^realm/emoji/(?P<emoji_name>[0-9a-zA-Z.\-_]+(?<![.\-_]))$', rest_dispatch,
{'DELETE': 'zerver.views.realm_emoji.delete_emoji'}),
{'GET': 'zerver.views.realm_emoji.list_emoji'}),
url(r'^realm/emoji/(?P<emoji_name>.*)$', rest_dispatch,
{'PUT': 'zerver.views.realm_emoji.upload_emoji',
'DELETE': 'zerver.views.realm_emoji.delete_emoji'}),
# realm/filters -> zerver.views.realm_filters
url(r'^realm/filters$', rest_dispatch,