Replace legacy endpoint for renaming a stream and add tests.

This moves the logic for renaming a stream to the REST API
update_stream_backend method, eliminating the legacy API endpoint for
doing so.

It also adds a nice test suite covering international stream names.
This commit is contained in:
baali 2016-09-28 12:37:09 +05:30 committed by Tim Abbott
parent 48bec80c61
commit 142dce2cd4
3 changed files with 51 additions and 23 deletions

View File

@ -189,7 +189,8 @@ class StreamAdminTest(ZulipTestCase):
events = [] # type: List[Dict[str, Any]]
with tornado_redirected_to_list(events):
result = self.client_post('/json/rename_stream?old_name=stream_name1&new_name=stream_name2')
result = self.client_patch('/json/streams/stream_name1',
{'new_name': ujson.dumps('stream_name2')})
self.assert_json_success(result)
event = events[1]['event']
@ -203,17 +204,50 @@ class StreamAdminTest(ZulipTestCase):
users = events[1]['users']
self.assertEqual(users, [user_profile.id])
stream_name1_exists = Stream.objects.filter(
name='stream_name1',
realm=realm,
).exists()
stream_name1_exists = get_stream('stream_name1', realm)
self.assertFalse(stream_name1_exists)
stream_name2_exists = Stream.objects.filter(
name='stream_name2',
realm=realm,
).exists()
stream_name2_exists = get_stream('stream_name2', realm)
self.assertTrue(stream_name2_exists)
# Test case to handle unicode stream name change
# *NOTE: Here Encoding is needed when Unicode string is passed as an argument*
with tornado_redirected_to_list(events):
result = self.client_patch('/json/streams/stream_name2',
{'new_name': ujson.dumps(u'नया नाम'.encode('utf-8'))})
self.assert_json_success(result)
# While querying, system can handle unicode strings.
stream_name_uni_exists = get_stream(u'नया नाम', realm)
self.assertTrue(stream_name_uni_exists)
# Test case to handle changing of unicode stream name to newer name
# NOTE: Unicode string being part of URL is handled cleanly
# by client_patch call, encoding of URL is not needed.
with tornado_redirected_to_list(events):
result = self.client_patch('/json/streams/नया नाम',
{'new_name': ujson.dumps(u'नाम में क्या रक्खा हे'.encode('utf-8'))})
self.assert_json_success(result)
# While querying, system can handle unicode strings.
stream_name_old_uni_exists = get_stream(u'नया नाम', realm)
self.assertFalse(stream_name_old_uni_exists)
stream_name_new_uni_exists = get_stream(u'नाम में क्या रक्खा हे', realm)
self.assertTrue(stream_name_new_uni_exists)
# Test case to change name from one language to other.
with tornado_redirected_to_list(events):
result = self.client_patch('/json/streams/नाम में क्या रक्खा हे',
{'new_name': ujson.dumps(u'français'.encode('utf-8'))})
self.assert_json_success(result)
stream_name_fr_exists = get_stream(u'français', realm)
self.assertTrue(stream_name_fr_exists)
# Test case to change name to mixed language name.
with tornado_redirected_to_list(events):
result = self.client_patch('/json/streams/français',
{'new_name': ujson.dumps(u'français name'.encode('utf-8'))})
self.assert_json_success(result)
stream_name_mixed_exists = get_stream(u'français name', realm)
self.assertTrue(stream_name_mixed_exists)
def test_rename_stream_requires_realm_admin(self):
# type: () -> None
email = 'hamlet@zulip.com'
@ -222,7 +256,8 @@ class StreamAdminTest(ZulipTestCase):
realm = user_profile.realm
stream, _ = create_stream_if_needed(realm, 'stream_name1')
result = self.client_post('/json/rename_stream?old_name=stream_name1&new_name=stream_name2')
result = self.client_patch('/json/streams/stream_name1',
{'new_name': ujson.dumps('stream_name2')})
self.assert_json_error(result, 'Must be a realm administrator')
def test_change_stream_description(self):

View File

@ -148,14 +148,6 @@ def remove_default_stream(request, user_profile, stream_name=REQ()):
do_remove_default_stream(user_profile.realm, stream_name)
return json_success()
@authenticated_json_post_view
@require_realm_admin
@has_request_variables
def json_rename_stream(request, user_profile, old_name=REQ(), new_name=REQ()):
# type: (HttpRequest, UserProfile, text_type, text_type) -> HttpResponse
do_rename_stream(user_profile.realm, old_name, new_name)
return json_success()
@authenticated_json_post_view
@require_realm_admin
@has_request_variables
@ -175,11 +167,13 @@ def json_make_stream_private(request, user_profile, stream_name=REQ()):
@require_realm_admin
@has_request_variables
def update_stream_backend(request, user_profile, stream_name,
description=REQ(validator=check_string, default=None)):
# type: (HttpRequest, UserProfile, text_type, Optional[text_type]) -> HttpResponse
description=REQ(validator=check_string, default=None),
new_name=REQ(validator=check_string, default=None)):
# type: (HttpRequest, UserProfile, text_type, Optional[text_type], Optional[text_type]) -> HttpResponse
if description is not None:
do_change_stream_description(user_profile.realm, stream_name, description)
do_change_stream_description(user_profile.realm, stream_name, description)
if stream_name is not None and new_name is not None:
do_rename_stream(user_profile.realm, stream_name, new_name)
return json_success({})
def list_subscriptions_backend(request, user_profile):

View File

@ -4,7 +4,6 @@ from django.conf.urls import url
legacy_urls = [
# These are json format views used by the web client. They require a logged in browser.
url(r'^json/rename_stream$', 'zerver.views.streams.json_rename_stream'),
url(r'^json/make_stream_public$', 'zerver.views.streams.json_make_stream_public'),
url(r'^json/make_stream_private$', 'zerver.views.streams.json_make_stream_private'),
url(r'^json/invite_users$', 'zerver.views.json_invite_users'),