message: Fix code to check group mention permission.

This commits fixes the code which checks group mention permission
to handle anonymous user groups correctly. Basically we were
not checking whether the UserGroup is linked to a NamedUserGroup
and directly accessing named_user_group which results in an
error.

We also update the error messages to include the group name
which has permission to mention the groups since now there
might be a comnbination of groups and users who has permission
to mention the group.

This commit also adds tests to check sending and editing messages
when can_mention_group is set to a anonymous user group.
This commit is contained in:
Sahil Batra
2024-05-18 13:18:42 +05:30
committed by Tim Abbott
parent ba92cd8b23
commit eec80a4a5a
3 changed files with 117 additions and 18 deletions

View File

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