From 662459b9e188d31eb45f3e0acb017ecdf6e2fcd0 Mon Sep 17 00:00:00 2001 From: Steve Howell Date: Sun, 2 Feb 2014 09:30:33 -0500 Subject: [PATCH] Fix do_rename_stream to update the stream's email address. This change also makes it so that the test_rename_stream() test exercises the code path. We need to subscribe the user to the stream in order to generate events. (imported from commit 77f965efbf5a766eb8de23486e303fa135b2e638) --- zerver/lib/actions.py | 30 +++++++++++++++++++++--------- zerver/test_events.py | 3 ++- zerver/test_subs.py | 4 ++-- 3 files changed, 25 insertions(+), 12 deletions(-) diff --git a/zerver/lib/actions.py b/zerver/lib/actions.py index 3f95da5e08..dfb745fd7a 100644 --- a/zerver/lib/actions.py +++ b/zerver/lib/actions.py @@ -1288,21 +1288,33 @@ def do_rename_stream(realm, old_name, new_name, log=True): to_dict_cache_key_id(message.id, True) for message in messages) cache_delete_many( to_dict_cache_key_id(message.id, False) for message in messages) + new_email = encode_email_address(stream) # We will tell our users to essentially # update stream.name = new_name where name = old_name - event = dict( - op="update", - type="stream", - property="name", - value=new_name, - name=old_name - ) - send_event(event, stream_user_ids(stream)) + # and update stream.email = new_email where name = old_name. + # We could optimize this by trying to send one message, but the + # client code really wants one property update at a time, and + # updating stream names is a pretty infrequent operation. + # More importantly, we want to key these updates by id, not name, + # since id is the immutable primary key, and obviously name is not. + data_updates = [ + ['email_address', new_email], + ['name', new_name], + ] + for property, value in data_updates: + event = dict( + op="update", + type="stream", + property=property, + value=value, + name=old_name + ) + send_event(event, stream_user_ids(stream)) # Even though the token doesn't change, the web client needs to update the # email forwarding address to display the correctly-escaped new name. - return {"email_address": encode_email_address(stream)} + return {"email_address": new_email} def do_change_stream_description(realm, stream_name, new_description): stream = get_stream(stream_name, realm) diff --git a/zerver/test_events.py b/zerver/test_events.py index 53493a1c9b..cb55b90628 100644 --- a/zerver/test_events.py +++ b/zerver/test_events.py @@ -237,7 +237,8 @@ class EventsRegisterTest(AuthedTestCase): def test_rename_stream(self): realm = get_realm('zulip.com') stream, _ = create_stream_if_needed(realm, 'old_name') - new_name = 'stream with a brand new name' + new_name = u'stream with a brand new name' + self.subscribe_to_stream(self.user_profile.email, stream.name) self.do_test(lambda: do_rename_stream(realm, stream.name, new_name)) def test_subscribe_events(self): diff --git a/zerver/test_subs.py b/zerver/test_subs.py index d67c0b4066..0b57aa58e0 100644 --- a/zerver/test_subs.py +++ b/zerver/test_subs.py @@ -118,7 +118,7 @@ class StreamAdminTest(AuthedTestCase): result = self.client.post('/json/rename_stream?old_name=stream_name1&new_name=stream_name2') self.assert_json_success(result) - event = events[0]['event'] + event = events[1]['event'] self.assertEqual(event, dict( op='update', type='stream', @@ -126,7 +126,7 @@ class StreamAdminTest(AuthedTestCase): value='stream_name2', name='stream_name1' )) - users = events[0]['users'] + users = events[1]['users'] self.assertEqual(users, [user_profile.id]) stream_name1_exists = Stream.objects.filter(