add_default_stream: use access_stream_by_name.

This commit is contained in:
Tim Abbott 2017-01-29 19:23:08 -08:00
parent de3f539f58
commit 19eaa92059
4 changed files with 13 additions and 10 deletions

View File

@ -2382,12 +2382,11 @@ def notify_default_streams(realm):
)
send_event(event, active_user_ids(realm))
def do_add_default_stream(realm, stream_name):
# type: (Realm, Text) -> None
stream, _ = create_stream_if_needed(realm, stream_name)
if not DefaultStream.objects.filter(realm=realm, stream=stream).exists():
DefaultStream.objects.create(realm=realm, stream=stream)
notify_default_streams(realm)
def do_add_default_stream(stream):
# type: (Stream) -> None
if not DefaultStream.objects.filter(realm=stream.realm, stream=stream).exists():
DefaultStream.objects.create(realm=stream.realm, stream=stream)
notify_default_streams(stream.realm)
def do_remove_default_stream(realm, stream_name):
# type: (Realm, Text) -> None

View File

@ -442,7 +442,8 @@ class EventsRegisterTest(ZulipTestCase):
]))),
])
events = self.do_test(lambda: do_add_default_stream(self.user_profile.realm, "Scotland"))
stream = get_stream("Scotland", self.user_profile.realm)
events = self.do_test(lambda: do_add_default_stream(stream))
error = default_streams_checker('events[0]', events[0])
self.assert_on_error(error)

View File

@ -704,13 +704,14 @@ class DefaultStreamTest(ZulipTestCase):
def test_add_and_remove_default_stream(self):
# type: () -> None
realm = get_realm("zulip")
(stream, _) = create_stream_if_needed(realm, "Added Stream")
orig_stream_names = self.get_default_stream_names(realm)
do_add_default_stream(realm, 'Added Stream')
do_add_default_stream(stream)
new_stream_names = self.get_default_stream_names(realm)
added_stream_names = new_stream_names - orig_stream_names
self.assertEqual(added_stream_names, set(['Added Stream']))
# idempotentcy--2nd call to add_default_stream should be a noop
do_add_default_stream(realm, 'Added Stream')
do_add_default_stream(stream)
self.assertEqual(self.get_default_stream_names(realm), new_stream_names)
# start removing
@ -726,6 +727,7 @@ class DefaultStreamTest(ZulipTestCase):
user_profile = get_user_profile_by_email('hamlet@zulip.com')
do_change_is_admin(user_profile, True)
stream_name = 'stream ADDED via api'
(stream, _) = create_stream_if_needed(user_profile.realm, stream_name)
result = self.client_post('/json/default_streams', dict(stream_name=stream_name))
self.assert_json_success(result)
self.assertTrue(stream_name in self.get_default_stream_names(user_profile.realm))

View File

@ -73,7 +73,8 @@ def deactivate_stream_backend(request, user_profile, stream_id):
@has_request_variables
def add_default_stream(request, user_profile, stream_name=REQ()):
# type: (HttpRequest, UserProfile, Text) -> HttpResponse
do_add_default_stream(user_profile.realm, stream_name)
(stream, recipient, sub) = access_stream_by_name(user_profile, stream_name)
do_add_default_stream(stream)
return json_success()
@require_realm_admin