"""Instance CRUD + listing tests — Go/Gin backend.""" import pytest from httpx import AsyncClient from tests.conftest import auth_header class TestListInstances: async def test_list_instances(self, client: AsyncClient, admin_token: str): resp = await client.get("/api/v1/instances", headers=auth_header(admin_token)) assert resp.status_code == 200 data = resp.json() assert isinstance(data, list) 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)) 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") async def test_unauthenticated(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)) 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) 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 data = resp.json() assert "id" in data 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"}, ) 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