test_config_env_warnings.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. """S6: warn on unknown MFA_*/BAMBUDDY_* env vars so typos like
  2. ``MFA_ENCYPTION_KEY`` are not silently swallowed by ``extra="ignore"``."""
  3. from __future__ import annotations
  4. import importlib
  5. import logging
  6. import pytest
  7. @pytest.mark.unit
  8. def test_unknown_mfa_env_var_logs_info(monkeypatch, caplog):
  9. """A typo'd MFA_* env var must be logged at INFO so operators see it."""
  10. monkeypatch.setenv("MFA_ENCYPTION_KEY", "typo-value") # missing R
  11. import backend.app.core.config as cfg_mod
  12. with caplog.at_level(logging.INFO):
  13. importlib.reload(cfg_mod)
  14. assert any("MFA_ENCYPTION_KEY" in rec.message for rec in caplog.records)
  15. @pytest.mark.unit
  16. def test_unknown_bambuddy_env_var_logs_info(monkeypatch, caplog):
  17. """An unrecognised BAMBUDDY_* env var must also be logged."""
  18. monkeypatch.setenv("BAMBUDDY_NEW_FEATURE", "v1")
  19. import backend.app.core.config as cfg_mod
  20. with caplog.at_level(logging.INFO):
  21. importlib.reload(cfg_mod)
  22. assert any("BAMBUDDY_NEW_FEATURE" in rec.message for rec in caplog.records)
  23. @pytest.mark.unit
  24. def test_known_intentional_env_var_does_not_log(monkeypatch, caplog):
  25. """MFA_ENCRYPTION_KEY is declared in _INTENTIONAL_UNSETTINGS — must be silent."""
  26. monkeypatch.setenv("MFA_ENCRYPTION_KEY", "x" * 44) # invalid but not a typo
  27. import backend.app.core.config as cfg_mod
  28. with caplog.at_level(logging.INFO):
  29. importlib.reload(cfg_mod)
  30. # The intentional var must not produce a typo warning.
  31. typo_warnings = [
  32. rec for rec in caplog.records if "MFA_ENCRYPTION_KEY" in rec.message and "typo" in rec.message.lower()
  33. ]
  34. assert typo_warnings == []