Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
message_edit: Allow moving message to stream based on setting value.
sahil839b52ad3e5363f
diff --git a/zerver/lib/actions.py b/zerver/lib/actions.pyindex 68ee76ea522..015a0b10433 100644--- a/zerver/lib/actions.py+++ b/zerver/lib/actions.py@@ -2802,7 +2802,7 @@ def check_update_message(if stream_id is not None:if not message.is_stream_message():raise JsonableError(_("Message must be a stream message"))- if not user_profile.is_realm_admin:+ if not user_profile.can_move_messages_between_streams():raise JsonableError(_("You don't have permission to move this message"))try:access_stream_by_id(user_profile, message.recipient.type_id)diff --git a/zerver/tests/test_message_edit.py b/zerver/tests/test_message_edit.pyindex f79a66e97fc..f8e76a5c3ea 100644--- a/zerver/tests/test_message_edit.py+++ b/zerver/tests/test_message_edit.py@@ -8,6 +8,7 @@ from django.db import IntegrityErrorfrom django.http import HttpResponsefrom zerver.lib.actions import (+ do_change_user_role,do_set_realm_property,do_update_message,get_topic_messages,@@ -17,7 +18,7 @@ from zerver.lib.message import MessageDict, has_message_access, messages_for_idsfrom zerver.lib.test_classes import ZulipTestCasefrom zerver.lib.test_helpers import cache_tries_captured, queries_capturedfrom zerver.lib.topic import LEGACY_PREV_TOPIC, TOPIC_NAME-from zerver.models import Message, Stream, UserMessage, UserProfile, get_realm, get_stream+from zerver.models import Message, Realm, Stream, UserMessage, UserProfile, get_realm, get_streamclass EditMessageTest(ZulipTestCase):@@ -1268,26 +1269,89 @@ class EditMessageTest(ZulipTestCase):f"This topic was moved here from #**test move stream>test** by @_**Iago|{user_profile.id}**",)- def test_move_message_to_stream_no_allowed(self) -> None:+ def test_move_message_between_streams_policy_setting(self) -> None:(user_profile, old_stream, new_stream, msg_id, msg_id_later) = self.prepare_move_topics(- "aaron", "test move stream", "new stream", "test"+ "othello", "old_stream_1", "new_stream_1", "test")- result = self.client_patch(- "/json/messages/" + str(msg_id),- {- "message_id": msg_id,- "stream_id": new_stream.id,- "propagate_mode": "change_all",- },+ def check_move_message_according_to_policy(role: int, expect_fail: bool = False) -> None:+ do_change_user_role(user_profile, role, acting_user=None)++ result = self.client_patch(+ "/json/messages/" + str(msg_id),+ {+ "message_id": msg_id,+ "stream_id": new_stream.id,+ "propagate_mode": "change_all",+ },+ )++ if expect_fail:+ self.assert_json_error(result, "You don't have permission to move this message")+ messages = get_topic_messages(user_profile, old_stream, "test")+ self.assertEqual(len(messages), 3)+ messages = get_topic_messages(user_profile, new_stream, "test")+ self.assertEqual(len(messages), 0)+ else:+ self.assert_json_success(result)+ messages = get_topic_messages(user_profile, old_stream, "test")+ self.assertEqual(len(messages), 1)+ messages = get_topic_messages(user_profile, new_stream, "test")+ self.assertEqual(len(messages), 4)++ # Check sending messages when policy is Realm.POLICY_ADMINS_ONLY.+ do_set_realm_property(+ user_profile.realm,+ "move_messages_between_streams_policy",+ Realm.POLICY_ADMINS_ONLY,+ acting_user=None,+ )+ check_move_message_according_to_policy(UserProfile.ROLE_MODERATOR, expect_fail=True)+ check_move_message_according_to_policy(UserProfile.ROLE_REALM_ADMINISTRATOR)++ (user_profile, old_stream, new_stream, msg_id, msg_id_later) = self.prepare_move_topics(+ "othello", "old_stream_2", "new_stream_2", "test"+ )+ # Check sending messages when policy is Realm.POLICY_MODERATORS_ONLY.+ do_set_realm_property(+ user_profile.realm,+ "move_messages_between_streams_policy",+ Realm.POLICY_MODERATORS_ONLY,+ acting_user=None,)- self.assert_json_error(result, "You don't have permission to move this message")+ check_move_message_according_to_policy(UserProfile.ROLE_MEMBER, expect_fail=True)+ check_move_message_according_to_policy(UserProfile.ROLE_MODERATOR)- messages = get_topic_messages(user_profile, old_stream, "test")- self.assertEqual(len(messages), 3)+ (user_profile, old_stream, new_stream, msg_id, msg_id_later) = self.prepare_move_topics(+ "othello", "old_stream_3", "new_stream_3", "test"+ )+ # Check sending messages when policy is Realm.POLICY_FULL_MEMBERS_ONLY.+ do_set_realm_property(+ user_profile.realm,+ "move_messages_between_streams_policy",+ Realm.POLICY_FULL_MEMBERS_ONLY,+ acting_user=None,+ )+ do_set_realm_property(+ user_profile.realm, "waiting_period_threshold", 100000, acting_user=None+ )+ check_move_message_according_to_policy(UserProfile.ROLE_MEMBER, expect_fail=True)- messages = get_topic_messages(user_profile, new_stream, "test")- self.assertEqual(len(messages), 0)+ do_set_realm_property(user_profile.realm, "waiting_period_threshold", 0, acting_user=None)+ check_move_message_according_to_policy(UserProfile.ROLE_MEMBER)++ (user_profile, old_stream, new_stream, msg_id, msg_id_later) = self.prepare_move_topics(+ "othello", "old_stream_4", "new_stream_4", "test"+ )+ # Check sending messages when policy is Realm.POLICY_MEMBERS_ONLY.+ do_set_realm_property(+ user_profile.realm,+ "move_messages_between_streams_policy",+ Realm.POLICY_MEMBERS_ONLY,+ acting_user=None,+ )+ check_move_message_according_to_policy(UserProfile.ROLE_GUEST, expect_fail=True)+ check_move_message_according_to_policy(UserProfile.ROLE_MEMBER)def test_move_message_to_stream_with_content(self) -> None:(user_profile, old_stream, new_stream, msg_id, msg_id_later) = self.prepare_move_topics(