747dd55225
- deps 新增 list_active_pm_study_ids、is_active_project_pm 与 require_admin_or_any_project_pm 依赖,用于把 PM 项目范围带进鉴权与 监控。get_cra_site_scope 内部延后导入 site CRUD,避免循环依赖。 - system_permissions API 改用 PM/ADMIN 双角色入口,permissions/monitoring 系统级权限新增 PM 配额,并细化访问日志、告警、监控指标的可见范围。 - members API 调整:项目 PM 仅可管理低于 PM 的项目角色,禁止互相 改写或授予 PM。 - api_permissions API 增加 GET /api-permissions/me,返回当前用户在该 项目的有效权限矩阵;保存权限矩阵时校验 PM 行为不被篡改。 - core/api_permissions:新增立项配置接口键、PM 默认拥有的监控/权限 系统级条目,并在权限元信息中标注 PM 共享角色。 - core/project_permissions:role_has_api_permission 命中默认角色矩阵; replace_api_endpoint_permissions 改为部分更新且永不持久化 ADMIN/PM。 - studies setup-config 各端点改用接口级权限装饰器,与新的 setup_config 权限键对齐。permission_monitor 新增 get_metrics 摘要供 PM 视图调用。 - 测试:新增 test_admin_pm_permissions 覆盖 PM 系统级权限、监控范围和 成员管理边界;conftest 兼容 SA_UUID 列;权限相关用例同步移除已失效 的 module_permission 链路。 Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
385 lines
11 KiB
Python
385 lines
11 KiB
Python
"""单元测试:前置权限检查"""
|
|
|
|
import pytest
|
|
import uuid
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
from app.core.project_permissions import (
|
|
role_has_api_permission,
|
|
get_missing_prerequisites,
|
|
)
|
|
from app.models.api_endpoint_permission import ApiEndpointPermission
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_prerequisite_permission_satisfied(db_session: AsyncSession):
|
|
"""测试前置权限满足的情况"""
|
|
study_id = uuid.uuid4()
|
|
|
|
# 创建权限:主权限 + 前置权限都允许
|
|
main_perm = ApiEndpointPermission(
|
|
study_id=study_id,
|
|
role="CRA",
|
|
endpoint_key="subjects:create",
|
|
allowed=True,
|
|
)
|
|
prereq_perm = ApiEndpointPermission(
|
|
study_id=study_id,
|
|
role="CRA",
|
|
endpoint_key="sites:read",
|
|
allowed=True,
|
|
)
|
|
db_session.add(main_perm)
|
|
db_session.add(prereq_perm)
|
|
await db_session.commit()
|
|
|
|
# 验证权限(包含前置权限检查)
|
|
result = await role_has_api_permission(
|
|
db_session, study_id, "CRA", "subjects:create", check_prerequisites=True
|
|
)
|
|
assert result is True
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_prerequisite_permission_missing(db_session: AsyncSession):
|
|
"""测试前置权限缺失的情况"""
|
|
study_id = uuid.uuid4()
|
|
|
|
# 创建权限:主权限允许,但前置权限不允许
|
|
main_perm = ApiEndpointPermission(
|
|
study_id=study_id,
|
|
role="CRA",
|
|
endpoint_key="subjects:create",
|
|
allowed=True,
|
|
)
|
|
prereq_perm = ApiEndpointPermission(
|
|
study_id=study_id,
|
|
role="CRA",
|
|
endpoint_key="sites:read",
|
|
allowed=False,
|
|
)
|
|
db_session.add(main_perm)
|
|
db_session.add(prereq_perm)
|
|
await db_session.commit()
|
|
|
|
# 验证权限(包含前置权限检查)
|
|
result = await role_has_api_permission(
|
|
db_session, study_id, "CRA", "subjects:create", check_prerequisites=True
|
|
)
|
|
assert result is False
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_default_prerequisite_permission_satisfies_when_not_overridden(db_session: AsyncSession):
|
|
"""预设角色未配置前置权限时,应使用默认权限矩阵判断"""
|
|
study_id = uuid.uuid4()
|
|
|
|
# CRA 默认拥有 sites:read,因此未显式配置前置权限时仍满足前置条件。
|
|
main_perm = ApiEndpointPermission(
|
|
study_id=study_id,
|
|
role="CRA",
|
|
endpoint_key="subjects:create",
|
|
allowed=True,
|
|
)
|
|
db_session.add(main_perm)
|
|
await db_session.commit()
|
|
|
|
# 验证权限(包含前置权限检查)
|
|
result = await role_has_api_permission(
|
|
db_session, study_id, "CRA", "subjects:create", check_prerequisites=True
|
|
)
|
|
assert result is True
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_custom_role_prerequisite_permission_not_configured(db_session: AsyncSession):
|
|
"""自定义角色无默认前置权限时,应被前置权限拦截"""
|
|
study_id = uuid.uuid4()
|
|
|
|
main_perm = ApiEndpointPermission(
|
|
study_id=study_id,
|
|
role="DATA_MANAGER",
|
|
endpoint_key="subjects:create",
|
|
allowed=True,
|
|
)
|
|
db_session.add(main_perm)
|
|
await db_session.commit()
|
|
|
|
result = await role_has_api_permission(
|
|
db_session, study_id, "DATA_MANAGER", "subjects:create", check_prerequisites=True
|
|
)
|
|
assert result is False
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_multiple_prerequisites_all_satisfied(db_session: AsyncSession):
|
|
"""测试多个前置权限都满足的情况"""
|
|
study_id = uuid.uuid4()
|
|
|
|
# 创建权限:主权限 + 所有前置权限都允许
|
|
main_perm = ApiEndpointPermission(
|
|
study_id=study_id,
|
|
role="CRA",
|
|
endpoint_key="visits:create",
|
|
allowed=True,
|
|
)
|
|
prereq1 = ApiEndpointPermission(
|
|
study_id=study_id,
|
|
role="CRA",
|
|
endpoint_key="subjects:read",
|
|
allowed=True,
|
|
)
|
|
prereq2 = ApiEndpointPermission(
|
|
study_id=study_id,
|
|
role="CRA",
|
|
endpoint_key="sites:read",
|
|
allowed=True,
|
|
)
|
|
db_session.add(main_perm)
|
|
db_session.add(prereq1)
|
|
db_session.add(prereq2)
|
|
await db_session.commit()
|
|
|
|
# 验证权限
|
|
result = await role_has_api_permission(
|
|
db_session, study_id, "CRA", "visits:create", check_prerequisites=True
|
|
)
|
|
assert result is True
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_multiple_prerequisites_one_missing(db_session: AsyncSession):
|
|
"""测试多个前置权限中有一个缺失的情况"""
|
|
study_id = uuid.uuid4()
|
|
|
|
# 创建权限:主权限允许,但其中一个前置权限不允许
|
|
main_perm = ApiEndpointPermission(
|
|
study_id=study_id,
|
|
role="CRA",
|
|
endpoint_key="visits:create",
|
|
allowed=True,
|
|
)
|
|
prereq1 = ApiEndpointPermission(
|
|
study_id=study_id,
|
|
role="CRA",
|
|
endpoint_key="subjects:read",
|
|
allowed=True,
|
|
)
|
|
prereq2 = ApiEndpointPermission(
|
|
study_id=study_id,
|
|
role="CRA",
|
|
endpoint_key="sites:read",
|
|
allowed=False,
|
|
)
|
|
db_session.add(main_perm)
|
|
db_session.add(prereq1)
|
|
db_session.add(prereq2)
|
|
await db_session.commit()
|
|
|
|
# 验证权限
|
|
result = await role_has_api_permission(
|
|
db_session, study_id, "CRA", "visits:create", check_prerequisites=True
|
|
)
|
|
assert result is False
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_get_missing_prerequisites_empty(db_session: AsyncSession):
|
|
"""测试获取缺失的前置权限 - 无缺失"""
|
|
study_id = uuid.uuid4()
|
|
|
|
# 创建权限:所有权限都允许
|
|
main_perm = ApiEndpointPermission(
|
|
study_id=study_id,
|
|
role="CRA",
|
|
endpoint_key="subjects:create",
|
|
allowed=True,
|
|
)
|
|
prereq_perm = ApiEndpointPermission(
|
|
study_id=study_id,
|
|
role="CRA",
|
|
endpoint_key="sites:read",
|
|
allowed=True,
|
|
)
|
|
db_session.add(main_perm)
|
|
db_session.add(prereq_perm)
|
|
await db_session.commit()
|
|
|
|
# 获取缺失的前置权限
|
|
missing = await get_missing_prerequisites(
|
|
db_session, study_id, "CRA", "subjects:create"
|
|
)
|
|
assert missing == []
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_get_missing_prerequisites_single(db_session: AsyncSession):
|
|
"""测试获取缺失的前置权限 - 单个缺失"""
|
|
study_id = uuid.uuid4()
|
|
|
|
# 创建权限:前置权限不允许
|
|
main_perm = ApiEndpointPermission(
|
|
study_id=study_id,
|
|
role="CRA",
|
|
endpoint_key="subjects:create",
|
|
allowed=True,
|
|
)
|
|
prereq_perm = ApiEndpointPermission(
|
|
study_id=study_id,
|
|
role="CRA",
|
|
endpoint_key="sites:read",
|
|
allowed=False,
|
|
)
|
|
db_session.add(main_perm)
|
|
db_session.add(prereq_perm)
|
|
await db_session.commit()
|
|
|
|
# 获取缺失的前置权限
|
|
missing = await get_missing_prerequisites(
|
|
db_session, study_id, "CRA", "subjects:create"
|
|
)
|
|
assert missing == ["sites:read"]
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_get_missing_prerequisites_multiple(db_session: AsyncSession):
|
|
"""测试获取缺失的前置权限 - 多个缺失"""
|
|
study_id = uuid.uuid4()
|
|
|
|
# 创建权限:两个前置权限都不允许
|
|
main_perm = ApiEndpointPermission(
|
|
study_id=study_id,
|
|
role="CRA",
|
|
endpoint_key="visits:create",
|
|
allowed=True,
|
|
)
|
|
prereq1 = ApiEndpointPermission(
|
|
study_id=study_id,
|
|
role="CRA",
|
|
endpoint_key="subjects:read",
|
|
allowed=False,
|
|
)
|
|
prereq2 = ApiEndpointPermission(
|
|
study_id=study_id,
|
|
role="CRA",
|
|
endpoint_key="sites:read",
|
|
allowed=False,
|
|
)
|
|
db_session.add(main_perm)
|
|
db_session.add(prereq1)
|
|
db_session.add(prereq2)
|
|
await db_session.commit()
|
|
|
|
# 获取缺失的前置权限
|
|
missing = await get_missing_prerequisites(
|
|
db_session, study_id, "CRA", "visits:create"
|
|
)
|
|
assert set(missing) == {"subjects:read", "sites:read"}
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_prerequisite_check_disabled(db_session: AsyncSession):
|
|
"""测试禁用前置权限检查"""
|
|
study_id = uuid.uuid4()
|
|
|
|
# 创建权限:主权限允许,前置权限不允许
|
|
main_perm = ApiEndpointPermission(
|
|
study_id=study_id,
|
|
role="CRA",
|
|
endpoint_key="subjects:create",
|
|
allowed=True,
|
|
)
|
|
prereq_perm = ApiEndpointPermission(
|
|
study_id=study_id,
|
|
role="CRA",
|
|
endpoint_key="sites:read",
|
|
allowed=False,
|
|
)
|
|
db_session.add(main_perm)
|
|
db_session.add(prereq_perm)
|
|
await db_session.commit()
|
|
|
|
# 验证权限(禁用前置权限检查)
|
|
result = await role_has_api_permission(
|
|
db_session, study_id, "CRA", "subjects:create", check_prerequisites=False
|
|
)
|
|
assert result is True
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_admin_bypasses_prerequisites(db_session: AsyncSession):
|
|
"""测试ADMIN角色绕过前置权限检查"""
|
|
study_id = uuid.uuid4()
|
|
|
|
# 不创建任何权限
|
|
|
|
# 验证权限(ADMIN应该总是被允许)
|
|
result = await role_has_api_permission(
|
|
db_session, study_id, "ADMIN", "subjects:create", check_prerequisites=True
|
|
)
|
|
assert result is True
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_prerequisite_with_no_prerequisites_operation(db_session: AsyncSession):
|
|
"""测试没有前置权限的操作"""
|
|
study_id = uuid.uuid4()
|
|
|
|
# 创建权限:操作允许(没有前置权限)
|
|
perm = ApiEndpointPermission(
|
|
study_id=study_id,
|
|
role="CRA",
|
|
endpoint_key="sites:read",
|
|
allowed=True,
|
|
)
|
|
db_session.add(perm)
|
|
await db_session.commit()
|
|
|
|
# 验证权限
|
|
result = await role_has_api_permission(
|
|
db_session, study_id, "CRA", "sites:read", check_prerequisites=True
|
|
)
|
|
assert result is True
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_default_prerequisite_not_reported_missing_when_not_overridden(db_session: AsyncSession):
|
|
"""预设角色默认拥有前置权限时,不应报告缺失"""
|
|
study_id = uuid.uuid4()
|
|
|
|
# 创建权限:主权限允许,前置权限未配置
|
|
main_perm = ApiEndpointPermission(
|
|
study_id=study_id,
|
|
role="CRA",
|
|
endpoint_key="subjects:create",
|
|
allowed=True,
|
|
)
|
|
db_session.add(main_perm)
|
|
await db_session.commit()
|
|
|
|
# 获取缺失的前置权限
|
|
missing = await get_missing_prerequisites(
|
|
db_session, study_id, "CRA", "subjects:create"
|
|
)
|
|
assert missing == []
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_custom_role_prerequisite_missing_not_configured(db_session: AsyncSession):
|
|
"""自定义角色无默认前置权限时,应报告缺失"""
|
|
study_id = uuid.uuid4()
|
|
|
|
main_perm = ApiEndpointPermission(
|
|
study_id=study_id,
|
|
role="DATA_MANAGER",
|
|
endpoint_key="subjects:create",
|
|
allowed=True,
|
|
)
|
|
db_session.add(main_perm)
|
|
await db_session.commit()
|
|
|
|
missing = await get_missing_prerequisites(
|
|
db_session, study_id, "DATA_MANAGER", "subjects:create"
|
|
)
|
|
assert "sites:read" in missing
|