移除QA预设角色并收紧角色模板编辑

This commit is contained in:
Cheng Zhou
2026-05-26 11:32:14 +08:00
parent 6c2f61eebd
commit 3c168565ce
26 changed files with 387 additions and 119 deletions
+82 -11
View File
@@ -67,6 +67,18 @@ async def test_create_template_creates_version_1(db_session, user_id):
assert template.versions[0].change_log == "初始版本"
@pytest.mark.asyncio
async def test_create_template_rejects_non_custom_role_type(db_session, user_id):
payload = PermissionTemplateCreate(
name="预设角色不允许新增",
template_type=TemplateType.ROLE,
permissions=SAMPLE_PERMISSIONS,
)
with pytest.raises(ValueError, match="只能新增自定义角色"):
await PermissionTemplateService.create_template(db_session, payload, user_id)
@pytest.mark.asyncio
async def test_create_template_invalid_endpoint_key(db_session, user_id):
payload = PermissionTemplateCreate(
@@ -95,16 +107,19 @@ async def test_create_template_invalid_permission_value(db_session, user_id):
@pytest.mark.asyncio
async def test_get_template_exists(db_session, user_id):
payload = PermissionTemplateCreate(
template = PermissionTemplate(
name="获取测试",
template_type=TemplateType.ROLE,
permissions=SAMPLE_PERMISSIONS,
)
created = await PermissionTemplateService.create_template(db_session, payload, user_id)
fetched = await PermissionTemplateService.get_template(db_session, created.id)
db_session.add(template)
await db_session.commit()
await db_session.refresh(template)
fetched = await PermissionTemplateService.get_template(db_session, template.id)
assert fetched is not None
assert fetched.id == created.id
assert fetched.id == template.id
assert fetched.name == "获取测试"
@@ -120,22 +135,52 @@ async def test_get_template_not_found(db_session):
@pytest.mark.asyncio
async def test_list_templates_filter_by_type(db_session, user_id):
await PermissionTemplateService.create_template(
db_session,
PermissionTemplateCreate(name="角色模板", template_type=TemplateType.ROLE, permissions=SAMPLE_PERMISSIONS),
user_id,
db_session.add(
PermissionTemplate(
name="角色模板",
template_type=TemplateType.ROLE,
is_system=True,
category="PM",
permissions=SAMPLE_PERMISSIONS,
)
)
await PermissionTemplateService.create_template(
db_session,
PermissionTemplateCreate(name="场景模板", template_type=TemplateType.SCENARIO, permissions=SAMPLE_PERMISSIONS),
PermissionTemplateCreate(name="自定义角色", template_type=TemplateType.CUSTOM, permissions=SAMPLE_PERMISSIONS),
user_id,
)
await db_session.commit()
role_templates = await PermissionTemplateService.list_templates(db_session, template_type=TemplateType.ROLE)
assert all(t.template_type == TemplateType.ROLE for t in role_templates)
assert len(role_templates) >= 1
@pytest.mark.asyncio
async def test_list_templates_excludes_qa_system_preset(db_session):
qa_template = PermissionTemplate(
name="质量保证",
template_type=TemplateType.ROLE,
is_system=True,
category="QA",
permissions={"QA": {"subjects:read": True}},
)
pm_template = PermissionTemplate(
name="项目经理",
template_type=TemplateType.ROLE,
is_system=True,
category="PM",
permissions={"PM": {"subjects:read": True}},
)
db_session.add_all([qa_template, pm_template])
await db_session.commit()
role_templates = await PermissionTemplateService.list_templates(db_session, template_type=TemplateType.ROLE)
assert "PM" in {template.category for template in role_templates}
assert "QA" not in {template.category for template in role_templates if template.is_system}
# ---------------------------------------------------------------------------
# 更新模板
# ---------------------------------------------------------------------------
@@ -175,7 +220,31 @@ async def test_update_template_permissions_creates_new_version(db_session, user_
@pytest.mark.asyncio
async def test_update_system_template_raises(db_session):
async def test_update_system_template_allows_name_and_description(db_session):
system_template = PermissionTemplate(
name="系统模板",
description="旧描述",
template_type=TemplateType.ROLE,
is_system=True,
permissions=SAMPLE_PERMISSIONS,
)
db_session.add(system_template)
await db_session.commit()
await db_session.refresh(system_template)
updated = await PermissionTemplateService.update_template(
db_session,
system_template.id,
PermissionTemplateUpdate(name="新名称", description="新描述"),
)
assert updated.name == "新名称"
assert updated.description == "新描述"
assert updated.permissions == SAMPLE_PERMISSIONS
@pytest.mark.asyncio
async def test_update_system_template_rejects_protected_fields(db_session):
system_template = PermissionTemplate(
name="系统模板",
template_type=TemplateType.ROLE,
@@ -188,7 +257,9 @@ async def test_update_system_template_raises(db_session):
with pytest.raises(ValueError, match="不允许修改系统预设模板"):
await PermissionTemplateService.update_template(
db_session, system_template.id, PermissionTemplateUpdate(name="改名")
db_session,
system_template.id,
PermissionTemplateUpdate(permissions={"PM": {"subjects:read": True}}),
)