test_oidc_env_reader.py 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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 EnvOIDCConfigError, env_bool, 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", "FALSE", "False", "0", "no", "NO"])
  73. def test_falsy_values_are_false(monkeypatch, raw):
  74. _set_required(monkeypatch)
  75. monkeypatch.setenv("BAMBUDDY_OIDC_AUTO_CREATE_USERS", raw)
  76. assert read_env_oidc_config()["auto_create_users"] is False
  77. @pytest.mark.parametrize("raw", ["on", "enabled", "y", "nonsense"])
  78. def test_an_unrecognized_boolean_is_rejected(monkeypatch, raw):
  79. """Only the documented spellings are accepted; an unrecognised value must
  80. not silently turn a flag on or off -- it must refuse the whole config
  81. instead of guessing (M-R4 strict boolean parsing)."""
  82. _set_required(monkeypatch)
  83. monkeypatch.setenv("BAMBUDDY_OIDC_AUTO_CREATE_USERS", raw)
  84. with pytest.raises(EnvOIDCConfigError, match="BAMBUDDY_OIDC_AUTO_CREATE_USERS"):
  85. read_env_oidc_config()
  86. def test_a_boolean_default_of_true_can_be_turned_off(monkeypatch):
  87. _set_required(monkeypatch)
  88. monkeypatch.setenv("BAMBUDDY_OIDC_REQUIRE_EMAIL_VERIFIED", "false")
  89. assert read_env_oidc_config()["require_email_verified"] is False
  90. # --- env_bool, tested directly ------------------------------------------------
  91. # The reader-level tests above pin the contract through read_env_oidc_config;
  92. # these exercise the helper itself so its default/blank/reject behavior is
  93. # proven independently of any particular BAMBUDDY_OIDC_* field.
  94. @pytest.mark.parametrize("raw", ["false", "FALSE", "0", "no", "NO"])
  95. def test_env_bool_falsy_values_are_false(monkeypatch, raw):
  96. monkeypatch.setenv("SOME_FLAG", raw)
  97. assert env_bool("SOME_FLAG", True) is False
  98. @pytest.mark.parametrize("default", [True, False])
  99. def test_env_bool_absent_is_the_given_default(monkeypatch, default):
  100. monkeypatch.delenv("SOME_FLAG", raising=False)
  101. assert env_bool("SOME_FLAG", default) is default
  102. @pytest.mark.parametrize("raw", ["", " "])
  103. @pytest.mark.parametrize("default", [True, False])
  104. def test_env_bool_blank_is_the_given_default(monkeypatch, raw, default):
  105. monkeypatch.setenv("SOME_FLAG", raw)
  106. assert env_bool("SOME_FLAG", default) is default
  107. @pytest.mark.parametrize("raw", ["on", "enabled", "y", "nonsense"])
  108. def test_env_bool_rejects_an_unrecognized_value(monkeypatch, raw):
  109. monkeypatch.setenv("SOME_FLAG", raw)
  110. with pytest.raises(EnvOIDCConfigError, match="SOME_FLAG"):
  111. env_bool("SOME_FLAG", True)
  112. def test_optional_strings_override_their_defaults(monkeypatch):
  113. _set_required(monkeypatch)
  114. monkeypatch.setenv("BAMBUDDY_OIDC_SCOPES", "openid profile groups")
  115. monkeypatch.setenv("BAMBUDDY_OIDC_EMAIL_CLAIM", "mail")
  116. monkeypatch.setenv("BAMBUDDY_OIDC_ICON_URL", "https://sso.example.com/logo.png")
  117. cfg = read_env_oidc_config()
  118. assert cfg["scopes"] == "openid profile groups"
  119. assert cfg["email_claim"] == "mail"
  120. assert cfg["icon_url"] == "https://sso.example.com/logo.png"
  121. @pytest.mark.parametrize("raw", ["", " "])
  122. def test_a_blank_scopes_is_unset(monkeypatch, raw):
  123. """`BAMBUDDY_OIDC_SCOPES=` in a compose file is a forgotten value, not a
  124. request for a provider with no scopes -- same rule as default_group."""
  125. _set_required(monkeypatch)
  126. monkeypatch.setenv("BAMBUDDY_OIDC_SCOPES", raw)
  127. assert read_env_oidc_config()["scopes"] == "openid email profile"
  128. @pytest.mark.parametrize("raw", ["", " "])
  129. def test_a_blank_email_claim_is_unset(monkeypatch, raw):
  130. _set_required(monkeypatch)
  131. monkeypatch.setenv("BAMBUDDY_OIDC_EMAIL_CLAIM", raw)
  132. assert read_env_oidc_config()["email_claim"] == "email"
  133. @pytest.mark.parametrize("raw", ["", " "])
  134. def test_a_blank_icon_url_is_unset(monkeypatch, raw):
  135. """Uncommenting `# BAMBUDDY_OIDC_ICON_URL=` in .env.example must not take
  136. the provider down -- the reader must still return a config, not refuse it."""
  137. _set_required(monkeypatch)
  138. monkeypatch.setenv("BAMBUDDY_OIDC_ICON_URL", raw)
  139. cfg = read_env_oidc_config()
  140. assert cfg is not None, "a blank optional var must not refuse the whole provider"
  141. assert cfg["icon_url"] is None
  142. def test_the_default_group_is_read_as_a_name(monkeypatch):
  143. """A name, not an id: group ids differ per install, so an id in a compose
  144. file would point at whatever group happened to be created third."""
  145. _set_required(monkeypatch)
  146. monkeypatch.setenv("BAMBUDDY_OIDC_DEFAULT_GROUP", "Operators")
  147. cfg = read_env_oidc_config()
  148. assert cfg["default_group"] == "Operators"
  149. assert "default_group_id" not in cfg, "resolution needs the database, not the reader"
  150. @pytest.mark.parametrize("raw", ["", " "])
  151. def test_a_blank_default_group_is_unset(monkeypatch, raw):
  152. _set_required(monkeypatch)
  153. monkeypatch.setenv("BAMBUDDY_OIDC_DEFAULT_GROUP", raw)
  154. assert read_env_oidc_config()["default_group"] is None
  155. def test_every_var_the_reader_knows_is_registered_in_the_typo_guard():
  156. """An unregistered BAMBUDDY_* var logs "possible typo" at every boot, which
  157. would tell operators their correct config is wrong. Asserted against the
  158. reader's own vars rather than a copied list, so a var added later is caught
  159. here instead of in someone's logs."""
  160. from backend.app.core.config import _INTENTIONAL_UNSETTINGS
  161. unregistered = {v for v in (*REQUIRED, *OPTIONAL) if v not in _INTENTIONAL_UNSETTINGS}
  162. assert not unregistered