"""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