File size: 3,149 Bytes
6793beb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
"""
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()