Implementuje účty, role, schvalování žáků v `/admin` a obnovu hesla přes SMTP podle OpenSpec změny `ucty-schvalovani-reset-hesla`. - Registrace žáků (`/registrace`), přihlášení (`/prihlaseni`), odhlášení (`/odhlaseni`), profil (`/profil`) a chráněný třídní pohled (`/trida`). - Hashování hesel (`scrypt`), podepsaná session cookie (`HttpOnly`, `SameSite=Lax`, za HTTPS `Secure`), okamžité zneplatnění session při změně hesla či zablokování účtu, globální CSRF kontrola, in-memory rate limiting a volitelný `ProxyFix`. - Atomická inicializace prvního účtu učitelky z `POZNAVACKY_ADMIN_EMAIL` / `POZNAVACKY_ADMIN_PASSWORD` při startu nad prázdnou tabulkou `uzivatele`. - Administrace `/admin`, `/admin/tridy` a `/admin/zaci` (správa tříd, fronta schvalování, schválení, zamítnutí, přeřazení, deaktivace/aktivace s ochranou účtů učitelky). - Obnova zapomenutého hesla (`/obnova-hesla`, `/obnova-hesla/<token>`) s jednorázovým SHA-256 hashovaným tokenem (migrace `0002_token_hash_index.sql`), anti-enumerací a SMTP odesílačem (STARTTLS / `SMTP_SSL`). - Synchronizovány specifikace `user-auth`, `student-approval`, `password-reset` do `openspec/specs/` a změna archivována. - Ověřeno offline: `.venv/bin/pytest -W error` (52 passed) a `.venv/bin/ruff check .`. Closes #2 Reviewed-on: #11 Co-authored-by: David Spáčil <spacildavi@gmail.com>
170 lines
5.1 KiB
Python
170 lines
5.1 KiB
Python
"""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
|
|
assert cfg.session_cookie_secure is True
|
|
assert cfg.trust_proxy == 0
|
|
assert cfg.password_reset_ttl_minutes == 60
|
|
|
|
|
|
def test_security_and_reset_config_options() -> None:
|
|
cfg = load_config(
|
|
environ={
|
|
"POZNAVACKY_SECRET_KEY": "tajny-klic",
|
|
"POZNAVACKY_BASE_URL": "http://127.0.0.1:8000",
|
|
"POZNAVACKY_SESSION_COOKIE_SECURE": "true",
|
|
"POZNAVACKY_TRUST_PROXY": "2",
|
|
"POZNAVACKY_PASSWORD_RESET_TTL_MINUTES": "30",
|
|
}
|
|
)
|
|
assert cfg.session_cookie_secure is True
|
|
assert cfg.trust_proxy == 2
|
|
assert cfg.password_reset_ttl_minutes == 30
|
|
|
|
cfg_http = load_config(
|
|
environ={
|
|
"POZNAVACKY_SECRET_KEY": "tajny-klic",
|
|
"POZNAVACKY_BASE_URL": "http://127.0.0.1:8000",
|
|
"PASSWORD_RESET_TTL_MINUTES": "45",
|
|
}
|
|
)
|
|
assert cfg_http.session_cookie_secure is False
|
|
assert cfg_http.password_reset_ttl_minutes == 45
|
|
|
|
cfg_override_false = load_config(
|
|
environ={
|
|
"POZNAVACKY_SECRET_KEY": "tajny-klic",
|
|
"POZNAVACKY_BASE_URL": "https://poznavacky.example.test",
|
|
"POZNAVACKY_SESSION_COOKIE_SECURE": "false",
|
|
}
|
|
)
|
|
assert cfg_override_false.session_cookie_secure is False
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("env_key", "bad_value"),
|
|
[
|
|
("POZNAVACKY_TRUST_PROXY", "-1"),
|
|
("POZNAVACKY_TRUST_PROXY", "abc"),
|
|
("POZNAVACKY_PASSWORD_RESET_TTL_MINUTES", "0"),
|
|
("POZNAVACKY_SESSION_COOKIE_SECURE", "mozna"),
|
|
],
|
|
)
|
|
def test_invalid_security_config_raises(env_key: str, bad_value: str) -> None:
|
|
with pytest.raises(ConfigError, match=env_key):
|
|
load_config(
|
|
environ={
|
|
"POZNAVACKY_SECRET_KEY": "klic",
|
|
env_key: bad_value,
|
|
}
|
|
)
|
|
|
|
|
|
@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)
|