114 lines
3.3 KiB
Python
114 lines
3.3 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
|
|
|
|
|
|
@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)
|