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("lighting desk", 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("lighting desk", 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("lighting desk", 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"