refactor: 完成多维度代码优化与测试基建搭建

- 重构API路由与请求路径,统一批量状态更新接口
- 优化类型导入与参数继承逻辑,简化基础查询参数实现
- 修复日志格式化与权限校验细节问题
- 新增多模块接口测试用例,完善测试覆盖
- 调整环境配置与初始化脚本细节
- 简化服务层方法命名,统一代码风格
This commit is contained in:
zhangtao
2026-06-21 20:25:39 +08:00
parent 3389781834
commit 0b38612cb2
39 changed files with 45455 additions and 3197 deletions
+473
View File
@@ -0,0 +1,473 @@
"""
模块接口测试 —— module_system(系统管理)
认证数据测试:admin 登录后验证 CRUD 真实数据。
"""
from fastapi.testclient import TestClient
from conftest import assert_route # noqa: F401
class TestAuth:
"""认证授权接口(无需认证)。"""
def test_auth_captcha(self, test_client: TestClient) -> None:
assert_route(test_client, "GET", "/system/auth/captcha/get")
def test_auth_login(self, test_client: TestClient) -> None:
assert_route(
test_client,
"POST",
"/system/auth/login",
data={"username": "admin", "password": "admin123"},
expected_status=200,
)
def test_auth_logout(self, test_client: TestClient, auth_headers: dict) -> None:
assert_route(
test_client,
"POST",
"/system/auth/logout",
auth=auth_headers,
json={"token": "mock_token"},
)
def test_auth_refresh(self, test_client: TestClient) -> None:
assert_route(
test_client,
"POST",
"/system/auth/token/refresh",
json={"refresh_token": "mock_refresh_token"},
)
def test_auth_tenants(self, test_client: TestClient, auth_headers: dict) -> None:
assert_route(test_client, "GET", "/system/auth/tenants", auth=auth_headers)
def test_auth_select_tenant(self, test_client: TestClient, auth_headers: dict) -> None:
assert_route(
test_client,
"POST",
"/system/auth/select-tenant",
auth=auth_headers,
json={"tenant_id": 1},
)
def test_auth_tenant_register(self, test_client: TestClient) -> None:
assert_route(
test_client,
"POST",
"/system/auth/tenant/register",
json={"name": "测试租户", "username": "admin", "password": "admin123"},
)
def test_auth_auto_login_users(self, test_client: TestClient, auth_headers: dict) -> None:
assert_route(test_client, "GET", "/system/auth/auto-login/users", auth=auth_headers)
def test_auth_auto_login_token(self, test_client: TestClient, auth_headers: dict) -> None:
assert_route(test_client, "POST", "/system/auth/auto-login/token?user_id=1", auth=auth_headers)
def test_auth_forgot_password(self, test_client: TestClient) -> None:
assert_route(
test_client,
"POST",
"/system/auth/forgot-password",
json={"username": "admin"},
)
def test_auth_reset_password(self, test_client: TestClient) -> None:
assert_route(
test_client,
"POST",
"/system/auth/reset-password",
json={"token": "mock_reset_token", "new_password": "newpass123"},
)
class TestUser:
"""用户管理接口 — 数据验证。"""
def test_user_current_info(self, test_client: TestClient, auth_headers: dict) -> None:
resp = test_client.get("/system/user/current/info", headers=auth_headers)
assert resp.status_code == 200
data = resp.json()
assert data["data"]["username"] == "admin", f"期望 admin,实际 {data['data'].get('username')}"
def test_user_list(self, test_client: TestClient, auth_headers: dict) -> None:
assert_route(test_client, "GET", "/system/user/list", auth=auth_headers)
def test_user_detail(self, test_client: TestClient, auth_headers: dict) -> None:
resp = test_client.get("/system/user/detail/1", headers=auth_headers)
assert resp.status_code == 200
data = resp.json()
assert data["data"]["username"] == "super", f"id=1 应为 super,实际 {data['data'].get('username')}"
def test_user_create(self, test_client: TestClient, auth_headers: dict) -> None:
assert_route(
test_client, "POST", "/system/user/create", auth=auth_headers,
json={"username": "test_user", "password": "test123", "name": "测试", "dept_id": 1},
)
def test_user_update(self, test_client: TestClient, auth_headers: dict) -> None:
assert_route(
test_client, "PUT", "/system/user/update/1", auth=auth_headers,
json={"name": "更新用户"},
)
def test_user_delete(self, test_client: TestClient, auth_headers: dict) -> None:
assert_route(test_client, "DELETE", "/system/user/delete", auth=auth_headers, json=[9999])
def test_user_import_template(self, test_client: TestClient, auth_headers: dict) -> None:
assert_route(test_client, "GET", "/system/user/import/template", auth=auth_headers)
def test_user_export(self, test_client: TestClient, auth_headers: dict) -> None:
assert_route(test_client, "GET", "/system/user/export", auth=auth_headers)
def test_user_import_data(self, test_client: TestClient, auth_headers: dict) -> None:
assert_route(
test_client, "POST", "/system/user/import/data", auth=auth_headers,
json={"list": []},
)
def test_user_current_info_update(self, test_client: TestClient, auth_headers: dict) -> None:
assert_route(
test_client, "PUT", "/system/user/current/info/update", auth=auth_headers,
json={"name": "更新个人信息"},
)
def test_user_password_change(self, test_client: TestClient, auth_headers: dict) -> None:
assert_route(
test_client, "PUT", "/system/user/password/change", auth=auth_headers,
json={"old_password": "old", "new_password": "new123"},
)
def test_user_password_forget(self, test_client: TestClient) -> None:
assert_route(
test_client, "POST", "/system/user/password/forget",
json={"username": "admin"},
)
def test_user_password_reset(self, test_client: TestClient, auth_headers: dict) -> None:
assert_route(
test_client, "PUT", "/system/user/password/reset/1", auth=auth_headers,
json={"password": "newpass123"},
)
def test_user_register(self, test_client: TestClient) -> None:
assert_route(
test_client, "POST", "/system/user/register",
json={"username": "new_user", "password": "pass123", "name": "新用户"},
)
def test_user_status_batch(self, test_client: TestClient, auth_headers: dict) -> None:
assert_route(
test_client, "PATCH", "/system/user/status/batch", auth=auth_headers,
json={"ids": [1], "status": 1},
)
class TestRole:
"""角色管理接口 — 数据验证。"""
def test_role_list(self, test_client: TestClient, auth_headers: dict) -> None:
assert_route(test_client, "GET", "/system/role/list", auth=auth_headers)
def test_role_detail(self, test_client: TestClient, auth_headers: dict) -> None:
assert_route(test_client, "GET", "/system/role/detail/1", auth=auth_headers)
def test_role_create(self, test_client: TestClient, auth_headers: dict) -> None:
assert_route(
test_client, "POST", "/system/role/create", auth=auth_headers,
json={"name": "测试角色", "code": "test_role", "sort": 1},
)
def test_role_update(self, test_client: TestClient, auth_headers: dict) -> None:
assert_route(
test_client, "PUT", "/system/role/update/1", auth=auth_headers,
json={"name": "更新角色"},
)
def test_role_delete(self, test_client: TestClient, auth_headers: dict) -> None:
assert_route(test_client, "DELETE", "/system/role/delete", auth=auth_headers, json=[9999])
def test_role_export(self, test_client: TestClient, auth_headers: dict) -> None:
assert_route(test_client, "GET", "/system/role/export", auth=auth_headers)
def test_role_permission(self, test_client: TestClient, auth_headers: dict) -> None:
assert_route(
test_client, "PUT", "/system/role/permission", auth=auth_headers,
json={"role_id": 1, "menu_ids": [1, 2]},
)
def test_role_status_batch(self, test_client: TestClient, auth_headers: dict) -> None:
assert_route(
test_client, "PATCH", "/system/role/status/batch", auth=auth_headers,
json={"ids": [1], "status": 1},
)
class TestDept:
"""部门管理接口 — 数据验证。"""
def test_dept_tree(self, test_client: TestClient, auth_headers: dict) -> None:
assert_route(test_client, "GET", "/system/dept/tree", auth=auth_headers)
def test_dept_detail(self, test_client: TestClient, auth_headers: dict) -> None:
assert_route(test_client, "GET", "/system/dept/detail/1", auth=auth_headers)
def test_dept_create(self, test_client: TestClient, auth_headers: dict) -> None:
assert_route(
test_client, "POST", "/system/dept/create", auth=auth_headers,
json={"name": "测试部门", "parent_id": 0, "sort": 1},
)
def test_dept_update(self, test_client: TestClient, auth_headers: dict) -> None:
assert_route(
test_client, "PUT", "/system/dept/update/1", auth=auth_headers,
json={"name": "更新部门"},
)
def test_dept_delete(self, test_client: TestClient, auth_headers: dict) -> None:
assert_route(test_client, "DELETE", "/system/dept/delete", auth=auth_headers, json=[9999])
def test_dept_status_batch(self, test_client: TestClient, auth_headers: dict) -> None:
assert_route(
test_client, "PATCH", "/system/dept/status/batch", auth=auth_headers,
json={"ids": [1], "status": 1},
)
class TestPosition:
"""岗位管理接口 — 数据验证。"""
def test_position_list(self, test_client: TestClient, auth_headers: dict) -> None:
assert_route(test_client, "GET", "/system/position/list", auth=auth_headers)
def test_position_detail(self, test_client: TestClient, auth_headers: dict) -> None:
assert_route(test_client, "GET", "/system/position/detail/1", auth=auth_headers)
def test_position_create(self, test_client: TestClient, auth_headers: dict) -> None:
assert_route(
test_client, "POST", "/system/position/create", auth=auth_headers,
json={"name": "测试岗位", "code": "test_pos", "sort": 1},
)
def test_position_update(self, test_client: TestClient, auth_headers: dict) -> None:
assert_route(
test_client, "PUT", "/system/position/update/1", auth=auth_headers,
json={"name": "更新岗位"},
)
def test_position_delete(self, test_client: TestClient, auth_headers: dict) -> None:
assert_route(test_client, "DELETE", "/system/position/delete", auth=auth_headers, json=[9999])
def test_position_export(self, test_client: TestClient, auth_headers: dict) -> None:
assert_route(test_client, "GET", "/system/position/export", auth=auth_headers)
def test_position_status_batch(self, test_client: TestClient, auth_headers: dict) -> None:
assert_route(
test_client, "PATCH", "/system/position/status/batch", auth=auth_headers,
json={"ids": [1], "status": 1},
)
class TestDict:
"""字典管理接口 — 数据验证。"""
def test_dict_type_list(self, test_client: TestClient, auth_headers: dict) -> None:
assert_route(test_client, "GET", "/system/dict/type/list", auth=auth_headers)
def test_dict_type_detail(self, test_client: TestClient, auth_headers: dict) -> None:
assert_route(test_client, "GET", "/system/dict/type/detail/1", auth=auth_headers)
def test_dict_type_create(self, test_client: TestClient, auth_headers: dict) -> None:
assert_route(
test_client, "POST", "/system/dict/type/create", auth=auth_headers,
json={"dict_name": "测试字典", "dict_type": "test_dict", "status": 0},
)
def test_dict_type_update(self, test_client: TestClient, auth_headers: dict) -> None:
assert_route(
test_client, "PUT", "/system/dict/type/update/1", auth=auth_headers,
json={"dict_name": "更新字典"},
)
def test_dict_type_delete(self, test_client: TestClient, auth_headers: dict) -> None:
assert_route(test_client, "DELETE", "/system/dict/type/delete", auth=auth_headers, json=[9999])
def test_dict_data_by_type(self, test_client: TestClient, auth_headers: dict) -> None:
assert_route(test_client, "GET", "/system/dict/data/info/sys_normal_disable", auth=auth_headers)
def test_dict_data_list(self, test_client: TestClient, auth_headers: dict) -> None:
assert_route(test_client, "GET", "/system/dict/data/list", auth=auth_headers)
def test_dict_data_create(self, test_client: TestClient, auth_headers: dict) -> None:
assert_route(
test_client, "POST", "/system/dict/data/create", auth=auth_headers,
json={"dict_type": "sys_normal_disable", "dict_label": "测试", "dict_value": "0", "sort": 1},
)
def test_dict_data_update(self, test_client: TestClient, auth_headers: dict) -> None:
assert_route(
test_client, "PUT", "/system/dict/data/update/1", auth=auth_headers,
json={"dict_label": "更新标签"},
)
def test_dict_data_delete(self, test_client: TestClient, auth_headers: dict) -> None:
assert_route(test_client, "DELETE", "/system/dict/data/delete", auth=auth_headers, json=[9999])
def test_dict_data_export(self, test_client: TestClient, auth_headers: dict) -> None:
assert_route(test_client, "POST", "/system/dict/data/export", auth=auth_headers)
def test_dict_data_status_batch(self, test_client: TestClient, auth_headers: dict) -> None:
assert_route(
test_client, "PATCH", "/system/dict/data/status/batch", auth=auth_headers,
json={"ids": [1], "status": 1},
)
def test_dict_type_export(self, test_client: TestClient, auth_headers: dict) -> None:
assert_route(test_client, "POST", "/system/dict/type/export", auth=auth_headers)
def test_dict_type_optionselect(self, test_client: TestClient, auth_headers: dict) -> None:
assert_route(test_client, "GET", "/system/dict/type/optionselect", auth=auth_headers)
def test_dict_type_status_batch(self, test_client: TestClient, auth_headers: dict) -> None:
assert_route(
test_client, "PATCH", "/system/dict/type/status/batch", auth=auth_headers,
json={"ids": [1], "status": 1},
)
class TestNotice:
"""公告通知接口 — 数据验证。"""
def test_notice_list(self, test_client: TestClient, auth_headers: dict) -> None:
assert_route(test_client, "GET", "/system/notice/list", auth=auth_headers)
def test_notice_detail(self, test_client: TestClient, auth_headers: dict) -> None:
assert_route(test_client, "GET", "/system/notice/detail/1", auth=auth_headers)
def test_notice_create(self, test_client: TestClient, auth_headers: dict) -> None:
assert_route(
test_client, "POST", "/system/notice/create", auth=auth_headers,
json={"notice_title": "测试公告", "notice_content": "内容", "status": 0},
)
def test_notice_update(self, test_client: TestClient, auth_headers: dict) -> None:
assert_route(
test_client, "PUT", "/system/notice/update/1", auth=auth_headers,
json={"notice_title": "更新公告"},
)
def test_notice_delete(self, test_client: TestClient, auth_headers: dict) -> None:
assert_route(test_client, "DELETE", "/system/notice/delete", auth=auth_headers, json=[9999])
def test_notice_unread_count(self, test_client: TestClient, auth_headers: dict) -> None:
assert_route(test_client, "GET", "/system/notice/unread-count", auth=auth_headers)
def test_notice_available(self, test_client: TestClient, auth_headers: dict) -> None:
assert_route(test_client, "GET", "/system/notice/available", auth=auth_headers)
def test_notice_export(self, test_client: TestClient, auth_headers: dict) -> None:
assert_route(test_client, "POST", "/system/notice/export", auth=auth_headers)
def test_notice_panel(self, test_client: TestClient, auth_headers: dict) -> None:
assert_route(test_client, "GET", "/system/notice/panel", auth=auth_headers)
def test_notice_read(self, test_client: TestClient, auth_headers: dict) -> None:
assert_route(test_client, "POST", "/system/notice/read/1", auth=auth_headers)
def test_notice_read_all(self, test_client: TestClient, auth_headers: dict) -> None:
assert_route(test_client, "POST", "/system/notice/read-all", auth=auth_headers)
def test_notice_status_batch(self, test_client: TestClient, auth_headers: dict) -> None:
assert_route(
test_client, "PATCH", "/system/notice/status/batch", auth=auth_headers,
json={"ids": [1], "status": 1},
)
class TestParams:
"""参数管理接口 — 数据验证。"""
def test_params_list(self, test_client: TestClient, auth_headers: dict) -> None:
assert_route(test_client, "GET", "/system/param/list", auth=auth_headers)
def test_params_create(self, test_client: TestClient, auth_headers: dict) -> None:
assert_route(
test_client, "POST", "/system/param/create", auth=auth_headers,
json={"param_name": "测试参数", "param_key": "test.key", "param_value": "val", "param_type": "string"},
)
def test_params_update(self, test_client: TestClient, auth_headers: dict) -> None:
assert_route(
test_client, "PUT", "/system/param/update/1", auth=auth_headers,
json={"param_value": "new_val"},
)
def test_params_delete(self, test_client: TestClient, auth_headers: dict) -> None:
assert_route(test_client, "DELETE", "/system/param/delete", auth=auth_headers, json=[9999])
def test_params_detail(self, test_client: TestClient, auth_headers: dict) -> None:
assert_route(test_client, "GET", "/system/param/detail/1", auth=auth_headers)
def test_params_export(self, test_client: TestClient, auth_headers: dict) -> None:
assert_route(test_client, "GET", "/system/param/export", auth=auth_headers)
def test_params_info(self, test_client: TestClient, auth_headers: dict) -> None:
assert_route(test_client, "GET", "/system/param/info", auth=auth_headers)
def test_params_by_key(self, test_client: TestClient, auth_headers: dict) -> None:
assert_route(test_client, "GET", "/system/param/key/test.key", auth=auth_headers)
def test_params_value_by_key(self, test_client: TestClient, auth_headers: dict) -> None:
assert_route(test_client, "GET", "/system/param/value/test.key", auth=auth_headers)
def test_params_status_batch(self, test_client: TestClient, auth_headers: dict) -> None:
assert_route(
test_client, "PATCH", "/system/param/status/batch", auth=auth_headers,
json={"ids": [1], "status": 1},
)
class TestLog:
"""日志管理接口 — 数据验证。"""
def test_login_log_list(self, test_client: TestClient, auth_headers: dict) -> None:
assert_route(test_client, "GET", "/system/log/login/list", auth=auth_headers)
def test_login_log_detail(self, test_client: TestClient, auth_headers: dict) -> None:
assert_route(test_client, "GET", "/system/log/login/detail/1", auth=auth_headers)
def test_operation_log_list(self, test_client: TestClient, auth_headers: dict) -> None:
assert_route(test_client, "GET", "/system/log/operation/list", auth=auth_headers)
def test_operation_log_detail(self, test_client: TestClient, auth_headers: dict) -> None:
assert_route(test_client, "GET", "/system/log/operation/detail/1", auth=auth_headers)
class TestTicket:
"""工单管理接口 — 数据验证。"""
def test_ticket_list(self, test_client: TestClient, auth_headers: dict) -> None:
assert_route(test_client, "GET", "/system/ticket/list", auth=auth_headers)
def test_ticket_detail(self, test_client: TestClient, auth_headers: dict) -> None:
assert_route(test_client, "GET", "/system/ticket/detail/1", auth=auth_headers)
def test_ticket_create(self, test_client: TestClient, auth_headers: dict) -> None:
assert_route(
test_client, "POST", "/system/ticket/create", auth=auth_headers,
json={"title": "测试工单", "ticket_type": "bug", "ticket_content": "内容描述"},
)
def test_ticket_update(self, test_client: TestClient, auth_headers: dict) -> None:
assert_route(
test_client, "PUT", "/system/ticket/update/1", auth=auth_headers,
json={"title": "更新工单"},
)
def test_ticket_delete(self, test_client: TestClient, auth_headers: dict) -> None:
assert_route(test_client, "DELETE", "/system/ticket/delete", auth=auth_headers, json=[9999])
def test_ticket_batch(self, test_client: TestClient, auth_headers: dict) -> None:
assert_route(test_client, "PUT", "/system/ticket/batch", auth=auth_headers)