tests: Use NamedUserGroup for queries.

This commit is contained in:
Sahil Batra 2024-04-18 15:53:46 +05:30 committed by Tim Abbott
parent a96c8b8352
commit e78d0aacaf
13 changed files with 143 additions and 123 deletions

View File

@ -68,13 +68,13 @@ from zerver.models import (
Client,
Huddle,
Message,
NamedUserGroup,
PreregistrationUser,
Realm,
RealmAuditLog,
Recipient,
Stream,
UserActivityInterval,
UserGroup,
UserProfile,
)
from zerver.models.clients import get_client
@ -104,7 +104,7 @@ class AnalyticsTestCase(ZulipTestCase):
self.default_realm = do_create_realm(
string_id="realmtest", name="Realm Test", date_created=self.TIME_ZERO - 2 * self.DAY
)
self.administrators_user_group = UserGroup.objects.get(
self.administrators_user_group = NamedUserGroup.objects.get(
name=SystemGroups.ADMINISTRATORS,
realm=self.default_realm,
is_system_group=True,

View File

@ -20,7 +20,7 @@ from zerver.lib.initial_password import initial_password
from zerver.lib.test_classes import ZulipTestCase
from zerver.lib.upload import upload_message_attachment
from zerver.lib.users import get_api_key
from zerver.models import Client, Message, UserGroup, UserPresence
from zerver.models import Client, Message, NamedUserGroup, UserPresence
from zerver.models.realms import get_realm
from zerver.models.users import get_user
from zerver.openapi.openapi import Parameter
@ -262,8 +262,13 @@ def create_user_group_data() -> Dict[str, object]:
["/user_groups/{user_group_id}:patch", "/user_groups/{user_group_id}:delete"]
)
def get_temp_user_group_id() -> Dict[str, object]:
user_group, _ = UserGroup.objects.get_or_create(
name="temp", realm=get_realm("zulip"), can_mention_group_id=11
user_group, _ = NamedUserGroup.objects.get_or_create(
name="temp",
realm=get_realm("zulip"),
can_mention_group_id=11,
named_group_name="temp",
named_group_can_mention_group_id=11,
realm_for_sharding=get_realm("zulip"),
)
return {
"user_group_id": user_group.id,

View File

@ -81,7 +81,6 @@ from zerver.models import (
RealmPlayground,
Recipient,
Subscription,
UserGroup,
UserProfile,
)
from zerver.models.groups import SystemGroups
@ -1092,7 +1091,7 @@ class TestRealmAuditLog(ZulipTestCase):
expected_system_user_group_count = len(NamedUserGroup.SYSTEM_USER_GROUP_ROLE_MAP) + 3
system_user_group_ids = sorted(
UserGroup.objects.filter(
NamedUserGroup.objects.filter(
realm=realm,
is_system_group=True,
).values_list("id", flat=True)
@ -1148,7 +1147,7 @@ class TestRealmAuditLog(ZulipTestCase):
hamlet = self.example_user("hamlet")
cordelia = self.example_user("cordelia")
now = timezone_now()
public_group = UserGroup.objects.get(
public_group = NamedUserGroup.objects.get(
name=SystemGroups.EVERYONE_ON_INTERNET, realm=hamlet.realm
)
user_group = check_add_user_group(
@ -1313,7 +1312,7 @@ class TestRealmAuditLog(ZulipTestCase):
)
old_group = user_group.can_mention_group
new_group = UserGroup.objects.get(
new_group = NamedUserGroup.objects.get(
name=SystemGroups.EVERYONE_ON_INTERNET, realm=user_group.realm
)
self.assertNotEqual(old_group.id, new_group.id)

View File

@ -115,7 +115,6 @@ from zerver.models import (
Realm,
RealmDomain,
Stream,
UserGroup,
UserProfile,
)
from zerver.models.realms import clear_supported_auth_backends_cache, get_realm
@ -7498,13 +7497,17 @@ class LDAPGroupSyncTest(ZulipTestCase):
},
LDAP_APPEND_DOMAIN="zulip.com",
), self.assertLogs("zulip.ldap", "DEBUG") as zulip_ldap_log:
self.assertFalse(UserGroup.objects.filter(realm=realm, name="cool_test_group").exists())
self.assertFalse(
NamedUserGroup.objects.filter(realm=realm, name="cool_test_group").exists()
)
create_user_group_in_database(
"cool_test_group", [], realm, acting_user=None, description="Created by LDAP sync"
)
self.assertTrue(UserGroup.objects.filter(realm=realm, name="cool_test_group").exists())
self.assertTrue(
NamedUserGroup.objects.filter(realm=realm, name="cool_test_group").exists()
)
user_group = NamedUserGroup.objects.get(realm=realm, name="cool_test_group")
@ -7537,7 +7540,7 @@ class LDAPGroupSyncTest(ZulipTestCase):
self.assertTrue(
is_user_in_group(
UserGroup.objects.get(realm=realm, name="cool_test_group"),
NamedUserGroup.objects.get(realm=realm, name="cool_test_group"),
cordelia,
direct_member_only=True,
)
@ -7548,7 +7551,7 @@ class LDAPGroupSyncTest(ZulipTestCase):
self.assertFalse(
is_user_in_group(
UserGroup.objects.get(realm=realm, name="cool_test_group"),
NamedUserGroup.objects.get(realm=realm, name="cool_test_group"),
cordelia,
direct_member_only=True,
)

View File

@ -231,7 +231,6 @@ from zerver.models import (
RealmUserDefault,
Service,
Stream,
UserGroup,
UserMessage,
UserPresence,
UserProfile,
@ -1840,7 +1839,7 @@ class NormalActionsTest(BaseAction):
check_user_group_update("events[0]", events[0], "description")
# Test can_mention_group setting update
moderators_group = UserGroup.objects.get(
moderators_group = NamedUserGroup.objects.get(
name="role:moderators", realm=self.user_profile.realm, is_system_group=True
)
events = self.verify_action(
@ -2733,7 +2732,7 @@ class NormalActionsTest(BaseAction):
def test_realm_update_plan_type(self) -> None:
realm = self.user_profile.realm
members_group = UserGroup.objects.get(name=SystemGroups.MEMBERS, realm=realm)
members_group = NamedUserGroup.objects.get(name=SystemGroups.MEMBERS, realm=realm)
do_change_realm_permission_group_setting(
realm, "can_access_all_users_group", members_group, acting_user=None
)
@ -3640,7 +3639,7 @@ class RealmPropertyActionTest(BaseAction):
check_realm_update("events[0]", events[0], name)
def do_set_realm_permission_group_setting_test(self, setting_name: str) -> None:
all_system_user_groups = UserGroup.objects.filter(
all_system_user_groups = NamedUserGroup.objects.filter(
realm=self.user_profile.realm,
is_system_group=True,
)
@ -4194,7 +4193,7 @@ class SubscribeActionTest(BaseAction):
events = self.verify_action(action, include_subscribers=include_subscribers, num_events=2)
check_stream_update("events[0]", events[0])
moderators_group = UserGroup.objects.get(
moderators_group = NamedUserGroup.objects.get(
name=SystemGroups.MODERATORS,
is_system_group=True,
realm=self.user_profile.realm,

View File

@ -1239,14 +1239,14 @@ class RealmImportExportTest(ExportFile):
@getter
def get_user_membership(r: Realm) -> Set[str]:
usergroup = UserGroup.objects.get(realm=r, name="hamletcharacters")
usergroup = NamedUserGroup.objects.get(realm=r, name="hamletcharacters")
usergroup_membership = UserGroupMembership.objects.filter(user_group=usergroup)
users = {membership.user_profile.email for membership in usergroup_membership}
return users
@getter
def get_group_group_membership(r: Realm) -> Set[str]:
usergroup = UserGroup.objects.get(realm=r, name="role:members")
usergroup = NamedUserGroup.objects.get(realm=r, name="role:members")
group_group_membership = GroupGroupMembership.objects.filter(supergroup=usergroup)
subgroups = {membership.subgroup.name for membership in group_group_membership}
return subgroups
@ -1256,7 +1256,7 @@ class RealmImportExportTest(ExportFile):
# We already check the members of the group through UserGroupMembership
# objects, but we also want to check direct_members field is set
# correctly since we do not include this in export data.
usergroup = UserGroup.objects.get(realm=r, name="hamletcharacters")
usergroup = NamedUserGroup.objects.get(realm=r, name="hamletcharacters")
direct_members = usergroup.direct_members.all()
direct_member_emails = {user.email for user in direct_members}
return direct_member_emails
@ -1266,14 +1266,14 @@ class RealmImportExportTest(ExportFile):
# We already check the subgroups of the group through GroupGroupMembership
# objects, but we also want to check that direct_subgroups field is set
# correctly since we do not include this in export data.
usergroup = UserGroup.objects.get(realm=r, name="role:members")
usergroup = NamedUserGroup.objects.get(realm=r, name="role:members")
direct_subgroups = usergroup.direct_subgroups.all()
direct_subgroup_names = {group.name for group in direct_subgroups}
return direct_subgroup_names
@getter
def get_user_group_can_mention_group_setting(r: Realm) -> str:
user_group = UserGroup.objects.get(realm=r, name="hamletcharacters")
user_group = NamedUserGroup.objects.get(realm=r, name="hamletcharacters")
return user_group.can_mention_group.name
# test botstoragedata and botconfigdata
@ -1330,7 +1330,7 @@ class RealmImportExportTest(ExportFile):
@getter
def get_user_group_mention(r: Realm) -> str:
user_group = UserGroup.objects.get(realm=r, name="hamletcharacters")
user_group = NamedUserGroup.objects.get(realm=r, name="hamletcharacters")
data_usergroup_id = f'data-user-group-id="{user_group.id}"'
mention_message = get_stream_messages(r).get(
rendered_content__contains=data_usergroup_id

View File

@ -56,11 +56,11 @@ from zerver.models import (
DefaultStream,
Message,
MultiuseInvite,
NamedUserGroup,
PreregistrationUser,
Realm,
ScheduledEmail,
Stream,
UserGroup,
UserMessage,
UserProfile,
)
@ -2465,7 +2465,7 @@ class MultiuseInviteTest(ZulipTestCase):
def test_multiuse_invite_without_permission_to_subscribe_others(self) -> None:
realm = get_realm("zulip")
members_group = UserGroup.objects.get(
members_group = NamedUserGroup.objects.get(
name=SystemGroups.MEMBERS, realm=realm, is_system_group=True
)
do_change_realm_permission_group_setting(
@ -2524,10 +2524,10 @@ class MultiuseInviteTest(ZulipTestCase):
def test_create_multiuse_invite_group_setting(self) -> None:
realm = get_realm("zulip")
full_members_system_group = UserGroup.objects.get(
full_members_system_group = NamedUserGroup.objects.get(
name=SystemGroups.FULL_MEMBERS, realm=realm, is_system_group=True
)
nobody_system_group = UserGroup.objects.get(
nobody_system_group = NamedUserGroup.objects.get(
name=SystemGroups.NOBODY, realm=realm, is_system_group=True
)
@ -2559,7 +2559,7 @@ class MultiuseInviteTest(ZulipTestCase):
def test_only_owner_can_change_create_multiuse_invite_group(self) -> None:
realm = get_realm("zulip")
full_members_system_group = UserGroup.objects.get(
full_members_system_group = NamedUserGroup.objects.get(
name=SystemGroups.FULL_MEMBERS, realm=realm, is_system_group=True
)
@ -2603,7 +2603,7 @@ class MultiuseInviteTest(ZulipTestCase):
def test_multiuse_link_for_inviting_as_admin(self) -> None:
realm = get_realm("zulip")
full_members_system_group = UserGroup.objects.get(
full_members_system_group = NamedUserGroup.objects.get(
name=SystemGroups.FULL_MEMBERS, realm=realm, is_system_group=True
)
@ -2634,7 +2634,7 @@ class MultiuseInviteTest(ZulipTestCase):
def test_multiuse_link_for_inviting_as_moderator(self) -> None:
realm = get_realm("zulip")
full_members_system_group = UserGroup.objects.get(
full_members_system_group = NamedUserGroup.objects.get(
name=SystemGroups.FULL_MEMBERS, realm=realm, is_system_group=True
)

View File

@ -63,7 +63,7 @@ from zerver.lib.mention import (
from zerver.lib.per_request_cache import flush_per_request_caches
from zerver.lib.test_classes import ZulipTestCase
from zerver.lib.tex import render_tex
from zerver.models import Message, RealmEmoji, RealmFilter, UserGroup, UserMessage, UserProfile
from zerver.models import Message, NamedUserGroup, RealmEmoji, RealmFilter, UserMessage, UserProfile
from zerver.models.clients import get_client
from zerver.models.groups import SystemGroups
from zerver.models.linkifiers import linkifiers_for_realm
@ -2614,7 +2614,7 @@ class MarkdownTest(ZulipTestCase):
)
self.assertEqual(rendering_result.mentions_user_ids, set())
def create_user_group_for_test(self, user_group_name: str) -> UserGroup:
def create_user_group_for_test(self, user_group_name: str) -> NamedUserGroup:
othello = self.example_user("othello")
return check_add_user_group(
get_realm("zulip"), user_group_name, [othello], acting_user=None
@ -2847,7 +2847,7 @@ class MarkdownTest(ZulipTestCase):
desdemona = self.example_user("desdemona")
iago = self.example_user("iago")
hamlet = self.example_user("hamlet")
moderators_group = UserGroup.objects.get(
moderators_group = NamedUserGroup.objects.get(
realm=iago.realm, name=SystemGroups.MODERATORS, is_system_group=True
)
content = "@*role:moderators* @**King Hamlet** test message"

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, Realm, UserGroup, UserProfile, UserTopic
from zerver.models import Message, NamedUserGroup, Realm, UserProfile, UserTopic
from zerver.models.groups import SystemGroups
from zerver.models.realms import get_realm
from zerver.models.streams import get_stream
@ -1459,7 +1459,7 @@ class EditMessageTest(ZulipTestCase):
leadership = check_add_user_group(othello.realm, "leadership", [othello], acting_user=None)
support = check_add_user_group(othello.realm, "support", [othello], acting_user=None)
moderators_system_group = UserGroup.objects.get(
moderators_system_group = NamedUserGroup.objects.get(
realm=iago.realm, name=SystemGroups.MODERATORS, is_system_group=True
)

View File

@ -49,12 +49,12 @@ from zerver.lib.test_helpers import (
from zerver.lib.timestamp import datetime_to_timestamp
from zerver.models import (
Message,
NamedUserGroup,
Realm,
RealmDomain,
Recipient,
Stream,
Subscription,
UserGroup,
UserMessage,
UserProfile,
)
@ -2115,7 +2115,7 @@ class StreamMessagesTest(ZulipTestCase):
leadership = check_add_user_group(othello.realm, "leadership", [othello], acting_user=None)
support = check_add_user_group(othello.realm, "support", [othello], acting_user=None)
moderators_system_group = UserGroup.objects.get(
moderators_system_group = NamedUserGroup.objects.get(
realm=iago.realm, name=SystemGroups.MODERATORS, is_system_group=True
)
@ -2186,7 +2186,7 @@ class StreamMessagesTest(ZulipTestCase):
# Test system bots.
content = "Test mentioning user group @*support*"
members_group = UserGroup.objects.get(
members_group = NamedUserGroup.objects.get(
name=SystemGroups.MEMBERS, realm=iago.realm, is_system_group=True
)
support.can_mention_group = members_group
@ -2200,7 +2200,7 @@ class StreamMessagesTest(ZulipTestCase):
):
self.send_stream_message(system_bot, "test_stream", content, recipient_realm=iago.realm)
everyone_group = UserGroup.objects.get(
everyone_group = NamedUserGroup.objects.get(
name=SystemGroups.EVERYONE, realm=iago.realm, is_system_group=True
)
support.can_mention_group = everyone_group

View File

@ -45,13 +45,13 @@ from zerver.models import (
Attachment,
CustomProfileField,
Message,
NamedUserGroup,
Realm,
RealmAuditLog,
RealmReactivationStatus,
RealmUserDefault,
ScheduledEmail,
Stream,
UserGroup,
UserGroupMembership,
UserMessage,
UserProfile,
@ -935,7 +935,7 @@ class RealmTest(ZulipTestCase):
self.assertEqual(realm.message_visibility_limit, None)
self.assertEqual(realm.upload_quota_gb, None)
members_system_group = UserGroup.objects.get(name=SystemGroups.MEMBERS, realm=realm)
members_system_group = NamedUserGroup.objects.get(name=SystemGroups.MEMBERS, realm=realm)
do_change_realm_permission_group_setting(
realm, "can_access_all_users_group", members_system_group, acting_user=None
)
@ -959,7 +959,7 @@ class RealmTest(ZulipTestCase):
self.assertEqual(
realm.upload_quota_gb, get_seat_count(realm) * settings.UPLOAD_QUOTA_PER_USER_GB
)
everyone_system_group = UserGroup.objects.get(name=SystemGroups.EVERYONE, realm=realm)
everyone_system_group = NamedUserGroup.objects.get(name=SystemGroups.EVERYONE, realm=realm)
self.assertEqual(realm.can_access_all_users_group_id, everyone_system_group.id)
do_set_realm_property(realm, "enable_spectator_access", True, acting_user=None)
@ -1255,7 +1255,7 @@ class RealmTest(ZulipTestCase):
def test_creating_realm_creates_system_groups(self) -> None:
realm = do_create_realm("realm_string_id", "realm name")
system_user_groups = UserGroup.objects.filter(realm=realm, is_system_group=True)
system_user_groups = NamedUserGroup.objects.filter(realm=realm, is_system_group=True)
self.assert_length(system_user_groups, 8)
user_group_names = [group.name for group in system_user_groups]
@ -1309,10 +1309,10 @@ class RealmTest(ZulipTestCase):
def test_changing_waiting_period_updates_system_groups(self) -> None:
realm = get_realm("zulip")
members_system_group = UserGroup.objects.get(
members_system_group = NamedUserGroup.objects.get(
realm=realm, name=SystemGroups.MEMBERS, is_system_group=True
)
full_members_system_group = UserGroup.objects.get(
full_members_system_group = NamedUserGroup.objects.get(
realm=realm, name=SystemGroups.FULL_MEMBERS, is_system_group=True
)
@ -1527,7 +1527,7 @@ class RealmAPITest(ZulipTestCase):
def do_test_realm_permission_group_setting_update_api(self, setting_name: str) -> None:
realm = get_realm("zulip")
all_system_user_groups = UserGroup.objects.filter(
all_system_user_groups = NamedUserGroup.objects.filter(
realm=realm,
is_system_group=True,
)
@ -1572,7 +1572,7 @@ class RealmAPITest(ZulipTestCase):
continue
realm = self.update_with_api(setting_name, user_group.id)
self.assertEqual(getattr(realm, setting_name), user_group)
self.assertEqual(getattr(realm, setting_name), user_group.usergroup_ptr)
def test_update_realm_properties(self) -> None:
for prop in Realm.property_types:
@ -1804,7 +1804,7 @@ class RealmAPITest(ZulipTestCase):
do_change_realm_plan_type(realm, Realm.PLAN_TYPE_LIMITED, acting_user=None)
self.login("iago")
members_group = UserGroup.objects.get(name="role:members", realm=realm)
members_group = NamedUserGroup.objects.get(name="role:members", realm=realm)
req = {"can_access_all_users_group": orjson.dumps(members_group.id).decode()}
result = self.client_patch("/json/realm", req)
self.assert_json_error(result, "Available on Zulip Cloud Plus. Upgrade to access.")

View File

@ -100,6 +100,7 @@ from zerver.models import (
DefaultStream,
DefaultStreamGroup,
Message,
NamedUserGroup,
Realm,
RealmAuditLog,
Recipient,
@ -277,7 +278,7 @@ class TestCreateStreams(ZulipTestCase):
self.assertEqual(events[0]["event"]["streams"][0]["name"], "Private stream")
self.assertEqual(events[0]["event"]["streams"][0]["stream_weekly_traffic"], None)
moderators_system_group = UserGroup.objects.get(
moderators_system_group = NamedUserGroup.objects.get(
name="role:moderators", realm=realm, is_system_group=True
)
new_streams, existing_streams = create_streams_if_needed(
@ -526,10 +527,10 @@ class TestCreateStreams(ZulipTestCase):
user = self.example_user("hamlet")
realm = user.realm
self.login_user(user)
moderators_system_group = UserGroup.objects.get(
moderators_system_group = NamedUserGroup.objects.get(
name="role:moderators", realm=realm, is_system_group=True
)
admins_system_group = UserGroup.objects.get(
admins_system_group = NamedUserGroup.objects.get(
name="role:administrators", realm=realm, is_system_group=True
)
@ -554,7 +555,7 @@ class TestCreateStreams(ZulipTestCase):
stream = get_stream("new_stream2", realm)
self.assertEqual(stream.can_remove_subscribers_group.id, admins_system_group.id)
hamletcharacters_group = UserGroup.objects.get(name="hamletcharacters", realm=realm)
hamletcharacters_group = NamedUserGroup.objects.get(name="hamletcharacters", realm=realm)
post_data = {
"subscriptions": orjson.dumps(
[{"name": "new_stream3", "description": "Third new stream"}]
@ -566,7 +567,7 @@ class TestCreateStreams(ZulipTestCase):
result, "'can_remove_subscribers_group' must be a system user group."
)
internet_group = UserGroup.objects.get(
internet_group = NamedUserGroup.objects.get(
name="role:internet", is_system_group=True, realm=realm
)
post_data = {
@ -581,7 +582,9 @@ class TestCreateStreams(ZulipTestCase):
"'can_remove_subscribers_group' setting cannot be set to 'role:internet' group.",
)
owners_group = UserGroup.objects.get(name="role:owners", is_system_group=True, realm=realm)
owners_group = NamedUserGroup.objects.get(
name="role:owners", is_system_group=True, realm=realm
)
post_data = {
"subscriptions": orjson.dumps(
[{"name": "new_stream3", "description": "Third new stream"}]
@ -594,7 +597,9 @@ class TestCreateStreams(ZulipTestCase):
"'can_remove_subscribers_group' setting cannot be set to 'role:owners' group.",
)
nobody_group = UserGroup.objects.get(name="role:nobody", is_system_group=True, realm=realm)
nobody_group = NamedUserGroup.objects.get(
name="role:nobody", is_system_group=True, realm=realm
)
post_data = {
"subscriptions": orjson.dumps(
[{"name": "new_stream3", "description": "Third new stream"}]
@ -2264,7 +2269,7 @@ class StreamAdminTest(ZulipTestCase):
realm = user_profile.realm
stream = self.subscribe(user_profile, "stream_name1")
moderators_system_group = UserGroup.objects.get(
moderators_system_group = NamedUserGroup.objects.get(
name="role:moderators", realm=realm, is_system_group=True
)
self.login("shiva")
@ -2284,7 +2289,7 @@ class StreamAdminTest(ZulipTestCase):
self.assertEqual(stream.can_remove_subscribers_group.id, moderators_system_group.id)
# This setting can only be set to system groups.
hamletcharacters_group = UserGroup.objects.get(name="hamletcharacters", realm=realm)
hamletcharacters_group = NamedUserGroup.objects.get(name="hamletcharacters", realm=realm)
result = self.client_patch(
f"/json/streams/{stream.id}",
{"can_remove_subscribers_group": orjson.dumps(hamletcharacters_group.id).decode()},
@ -2293,7 +2298,7 @@ class StreamAdminTest(ZulipTestCase):
result, "'can_remove_subscribers_group' must be a system user group."
)
internet_group = UserGroup.objects.get(
internet_group = NamedUserGroup.objects.get(
name="role:internet", is_system_group=True, realm=realm
)
result = self.client_patch(
@ -2305,7 +2310,9 @@ class StreamAdminTest(ZulipTestCase):
"'can_remove_subscribers_group' setting cannot be set to 'role:internet' group.",
)
owners_group = UserGroup.objects.get(name="role:owners", is_system_group=True, realm=realm)
owners_group = NamedUserGroup.objects.get(
name="role:owners", is_system_group=True, realm=realm
)
result = self.client_patch(
f"/json/streams/{stream.id}",
{"can_remove_subscribers_group": orjson.dumps(owners_group.id).decode()},
@ -2315,7 +2322,9 @@ class StreamAdminTest(ZulipTestCase):
"'can_remove_subscribers_group' setting cannot be set to 'role:owners' group.",
)
nobody_group = UserGroup.objects.get(name="role:nobody", is_system_group=True, realm=realm)
nobody_group = NamedUserGroup.objects.get(
name="role:nobody", is_system_group=True, realm=realm
)
result = self.client_patch(
f"/json/streams/{stream.id}",
{"can_remove_subscribers_group": orjson.dumps(nobody_group.id).decode()},

View File

@ -46,23 +46,25 @@ from zerver.models.realms import get_realm
class UserGroupTestCase(ZulipTestCase):
def assert_user_membership(self, user_group: UserGroup, members: Iterable[UserProfile]) -> None:
def assert_user_membership(
self, user_group: NamedUserGroup, members: Iterable[UserProfile]
) -> None:
user_ids = get_user_group_member_ids(user_group, direct_member_only=True)
self.assertSetEqual(set(user_ids), {member.id for member in members})
def assert_subgroup_membership(
self, user_group: UserGroup, members: Iterable[UserGroup]
self, user_group: NamedUserGroup, members: Iterable[UserGroup]
) -> None:
subgroup_ids = get_subgroup_ids(user_group, direct_subgroup_only=True)
self.assertSetEqual(set(subgroup_ids), {member.id for member in members})
def create_user_group_for_test(self, group_name: str) -> UserGroup:
def create_user_group_for_test(self, group_name: str) -> NamedUserGroup:
members = [self.example_user("othello")]
return check_add_user_group(get_realm("zulip"), group_name, members, acting_user=None)
def test_user_groups_in_realm_serialized(self) -> None:
realm = get_realm("zulip")
user_group = UserGroup.objects.filter(realm=realm).first()
user_group = NamedUserGroup.objects.filter(realm=realm).first()
assert user_group is not None
empty_user_group = check_add_user_group(realm, "newgroup", [], acting_user=None)
@ -74,7 +76,7 @@ class UserGroupTestCase(ZulipTestCase):
self.assertEqual(user_groups[0]["members"], [])
self.assertEqual(user_groups[0]["direct_subgroup_ids"], [])
owners_system_group = UserGroup.objects.get(name=SystemGroups.OWNERS, realm=realm)
owners_system_group = NamedUserGroup.objects.get(name=SystemGroups.OWNERS, realm=realm)
membership = UserGroupMembership.objects.filter(user_group=owners_system_group).values_list(
"user_profile_id", flat=True
)
@ -84,7 +86,9 @@ class UserGroupTestCase(ZulipTestCase):
self.assertEqual(set(user_groups[1]["members"]), set(membership))
self.assertEqual(user_groups[1]["direct_subgroup_ids"], [])
admins_system_group = UserGroup.objects.get(name=SystemGroups.ADMINISTRATORS, realm=realm)
admins_system_group = NamedUserGroup.objects.get(
name=SystemGroups.ADMINISTRATORS, realm=realm
)
self.assertEqual(user_groups[2]["id"], admins_system_group.id)
# Check that owners system group is present in "direct_subgroup_ids"
self.assertEqual(user_groups[2]["direct_subgroup_ids"], [owners_system_group.id])
@ -226,10 +230,10 @@ class UserGroupTestCase(ZulipTestCase):
iago = self.example_user("iago")
hamlet = self.example_user("hamlet")
moderators_group = UserGroup.objects.get(
moderators_group = NamedUserGroup.objects.get(
name=SystemGroups.MODERATORS, realm=realm, is_system_group=True
)
administrators_group = UserGroup.objects.get(
administrators_group = NamedUserGroup.objects.get(
name=SystemGroups.ADMINISTRATORS, realm=realm, is_system_group=True
)
@ -247,7 +251,7 @@ class UserGroupTestCase(ZulipTestCase):
iago = self.example_user("iago")
zulip_realm = get_realm("zulip")
zulip_group = check_add_user_group(zulip_realm, "zulip", [], acting_user=None)
moderators_group = UserGroup.objects.get(
moderators_group = NamedUserGroup.objects.get(
name=SystemGroups.MODERATORS, realm=zulip_realm, is_system_group=True
)
@ -274,14 +278,14 @@ class UserGroupAPITestCase(UserGroupTestCase):
}
result = self.client_post("/json/user_groups/create", info=params)
self.assert_json_success(result)
self.assert_length(UserGroup.objects.filter(realm=hamlet.realm), 10)
self.assert_length(NamedUserGroup.objects.filter(realm=hamlet.realm), 10)
# Check default value of can_mention_group setting.
everyone_system_group = UserGroup.objects.get(
everyone_system_group = NamedUserGroup.objects.get(
name="role:everyone", realm=hamlet.realm, is_system_group=True
)
support_group = UserGroup.objects.get(name="support", realm=hamlet.realm)
self.assertEqual(support_group.can_mention_group, everyone_system_group)
support_group = NamedUserGroup.objects.get(name="support", realm=hamlet.realm)
self.assertEqual(support_group.can_mention_group, everyone_system_group.usergroup_ptr)
# Test invalid member error
params = {
@ -291,7 +295,7 @@ class UserGroupAPITestCase(UserGroupTestCase):
}
result = self.client_post("/json/user_groups/create", info=params)
self.assert_json_error(result, "Invalid user ID: 1111")
self.assert_length(UserGroup.objects.filter(realm=hamlet.realm), 10)
self.assert_length(NamedUserGroup.objects.filter(realm=hamlet.realm), 10)
# Test we cannot create group with same name again
params = {
@ -301,7 +305,7 @@ class UserGroupAPITestCase(UserGroupTestCase):
}
result = self.client_post("/json/user_groups/create", info=params)
self.assert_json_error(result, "User group 'support' already exists.")
self.assert_length(UserGroup.objects.filter(realm=hamlet.realm), 10)
self.assert_length(NamedUserGroup.objects.filter(realm=hamlet.realm), 10)
# Test we cannot create group with same name again
params = {
@ -311,7 +315,7 @@ class UserGroupAPITestCase(UserGroupTestCase):
}
result = self.client_post("/json/user_groups/create", info=params)
self.assert_json_error(result, "User group name cannot exceed 100 characters.")
self.assert_length(UserGroup.objects.filter(realm=hamlet.realm), 10)
self.assert_length(NamedUserGroup.objects.filter(realm=hamlet.realm), 10)
# Test emtpty group name.
params = {
@ -321,7 +325,7 @@ class UserGroupAPITestCase(UserGroupTestCase):
}
result = self.client_post("/json/user_groups/create", info=params)
self.assert_json_error(result, "User group name can't be empty!")
self.assert_length(UserGroup.objects.filter(realm=hamlet.realm), 10)
self.assert_length(NamedUserGroup.objects.filter(realm=hamlet.realm), 10)
# Test invalid prefixes for user group name.
params = {
@ -331,27 +335,27 @@ class UserGroupAPITestCase(UserGroupTestCase):
}
result = self.client_post("/json/user_groups/create", info=params)
self.assert_json_error(result, "User group name cannot start with '@'.")
self.assert_length(UserGroup.objects.filter(realm=hamlet.realm), 10)
self.assert_length(NamedUserGroup.objects.filter(realm=hamlet.realm), 10)
params["name"] = "role:manager"
result = self.client_post("/json/user_groups/create", info=params)
self.assert_json_error(result, "User group name cannot start with 'role:'.")
self.assert_length(UserGroup.objects.filter(realm=hamlet.realm), 10)
self.assert_length(NamedUserGroup.objects.filter(realm=hamlet.realm), 10)
params["name"] = "user:1"
result = self.client_post("/json/user_groups/create", info=params)
self.assert_json_error(result, "User group name cannot start with 'user:'.")
self.assert_length(UserGroup.objects.filter(realm=hamlet.realm), 10)
self.assert_length(NamedUserGroup.objects.filter(realm=hamlet.realm), 10)
params["name"] = "stream:1"
result = self.client_post("/json/user_groups/create", info=params)
self.assert_json_error(result, "User group name cannot start with 'stream:'.")
self.assert_length(UserGroup.objects.filter(realm=hamlet.realm), 10)
self.assert_length(NamedUserGroup.objects.filter(realm=hamlet.realm), 10)
params["name"] = "channel:1"
result = self.client_post("/json/user_groups/create", info=params)
self.assert_json_error(result, "User group name cannot start with 'channel:'.")
self.assert_length(UserGroup.objects.filter(realm=hamlet.realm), 10)
self.assert_length(NamedUserGroup.objects.filter(realm=hamlet.realm), 10)
def test_can_mention_group_setting_during_user_group_creation(self) -> None:
self.login("hamlet")
@ -359,7 +363,7 @@ class UserGroupAPITestCase(UserGroupTestCase):
leadership_group = check_add_user_group(
hamlet.realm, "leadership", [hamlet], acting_user=None
)
moderators_group = UserGroup.objects.get(
moderators_group = NamedUserGroup.objects.get(
name="role:moderators", realm=hamlet.realm, is_system_group=True
)
params = {
@ -370,8 +374,8 @@ class UserGroupAPITestCase(UserGroupTestCase):
}
result = self.client_post("/json/user_groups/create", info=params)
self.assert_json_success(result)
support_group = UserGroup.objects.get(name="support", realm=hamlet.realm)
self.assertEqual(support_group.can_mention_group, moderators_group)
support_group = NamedUserGroup.objects.get(name="support", realm=hamlet.realm)
self.assertEqual(support_group.can_mention_group, moderators_group.usergroup_ptr)
params = {
"name": "test",
@ -381,10 +385,10 @@ class UserGroupAPITestCase(UserGroupTestCase):
}
result = self.client_post("/json/user_groups/create", info=params)
self.assert_json_success(result)
test_group = UserGroup.objects.get(name="test", realm=hamlet.realm)
test_group = NamedUserGroup.objects.get(name="test", realm=hamlet.realm)
self.assertEqual(test_group.can_mention_group, leadership_group.usergroup_ptr)
nobody_group = UserGroup.objects.get(
nobody_group = NamedUserGroup.objects.get(
name="role:nobody", realm=hamlet.realm, is_system_group=True
)
params = {
@ -395,10 +399,10 @@ class UserGroupAPITestCase(UserGroupTestCase):
}
result = self.client_post("/json/user_groups/create", info=params)
self.assert_json_success(result)
marketing_group = UserGroup.objects.get(name="marketing", realm=hamlet.realm)
self.assertEqual(marketing_group.can_mention_group, nobody_group)
marketing_group = NamedUserGroup.objects.get(name="marketing", realm=hamlet.realm)
self.assertEqual(marketing_group.can_mention_group, nobody_group.usergroup_ptr)
internet_group = UserGroup.objects.get(
internet_group = NamedUserGroup.objects.get(
name="role:internet", realm=hamlet.realm, is_system_group=True
)
params = {
@ -412,7 +416,7 @@ class UserGroupAPITestCase(UserGroupTestCase):
result, "'can_mention_group' setting cannot be set to 'role:internet' group."
)
owners_group = UserGroup.objects.get(
owners_group = NamedUserGroup.objects.get(
name="role:owners", realm=hamlet.realm, is_system_group=True
)
params = {
@ -442,7 +446,8 @@ class UserGroupAPITestCase(UserGroupTestCase):
result = self.client_get("/json/user_groups")
response_dict = self.assert_json_success(result)
self.assert_length(
response_dict["user_groups"], UserGroup.objects.filter(realm=user_profile.realm).count()
response_dict["user_groups"],
NamedUserGroup.objects.filter(realm=user_profile.realm).count(),
)
def test_can_edit_user_groups(self) -> None:
@ -460,7 +465,7 @@ class UserGroupAPITestCase(UserGroupTestCase):
"description": "Support team",
}
self.client_post("/json/user_groups/create", info=params)
user_group = UserGroup.objects.get(name="support")
user_group = NamedUserGroup.objects.get(name="support")
# Test success
params = {
"name": "help",
@ -468,7 +473,7 @@ class UserGroupAPITestCase(UserGroupTestCase):
}
result = self.client_patch(f"/json/user_groups/{user_group.id}", info=params)
self.assert_json_success(result)
user_group = UserGroup.objects.get(id=user_group.id)
user_group = NamedUserGroup.objects.get(id=user_group.id)
self.assertEqual(user_group.name, "help")
self.assertEqual(user_group.description, "Troubleshooting team")
@ -480,7 +485,7 @@ class UserGroupAPITestCase(UserGroupTestCase):
params = {"name": "help team"}
result = self.client_patch(f"/json/user_groups/{user_group.id}", info=params)
self.assert_json_success(result)
user_group = UserGroup.objects.get(id=user_group.id)
user_group = NamedUserGroup.objects.get(id=user_group.id)
self.assertEqual(user_group.name, "help team")
self.assertEqual(user_group.description, "Troubleshooting team")
@ -533,7 +538,7 @@ class UserGroupAPITestCase(UserGroupTestCase):
hamlet.realm, "marketing", [hamlet], acting_user=None
)
moderators_group = UserGroup.objects.get(
moderators_group = NamedUserGroup.objects.get(
name="role:moderators", realm=hamlet.realm, is_system_group=True
)
@ -543,18 +548,18 @@ class UserGroupAPITestCase(UserGroupTestCase):
}
result = self.client_patch(f"/json/user_groups/{support_group.id}", info=params)
self.assert_json_success(result)
support_group = UserGroup.objects.get(name="support", realm=hamlet.realm)
self.assertEqual(support_group.can_mention_group, moderators_group)
support_group = NamedUserGroup.objects.get(name="support", realm=hamlet.realm)
self.assertEqual(support_group.can_mention_group, moderators_group.usergroup_ptr)
params = {
"can_mention_group": orjson.dumps(marketing_group.id).decode(),
}
result = self.client_patch(f"/json/user_groups/{support_group.id}", info=params)
self.assert_json_success(result)
support_group = UserGroup.objects.get(name="support", realm=hamlet.realm)
support_group = NamedUserGroup.objects.get(name="support", realm=hamlet.realm)
self.assertEqual(support_group.can_mention_group, marketing_group.usergroup_ptr)
nobody_group = UserGroup.objects.get(
nobody_group = NamedUserGroup.objects.get(
name="role:nobody", realm=hamlet.realm, is_system_group=True
)
params = {
@ -562,10 +567,10 @@ class UserGroupAPITestCase(UserGroupTestCase):
}
result = self.client_patch(f"/json/user_groups/{support_group.id}", info=params)
self.assert_json_success(result)
support_group = UserGroup.objects.get(name="support", realm=hamlet.realm)
self.assertEqual(support_group.can_mention_group, nobody_group)
support_group = NamedUserGroup.objects.get(name="support", realm=hamlet.realm)
self.assertEqual(support_group.can_mention_group, nobody_group.usergroup_ptr)
owners_group = UserGroup.objects.get(
owners_group = NamedUserGroup.objects.get(
name="role:owners", realm=hamlet.realm, is_system_group=True
)
params = {
@ -576,7 +581,7 @@ class UserGroupAPITestCase(UserGroupTestCase):
result, "'can_mention_group' setting cannot be set to 'role:owners' group."
)
internet_group = UserGroup.objects.get(
internet_group = NamedUserGroup.objects.get(
name="role:internet", realm=hamlet.realm, is_system_group=True
)
params = {
@ -615,16 +620,16 @@ class UserGroupAPITestCase(UserGroupTestCase):
"description": "Support team",
}
self.client_post("/json/user_groups/create", info=params)
user_group = UserGroup.objects.get(name="support")
user_group = NamedUserGroup.objects.get(name="support")
# Test success
self.assertEqual(UserGroup.objects.filter(realm=hamlet.realm).count(), 10)
self.assertEqual(NamedUserGroup.objects.filter(realm=hamlet.realm).count(), 10)
self.assertEqual(UserGroupMembership.objects.count(), 45)
self.assertTrue(UserGroup.objects.filter(id=user_group.id).exists())
self.assertTrue(NamedUserGroup.objects.filter(id=user_group.id).exists())
result = self.client_delete(f"/json/user_groups/{user_group.id}")
self.assert_json_success(result)
self.assertEqual(UserGroup.objects.filter(realm=hamlet.realm).count(), 9)
self.assertEqual(NamedUserGroup.objects.filter(realm=hamlet.realm).count(), 9)
self.assertEqual(UserGroupMembership.objects.count(), 44)
self.assertFalse(UserGroup.objects.filter(id=user_group.id).exists())
self.assertFalse(NamedUserGroup.objects.filter(id=user_group.id).exists())
# Test when invalid user group is supplied; transaction needed for
# error handling
with transaction.atomic():
@ -699,7 +704,7 @@ class UserGroupAPITestCase(UserGroupTestCase):
"description": "Support team",
}
self.client_post("/json/user_groups/create", info=params)
user_group = UserGroup.objects.get(name="support")
user_group = NamedUserGroup.objects.get(name="support")
# Test add members
self.assert_user_membership(user_group, [hamlet])
@ -844,18 +849,18 @@ class UserGroupAPITestCase(UserGroupTestCase):
if error_msg is None:
self.assert_json_success(result)
# One group already exists in the test database.
self.assert_length(UserGroup.objects.filter(realm=realm), 10)
self.assert_length(NamedUserGroup.objects.filter(realm=realm), 10)
else:
self.assert_json_error(result, error_msg)
def check_delete_user_group(acting_user: str, error_msg: Optional[str] = None) -> None:
self.login(acting_user)
user_group = UserGroup.objects.get(name="support")
user_group = NamedUserGroup.objects.get(name="support")
with transaction.atomic():
result = self.client_delete(f"/json/user_groups/{user_group.id}")
if error_msg is None:
self.assert_json_success(result)
self.assert_length(UserGroup.objects.filter(realm=realm), 9)
self.assert_length(NamedUserGroup.objects.filter(realm=realm), 9)
else:
self.assert_json_error(result, error_msg)
@ -940,7 +945,7 @@ class UserGroupAPITestCase(UserGroupTestCase):
"description": "Support team",
}
self.client_post("/json/user_groups/create", info=params)
user_group = UserGroup.objects.get(name="support")
user_group = NamedUserGroup.objects.get(name="support")
def check_update_user_group(
new_name: str,
@ -1149,7 +1154,7 @@ class UserGroupAPITestCase(UserGroupTestCase):
othello = self.example_user("othello")
aaron = self.example_user("aaron")
user_group = UserGroup.objects.get(
user_group = NamedUserGroup.objects.get(
realm=iago.realm, name=SystemGroups.FULL_MEMBERS, is_system_group=True
)
@ -1186,7 +1191,7 @@ class UserGroupAPITestCase(UserGroupTestCase):
hamlet.save()
do_set_realm_property(realm, "waiting_period_threshold", 10, acting_user=None)
full_members_group = UserGroup.objects.get(
full_members_group = NamedUserGroup.objects.get(
realm=realm, name=SystemGroups.FULL_MEMBERS, is_system_group=True
)
@ -1345,7 +1350,7 @@ class UserGroupAPITestCase(UserGroupTestCase):
desdemona = self.example_user("desdemona")
iago = self.example_user("iago")
othello = self.example_user("othello")
admins_group = UserGroup.objects.get(
admins_group = NamedUserGroup.objects.get(
realm=realm, name=SystemGroups.ADMINISTRATORS, is_system_group=True
)
@ -1411,7 +1416,7 @@ class UserGroupAPITestCase(UserGroupTestCase):
iago = self.example_user("iago")
desdemona = self.example_user("desdemona")
shiva = self.example_user("shiva")
moderators_group = UserGroup.objects.get(
moderators_group = NamedUserGroup.objects.get(
name=SystemGroups.MODERATORS, realm=realm, is_system_group=True
)
self.login("iago")
@ -1453,13 +1458,13 @@ class UserGroupAPITestCase(UserGroupTestCase):
def test_get_subgroups_of_user_group(self) -> None:
realm = get_realm("zulip")
owners_group = UserGroup.objects.get(
owners_group = NamedUserGroup.objects.get(
name=SystemGroups.OWNERS, realm=realm, is_system_group=True
)
admins_group = UserGroup.objects.get(
admins_group = NamedUserGroup.objects.get(
name=SystemGroups.ADMINISTRATORS, realm=realm, is_system_group=True
)
moderators_group = UserGroup.objects.get(
moderators_group = NamedUserGroup.objects.get(
name=SystemGroups.MODERATORS, realm=realm, is_system_group=True
)
self.login("iago")