2021-05-21 07:02:43 +02:00
|
|
|
from datetime import timedelta
|
|
|
|
from typing import Optional
|
2022-01-31 18:24:00 +01:00
|
|
|
from unittest import mock
|
2017-09-25 09:47:15 +02:00
|
|
|
|
2020-08-07 01:09:47 +02:00
|
|
|
import orjson
|
2021-05-21 07:02:43 +02:00
|
|
|
from django.utils.timezone import now as timezone_now
|
2018-08-14 21:37:52 +02:00
|
|
|
|
2022-04-14 23:57:15 +02:00
|
|
|
from zerver.actions.realm_settings import do_set_realm_property
|
2022-04-14 23:30:17 +02:00
|
|
|
from zerver.actions.user_groups import promote_new_full_members
|
2022-04-14 23:42:50 +02:00
|
|
|
from zerver.lib.streams import ensure_stream
|
2017-09-25 09:47:15 +02:00
|
|
|
from zerver.lib.test_classes import ZulipTestCase
|
2020-06-11 00:54:34 +02:00
|
|
|
from zerver.lib.test_helpers import most_recent_usermessage
|
2017-09-25 09:47:15 +02:00
|
|
|
from zerver.lib.user_groups import (
|
|
|
|
create_user_group,
|
2021-10-09 20:02:39 +02:00
|
|
|
get_direct_memberships_of_users,
|
2021-10-09 19:53:03 +02:00
|
|
|
get_direct_user_groups,
|
2021-09-29 02:46:57 +02:00
|
|
|
get_recursive_group_members,
|
|
|
|
get_recursive_membership_groups,
|
|
|
|
get_recursive_subgroups,
|
2017-11-07 07:56:26 +01:00
|
|
|
user_groups_in_realm_serialized,
|
2017-09-25 09:47:15 +02:00
|
|
|
)
|
2021-09-29 02:46:57 +02:00
|
|
|
from zerver.models import (
|
|
|
|
GroupGroupMembership,
|
|
|
|
Realm,
|
|
|
|
UserGroup,
|
|
|
|
UserGroupMembership,
|
|
|
|
UserProfile,
|
|
|
|
get_realm,
|
|
|
|
)
|
2020-06-11 00:54:34 +02:00
|
|
|
|
2017-09-25 09:47:15 +02:00
|
|
|
|
|
|
|
class UserGroupTestCase(ZulipTestCase):
|
2021-02-12 08:19:30 +01:00
|
|
|
def create_user_group_for_test(
|
2021-02-12 08:20:45 +01:00
|
|
|
self, group_name: str, realm: Realm = get_realm("zulip")
|
2021-02-12 08:19:30 +01:00
|
|
|
) -> UserGroup:
|
2021-02-12 08:20:45 +01:00
|
|
|
members = [self.example_user("othello")]
|
2017-09-25 09:47:15 +02:00
|
|
|
return create_user_group(group_name, members, realm)
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_user_groups_in_realm_serialized(self) -> None:
|
2021-02-12 08:20:45 +01:00
|
|
|
realm = get_realm("zulip")
|
2017-11-13 07:49:01 +01:00
|
|
|
user_group = UserGroup.objects.first()
|
2021-07-24 16:56:39 +02:00
|
|
|
assert user_group is not None
|
2017-11-13 07:49:01 +01:00
|
|
|
membership = UserGroupMembership.objects.filter(user_group=user_group)
|
2021-02-12 08:20:45 +01:00
|
|
|
membership = membership.values_list("user_profile_id", flat=True)
|
|
|
|
empty_user_group = create_user_group("newgroup", [], realm)
|
2017-11-30 01:09:23 +01:00
|
|
|
|
2017-11-13 07:49:01 +01:00
|
|
|
user_groups = user_groups_in_realm_serialized(realm)
|
2021-08-11 15:10:17 +02:00
|
|
|
self.assert_length(user_groups, 9)
|
2021-02-12 08:20:45 +01:00
|
|
|
self.assertEqual(user_groups[0]["id"], user_group.id)
|
2021-08-11 15:10:17 +02:00
|
|
|
self.assertEqual(user_groups[0]["name"], "@role:owners")
|
|
|
|
self.assertEqual(user_groups[0]["description"], "Owners of this organization")
|
2021-02-12 08:20:45 +01:00
|
|
|
self.assertEqual(set(user_groups[0]["members"]), set(membership))
|
2022-02-28 11:50:33 +01:00
|
|
|
self.assertEqual(user_groups[0]["subgroups"], [])
|
|
|
|
|
|
|
|
admins_system_group = UserGroup.objects.get(name="@role:administrators", realm=realm)
|
|
|
|
self.assertEqual(user_groups[1]["id"], admins_system_group.id)
|
|
|
|
# Check that owners system group is present in "subgroups"
|
|
|
|
self.assertEqual(user_groups[1]["subgroups"], [user_group.id])
|
2017-11-07 07:56:26 +01:00
|
|
|
|
2021-08-11 15:10:17 +02:00
|
|
|
self.assertEqual(user_groups[8]["id"], empty_user_group.id)
|
|
|
|
self.assertEqual(user_groups[8]["name"], "newgroup")
|
|
|
|
self.assertEqual(user_groups[8]["description"], "")
|
|
|
|
self.assertEqual(user_groups[8]["members"], [])
|
2017-11-30 01:09:23 +01:00
|
|
|
|
2021-10-09 19:53:03 +02:00
|
|
|
def test_get_direct_user_groups(self) -> None:
|
2021-02-12 08:20:45 +01:00
|
|
|
othello = self.example_user("othello")
|
|
|
|
self.create_user_group_for_test("support")
|
2021-10-09 19:53:03 +02:00
|
|
|
user_groups = get_direct_user_groups(othello)
|
2021-08-12 12:15:06 +02:00
|
|
|
self.assert_length(user_groups, 3)
|
|
|
|
# othello is a direct member of two role-based system groups also.
|
|
|
|
user_group_names = [group.name for group in user_groups]
|
|
|
|
self.assertEqual(set(user_group_names), {"support", "@role:members", "@role:fullmembers"})
|
2017-09-25 09:47:15 +02:00
|
|
|
|
2021-09-29 02:46:57 +02:00
|
|
|
def test_recursive_queries_for_user_groups(self) -> None:
|
|
|
|
realm = get_realm("zulip")
|
|
|
|
iago = self.example_user("iago")
|
|
|
|
desdemona = self.example_user("desdemona")
|
|
|
|
shiva = self.example_user("shiva")
|
|
|
|
|
|
|
|
leadership_group = UserGroup.objects.create(realm=realm, name="Leadership")
|
|
|
|
UserGroupMembership.objects.create(user_profile=desdemona, user_group=leadership_group)
|
|
|
|
|
|
|
|
staff_group = UserGroup.objects.create(realm=realm, name="Staff")
|
|
|
|
UserGroupMembership.objects.create(user_profile=iago, user_group=staff_group)
|
|
|
|
GroupGroupMembership.objects.create(supergroup=staff_group, subgroup=leadership_group)
|
|
|
|
|
|
|
|
everyone_group = UserGroup.objects.create(realm=realm, name="Everyone")
|
|
|
|
UserGroupMembership.objects.create(user_profile=shiva, user_group=everyone_group)
|
|
|
|
GroupGroupMembership.objects.create(supergroup=everyone_group, subgroup=staff_group)
|
|
|
|
|
|
|
|
self.assertCountEqual(list(get_recursive_subgroups(leadership_group)), [leadership_group])
|
|
|
|
self.assertCountEqual(
|
|
|
|
list(get_recursive_subgroups(staff_group)), [leadership_group, staff_group]
|
|
|
|
)
|
|
|
|
self.assertCountEqual(
|
|
|
|
list(get_recursive_subgroups(everyone_group)),
|
|
|
|
[leadership_group, staff_group, everyone_group],
|
|
|
|
)
|
|
|
|
|
|
|
|
self.assertCountEqual(list(get_recursive_group_members(leadership_group)), [desdemona])
|
|
|
|
self.assertCountEqual(list(get_recursive_group_members(staff_group)), [desdemona, iago])
|
|
|
|
self.assertCountEqual(
|
|
|
|
list(get_recursive_group_members(everyone_group)), [desdemona, iago, shiva]
|
|
|
|
)
|
|
|
|
|
2021-08-12 12:15:06 +02:00
|
|
|
self.assertIn(leadership_group, list(get_recursive_membership_groups(desdemona)))
|
|
|
|
self.assertIn(staff_group, list(get_recursive_membership_groups(desdemona)))
|
|
|
|
self.assertIn(everyone_group, list(get_recursive_membership_groups(desdemona)))
|
|
|
|
|
|
|
|
self.assertIn(staff_group, list(get_recursive_membership_groups(iago)))
|
|
|
|
self.assertIn(everyone_group, list(get_recursive_membership_groups(iago)))
|
|
|
|
|
|
|
|
self.assertIn(everyone_group, list(get_recursive_membership_groups(shiva)))
|
2021-09-29 02:46:57 +02:00
|
|
|
|
2021-08-11 15:10:17 +02:00
|
|
|
def test_subgroups_of_role_based_system_groups(self) -> None:
|
|
|
|
realm = get_realm("zulip")
|
|
|
|
owners_group = UserGroup.objects.get(realm=realm, name="@role:owners", is_system_group=True)
|
|
|
|
admins_group = UserGroup.objects.get(
|
|
|
|
realm=realm, name="@role:administrators", is_system_group=True
|
|
|
|
)
|
|
|
|
moderators_group = UserGroup.objects.get(
|
|
|
|
realm=realm, name="@role:moderators", is_system_group=True
|
|
|
|
)
|
|
|
|
full_members_group = UserGroup.objects.get(
|
|
|
|
realm=realm, name="@role:fullmembers", is_system_group=True
|
|
|
|
)
|
|
|
|
members_group = UserGroup.objects.get(
|
|
|
|
realm=realm, name="@role:members", is_system_group=True
|
|
|
|
)
|
|
|
|
everyone_group = UserGroup.objects.get(
|
|
|
|
realm=realm, name="@role:everyone", is_system_group=True
|
|
|
|
)
|
|
|
|
everyone_on_internet_group = UserGroup.objects.get(
|
|
|
|
realm=realm, name="@role:internet", is_system_group=True
|
|
|
|
)
|
|
|
|
|
|
|
|
self.assertCountEqual(list(get_recursive_subgroups(owners_group)), [owners_group])
|
|
|
|
self.assertCountEqual(
|
|
|
|
list(get_recursive_subgroups(admins_group)), [owners_group, admins_group]
|
|
|
|
)
|
|
|
|
self.assertCountEqual(
|
|
|
|
list(get_recursive_subgroups(moderators_group)),
|
|
|
|
[owners_group, admins_group, moderators_group],
|
|
|
|
)
|
|
|
|
self.assertCountEqual(
|
|
|
|
list(get_recursive_subgroups(full_members_group)),
|
|
|
|
[owners_group, admins_group, moderators_group, full_members_group],
|
|
|
|
)
|
|
|
|
self.assertCountEqual(
|
|
|
|
list(get_recursive_subgroups(members_group)),
|
|
|
|
[owners_group, admins_group, moderators_group, full_members_group, members_group],
|
|
|
|
)
|
|
|
|
self.assertCountEqual(
|
|
|
|
list(get_recursive_subgroups(everyone_group)),
|
|
|
|
[
|
|
|
|
owners_group,
|
|
|
|
admins_group,
|
|
|
|
moderators_group,
|
|
|
|
full_members_group,
|
|
|
|
members_group,
|
|
|
|
everyone_group,
|
|
|
|
],
|
|
|
|
)
|
|
|
|
self.assertCountEqual(
|
|
|
|
list(get_recursive_subgroups(everyone_on_internet_group)),
|
|
|
|
[
|
|
|
|
owners_group,
|
|
|
|
admins_group,
|
|
|
|
moderators_group,
|
|
|
|
full_members_group,
|
|
|
|
members_group,
|
|
|
|
everyone_group,
|
|
|
|
everyone_on_internet_group,
|
|
|
|
],
|
|
|
|
)
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2021-05-21 07:02:43 +02:00
|
|
|
class UserGroupAPITestCase(UserGroupTestCase):
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_user_group_create(self) -> None:
|
2021-02-12 08:20:45 +01:00
|
|
|
hamlet = self.example_user("hamlet")
|
2017-11-01 10:04:16 +01:00
|
|
|
|
|
|
|
# Test success
|
2021-02-12 08:20:45 +01:00
|
|
|
self.login("hamlet")
|
2017-11-01 10:04:16 +01:00
|
|
|
params = {
|
2021-02-12 08:20:45 +01:00
|
|
|
"name": "support",
|
|
|
|
"members": orjson.dumps([hamlet.id]).decode(),
|
|
|
|
"description": "Support team",
|
2017-11-01 10:04:16 +01:00
|
|
|
}
|
2021-02-12 08:20:45 +01:00
|
|
|
result = self.client_post("/json/user_groups/create", info=params)
|
2017-11-01 10:04:16 +01:00
|
|
|
self.assert_json_success(result)
|
2021-08-11 15:10:17 +02:00
|
|
|
self.assert_length(UserGroup.objects.filter(realm=hamlet.realm), 9)
|
2017-11-01 10:04:16 +01:00
|
|
|
|
|
|
|
# Test invalid member error
|
|
|
|
params = {
|
2021-02-12 08:20:45 +01:00
|
|
|
"name": "backend",
|
|
|
|
"members": orjson.dumps([1111]).decode(),
|
|
|
|
"description": "Backend team",
|
2017-11-01 10:04:16 +01:00
|
|
|
}
|
2021-02-12 08:20:45 +01:00
|
|
|
result = self.client_post("/json/user_groups/create", info=params)
|
2017-11-01 10:04:16 +01:00
|
|
|
self.assert_json_error(result, "Invalid user ID: 1111")
|
2021-08-11 15:10:17 +02:00
|
|
|
self.assert_length(UserGroup.objects.filter(realm=hamlet.realm), 9)
|
2017-11-01 10:04:16 +01:00
|
|
|
|
2021-08-14 14:12:15 +02:00
|
|
|
# Test we cannot create group with same name again
|
2017-11-01 10:04:16 +01:00
|
|
|
params = {
|
2021-02-12 08:20:45 +01:00
|
|
|
"name": "support",
|
|
|
|
"members": orjson.dumps([hamlet.id]).decode(),
|
|
|
|
"description": "Support team",
|
2017-11-01 10:04:16 +01:00
|
|
|
}
|
2021-02-12 08:20:45 +01:00
|
|
|
result = self.client_post("/json/user_groups/create", info=params)
|
2017-11-01 10:04:16 +01:00
|
|
|
self.assert_json_error(result, "User group 'support' already exists.")
|
2021-08-11 15:10:17 +02:00
|
|
|
self.assert_length(UserGroup.objects.filter(realm=hamlet.realm), 9)
|
2017-11-02 07:53:08 +01:00
|
|
|
|
2018-08-16 02:44:51 +02:00
|
|
|
def test_user_group_get(self) -> None:
|
|
|
|
# Test success
|
2021-02-12 08:20:45 +01:00
|
|
|
user_profile = self.example_user("hamlet")
|
2020-03-06 18:40:46 +01:00
|
|
|
self.login_user(user_profile)
|
2021-02-12 08:20:45 +01:00
|
|
|
result = self.client_get("/json/user_groups")
|
2018-08-16 02:44:51 +02:00
|
|
|
self.assert_json_success(result)
|
2021-02-12 08:19:30 +01:00
|
|
|
self.assert_length(
|
2021-02-12 08:20:45 +01:00
|
|
|
result.json()["user_groups"], UserGroup.objects.filter(realm=user_profile.realm).count()
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
2018-08-16 02:44:51 +02:00
|
|
|
|
2021-05-21 07:06:03 +02:00
|
|
|
def test_can_edit_user_groups(self) -> None:
|
|
|
|
def validation_func(user_profile: UserProfile) -> bool:
|
|
|
|
user_profile.refresh_from_db()
|
|
|
|
return user_profile.can_edit_user_groups()
|
|
|
|
|
|
|
|
self.check_has_permission_policies("user_group_edit_policy", validation_func)
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_user_group_update(self) -> None:
|
2021-02-12 08:20:45 +01:00
|
|
|
hamlet = self.example_user("hamlet")
|
|
|
|
self.login("hamlet")
|
2017-11-02 07:53:08 +01:00
|
|
|
params = {
|
2021-02-12 08:20:45 +01:00
|
|
|
"name": "support",
|
|
|
|
"members": orjson.dumps([hamlet.id]).decode(),
|
|
|
|
"description": "Support team",
|
2017-11-02 07:53:08 +01:00
|
|
|
}
|
2021-02-12 08:20:45 +01:00
|
|
|
self.client_post("/json/user_groups/create", info=params)
|
|
|
|
user_group = UserGroup.objects.get(name="support")
|
2017-11-02 07:53:08 +01:00
|
|
|
# Test success
|
|
|
|
params = {
|
2021-02-12 08:20:45 +01:00
|
|
|
"name": "help",
|
|
|
|
"description": "Troubleshooting team",
|
2017-11-02 07:53:08 +01:00
|
|
|
}
|
2021-02-12 08:20:45 +01:00
|
|
|
result = self.client_patch(f"/json/user_groups/{user_group.id}", info=params)
|
2017-11-02 07:53:08 +01:00
|
|
|
self.assert_json_success(result)
|
|
|
|
|
|
|
|
# Test when new data is not supplied.
|
2021-02-12 08:20:45 +01:00
|
|
|
result = self.client_patch(f"/json/user_groups/{user_group.id}", info={})
|
2017-11-02 07:53:08 +01:00
|
|
|
self.assert_json_error(result, "No new data supplied")
|
|
|
|
|
|
|
|
# Test when invalid user group is supplied
|
2021-02-12 08:20:45 +01:00
|
|
|
params = {"name": "help"}
|
|
|
|
result = self.client_patch("/json/user_groups/1111", info=params)
|
2017-11-02 07:53:08 +01:00
|
|
|
self.assert_json_error(result, "Invalid user group")
|
2017-11-02 08:15:14 +01:00
|
|
|
|
2018-08-08 16:10:59 +02:00
|
|
|
def test_user_group_update_to_already_existing_name(self) -> None:
|
2021-02-12 08:20:45 +01:00
|
|
|
hamlet = self.example_user("hamlet")
|
2020-03-06 18:40:46 +01:00
|
|
|
self.login_user(hamlet)
|
2021-02-12 08:20:45 +01:00
|
|
|
realm = get_realm("zulip")
|
|
|
|
support_user_group = create_user_group("support", [hamlet], realm)
|
|
|
|
marketing_user_group = create_user_group("marketing", [hamlet], realm)
|
2018-08-08 16:10:59 +02:00
|
|
|
|
|
|
|
params = {
|
2021-02-12 08:20:45 +01:00
|
|
|
"name": marketing_user_group.name,
|
2018-08-08 16:10:59 +02:00
|
|
|
}
|
2021-02-12 08:20:45 +01:00
|
|
|
result = self.client_patch(f"/json/user_groups/{support_user_group.id}", info=params)
|
2021-02-12 08:19:30 +01:00
|
|
|
self.assert_json_error(result, f"User group '{marketing_user_group.name}' already exists.")
|
2018-08-08 16:10:59 +02:00
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_user_group_delete(self) -> None:
|
2021-02-12 08:20:45 +01:00
|
|
|
hamlet = self.example_user("hamlet")
|
|
|
|
self.login("hamlet")
|
2017-11-02 08:15:14 +01:00
|
|
|
params = {
|
2021-02-12 08:20:45 +01:00
|
|
|
"name": "support",
|
|
|
|
"members": orjson.dumps([hamlet.id]).decode(),
|
|
|
|
"description": "Support team",
|
2017-11-02 08:15:14 +01:00
|
|
|
}
|
2021-02-12 08:20:45 +01:00
|
|
|
self.client_post("/json/user_groups/create", info=params)
|
|
|
|
user_group = UserGroup.objects.get(name="support")
|
2017-11-02 08:15:14 +01:00
|
|
|
# Test success
|
2021-08-11 15:10:17 +02:00
|
|
|
self.assertEqual(UserGroup.objects.filter(realm=hamlet.realm).count(), 9)
|
2021-08-12 12:15:06 +02:00
|
|
|
self.assertEqual(UserGroupMembership.objects.count(), 19)
|
2021-02-12 08:20:45 +01:00
|
|
|
result = self.client_delete(f"/json/user_groups/{user_group.id}")
|
2017-11-02 08:15:14 +01:00
|
|
|
self.assert_json_success(result)
|
2021-08-11 15:10:17 +02:00
|
|
|
self.assertEqual(UserGroup.objects.filter(realm=hamlet.realm).count(), 8)
|
2021-08-12 12:15:06 +02:00
|
|
|
self.assertEqual(UserGroupMembership.objects.count(), 18)
|
2017-11-02 08:15:14 +01:00
|
|
|
# Test when invalid user group is supplied
|
2021-02-12 08:20:45 +01:00
|
|
|
result = self.client_delete("/json/user_groups/1111")
|
2017-11-02 08:15:14 +01:00
|
|
|
self.assert_json_error(result, "Invalid user group")
|
2017-11-02 08:53:30 +01:00
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_update_members_of_user_group(self) -> None:
|
2021-02-12 08:20:45 +01:00
|
|
|
hamlet = self.example_user("hamlet")
|
|
|
|
self.login("hamlet")
|
2017-11-02 08:53:30 +01:00
|
|
|
params = {
|
2021-02-12 08:20:45 +01:00
|
|
|
"name": "support",
|
|
|
|
"members": orjson.dumps([hamlet.id]).decode(),
|
|
|
|
"description": "Support team",
|
2017-11-02 08:53:30 +01:00
|
|
|
}
|
2021-02-12 08:20:45 +01:00
|
|
|
self.client_post("/json/user_groups/create", info=params)
|
|
|
|
user_group = UserGroup.objects.get(name="support")
|
2017-11-02 08:53:30 +01:00
|
|
|
# Test add members
|
2021-08-12 12:23:55 +02:00
|
|
|
self.assertEqual(UserGroupMembership.objects.filter(user_group=user_group).count(), 1)
|
2018-02-19 13:38:18 +01:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
othello = self.example_user("othello")
|
2017-11-02 08:53:30 +01:00
|
|
|
add = [othello.id]
|
2021-02-12 08:20:45 +01:00
|
|
|
params = {"add": orjson.dumps(add).decode()}
|
|
|
|
result = self.client_post(f"/json/user_groups/{user_group.id}/members", info=params)
|
2017-11-02 08:53:30 +01:00
|
|
|
self.assert_json_success(result)
|
2021-08-12 12:23:55 +02:00
|
|
|
self.assertEqual(UserGroupMembership.objects.filter(user_group=user_group).count(), 2)
|
2021-10-09 20:02:39 +02:00
|
|
|
members = get_direct_memberships_of_users(user_group, [hamlet, othello])
|
2021-05-17 05:41:32 +02:00
|
|
|
self.assert_length(members, 2)
|
2017-11-02 08:53:30 +01:00
|
|
|
|
|
|
|
# Test adding a member already there.
|
2021-02-12 08:20:45 +01:00
|
|
|
result = self.client_post(f"/json/user_groups/{user_group.id}/members", info=params)
|
2020-06-09 00:25:09 +02:00
|
|
|
self.assert_json_error(result, f"User {othello.id} is already a member of this group")
|
2021-08-12 12:23:55 +02:00
|
|
|
self.assertEqual(UserGroupMembership.objects.filter(user_group=user_group).count(), 2)
|
2021-10-09 20:02:39 +02:00
|
|
|
members = get_direct_memberships_of_users(user_group, [hamlet, othello])
|
2021-05-17 05:41:32 +02:00
|
|
|
self.assert_length(members, 2)
|
2017-11-02 08:53:30 +01:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
aaron = self.example_user("aaron")
|
2017-11-02 08:53:30 +01:00
|
|
|
|
docs: Add missing space to compound verbs “log in”, “set up”, etc.
Noun: backup, checkout, cleanup, login, logout, setup, shutdown, signup,
timeout.
Verb: back up, check out, clean up, log in, log out, set up, shut
down, sign up, time out.
Signed-off-by: Anders Kaseorg <anders@zulip.com>
2021-04-25 23:05:38 +02:00
|
|
|
# For normal testing we again log in with hamlet
|
2018-02-19 13:38:18 +01:00
|
|
|
self.logout()
|
2020-03-06 18:40:46 +01:00
|
|
|
self.login_user(hamlet)
|
2018-02-19 13:38:18 +01:00
|
|
|
# Test remove members
|
2021-02-12 08:20:45 +01:00
|
|
|
params = {"delete": orjson.dumps([othello.id]).decode()}
|
|
|
|
result = self.client_post(f"/json/user_groups/{user_group.id}/members", info=params)
|
2017-11-02 08:53:30 +01:00
|
|
|
self.assert_json_success(result)
|
2021-08-12 12:23:55 +02:00
|
|
|
self.assertEqual(UserGroupMembership.objects.filter(user_group=user_group).count(), 1)
|
2021-10-09 20:02:39 +02:00
|
|
|
members = get_direct_memberships_of_users(user_group, [hamlet, othello, aaron])
|
2021-05-21 07:02:43 +02:00
|
|
|
self.assert_length(members, 1)
|
2018-02-19 13:38:18 +01:00
|
|
|
|
|
|
|
# Test remove a member that's already removed
|
2021-02-12 08:20:45 +01:00
|
|
|
params = {"delete": orjson.dumps([othello.id]).decode()}
|
|
|
|
result = self.client_post(f"/json/user_groups/{user_group.id}/members", info=params)
|
2020-06-09 00:25:09 +02:00
|
|
|
self.assert_json_error(result, f"There is no member '{othello.id}' in this user group")
|
2021-08-12 12:23:55 +02:00
|
|
|
self.assertEqual(UserGroupMembership.objects.filter(user_group=user_group).count(), 1)
|
2021-10-09 20:02:39 +02:00
|
|
|
members = get_direct_memberships_of_users(user_group, [hamlet, othello, aaron])
|
2021-05-21 07:02:43 +02:00
|
|
|
self.assert_length(members, 1)
|
2017-11-02 08:53:30 +01:00
|
|
|
|
|
|
|
# Test when nothing is provided
|
2021-02-12 08:20:45 +01:00
|
|
|
result = self.client_post(f"/json/user_groups/{user_group.id}/members", info={})
|
2017-11-02 08:53:30 +01:00
|
|
|
msg = 'Nothing to do. Specify at least one of "add" or "delete".'
|
|
|
|
self.assert_json_error(result, msg)
|
2018-02-19 13:38:18 +01:00
|
|
|
|
2018-08-14 21:37:52 +02:00
|
|
|
def test_mentions(self) -> None:
|
2021-02-12 08:20:45 +01:00
|
|
|
cordelia = self.example_user("cordelia")
|
|
|
|
hamlet = self.example_user("hamlet")
|
|
|
|
othello = self.example_user("othello")
|
|
|
|
zoe = self.example_user("ZOE")
|
2018-08-14 21:37:52 +02:00
|
|
|
|
|
|
|
realm = cordelia.realm
|
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
group_name = "support"
|
2021-05-10 07:02:14 +02:00
|
|
|
stream_name = "Dev help"
|
2018-08-14 21:37:52 +02:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
content_with_group_mention = "hey @*support* can you help us with this?"
|
2018-08-14 21:37:52 +02:00
|
|
|
|
2021-04-02 18:11:45 +02:00
|
|
|
ensure_stream(realm, stream_name, acting_user=None)
|
2018-08-14 21:37:52 +02:00
|
|
|
|
|
|
|
all_users = {cordelia, hamlet, othello, zoe}
|
|
|
|
support_team = {hamlet, zoe}
|
|
|
|
sender = cordelia
|
|
|
|
other_users = all_users - support_team
|
|
|
|
|
|
|
|
for user in all_users:
|
|
|
|
self.subscribe(user, stream_name)
|
|
|
|
|
|
|
|
create_user_group(
|
|
|
|
name=group_name,
|
|
|
|
members=list(support_team),
|
|
|
|
realm=realm,
|
|
|
|
)
|
|
|
|
|
|
|
|
payload = dict(
|
|
|
|
type="stream",
|
|
|
|
to=stream_name,
|
2021-02-12 08:20:45 +01:00
|
|
|
topic="whatever",
|
2018-08-14 21:37:52 +02:00
|
|
|
content=content_with_group_mention,
|
|
|
|
)
|
|
|
|
|
2020-10-28 10:32:49 +01:00
|
|
|
result = self.api_post(sender, "/json/messages", payload)
|
2018-08-14 21:37:52 +02:00
|
|
|
|
|
|
|
self.assert_json_success(result)
|
|
|
|
|
|
|
|
for user in support_team:
|
|
|
|
um = most_recent_usermessage(user)
|
|
|
|
self.assertTrue(um.flags.mentioned)
|
|
|
|
|
|
|
|
for user in other_users:
|
|
|
|
um = most_recent_usermessage(user)
|
|
|
|
self.assertFalse(um.flags.mentioned)
|
2019-11-02 17:58:55 +01:00
|
|
|
|
2021-05-21 07:02:43 +02:00
|
|
|
def test_user_group_edit_policy_for_creating_and_deleting_user_group(self) -> None:
|
2021-02-12 08:20:45 +01:00
|
|
|
hamlet = self.example_user("hamlet")
|
2021-08-16 10:09:10 +02:00
|
|
|
realm = hamlet.realm
|
2021-05-21 07:02:43 +02:00
|
|
|
|
|
|
|
def check_create_user_group(acting_user: str, error_msg: Optional[str] = None) -> None:
|
|
|
|
self.login(acting_user)
|
|
|
|
params = {
|
|
|
|
"name": "support",
|
|
|
|
"members": orjson.dumps([hamlet.id]).decode(),
|
|
|
|
"description": "Support Team",
|
|
|
|
}
|
|
|
|
result = self.client_post("/json/user_groups/create", info=params)
|
|
|
|
if error_msg is None:
|
|
|
|
self.assert_json_success(result)
|
|
|
|
# One group already exists in the test database.
|
2021-08-11 15:10:17 +02:00
|
|
|
self.assert_length(UserGroup.objects.filter(realm=realm), 9)
|
2021-05-21 07:02:43 +02:00
|
|
|
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")
|
|
|
|
result = self.client_delete(f"/json/user_groups/{user_group.id}")
|
|
|
|
if error_msg is None:
|
|
|
|
self.assert_json_success(result)
|
2021-08-11 15:10:17 +02:00
|
|
|
self.assert_length(UserGroup.objects.filter(realm=realm), 8)
|
2021-05-21 07:02:43 +02:00
|
|
|
else:
|
|
|
|
self.assert_json_error(result, error_msg)
|
|
|
|
|
|
|
|
realm = hamlet.realm
|
|
|
|
|
|
|
|
# Check only admins are allowed to create/delete user group. Admins are allowed even if
|
|
|
|
# they are not a member of the group.
|
2021-02-12 08:19:30 +01:00
|
|
|
do_set_realm_property(
|
2021-05-21 07:02:43 +02:00
|
|
|
realm,
|
2021-03-01 11:33:24 +01:00
|
|
|
"user_group_edit_policy",
|
2021-05-21 07:02:43 +02:00
|
|
|
Realm.POLICY_ADMINS_ONLY,
|
2021-03-01 11:33:24 +01:00
|
|
|
acting_user=None,
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
2021-05-21 07:02:43 +02:00
|
|
|
check_create_user_group("shiva", "Insufficient permission")
|
|
|
|
check_create_user_group("iago")
|
|
|
|
|
|
|
|
check_delete_user_group("shiva", "Insufficient permission")
|
|
|
|
check_delete_user_group("iago")
|
2019-11-02 17:58:55 +01:00
|
|
|
|
2021-05-21 07:02:43 +02:00
|
|
|
# Check moderators are allowed to create/delete user group but not members. Moderators are
|
|
|
|
# allowed even if they are not a member of the group.
|
|
|
|
do_set_realm_property(
|
|
|
|
realm,
|
|
|
|
"user_group_edit_policy",
|
|
|
|
Realm.POLICY_MODERATORS_ONLY,
|
|
|
|
acting_user=None,
|
|
|
|
)
|
|
|
|
check_create_user_group("cordelia", "Insufficient permission")
|
|
|
|
check_create_user_group("shiva")
|
|
|
|
|
|
|
|
check_delete_user_group("hamlet", "Insufficient permission")
|
|
|
|
check_delete_user_group("shiva")
|
|
|
|
|
|
|
|
# Check only members are allowed to create the user group and they are allowed to delete
|
|
|
|
# a user group only if they are a member of that group.
|
|
|
|
do_set_realm_property(
|
|
|
|
realm,
|
|
|
|
"user_group_edit_policy",
|
|
|
|
Realm.POLICY_MEMBERS_ONLY,
|
|
|
|
acting_user=None,
|
|
|
|
)
|
|
|
|
check_create_user_group("polonius", "Not allowed for guest users")
|
|
|
|
check_create_user_group("cordelia")
|
|
|
|
|
|
|
|
check_delete_user_group("polonius", "Not allowed for guest users")
|
|
|
|
check_delete_user_group("cordelia", "Insufficient permission")
|
|
|
|
check_delete_user_group("hamlet")
|
|
|
|
|
|
|
|
# Check only full members are allowed to create the user group and they are allowed to delete
|
|
|
|
# a user group only if they are a member of that group.
|
|
|
|
do_set_realm_property(
|
|
|
|
realm,
|
|
|
|
"user_group_edit_policy",
|
|
|
|
Realm.POLICY_FULL_MEMBERS_ONLY,
|
|
|
|
acting_user=None,
|
|
|
|
)
|
|
|
|
cordelia = self.example_user("cordelia")
|
|
|
|
do_set_realm_property(realm, "waiting_period_threshold", 10, acting_user=None)
|
|
|
|
|
|
|
|
cordelia.date_joined = timezone_now() - timedelta(days=9)
|
|
|
|
cordelia.save()
|
|
|
|
check_create_user_group("cordelia", "Insufficient permission")
|
|
|
|
|
|
|
|
cordelia.date_joined = timezone_now() - timedelta(days=11)
|
|
|
|
cordelia.save()
|
|
|
|
check_create_user_group("cordelia")
|
|
|
|
|
|
|
|
hamlet.date_joined = timezone_now() - timedelta(days=9)
|
|
|
|
hamlet.save()
|
|
|
|
|
|
|
|
check_delete_user_group("cordelia", "Insufficient permission")
|
|
|
|
check_delete_user_group("hamlet", "Insufficient permission")
|
|
|
|
|
|
|
|
hamlet.date_joined = timezone_now() - timedelta(days=11)
|
|
|
|
hamlet.save()
|
|
|
|
check_delete_user_group("hamlet")
|
|
|
|
|
|
|
|
def test_user_group_edit_policy_for_updating_user_groups(self) -> None:
|
|
|
|
othello = self.example_user("othello")
|
|
|
|
self.login("othello")
|
2019-11-02 17:58:55 +01:00
|
|
|
params = {
|
2021-02-12 08:20:45 +01:00
|
|
|
"name": "support",
|
2021-05-21 07:02:43 +02:00
|
|
|
"members": orjson.dumps([othello.id]).decode(),
|
2021-02-12 08:20:45 +01:00
|
|
|
"description": "Support team",
|
2019-11-02 17:58:55 +01:00
|
|
|
}
|
2021-05-21 07:02:43 +02:00
|
|
|
self.client_post("/json/user_groups/create", info=params)
|
2021-02-12 08:20:45 +01:00
|
|
|
user_group = UserGroup.objects.get(name="support")
|
2019-11-02 17:58:55 +01:00
|
|
|
|
2021-05-21 07:02:43 +02:00
|
|
|
def check_update_user_group(
|
|
|
|
new_name: str,
|
|
|
|
new_description: str,
|
|
|
|
acting_user: str,
|
|
|
|
error_msg: Optional[str] = None,
|
|
|
|
) -> None:
|
|
|
|
self.login(acting_user)
|
|
|
|
params = {
|
|
|
|
"name": new_name,
|
|
|
|
"description": new_description,
|
|
|
|
}
|
|
|
|
result = self.client_patch(f"/json/user_groups/{user_group.id}", info=params)
|
|
|
|
if error_msg is None:
|
|
|
|
self.assert_json_success(result)
|
|
|
|
else:
|
|
|
|
self.assert_json_error(result, error_msg)
|
|
|
|
|
|
|
|
realm = othello.realm
|
|
|
|
|
|
|
|
# Check only admins are allowed to update user group. Admins are allowed even if
|
|
|
|
# they are not a member of the group.
|
|
|
|
do_set_realm_property(
|
|
|
|
realm,
|
|
|
|
"user_group_edit_policy",
|
|
|
|
Realm.POLICY_ADMINS_ONLY,
|
|
|
|
acting_user=None,
|
|
|
|
)
|
|
|
|
check_update_user_group("help", "Troubleshooting team", "shiva", "Insufficient permission")
|
|
|
|
check_update_user_group("help", "Troubleshooting team", "iago")
|
2019-11-02 17:58:55 +01:00
|
|
|
|
2021-05-21 07:02:43 +02:00
|
|
|
# Check moderators are allowed to update user group but not members. Moderators are
|
|
|
|
# allowed even if they are not a member of the group.
|
|
|
|
do_set_realm_property(
|
|
|
|
realm,
|
|
|
|
"user_group_edit_policy",
|
|
|
|
Realm.POLICY_MODERATORS_ONLY,
|
|
|
|
acting_user=None,
|
|
|
|
)
|
|
|
|
check_update_user_group("support", "Support team", "othello", "Insufficient permission")
|
|
|
|
check_update_user_group("support", "Support team", "iago")
|
2019-11-02 17:58:55 +01:00
|
|
|
|
2021-05-21 07:02:43 +02:00
|
|
|
# Check only members are allowed to update the user group and only if belong to the
|
|
|
|
# user group.
|
|
|
|
do_set_realm_property(
|
|
|
|
realm,
|
|
|
|
"user_group_edit_policy",
|
|
|
|
Realm.POLICY_MEMBERS_ONLY,
|
|
|
|
acting_user=None,
|
|
|
|
)
|
|
|
|
check_update_user_group(
|
|
|
|
"help", "Troubleshooting team", "polonius", "Not allowed for guest users"
|
|
|
|
)
|
|
|
|
check_update_user_group(
|
|
|
|
"help",
|
|
|
|
"Troubleshooting team",
|
|
|
|
"cordelia",
|
|
|
|
"Insufficient permission",
|
|
|
|
)
|
|
|
|
check_update_user_group("help", "Troubleshooting team", "othello")
|
2019-11-02 17:58:55 +01:00
|
|
|
|
2021-05-21 07:02:43 +02:00
|
|
|
# Check only full members are allowed to update the user group and only if belong to the
|
|
|
|
# user group.
|
|
|
|
do_set_realm_property(
|
|
|
|
realm, "user_group_edit_policy", Realm.POLICY_FULL_MEMBERS_ONLY, acting_user=None
|
|
|
|
)
|
|
|
|
do_set_realm_property(realm, "waiting_period_threshold", 10, acting_user=None)
|
|
|
|
othello = self.example_user("othello")
|
|
|
|
othello.date_joined = timezone_now() - timedelta(days=9)
|
|
|
|
othello.save()
|
2019-11-02 17:58:55 +01:00
|
|
|
|
2021-05-21 07:02:43 +02:00
|
|
|
cordelia = self.example_user("cordelia")
|
|
|
|
cordelia.date_joined = timezone_now() - timedelta(days=11)
|
|
|
|
cordelia.save()
|
|
|
|
check_update_user_group(
|
|
|
|
"support",
|
|
|
|
"Support team",
|
|
|
|
"cordelia",
|
|
|
|
"Insufficient permission",
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
2021-05-21 07:02:43 +02:00
|
|
|
check_update_user_group("support", "Support team", "othello", "Insufficient permission")
|
2019-11-02 17:58:55 +01:00
|
|
|
|
2021-05-21 07:02:43 +02:00
|
|
|
othello.date_joined = timezone_now() - timedelta(days=11)
|
|
|
|
othello.save()
|
|
|
|
check_update_user_group("support", "Support team", "othello")
|
2019-11-02 17:58:55 +01:00
|
|
|
|
2021-05-21 07:02:43 +02:00
|
|
|
def test_user_group_edit_policy_for_updating_members(self) -> None:
|
|
|
|
user_group = self.create_user_group_for_test("support")
|
|
|
|
aaron = self.example_user("aaron")
|
|
|
|
othello = self.example_user("othello")
|
|
|
|
cordelia = self.example_user("cordelia")
|
2019-11-02 17:58:55 +01:00
|
|
|
|
2021-05-21 07:02:43 +02:00
|
|
|
def check_adding_members_to_group(
|
|
|
|
acting_user: str, error_msg: Optional[str] = None
|
|
|
|
) -> None:
|
|
|
|
self.login(acting_user)
|
|
|
|
params = {"add": orjson.dumps([aaron.id]).decode()}
|
|
|
|
result = self.client_post(f"/json/user_groups/{user_group.id}/members", info=params)
|
|
|
|
if error_msg is None:
|
|
|
|
self.assert_json_success(result)
|
2021-08-12 12:23:55 +02:00
|
|
|
self.assertEqual(
|
|
|
|
UserGroupMembership.objects.filter(user_group=user_group).count(), 2
|
|
|
|
)
|
2021-10-09 20:02:39 +02:00
|
|
|
members = get_direct_memberships_of_users(user_group, [aaron, othello])
|
2021-05-21 07:02:43 +02:00
|
|
|
self.assert_length(members, 2)
|
|
|
|
else:
|
|
|
|
self.assert_json_error(result, error_msg)
|
|
|
|
|
|
|
|
def check_removing_members_from_group(
|
|
|
|
acting_user: str, error_msg: Optional[str] = None
|
|
|
|
) -> None:
|
|
|
|
self.login(acting_user)
|
|
|
|
params = {"delete": orjson.dumps([aaron.id]).decode()}
|
|
|
|
result = self.client_post(f"/json/user_groups/{user_group.id}/members", info=params)
|
|
|
|
if error_msg is None:
|
|
|
|
self.assert_json_success(result)
|
2021-08-12 12:23:55 +02:00
|
|
|
self.assertEqual(
|
|
|
|
UserGroupMembership.objects.filter(user_group=user_group).count(), 1
|
|
|
|
)
|
2021-10-09 20:02:39 +02:00
|
|
|
members = get_direct_memberships_of_users(user_group, [aaron, othello])
|
2021-05-21 07:02:43 +02:00
|
|
|
self.assert_length(members, 1)
|
|
|
|
else:
|
|
|
|
self.assert_json_error(result, error_msg)
|
2019-11-02 17:58:55 +01:00
|
|
|
|
2021-05-21 07:02:43 +02:00
|
|
|
realm = get_realm("zulip")
|
|
|
|
# Check only admins are allowed to add/remove users from the group. Admins are allowed even if
|
|
|
|
# they are not a member of the group.
|
|
|
|
do_set_realm_property(
|
|
|
|
realm,
|
|
|
|
"user_group_edit_policy",
|
|
|
|
Realm.POLICY_ADMINS_ONLY,
|
|
|
|
acting_user=None,
|
|
|
|
)
|
|
|
|
check_adding_members_to_group("shiva", "Insufficient permission")
|
|
|
|
check_adding_members_to_group("iago")
|
2019-11-02 17:58:55 +01:00
|
|
|
|
2021-05-21 07:02:43 +02:00
|
|
|
check_removing_members_from_group("shiva", "Insufficient permission")
|
|
|
|
check_removing_members_from_group("iago")
|
2019-11-02 17:58:55 +01:00
|
|
|
|
2021-05-21 07:02:43 +02:00
|
|
|
# Check moderators are allowed to add/remove users from the group but not members. Moderators are
|
|
|
|
# allowed even if they are not a member of the group.
|
|
|
|
do_set_realm_property(
|
|
|
|
realm,
|
|
|
|
"user_group_edit_policy",
|
|
|
|
Realm.POLICY_MODERATORS_ONLY,
|
|
|
|
acting_user=None,
|
|
|
|
)
|
|
|
|
check_adding_members_to_group("cordelia", "Insufficient permission")
|
|
|
|
check_adding_members_to_group("shiva")
|
|
|
|
|
|
|
|
check_removing_members_from_group("hamlet", "Insufficient permission")
|
|
|
|
check_removing_members_from_group("shiva")
|
|
|
|
|
|
|
|
# Check only members are allowed to add/remove users in the group and only if belong to the
|
|
|
|
# user group.
|
|
|
|
do_set_realm_property(
|
|
|
|
realm,
|
|
|
|
"user_group_edit_policy",
|
|
|
|
Realm.POLICY_MEMBERS_ONLY,
|
|
|
|
acting_user=None,
|
|
|
|
)
|
|
|
|
check_adding_members_to_group("polonius", "Not allowed for guest users")
|
|
|
|
check_adding_members_to_group("cordelia", "Insufficient permission")
|
|
|
|
check_adding_members_to_group("othello")
|
|
|
|
|
|
|
|
check_removing_members_from_group("polonius", "Not allowed for guest users")
|
|
|
|
check_removing_members_from_group("cordelia", "Insufficient permission")
|
|
|
|
check_removing_members_from_group("othello")
|
|
|
|
|
|
|
|
# Check only full members are allowed to add/remove users in the group and only if belong to the
|
|
|
|
# user group.
|
|
|
|
do_set_realm_property(
|
|
|
|
realm,
|
|
|
|
"user_group_edit_policy",
|
|
|
|
Realm.POLICY_FULL_MEMBERS_ONLY,
|
|
|
|
acting_user=None,
|
|
|
|
)
|
|
|
|
do_set_realm_property(realm, "waiting_period_threshold", 10, acting_user=None)
|
|
|
|
|
|
|
|
othello.date_joined = timezone_now() - timedelta(days=9)
|
|
|
|
othello.save()
|
|
|
|
check_adding_members_to_group("cordelia", "Insufficient permission")
|
|
|
|
|
|
|
|
cordelia.date_joined = timezone_now() - timedelta(days=11)
|
|
|
|
cordelia.save()
|
|
|
|
check_adding_members_to_group("cordelia", "Insufficient permission")
|
|
|
|
|
|
|
|
othello.date_joined = timezone_now() - timedelta(days=11)
|
|
|
|
othello.save()
|
|
|
|
check_adding_members_to_group("othello")
|
|
|
|
|
|
|
|
othello.date_joined = timezone_now() - timedelta(days=9)
|
|
|
|
othello.save()
|
|
|
|
|
|
|
|
check_removing_members_from_group("cordelia", "Insufficient permission")
|
|
|
|
check_removing_members_from_group("othello", "Insufficient permission")
|
|
|
|
|
|
|
|
othello.date_joined = timezone_now() - timedelta(days=11)
|
|
|
|
othello.save()
|
|
|
|
check_removing_members_from_group("othello")
|
2021-08-06 15:22:08 +02:00
|
|
|
|
|
|
|
def test_editing_system_user_groups(self) -> None:
|
|
|
|
desdemona = self.example_user("desdemona")
|
|
|
|
iago = self.example_user("iago")
|
|
|
|
othello = self.example_user("othello")
|
|
|
|
aaron = self.example_user("aaron")
|
2021-08-11 15:10:17 +02:00
|
|
|
|
|
|
|
user_group = UserGroup.objects.get(
|
|
|
|
realm=iago.realm, name="@role:fullmembers", is_system_group=True
|
2021-08-06 15:22:08 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
def check_support_group_permission(acting_user: UserProfile) -> None:
|
|
|
|
self.login_user(acting_user)
|
|
|
|
params = {
|
2021-08-20 19:49:23 +02:00
|
|
|
"name": "Full members user group",
|
|
|
|
"description": "Full members system user group.",
|
2021-08-06 15:22:08 +02:00
|
|
|
}
|
|
|
|
result = self.client_patch(f"/json/user_groups/{user_group.id}", info=params)
|
|
|
|
self.assert_json_error(result, "Insufficient permission")
|
|
|
|
|
|
|
|
params = {"add": orjson.dumps([aaron.id]).decode()}
|
|
|
|
result = self.client_post(f"/json/user_groups/{user_group.id}/members", info=params)
|
|
|
|
self.assert_json_error(result, "Insufficient permission")
|
|
|
|
|
|
|
|
params = {"delete": orjson.dumps([othello.id]).decode()}
|
|
|
|
result = self.client_post(f"/json/user_groups/{user_group.id}/members", info=params)
|
|
|
|
self.assert_json_error(result, "Insufficient permission")
|
|
|
|
|
|
|
|
check_support_group_permission(desdemona)
|
|
|
|
check_support_group_permission(iago)
|
|
|
|
check_support_group_permission(othello)
|
2022-01-31 18:24:00 +01:00
|
|
|
|
|
|
|
def test_promote_new_full_members(self) -> None:
|
|
|
|
realm = get_realm("zulip")
|
|
|
|
|
|
|
|
cordelia = self.example_user("cordelia")
|
|
|
|
hamlet = self.example_user("hamlet")
|
|
|
|
cordelia.date_joined = timezone_now() - timedelta(days=11)
|
|
|
|
cordelia.save()
|
|
|
|
|
|
|
|
hamlet.date_joined = timezone_now() - timedelta(days=8)
|
|
|
|
hamlet.save()
|
|
|
|
|
|
|
|
do_set_realm_property(realm, "waiting_period_threshold", 10, acting_user=None)
|
|
|
|
full_members_group = UserGroup.objects.get(
|
|
|
|
realm=realm, name="@role:fullmembers", is_system_group=True
|
|
|
|
)
|
|
|
|
|
|
|
|
self.assertTrue(
|
|
|
|
UserGroupMembership.objects.filter(
|
|
|
|
user_profile=cordelia, user_group=full_members_group
|
|
|
|
).exists()
|
|
|
|
)
|
|
|
|
self.assertFalse(
|
|
|
|
UserGroupMembership.objects.filter(
|
|
|
|
user_profile=hamlet, user_group=full_members_group
|
|
|
|
).exists()
|
|
|
|
)
|
|
|
|
|
|
|
|
current_time = timezone_now()
|
|
|
|
with mock.patch(
|
2022-04-14 23:30:17 +02:00
|
|
|
"zerver.actions.user_groups.timezone_now", return_value=current_time + timedelta(days=3)
|
2022-01-31 18:24:00 +01:00
|
|
|
):
|
|
|
|
promote_new_full_members()
|
|
|
|
|
|
|
|
self.assertTrue(
|
|
|
|
UserGroupMembership.objects.filter(
|
|
|
|
user_profile=cordelia, user_group=full_members_group
|
|
|
|
).exists()
|
|
|
|
)
|
|
|
|
self.assertTrue(
|
|
|
|
UserGroupMembership.objects.filter(
|
|
|
|
user_profile=hamlet, user_group=full_members_group
|
|
|
|
).exists()
|
|
|
|
)
|
2022-03-02 11:58:37 +01:00
|
|
|
|
|
|
|
def test_updating_subgroups_of_user_group(self) -> None:
|
|
|
|
realm = get_realm("zulip")
|
|
|
|
desdemona = self.example_user("desdemona")
|
|
|
|
iago = self.example_user("iago")
|
|
|
|
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)
|
|
|
|
|
|
|
|
self.login("cordelia")
|
|
|
|
# Non-admin and non-moderators who are not a member of group cannot add or remove subgroups.
|
|
|
|
params = {"add": orjson.dumps([leadership_group.id]).decode()}
|
|
|
|
result = self.client_post(f"/json/user_groups/{support_group.id}/subgroups", info=params)
|
|
|
|
self.assert_json_error(result, "Insufficient permission")
|
|
|
|
|
|
|
|
self.login("iago")
|
|
|
|
result = self.client_post(f"/json/user_groups/{support_group.id}/subgroups", info=params)
|
|
|
|
self.assert_json_success(result)
|
|
|
|
|
|
|
|
params = {"delete": orjson.dumps([leadership_group.id]).decode()}
|
|
|
|
result = self.client_post(f"/json/user_groups/{support_group.id}/subgroups", info=params)
|
|
|
|
self.assert_json_success(result)
|
|
|
|
|
|
|
|
self.login("shiva")
|
|
|
|
params = {"add": orjson.dumps([leadership_group.id]).decode()}
|
|
|
|
result = self.client_post(f"/json/user_groups/{support_group.id}/subgroups", info=params)
|
|
|
|
self.assert_json_success(result)
|
|
|
|
|
|
|
|
params = {"delete": orjson.dumps([leadership_group.id]).decode()}
|
|
|
|
result = self.client_post(f"/json/user_groups/{support_group.id}/subgroups", info=params)
|
|
|
|
self.assert_json_success(result)
|
|
|
|
|
|
|
|
self.login("hamlet")
|
|
|
|
# Non-admin and non-moderators who are a member of the user group can add or remove subgroups.
|
|
|
|
params = {"add": orjson.dumps([leadership_group.id]).decode()}
|
|
|
|
result = self.client_post(f"/json/user_groups/{support_group.id}/subgroups", info=params)
|
|
|
|
self.assert_json_success(result)
|
|
|
|
|
|
|
|
params = {"delete": orjson.dumps([leadership_group.id]).decode()}
|
|
|
|
result = self.client_post(f"/json/user_groups/{support_group.id}/subgroups", info=params)
|
|
|
|
self.assert_json_success(result)
|
|
|
|
|
|
|
|
# Users need not be part of the subgroup to add or remove it from a user group.
|
|
|
|
self.login("othello")
|
|
|
|
params = {"add": orjson.dumps([leadership_group.id]).decode()}
|
|
|
|
result = self.client_post(f"/json/user_groups/{support_group.id}/subgroups", info=params)
|
|
|
|
self.assert_json_success(result)
|
|
|
|
|
|
|
|
params = {"delete": orjson.dumps([leadership_group.id]).decode()}
|
|
|
|
result = self.client_post(f"/json/user_groups/{support_group.id}/subgroups", info=params)
|
|
|
|
self.assert_json_success(result)
|
|
|
|
|
|
|
|
result = self.client_post(f"/json/user_groups/{support_group.id}/subgroups", info=params)
|
|
|
|
self.assert_json_error(
|
|
|
|
result,
|
|
|
|
("User group {group_id} is not a subgroup of this group.").format(
|
|
|
|
group_id=leadership_group.id
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
params = {"add": orjson.dumps([leadership_group.id]).decode()}
|
|
|
|
self.client_post(f"/json/user_groups/{support_group.id}/subgroups", info=params)
|
|
|
|
|
|
|
|
result = self.client_post(f"/json/user_groups/{support_group.id}/subgroups", info=params)
|
|
|
|
self.assert_json_error(
|
|
|
|
result,
|
|
|
|
("User group {group_id} is already a subgroup of this group.").format(
|
|
|
|
group_id=leadership_group.id
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
# Invalid subgroup id will raise an error.
|
|
|
|
params = {"add": orjson.dumps([leadership_group.id, 101]).decode()}
|
|
|
|
result = self.client_post(f"/json/user_groups/{support_group.id}/subgroups", info=params)
|
|
|
|
self.assert_json_error(result, "Invalid user group ID: 101")
|
|
|
|
|
|
|
|
# Test when nothing is provided
|
|
|
|
result = self.client_post(f"/json/user_groups/{support_group.id}/subgroups", info={})
|
|
|
|
self.assert_json_error(result, 'Nothing to do. Specify at least one of "add" or "delete".')
|