def test_del_user_to_role(): with get_session_local() as session: user1 = generate_user() user2 = generate_user() user3 = generate_user() role1 = generate_role() role2 = generate_role() session.add_all((user1, user2, user3, role1, role2)) session.flush() session.add_all((generate_user2role(user1.id, role1.id), generate_user2role(user1.id, role1.id), generate_user2role(user2.id, role1.id), generate_user2role(user2.id, role2.id), generate_user2role(user3.id, role2.id) )) session.commit() with override_get_user(is_superuser=True): client = get_client() response = client.post(f'{api_prefix}/{role1.id}/users:delete', json=[user1.id]) assert_response_success(response) response = client.get(f'{api_prefix}/{role1.id}/users') data = assert_response_success(response) assert len(data) == 1 response = client.post(f'{api_prefix}/{role1.id}/users:delete', json=[user2.id, user3.id]) assert_response_success(response) response = client.get(f'{api_prefix}/{role1.id}/users') data = assert_response_success(response) assert len(data) == 0 response = client.get(f'{api_prefix}/{role2.id}/users') data = assert_response_success(response) assert len(data) == 2
def test_update_user_as_secretary_and_cancel_user_as_secretary_if_no_user_group(): with get_session_local() as session: u1 = generate_user() u2 = generate_user() u3 = generate_user() u1.user_identity = UserIdentity.ADMIN u3.user_identity = UserIdentity.ADMIN g1 = generate_role() g2 = generate_role() session.add_all((u1, u2, u3, g1, g2)) session.commit() def assert_user_identity(u1_id, u2_id, u3_id, i1, i2, i3): with get_session_local() as session: u1 = session.query(UserDB).get(u1_id) u2 = session.query(UserDB).get(u2_id) u3 = session.query(UserDB).get(u3_id) assert u1.user_identity == i1 assert u2.user_identity == i2 assert u3.user_identity == i3 client = get_client() with override_get_user(is_superuser=True): # test add response = client.post(f'{api_prefix}/{g1.id}/users:add', json=[u1.id, u2.id, u3.id]) assert_response_success(response) assert_user_identity(u1.id, u2.id, u3.id, UserIdentity.COMMON_USER + UserIdentity.ADMIN, UserIdentity.COMMON_USER + UserIdentity.ADMIN, UserIdentity.COMMON_USER + UserIdentity.ADMIN) # test add more response = client.post(f'{api_prefix}/{g2.id}/users:add', json=[u2.id, u3.id]) assert_response_success(response) assert_user_identity(u1.id, u2.id, u3.id, UserIdentity.COMMON_USER + UserIdentity.ADMIN, UserIdentity.COMMON_USER + UserIdentity.ADMIN, UserIdentity.COMMON_USER + UserIdentity.ADMIN) # test delete response = client.post(f'{api_prefix}/{g1.id}/users:delete', json=[u1.id, u2.id]) assert_response_success(response) assert_user_identity(u1.id, u2.id, u3.id, UserIdentity.COMMON_USER, UserIdentity.ADMIN, UserIdentity.ADMIN) # test delete more response = client.post(f'{api_prefix}/{g2.id}/users:delete', json=[u2.id, u3.id]) assert_response_success(response) assert_user_identity(u1.id, u2.id, u3.id, UserIdentity.COMMON_USER, UserIdentity.COMMON_USER, UserIdentity.ADMIN) # test delete all response = client.post(f'{api_prefix}/{g1.id}/users:delete', json=[u2.id, u3.id]) assert_response_success(response) assert_user_identity(u1.id, u2.id, u3.id, UserIdentity.COMMON_USER, UserIdentity.COMMON_USER, UserIdentity.COMMON_USER)
def test_get_users_by_role_id_query(): with get_session_local() as session: user1 = generate_user() user2 = generate_user() user3 = generate_user() role1 = generate_role() session.add_all((user1, user2, user3, role1)) session.flush() session.add(User2RoleDB(user_id=user1.id, role_id=role1.id)) session.add(User2RoleDB(user_id=user2.id, role_id=role1.id)) session.commit() paginate = get_users_by_role_id(session, role1.id, PageInfo(page_id=1, page_size=10)) assert len(paginate.items) == 2 for user in paginate.items: assert user.id != user3.id
def test_search_user_search(): with get_session_local() as session: role = generate_role() session.add(role) user_line = [generate_user() for _ in range(10)] for i, user in enumerate(user_line): user.name = str(i) user.sex = i % 2 + 1 user2role_line = [] session.add_all(user_line) session.flush() for user in user_line: user2role_line.append( generate_user2role(user_id=user.id, role_id=role.id)) session.add_all(user2role_line) session.commit() client = get_client() with override_get_user(): response = client.get(f'{api_prefix}/', params={'role_id': role.id}) assert_response_fail(response) with override_get_user() as user: user.role_set.add(MANAGE_ROLE_PERMISSION_NAME.format(role_id=role.id)) user.role_id_set.add(role.id) response = client.get(f'{api_prefix}/', params={'role_id': role.id}) data = assert_response_success(response) assert len(data) == 10 response = client.get(f'{api_prefix}/', params={ 'role_id': role.id, 'sex': 1 }) data = assert_response_success(response) assert len(data) == 5
def test_del_role(): with get_session_local() as session: user11 = generate_user() user12 = generate_user() user2 = generate_user() user3 = generate_user() role1 = generate_role() role2 = generate_role() session.add_all((user11, user12, user2, user3, role1, role2)) session.flush() session.add_all((generate_user2role(user11.id, role1.id), generate_user2role(user12.id, role1.id), generate_user2role(user2.id, role2.id) )) session.commit() with override_get_user(is_superuser=True): client = get_client() response = client.delete(f'{api_prefix}/{role1.id}') assert_response_success(response) r: User2RoleDB = session.query(User2RoleDB).one() assert r.user_id == user2.id assert r.role_id == role2.id
def test_role_user_list(): with override_get_user(is_superuser=True): client = get_client() response = client.post(api_prefix, json=dict(name=uuid())) g1 = assert_response_success(response) response = client.post(api_prefix, json=dict(name=uuid(), parent_id=g1['id'])) g2 = assert_response_success(response) with get_session_local() as session: user11 = generate_user() user12 = generate_user() user2 = generate_user() user3 = generate_user() session.add_all((user11, user12, user2, user3)) session.flush() session.add_all((generate_user2role(user11.id, g1['id']), generate_user2role(user12.id, g1['id']), generate_user2role(user2.id, g2['id']), generate_user2role(user2.id, g2['id']) )) session.commit() with override_get_user() as user: user.role_id_set.add(g1['id']) user.role_set.add(g1['name']) client = get_client() response = client.get(f'{api_prefix}/{g1["id"]}/users') data = assert_response_success(response) assert len(data) == 2 response = client.get(f'{api_prefix}/{g2["id"]}/users') data = assert_response_success(response) print(data) assert len(data) == 1 assert data[0]['id'] == user2.id
def test_get_user_with_permission_and_group_by_id(): with get_session_local() as session: # simple user = generate_user() role = generate_role() permission1 = generate_permission() permission2 = generate_permission() session.add_all((user, role, permission1, permission2)) session.flush() session.add_all((generate_user2role(user.id, role.id), generate_permission2role(permission1.id, role.id), generate_permission2role(permission2.id, role.id))) session.commit() user = get_user_with_permission_and_group_by_id(session, user.id) assert user is not None assert user.permission_set == {permission1.name, permission2.name} assert user.role_set == {role.name} clean_all() with get_session_local() as session: # more group and permission user = generate_user() role1 = generate_role() role2 = generate_role() permission1 = generate_permission() permission2 = generate_permission() permission3 = generate_permission() session.add_all( (user, role1, role2, permission1, permission2, permission3)) session.flush() session.add_all((generate_user2role(user.id, role1.id), generate_user2role(user.id, role2.id), generate_permission2role(permission1.id, role1.id), generate_permission2role(permission2.id, role1.id), generate_permission2role(permission3.id, role2.id))) session.commit() user = get_user_with_permission_and_group_by_id(session, user.id) assert user is not None assert user.permission_set == { permission1.name, permission2.name, permission3.name } assert user.role_set == {role1.name, role2.name} clean_all() with get_session_local() as session: # has group, no permission user = generate_user() role1 = generate_role() role2 = generate_role() session.add_all((user, role1, role2)) session.flush() session.add_all(( generate_user2role(user.id, role1.id), generate_user2role(user.id, role2.id), )) session.commit() user = get_user_with_permission_and_group_by_id(session, user.id) assert user is not None assert len(user.permission_set) == 0 assert user.role_set == {role1.name, role2.name} clean_all() with get_session_local() as session: # no group, no permission user = generate_user() session.add(user) session.commit() user = get_user_with_permission_and_group_by_id(session, user.id) assert user is not None assert len(user.permission_set) == 0 assert len(user.role_set) == 0