Kostra aplikace, SQLite schéma a Docker nasazení (#10)
Základní technická kostra aplikace Poznávačky podle `docs/navrh.md` (OpenSpec změna `kostra-aplikace`, archivovaná v tomto PR). ## Změny - Balíček `poznavacky/` (`pyproject.toml`, `Flask` + `cheroot`, načítání konfigurace z prostředí včetně `.env.example`, `GET /healthz`). - SQLite vrstva (`WAL`, `foreign_keys = ON`, `busy_timeout = 5000`), atomický verzovaný runner migrací při startu a iniciální migrace `0001_initial_schema.sql` se všemi tabulkami datového modelu. - `Dockerfile` (`python:3.13-slim`, neprivilegovaný `appuser`, `HEALTHCHECK` na `/healthz`), `.dockerignore` a `compose.yaml` s perzistentním volume `/data`. - Testy v `pytest` nad dočasnou SQLite DB se syntetickými fixturami (`tests/`) a doplněné příkazy v `AGENTS.md`. - Synchronizace nových specifikací (`app-runtime`, `database-schema`) do `openspec/specs/` a archivace změny do `openspec/changes/archive/2026-11-26-kostra-aplikace/`. ## Ověření - `.venv/bin/pytest -v`: 19 passed (offline). - Lokální běh `python -m poznavacky`: `GET /healthz` vrátil `200`, čisté ukončení na `SIGTERM` (`exit 0`). - `docker compose up -d --build` a `restart`: `GET /healthz` `200`, běh pod `appuser`, perzistence `/data/poznavacky.db` i `/data/media` po restartu. Closes #1 Reviewed-on: #10 Co-authored-by: David Spáčil <spacildavi@gmail.com>
This commit was merged in pull request #10.
This commit is contained in:
1 parent
0628d63feb
commit
c413e884d0
27 files changed
+2014
-2
No files matched your search
@@ -0,0 +1,113 @@
|
||||
"""Testy načítání konfigurace z prostředí a souboru .env.example."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
|
||||
from poznavacky import create_app
|
||||
from poznavacky.config import ConfigError, load_config
|
||||
|
||||
REPO_ROOT = Path(__file__).resolve().parent.parent
|
||||
|
||||
|
||||
def test_load_config_from_environ(tmp_path: Path) -> None:
|
||||
data_dir = tmp_path / "moje-data"
|
||||
cfg = load_config(
|
||||
environ={
|
||||
"POZNAVACKY_SECRET_KEY": "supertajny-klic",
|
||||
"POZNAVACKY_DATA_DIR": str(data_dir),
|
||||
"POZNAVACKY_HOST": "0.0.0.0",
|
||||
"POZNAVACKY_PORT": "9001",
|
||||
"POZNAVACKY_BASE_URL": "https://poznavacky.example.test",
|
||||
"SMTP_HOST": "mail.example.test",
|
||||
"SMTP_PORT": "465",
|
||||
"SMTP_TLS": "false",
|
||||
}
|
||||
)
|
||||
|
||||
assert cfg.secret_key == "supertajny-klic"
|
||||
assert cfg.data_dir == data_dir
|
||||
assert cfg.db_path == data_dir / "poznavacky.db"
|
||||
assert cfg.media_dir == data_dir / "media"
|
||||
assert cfg.host == "0.0.0.0"
|
||||
assert cfg.port == 9001
|
||||
assert cfg.base_url == "https://poznavacky.example.test"
|
||||
assert cfg.smtp_host == "mail.example.test"
|
||||
assert cfg.smtp_port == 465
|
||||
assert cfg.smtp_tls is False
|
||||
assert cfg.testing is False
|
||||
|
||||
|
||||
@pytest.mark.parametrize("secret", [None, "", " "])
|
||||
def test_missing_secret_key_fails_outside_testing(
|
||||
secret: str | None,
|
||||
) -> None:
|
||||
env = {} if secret is None else {"POZNAVACKY_SECRET_KEY": secret}
|
||||
with pytest.raises(ConfigError, match="POZNAVACKY_SECRET_KEY"):
|
||||
load_config(environ=env)
|
||||
|
||||
|
||||
def test_testing_mode_allows_default_secret(tmp_path: Path) -> None:
|
||||
cfg = load_config(
|
||||
environ={},
|
||||
overrides={
|
||||
"TESTING": True,
|
||||
"POZNAVACKY_DATA_DIR": str(tmp_path / "data"),
|
||||
},
|
||||
)
|
||||
assert cfg.testing is True
|
||||
assert cfg.secret_key
|
||||
|
||||
|
||||
def test_invalid_port_raises_config_error() -> None:
|
||||
with pytest.raises(ConfigError, match="POZNAVACKY_PORT"):
|
||||
load_config(
|
||||
environ={
|
||||
"POZNAVACKY_SECRET_KEY": "klic",
|
||||
"POZNAVACKY_PORT": "70000",
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
def test_create_app_initializes_data_and_media_dirs(tmp_path: Path) -> None:
|
||||
target_dir = tmp_path / "neexistujici" / "data"
|
||||
assert not target_dir.exists()
|
||||
|
||||
app = create_app(
|
||||
environ={
|
||||
"POZNAVACKY_SECRET_KEY": "klic-pro-start",
|
||||
"POZNAVACKY_DATA_DIR": str(target_dir),
|
||||
}
|
||||
)
|
||||
|
||||
assert target_dir.is_dir()
|
||||
assert (target_dir / "media").is_dir()
|
||||
assert (target_dir / "poznavacky.db").is_file()
|
||||
assert app.config["DB_PATH"] == target_dir / "poznavacky.db"
|
||||
|
||||
|
||||
def test_env_example_documents_all_required_variables() -> None:
|
||||
env_example = (REPO_ROOT / ".env.example").read_text(encoding="utf-8")
|
||||
defined_keys = {
|
||||
line.split("=", 1)[0].strip()
|
||||
for line in env_example.splitlines()
|
||||
if line.strip() and not line.strip().startswith("#") and "=" in line
|
||||
}
|
||||
expected = {
|
||||
"POZNAVACKY_SECRET_KEY",
|
||||
"POZNAVACKY_DATA_DIR",
|
||||
"POZNAVACKY_BASE_URL",
|
||||
"POZNAVACKY_HOST",
|
||||
"POZNAVACKY_PORT",
|
||||
"POZNAVACKY_ADMIN_EMAIL",
|
||||
"POZNAVACKY_ADMIN_PASSWORD",
|
||||
"SMTP_HOST",
|
||||
"SMTP_PORT",
|
||||
"SMTP_USER",
|
||||
"SMTP_PASS",
|
||||
"SMTP_FROM",
|
||||
"SMTP_TLS",
|
||||
}
|
||||
assert expected.issubset(defined_keys)
|
||||
Reference in new issue
Block a user