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:
logan committed 2026-06-03 02:24:40 +08:00
1 parent bec2cafec2
commit 2984f11181
22 files changed
+408 -978

No files matched your search

+28 -83
View File
@@ -1,107 +1,52 @@
"""Instance CRUD + listing tests — Go/Gin backend."""
"""Instance API tests"""
import pytest
from httpx import AsyncClient
from tests.conftest import auth_header
def auth_header(token: str) -> dict:
return {"Authorization": f"Bearer {token}"}
class TestListInstances:
async def test_list_instances(self, client: AsyncClient, admin_token: str):
async def test_admin_sees_all(self, client: AsyncClient, admin_token):
resp = await client.get("/api/v1/instances", headers=auth_header(admin_token))
assert resp.status_code == 200
data = resp.json()
assert isinstance(data, list)
items = data.get("items", data) if isinstance(data, dict) else data
assert len(items) >= 3 # MySQL + 2 Redis
async def test_list_filter_db_type(self, client: AsyncClient, admin_token: str):
"""Go backend does not support ?db_type= filter, filter client-side."""
resp = await client.get("/api/v1/instances", headers=auth_header(admin_token))
async def test_developer_sees_group_instances(self, client: AsyncClient, developer_token):
"""testtest 在 test 权限组,应看到绑定的实例"""
resp = await client.get("/api/v1/instances", headers=auth_header(developer_token))
assert resp.status_code == 200
data = resp.json()
mysql_instances = [i for i in data if i["db_type"] == "mysql"]
if not mysql_instances:
pytest.skip("No mysql instances")
items = data.get("items", data) if isinstance(data, dict) else data
assert len(items) >= 1
async def test_unauthenticated(self, client: AsyncClient):
async def test_no_auth(self, client: AsyncClient):
resp = await client.get("/api/v1/instances")
assert resp.status_code == 401
class TestGetInstance:
async def test_get_existing(self, client: AsyncClient, admin_token: str):
# List first to get a valid id
lst = await client.get("/api/v1/instances", headers=auth_header(admin_token))
instances = lst.json()
if not instances:
pytest.skip("No instances in database")
inst_id = instances[0]["id"]
resp = await client.get(f"/api/v1/instances/{inst_id}", headers=auth_header(admin_token))
async def test_get_mysql_instance(self, client: AsyncClient, admin_token, mysql_instance):
resp = await client.get(f"/api/v1/instances/{mysql_instance['id']}", headers=auth_header(admin_token))
assert resp.status_code == 200
assert resp.json()["id"] == inst_id
async def test_get_not_found(self, client: AsyncClient, admin_token: str):
resp = await client.get("/api/v1/instances/99999", headers=auth_header(admin_token))
assert resp.status_code in (404, 400)
assert resp.json()["db_type"] == "mysql"
class TestCreateInstance:
async def test_create_instance(self, client: AsyncClient, admin_token: str):
import uuid
resp = await client.post("/api/v1/instances", headers=auth_header(admin_token), json={
"name": f"test-{uuid.uuid4().hex[:6]}",
"db_type": "mysql",
"host": "10.0.0.1",
"port": 3306,
"username": "root",
})
assert resp.status_code == 201
class TestTestConnection:
async def test_mysql_connection(self, client: AsyncClient, admin_token, mysql_instance):
resp = await client.post(
f"/api/v1/instances/{mysql_instance['id']}/test-connection",
headers=auth_header(admin_token), json={},
)
assert resp.status_code == 200
data = resp.json()
assert "id" in data
assert data.get("success") is True
async def test_developer_cannot_create(self, client: AsyncClient, developer_token: str):
resp = await client.post("/api/v1/instances", headers=auth_header(developer_token), json={
"name": "nope", "db_type": "mysql", "host": "10.0.0.1",
"port": 3306, "username": "root",
})
assert resp.status_code == 403
class TestUpdateInstance:
async def test_update_instance(self, client: AsyncClient, admin_token: str):
lst = await client.get("/api/v1/instances", headers=auth_header(admin_token))
instances = lst.json()
if not instances:
pytest.skip("No instances")
inst_id = instances[0]["id"]
resp = await client.put(
f"/api/v1/instances/{inst_id}",
headers=auth_header(admin_token),
json={"name": "updated-test"},
async def test_redis_connection(self, client: AsyncClient, admin_token, redis_instance):
resp = await client.post(
f"/api/v1/instances/{redis_instance['id']}/test-connection",
headers=auth_header(admin_token), json={},
)
assert resp.status_code == 200
async def test_update_not_found(self, client: AsyncClient, admin_token: str):
resp = await client.put(
"/api/v1/instances/99999",
headers=auth_header(admin_token),
json={"name": "x"},
)
assert resp.status_code in (404, 400)
class TestDeleteInstance:
async def test_delete_not_found(self, client: AsyncClient, admin_token: str):
resp = await client.delete("/api/v1/instances/99999", headers=auth_header(admin_token))
assert resp.status_code in (200, 404)
class TestInstanceConnections:
async def test_list_databases(self, client: AsyncClient, admin_token: str, test_mysql_instance):
inst = test_mysql_instance
resp = await client.get(
f"/api/v1/instances/{inst['id']}/databases",
headers=auth_header(admin_token),
)
assert resp.status_code == 200
async def test_accessible_instances(self, client: AsyncClient, admin_token: str):
resp = await client.get("/api/v1/instances/accessible", headers=auth_header(admin_token))
assert resp.status_code == 200
assert resp.json().get("success") is True