"""Tests pour src.config.initialization.""" from __future__ import annotations from unittest.mock import MagicMock, patch import pytest from src.config.initialization import InitializationError, Settings, get_settings _ENV_KEYS = ("BASE_URL", "HTTP_USERNAME", "HTTP_PASSWORD", "LWE_USERNAME", "LWE_PASSWORD") @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, ) @pytest.fixture def mock_login_success() -> MagicMock: mock_response = MagicMock() mock_response.cookies.get.return_value = "sess_xyz789" mock_client = MagicMock() mock_client.post.return_value = mock_response mock_client.__enter__.return_value = mock_client mock_client.__exit__.return_value = False return mock_client def test_get_settings_with_all_environment_variables( isolated_env: None, monkeypatch: pytest.MonkeyPatch, mock_login_success: MagicMock, ) -> None: monkeypatch.setenv("BASE_URL", "https://api.example.com/") monkeypatch.setenv("LWE_USERNAME", "admin") monkeypatch.setenv("LWE_PASSWORD", "secret") with patch("src.config.initialization.Client", return_value=mock_login_success): settings = get_settings() assert settings == Settings( base_url="https://api.example.com", phpsessid="sess_xyz789", ) mock_login_success.post.assert_called_once_with( "https://api.example.com/rest/user/login", json={"id": "admin", "password": "secret"}, ) def test_get_settings_injects_basic_auth_when_lwe_credentials_are_set( isolated_env: None, monkeypatch: pytest.MonkeyPatch, mock_login_success: MagicMock, ) -> None: monkeypatch.setenv("BASE_URL", "api.example.com") monkeypatch.setenv("LWE_USERNAME", "admin") monkeypatch.setenv("LWE_PASSWORD", "secret") monkeypatch.setenv("HTTP_USERNAME", "basic_user") monkeypatch.setenv("HTTP_PASSWORD", "basic_pass") with patch("src.config.initialization.Client", return_value=mock_login_success): settings = get_settings() assert settings.base_url == "https://basic_user:basic_pass@api.example.com" mock_login_success.post.assert_called_once_with( "https://basic_user:basic_pass@api.example.com/rest/user/login", json={"id": "admin", "password": "secret"}, ) 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_USERNAME" in message assert "LWE_PASSWORD" 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 "USERNAME" in message assert "PASSWORD" in message def test_get_settings_raises_when_login_does_not_return_phpsessid( isolated_env: None, monkeypatch: pytest.MonkeyPatch, ) -> None: monkeypatch.setenv("BASE_URL", "https://api.example.com") monkeypatch.setenv("LWE_USERNAME", "admin") monkeypatch.setenv("LWE_PASSWORD", "secret") mock_response = MagicMock() mock_response.cookies.get.return_value = None mock_client = MagicMock() mock_client.post.return_value = mock_response mock_client.__enter__.return_value = mock_client mock_client.__exit__.return_value = False with patch("src.config.initialization.Client", return_value=mock_client): with pytest.raises(InitializationError) as exc_info: get_settings() assert "PHPSESSID non trouvé" in str(exc_info.value) def test_get_settings_raises_when_login_request_fails( isolated_env: None, monkeypatch: pytest.MonkeyPatch, ) -> None: monkeypatch.setenv("BASE_URL", "https://api.example.com") monkeypatch.setenv("LWE_USERNAME", "admin") monkeypatch.setenv("LWE_PASSWORD", "secret") with patch( "src.config.initialization.Client", side_effect=RuntimeError("network down"), ): with pytest.raises(InitializationError) as exc_info: get_settings() assert "Erreur lors de la connexion" in str(exc_info.value)