Files
opsxsql/tests/test_instances.py
T
logan 2984f11181 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通过
2026-06-03 02:24:40 +08:00

53 lines
2.1 KiB
Python

"""Instance API tests"""
import pytest
from httpx import AsyncClient
def auth_header(token: str) -> dict:
return {"Authorization": f"Bearer {token}"}
class TestListInstances:
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()
items = data.get("items", data) if isinstance(data, dict) else data
assert len(items) >= 3 # MySQL + 2 Redis
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()
items = data.get("items", data) if isinstance(data, dict) else data
assert len(items) >= 1
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_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()["db_type"] == "mysql"
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 data.get("success") is True
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
assert resp.json().get("success") is True