Fix subscribing to existing streams when can_create_streams=False.

Previously, a user with can_create_streams=False would be incorrectly
unable to subscribe to streams, whether the streams previously existed
or not.
This commit is contained in:
Tim Abbott
2016-05-18 18:46:33 -07:00
parent c4254497b2
commit e781136132
2 changed files with 16 additions and 11 deletions

View File

@@ -841,14 +841,19 @@ class SubscriptionAPITest(AuthedTestCase):
"Stream name (%s) too long." % (long_stream_name,))
def test_user_settings_for_adding_streams(self):
with stub(UserProfile, 'can_create_streams', lambda self: True):
result = self.common_subscribe_to_streams(self.test_email, ['stream1'])
self.assert_json_success(result)
with stub(UserProfile, 'can_create_streams', lambda self: False):
result = self.common_subscribe_to_streams(self.test_email, ['stream1'])
self.assert_json_error(result, 'User cannot create streams.')
with stub(UserProfile, 'can_create_streams', lambda self: True):
result = self.common_subscribe_to_streams(self.test_email, ['stream2'])
self.assert_json_success(result)
# User should still be able to subscribe to an existing stream
with stub(UserProfile, 'can_create_streams', lambda self: False):
result = self.common_subscribe_to_streams(self.test_email, ['stream2'])
self.assert_json_success(result)
def test_subscriptions_add_invalid_stream(self):
"""
Calling POST /json/users/me/subscriptions on a stream whose name is invalid (as

View File

@@ -66,7 +66,12 @@ def list_to_streams(streams_raw, user_profile, autocreate=False, invite_only=Fal
rejects.append(stream_name)
else:
existing_streams.append(stream)
if autocreate:
if rejects:
if not user_profile.can_create_streams():
raise JsonableError('User cannot create streams.')
elif not autocreate:
raise JsonableError("Stream(s) (%s) do not exist" % ", ".join(rejects))
for stream_name in rejects:
stream, created = create_stream_if_needed(user_profile.realm,
stream_name,
@@ -75,8 +80,6 @@ def list_to_streams(streams_raw, user_profile, autocreate=False, invite_only=Fal
created_streams.append(stream)
else:
existing_streams.append(stream)
elif rejects:
raise JsonableError("Stream(s) (%s) do not exist" % ", ".join(rejects))
return existing_streams, created_streams
@@ -256,10 +259,6 @@ def add_subscriptions_backend(request, user_profile,
announce = REQ(validator=check_bool, default=False),
principals = REQ(validator=check_list(check_string), default=None),
authorization_errors_fatal = REQ(validator=check_bool, default=True)):
if not user_profile.can_create_streams():
return json_error('User cannot create streams.')
stream_names = []
for stream in streams_raw:
stream_name = stream["name"].strip()
@@ -269,6 +268,7 @@ def add_subscriptions_backend(request, user_profile,
return json_error("Invalid stream name (%s)." % (stream_name,))
stream_names.append(stream_name)
# Enforcement of can_create_streams policy is inside list_to_streams.
existing_streams, created_streams = \
list_to_streams(stream_names, user_profile, autocreate=True, invite_only=invite_only)
authorized_streams, unauthorized_streams = \