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.

message: Fix code to check group mention permission.

Sahil Batraeec80a4a5aaa
diff --git a/zerver/lib/message.py b/zerver/lib/message.py
index b873e310ec2..5b37f745d4a 100644
--- a/zerver/lib/message.py
+++ b/zerver/lib/message.py
@@ -1220,21 +1220,23 @@ def check_user_group_mention_allowed(sender: UserProfile, user_group_ids: List[i
for group in user_groups:
can_mention_group = group.can_mention_group
- can_mention_group_name = can_mention_group.named_user_group.name
if sender_is_system_bot:
- if can_mention_group_name == SystemGroups.EVERYONE:
+ if (
+ hasattr(can_mention_group, "named_user_group")
+ and can_mention_group.named_user_group.name == SystemGroups.EVERYONE
+ ):
continue
raise JsonableError(
- _(
- "You are not allowed to mention user group '{user_group_name}'. You must be a member of '{can_mention_group_name}' to mention this group."
- ).format(user_group_name=group.name, can_mention_group_name=can_mention_group_name)
+ _("You are not allowed to mention user group '{user_group_name}'.").format(
+ user_group_name=group.name
+ )
)
if not is_user_in_group(can_mention_group, sender, direct_member_only=False):
raise JsonableError(
- _(
- "You are not allowed to mention user group '{user_group_name}'. You must be a member of '{can_mention_group_name}' to mention this group."
- ).format(user_group_name=group.name, can_mention_group_name=can_mention_group_name)
+ _("You are not allowed to mention user group '{user_group_name}'.").format(
+ user_group_name=group.name
+ )
)
diff --git a/zerver/tests/test_message_edit.py b/zerver/tests/test_message_edit.py
index c8d3d2e1d92..494e53ceed2 100644
--- a/zerver/tests/test_message_edit.py
+++ b/zerver/tests/test_message_edit.py
@@ -17,7 +17,7 @@ from zerver.lib.test_classes import ZulipTestCase
from zerver.lib.test_helpers import queries_captured
from zerver.lib.topic import TOPIC_NAME
from zerver.lib.utils import assert_is_not_none
-from zerver.models import Message, NamedUserGroup, Realm, UserProfile, UserTopic
+from zerver.models import Message, NamedUserGroup, Realm, UserGroup, UserProfile, UserTopic
from zerver.models.groups import SystemGroups
from zerver.models.realms import get_realm
from zerver.models.streams import get_stream
@@ -1487,7 +1487,7 @@ class EditMessageTest(ZulipTestCase):
)
self.assert_json_error(
result,
- f"You are not allowed to mention user group '{leadership.name}'. You must be a member of '{moderators_system_group.name}' to mention this group.",
+ f"You are not allowed to mention user group '{leadership.name}'.",
)
# The restriction does not apply on silent mention.
@@ -1536,7 +1536,7 @@ class EditMessageTest(ZulipTestCase):
)
self.assert_json_error(
result,
- f"You are not allowed to mention user group '{support.name}'. You must be a member of '{leadership.name}' to mention this group.",
+ f"You are not allowed to mention user group '{support.name}'.",
)
msg_id = self.send_stream_message(othello, "test_stream", "Test message")
@@ -1571,7 +1571,7 @@ class EditMessageTest(ZulipTestCase):
)
self.assert_json_error(
result,
- f"You are not allowed to mention user group '{support.name}'. You must be a member of '{leadership.name}' to mention this group.",
+ f"You are not allowed to mention user group '{support.name}'.",
)
msg_id = self.send_stream_message(othello, "test_stream", "Test message")
@@ -1584,7 +1584,7 @@ class EditMessageTest(ZulipTestCase):
)
self.assert_json_error(
result,
- f"You are not allowed to mention user group '{leadership.name}'. You must be a member of '{moderators_system_group.name}' to mention this group.",
+ f"You are not allowed to mention user group '{leadership.name}'.",
)
msg_id = self.send_stream_message(shiva, "test_stream", "Test message")
@@ -1596,3 +1596,62 @@ class EditMessageTest(ZulipTestCase):
},
)
self.assert_json_success(result)
+
+ # Test all the cases when can_mention_group is not a named user group.
+ content = "Test mentioning user group @*leadership*"
+ user_group = UserGroup.objects.create(realm=iago.realm)
+ user_group.direct_members.set([othello])
+ user_group.direct_subgroups.set([moderators_system_group])
+ leadership.can_mention_group = user_group
+ leadership.save()
+
+ msg_id = self.send_stream_message(othello, "test_stream", "Test message")
+ self.login("othello")
+ result = self.client_patch(
+ "/json/messages/" + str(msg_id),
+ {
+ "content": content,
+ },
+ )
+ self.assert_json_success(result)
+
+ msg_id = self.send_stream_message(shiva, "test_stream", "Test message")
+ self.login("shiva")
+ result = self.client_patch(
+ "/json/messages/" + str(msg_id),
+ {
+ "content": content,
+ },
+ )
+ self.assert_json_success(result)
+
+ msg_id = self.send_stream_message(iago, "test_stream", "Test message")
+ self.login("iago")
+ result = self.client_patch(
+ "/json/messages/" + str(msg_id),
+ {
+ "content": content,
+ },
+ )
+ self.assert_json_success(result)
+
+ msg_id = self.send_stream_message(cordelia, "test_stream", "Test message")
+ self.login("cordelia")
+ result = self.client_patch(
+ "/json/messages/" + str(msg_id),
+ {
+ "content": content,
+ },
+ )
+ self.assert_json_error(
+ result, f"You are not allowed to mention user group '{leadership.name}'."
+ )
+
+ content = "Test mentioning user group @_*leadership*"
+ result = self.client_patch(
+ "/json/messages/" + str(msg_id),
+ {
+ "content": content,
+ },
+ )
+ self.assert_json_success(result)
diff --git a/zerver/tests/test_message_send.py b/zerver/tests/test_message_send.py
index 71a9685e6c4..31656986e22 100644
--- a/zerver/tests/test_message_send.py
+++ b/zerver/tests/test_message_send.py
@@ -55,6 +55,7 @@ from zerver.models import (
Recipient,
Stream,
Subscription,
+ UserGroup,
UserMessage,
UserProfile,
)
@@ -2129,7 +2130,7 @@ class StreamMessagesTest(ZulipTestCase):
leadership.save()
with self.assertRaisesRegex(
JsonableError,
- f"You are not allowed to mention user group '{leadership.name}'. You must be a member of '{moderators_system_group.name}' to mention this group.",
+ f"You are not allowed to mention user group '{leadership.name}'.",
):
self.send_stream_message(cordelia, "test_stream", content)
@@ -2156,7 +2157,7 @@ class StreamMessagesTest(ZulipTestCase):
content = "Test mentioning user group @*support*"
with self.assertRaisesRegex(
JsonableError,
- f"You are not allowed to mention user group '{support.name}'. You must be a member of '{leadership.name}' to mention this group.",
+ f"You are not allowed to mention user group '{support.name}'.",
):
self.send_stream_message(iago, "test_stream", content)
@@ -2171,13 +2172,13 @@ class StreamMessagesTest(ZulipTestCase):
content = "Test mentioning user group @*support* @*leadership*"
with self.assertRaisesRegex(
JsonableError,
- f"You are not allowed to mention user group '{support.name}'. You must be a member of '{leadership.name}' to mention this group.",
+ f"You are not allowed to mention user group '{support.name}'.",
):
self.send_stream_message(iago, "test_stream", content)
with self.assertRaisesRegex(
JsonableError,
- f"You are not allowed to mention user group '{leadership.name}'. You must be a member of '{moderators_system_group.name}' to mention this group.",
+ f"You are not allowed to mention user group '{leadership.name}'.",
):
self.send_stream_message(othello, "test_stream", content)
@@ -2197,7 +2198,7 @@ class StreamMessagesTest(ZulipTestCase):
system_bot = get_system_bot(settings.EMAIL_GATEWAY_BOT, internal_realm.id)
with self.assertRaisesRegex(
JsonableError,
- f"You are not allowed to mention user group '{support.name}'. You must be a member of '{members_group.name}' to mention this group.",
+ f"You are not allowed to mention user group '{support.name}'.",
):
self.send_stream_message(system_bot, "test_stream", content, recipient_realm=iago.realm)
@@ -2213,6 +2214,43 @@ class StreamMessagesTest(ZulipTestCase):
result = self.api_get(shiva, "/api/v1/messages/" + str(msg_id))
self.assert_json_success(result)
+ # Test all the cases when can_mention_group is not a named user group.
+ content = "Test mentioning user group @*leadership*"
+ user_group = UserGroup.objects.create(realm=iago.realm)
+ user_group.direct_members.set([othello])
+ user_group.direct_subgroups.set([moderators_system_group])
+ leadership.can_mention_group = user_group
+ leadership.save()
+
+ msg_id = self.send_stream_message(othello, "test_stream", content)
+ result = self.api_get(cordelia, "/api/v1/messages/" + str(msg_id))
+ self.assert_json_success(result)
+
+ msg_id = self.send_stream_message(shiva, "test_stream", content)
+ result = self.api_get(cordelia, "/api/v1/messages/" + str(msg_id))
+ self.assert_json_success(result)
+
+ msg_id = self.send_stream_message(iago, "test_stream", content)
+ result = self.api_get(cordelia, "/api/v1/messages/" + str(msg_id))
+ self.assert_json_success(result)
+
+ with self.assertRaisesRegex(
+ JsonableError,
+ f"You are not allowed to mention user group '{leadership.name}'.",
+ ):
+ self.send_stream_message(cordelia, "test_stream", content)
+
+ with self.assertRaisesRegex(
+ JsonableError,
+ f"You are not allowed to mention user group '{leadership.name}'.",
+ ):
+ self.send_stream_message(system_bot, "test_stream", content, recipient_realm=iago.realm)
+
+ content = "Test mentioning user group @_*leadership*"
+ msg_id = self.send_stream_message(shiva, "test_stream", content)
+ result = self.api_get(cordelia, "/api/v1/messages/" + str(msg_id))
+ self.assert_json_success(result)
+
def test_stream_message_mirroring(self) -> None:
user = self.mit_user("starnine")
self.subscribe(user, "Verona")