|
| 1 | +"""Gateway auth: shared token + Origin rejection (MASTER-FIX-PLAN.md Phase 3 item 12). |
| 2 | +
|
| 3 | +FakeRouter/store/vectors fixtures (conftest.py) keep this offline and keyless - |
| 4 | +no network call, no API key, ever. |
| 5 | +""" |
| 6 | + |
| 7 | +from __future__ import annotations |
| 8 | + |
| 9 | +from fastapi.testclient import TestClient |
| 10 | + |
| 11 | +from personal_llm.engine import Engine |
| 12 | +from personal_llm.interfaces import api |
| 13 | +from personal_llm.voice import SpeechToText, TextToSpeech |
| 14 | + |
| 15 | +TOKEN = "test-token" |
| 16 | + |
| 17 | + |
| 18 | +def _fake_engine(store, vectors, router) -> Engine: |
| 19 | + return Engine(store=store, vectors=vectors, router=router, stt=SpeechToText(), tts=TextToSpeech()) |
| 20 | + |
| 21 | + |
| 22 | +def _client(monkeypatch, store, vectors, router): |
| 23 | + monkeypatch.setattr(api, "build_engine", lambda: _fake_engine(store, vectors, router)) |
| 24 | + monkeypatch.setattr(api, "_load_or_create_gateway_token", lambda: TOKEN) |
| 25 | + return TestClient(api.app) |
| 26 | + |
| 27 | + |
| 28 | +def test_tokenless_request_is_rejected(monkeypatch, store, vectors, router): |
| 29 | + client = _client(monkeypatch, store, vectors, router) |
| 30 | + |
| 31 | + resp = client.get("/stats") |
| 32 | + |
| 33 | + assert resp.status_code == 401 |
| 34 | + |
| 35 | + |
| 36 | +def test_wrong_token_is_rejected(monkeypatch, store, vectors, router): |
| 37 | + client = _client(monkeypatch, store, vectors, router) |
| 38 | + |
| 39 | + resp = client.get("/stats", headers={api.GATEWAY_TOKEN_HEADER: "not-the-token"}) |
| 40 | + |
| 41 | + assert resp.status_code == 401 |
| 42 | + |
| 43 | + |
| 44 | +def test_correct_token_allows_stats(monkeypatch, store, vectors, router): |
| 45 | + client = _client(monkeypatch, store, vectors, router) |
| 46 | + |
| 47 | + resp = client.get("/stats", headers={api.GATEWAY_TOKEN_HEADER: TOKEN}) |
| 48 | + |
| 49 | + assert resp.status_code == 200 |
| 50 | + assert resp.json() == store.stats() |
| 51 | + |
| 52 | + |
| 53 | +def test_origin_header_is_rejected_even_with_a_valid_token(monkeypatch, store, vectors, router): |
| 54 | + client = _client(monkeypatch, store, vectors, router) |
| 55 | + |
| 56 | + resp = client.get( |
| 57 | + "/stats", |
| 58 | + headers={api.GATEWAY_TOKEN_HEADER: TOKEN, "Origin": "https://evil.example"}, |
| 59 | + ) |
| 60 | + |
| 61 | + assert resp.status_code == 403 |
| 62 | + |
| 63 | + |
| 64 | +def test_ask_endpoint_requires_token(monkeypatch, store, vectors, router): |
| 65 | + client = _client(monkeypatch, store, vectors, router) |
| 66 | + |
| 67 | + resp = client.post("/ask", json={"question": "anything"}) |
| 68 | + |
| 69 | + assert resp.status_code == 401 |
| 70 | + |
| 71 | + |
| 72 | +def test_ask_endpoint_round_trips_with_a_valid_token(monkeypatch, store, vectors, router): |
| 73 | + client = _client(monkeypatch, store, vectors, router) |
| 74 | + |
| 75 | + resp = client.post( |
| 76 | + "/ask", |
| 77 | + json={"question": "anything"}, |
| 78 | + headers={api.GATEWAY_TOKEN_HEADER: TOKEN}, |
| 79 | + ) |
| 80 | + |
| 81 | + assert resp.status_code == 200 |
| 82 | + assert "text" in resp.json() |
| 83 | + |
| 84 | + |
| 85 | +def test_voice_ask_endpoint_requires_token(monkeypatch, store, vectors, router): |
| 86 | + client = _client(monkeypatch, store, vectors, router) |
| 87 | + |
| 88 | + resp = client.post("/voice/ask", files={"file": ("clip.wav", b"not real audio", "audio/wav")}) |
| 89 | + |
| 90 | + assert resp.status_code == 401 |
| 91 | + |
| 92 | + |
| 93 | +def test_load_or_create_gateway_token_persists_across_calls(tmp_path, monkeypatch): |
| 94 | + token_path = tmp_path / "gateway_token" |
| 95 | + monkeypatch.setattr(api, "_gateway_token", None) |
| 96 | + monkeypatch.setattr(api, "get_settings", lambda: type("S", (), {"personal_llm_gateway_token_path": str(token_path)})()) |
| 97 | + |
| 98 | + first = api._load_or_create_gateway_token() |
| 99 | + |
| 100 | + monkeypatch.setattr(api, "_gateway_token", None) |
| 101 | + second = api._load_or_create_gateway_token() |
| 102 | + |
| 103 | + assert first == second |
| 104 | + assert token_path.read_text().strip() == first |
0 commit comments