Download tests/test_api.py from mrmayankmathurorg/Customer-EMAIL-Agent: direct link, hf CLI and curl.
- Browser
- Download file 3.15 kB
-
https://huggingface.co/spaces/mrmayankmathurorg/Customer-EMAIL-Agent/resolve/main/tests/test_api.py
- Command line
-
hf download hf://spaces/mrmayankmathurorg/Customer-EMAIL-Agent/tests/test_api.py
-
curl -L -o test_api.py https://huggingface.co/spaces/mrmayankmathurorg/Customer-EMAIL-Agent/resolve/main/tests/test_api.py
3.15 kB
| """ | |
| 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 | |
| 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} | |
| 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"} | |
| 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"] == [] | |
| 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 | |
| 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 | |
| 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() | |