Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
streams: Grant guest users access to web-public streams.
Clara Dantase22e12fe604b
diff --git a/zerver/lib/streams.py b/zerver/lib/streams.pyindex c4a80bf7044..5fa5d12e61e 100644--- a/zerver/lib/streams.py+++ b/zerver/lib/streams.py@@ -163,6 +163,10 @@ def access_stream_for_send_message(sender: UserProfile,elif sender.is_new_member:raise JsonableError(_("New members cannot send to this stream."))+ if stream.is_web_public:+ # Even guest users can write to web-public streams.+ return+if not (stream.invite_only or sender.is_guest):# This is a public stream and sender is not a guest userreturn@@ -242,6 +246,10 @@ def access_stream_common(user_profile: UserProfile, stream: Stream,except Subscription.DoesNotExist:sub = None+ # Any realm user, even guests, can access web_public streams.+ if stream.is_web_public:+ return (recipient, sub)+# If the stream is in your realm and public, you can access it.if stream.is_public() and not user_profile.is_guest:return (recipient, sub)@@ -353,6 +361,9 @@ def can_access_stream_history(user_profile: UserProfile, stream: Stream) -> boolaccess_stream is being called elsewhere to confirm that the usercan actually see this stream."""+ if stream.is_web_public:+ return True+if stream.is_history_realm_public() and not user_profile.is_guest:return Truediff --git a/zerver/tests/test_message_send.py b/zerver/tests/test_message_send.pyindex 92f1596e75f..db514ed6a04 100644--- a/zerver/tests/test_message_send.py+++ b/zerver/tests/test_message_send.py@@ -1889,6 +1889,25 @@ class CheckMessageTest(ZulipTestCase):ret = check_message(sender, client, addressee, message_content)self.assertEqual(ret['message'].sender.id, sender.id)+ def test_guest_user_can_send_message(self) -> None:+ # Guest users can write to web_public streams.+ sender = self.example_user("polonius")+ client = make_client(name="test suite")+ rome_stream = get_stream("Rome", sender.realm)++ is_sender_subscriber = Subscription.objects.filter(+ user_profile=sender,+ recipient__type_id=rome_stream.id,+ ).exists()+ self.assertFalse(is_sender_subscriber)+ self.assertTrue(rome_stream.is_web_public)++ topic_name = 'issue'+ message_content = 'whatever'+ addressee = Addressee.for_stream_name(rome_stream.name, topic_name)+ ret = check_message(sender, client, addressee, message_content)+ self.assertEqual(ret['message'].sender.id, sender.id)+def test_bot_pm_feature(self) -> None:"""We send a PM to a bot's owner if their bot sends a message toan unsubscribed stream"""diff --git a/zerver/tests/test_subs.py b/zerver/tests/test_subs.pyindex d4eab5418cb..4bfd8e1ec13 100644--- a/zerver/tests/test_subs.py+++ b/zerver/tests/test_subs.py@@ -53,6 +53,7 @@ from zerver.lib.stream_subscription import (from zerver.lib.streams import (access_stream_by_id,access_stream_by_name,+ can_access_stream_history,create_streams_if_needed,filter_stream_authorization,list_to_streams,@@ -199,6 +200,11 @@ class TestCreateStreams(ZulipTestCase):"name": "publicstream","description": "Public stream with public history",},+ {+ "name": "webpublicstream",+ "description": "Web public stream",+ "is_web_public": True+ },{"name": "privatestream","description": "Private stream with non-public history",@@ -220,11 +226,13 @@ class TestCreateStreams(ZulipTestCase):created, existing = create_streams_if_needed(realm, stream_dicts)- self.assertEqual(len(created), 4)+ self.assertEqual(len(created), 5)self.assertEqual(len(existing), 0)for stream in created:if stream.name == 'publicstream':self.assertTrue(stream.history_public_to_subscribers)+ if stream.name == 'webpublicstream':+ self.assertTrue(stream.history_public_to_subscribers)if stream.name == 'privatestream':self.assertFalse(stream.history_public_to_subscribers)if stream.name == 'privatewithhistory':@@ -4289,6 +4297,14 @@ class AccessStreamTest(ZulipTestCase):self.assertEqual(sub_ret.recipient, rec_ret)self.assertEqual(sub_ret.recipient.type_id, stream.id)+ stream_name = "web_public_stream"+ stream = self.make_stream(stream_name, guest_user_profile.realm, is_web_public=True)+ # Guest users have access to web public streams even if they aren't subscribed.+ (stream_ret, rec_ret, sub_ret) = access_stream_by_id(guest_user_profile, stream.id)+ self.assertTrue(can_access_stream_history(guest_user_profile, stream))+ assert sub_ret is None+ self.assertEqual(stream.id, stream_ret.id)+class StreamTrafficTest(ZulipTestCase):def test_average_weekly_stream_traffic_calculation(self) -> None:# No traffic data for the stream