test_oidc_env_reader.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. """BAMBUDDY_OIDC_* reader (#2593).
  2. The reader is deliberately dumb: it maps env vars to field names and applies
  3. defaults. Whether the resulting provider is *valid* is decided later, by the
  4. same OIDCProviderCreate schema the API uses, so env config cannot bypass a
  5. check the UI enforces.
  6. """
  7. from __future__ import annotations
  8. import pytest
  9. from backend.app.core.oidc_env import read_env_oidc_config
  10. REQUIRED = {
  11. "BAMBUDDY_OIDC_NAME": "Keycloak",
  12. "BAMBUDDY_OIDC_ISSUER_URL": "https://sso.example.com/realms/main",
  13. "BAMBUDDY_OIDC_CLIENT_ID": "bambuddy",
  14. "BAMBUDDY_OIDC_CLIENT_SECRET": "s3cr3t",
  15. }
  16. OPTIONAL = (
  17. "BAMBUDDY_OIDC_SCOPES",
  18. "BAMBUDDY_OIDC_ENABLED",
  19. "BAMBUDDY_OIDC_AUTO_CREATE_USERS",
  20. "BAMBUDDY_OIDC_AUTO_LINK_EXISTING",
  21. "BAMBUDDY_OIDC_EMAIL_CLAIM",
  22. "BAMBUDDY_OIDC_REQUIRE_EMAIL_VERIFIED",
  23. "BAMBUDDY_OIDC_ICON_URL",
  24. "BAMBUDDY_OIDC_AUTOLOGIN",
  25. "BAMBUDDY_OIDC_DEFAULT_GROUP",
  26. )
  27. @pytest.fixture(autouse=True)
  28. def clean_env(monkeypatch):
  29. for key in (*REQUIRED, *OPTIONAL):
  30. monkeypatch.delenv(key, raising=False)
  31. def _set_required(monkeypatch):
  32. for key, value in REQUIRED.items():
  33. monkeypatch.setenv(key, value)
  34. def test_returns_none_when_nothing_is_configured():
  35. assert read_env_oidc_config() is None
  36. @pytest.mark.parametrize("missing", sorted(REQUIRED))
  37. def test_returns_none_when_any_single_required_var_is_missing(monkeypatch, missing):
  38. """All four or nothing -- a half-configured provider must not reach the
  39. database, where it would fail at authorize time instead of at startup."""
  40. _set_required(monkeypatch)
  41. monkeypatch.delenv(missing)
  42. assert read_env_oidc_config() is None
  43. def test_an_empty_required_var_counts_as_unset(monkeypatch):
  44. """`BAMBUDDY_OIDC_CLIENT_SECRET=` in a compose file is a forgotten value,
  45. not an intentional empty secret."""
  46. _set_required(monkeypatch)
  47. monkeypatch.setenv("BAMBUDDY_OIDC_CLIENT_SECRET", "")
  48. assert read_env_oidc_config() is None
  49. def test_reads_the_required_vars(monkeypatch):
  50. _set_required(monkeypatch)
  51. cfg = read_env_oidc_config()
  52. assert cfg["name"] == "Keycloak"
  53. assert cfg["issuer_url"] == "https://sso.example.com/realms/main"
  54. assert cfg["client_id"] == "bambuddy"
  55. assert cfg["client_secret"] == "s3cr3t"
  56. def test_applies_the_documented_defaults(monkeypatch):
  57. _set_required(monkeypatch)
  58. cfg = read_env_oidc_config()
  59. assert cfg["scopes"] == "openid email profile"
  60. assert cfg["is_enabled"] is True
  61. assert cfg["auto_create_users"] is False
  62. assert cfg["auto_link_existing_accounts"] is False
  63. assert cfg["email_claim"] == "email"
  64. assert cfg["require_email_verified"] is True
  65. assert cfg["icon_url"] is None
  66. assert cfg["is_autologin"] is False
  67. @pytest.mark.parametrize("raw", ["true", "TRUE", "True", "1", "yes", "YES", " yes "])
  68. def test_booleans_accept_the_project_truthy_spellings(monkeypatch, raw):
  69. _set_required(monkeypatch)
  70. monkeypatch.setenv("BAMBUDDY_OIDC_AUTO_CREATE_USERS", raw)
  71. assert read_env_oidc_config()["auto_create_users"] is True
  72. @pytest.mark.parametrize("raw", ["false", "0", "no", "", "off", "nonsense"])
  73. def test_anything_else_is_false(monkeypatch, raw):
  74. """Only the three documented spellings enable a flag; an unrecognised value
  75. must not silently turn on auto-create-users."""
  76. _set_required(monkeypatch)
  77. monkeypatch.setenv("BAMBUDDY_OIDC_AUTO_CREATE_USERS", raw)
  78. assert read_env_oidc_config()["auto_create_users"] is False
  79. def test_a_boolean_default_of_true_can_be_turned_off(monkeypatch):
  80. _set_required(monkeypatch)
  81. monkeypatch.setenv("BAMBUDDY_OIDC_REQUIRE_EMAIL_VERIFIED", "false")
  82. assert read_env_oidc_config()["require_email_verified"] is False
  83. def test_optional_strings_override_their_defaults(monkeypatch):
  84. _set_required(monkeypatch)
  85. monkeypatch.setenv("BAMBUDDY_OIDC_SCOPES", "openid profile groups")
  86. monkeypatch.setenv("BAMBUDDY_OIDC_EMAIL_CLAIM", "mail")
  87. monkeypatch.setenv("BAMBUDDY_OIDC_ICON_URL", "https://sso.example.com/logo.png")
  88. cfg = read_env_oidc_config()
  89. assert cfg["scopes"] == "openid profile groups"
  90. assert cfg["email_claim"] == "mail"
  91. assert cfg["icon_url"] == "https://sso.example.com/logo.png"
  92. def test_the_default_group_is_read_as_a_name(monkeypatch):
  93. """A name, not an id: group ids differ per install, so an id in a compose
  94. file would point at whatever group happened to be created third."""
  95. _set_required(monkeypatch)
  96. monkeypatch.setenv("BAMBUDDY_OIDC_DEFAULT_GROUP", "Operators")
  97. cfg = read_env_oidc_config()
  98. assert cfg["default_group"] == "Operators"
  99. assert "default_group_id" not in cfg, "resolution needs the database, not the reader"
  100. @pytest.mark.parametrize("raw", ["", " "])
  101. def test_a_blank_default_group_is_unset(monkeypatch, raw):
  102. _set_required(monkeypatch)
  103. monkeypatch.setenv("BAMBUDDY_OIDC_DEFAULT_GROUP", raw)
  104. assert read_env_oidc_config()["default_group"] is None
  105. def test_every_var_the_reader_knows_is_registered_in_the_typo_guard():
  106. """An unregistered BAMBUDDY_* var logs "possible typo" at every boot, which
  107. would tell operators their correct config is wrong. Asserted against the
  108. reader's own vars rather than a copied list, so a var added later is caught
  109. here instead of in someone's logs."""
  110. from backend.app.core.config import _INTENTIONAL_UNSETTINGS
  111. unregistered = {v for v in (*REQUIRED, *OPTIONAL) if v not in _INTENTIONAL_UNSETTINGS}
  112. assert not unregistered