"""Tests pour src.config.initialization.""" from __future__ import annotations import pytest from src.config.initialization import InitializationError, Settings, get_settings _ENV_KEYS = ("BASE_URL", "LWE_TOKEN", "PHPSESSID") @pytest.fixture(autouse=True) def _reset_settings_cache() -> None: get_settings.cache_clear() yield get_settings.cache_clear() @pytest.fixture def isolated_env(monkeypatch: pytest.MonkeyPatch) -> None: """Empeche le chargement du .env local et nettoie les variables.""" for key in _ENV_KEYS: monkeypatch.delenv(key, raising=False) monkeypatch.setattr( "src.config.initialization.load_dotenv", lambda *_args, **_kwargs: False, ) def test_get_settings_with_all_environment_variables( isolated_env: None, monkeypatch: pytest.MonkeyPatch, ) -> None: monkeypatch.setenv("BASE_URL", "https://api.example.com/") monkeypatch.setenv("LWE_TOKEN", "lwe_tok_abc123") monkeypatch.setenv("PHPSESSID", "sess_xyz789") settings = get_settings() assert settings == Settings( base_url="https://api.example.com", lwe_token="lwe_tok_abc123", phpsessid="sess_xyz789", ) def test_get_settings_raises_when_environment_variables_are_missing( isolated_env: None, monkeypatch: pytest.MonkeyPatch, ) -> None: monkeypatch.setenv("BASE_URL", "https://api.example.com") with pytest.raises(InitializationError) as exc_info: get_settings() message = str(exc_info.value) assert "Variables d'environnement manquantes" in message assert "LWE_TOKEN" in message assert "PHPSESSID" in message def test_get_settings_raises_when_env_file_is_missing( isolated_env: None, ) -> None: with pytest.raises(InitializationError) as exc_info: get_settings() message = str(exc_info.value) assert "Variables d'environnement manquantes" in message assert "BASE_URL" in message assert "LWE_TOKEN" in message assert "PHPSESSID" in message