2020-05-26 07:16:25 +02:00
|
|
|
from unittest import mock
|
2017-09-25 09:47:15 +02:00
|
|
|
|
2020-08-07 01:09:47 +02:00
|
|
|
import orjson
|
2018-08-14 21:37:52 +02:00
|
|
|
|
2020-06-11 00:54:34 +02:00
|
|
|
from zerver.lib.actions import do_set_realm_property, 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 (
|
|
|
|
check_add_user_to_user_group,
|
|
|
|
check_remove_user_from_user_group,
|
|
|
|
create_user_group,
|
2020-06-11 00:54:34 +02:00
|
|
|
get_memberships_of_users,
|
2017-09-25 09:47:15 +02:00
|
|
|
get_user_groups,
|
|
|
|
user_groups_in_realm,
|
2017-11-07 07:56:26 +01:00
|
|
|
user_groups_in_realm_serialized,
|
2017-09-25 09:47:15 +02:00
|
|
|
)
|
2020-06-11 00:54:34 +02:00
|
|
|
from zerver.models import Realm, UserGroup, UserGroupMembership, get_realm
|
|
|
|
|
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(self) -> None:
|
2021-02-12 08:20:45 +01:00
|
|
|
realm = get_realm("zulip")
|
2017-11-13 07:49:01 +01:00
|
|
|
self.assertEqual(len(user_groups_in_realm(realm)), 1)
|
2021-02-12 08:20:45 +01:00
|
|
|
self.create_user_group_for_test("support")
|
2017-09-25 09:47:15 +02:00
|
|
|
user_groups = user_groups_in_realm(realm)
|
2017-11-13 07:49:01 +01:00
|
|
|
self.assertEqual(len(user_groups), 2)
|
2020-04-09 21:51:58 +02:00
|
|
|
names = {ug.name for ug in user_groups}
|
2021-02-12 08:20:45 +01:00
|
|
|
self.assertEqual(names, {"hamletcharacters", "support"})
|
2017-09-25 09:47:15 +02:00
|
|
|
|
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()
|
|
|
|
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)
|
2017-11-30 01:09:23 +01:00
|
|
|
self.assertEqual(len(user_groups), 2)
|
2021-02-12 08:20:45 +01:00
|
|
|
self.assertEqual(user_groups[0]["id"], user_group.id)
|
|
|
|
self.assertEqual(user_groups[0]["name"], "hamletcharacters")
|
|
|
|
self.assertEqual(user_groups[0]["description"], "Characters of Hamlet")
|
|
|
|
self.assertEqual(set(user_groups[0]["members"]), set(membership))
|
2017-11-07 07:56:26 +01:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
self.assertEqual(user_groups[1]["id"], empty_user_group.id)
|
|
|
|
self.assertEqual(user_groups[1]["name"], "newgroup")
|
|
|
|
self.assertEqual(user_groups[1]["description"], "")
|
|
|
|
self.assertEqual(user_groups[1]["members"], [])
|
2017-11-30 01:09:23 +01:00
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_get_user_groups(self) -> None:
|
2021-02-12 08:20:45 +01:00
|
|
|
othello = self.example_user("othello")
|
|
|
|
self.create_user_group_for_test("support")
|
2017-09-25 09:47:15 +02:00
|
|
|
user_groups = get_user_groups(othello)
|
|
|
|
self.assertEqual(len(user_groups), 1)
|
2021-02-12 08:20:45 +01:00
|
|
|
self.assertEqual(user_groups[0].name, "support")
|
2017-09-25 09:47:15 +02:00
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_check_add_user_to_user_group(self) -> None:
|
2021-02-12 08:20:45 +01:00
|
|
|
user_group = self.create_user_group_for_test("support")
|
|
|
|
hamlet = self.example_user("hamlet")
|
2017-09-25 09:47:15 +02:00
|
|
|
self.assertTrue(check_add_user_to_user_group(hamlet, user_group))
|
|
|
|
self.assertFalse(check_add_user_to_user_group(hamlet, user_group))
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_check_remove_user_from_user_group(self) -> None:
|
2021-02-12 08:20:45 +01:00
|
|
|
user_group = self.create_user_group_for_test("support")
|
|
|
|
othello = self.example_user("othello")
|
2017-09-25 09:47:15 +02:00
|
|
|
self.assertTrue(check_remove_user_from_user_group(othello, user_group))
|
|
|
|
self.assertFalse(check_remove_user_from_user_group(othello, user_group))
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
with mock.patch(
|
2021-02-12 08:20:45 +01:00
|
|
|
"zerver.lib.user_groups.remove_user_from_user_group", side_effect=Exception
|
2021-02-12 08:19:30 +01:00
|
|
|
):
|
2017-09-25 09:47:15 +02:00
|
|
|
self.assertFalse(check_remove_user_from_user_group(othello, user_group))
|
2017-11-01 10:04:16 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2017-11-01 10:04:16 +01:00
|
|
|
class UserGroupAPITestCase(ZulipTestCase):
|
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)
|
2017-11-13 07:49:01 +01:00
|
|
|
self.assert_length(UserGroup.objects.all(), 2)
|
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")
|
2017-11-13 07:49:01 +01:00
|
|
|
self.assert_length(UserGroup.objects.all(), 2)
|
2017-11-01 10:04:16 +01:00
|
|
|
|
|
|
|
# Test we cannot add hamlet again
|
|
|
|
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.")
|
2017-11-13 07:49:01 +01:00
|
|
|
self.assert_length(UserGroup.objects.all(), 2)
|
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
|
|
|
|
2018-06-14 08:35:05 +02:00
|
|
|
def test_user_group_create_by_guest_user(self) -> None:
|
2021-02-12 08:20:45 +01:00
|
|
|
guest_user = self.example_user("polonius")
|
2018-06-14 08:35:05 +02:00
|
|
|
|
|
|
|
# Guest users can't create user group
|
2020-03-06 18:40:46 +01:00
|
|
|
self.login_user(guest_user)
|
2018-06-14 08:35:05 +02:00
|
|
|
params = {
|
2021-02-12 08:20:45 +01:00
|
|
|
"name": "support",
|
|
|
|
"members": orjson.dumps([guest_user.id]).decode(),
|
|
|
|
"description": "Support team",
|
2018-06-14 08:35:05 +02:00
|
|
|
}
|
2021-02-12 08:20:45 +01:00
|
|
|
result = self.client_post("/json/user_groups/create", info=params)
|
2018-06-14 08:35:05 +02:00
|
|
|
self.assert_json_error(result, "Not allowed for guest users")
|
|
|
|
|
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-02-19 13:38:18 +01:00
|
|
|
self.logout()
|
|
|
|
# Test when user not a member of user group tries to modify it
|
2021-02-12 08:20:45 +01:00
|
|
|
cordelia = self.example_user("cordelia")
|
2020-03-06 18:40:46 +01:00
|
|
|
self.login_user(cordelia)
|
2018-02-19 13:38:18 +01:00
|
|
|
params = {
|
2021-02-12 08:20:45 +01:00
|
|
|
"name": "help",
|
|
|
|
"description": "Troubleshooting",
|
2018-02-19 13:38:18 +01:00
|
|
|
}
|
2021-02-12 08:20:45 +01:00
|
|
|
result = self.client_patch(f"/json/user_groups/{user_group.id}", info=params)
|
2021-02-12 08:19:30 +01:00
|
|
|
self.assert_json_error(
|
|
|
|
result, "Only group members and organization administrators can administer this group."
|
|
|
|
)
|
2018-02-19 13:38:18 +01:00
|
|
|
|
|
|
|
self.logout()
|
|
|
|
# Test when organization admin tries to modify group
|
2021-02-12 08:20:45 +01:00
|
|
|
iago = self.example_user("iago")
|
2020-03-06 18:40:46 +01:00
|
|
|
self.login_user(iago)
|
2018-02-19 13:38:18 +01:00
|
|
|
params = {
|
2021-02-12 08:20:45 +01:00
|
|
|
"name": "help",
|
|
|
|
"description": "Troubleshooting",
|
2018-02-19 13:38:18 +01:00
|
|
|
}
|
2021-02-12 08:20:45 +01:00
|
|
|
result = self.client_patch(f"/json/user_groups/{user_group.id}", info=params)
|
2018-02-19 13:38:18 +01:00
|
|
|
self.assert_json_success(result)
|
|
|
|
|
2018-06-14 08:35:05 +02:00
|
|
|
def test_user_group_update_by_guest_user(self) -> None:
|
2021-02-12 08:20:45 +01:00
|
|
|
hamlet = self.example_user("hamlet")
|
|
|
|
guest_user = self.example_user("polonius")
|
2020-03-06 18:40:46 +01:00
|
|
|
self.login_user(hamlet)
|
2018-06-14 08:35:05 +02:00
|
|
|
params = {
|
2021-02-12 08:20:45 +01:00
|
|
|
"name": "support",
|
|
|
|
"members": orjson.dumps([hamlet.id, guest_user.id]).decode(),
|
|
|
|
"description": "Support team",
|
2018-06-14 08:35:05 +02:00
|
|
|
}
|
2021-02-12 08:20:45 +01:00
|
|
|
result = self.client_post("/json/user_groups/create", info=params)
|
2018-06-14 08:35:05 +02:00
|
|
|
self.assert_json_success(result)
|
2021-02-12 08:20:45 +01:00
|
|
|
user_group = UserGroup.objects.get(name="support")
|
2018-06-14 08:35:05 +02:00
|
|
|
|
|
|
|
# Guest user can't edit any detail of an user group
|
2020-03-06 18:40:46 +01:00
|
|
|
self.login_user(guest_user)
|
2018-06-14 08:35:05 +02:00
|
|
|
params = {
|
2021-02-12 08:20:45 +01:00
|
|
|
"name": "help",
|
|
|
|
"description": "Troubleshooting team",
|
2018-06-14 08:35:05 +02:00
|
|
|
}
|
2021-02-12 08:20:45 +01:00
|
|
|
result = self.client_patch(f"/json/user_groups/{user_group.id}", info=params)
|
2018-06-14 08:35:05 +02:00
|
|
|
self.assert_json_error(result, "Not allowed for guest users")
|
|
|
|
|
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
|
2017-11-13 07:49:01 +01:00
|
|
|
self.assertEqual(UserGroup.objects.count(), 2)
|
|
|
|
self.assertEqual(UserGroupMembership.objects.count(), 3)
|
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)
|
2017-11-13 07:49:01 +01:00
|
|
|
self.assertEqual(UserGroup.objects.count(), 1)
|
|
|
|
self.assertEqual(UserGroupMembership.objects.count(), 2)
|
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
|
|
|
|
2018-02-19 13:38:18 +01:00
|
|
|
# Test when user not a member of user group tries to delete it
|
|
|
|
params = {
|
2021-02-12 08:20:45 +01:00
|
|
|
"name": "Development",
|
|
|
|
"members": orjson.dumps([hamlet.id]).decode(),
|
|
|
|
"description": "Development team",
|
2018-02-19 13:38:18 +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="Development")
|
2018-02-19 13:38:18 +01:00
|
|
|
self.assertEqual(UserGroup.objects.count(), 2)
|
|
|
|
self.logout()
|
2021-02-12 08:20:45 +01:00
|
|
|
cordelia = self.example_user("cordelia")
|
2020-03-06 18:40:46 +01:00
|
|
|
self.login_user(cordelia)
|
2018-02-19 13:38:18 +01:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
result = self.client_delete(f"/json/user_groups/{user_group.id}")
|
2021-02-12 08:19:30 +01:00
|
|
|
self.assert_json_error(
|
|
|
|
result, "Only group members and organization administrators can administer this group."
|
|
|
|
)
|
2018-02-19 13:38:18 +01:00
|
|
|
self.assertEqual(UserGroup.objects.count(), 2)
|
|
|
|
|
|
|
|
self.logout()
|
|
|
|
# Test when organization admin tries to delete group
|
2021-02-12 08:20:45 +01:00
|
|
|
iago = self.example_user("iago")
|
2020-03-06 18:40:46 +01:00
|
|
|
self.login_user(iago)
|
2018-02-19 13:38:18 +01:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
result = self.client_delete(f"/json/user_groups/{user_group.id}")
|
2018-02-19 13:38:18 +01:00
|
|
|
self.assert_json_success(result)
|
|
|
|
self.assertEqual(UserGroup.objects.count(), 1)
|
|
|
|
self.assertEqual(UserGroupMembership.objects.count(), 2)
|
|
|
|
|
2018-06-14 08:35:05 +02:00
|
|
|
def test_user_group_delete_by_guest_user(self) -> None:
|
2021-02-12 08:20:45 +01:00
|
|
|
hamlet = self.example_user("hamlet")
|
|
|
|
guest_user = self.example_user("polonius")
|
2020-03-06 18:40:46 +01:00
|
|
|
self.login_user(hamlet)
|
2018-06-14 08:35:05 +02:00
|
|
|
params = {
|
2021-02-12 08:20:45 +01:00
|
|
|
"name": "support",
|
|
|
|
"members": orjson.dumps([hamlet.id, guest_user.id]).decode(),
|
|
|
|
"description": "Support team",
|
2018-06-14 08:35:05 +02:00
|
|
|
}
|
2021-02-12 08:20:45 +01:00
|
|
|
result = self.client_post("/json/user_groups/create", info=params)
|
2018-06-14 08:35:05 +02:00
|
|
|
self.assert_json_success(result)
|
2021-02-12 08:20:45 +01:00
|
|
|
user_group = UserGroup.objects.get(name="support")
|
2018-06-14 08:35:05 +02:00
|
|
|
|
|
|
|
# Guest users can't delete any user group(not even those of which they are a member)
|
2020-03-06 18:40:46 +01:00
|
|
|
self.login_user(guest_user)
|
2021-02-12 08:20:45 +01:00
|
|
|
result = self.client_delete(f"/json/user_groups/{user_group.id}")
|
2018-06-14 08:35:05 +02:00
|
|
|
self.assert_json_error(result, "Not allowed for guest users")
|
|
|
|
|
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
|
2017-11-13 07:49:01 +01:00
|
|
|
self.assertEqual(UserGroupMembership.objects.count(), 3)
|
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)
|
2017-11-13 07:49:01 +01:00
|
|
|
self.assertEqual(UserGroupMembership.objects.count(), 4)
|
2017-11-02 08:53:30 +01:00
|
|
|
members = get_memberships_of_users(user_group, [hamlet, othello])
|
|
|
|
self.assertEqual(len(members), 2)
|
|
|
|
|
|
|
|
# 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")
|
2017-11-13 07:49:01 +01:00
|
|
|
self.assertEqual(UserGroupMembership.objects.count(), 4)
|
2017-11-02 08:53:30 +01:00
|
|
|
members = get_memberships_of_users(user_group, [hamlet, othello])
|
|
|
|
self.assertEqual(len(members), 2)
|
|
|
|
|
2018-02-19 13:38:18 +01:00
|
|
|
self.logout()
|
|
|
|
# Test when user not a member of user group tries to add members to it
|
2021-02-12 08:20:45 +01:00
|
|
|
cordelia = self.example_user("cordelia")
|
2020-03-06 18:40:46 +01:00
|
|
|
self.login_user(cordelia)
|
2018-02-19 13:38:18 +01:00
|
|
|
add = [cordelia.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)
|
2021-02-12 08:19:30 +01:00
|
|
|
self.assert_json_error(
|
|
|
|
result, "Only group members and organization administrators can administer this group."
|
|
|
|
)
|
2018-02-19 13:38:18 +01:00
|
|
|
self.assertEqual(UserGroupMembership.objects.count(), 4)
|
|
|
|
|
|
|
|
self.logout()
|
|
|
|
# Test when organization admin tries to add members to group
|
2021-02-12 08:20:45 +01:00
|
|
|
iago = self.example_user("iago")
|
2020-03-06 18:40:46 +01:00
|
|
|
self.login_user(iago)
|
2021-02-12 08:20:45 +01:00
|
|
|
aaron = self.example_user("aaron")
|
2018-02-19 13:38:18 +01:00
|
|
|
add = [aaron.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)
|
2018-02-19 13:38:18 +01:00
|
|
|
self.assertEqual(UserGroupMembership.objects.count(), 5)
|
|
|
|
members = get_memberships_of_users(user_group, [hamlet, othello, aaron])
|
|
|
|
self.assertEqual(len(members), 3)
|
2017-11-02 08:53:30 +01:00
|
|
|
|
2018-02-19 13:38:18 +01:00
|
|
|
# For normal testing we again login with hamlet
|
|
|
|
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)
|
2018-02-19 13:38:18 +01:00
|
|
|
self.assertEqual(UserGroupMembership.objects.count(), 4)
|
|
|
|
members = get_memberships_of_users(user_group, [hamlet, othello, aaron])
|
|
|
|
self.assertEqual(len(members), 2)
|
|
|
|
|
|
|
|
# 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")
|
2018-02-19 13:38:18 +01:00
|
|
|
self.assertEqual(UserGroupMembership.objects.count(), 4)
|
|
|
|
members = get_memberships_of_users(user_group, [hamlet, othello, aaron])
|
|
|
|
self.assertEqual(len(members), 2)
|
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
|
|
|
|
|
|
|
# Test when user not a member of user group tries to remove members
|
|
|
|
self.logout()
|
2020-03-06 18:40:46 +01:00
|
|
|
self.login_user(cordelia)
|
2021-02-12 08:20:45 +01:00
|
|
|
params = {"delete": orjson.dumps([hamlet.id]).decode()}
|
|
|
|
result = self.client_post(f"/json/user_groups/{user_group.id}/members", info=params)
|
2021-02-12 08:19:30 +01:00
|
|
|
self.assert_json_error(
|
|
|
|
result, "Only group members and organization administrators can administer this group."
|
|
|
|
)
|
2018-02-19 13:38:18 +01:00
|
|
|
self.assertEqual(UserGroupMembership.objects.count(), 4)
|
|
|
|
|
|
|
|
self.logout()
|
|
|
|
# Test when organization admin tries to remove members from group
|
2021-02-12 08:20:45 +01:00
|
|
|
iago = self.example_user("iago")
|
2020-03-06 18:40:46 +01:00
|
|
|
self.login_user(iago)
|
2021-02-12 08:20:45 +01:00
|
|
|
result = self.client_post(f"/json/user_groups/{user_group.id}/members", info=params)
|
2018-02-19 13:38:18 +01:00
|
|
|
self.assert_json_success(result)
|
|
|
|
self.assertEqual(UserGroupMembership.objects.count(), 3)
|
|
|
|
members = get_memberships_of_users(user_group, [hamlet, othello, aaron])
|
|
|
|
self.assertEqual(len(members), 1)
|
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"
|
|
|
|
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
|
|
|
|
|
|
|
ensure_stream(realm, stream_name)
|
|
|
|
|
|
|
|
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
|
|
|
client="test suite",
|
|
|
|
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
|
|
|
|
|
|
|
def test_only_admin_manage_groups(self) -> None:
|
2021-02-12 08:20:45 +01:00
|
|
|
iago = self.example_user("iago")
|
|
|
|
hamlet = self.example_user("hamlet")
|
|
|
|
cordelia = self.example_user("cordelia")
|
2020-03-06 18:40:46 +01:00
|
|
|
self.login_user(iago)
|
2021-02-12 08:19:30 +01:00
|
|
|
do_set_realm_property(
|
2021-02-12 08:20:45 +01:00
|
|
|
iago.realm, "user_group_edit_policy", Realm.USER_GROUP_EDIT_POLICY_ADMINS
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
2019-11-02 17:58:55 +01:00
|
|
|
|
|
|
|
params = {
|
2021-02-12 08:20:45 +01:00
|
|
|
"name": "support",
|
|
|
|
"members": orjson.dumps([iago.id, hamlet.id]).decode(),
|
|
|
|
"description": "Support team",
|
2019-11-02 17:58:55 +01:00
|
|
|
}
|
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
result = self.client_post("/json/user_groups/create", info=params)
|
2019-11-02 17:58:55 +01:00
|
|
|
self.assert_json_success(result)
|
2021-02-12 08:20:45 +01:00
|
|
|
user_group = UserGroup.objects.get(name="support")
|
2019-11-02 17:58:55 +01:00
|
|
|
|
|
|
|
# Test add member
|
2021-02-12 08:20:45 +01:00
|
|
|
params = {"add": orjson.dumps([cordelia.id]).decode()}
|
|
|
|
result = self.client_post(f"/json/user_groups/{user_group.id}/members", info=params)
|
2019-11-02 17:58:55 +01:00
|
|
|
self.assert_json_success(result)
|
|
|
|
|
|
|
|
# Test remove member
|
2021-02-12 08:20:45 +01:00
|
|
|
params = {"delete": orjson.dumps([cordelia.id]).decode()}
|
|
|
|
result = self.client_post(f"/json/user_groups/{user_group.id}/members", info=params)
|
2019-11-02 17:58:55 +01:00
|
|
|
self.assert_json_success(result)
|
|
|
|
|
|
|
|
# Test changing groups name
|
|
|
|
params = {
|
2021-02-12 08:20:45 +01:00
|
|
|
"name": "help",
|
|
|
|
"description": "Troubleshooting",
|
2019-11-02 17:58:55 +01:00
|
|
|
}
|
2021-02-12 08:20:45 +01:00
|
|
|
result = self.client_patch(f"/json/user_groups/{user_group.id}", info=params)
|
2019-11-02 17:58:55 +01:00
|
|
|
self.assert_json_success(result)
|
|
|
|
|
|
|
|
# Test delete a group
|
2021-02-12 08:20:45 +01:00
|
|
|
result = self.client_delete(f"/json/user_groups/{user_group.id}")
|
2019-11-02 17:58:55 +01:00
|
|
|
self.assert_json_success(result)
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
user_group = create_user_group(
|
2021-02-12 08:20:45 +01:00
|
|
|
name="support",
|
2021-02-12 08:19:30 +01:00
|
|
|
members=[hamlet, iago],
|
|
|
|
realm=iago.realm,
|
|
|
|
)
|
2019-11-02 17:58:55 +01:00
|
|
|
|
|
|
|
self.logout()
|
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
self.login("hamlet")
|
2019-11-02 17:58:55 +01:00
|
|
|
|
|
|
|
# Test creating a group
|
|
|
|
params = {
|
2021-02-12 08:20:45 +01:00
|
|
|
"name": "support2",
|
|
|
|
"members": orjson.dumps([hamlet.id]).decode(),
|
|
|
|
"description": "Support team",
|
2019-11-02 17:58:55 +01:00
|
|
|
}
|
2021-02-12 08:20:45 +01:00
|
|
|
result = self.client_post("/json/user_groups/create", info=params)
|
2019-11-02 17:58:55 +01:00
|
|
|
self.assert_json_error(result, "Must be an organization administrator")
|
|
|
|
|
|
|
|
# Test add member
|
2021-02-12 08:20:45 +01:00
|
|
|
params = {"add": orjson.dumps([cordelia.id]).decode()}
|
|
|
|
result = self.client_post(f"/json/user_groups/{user_group.id}/members", info=params)
|
2019-11-02 17:58:55 +01:00
|
|
|
self.assert_json_error(result, "Must be an organization administrator")
|
|
|
|
|
|
|
|
# Test delete a group
|
2021-02-12 08:20:45 +01:00
|
|
|
result = self.client_delete(f"/json/user_groups/{user_group.id}")
|
2019-11-02 17:58:55 +01:00
|
|
|
self.assert_json_error(result, "Must be an organization administrator")
|
|
|
|
|
|
|
|
# Test changing groups name
|
|
|
|
params = {
|
2021-02-12 08:20:45 +01:00
|
|
|
"name": "help",
|
|
|
|
"description": "Troubleshooting",
|
2019-11-02 17:58:55 +01:00
|
|
|
}
|
2021-02-12 08:20:45 +01:00
|
|
|
result = self.client_patch(f"/json/user_groups/{user_group.id}", info=params)
|
2019-11-02 17:58:55 +01:00
|
|
|
self.assert_json_error(result, "Must be an organization administrator")
|