Spaces:
Running on CPU Upgrade

File size: 7,160 Bytes
427634d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
from pathlib import Path

import pytest_asyncio
from httpx import ASGITransport, AsyncClient

from app.main import create_app


@pytest_asyncio.fixture
async def frontend_client(tmp_path: Path, monkeypatch):
    dist_dir = tmp_path / "frontend-dist"
    assets_dir = dist_dir / "assets"
    assets_dir.mkdir(parents=True)
    (dist_dir / "index.html").write_text("<html><body>lighting desk</body></html>", encoding="utf-8")
    (assets_dir / "app.js").write_text("console.log('lighting desk');", encoding="utf-8")

    monkeypatch.setenv("LIGHTING_MVP_DATABASE_URL", f"sqlite+aiosqlite:///{tmp_path / 'test.db'}")
    monkeypatch.setenv("LIGHTING_MVP_OBJECT_STORAGE_ROOT", str(tmp_path / "storage"))
    monkeypatch.setenv("LIGHTING_MVP_FRONTEND_DIST_DIR", str(dist_dir))
    monkeypatch.setenv("LIGHTING_MVP_OPENAI_API_KEY", "")
    monkeypatch.setenv("LIGHTING_MVP_ROOT_PATH", "")
    monkeypatch.setenv("LIGHTING_MVP_SKIP_CORPUS_SEED", "1")
    monkeypatch.setenv("LIGHTING_MVP_ACCESS_GATE_PASSWORD", "top-secret")

    app = create_app()

    async with app.router.lifespan_context(app):
        async with AsyncClient(
            transport=ASGITransport(app=app),
            base_url="https://testserver",
            follow_redirects=False,
        ) as client:
            accepted = await client.post(
                "/login",
                data={"identifier": "admin", "password": "top-secret"},
            )
            assert accepted.status_code == 302
            yield client


@pytest_asyncio.fixture
async def gated_frontend_client(tmp_path: Path, monkeypatch):
    dist_dir = tmp_path / "frontend-dist"
    assets_dir = dist_dir / "assets"
    assets_dir.mkdir(parents=True)
    (dist_dir / "index.html").write_text("<html><body>lighting desk</body></html>", encoding="utf-8")
    (assets_dir / "app.js").write_text("console.log('lighting desk');", encoding="utf-8")

    monkeypatch.setenv("LIGHTING_MVP_DATABASE_URL", f"sqlite+aiosqlite:///{tmp_path / 'test.db'}")
    monkeypatch.setenv("LIGHTING_MVP_OBJECT_STORAGE_ROOT", str(tmp_path / "storage"))
    monkeypatch.setenv("LIGHTING_MVP_FRONTEND_DIST_DIR", str(dist_dir))
    monkeypatch.setenv("LIGHTING_MVP_OPENAI_API_KEY", "")
    monkeypatch.setenv("LIGHTING_MVP_ROOT_PATH", "")
    monkeypatch.setenv("LIGHTING_MVP_SKIP_CORPUS_SEED", "1")
    monkeypatch.setenv("LIGHTING_MVP_ACCESS_GATE_PASSWORD", "top-secret")

    app = create_app()

    async with app.router.lifespan_context(app):
        async with AsyncClient(
            transport=ASGITransport(app=app),
            base_url="https://testserver",
            follow_redirects=False,
        ) as client:
            yield client


