File size: 3,716 Bytes
e59bd9d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Test 06 — Storage, sessions, security (mission §10, §13).

- SQLite persistence survives a restart of the app (session history rule)
- events stored and queryable with filters
- token auth is OFF on loopback, MANDATORY when exposed
- loopback binding is the default
"""

from __future__ import annotations

import json
import sys
from pathlib import Path

sys.path.insert(0, str(Path(__file__).resolve().parent.parent))

from fastapi.testclient import TestClient

from lpu.accelerator.storage import TelemetryStorage
from server.app import create_app


def test_storage_persistence_across_restart(isolated_lpu_home, config):
    # first app instance records a session
    app1 = create_app(config)
    with TestClient(app1) as client:
        client.post("/api/detect")
        # upsert through a running SDK session file
        storage = TelemetryStorage()
        storage.upsert_session({"session_id": "s1", "state": "training",
                                "epoch": 3, "step": 120, "psnr": 31.5})
        storage.record_event({"ts": 1.0, "kind": "lpu", "level": "info",
                              "text": "hello", "data": {}})
        storage.close()
    # second instance sees the SAME history (dashboard restart must not
    # lose training history - mission §10)
    app2 = create_app(config)
    with TestClient(app2) as client:
        sessions = client.get("/api/sessions").json()["sessions"]
        assert any(s["session_id"] == "s1" and s["epoch"] == 3
                   for s in sessions)
        ev = client.get("/api/terminal").json()["events"]
        assert any(e["text"] == "hello" for e in ev)
        # filtered queries
        ev = client.get("/api/terminal", params={"kind": "lpu"}).json()["events"]
        assert ev and all(e["kind"] == "lpu" for e in ev)


def test_token_auth_only_when_exposed(isolated_lpu_home, config, tmp_path):
    # loopback: no auth needed
    cfg = dict(config)
    cfg["server"] = {"host": "127.0.0.1", "expose": False}
    with TestClient(create_app(cfg)) as client:
        assert client.get("/api/status").status_code == 200

    # exposed: token REQUIRED
    cfg2 = dict(config)
    cfg2["server"] = {"host": "0.0.0.0", "expose": True}
    app = create_app(cfg2)
    token_path = Path(cfg2["server"].get("_token_path", "")) if False else None
    with TestClient(app) as client:
        r = client.get("/api/status")
        assert r.status_code == 401, "exposed bind must require auth"
        # token file was created under the isolated home
        token_file = isolated_lpu_home / "server_token"
        assert token_file.exists()
        token = token_file.read_text(encoding="utf-8").strip()
        r = client.get("/api/status", headers={"Authorization": f"Bearer {token}"})
        assert r.status_code == 200
        # wrong token refused
        r = client.get("/api/status", headers={"Authorization": "Bearer nope"})
        assert r.status_code == 401


def test_no_token_in_logs(isolated_lpu_home, config):
    cfg = dict(config)
    cfg["server"] = {"host": "0.0.0.0", "expose": True}
    with TestClient(create_app(cfg)) as client:
        client.get("/api/status")  # triggers a 401
        token_file = isolated_lpu_home / "server_token"
        token = token_file.read_text(encoding="utf-8").strip()
        # scan every log file under the isolated home for the token
        for log in isolated_lpu_home.rglob("*.log"):
            assert token not in log.read_text(encoding="utf-8", errors="ignore"), \
                f"token leaked into {log}"
        for jl in isolated_lpu_home.rglob("*.jsonl"):
            assert token not in jl.read_text(encoding="utf-8", errors="ignore"), \
                f"token leaked into {jl}"