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, Recipient,
Stream, Stream,
Subscription, Subscription,
UserGroup,
UserGroupMembership, UserGroupMembership,
UserMessage, UserMessage,
UserProfile, UserProfile,
@ -1977,6 +1978,23 @@ Output:
realm, "can_access_all_users_group", members_group, acting_user=None 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): class ZulipTestCase(ZulipTestCaseMixin, TestCase):
@contextmanager @contextmanager

View File

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

View File

@ -232,7 +232,6 @@ from zerver.models import (
RealmUserDefault, RealmUserDefault,
Service, Service,
Stream, Stream,
UserGroup,
UserMessage, UserMessage,
UserPresence, UserPresence,
UserProfile, UserProfile,
@ -1776,9 +1775,9 @@ class NormalActionsTest(BaseAction):
moderators_group = NamedUserGroup.objects.get( moderators_group = NamedUserGroup.objects.get(
name=SystemGroups.MODERATORS, realm=self.user_profile.realm, is_system_group=True name=SystemGroups.MODERATORS, realm=self.user_profile.realm, is_system_group=True
) )
user_group = UserGroup.objects.create(realm=self.user_profile.realm) user_group = self.create_or_update_anonymous_group_for_setting(
user_group.direct_members.set([othello]) [othello], [moderators_group]
user_group.direct_subgroups.set([moderators_group]) )
with self.verify_action() as events: with self.verify_action() as events:
check_add_user_group( check_add_user_group(
@ -1821,9 +1820,9 @@ class NormalActionsTest(BaseAction):
check_user_group_update("events[0]", events[0], "can_mention_group") check_user_group_update("events[0]", events[0], "can_mention_group")
self.assertEqual(events[0]["data"]["can_mention_group"], moderators_group.id) self.assertEqual(events[0]["data"]["can_mention_group"], moderators_group.id)
setting_group = UserGroup.objects.create(realm=self.user_profile.realm) setting_group = self.create_or_update_anonymous_group_for_setting(
setting_group.direct_members.set([othello.id]) [othello], [moderators_group]
setting_group.direct_subgroups.set([moderators_group.id]) )
with self.verify_action() as events: with self.verify_action() as events:
do_change_user_group_permission_setting( do_change_user_group_permission_setting(
backend, 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.test_helpers import queries_captured
from zerver.lib.topic import TOPIC_NAME from zerver.lib.topic import TOPIC_NAME
from zerver.lib.utils import assert_is_not_none 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.groups import SystemGroups
from zerver.models.realms import EditTopicPolicyEnum, WildcardMentionPolicyEnum, get_realm from zerver.models.realms import EditTopicPolicyEnum, WildcardMentionPolicyEnum, get_realm
from zerver.models.streams import get_stream 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. # Test all the cases when can_mention_group is not a named user group.
content = "Test mentioning user group @*leadership*" content = "Test mentioning user group @*leadership*"
user_group = UserGroup.objects.create(realm=iago.realm) user_group = self.create_or_update_anonymous_group_for_setting(
user_group.direct_members.set([othello]) [othello], [moderators_system_group]
user_group.direct_subgroups.set([moderators_system_group]) )
leadership.can_mention_group = user_group leadership.can_mention_group = user_group
leadership.save() leadership.save()

View File

@ -55,7 +55,6 @@ from zerver.models import (
Recipient, Recipient,
Stream, Stream,
Subscription, Subscription,
UserGroup,
UserMessage, UserMessage,
UserProfile, UserProfile,
) )
@ -2228,9 +2227,9 @@ class StreamMessagesTest(ZulipTestCase):
# Test all the cases when can_mention_group is not a named user group. # Test all the cases when can_mention_group is not a named user group.
content = "Test mentioning user group @*leadership*" content = "Test mentioning user group @*leadership*"
user_group = UserGroup.objects.create(realm=iago.realm) user_group = self.create_or_update_anonymous_group_for_setting(
user_group.direct_members.set([othello]) [othello], [moderators_system_group]
user_group.direct_subgroups.set([moderators_system_group]) )
leadership.can_mention_group = user_group leadership.can_mention_group = user_group
leadership.save() leadership.save()

View File

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