@pytest_asyncio.fixture
async def unconfigured_access_gate_client(tmp_path: Path, monkeypatch):
    dist_dir = tmp_path / "frontend-dist"
    assets_dir = dist_dir / "assets"
    assets_dir.mkdir(parents=True)
    (dist_dir / "index.html").write_text("<html><body>lighting desk</body></html>", encoding="utf-8")
    (assets_dir / "app.js").write_text("console.log('lighting desk');", encoding="utf-8")

    monkeypatch.setenv("LIGHTING_MVP_DATABASE_URL", f"sqlite+aiosqlite:///{tmp_path / 'test.db'}")
    monkeypatch.setenv("LIGHTING_MVP_OBJECT_STORAGE_ROOT", str(tmp_path / "storage"))
    monkeypatch.setenv("LIGHTING_MVP_FRONTEND_DIST_DIR", str(dist_dir))
    monkeypatch.setenv("LIGHTING_MVP_OPENAI_API_KEY", "")
    monkeypatch.setenv("LIGHTING_MVP_ROOT_PATH", "")
    monkeypatch.setenv("LIGHTING_MVP_SKIP_CORPUS_SEED", "1")
    monkeypatch.delenv("LIGHTING_MVP_ACCESS_GATE_PASSWORD", raising=False)

    app = create_app()

    async with app.router.lifespan_context(app):
        async with AsyncClient(
            transport=ASGITransport(app=app),
            base_url="https://testserver",
            follow_redirects=False,
        ) as client:
            yield client


async def test_root_serves_index_html(frontend_client):
    response = await frontend_client.get("/")

    assert response.status_code == 200
    assert response.headers["content-type"].startswith("text/html")
    assert "lighting desk" in response.text


async def test_root_supports_head_requests(frontend_client):
    response = await frontend_client.head("/")

    assert response.status_code == 200
    assert response.headers["content-type"].startswith("text/html")


async def test_assets_are_served_from_frontend_dist(frontend_client):
    response = await frontend_client.get("/assets/app.js")

    assert response.status_code == 200
    assert "lighting desk" in response.text


async def test_missing_api_route_stays_a_404(frontend_client):
    response = await frontend_client.get("/api/v1/not-real")

    assert response.status_code == 404


async def test_access_gate_redirects_root_to_login(gated_frontend_client):
    response = await gated_frontend_client.get("/")

    assert response.status_code == 302
    assert response.headers["location"].startswith("/login")


async def test_access_gate_keeps_health_public(gated_frontend_client):
    response = await gated_frontend_client.get("/health")

    assert response.status_code == 200
    assert response.json().get("status") == "ok"


async def test_access_gate_rejects_api_requests_without_session(gated_frontend_client):
    response = await gated_frontend_client.get("/api/v1/not-real")

    assert response.status_code == 401
    assert response.json()["detail"]["code"] == "UNAUTHENTICATED"


async def test_access_gate_accepts_login_and_unlocks_app(gated_frontend_client):
    login_page = await gated_frontend_client.get("/login")
    assert login_page.status_code == 200
    assert "password" in login_page.text.lower()
    assert "identifier" in login_page.text.lower()

    rejected = await gated_frontend_client.post(
        "/login",
        data={"identifier": "admin", "password": "wrong-password"},
    )
    assert rejected.status_code == 401

    accepted = await gated_frontend_client.post(
        "/login",
        data={"identifier": "admin", "password": "top-secret"},
    )
    assert accepted.status_code == 302
    assert accepted.headers["location"] == "/"
    assert "lighting_session=" in accepted.headers["set-cookie"].lower()
    assert "httponly" in accepted.headers["set-cookie"].lower()

    unlocked = await gated_frontend_client.get("/")
    assert unlocked.status_code == 200
    assert "lighting desk" in unlocked.text


async def test_access_gate_unconfigured_blocks_root(unconfigured_access_gate_client):
    response = await unconfigured_access_gate_client.get("/")

    assert response.status_code == 503
    assert "access gate" in response.text.lower()


async def test_access_gate_unconfigured_blocks_api(unconfigured_access_gate_client):
    response = await unconfigured_access_gate_client.get("/api/v1/not-real")

    assert response.status_code == 503
    assert response.json()["detail"]["code"] == "ACCESS_GATE_NOT_CONFIGURED"


async def test_access_gate_unconfigured_keeps_health_public(unconfigured_access_gate_client):
    response = await unconfigured_access_gate_client.get("/health")

    assert response.status_code == 200
    assert response.json().get("status") == "ok"