""" Smoke tests for the FastAPI application. """ from unittest.mock import AsyncMock, MagicMock, patch import pytest from httpx import ASGITransport, AsyncClient from src.api import routes from src.core.config import settings from src.main import app @pytest.fixture(autouse=True) def test_api_token(monkeypatch): """Use an explicit test token for routes protected by verify_token.""" monkeypatch.setattr(settings, "API_TOKEN", "test-api-token") table = MagicMock() table.select.return_value = table table.eq.return_value = table table.order.return_value = table table.limit.return_value = table table.single.return_value = table table.execute.return_value.data = [] supabase = MagicMock() supabase.table.return_value = table monkeypatch.setattr(routes, "supabase", supabase) def _auth_headers() -> dict[str, str]: return {"x-api-token": settings.API_TOKEN} @pytest.mark.asyncio async def test_health_check(): """Health endpoint should return 200 with status ok.""" async with AsyncClient(transport=ASGITransport(app=app), base_url="http://test") as client: response = await client.get("/health") assert response.status_code == 200 assert response.json() == {"status": "ok"} @pytest.mark.asyncio async def test_list_escalated_tickets_empty(): """Review listing endpoint should return empty list initially.""" async with AsyncClient(transport=ASGITransport(app=app), base_url="http://test") as client: response = await client.get("/api/v1/review", headers=_auth_headers()) assert response.status_code == 200 data = response.json() assert data["pending_count"] == 0 assert data["tickets"] == [] @pytest.mark.asyncio async def test_list_follow_ups_empty(): """Follow-ups endpoint should return empty list initially.""" async with AsyncClient(transport=ASGITransport(app=app), base_url="http://test") as client: response = await client.get("/api/v1/follow-ups", headers=_auth_headers()) assert response.status_code == 200 data = response.json() assert data["pending_count"] == 0 @pytest.mark.asyncio async def test_review_nonexistent_ticket(): """Reviewing a non-existent ticket should return 404.""" async with AsyncClient(transport=ASGITransport(app=app), base_url="http://test") as client: response = await client.post( "/api/v1/review/nonexistent", params={"action": "approve"}, headers=_auth_headers(), ) assert response.status_code == 404 @pytest.mark.asyncio @patch("src.api.routes.fetch_new_emails", new_callable=AsyncMock, return_value=[]) async def test_process_email_batch_no_credentials(mock_fetch): """Batch processing with no credentials should return 0 processed.""" async with AsyncClient(transport=ASGITransport(app=app), base_url="http://test") as client: response = await client.post( "/api/v1/process-email-batch", headers=_auth_headers() ) assert response.status_code == 200 data = response.json() assert data["processed"] == 0 mock_fetch.assert_awaited_once()