""" 集成测试: 查询相关端点 — 权限/浏览/Redis/认证 """ import pytest pytestmark = pytest.mark.skip(reason="Skip: awaiting Go backend alignment") 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", "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" 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", }) assert resp.status_code == 401