File size: 1,697 Bytes
8c1b9fe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from __future__ import annotations

import pytest
from auralynq.config import reload_settings
from auralynq.pipeline import build_index
from auralynq.serving.app import create_app
from fastapi.testclient import TestClient


@pytest.fixture
def secured_client(corpus_dir, monkeypatch):
    build_index(corpus_dir)
    from auralynq.agent import runner

    runner._CACHE.clear()
    monkeypatch.setenv("AURALYNQ_SERVE__API_KEY", "s3cret-token")
    reload_settings()
    return TestClient(create_app())


def test_open_by_default(corpus_dir):
    build_index(corpus_dir)
    app = create_app()  # no API key set -> open
    r = TestClient(app).post("/query", json={"question": "What is the capital of France?"})
    assert r.status_code == 200


def test_health_and_metrics_are_public_even_when_secured(secured_client):
    assert secured_client.get("/health").status_code == 200
    assert secured_client.get("/metrics").status_code == 200


def test_protected_endpoint_requires_token(secured_client):
    r = secured_client.post("/query", json={"question": "hi"})
    assert r.status_code == 401
    assert r.json()["error"]["code"] == "unauthorized"
    assert r.headers.get("WWW-Authenticate") == "Bearer"


def test_valid_token_allows_access(secured_client):
    r = secured_client.post(
        "/query",
        json={"question": "What is the capital of France?"},
        headers={"Authorization": "Bearer s3cret-token"},
    )
    assert r.status_code == 200
    assert r.json()["answer"]


def test_wrong_token_rejected(secured_client):
    r = secured_client.post(
        "/query", json={"question": "hi"}, headers={"Authorization": "Bearer nope"}
    )
    assert r.status_code == 401