message_edit: Allow moving message to stream based on setting value.

We allow the users to move message between streams according to the
value of 'move_messages_between_streams_policy'.
This commit is contained in:
sahil839 2021-04-09 01:00:40 +05:30 committed by Tim Abbott
parent de6bd22ee9
commit b52ad3e536
2 changed files with 80 additions and 16 deletions

View File

@ -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)

View File

@ -8,6 +8,7 @@ from django.db import IntegrityError
from django.http import HttpResponse
from 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_ids
from zerver.lib.test_classes import ZulipTestCase
from zerver.lib.test_helpers import cache_tries_captured, queries_captured
from 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_stream
class 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,
)
self.assert_json_error(result, "You don't have permission to move this message")
check_move_message_according_to_policy(UserProfile.ROLE_MODERATOR, expect_fail=True)
check_move_message_according_to_policy(UserProfile.ROLE_REALM_ADMINISTRATOR)
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_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,
)
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, new_stream, "test")
self.assertEqual(len(messages), 0)
(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)
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(