porchlight/tests/test_config.py

100 lines
3.5 KiB
Python

# tests/test_config.py
from pathlib import Path
import pytest
from porchlight.config import Settings, StorageBackend
def test_default_settings() -> None:
settings = Settings(
issuer="http://localhost:8000",
)
assert settings.issuer == "http://localhost:8000"
assert settings.storage_backend == StorageBackend.SQLITE
assert settings.sqlite_path == "data/oidc_op.db"
assert settings.manage_client_id == "manage-app"
assert settings.invite_ttl == 86400
assert settings.theme == "default"
def test_mongodb_settings() -> None:
settings = Settings(
issuer="http://localhost:8000",
storage_backend=StorageBackend.MONGODB,
mongodb_uri="mongodb://mongo:27017",
mongodb_database="test_db",
)
assert settings.storage_backend == StorageBackend.MONGODB
assert settings.mongodb_uri == "mongodb://mongo:27017"
assert settings.mongodb_database == "test_db"
def test_default_settings_has_empty_clients() -> None:
settings = Settings(issuer="http://localhost:8000")
assert settings.clients == {}
def test_settings_from_env(monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setenv("OIDC_OP_ISSUER", "https://op.example.org")
monkeypatch.setenv("OIDC_OP_STORAGE_BACKEND", "mongodb")
monkeypatch.setenv("OIDC_OP_MONGODB_URI", "mongodb://remote:27017")
settings = Settings()
assert settings.issuer == "https://op.example.org"
assert settings.storage_backend == StorageBackend.MONGODB
def test_settings_from_toml_file(tmp_path: Path) -> None:
toml_content = """\
issuer = "https://toml.example.com"
debug = true
sqlite_path = "custom/path.db"
[clients.my-app]
client_secret = "secret123"
redirect_uris = ["https://app.example.com/callback"]
scope = ["openid", "profile"]
"""
toml_file = tmp_path / "test.toml"
toml_file.write_text(toml_content)
settings = Settings(_toml_file=str(toml_file))
assert settings.issuer == "https://toml.example.com"
assert settings.debug is True
assert settings.sqlite_path == "custom/path.db"
assert "my-app" in settings.clients
assert settings.clients["my-app"].client_secret == "secret123"
assert settings.clients["my-app"].redirect_uris == ["https://app.example.com/callback"]
assert settings.clients["my-app"].scope == ["openid", "profile"]
assert settings.clients["my-app"].response_types == ["code"]
assert settings.clients["my-app"].token_endpoint_auth_method == "client_secret_basic"
def test_env_vars_override_toml_values(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
toml_content = """\
issuer = "https://toml.example.com"
debug = false
"""
toml_file = tmp_path / "test.toml"
toml_file.write_text(toml_content)
monkeypatch.setenv("OIDC_OP_ISSUER", "https://env.example.com")
monkeypatch.setenv("OIDC_OP_DEBUG", "true")
settings = Settings(_toml_file=str(toml_file))
assert settings.issuer == "https://env.example.com"
assert settings.debug is True
def test_missing_toml_file_uses_defaults() -> None:
settings = Settings(issuer="http://localhost:8000", _toml_file="/nonexistent/path.toml")
assert settings.issuer == "http://localhost:8000"
assert settings.clients == {}
def test_config_file_env_var_override(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
toml_file = tmp_path / "test.toml"
toml_file.write_text('issuer = "https://custom-path.example.com"\n')
monkeypatch.setenv("OIDC_OP_CONFIG_FILE", str(toml_file))
settings = Settings()
assert settings.issuer == "https://custom-path.example.com"