feat: fastapi-backend mit healthcheck und alembic-basis

- GET /api/health prüft die Datenbankverbindung
- SQLite im WAL-Modus unter /data/app.db
- Auslieferung des gebauten Frontends mit SPA-Fallback
- leere Basis-Migration, Tests mit pytest

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
dbildhauerandClaude Opus 5.5 committed 2026-10-25 11:04:33 +01:00
1 parent ff666175d0
commit 8e6ef05304
15 files changed
+414

No files matched your search

+36
View File
@@ -0,0 +1,36 @@
from collections.abc import Iterator
import pytest
from fastapi.testclient import TestClient
from app.config import get_settings
from app.db import _session_factory, get_engine
def _clear_caches() -> None:
get_settings.cache_clear()
get_engine.cache_clear()
_session_factory.cache_clear()
@pytest.fixture
def static_dir(tmp_path):
"""Directory for the built frontend; empty by default (no SPA routes)."""
path = tmp_path / "static"
path.mkdir()
return path
@pytest.fixture
def client(tmp_path, static_dir, monkeypatch) -> Iterator[TestClient]:
monkeypatch.setenv("DATA_DIR", str(tmp_path / "data"))
monkeypatch.setenv("STATIC_DIR", str(static_dir))
_clear_caches()
from app.main import create_app
with TestClient(create_app()) as test_client:
yield test_client
get_engine().dispose()
_clear_caches()
+38
View File
@@ -0,0 +1,38 @@
import pytest
@pytest.fixture
def spa_client(tmp_path, static_dir, client):
(static_dir / "assets").mkdir()
(static_dir / "index.html").write_text("<html>spa</html>")
(static_dir / "assets" / "app.js").write_text("console.log('x')")
(tmp_path / "secret.txt").write_text("secret")
return client
def test_serves_index_at_root(spa_client):
response = spa_client.get("/")
assert response.status_code == 200
assert "spa" in response.text
def test_serves_static_asset(spa_client):
response = spa_client.get("/assets/app.js")
assert response.status_code == 200
assert "console.log" in response.text
def test_client_route_falls_back_to_index(spa_client):
response = spa_client.get("/clips/123")
assert response.status_code == 200
assert "spa" in response.text
def test_path_traversal_is_blocked(spa_client):
response = spa_client.get("/..%2Fsecret.txt")
assert "secret" not in response.text
def test_api_paths_are_not_swallowed_by_spa(spa_client):
assert spa_client.get("/api/unknown").status_code == 404
assert spa_client.get("/api/health").json()["status"] == "ok"
+23
View File
@@ -0,0 +1,23 @@
from sqlalchemy import text
from app import __version__
from app.db import get_engine
def test_health_ok(client):
response = client.get("/api/health")
assert response.status_code == 200
assert response.json() == {"status": "ok", "version": __version__, "db": "ok"}
def test_sqlite_uses_wal(client):
client.get("/api/health")
with get_engine().connect() as conn:
assert conn.execute(text("PRAGMA journal_mode")).scalar() == "wal"
assert conn.execute(text("PRAGMA foreign_keys")).scalar() == 1
def test_unknown_api_path_returns_404(client):
assert client.get("/api/does-not-exist").status_code == 404