Files
bindery/tests/test_config.py
T
tim c30622e3a0 Download stories and build epubs with FanFicFare
FanFicFare adapters fetch metadata and chapters, and its epub writer
builds each book from the store. Environment variables configure the
app, and a default personal.ini is copied to the config folder on first
start.
2026-09-27 23:38:28 +02:00

113 lines
3.8 KiB
Python

"""Reading the configuration from environment variables."""
import importlib
from pathlib import Path
import pytest
from bindery import config
@pytest.fixture
def reload_config(monkeypatch):
"""Load the config module again with a changed environment, and restore it afterwards."""
yield lambda: importlib.reload(config)
monkeypatch.undo()
importlib.reload(config)
def test_bool(monkeypatch):
for value in ("true", "Yes", " 1 ", "on", "y"):
monkeypatch.setenv("X_TEST", value)
assert config._bool("X_TEST")
for value in ("false", "0", "no", ""):
monkeypatch.setenv("X_TEST", value)
assert not config._bool("X_TEST")
monkeypatch.delenv("X_TEST")
assert config._bool("X_TEST", "true")
def test_list(monkeypatch):
monkeypatch.setenv("X_TEST", " title, tags ,,series ")
assert config._list("X_TEST") == ["title", "tags", "series"]
monkeypatch.delenv("X_TEST")
assert config._list("X_TEST") == []
assert config._list("X_TEST", "title") == ["title"]
def test_relative_paths_become_absolute(monkeypatch, tmp_path, reload_config):
monkeypatch.chdir(tmp_path)
for name, value in [("CONFIG_DIR", "dev/config"), ("INGEST_DIR", "dev/ingest"), ("TMP_DIR", "dev/tmp")]:
monkeypatch.setenv(name, value)
monkeypatch.setenv("CWA_DB_PATH", "cwa/app.db")
monkeypatch.setenv("CALIBRE_DB_PATH", "library/metadata.db")
reload_config()
assert config.CONFIG_DIR == Path.cwd() / "dev/config"
assert config.INGEST_DIR == Path.cwd() / "dev/ingest"
assert config.TMP_DIR == Path.cwd() / "dev/tmp"
assert config.CWA_DB_PATH == Path.cwd() / "cwa/app.db"
assert config.CALIBRE_DB_PATH == Path.cwd() / "library/metadata.db"
@pytest.mark.parametrize(
("value", "expected"),
[
("", ""),
("/", ""),
("bindery", "/bindery"),
("/bindery/", "/bindery"),
(" /a/b ", "/a/b"),
],
)
def test_base_path(value, expected):
assert config._base_path(value) == expected
def test_values_from_the_environment(monkeypatch, reload_config):
monkeypatch.setenv("URL_BASE", "bindery/")
monkeypatch.setenv("AUTH_METHOD", " CWA ")
monkeypatch.setenv("UPDATE_INTERVAL_HOURS", "1.5")
monkeypatch.setenv("DEFAULT_LOCKED_FIELDS", "title, authors")
monkeypatch.setenv("CWA_ALLOWED_USERS", "alice,bob")
monkeypatch.setenv("CONFIG_DIR", "/data/bindery")
monkeypatch.setenv("TRUST_PROXY", "yes")
monkeypatch.setenv("CWA_DB_PATH", " /cwa/app.db ")
monkeypatch.setenv("CALIBRE_DB_PATH", "/library/metadata.db")
reload_config()
assert config.URL_BASE == "/bindery"
assert config.AUTH_METHOD == "cwa"
assert config.UPDATE_INTERVAL_HOURS == 1.5
assert config.DEFAULT_LOCKED_FIELDS == ["title", "authors"]
assert config.CWA_ALLOWED_USERS == ["alice", "bob"]
assert str(config.DB_PATH) == "/data/bindery/bindery.db"
assert config.TRUST_PROXY is True
assert str(config.CWA_DB_PATH) == "/cwa/app.db"
assert str(config.CALIBRE_DB_PATH) == "/library/metadata.db"
def test_defaults(monkeypatch, reload_config):
for name in (
"URL_BASE",
"AUTH_METHOD",
"UPDATE_INTERVAL_HOURS",
"DEFAULT_LOCKED_FIELDS",
"LOG_LEVEL",
"PROXY_AUTH_USER_HEADER",
"SESSION_COOKIE_SECURE",
"TRUST_PROXY",
"CWA_DB_PATH",
"CALIBRE_DB_PATH",
):
monkeypatch.delenv(name, raising=False)
reload_config()
assert config.URL_BASE == ""
assert config.AUTH_METHOD == "none"
assert config.UPDATE_INTERVAL_HOURS == 24
assert config.DEFAULT_LOCKED_FIELDS == ["title"]
assert config.LOG_LEVEL == "INFO"
assert config.PROXY_AUTH_USER_HEADER == "X-Remote-User"
assert config.SESSION_COOKIE_SECURE is False
assert config.TRUST_PROXY is False
assert config.CWA_DB_PATH is None
assert config.CALIBRE_DB_PATH is None