tests: Add a helper function to create anonymous groups.

This commit adds a new helper function to create or update
a UserGroup object for a setting. We could have used existing
update_or_create_user_group_for_setting but that also validates
user IDs and subgroup IDs which we can skip in tests.
This commit is contained in:
Sahil Batra 2024-05-28 12:55:40 +05:30 committed by Tim Abbott
parent 81765a1e83
commit aa84080ad6
6 changed files with 38 additions and 26 deletions

View File

@ -107,6 +107,7 @@ from zerver.models import (
Recipient,
Stream,
Subscription,
UserGroup,
UserGroupMembership,
UserMessage,
UserProfile,
@ -1977,6 +1978,23 @@ Output:
realm, "can_access_all_users_group", members_group, acting_user=None
)
def create_or_update_anonymous_group_for_setting(
self,
direct_members: List[UserProfile],
direct_subgroups: List[NamedUserGroup],
existing_setting_group: Optional[UserGroup] = None,
) -> UserGroup:
realm = get_realm("zulip")
if existing_setting_group is not None:
existing_setting_group.direct_members.set(direct_members)
existing_setting_group.direct_subgroups.set(direct_subgroups)
return existing_setting_group
user_group = UserGroup.objects.create(realm=realm)
user_group.direct_members.set(direct_members)
user_group.direct_subgroups.set(direct_subgroups)
return user_group
class ZulipTestCase(ZulipTestCaseMixin, TestCase):
@contextmanager

View File

@ -81,7 +81,6 @@ from zerver.models import (
RealmPlayground,
Recipient,
Subscription,
UserGroup,
UserProfile,
)
from zerver.models.groups import SystemGroups
@ -1347,9 +1346,7 @@ class TestRealmAuditLog(ZulipTestCase):
name=SystemGroups.MODERATORS, realm=user_group.realm, is_system_group=True
)
old_group = user_group.can_mention_group
new_group = UserGroup.objects.create(realm=user_group.realm)
new_group.direct_members.set([hamlet.id])
new_group.direct_subgroups.set([moderators_group.id])
new_group = self.create_or_update_anonymous_group_for_setting([hamlet], [moderators_group])
now = timezone_now()
do_change_user_group_permission_setting(
@ -1382,9 +1379,9 @@ class TestRealmAuditLog(ZulipTestCase):
# Since the old setting value was a anonymous group, we just update the
# members and subgroups of the already existing UserGroup instead of creating
# a new UserGroup object to keep this consistent with the actual code.
new_group = user_group.can_mention_group
new_group.direct_members.set([othello.id])
new_group.direct_subgroups.set([moderators_group.id])
new_group = self.create_or_update_anonymous_group_for_setting(
[othello], [moderators_group], existing_setting_group=user_group.can_mention_group
)
now = timezone_now()
do_change_user_group_permission_setting(

View File

@ -232,7 +232,6 @@ from zerver.models import (
RealmUserDefault,
Service,
Stream,
UserGroup,
UserMessage,
UserPresence,
UserProfile,
@ -1776,9 +1775,9 @@ class NormalActionsTest(BaseAction):
moderators_group = NamedUserGroup.objects.get(
name=SystemGroups.MODERATORS, realm=self.user_profile.realm, is_system_group=True
)
user_group = UserGroup.objects.create(realm=self.user_profile.realm)
user_group.direct_members.set([othello])
user_group.direct_subgroups.set([moderators_group])
user_group = self.create_or_update_anonymous_group_for_setting(
[othello], [moderators_group]
)
with self.verify_action() as events:
check_add_user_group(
@ -1821,9 +1820,9 @@ class NormalActionsTest(BaseAction):
check_user_group_update("events[0]", events[0], "can_mention_group")
self.assertEqual(events[0]["data"]["can_mention_group"], moderators_group.id)
setting_group = UserGroup.objects.create(realm=self.user_profile.realm)
setting_group.direct_members.set([othello.id])
setting_group.direct_subgroups.set([moderators_group.id])
setting_group = self.create_or_update_anonymous_group_for_setting(
[othello], [moderators_group]
)
with self.verify_action() as events:
do_change_user_group_permission_setting(
backend,

View File

@ -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, UserGroup, UserProfile, UserTopic
from zerver.models import Message, NamedUserGroup, Realm, UserProfile, UserTopic
from zerver.models.groups import SystemGroups
from zerver.models.realms import EditTopicPolicyEnum, WildcardMentionPolicyEnum, get_realm
from zerver.models.streams import get_stream
@ -1599,9 +1599,9 @@ class EditMessageTest(ZulipTestCase):
# 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])
user_group = self.create_or_update_anonymous_group_for_setting(
[othello], [moderators_system_group]
)
leadership.can_mention_group = user_group
leadership.save()

View File

@ -55,7 +55,6 @@ from zerver.models import (
Recipient,
Stream,
Subscription,
UserGroup,
UserMessage,
UserProfile,
)
@ -2228,9 +2227,9 @@ class StreamMessagesTest(ZulipTestCase):
# 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])
user_group = self.create_or_update_anonymous_group_for_setting(
[othello], [moderators_system_group]
)
leadership.can_mention_group = user_group
leadership.save()

View File

@ -105,10 +105,9 @@ class UserGroupTestCase(ZulipTestCase):
self.assertEqual(user_groups[9]["can_mention_group"], everyone_group.id)
othello = self.example_user("othello")
setting_group = UserGroup.objects.create(realm=realm)
setting_group.direct_members.set([othello])
setting_group.direct_subgroups.set([admins_system_group])
setting_group = self.create_or_update_anonymous_group_for_setting(
[othello], [admins_system_group]
)
new_user_group = check_add_user_group(
realm,
"newgroup2",