File size: 3,088 Bytes
6b8bb0d
 
 
 
9894b89
 
6b8bb0d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e61da91
 
 
 
9894b89
 
 
e61da91
122cdd6
e61da91
 
 
 
 
 
9894b89
e61da91
 
 
 
 
 
122cdd6
e61da91
122cdd6
e61da91
 
 
 
 
9894b89
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
92
93
"""FastAPI boundary tests."""

from __future__ import annotations

import hashlib

from fastapi.testclient import TestClient

from src.config import settings
from src.interfaces.api import app


def test_health_returns_status() -> None:
    """Given a running API, health should be available without external services."""
    response = TestClient(app).get("/health")

    assert response.status_code == 200
    assert response.json() == {"status": "ok"}


def test_upload_rejects_non_pdf_content() -> None:
    """Given a fake PDF, upload should reject it before writing or embedding."""
    response = TestClient(app).post(
        "/upload",
        files={"file": ("notes.pdf", b"not a pdf", "application/pdf")},
    )

    assert response.status_code == 400


def test_configured_api_key_protects_documents(monkeypatch) -> None:
    """Configured API auth should reject missing or incorrect keys."""
    monkeypatch.setattr(settings, "api_key", "test-secret")
    monkeypatch.setattr("src.interfaces.api.list_documents", lambda: [])
    client = TestClient(app)

    assert client.get("/documents").status_code == 401
    assert client.get("/documents", headers={"X-API-Key": "wrong"}).status_code == 401
    assert client.get("/documents", headers={"X-API-Key": "test-secret"}).status_code == 200


def test_monitoring_endpoint_is_disabled_without_api_key(monkeypatch) -> None:
    monkeypatch.setattr(settings, "api_key", None)
    monkeypatch.setattr(settings, "api_key_sha256", None)
    monkeypatch.delenv("RAG_API_KEY", raising=False)
    monkeypatch.delenv("RAG_API_KEY_SHA256", raising=False)

    response = TestClient(app).get("/monitoring/retrieval/summary")

    assert response.status_code == 404


def test_monitoring_endpoint_requires_key_and_returns_summary(monkeypatch) -> None:
    monkeypatch.setattr(settings, "api_key", "test-secret")
    monkeypatch.setattr(settings, "api_key_sha256", None)
    monkeypatch.setattr(
        "src.monitoring.load_retrieval_summary",
        lambda: {"promotion_gate": {"status": "insufficient_data"}},
    )
    client = TestClient(app)

    assert client.get("/monitoring/retrieval/summary").status_code == 401
    response = client.get(
        "/monitoring/retrieval/summary",
        headers={"X-API-Key": "test-secret"},
    )

    assert response.status_code == 200
    assert response.json()["promotion_gate"]["status"] == "insufficient_data"


def test_monitoring_endpoint_accepts_hashed_api_key(monkeypatch) -> None:
    monkeypatch.setattr(settings, "api_key", None)
    monkeypatch.setattr(
        settings,
        "api_key_sha256",
        hashlib.sha256(b"test-secret").hexdigest(),
    )
    monkeypatch.setattr(
        "src.monitoring.load_retrieval_summary",
        lambda: {"promotion_gate": {"status": "insufficient_data"}},
    )
    client = TestClient(app)

    assert client.get("/monitoring/retrieval/summary").status_code == 401
    assert (
        client.get(
            "/monitoring/retrieval/summary",
            headers={"X-API-Key": "test-secret"},
        ).status_code
        == 200
    )