From 3df87d0901acf18f6c11f8de9bdff07f253f1d8f Mon Sep 17 00:00:00 2001 From: sahil839 Date: Thu, 7 Jan 2021 01:07:38 +0530 Subject: [PATCH] stream: Fix error handling in access_stream_for_send_message. According to tests we should not allow bot without owners to post in streams with STREAM_POST_POLICY_RESTRICT_NEW_MEMBERS. But the code does not handle this and the related test passes and raises error for case of bots without owner because the bot is itself a new member. This commit fixes this by adding a condition to check if there is no bot owner and then raise error if there is no owner. --- zerver/lib/streams.py | 2 +- zerver/tests/test_message_send.py | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/zerver/lib/streams.py b/zerver/lib/streams.py index eee28ac1ec..fd984be121 100644 --- a/zerver/lib/streams.py +++ b/zerver/lib/streams.py @@ -205,7 +205,7 @@ def access_stream_for_send_message( elif stream.stream_post_policy != Stream.STREAM_POST_POLICY_EVERYONE and sender.is_guest: raise JsonableError(_("Guests cannot send to this stream.")) elif stream.stream_post_policy == Stream.STREAM_POST_POLICY_RESTRICT_NEW_MEMBERS: - if sender.is_bot and (sender.bot_owner is not None and sender.bot_owner.is_new_member): + if sender.is_bot and (sender.bot_owner is None or sender.bot_owner.is_new_member): raise JsonableError(_("New members cannot send to this stream.")) elif sender.is_new_member: raise JsonableError(_("New members cannot send to this stream.")) diff --git a/zerver/tests/test_message_send.py b/zerver/tests/test_message_send.py index 8b42c95ceb..40b1ce6a9d 100644 --- a/zerver/tests/test_message_send.py +++ b/zerver/tests/test_message_send.py @@ -279,7 +279,8 @@ class MessagePOSTTest(ZulipTestCase): non_admin_owned_bot, stream_name, "New members cannot send to this stream." ) - # Bots without owner (except cross realm bot) cannot send to announcement only stream + # Bots without owner (except cross realm bot) cannot send to STREAM_POST_POLICY_ADMINS_ONLY and + # STREAM_POST_POLICY_RESTRICT_NEW_MEMBERS streams bot_without_owner = do_create_user( email="free-bot@zulip.testserver", password="",