143 lines
5.1 KiB
Python
143 lines
5.1 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")
|
|
monkeypatch.setenv("BINDERY_USERNAME", " reader ")
|
|
monkeypatch.setenv("BINDERY_PASSWORD_HASH", " hash ")
|
|
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"
|
|
assert config.LOCAL_USERNAME == "reader"
|
|
assert config.LOCAL_PASSWORD_HASH == "hash"
|
|
|
|
|
|
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",
|
|
"INGEST_DIR",
|
|
"BINDERY_USERNAME",
|
|
"BINDERY_PASSWORD_HASH",
|
|
):
|
|
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
|
|
assert config.INGEST_DIR is None
|
|
assert config.LOCAL_USERNAME == ""
|
|
assert config.LOCAL_PASSWORD_HASH == ""
|
|
|
|
|
|
@pytest.mark.parametrize("value", ["", " "])
|
|
def test_empty_ingest_directory_disables_uploads(monkeypatch, reload_config, value):
|
|
monkeypatch.setenv("INGEST_DIR", value)
|
|
reload_config()
|
|
assert config.INGEST_DIR is None
|
|
|
|
|
|
def test_prepare_without_cwa_creates_only_bindery_folders(monkeypatch, tmp_path, caplog):
|
|
monkeypatch.setattr(config, "INGEST_DIR", None)
|
|
monkeypatch.setattr(config, "CWA_URL", "")
|
|
monkeypatch.setattr(config, "CALIBRE_DB_PATH", None)
|
|
for name, folder in [("CONFIG_DIR", "config"), ("UPLOAD_DIR", "uploads"), ("TMP_DIR", "tmp")]:
|
|
monkeypatch.setattr(config, name, tmp_path / folder)
|
|
monkeypatch.setattr(config, "PERSONAL_INI", tmp_path / "config" / "personal.ini")
|
|
config.prepare()
|
|
assert config.PERSONAL_INI.exists()
|
|
assert {p.name for p in tmp_path.iterdir()} == {"config", "uploads", "tmp"}
|
|
assert caplog.records == []
|