104 lines
3.1 KiB
Python
104 lines
3.1 KiB
Python
from __future__ import annotations
|
|
|
|
import os
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
|
|
from tenbackward.config import ConfigError, load_config, validate_config
|
|
|
|
|
|
def _populate(env_setup) -> dict[str, str]:
|
|
return {k: os.environ[k] for k in os.environ if k.startswith(("MASTODON_", "SITE_", "HASHTAGS", "RUN_AT", "THROWBACK_", "RETRY_", "TZ"))}
|
|
|
|
|
|
def test_load_config_succeeds_with_complete_env(env_setup) -> None:
|
|
config = load_config()
|
|
assert config.mastodon_base_url == "https://mastodon.example"
|
|
assert config.run_at == "09:00"
|
|
assert config.retry_count == 3
|
|
assert config.throwback_prefix == "Throwback:"
|
|
|
|
|
|
def test_validate_config_lists_every_missing_key(env_setup, monkeypatch) -> None:
|
|
monkeypatch.delenv("MASTODON_ACCESS_TOKEN")
|
|
monkeypatch.delenv("SITE_URL")
|
|
|
|
values = {k: os.environ.get(k, "") for k in [
|
|
"MASTODON_BASE_URL",
|
|
"MASTODON_ACCESS_TOKEN",
|
|
"SITE_URL",
|
|
"HASHTAGS",
|
|
"RUN_AT",
|
|
]}
|
|
|
|
with pytest.raises(ConfigError) as excinfo:
|
|
validate_config(values)
|
|
|
|
message = str(excinfo.value)
|
|
assert "MASTODON_ACCESS_TOKEN" in message
|
|
assert "SITE_URL" in message
|
|
|
|
|
|
def test_validate_config_rejects_bad_run_at() -> None:
|
|
values = {
|
|
"MASTODON_BASE_URL": "https://mastodon.example",
|
|
"MASTODON_ACCESS_TOKEN": "x",
|
|
"SITE_URL": "https://blog.example.com",
|
|
"HASHTAGS": "#x",
|
|
"RUN_AT": "25:99",
|
|
"MASTODON_VISIBILITY": "public",
|
|
"THROWBACK_PREFIX": "Throwback:",
|
|
"RETRY_COUNT": "3",
|
|
"TZ": "Europe/Berlin",
|
|
}
|
|
with pytest.raises(ConfigError):
|
|
validate_config(values)
|
|
|
|
|
|
def test_load_config_applies_optional_defaults(env_setup, monkeypatch) -> None:
|
|
monkeypatch.delenv("THROWBACK_PREFIX", raising=False)
|
|
monkeypatch.delenv("RETRY_COUNT", raising=False)
|
|
|
|
config = load_config()
|
|
assert config.throwback_prefix == "Throwback:"
|
|
assert config.retry_count == 3
|
|
assert config.tz == "Europe/Berlin"
|
|
assert config.mastodon_visibility == "public"
|
|
|
|
|
|
def test_load_config_reads_dotenv_file(tmp_path: Path, monkeypatch) -> None:
|
|
dotenv = tmp_path / ".env"
|
|
dotenv.write_text(
|
|
"MASTODON_BASE_URL=https://from-file.example\n"
|
|
"MASTODON_ACCESS_TOKEN=file-token\n"
|
|
"SITE_URL=https://blog.example.com\n"
|
|
"HASHTAGS=#throwback\n"
|
|
"RUN_AT=12:34\n"
|
|
)
|
|
|
|
for key in ("MASTODON_BASE_URL", "MASTODON_ACCESS_TOKEN", "SITE_URL", "HASHTAGS", "RUN_AT"):
|
|
monkeypatch.delenv(key, raising=False)
|
|
|
|
config = load_config(dotenv_path=dotenv)
|
|
assert config.mastodon_base_url == "https://from-file.example"
|
|
assert config.mastodon_access_token == "file-token"
|
|
assert config.run_at == "12:34"
|
|
|
|
|
|
def test_env_overrides_dotenv(tmp_path: Path, monkeypatch) -> None:
|
|
dotenv = tmp_path / ".env"
|
|
dotenv.write_text("RUN_AT=01:00\n")
|
|
|
|
monkeypatch.setenv("RUN_AT", "23:00")
|
|
for key in (
|
|
"MASTODON_BASE_URL",
|
|
"MASTODON_ACCESS_TOKEN",
|
|
"SITE_URL",
|
|
"HASHTAGS",
|
|
):
|
|
monkeypatch.setenv(key, "x")
|
|
|
|
config = load_config(dotenv_path=dotenv)
|
|
assert config.run_at == "23:00"
|