def test_update_members_of_user_group(self) -> None: hamlet = self.example_user("hamlet") self.login("hamlet") params = { "name": "support", "members": orjson.dumps([hamlet.id]).decode(), "description": "Support team", } self.client_post("/json/user_groups/create", info=params) user_group = UserGroup.objects.get(name="support") # Test add members self.assertEqual(UserGroupMembership.objects.filter(user_group=user_group).count(), 1) othello = self.example_user("othello") add = [othello.id] params = {"add": orjson.dumps(add).decode()} result = self.client_post(f"/json/user_groups/{user_group.id}/members", info=params) self.assert_json_success(result) self.assertEqual(UserGroupMembership.objects.filter(user_group=user_group).count(), 2) members = get_direct_memberships_of_users(user_group, [hamlet, othello]) self.assert_length(members, 2) # Test adding a member already there. result = self.client_post(f"/json/user_groups/{user_group.id}/members", info=params) self.assert_json_error(result, f"User {othello.id} is already a member of this group") self.assertEqual(UserGroupMembership.objects.filter(user_group=user_group).count(), 2) members = get_direct_memberships_of_users(user_group, [hamlet, othello]) self.assert_length(members, 2) aaron = self.example_user("aaron") # For normal testing we again log in with hamlet self.logout() self.login_user(hamlet) # Test remove members params = {"delete": orjson.dumps([othello.id]).decode()} result = self.client_post(f"/json/user_groups/{user_group.id}/members", info=params) self.assert_json_success(result) self.assertEqual(UserGroupMembership.objects.filter(user_group=user_group).count(), 1) members = get_direct_memberships_of_users(user_group, [hamlet, othello, aaron]) self.assert_length(members, 1) # Test remove a member that's already removed 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, f"There is no member '{othello.id}' in this user group") self.assertEqual(UserGroupMembership.objects.filter(user_group=user_group).count(), 1) members = get_direct_memberships_of_users(user_group, [hamlet, othello, aaron]) self.assert_length(members, 1) # Test when nothing is provided result = self.client_post(f"/json/user_groups/{user_group.id}/members", info={}) msg = 'Nothing to do. Specify at least one of "add" or "delete".' self.assert_json_error(result, 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) self.assertEqual( UserGroupMembership.objects.filter(user_group=user_group).count(), 1 ) members = get_direct_memberships_of_users(user_group, [aaron, othello]) self.assert_length(members, 1) else: self.assert_json_error(result, error_msg)
def add_members_to_group_backend(request: HttpRequest, user_profile: UserProfile, user_group_id: int, members: Sequence[int]) -> HttpResponse: if not members: return json_success() user_group = access_user_group_by_id(user_group_id, user_profile) user_profiles = user_ids_to_users(members, user_profile.realm) existing_member_ids = set( get_direct_memberships_of_users(user_group, user_profiles)) for user_profile in user_profiles: if user_profile.id in existing_member_ids: raise JsonableError( _("User {user_id} is already a member of this group").format( user_id=user_profile.id, )) bulk_add_members_to_user_group(user_group, user_profiles) return json_success()