From aa84080ad63a28185aeeb6b4165da8413521da8b Mon Sep 17 00:00:00 2001 From: Sahil Batra Date: Tue, 28 May 2024 12:55:40 +0530 Subject: [PATCH] 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. --- zerver/lib/test_classes.py | 18 ++++++++++++++++++ zerver/tests/test_audit_log.py | 11 ++++------- zerver/tests/test_events.py | 13 ++++++------- zerver/tests/test_message_edit.py | 8 ++++---- zerver/tests/test_message_send.py | 7 +++---- zerver/tests/test_user_groups.py | 7 +++---- 6 files changed, 38 insertions(+), 26 deletions(-) diff --git a/zerver/lib/test_classes.py b/zerver/lib/test_classes.py index 068e89bb27..21f00167ad 100644 --- a/zerver/lib/test_classes.py +++ b/zerver/lib/test_classes.py @@ -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 diff --git a/zerver/tests/test_audit_log.py b/zerver/tests/test_audit_log.py index 1e842d6f45..848a49746a 100644 --- a/zerver/tests/test_audit_log.py +++ b/zerver/tests/test_audit_log.py @@ -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( diff --git a/zerver/tests/test_events.py b/zerver/tests/test_events.py index 984c7a5eda..91be738ec9 100644 --- a/zerver/tests/test_events.py +++ b/zerver/tests/test_events.py @@ -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, diff --git a/zerver/tests/test_message_edit.py b/zerver/tests/test_message_edit.py index 57b6eb04a8..9300825e95 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, 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() diff --git a/zerver/tests/test_message_send.py b/zerver/tests/test_message_send.py index 2f981c9ca3..cac7bfee22 100644 --- a/zerver/tests/test_message_send.py +++ b/zerver/tests/test_message_send.py @@ -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() diff --git a/zerver/tests/test_user_groups.py b/zerver/tests/test_user_groups.py index 57344012b1..653f178935 100644 --- a/zerver/tests/test_user_groups.py +++ b/zerver/tests/test_user_groups.py @@ -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",