Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

stream: Use `can_create_topic_group` setting while sending message.

Saubhagya Patela809c543112a
diff --git a/zerver/actions/message_edit.py b/zerver/actions/message_edit.py
index 53397d51c63..d5ccb83e926 100644
--- a/zerver/actions/message_edit.py
+++ b/zerver/actions/message_edit.py
@@ -59,6 +59,7 @@ from zerver.lib.streams import (
can_edit_topic,
can_move_messages_out_of_channel,
can_resolve_topics,
+ check_for_can_create_topic_group_violation,
check_stream_access_based_on_can_send_message_group,
get_stream_topics_policy,
notify_stream_is_recently_active_update,
@@ -97,7 +98,11 @@ from zerver.models import (
UserProfile,
UserTopic,
)
-from zerver.models.streams import StreamTopicsPolicyEnum, get_stream_by_id_in_realm
+from zerver.models.streams import (
+ StreamTopicsPolicyEnum,
+ get_stream_by_id_for_sending_message,
+ get_stream_by_id_in_realm,
+)
from zerver.models.users import ResolvedTopicNoticeAutoReadPolicyEnum, get_system_bot
from zerver.tornado.django_api import send_event_on_commit
@@ -1487,7 +1492,10 @@ def build_message_edit_request(
)
orig_stream_id = message.recipient.type_id
- orig_stream = get_stream_by_id_in_realm(orig_stream_id, message.realm)
+ # We call get_stream_by_id_for_sending_message instead of
+ # get_stream_by_id_in_realm here so that "can_create_topic_group" is
+ # fetched using select_related and we can save a couple of DB queries.
+ orig_stream = get_stream_by_id_for_sending_message(orig_stream_id, message.realm)
is_stream_edited = False
target_stream = orig_stream
@@ -1685,6 +1693,17 @@ def check_update_message(
message, user_profile, topic_name, stream_id
)
+ if (
+ message_edit_request.is_message_moved
+ and not message_edit_request.topic_resolved
+ and not message_edit_request.topic_unresolved
+ ):
+ check_for_can_create_topic_group_violation(
+ user_profile,
+ message_edit_request.target_stream,
+ message_edit_request.target_topic_name,
+ )
+
updated_message_result = do_update_message(
user_profile,
message,
diff --git a/zerver/actions/message_send.py b/zerver/actions/message_send.py
index 3c9181e9fda..fe590cdd2ac 100644
--- a/zerver/actions/message_send.py
+++ b/zerver/actions/message_send.py
@@ -70,6 +70,7 @@ from zerver.lib.stream_topic import StreamTopicTarget
from zerver.lib.streams import (
access_stream_for_send_message,
channel_events_topic_name,
+ check_for_can_create_topic_group_violation,
ensure_stream,
get_stream_topics_policy,
notify_stream_is_recently_active_update,
@@ -1796,6 +1797,10 @@ def check_message(
# else can sneak past the access check.
assert sender.bot_type == sender.OUTGOING_WEBHOOK_BOT
+ check_for_can_create_topic_group_violation(
+ user_profile=sender, stream=stream, topic_name=topic_name
+ )
+
topics_policy = get_stream_topics_policy(realm, stream)
empty_topic_display_name = get_topic_display_name("", sender.default_language)
if topics_policy == StreamTopicsPolicyEnum.disable_empty_topic.value and topic_name == "":
diff --git a/zerver/lib/streams.py b/zerver/lib/streams.py
index c8b6fa4f0b8..dfe43911456 100644
--- a/zerver/lib/streams.py
+++ b/zerver/lib/streams.py
@@ -26,7 +26,7 @@ from zerver.lib.stream_subscription import (
from zerver.lib.stream_traffic import get_average_weekly_stream_traffic, get_streams_traffic
from zerver.lib.string_validation import check_stream_name
from zerver.lib.timestamp import datetime_to_timestamp
-from zerver.lib.topic import get_topic_display_name
+from zerver.lib.topic import get_topic_display_name, messages_for_topic
from zerver.lib.types import APIStreamDict, UserGroupMembersData
from zerver.lib.user_groups import (
UserGroupMembershipDetails,
@@ -699,6 +699,51 @@ def access_stream_for_send_message(
)
+def check_user_can_create_new_topics(user_profile: UserProfile, stream: Stream) -> None:
+ can_create_topic_group = stream.can_create_topic_group
+ if (
+ hasattr(can_create_topic_group, "named_user_group")
+ and can_create_topic_group.named_user_group.name == SystemGroups.NOBODY
+ ):
+ raise JsonableError(_("You do not have permission to create new topics in this channel."))
+
+ if not user_has_permission_for_group_setting(
+ stream.can_create_topic_group_id,
+ user_profile,
+ Stream.stream_permission_group_settings["can_create_topic_group"],
+ direct_member_only=False,
+ ):
+ raise JsonableError(_("You do not have permission to create new topics in this channel."))
+
+
+def check_for_can_create_topic_group_violation(
+ user_profile: UserProfile, stream: Stream, topic_name: str
+) -> None:
+ if is_cross_realm_bot_email(user_profile.delivery_email):
+ return
+
+ can_create_topic_group = stream.can_create_topic_group
+ if (
+ hasattr(can_create_topic_group, "named_user_group")
+ and can_create_topic_group.named_user_group.name == SystemGroups.EVERYONE
+ ):
+ return
+
+ assert stream.recipient_id is not None
+ topic_exists = messages_for_topic(
+ realm_id=stream.realm_id, stream_recipient_id=stream.recipient_id, topic_name=topic_name
+ ).exists()
+
+ if not topic_exists:
+ try:
+ check_user_can_create_new_topics(user_profile, stream)
+ except JsonableError as e:
+ if user_profile.is_bot and user_profile.bot_owner is not None:
+ check_user_can_create_new_topics(user_profile.bot_owner, stream)
+ else:
+ raise JsonableError(e.msg)
+
+
def check_for_exactly_one_stream_arg(stream_id: int | None, stream: str | None) -> None:
if stream_id is None and stream is None:
# Uses the same translated string as RequestVariableMissingError
@@ -976,7 +1021,7 @@ def access_stream_by_id_for_message(
) -> tuple[Stream, Subscription | None]:
"""
Variant of access_stream_by_id that uses get_stream_by_id_for_sending_message
- to ensure we do a select_related("can_send_message_group").
+ to ensure we do a select_related("can_send_message_group", "can_create_topic_group").
"""
error = _("Invalid channel ID")
try:
diff --git a/zerver/models/streams.py b/zerver/models/streams.py
index 7cea97af01a..128cec31e8f 100644
--- a/zerver/models/streams.py
+++ b/zerver/models/streams.py
@@ -334,13 +334,21 @@ def get_stream_by_id_in_realm(stream_id: int, realm: Realm) -> Stream:
def get_stream_by_name_for_sending_message(stream_name: str, realm: Realm) -> Stream:
return Stream.objects.select_related(
- "can_send_message_group", "can_send_message_group__named_user_group"
+ "can_send_message_group",
+ "can_send_message_group__named_user_group",
+ "can_create_topic_group",
+ "can_create_topic_group__named_user_group",
).get(name__iexact=stream_name.strip(), realm_id=realm.id)
def get_stream_by_id_for_sending_message(stream_id: int, realm: Realm) -> Stream:
return Stream.objects.select_related(
- "realm", "recipient", "can_send_message_group", "can_send_message_group__named_user_group"
+ "realm",
+ "recipient",
+ "can_send_message_group",
+ "can_send_message_group__named_user_group",
+ "can_create_topic_group",
+ "can_create_topic_group__named_user_group",
).get(id=stream_id, realm=realm)
diff --git a/zerver/openapi/zulip.yaml b/zerver/openapi/zulip.yaml
index e4754d3fcaa..b7eb6eb6d25 100644
--- a/zerver/openapi/zulip.yaml
+++ b/zerver/openapi/zulip.yaml
@@ -22782,6 +22782,9 @@ paths:
Note that a user must have metadata access to a channel and permission
to administer the channel in order to modify this setting.
+ Note that using this permission to send a message to a new topic requires
+ also having permission to create new topics in the channel.
+
**Changes**: New in Zulip 10.0 (feature level 333). Previously
`stream_post_policy` field used to control the permission to
post in the channel.
@@ -28725,6 +28728,9 @@ components:
Note that a user must have metadata access to a channel and permission
to administer the channel in order to modify this setting.
+ Note that using this permission to send a message to a new topic requires
+ also having permission to create new topics in the channel.
+
**Changes**: New in Zulip 10.0 (feature level 333). Previously
`stream_post_policy` field used to control the permission to
post in the channel.
diff --git a/zerver/tests/test_message_move_stream.py b/zerver/tests/test_message_move_stream.py
index 23c6ba1bc4c..8d62c0efa86 100644
--- a/zerver/tests/test_message_move_stream.py
+++ b/zerver/tests/test_message_move_stream.py
@@ -1406,6 +1406,158 @@ class MessageMoveStreamTest(ZulipTestCase):
messages = get_topic_messages(user_profile, new_stream, "test")
self.assert_length(messages, 4)
+ def test_move_message_to_stream_based_on_can_create_create_topic_group(self) -> None:
+ desdemona = self.example_user("desdemona")
+ iago = self.example_user("iago")
+ aaron = self.example_user("aaron")
+ cordelia = self.example_user("cordelia")
+ realm = iago.realm
+
+ error_msg = "You do not have permission to create new topics in this channel."
+
+ new_stream = self.subscribe(iago, "new_stream")
+ self.send_stream_message(iago, "new_stream", topic_name="existing topic")
+
+ def check_move_message_to_stream(
+ user: UserProfile,
+ expect_fail: bool = False,
+ topic_name: str = "test",
+ ) -> None:
+ (_, _, _, msg_id, _) = self.prepare_move_topics(
+ "iago", "old_stream", "new_stream", "test"
+ )
+ result = self.api_patch(
+ user,
+ "/api/v1/messages/" + str(msg_id),
+ {
+ "stream_id": new_stream.id,
+ "propagate_mode": "change_all",
+ "topic": topic_name,
+ },
+ )
+
+ if expect_fail:
+ self.assert_json_error(result, error_msg)
+ return
+
+ self.assert_json_success(result)
+
+ nobody_group = NamedUserGroup.objects.get(
+ name=SystemGroups.NOBODY, realm=realm, is_system_group=True
+ )
+ do_change_stream_group_based_setting(
+ new_stream, "can_create_topic_group", nobody_group, acting_user=desdemona
+ )
+ # Moving message to a new topic is not allowed.
+ check_move_message_to_stream(desdemona, expect_fail=True)
+ check_move_message_to_stream(iago, expect_fail=True)
+
+ # Moving message to an existing topic is allowed.
+ check_move_message_to_stream(desdemona, topic_name="existing topic")
+
+ owners_group = NamedUserGroup.objects.get(
+ name=SystemGroups.OWNERS, realm=realm, is_system_group=True
+ )
+ do_change_stream_group_based_setting(
+ new_stream, "can_create_topic_group", owners_group, acting_user=desdemona
+ )
+
+ # Moving message to a new topic is allowed to owners only.
+ check_move_message_to_stream(iago, expect_fail=True)
+ check_move_message_to_stream(desdemona)
+ # Moving message to an existing topic is allowed.
+ check_move_message_to_stream(iago, topic_name="existing topic")
+
+ anonymous_group = UserGroupMembersData(
+ direct_members=[aaron.id, iago.id],
+ direct_subgroups=[],
+ )
+ do_change_stream_group_based_setting(
+ new_stream, "can_create_topic_group", anonymous_group, acting_user=desdemona
+ )
+
+ # Moving message to a new topic is not allowed, except for anonymous group members.
+ check_move_message_to_stream(desdemona, expect_fail=True)
+ check_move_message_to_stream(cordelia, expect_fail=True)
+ check_move_message_to_stream(iago)
+ check_move_message_to_stream(aaron)
+ # Moving message to an existing topic is allowed.
+ check_move_message_to_stream(desdemona, topic_name="existing topic")
+ check_move_message_to_stream(cordelia, topic_name="existing topic")
+
+ def test_move_and_resolve_topic_without_permission_to_create_new_topics(self) -> None:
+ iago = self.example_user("iago")
+ shiva = self.example_user("shiva")
+ realm = iago.realm
+
+ new_stream = self.subscribe(iago, "new_stream")
+ self.subscribe(shiva, "new_stream")
+ self.subscribe(shiva, "old_stream")
+ self.send_stream_message(iago, "new_stream", topic_name="existing_topic")
+
+ admins_group = NamedUserGroup.objects.get(
+ name=SystemGroups.ADMINISTRATORS, realm_for_sharding=realm, is_system_group=True
+ )
+ moderators_group = NamedUserGroup.objects.get(
+ name=SystemGroups.MODERATORS, realm_for_sharding=realm, is_system_group=True
+ )
+
+ do_change_stream_group_based_setting(
+ new_stream, "can_create_topic_group", admins_group, acting_user=iago
+ )
+ do_change_stream_group_based_setting(
+ new_stream, "can_resolve_topics_group", moderators_group, acting_user=iago
+ )
+
+ (_, _, _, msg_id, _) = self.prepare_move_topics("iago", "old_stream", "new_stream", "test")
+
+ # Test just changing to a new topic not existing in the new stream fails.
+ topic_name = "test 2"
+ result = self.api_patch(
+ shiva,
+ "/api/v1/messages/" + str(msg_id),
+ {
+ "stream_id": new_stream.id,
+ "propagate_mode": "change_all",
+ "topic": topic_name,
+ },
+ )
+ self.assert_json_error(
+ result, "You do not have permission to create new topics in this channel."
+ )
+
+ # Test changing anything other than adding the resolved topic prefix fails.
+ topic_name = RESOLVED_TOPIC_PREFIX + "existing_topic 2"
+ result = self.api_patch(
+ shiva,
+ "/api/v1/messages/" + str(msg_id),
+ {
+ "stream_id": new_stream.id,
+ "propagate_mode": "change_all",
+ "topic": topic_name,
+ },
+ )
+ self.assert_json_error(
+ result, "You do not have permission to create new topics in this channel."
+ )
+
+ # Test just adding resolved topic prefix to the old topic works even when the original
+ # topic does not exist in new stream.
+ topic_name = RESOLVED_TOPIC_PREFIX + "test"
+ result = self.api_patch(
+ shiva,
+ "/api/v1/messages/" + str(msg_id),
+ {
+ "stream_id": new_stream.id,
+ "propagate_mode": "change_all",
+ "topic": topic_name,
+ },
+ )
+ self.assert_json_success(result)
+ msg = Message.objects.get(id=msg_id)
+ self.assertEqual(msg.topic_name(), topic_name)
+ self.assertEqual(msg.recipient_id, new_stream.recipient_id)
+
def test_move_message_to_stream_and_topic(self) -> None:
(user_profile, old_stream, new_stream, msg_id, _msg_id_later) = self.prepare_move_topics(
"iago", "test move stream", "new stream", "test"
diff --git a/zerver/tests/test_message_move_topic.py b/zerver/tests/test_message_move_topic.py
index 0375510ac0b..6d8c905ebd5 100644
--- a/zerver/tests/test_message_move_topic.py
+++ b/zerver/tests/test_message_move_topic.py
@@ -2867,3 +2867,178 @@ class MessageMoveTopicTest(ZulipTestCase):
topic_name="new topic",
expected_error="Only the general chat topic is allowed in this channel.",
)
+
+ def test_can_move_messages_can_create_topic_group(self) -> None:
+ hamlet = self.example_user("hamlet")
+ cordelia = self.example_user("cordelia")
+ iago = self.example_user("iago")
+ realm = hamlet.realm
+
+ hamletcharacters_group = NamedUserGroup.objects.get(name="hamletcharacters", realm=realm)
+ nobody_system_group = NamedUserGroup.objects.get(
+ name=SystemGroups.NOBODY, realm=realm, is_system_group=True
+ )
+
+ error_msg = "You do not have permission to create new topics in this channel."
+
+ stream = get_stream("Denmark", realm)
+ self.send_stream_message(hamlet, stream.name, topic_name="old topic")
+ self.send_stream_message(hamlet, stream.name, topic_name="existing topic")
+
+ def check_move_message(
+ user_profile: UserProfile,
+ stream: Stream,
+ topic_name: str,
+ expect_fail: bool = False,
+ ) -> None:
+ params = {
+ "stream_id": stream.id,
+ "topic": topic_name,
+ }
+
+ self.subscribe(user_profile, stream.name)
+ msg_id = self.send_stream_message(user_profile, stream.name, topic_name="old topic")
+
+ result = self.api_patch(
+ user_profile,
+ "/api/v1/messages/" + str(msg_id),
+ params,
+ )
+ if expect_fail:
+ self.assert_json_error(result, error_msg)
+ return
+
+ self.assert_json_success(result)
+ if topic_name == "new topic":
+ # Delete this topic, as it contains messages and is no longer new.
+ messages = get_topic_messages(user_profile, stream, topic_name)
+ do_delete_messages(user_profile.realm, messages, acting_user=None)
+
+ # When nobody is allowed to move messages to new topics.
+ do_change_stream_group_based_setting(
+ stream, "can_create_topic_group", nobody_system_group, acting_user=iago
+ )
+
+ check_move_message(hamlet, stream, topic_name="new topic", expect_fail=True)
+ check_move_message(iago, stream, topic_name="new topic", expect_fail=True)
+ check_move_message(cordelia, stream, topic_name="new topic", expect_fail=True)
+
+ # However moving messages to existing topics is allowed.
+ check_move_message(hamlet, stream, topic_name="existing topic")
+ check_move_message(iago, stream, topic_name="existing topic")
+ check_move_message(cordelia, stream, topic_name="existing topic")
+
+ # For a user defined group, its members can move messages to a new topic.
+ do_change_stream_group_based_setting(
+ stream,
+ "can_create_topic_group",
+ hamletcharacters_group,
+ acting_user=iago,
+ )
+ check_move_message(iago, stream, topic_name="new topic", expect_fail=True)
+ check_move_message(hamlet, stream, topic_name="new topic")
+ check_move_message(cordelia, stream, topic_name="new topic")
+
+ # However moving messages to existing topics is allowed.
+ check_move_message(iago, stream, topic_name="existing topic")
+ check_move_message(hamlet, stream, topic_name="existing topic")
+ check_move_message(cordelia, stream, topic_name="existing topic")
+
+ # For an anonymous group, its members can move messages to a new topic.
+ anonymous_group = UserGroupMembersData(
+ direct_members=[hamlet.id, iago.id],
+ direct_subgroups=[],
+ )
+ do_change_stream_group_based_setting(
+ stream,
+ "can_create_topic_group",
+ anonymous_group,
+ acting_user=iago,
+ )
+ check_move_message(cordelia, stream, topic_name="new topic", expect_fail=True)
+ check_move_message(hamlet, stream, topic_name="new topic")
+ check_move_message(iago, stream, topic_name="new topic")
+
+ # However moving messages to existing topics is allowed.
+ check_move_message(cordelia, stream, topic_name="existing topic")
+ check_move_message(hamlet, stream, topic_name="existing topic")
+ check_move_message(iago, stream, topic_name="existing topic")
+
+ def test_resolve_and_unresolve_topic_without_permission_to_create_new_topics(self) -> None:
+ iago = self.example_user("iago")
+ shiva = self.example_user("shiva")
+ realm = iago.realm
+ stream = get_stream("Verona", realm)
+
+ admins_group = NamedUserGroup.objects.get(
+ name=SystemGroups.ADMINISTRATORS, realm_for_sharding=realm, is_system_group=True
+ )
+ moderators_group = NamedUserGroup.objects.get(
+ name=SystemGroups.MODERATORS, realm_for_sharding=realm, is_system_group=True
+ )
+
+ do_change_stream_group_based_setting(
+ stream, "can_create_topic_group", admins_group, acting_user=iago
+ )
+ do_change_stream_group_based_setting(
+ stream, "can_resolve_topics_group", moderators_group, acting_user=iago
+ )
+
+ self.subscribe(iago, stream.name)
+ self.subscribe(shiva, stream.name)
+
+ original_topic_name = "topic 1"
+ msg_id = self.send_stream_message(iago, "Verona", topic_name=original_topic_name)
+
+ # Test just changing to another topic fails.
+ params = {
+ "topic": "topic 2",
+ }
+ result = self.api_patch(
+ shiva,
+ "/api/v1/messages/" + str(msg_id),
+ params,
+ )
+ self.assert_json_error(
+ result, "You do not have permission to create new topics in this channel."
+ )
+
+ # Test changing anything other than adding the resolved topic prefix fails.
+ params = {
+ "topic": RESOLVED_TOPIC_PREFIX + "topic 2",
+ }
+ result = self.api_patch(
+ shiva,
+ "/api/v1/messages/" + str(msg_id),
+ params,
+ )
+ self.assert_json_error(
+ result, "You do not have permission to create new topics in this channel."
+ )
+
+ # Test resolving the topic works.
+ resolved_topic_name = RESOLVED_TOPIC_PREFIX + original_topic_name
+ params = {
+ "topic": resolved_topic_name,
+ }
+ result = self.api_patch(
+ shiva,
+ "/api/v1/messages/" + str(msg_id),
+ params,
+ )
+ self.assert_json_success(result)
+ msg = Message.objects.get(id=msg_id)
+ self.assertEqual(msg.topic_name(), resolved_topic_name)
+
+ # Test unresolving the topic works.
+ params = {
+ "topic": original_topic_name,
+ }
+ result = self.api_patch(
+ shiva,
+ "/api/v1/messages/" + str(msg_id),
+ params,
+ )
+ self.assert_json_success(result)
+ msg = Message.objects.get(id=msg_id)
+ self.assertEqual(msg.topic_name(), original_topic_name)
diff --git a/zerver/tests/test_message_send.py b/zerver/tests/test_message_send.py
index 2c6985b7236..2f2b6905379 100644
--- a/zerver/tests/test_message_send.py
+++ b/zerver/tests/test_message_send.py
@@ -11,6 +11,7 @@ from django.utils.timezone import now as timezone_now
from zerver.actions.create_realm import do_create_realm
from zerver.actions.create_user import do_create_user
+from zerver.actions.message_delete import do_delete_messages
from zerver.actions.message_send import (
build_message_send_dict,
check_message,
@@ -79,7 +80,12 @@ from zerver.models.groups import SystemGroups
from zerver.models.realms import RealmTopicsPolicyEnum, get_realm
from zerver.models.recipients import get_direct_message_group, get_or_create_direct_message_group
from zerver.models.streams import StreamTopicsPolicyEnum, get_stream
-from zerver.models.users import get_system_bot, get_user, get_user_by_delivery_email
+from zerver.models.users import (
+ get_system_bot,
+ get_user,
+ get_user_by_delivery_email,
+ is_cross_realm_bot_email,
+)
from zerver.views.message_send import InvalidMirrorInputError
@@ -91,10 +97,14 @@ class MessagePOSTTest(ZulipTestCase):
error_msg: str | None = None,
*,
allow_unsubscribed_sender: bool = False,
+ topic_name: str = "test",
) -> None:
if error_msg is None:
msg_id = self.send_stream_message(
- user, stream_name, allow_unsubscribed_sender=allow_unsubscribed_sender
+ user,
+ stream_name,
+ allow_unsubscribed_sender=allow_unsubscribed_sender,
+ topic_name=topic_name,
)
result = self.api_get(user, "/api/v1/messages/" + str(msg_id))
self.assert_json_success(result)
@@ -532,6 +542,217 @@ class MessagePOSTTest(ZulipTestCase):
allow_unsubscribed_sender=True,
)
+ def test_can_create_topic_group_permission(self) -> None:
+ realm = get_realm("zulip")
+
+ desdemona = self.example_user("desdemona")
+ iago = self.example_user("iago")
+ hamlet = self.example_user("hamlet")
+ cordelia = self.example_user("cordelia")
+ othello = self.example_user("othello")
+ polonius = self.example_user("polonius")
+
+ desdemona_owned_bot = self.create_test_bot(
+ short_name="whatever1",
+ full_name="whatever1",
+ user_profile=desdemona,
+ )
+ iago_owned_bot = self.create_test_bot(
+ short_name="whatever2",
+ full_name="whatever2",
+ user_profile=iago,
+ )
+ cordelia_owned_bot = self.create_test_bot(
+ short_name="whatever3",
+ full_name="whatever3",
+ user_profile=cordelia,
+ )
+ othello_owned_bot = self.create_test_bot(
+ short_name="whatever4",
+ full_name="whatever4",
+ user_profile=othello,
+ )
+ notification_bot = get_system_bot("[email protected]", realm.id)
+
+ bot_without_owner = do_create_user(
+ password="",
+ realm=realm,
+ full_name="freebot",
+ bot_type=UserProfile.DEFAULT_BOT,
+ acting_user=None,
+ )
+ can_create_topic_error_msg = (
+ "You do not have permission to create new topics in this channel."
+ )
+
+ stream_name = "Verona"
+ stream = get_stream(stream_name, realm)
+
+ def check_sending_message_for_can_create_topic_group(
+ sender: UserProfile,
+ topic_name: str = "new_topic",
+ expect_fail: bool = False,
+ ) -> None:
+ if expect_fail:
+ self._send_and_verify_message(
+ sender,
+ stream_name,
+ can_create_topic_error_msg,
+ topic_name=topic_name,
+ )
+ return
+
+ if is_cross_realm_bot_email(sender.delivery_email):
+ internal_send_stream_message(
+ notification_bot, stream, topic_name, "Message from notification bot."
+ )
+ message = self.get_last_message()
+ self.assertEqual(message.content, "Message from notification bot.")
+ else:
+ self._send_and_verify_message(sender, stream_name, topic_name=topic_name)
+
+ sent_message = self.get_last_message()
+ # Delete last sent message, so that the topic used for testing
+ # new topics is empty.
+ do_delete_messages(realm, [sent_message], acting_user=None)
+
+ self._send_and_verify_message(desdemona, stream_name, topic_name="existing topic")
+
+ # Set topic creation settings to nobody group.
+ nobody_group = NamedUserGroup.objects.get(
+ name=SystemGroups.NOBODY, realm=realm, is_system_group=True
+ )
+ do_change_stream_group_based_setting(
+ stream, "can_create_topic_group", nobody_group, acting_user=iago
+ )
+
+ check_sending_message_for_can_create_topic_group(desdemona, expect_fail=True)
+ check_sending_message_for_can_create_topic_group(desdemona_owned_bot, expect_fail=True)
+ check_sending_message_for_can_create_topic_group(bot_without_owner, expect_fail=True)
+
+ # Cross realm bots should be allowed
+ check_sending_message_for_can_create_topic_group(notification_bot)
+
+ # Sending message to an existing topic should be allowed.
+ check_sending_message_for_can_create_topic_group(desdemona, topic_name="existing topic")
+ check_sending_message_for_can_create_topic_group(
+ desdemona_owned_bot, topic_name="existing topic"
+ )
+ check_sending_message_for_can_create_topic_group(cordelia, topic_name="existing topic")
+ check_sending_message_for_can_create_topic_group(
+ bot_without_owner, topic_name="existing topic"
+ )
+
+ owners_group = NamedUserGroup.objects.get(
+ name=SystemGroups.OWNERS, realm=realm, is_system_group=True
+ )
+ do_change_stream_group_based_setting(
+ stream, "can_create_topic_group", owners_group, acting_user=iago
+ )
+
+ check_sending_message_for_can_create_topic_group(iago, expect_fail=True)
+ check_sending_message_for_can_create_topic_group(iago_owned_bot, expect_fail=True)
+ check_sending_message_for_can_create_topic_group(bot_without_owner, expect_fail=True)
+
+ check_sending_message_for_can_create_topic_group(desdemona)
+ check_sending_message_for_can_create_topic_group(desdemona_owned_bot)
+
+ # Cross realm bots should be allowed
+ check_sending_message_for_can_create_topic_group(notification_bot)
+
+ # Sending message to an existing topic should be allowed.
+ check_sending_message_for_can_create_topic_group(iago, topic_name="existing topic")
+ check_sending_message_for_can_create_topic_group(
+ iago_owned_bot, topic_name="existing topic"
+ )
+ check_sending_message_for_can_create_topic_group(
+ bot_without_owner, topic_name="existing topic"
+ )
+
+ hamletcharacters_group = NamedUserGroup.objects.get(name="hamletcharacters", realm=realm)
+ do_change_stream_group_based_setting(
+ stream, "can_create_topic_group", hamletcharacters_group, acting_user=iago
+ )
+
+ check_sending_message_for_can_create_topic_group(desdemona, expect_fail=True)
+ check_sending_message_for_can_create_topic_group(desdemona_owned_bot, expect_fail=True)
+ check_sending_message_for_can_create_topic_group(iago, expect_fail=True)
+ check_sending_message_for_can_create_topic_group(iago_owned_bot, expect_fail=True)
+ check_sending_message_for_can_create_topic_group(bot_without_owner, expect_fail=True)
+
+ check_sending_message_for_can_create_topic_group(hamlet)
+ check_sending_message_for_can_create_topic_group(cordelia)
+ check_sending_message_for_can_create_topic_group(cordelia_owned_bot)
+
+ # Cross realm bots should be allowed
+ check_sending_message_for_can_create_topic_group(notification_bot)
+
+ # Sending message to an existing topic should be allowed.
+ check_sending_message_for_can_create_topic_group(desdemona, topic_name="existing topic")
+ check_sending_message_for_can_create_topic_group(
+ desdemona_owned_bot, topic_name="existing topic"
+ )
+ check_sending_message_for_can_create_topic_group(iago, topic_name="existing topic")
+ check_sending_message_for_can_create_topic_group(
+ iago_owned_bot, topic_name="existing topic"
+ )
+ check_sending_message_for_can_create_topic_group(
+ bot_without_owner, topic_name="existing topic"
+ )
+
+ setting_group_member_dict = UserGroupMembersData(
+ direct_members=[othello.id], direct_subgroups=[owners_group.id]
+ )
+ do_change_stream_group_based_setting(
+ stream, "can_create_topic_group", setting_group_member_dict, acting_user=iago
+ )
+
+ check_sending_message_for_can_create_topic_group(iago, expect_fail=True)
+ check_sending_message_for_can_create_topic_group(iago_owned_bot, expect_fail=True)
+ check_sending_message_for_can_create_topic_group(hamlet, expect_fail=True)
+ check_sending_message_for_can_create_topic_group(cordelia, expect_fail=True)
+ check_sending_message_for_can_create_topic_group(cordelia_owned_bot, expect_fail=True)
+ check_sending_message_for_can_create_topic_group(bot_without_owner, expect_fail=True)
+
+ check_sending_message_for_can_create_topic_group(desdemona)
+ check_sending_message_for_can_create_topic_group(desdemona_owned_bot)
+ check_sending_message_for_can_create_topic_group(othello)
+ check_sending_message_for_can_create_topic_group(othello_owned_bot)
+
+ # Cross realm bots should be allowed
+ check_sending_message_for_can_create_topic_group(notification_bot)
+
+ # Sending message to an existing topic should be allowed.
+ check_sending_message_for_can_create_topic_group(iago, topic_name="existing topic")
+ check_sending_message_for_can_create_topic_group(
+ iago_owned_bot, topic_name="existing topic"
+ )
+ check_sending_message_for_can_create_topic_group(hamlet, topic_name="existing topic")
+ check_sending_message_for_can_create_topic_group(cordelia, topic_name="existing topic")
+ check_sending_message_for_can_create_topic_group(
+ cordelia_owned_bot, topic_name="existing topic"
+ )
+ check_sending_message_for_can_create_topic_group(
+ bot_without_owner, topic_name="existing topic"
+ )
+
+ everyone_group = NamedUserGroup.objects.get(
+ name=SystemGroups.EVERYONE, realm=realm, is_system_group=True
+ )
+ do_change_stream_group_based_setting(
+ stream, "can_create_topic_group", everyone_group, acting_user=iago
+ )
+ check_sending_message_for_can_create_topic_group(othello)
+ check_sending_message_for_can_create_topic_group(othello_owned_bot)
+ check_sending_message_for_can_create_topic_group(iago)
+ check_sending_message_for_can_create_topic_group(iago_owned_bot)
+ check_sending_message_for_can_create_topic_group(polonius)
+ check_sending_message_for_can_create_topic_group(bot_without_owner)
+
+ # Cross realm bots should be allowed
+ check_sending_message_for_can_create_topic_group(notification_bot)
+
def test_api_message_with_default_to(self) -> None:
"""
Sending messages without a to field should be sent to the default