fix: strengthen load_federation_key() docstring, tests, and env var coverage

- Add docstring to load_federation_key() for module consistency
- Strengthen test_load_federation_key_uses_default_path to call the
  function and assert it returns "" when file absent
- Add test_load_federation_key_uses_env_var_override to cover the
  MUXPLEX_FEDERATION_KEY_FILE env var override path

All 31 tests pass.
This commit is contained in:
Brian Krabach
2026-04-01 10:12:09 -07:00
parent afef15b955
commit 8e779b49b9
2 changed files with 24 additions and 3 deletions
+6
View File
@@ -80,6 +80,12 @@ def patch_settings(patch: dict) -> dict:
def load_federation_key() -> str: def load_federation_key() -> str:
"""Load the federation key from disk or env-overridden path.
Reads from FEDERATION_KEY_PATH by default; override via
MUXPLEX_FEDERATION_KEY_FILE env var. Returns empty string when
the file does not exist.
"""
env_path = os.environ.get("MUXPLEX_FEDERATION_KEY_FILE") env_path = os.environ.get("MUXPLEX_FEDERATION_KEY_FILE")
path = Path(env_path) if env_path else FEDERATION_KEY_PATH path = Path(env_path) if env_path else FEDERATION_KEY_PATH
try: try:
+18 -3
View File
@@ -447,11 +447,26 @@ def test_load_federation_key_reads_existing_file(tmp_path, monkeypatch):
assert result == "my-secret-key" assert result == "my-secret-key"
def test_load_federation_key_uses_default_path(monkeypatch): def test_load_federation_key_uses_default_path(tmp_path, monkeypatch):
"""load_federation_key() uses ~/.config/muxplex/federation_key when env var is not set.""" """load_federation_key() uses ~/.config/muxplex/federation_key when env var is not set."""
from muxplex.settings import FEDERATION_KEY_PATH from muxplex.settings import FEDERATION_KEY_PATH
monkeypatch.delenv("MUXPLEX_FEDERATION_KEY_FILE", raising=False) monkeypatch.delenv("MUXPLEX_FEDERATION_KEY_FILE", raising=False)
# The default path should be used; we just verify no env override changes behaviour
# When the default file doesn't exist, returns empty string
assert FEDERATION_KEY_PATH == Path.home() / ".config" / "muxplex" / "federation_key" assert FEDERATION_KEY_PATH == Path.home() / ".config" / "muxplex" / "federation_key"
# Redirect the constant so the function uses a guaranteed-absent path in tmp_path
monkeypatch.setattr(settings_mod, "FEDERATION_KEY_PATH", tmp_path / "absent_key")
# Also verify the function runs without error (returns "" when file absent)
result = load_federation_key()
assert isinstance(result, str)
assert result == ""
def test_load_federation_key_uses_env_var_override(tmp_path, monkeypatch):
"""load_federation_key() reads from MUXPLEX_FEDERATION_KEY_FILE when set."""
key_file = tmp_path / "custom_key"
key_file.write_text("env-override-key\n")
monkeypatch.setenv("MUXPLEX_FEDERATION_KEY_FILE", str(key_file))
result = load_federation_key()
assert result == "env-override-key"