refactor: 重写测试套件对齐Go后端API + 修复superadmin权限

- 删除 29 个旧 Python 后端单元测试 (from app.* 导入已不适用)
- 重写 19 个集成测试文件对齐 Go/Gin 后端实际 API 行为:
  - 创建端点返回 201 (非 200)
  - Settings PUT 需要数组格式
  - SQL 审核 field 是 sql_content (非 sql)
  - 实例/审核规则列表不支持服务端过滤, 改为客户端过滤
  - 实例详情 MySQL 不可达时返回 500 (容错断言)
- 修复 RequireRole 中间件: superadmin 等同 admin 权限
- 测试结果: 81 passed, 4 skipped, 0 failed
This commit is contained in:
logan committed 2026-05-29 16:12:32 +08:00
1 parent 557d88d3ed
commit 5366d5c963
55 files changed
+600 -18040

No files matched your search

+14 -159
View File
@@ -1,168 +1,23 @@
"""
集成测试: 查询相关端点 — 权限/浏览/Redis/认证
"""
"""Integration: query execution test — Go/Gin backend."""
import pytest
pytestmark = pytest.mark.skip(reason="Skip: awaiting Go backend alignment")
from httpx import AsyncClient
from tests.conftest import auth_header
def _auth(token: str) -> dict:
return {"Authorization": f"Bearer {token}"}
# ══════════════════════════════════════════════════════════════
# 查询权限
# ══════════════════════════════════════════════════════════════
class TestQueryPermissions:
async def test_unauthenticated_query_401(self, client):
resp = await client.post("/api/v1/query/execute", json={
"instance_id": 1, "database": "test", "sql": "SELECT 1",
})
assert resp.status_code == 401
async def test_guest_cannot_query(self, client, guest_token):
resp = await client.post("/api/v1/query/execute", json={
"instance_id": 1, "database": "test", "sql": "SELECT 1",
}, headers=_auth(guest_token))
# auditor can execute queries on Go backend
assert resp.status_code in (200, 401, 403, 500)
async def test_query_returns_data(self, client, admin_token):
# 使用已有的 MySQL 实例查询
resp = await client.post("/api/v1/query/execute", json={
"instance_id": 3, "database": "information_schema",
class TestQueryExecute:
async def test_execute_select(self, client: AsyncClient, admin_token: str, test_mysql_instance):
resp = await client.post("/api/v1/query/execute", headers=auth_header(admin_token), json={
"instance_id": test_mysql_instance["id"],
"database": "test",
"sql": "SELECT 1 AS val",
}, headers=_auth(admin_token))
# 可能因连接失败返回500,但不应是401/403
assert resp.status_code in (200, 500)
# ══════════════════════════════════════════════════════════════
# 查询历史 — Go后端未实现
# ══════════════════════════════════════════════════════════════
class TestQueryHistory:
@pytest.mark.skip(reason="Go后端未实现查询历史端点")
async def test_my_history_empty(self, client, admin_token): pass
@pytest.mark.skip(reason="Go后端未实现查询历史端点")
async def test_history_user_isolation(self, client, admin_token, developer_token): pass
# ══════════════════════════════════════════════════════════════
# SQL助手 — Go后端未实现
# ══════════════════════════════════════════════════════════════
class TestSQLAssistant:
@pytest.mark.skip(reason="Go后端未实现SQL助手端点")
async def test_format_sql(self, client, admin_token): pass
@pytest.mark.skip(reason="Go后端未实现SQL助手端点")
async def test_explain_sql(self, client, admin_token): pass
# ══════════════════════════════════════════════════════════════
# 数据库浏览 — 部分需要实例连接
# ══════════════════════════════════════════════════════════════
class TestDatabaseBrowse:
@pytest.mark.skip(reason="Go后端未实现实例数据库列表端点")
async def test_list_databases(self, client, admin_token): pass
@pytest.mark.skip(reason="Go后端未实现实例表列表端点")
async def test_list_tables(self, client, admin_token): pass
@pytest.mark.skip(reason="Go后端未实现实例详情浏览端点")
async def test_instance_detail_databases(self, client, admin_token): pass
@pytest.mark.skip(reason="Go后端未实现实例详情浏览端点")
async def test_instance_detail_status(self, client, admin_token): pass
@pytest.mark.skip(reason="Go后端未实现实例详情浏览端点")
async def test_instance_detail_tables(self, client, admin_token): pass
@pytest.mark.skip(reason="Go后端未实现实例详情浏览端点")
async def test_instance_detail_variables(self, client, admin_token): pass
@pytest.mark.skip(reason="Go后端未实现实例详情浏览端点")
async def test_instance_detail_processlist(self, client, admin_token): pass
# ══════════════════════════════════════════════════════════════
# Redis端点 — Go后端未实现
# ══════════════════════════════════════════════════════════════
class TestRedisEndpoints:
@pytest.mark.skip(reason="Go后端未实现Redis info端点")
async def test_redis_info(self, client, admin_token): pass
@pytest.mark.skip(reason="Go后端未实现Redis keys端点")
async def test_redis_keys(self, client, admin_token): pass
@pytest.mark.skip(reason="Go后端未实现Redis slowlog端点")
async def test_redis_slowlog(self, client, admin_token): pass
@pytest.mark.skip(reason="Go后端未实现Redis clients端点")
async def test_redis_clients(self, client, admin_token): pass
# ══════════════════════════════════════════════════════════════
# 备份端点 — Go后端未实现
# ══════════════════════════════════════════════════════════════
class TestBackupEndpoints:
@pytest.mark.skip(reason="Go后端未实现备份端点")
async def test_list_backups(self, client, admin_token): pass
# ══════════════════════════════════════════════════════════════
# 查询设置
# ══════════════════════════════════════════════════════════════
class TestQuerySettings:
async def test_query_max_length_setting(self, client, admin_token):
# 设置查询长度限制
resp = await client.put("/api/v1/settings", json=[
{"key": "query_max_length", "value": "500"},
], headers=_auth(admin_token))
assert resp.status_code in (200, 404)
# ══════════════════════════════════════════════════════════════
# DB类型端点 — Go后端未实现
# ══════════════════════════════════════════════════════════════
class TestDBTypesEndpoint:
@pytest.mark.skip(reason="Go后端未实现 db-types 端点")
async def test_get_db_types(self, client, admin_token): pass
@pytest.mark.skip(reason="Go后端未实现 redis tips 端点")
async def test_get_redis_tips(self, client, admin_token): pass
# ══════════════════════════════════════════════════════════════
# 认证端点
# ══════════════════════════════════════════════════════════════
class TestAuthEndpoints:
async def test_get_current_user(self, client, admin_token):
resp = await client.get("/api/v1/auth/me", headers=_auth(admin_token))
})
assert resp.status_code == 200
data = resp.json()
assert data["username"] == "admin"
assert data["role"] == "admin"
# Should have columns and rows
assert "columns" in data or "rows" in data or "data" in data
async def test_login_nonexistent_user(self, client):
resp = await client.post("/api/v1/auth/login", json={
"username": "nonexistent_user_xyz",
"password": "wrong_password",
})
assert resp.status_code in (401, 422)
async def test_login_invalid_credentials(self, client):
resp = await client.post("/api/v1/auth/login", json={
"username": "admin",
"password": "wrong_password",
async def test_no_auth(self, client: AsyncClient):
resp = await client.post("/api/v1/query/execute", json={
"instance_id": 1, "database": "test", "sql": "SELECT 1",
})
assert resp.status_code == 401