refactor: tests目录精简重构 + 脏数据清理脚本
- 删除16个旧Python测试文件(970行→400行) - 重写conftest.py适配真实种子用户(admin/test123/test234/testtest/logan) - 5个精简测试文件覆盖核心API: - test_auth.py: 登录/鉴权/种子用户验证 (6 tests) - test_instances.py: 实例列表/详情/连接测试 (6 tests) - test_tickets.py: 工单CRUD/只读SQL拦截/auditor权限 (7 tests) - test_workflow.py: E2E完整审批流/拒绝/撤销 (3 tests) - test_permissions.py: 角色权限/执行权限/workflow权限 (6 tests) - 新增 cleanup_test_data.py 清理测试脏数据(支持--dry-run) - 全量29测试3.27s通过
This commit is contained in:
1 parent
bec2cafec2
commit
2984f11181
22 files changed
+408
-978
No files matched your search
+92
-65
@@ -1,78 +1,105 @@
|
||||
"""Workflow CRUD tests — Go/Gin backend."""
|
||||
"""Full E2E ticket workflow test — submit→approve→approve→execute"""
|
||||
import pytest
|
||||
from httpx import AsyncClient
|
||||
from tests.conftest import auth_header
|
||||
def auth_header(token: str) -> dict:
|
||||
return {"Authorization": f"Bearer {token}"}
|
||||
|
||||
|
||||
class TestListWorkflows:
|
||||
async def test_list_workflows(self, client: AsyncClient, admin_token: str):
|
||||
resp = await client.get("/api/v1/workflows", headers=auth_header(admin_token))
|
||||
assert resp.status_code == 200
|
||||
data = resp.json()
|
||||
assert isinstance(data, list)
|
||||
class TestE2EWorkflow:
|
||||
"""完整审批流程: testtest提交 → test123初审 → test234复审 → admin执行"""
|
||||
|
||||
async def test_no_auth(self, client: AsyncClient):
|
||||
resp = await client.get("/api/v1/workflows")
|
||||
assert resp.status_code == 401
|
||||
|
||||
|
||||
class TestCreateWorkflow:
|
||||
async def test_create_success(self, client: AsyncClient, admin_token: str):
|
||||
import uuid
|
||||
resp = await client.post("/api/v1/workflows", headers=auth_header(admin_token), json={
|
||||
"name": f"wf-test-{uuid.uuid4().hex[:6]}",
|
||||
"description": "Test workflow",
|
||||
"is_active": True,
|
||||
async def test_full_approval_flow(self, client: AsyncClient,
|
||||
developer_token, approver1_token,
|
||||
approver2_token, admin_token,
|
||||
mysql_instance):
|
||||
# Step 1: developer 提交工单
|
||||
resp = await client.post("/api/v1/tickets", headers=auth_header(developer_token), json={
|
||||
"title": "E2E测试-完整审批流",
|
||||
"db_type": "mysql",
|
||||
"instance_id": mysql_instance["id"],
|
||||
"target_database": "opsxsql_test",
|
||||
"sql_content": "CREATE TABLE IF NOT EXISTS _e2e_test (id INT PRIMARY KEY, val VARCHAR(50))",
|
||||
"workflow_id": 4,
|
||||
})
|
||||
assert resp.status_code == 201
|
||||
data = resp.json()
|
||||
assert "id" in data
|
||||
assert "message" in data
|
||||
assert resp.status_code in (200, 201)
|
||||
ticket = resp.json()
|
||||
tid = ticket["id"]
|
||||
assert ticket["status"] == "pending"
|
||||
|
||||
async def test_create_with_category(self, client: AsyncClient, admin_token: str):
|
||||
import uuid
|
||||
resp = await client.post("/api/v1/workflows", headers=auth_header(admin_token), json={
|
||||
"name": f"wf-cat-{uuid.uuid4().hex[:6]}",
|
||||
"description": "With category",
|
||||
"category": "ticket",
|
||||
"is_active": True,
|
||||
# Step 1.5: 验证审批记录已生成
|
||||
resp = await client.get(f"/api/v1/tickets/{tid}/approvals", headers=auth_header(admin_token))
|
||||
assert resp.status_code == 200
|
||||
approvals = resp.json()
|
||||
assert len(approvals) >= 2 # 至少有初审+复审+执行
|
||||
|
||||
# Step 2: test123 初审通过
|
||||
resp = await client.post(f"/api/v1/tickets/{tid}/review", headers=auth_header(approver1_token), json={
|
||||
"action": "approve", "comment": "初审通过",
|
||||
})
|
||||
assert resp.status_code == 201
|
||||
|
||||
|
||||
class TestGetWorkflow:
|
||||
async def test_get_existing(self, client: AsyncClient, admin_token: str):
|
||||
# List first
|
||||
lst = await client.get("/api/v1/workflows", headers=auth_header(admin_token))
|
||||
wfs = lst.json()
|
||||
if not wfs:
|
||||
pytest.skip("No workflows")
|
||||
wf_id = wfs[0]["id"]
|
||||
resp = await client.get(f"/api/v1/workflows/{wf_id}", headers=auth_header(admin_token))
|
||||
assert resp.status_code == 200
|
||||
data = resp.json()
|
||||
assert data["id"] == wf_id
|
||||
assert "name" in data
|
||||
assert "steps" in data
|
||||
assert "is_active" in data
|
||||
assert resp.json()["ticket"]["status"] == "reviewing"
|
||||
|
||||
|
||||
class TestUpdateWorkflow:
|
||||
async def test_update_name(self, client: AsyncClient, admin_token: str):
|
||||
lst = await client.get("/api/v1/workflows", headers=auth_header(admin_token))
|
||||
wfs = lst.json()
|
||||
if not wfs:
|
||||
pytest.skip("No workflows")
|
||||
wf_id = wfs[0]["id"]
|
||||
resp = await client.put(
|
||||
f"/api/v1/workflows/{wf_id}",
|
||||
headers=auth_header(admin_token),
|
||||
json={"name": "updated-name", "description": "updated desc"},
|
||||
)
|
||||
# Step 3: test234 复审通过
|
||||
resp = await client.post(f"/api/v1/tickets/{tid}/review", headers=auth_header(approver2_token), json={
|
||||
"action": "approve", "comment": "复审通过",
|
||||
})
|
||||
assert resp.status_code == 200
|
||||
assert resp.json()["ticket"]["status"] == "approved"
|
||||
|
||||
# Step 4: admin 执行
|
||||
resp = await client.post(f"/api/v1/tickets/{tid}/execute", headers=auth_header(admin_token), json={})
|
||||
assert resp.status_code in (200, 201)
|
||||
ticket = resp.json()["ticket"]
|
||||
assert ticket["status"] == "success"
|
||||
|
||||
class TestDeleteWorkflow:
|
||||
async def test_delete_not_found(self, client: AsyncClient, admin_token: str):
|
||||
resp = await client.delete("/api/v1/workflows/99999", headers=auth_header(admin_token))
|
||||
assert resp.status_code in (200, 404)
|
||||
# Step 5: 验证执行结果是结构化 JSON
|
||||
import json
|
||||
exec_result = json.loads(ticket["execute_result"])
|
||||
assert exec_result["status"] == "success"
|
||||
assert "items" in exec_result
|
||||
assert len(exec_result["items"]) >= 1
|
||||
item = exec_result["items"][0]
|
||||
assert item["status"] == "success"
|
||||
assert "duration_ms" in item
|
||||
|
||||
async def test_reject_flow(self, client: AsyncClient,
|
||||
developer_token, approver1_token, mysql_instance):
|
||||
"""审批拒绝流程"""
|
||||
resp = await client.post("/api/v1/tickets", headers=auth_header(developer_token), json={
|
||||
"title": "E2E测试-拒绝",
|
||||
"db_type": "mysql",
|
||||
"instance_id": mysql_instance["id"],
|
||||
"target_database": "opsxsql_test",
|
||||
"sql_content": "CREATE TABLE IF NOT EXISTS _reject_test (id INT)",
|
||||
"workflow_id": 4,
|
||||
})
|
||||
assert resp.status_code in (200, 201)
|
||||
tid = resp.json()["id"]
|
||||
|
||||
# 初审拒绝
|
||||
resp = await client.post(f"/api/v1/tickets/{tid}/review", headers=auth_header(approver1_token), json={
|
||||
"action": "reject", "comment": "不通过",
|
||||
})
|
||||
assert resp.status_code == 200
|
||||
assert resp.json()["ticket"]["status"] == "rejected"
|
||||
|
||||
async def test_cancel_flow(self, client: AsyncClient,
|
||||
developer_token, mysql_instance):
|
||||
"""提交人撤销工单"""
|
||||
resp = await client.post("/api/v1/tickets", headers=auth_header(developer_token), json={
|
||||
"title": "E2E测试-撤销",
|
||||
"db_type": "mysql",
|
||||
"instance_id": mysql_instance["id"],
|
||||
"target_database": "opsxsql_test",
|
||||
"sql_content": "CREATE TABLE IF NOT EXISTS _cancel_test (id INT)",
|
||||
"workflow_id": 4,
|
||||
})
|
||||
assert resp.status_code in (200, 201)
|
||||
tid = resp.json()["id"]
|
||||
|
||||
# 提交人撤销
|
||||
resp = await client.post(f"/api/v1/tickets/{tid}/cancel", headers=auth_header(developer_token), json={
|
||||
"reason": "不想执行了",
|
||||
})
|
||||
assert resp.status_code == 200
|
||||
assert resp.json()["ticket"]["status"] == "cancelled"
|
||||
Reference in new issue
Block a user