def test_role_list_by_user_group(): 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())) g2 = assert_response_success(response) response = client.post(api_prefix, json=dict(name=uuid(), parent_id=g1['id'])) g3 = assert_response_success(response) response = client.post(api_prefix, json=dict(name=uuid(), parent_id=g3['id'])) g4 = assert_response_success(response) with override_get_user() as user: user.role_id_set.add(g2['id']) user.role_set.add(g2['name']) client = get_client() response = client.get(api_prefix) data = assert_response_success(response) assert len(data) == 1 with override_get_user() as user: user.role_id_set.add(g3['id']) user.role_set.add(g3['name']) client = get_client() response = client.get(api_prefix) data = assert_response_success(response) assert len(data) == 2 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(api_prefix) data = assert_response_success(response) assert len(data) == 3
def test_add_permission(): old = 0 with get_session_local() as session: old = session.query(PermissionDB).count() with override_get_user(is_superuser=True): client = get_client() for i in range(3): response = client.post(api_prefix, json=dict(name=uuid())) assert_response_success(response) with get_session_local() as session: assert session.query(PermissionDB).count() == old + 3
def test_add_role(): with override_get_user(is_superuser=True): client = get_client() response = client.post(api_prefix, json=dict(name=uuid())) g1 = assert_response_success(response) assert g1['grand_id'] == '' assert g1['parent_id'] == 0 response = client.post(api_prefix, json=dict(name=uuid())) g2 = assert_response_success(response) response = client.post(api_prefix, json=dict(name=uuid(), parent_id=g1['id'])) g3 = assert_response_success(response) assert g3['grand_id'] == f'|{g1["id"]}|' assert g3['parent_id'] == g1['id'] response = client.post(api_prefix, json=dict(name=uuid(), parent_id=g3['id'])) g4 = assert_response_success(response) assert g4['grand_id'] == f'|{g1["id"]}|{g3["id"]}|' assert g4['parent_id'] == g3['id'] with get_session_local() as session: assert session.query(RoleDB).count() == 4
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_update_permission(): with get_session_local() as session: permission = generate_permission() session.add(permission) session.commit() new_name = uuid() with override_get_user(is_superuser=True): client = get_client() response = client.put(f'{api_prefix}/{permission.id}', json=dict(name=new_name)) assert_response_success(response) session.refresh(permission) assert permission.name == new_name
def test_update_delete_not_find(): not_exist_id = -1 with override_get_user(is_superuser=True): client = get_client() response = client.post(f'{api_prefix}/{not_exist_id}/users:delete', json=[1, 2]) assert_response_fail(response) assert response.json()['code'] == 404 response = client.post(f'{api_prefix}/{not_exist_id}/users:add', json=[1, 2]) assert_response_fail(response) assert response.json()['code'] == 404 response = client.delete(f'{api_prefix}/{not_exist_id}') assert_response_fail(response) assert response.json()['code'] == 404 response = client.put(f'{api_prefix}/{not_exist_id}', json=dict(name=uuid())) assert_response_fail(response) assert response.json()['code'] == 404
def generate_permission(name=None): return PermissionDB(name=name or uuid())
def generate_role() -> RoleDB: return RoleDB(name=uuid())