"""Ticket CRUD + workflow tests — Go/Gin backend.""" import pytest from httpx import AsyncClient from tests.conftest import auth_header class TestListTickets: async def test_list_tickets(self, client: AsyncClient, admin_token: str): resp = await client.get("/api/v1/tickets", headers=auth_header(admin_token)) assert resp.status_code == 200 data = resp.json() assert "items" in data assert isinstance(data["items"], list) async def test_list_with_pagination(self, client: AsyncClient, admin_token: str): resp = await client.get( "/api/v1/tickets?page=1&page_size=5", headers=auth_header(admin_token), ) assert resp.status_code == 200 data = resp.json() assert "items" in data assert "total" in data async def test_no_auth(self, client: AsyncClient): resp = await client.get("/api/v1/tickets") assert resp.status_code == 401 class TestCreateTicket: async def test_create_ticket(self, client: AsyncClient, admin_token: str, test_mysql_instance): resp = await client.post("/api/v1/tickets", headers=auth_header(admin_token), json={ "title": "Test ticket", "db_type": "mysql", "instance_id": test_mysql_instance["id"], "target_database": "test", "sql_content": "SELECT 1", "reason": "Test", }) assert resp.status_code == 201 data = resp.json() assert "id" in data async def test_no_auth(self, client: AsyncClient): resp = await client.post("/api/v1/tickets", json={ "title": "No auth", "db_type": "mysql", "instance_id": 1, "target_database": "test", "sql_content": "SELECT 1", "reason": "test", }) assert resp.status_code == 401 class TestGetTicket: async def test_get_ticket(self, client: AsyncClient, admin_token: str): # List first lst = await client.get("/api/v1/tickets", headers=auth_header(admin_token)) items = lst.json().get("items", []) if not items: pytest.skip("No tickets in database") tid = items[0]["id"] resp = await client.get(f"/api/v1/tickets/{tid}", headers=auth_header(admin_token)) assert resp.status_code == 200 assert resp.json()["id"] == tid class TestReviewTicket: async def test_approve_ticket(self, client: AsyncClient, admin_token: str): # Find a pending ticket lst = await client.get( "/api/v1/tickets?status=pending&page=1&page_size=5", headers=auth_header(admin_token), ) items = lst.json().get("items", []) pending = [t for t in items if t.get("status") == "pending"] if not pending: pytest.skip("No pending tickets to approve") tid = pending[0]["id"] resp = await client.post( f"/api/v1/tickets/{tid}/approve", headers=auth_header(admin_token), json={"action": "approve", "comment": "Test approve"}, ) assert resp.status_code in (200, 400) # 400 if already approved async def test_developer_cannot_approve(self, client: AsyncClient, developer_token: str): resp = await client.post( "/api/v1/tickets/1/approve", headers=auth_header(developer_token), json={"action": "approve"}, ) # May be 403 or 400 depending on ticket state assert resp.status_code in (200, 400, 403, 404)