def test_setup_on_override(mocked_import_config_module): """If non-overridden option is accessed, then config should be loaded.""" config = Konfig() with config.override(SOMETHING="awesome"): assert config.EXAMPLE == "test" # Py2.7, Py3.5: replace with `assert_called` when 2.7/3.5 support will be dropped. assert mocked_import_config_module.called is True assert mocked_import_config_module.call_count >= 1
def test_get_secret_with_prefix(vault_prefix, transform): """Trailing and leading slashes don't matter.""" config = Konfig(vault_backend=VaultBackend(transform(vault_prefix), try_env_first=False)) assert config.get_secret("/path/to") == { "SECRET": "value", "IS_SECRET": True, "DECIMAL": "1.3" }
def test_override_unknown_option(): """If an option passed to `override` doesn't exist in the config module an error should be risen. Active only with `strict_override` config option. """ config = Konfig(strict_override=True) with pytest.raises( ForbiddenOverrideError, match= "Can't override `NOT_EXIST` config option, because it is not defined in the config module", ): with config.override(NOT_EXIST=123): pass
def test_missing_vault_backend(): config = Konfig() with pytest.raises( VaultBackendMissing, match="Vault backend is not configured. " "Please specify `vault_backend` option in your `Konfig` initialization", ): config.SECRET
def test_retry_object(vault_prefix, mocker): config = Konfig( vault_backend=VaultBackend( vault_prefix, retry=Retrying(retry=retry_if_exception_type(KonfettiError), reraise=True, stop=stop_after_attempt(2)), ) ) mocker.patch("requests.adapters.HTTPAdapter.send", side_effect=KonfettiError) m = mocker.patch.object(config.vault_backend, "_call", wraps=config.vault_backend._call) with pytest.raises(KonfettiError): config.SECRET assert m.called is True assert m.call_count == 2
async def test_retry_object(vault_prefix, mocker): config = Konfig(vault_backend=AsyncVaultBackend( vault_prefix, retry=AsyncRetrying(retry=retry_if_exception_type(KonfettiError), reraise=True, stop=stop_after_attempt(2)), )) mocker.patch("aiohttp.ClientSession._request", side_effect=KonfettiError) m = mocker.patch.object(config.vault_backend, "_call", wraps=config.vault_backend._call) with pytest.raises(KonfettiError): await config.SECRET assert m.called is True assert m.call_count == 2
async def test_asdict_shortcut(vault_prefix, vault_addr, vault_token): # If there are no coroutines - nothing should be awaited class TestSettings: SECRET = 1 VAULT_ADDR = env("VAULT_ADDR") VAULT_TOKEN = env("VAULT_TOKEN") config = Konfig.from_object(TestSettings, vault_backend=AsyncVaultBackend(vault_prefix)) assert await config.asdict() == { "SECRET": 1, "VAULT_ADDR": vault_addr, "VAULT_TOKEN": vault_token }
def test_vault_var_reusage(vault_prefix, vault_addr, vault_token): variable = vault("path/to") class Test: VAULT_ADDR = vault_addr VAULT_TOKEN = vault_token SECRET = variable["SECRET"] IS_SECRET = variable["IS_SECRET"] config = Konfig.from_object(Test, vault_backend=VaultBackend(vault_prefix)) assert config.asdict() == { "SECRET": "value", "IS_SECRET": True, "VAULT_ADDR": vault_addr, "VAULT_TOKEN": vault_token, }
async def test_asdict(monkeypatch, vault_prefix, vault_addr, vault_token): # All options, including dicts should be evaluated monkeypatch.setenv("KONFETTI_SETTINGS", "test_app.settings.subset") config = Konfig(vault_backend=AsyncVaultBackend(vault_prefix)) assert await config.asdict() == { "DEBUG": True, "SECRET": "value", "KEY": "value", "VAULT_ADDR": vault_addr, "VAULT_TOKEN": vault_token, "NESTED_SECRET": "what?", "WHOLE_SECRET": { "DECIMAL": "1.3", "IS_SECRET": True, "SECRET": "value" }, "DICTIONARY": { "env": True, "static": 1, "vault": "value" }, }
def test_config_instance(app, vault_prefix): config = Konfig(vault_backend=VaultBackend(vault_prefix), strict_override=False) FlaskKonfig(app, konfig=config, **CUSTOM_KWARGS) assert_config(app.config) assert app.config.SECRET == "value"
def test_no_setup_on_override(mocked_import_config_module): """If overridden option is accessed, then config is not loaded.""" config = Konfig(strict_override=False) with config.override(EXAMPLE="awesome"): assert config.EXAMPLE == "awesome" mocked_import_config_module.assert_not_called()
def test_strict_override_valid(): config = Konfig(strict_override=True) with config.override(INTEGER=123): assert config.INTEGER == 123
def config_with_cached_vault(vault_prefix): return Konfig(vault_backend=VaultBackend(vault_prefix, cache_ttl=1))
from konfetti import env, Konfig KEY = "value" DEBUG = env("DEBUG", default=True, cast=bool) config = Konfig.from_object(__name__)