- 删除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通过
106 lines
4.5 KiB
Python
106 lines
4.5 KiB
Python
"""Full E2E ticket workflow test — submit→approve→approve→execute"""
|
|
import pytest
|
|
from httpx import AsyncClient
|
|
def auth_header(token: str) -> dict:
|
|
return {"Authorization": f"Bearer {token}"}
|
|
|
|
|
|
class TestE2EWorkflow:
|
|
"""完整审批流程: testtest提交 → test123初审 → test234复审 → admin执行"""
|
|
|
|
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 in (200, 201)
|
|
ticket = resp.json()
|
|
tid = ticket["id"]
|
|
assert ticket["status"] == "pending"
|
|
|
|
# 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 == 200
|
|
assert resp.json()["ticket"]["status"] == "reviewing"
|
|
|
|
# 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"
|
|
|
|
# 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"
|