user_groups: Track acting user for user group creation.

This is a prep-commit for populating RealmAuditLogs for changes made to
UserGroup.

Signed-off-by: Zixuan James Li <p359101898@gmail.com>
This commit is contained in:
Zixuan James Li 2022-11-20 18:37:11 -08:00 committed by Tim Abbott
parent a1bc9adefc
commit b3aba796f1
12 changed files with 72 additions and 36 deletions

View File

@ -1,5 +1,5 @@
import datetime
from typing import Dict, List, Sequence, TypedDict
from typing import Dict, List, Optional, Sequence, TypedDict
import django.db.utils
from django.db import transaction
@ -113,10 +113,17 @@ def do_send_create_user_group_event(
def check_add_user_group(
realm: Realm, name: str, initial_members: List[UserProfile], description: str
realm: Realm,
name: str,
initial_members: List[UserProfile],
description: str,
*,
acting_user: Optional[UserProfile],
) -> None:
try:
user_group = create_user_group(name, initial_members, realm, description=description)
user_group = create_user_group(
name, initial_members, realm, description=description, acting_user=acting_user
)
do_send_create_user_group_event(user_group, initial_members)
except django.db.utils.IntegrityError:
raise JsonableError(_("User group '{}' already exists.").format(name))

View File

@ -1,4 +1,4 @@
from typing import Dict, Iterable, List, Sequence, TypedDict
from typing import Dict, Iterable, List, Optional, Sequence, TypedDict
from django.db import transaction
from django.db.models import QuerySet
@ -104,6 +104,7 @@ def create_user_group(
members: List[UserProfile],
realm: Realm,
*,
acting_user: Optional[UserProfile],
description: str = "",
is_system_group: bool = False,
) -> UserGroup:

View File

@ -821,9 +821,11 @@ class TestMissedMessages(ZulipTestCase):
othello = self.example_user("othello")
cordelia = self.example_user("cordelia")
hamlet_only = create_user_group("hamlet_only", [hamlet], get_realm("zulip"))
hamlet_only = create_user_group(
"hamlet_only", [hamlet], get_realm("zulip"), acting_user=None
)
hamlet_and_cordelia = create_user_group(
"hamlet_and_cordelia", [hamlet, cordelia], get_realm("zulip")
"hamlet_and_cordelia", [hamlet, cordelia], get_realm("zulip"), acting_user=None
)
hamlet_only_message_id = self.send_stream_message(othello, "Denmark", "@*hamlet_only*")
@ -861,7 +863,7 @@ class TestMissedMessages(ZulipTestCase):
othello = self.example_user("othello")
hamlet_and_cordelia = create_user_group(
"hamlet_and_cordelia", [hamlet, cordelia], get_realm("zulip")
"hamlet_and_cordelia", [hamlet, cordelia], get_realm("zulip"), acting_user=None
)
user_group_mentioned_message_id = self.send_stream_message(
@ -901,7 +903,7 @@ class TestMissedMessages(ZulipTestCase):
othello = self.example_user("othello")
hamlet_and_cordelia = create_user_group(
"hamlet_and_cordelia", [hamlet, cordelia], get_realm("zulip")
"hamlet_and_cordelia", [hamlet, cordelia], get_realm("zulip"), acting_user=None
)
wildcard_mentioned_message_id = self.send_stream_message(othello, "Denmark", "@**all**")
@ -1545,7 +1547,7 @@ class TestMissedMessages(ZulipTestCase):
othello = self.example_user("othello")
cordelia = self.example_user("cordelia")
large_user_group = create_user_group(
"large_user_group", [hamlet, othello, cordelia], get_realm("zulip")
"large_user_group", [hamlet, othello, cordelia], get_realm("zulip"), acting_user=None
)
# Do note that the event dicts for the missed messages are constructed by hand

View File

@ -430,7 +430,7 @@ class MissedMessageNotificationsTest(ZulipTestCase):
# Test with a user group mention
hamlet_and_cordelia = create_user_group(
"hamlet_and_cordelia", [user_profile, cordelia], cordelia.realm
"hamlet_and_cordelia", [user_profile, cordelia], cordelia.realm, acting_user=None
)
client_descriptor = allocate_event_queue(user_profile)
self.assertTrue(client_descriptor.event_queue.empty())

View File

@ -1279,7 +1279,7 @@ class NormalActionsTest(BaseAction):
othello = self.example_user("othello")
events = self.verify_action(
lambda: check_add_user_group(
self.user_profile.realm, "backend", [othello], "Backend team"
self.user_profile.realm, "backend", [othello], "Backend team", acting_user=None
)
)
check_user_group_add("events[0]", events[0])
@ -1305,7 +1305,7 @@ class NormalActionsTest(BaseAction):
check_user_group_remove_members("events[0]", events[0])
api_design = create_user_group(
"api-design", [hamlet], hamlet.realm, description="API design team"
"api-design", [hamlet], hamlet.realm, description="API design team", acting_user=None
)
# Test add subgroups

View File

@ -2265,7 +2265,7 @@ class MarkdownTest(ZulipTestCase):
def create_user_group_for_test(self, user_group_name: str) -> UserGroup:
othello = self.example_user("othello")
return create_user_group(user_group_name, [othello], get_realm("zulip"))
return create_user_group(user_group_name, [othello], get_realm("zulip"), acting_user=None)
def test_user_group_mention_single(self) -> None:
sender_user_profile = self.example_user("othello")

View File

@ -240,8 +240,10 @@ class TestNotificationData(ZulipTestCase):
cordelia = self.example_user("cordelia")
realm = hamlet.realm
hamlet_only = create_user_group("hamlet_only", [hamlet], realm)
hamlet_and_cordelia = create_user_group("hamlet_and_cordelia", [hamlet, cordelia], realm)
hamlet_only = create_user_group("hamlet_only", [hamlet], realm, acting_user=None)
hamlet_and_cordelia = create_user_group(
"hamlet_and_cordelia", [hamlet, cordelia], realm, acting_user=None
)
mention_backend = MentionBackend(realm.id)

View File

@ -1504,7 +1504,10 @@ class HandlePushNotificationTest(PushNotificationTest):
othello = self.example_user("othello")
cordelia = self.example_user("cordelia")
large_user_group = create_user_group(
"large_user_group", [self.user_profile, othello, cordelia], get_realm("zulip")
"large_user_group",
[self.user_profile, othello, cordelia],
get_realm("zulip"),
acting_user=None,
)
# Personal mention in a stream message should soft reactivate the user
@ -1873,7 +1876,9 @@ class TestGetAPNsPayload(PushNotificationTest):
def test_get_message_payload_apns_user_group_mention(self) -> None:
user_profile = self.example_user("othello")
user_group = create_user_group("test_user_group", [user_profile], get_realm("zulip"))
user_group = create_user_group(
"test_user_group", [user_profile], get_realm("zulip"), acting_user=None
)
stream = Stream.objects.filter(name="Verona").get()
message = self.get_message(Recipient.STREAM, stream.id, stream.realm_id)
payload = get_message_payload_apns(

View File

@ -2417,9 +2417,14 @@ class StreamAdminTest(ZulipTestCase):
def test_can_remove_subscribers_group(self) -> None:
realm = get_realm("zulip")
leadership_group = create_user_group(
"leadership", [self.example_user("iago"), self.example_user("shiva")], realm
"leadership",
[self.example_user("iago"), self.example_user("shiva")],
realm,
acting_user=None,
)
managers_group = create_user_group(
"managers", [self.example_user("hamlet")], realm=realm, acting_user=None
)
managers_group = create_user_group("managers", [self.example_user("hamlet")], realm=realm)
add_subgroups_to_user_group(managers_group, [leadership_group])
cordelia = self.example_user("cordelia")

View File

@ -35,7 +35,7 @@ class UserGroupTestCase(ZulipTestCase):
self, group_name: str, realm: Realm = get_realm("zulip")
) -> UserGroup:
members = [self.example_user("othello")]
return create_user_group(group_name, members, realm)
return create_user_group(group_name, members, realm, acting_user=None)
def test_user_groups_in_realm_serialized(self) -> None:
realm = get_realm("zulip")
@ -44,7 +44,7 @@ class UserGroupTestCase(ZulipTestCase):
membership = UserGroupMembership.objects.filter(user_group=user_group).values_list(
"user_profile_id", flat=True
)
empty_user_group = create_user_group("newgroup", [], realm)
empty_user_group = create_user_group("newgroup", [], realm, acting_user=None)
user_groups = user_groups_in_realm_serialized(realm)
self.assert_length(user_groups, 9)
@ -289,7 +289,9 @@ class UserGroupAPITestCase(UserGroupTestCase):
self.assert_json_error(result, "Invalid user group")
lear_realm = get_realm("lear")
lear_test_group = create_user_group("test", [self.lear_user("cordelia")], lear_realm)
lear_test_group = create_user_group(
"test", [self.lear_user("cordelia")], lear_realm, acting_user=None
)
result = self.client_patch(f"/json/user_groups/{lear_test_group.id}", info=params)
self.assert_json_error(result, "Invalid user group")
@ -297,8 +299,8 @@ class UserGroupAPITestCase(UserGroupTestCase):
hamlet = self.example_user("hamlet")
self.login_user(hamlet)
realm = get_realm("zulip")
support_user_group = create_user_group("support", [hamlet], realm)
marketing_user_group = create_user_group("marketing", [hamlet], realm)
support_user_group = create_user_group("support", [hamlet], realm, acting_user=None)
marketing_user_group = create_user_group("marketing", [hamlet], realm, acting_user=None)
params = {
"name": marketing_user_group.name,
@ -328,7 +330,9 @@ class UserGroupAPITestCase(UserGroupTestCase):
self.assert_json_error(result, "Invalid user group")
lear_realm = get_realm("lear")
lear_test_group = create_user_group("test", [self.lear_user("cordelia")], lear_realm)
lear_test_group = create_user_group(
"test", [self.lear_user("cordelia")], lear_realm, acting_user=None
)
result = self.client_delete(f"/json/user_groups/{lear_test_group.id}")
self.assert_json_error(result, "Invalid user group")
@ -411,9 +415,7 @@ class UserGroupAPITestCase(UserGroupTestCase):
self.subscribe(user, stream_name)
create_user_group(
name=group_name,
members=list(support_team),
realm=realm,
name=group_name, members=list(support_team), realm=realm, acting_user=None
)
payload = dict(
@ -831,8 +833,10 @@ class UserGroupAPITestCase(UserGroupTestCase):
hamlet = self.example_user("hamlet")
othello = self.example_user("othello")
leadership_group = create_user_group("leadership", [desdemona, iago, hamlet], realm)
support_group = create_user_group("support", [hamlet, othello], realm)
leadership_group = create_user_group(
"leadership", [desdemona, iago, hamlet], realm, acting_user=None
)
support_group = create_user_group("support", [hamlet, othello], realm, acting_user=None)
self.login("cordelia")
# Non-admin and non-moderators who are not a member of group cannot add or remove subgroups.
@ -897,7 +901,9 @@ class UserGroupAPITestCase(UserGroupTestCase):
)
lear_realm = get_realm("lear")
lear_test_group = create_user_group("test", [self.lear_user("cordelia")], lear_realm)
lear_test_group = create_user_group(
"test", [self.lear_user("cordelia")], lear_realm, acting_user=None
)
result = self.client_post(f"/json/user_groups/{lear_test_group.id}/subgroups", info=params)
self.assert_json_error(result, "Invalid user group")
@ -930,7 +936,7 @@ class UserGroupAPITestCase(UserGroupTestCase):
lear_realm = get_realm("lear")
lear_cordelia = self.lear_user("cordelia")
lear_test_group = create_user_group("test", [lear_cordelia], lear_realm)
lear_test_group = create_user_group("test", [lear_cordelia], lear_realm, acting_user=None)
result = self.client_get(
f"/json/user_groups/{lear_test_group.id}/members/{lear_cordelia.id}"
)
@ -990,7 +996,9 @@ class UserGroupAPITestCase(UserGroupTestCase):
self.assert_json_error(result, "Invalid user group")
lear_realm = get_realm("lear")
lear_test_group = create_user_group("test", [self.lear_user("cordelia")], lear_realm)
lear_test_group = create_user_group(
"test", [self.lear_user("cordelia")], lear_realm, acting_user=None
)
result = self.client_get(f"/json/user_groups/{lear_test_group.id}/members")
self.assert_json_error(result, "Invalid user group")
@ -1036,7 +1044,9 @@ class UserGroupAPITestCase(UserGroupTestCase):
self.assert_json_error(result, "Invalid user group")
lear_realm = get_realm("lear")
lear_test_group = create_user_group("test", [self.lear_user("cordelia")], lear_realm)
lear_test_group = create_user_group(
"test", [self.lear_user("cordelia")], lear_realm, acting_user=None
)
result = self.client_get(f"/json/user_groups/{lear_test_group.id}/subgroups")
self.assert_json_error(result, "Invalid user group")

View File

@ -43,7 +43,9 @@ def add_user_group(
description: str = REQ(),
) -> HttpResponse:
user_profiles = user_ids_to_users(members, user_profile.realm)
check_add_user_group(user_profile.realm, name, user_profiles, description)
check_add_user_group(
user_profile.realm, name, user_profiles, description, acting_user=user_profile
)
return json_success(request)

View File

@ -1237,4 +1237,6 @@ def create_user_groups() -> None:
get_user_by_delivery_email("cordelia@zulip.com", zulip),
get_user_by_delivery_email("hamlet@zulip.com", zulip),
]
create_user_group("hamletcharacters", members, zulip, description="Characters of Hamlet")
create_user_group(
"hamletcharacters", members, zulip, description="Characters of Hamlet", acting_user=None
)