20ce6bccef
后端: - 删除 StudyRolePermission 模型和 project_permissions API 文件 - 重写 project_permissions.py core,移除所有模块级权限函数 - 重写 permission_cache.py,移除模块级权限缓存逻辑 - 新增权限模板功能(PermissionTemplate 模型、API、服务层) - 新增 permission_templates 数据库迁移 - 迁移 8 个模块(attachments、audit_logs、dashboard、faqs、 faq_categories、fees_attachments、knowledge_notes、 material_equipments、overview、subject_histories、subject_pds) 至接口级权限检查 - 删除所有模块级权限相关测试文件,新增权限模板测试 前端: - 删除 ProjectPermissions.vue 和 ProjectPermissionsModule.vue - 重写 projectRoutePermissions.ts,改为基于接口级权限格式 - 更新 store/study.ts、router/index.ts、AuditLogs.vue、Projects.vue 中的权限 API 调用,从 fetchProjectRolePermissions 改为 fetchApiEndpointPermissions - 清理 types/api.ts 中的旧模块级权限类型定义 - 新增 PermissionTemplateSelector.vue 组件 - 更新权限管理页面,移除模块级权限 tab Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
345 lines
9.5 KiB
Python
345 lines
9.5 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_prerequisite_permission_not_configured(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()
|
|
|
|
# 验证权限(包含前置权限检查)
|
|
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_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_prerequisite_missing_not_configured(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 "sites:read" in missing
|