|
|
@@ -2,6 +2,7 @@
|
|
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
+import base64
|
|
|
from unittest.mock import MagicMock, patch
|
|
|
|
|
|
import pytest
|
|
|
@@ -34,6 +35,7 @@ def mock_login_success() -> MagicMock:
|
|
|
mock_response = MagicMock()
|
|
|
mock_response.cookies.get.return_value = "sess_xyz789"
|
|
|
mock_client = MagicMock()
|
|
|
+ mock_client.headers = {}
|
|
|
mock_client.post.return_value = mock_response
|
|
|
mock_client.__enter__.return_value = mock_client
|
|
|
mock_client.__exit__.return_value = False
|
|
|
@@ -55,31 +57,41 @@ def test_get_settings_with_all_environment_variables(
|
|
|
assert settings == Settings(
|
|
|
base_url="https://api.example.com",
|
|
|
phpsessid="sess_xyz789",
|
|
|
+ authorization="",
|
|
|
)
|
|
|
mock_login_success.post.assert_called_once_with(
|
|
|
"https://api.example.com/rest/user/login",
|
|
|
- json={"id": "admin", "password": "secret"},
|
|
|
+ data={"id": "admin", "password": "secret"},
|
|
|
)
|
|
|
+ assert mock_login_success.headers == {}
|
|
|
|
|
|
|
|
|
-def test_get_settings_injects_basic_auth_when_lwe_credentials_are_set(
|
|
|
+def test_get_settings_sets_basic_authorization_when_http_credentials_are_set(
|
|
|
isolated_env: None,
|
|
|
monkeypatch: pytest.MonkeyPatch,
|
|
|
mock_login_success: MagicMock,
|
|
|
) -> None:
|
|
|
- monkeypatch.setenv("BASE_URL", "api.example.com")
|
|
|
+ monkeypatch.setenv("BASE_URL", "https://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")
|
|
|
|
|
|
+ expected_token = base64.b64encode(b"basic_user:basic_pass").decode("utf-8")
|
|
|
+ expected_authorization = f"Basic {expected_token}"
|
|
|
+
|
|
|
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"
|
|
|
+ assert settings == Settings(
|
|
|
+ base_url="https://api.example.com",
|
|
|
+ phpsessid="sess_xyz789",
|
|
|
+ authorization=expected_authorization,
|
|
|
+ )
|
|
|
+ assert mock_login_success.headers == {"Authorization": expected_authorization}
|
|
|
mock_login_success.post.assert_called_once_with(
|
|
|
- "https://basic_user:basic_pass@api.example.com/rest/user/login",
|
|
|
- json={"id": "admin", "password": "secret"},
|
|
|
+ "https://api.example.com/rest/user/login",
|
|
|
+ data={"id": "admin", "password": "secret"},
|
|
|
)
|
|
|
|
|
|
|
|
|
@@ -107,8 +119,8 @@ def test_get_settings_raises_when_env_file_is_missing(
|
|
|
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
|
|
|
+ assert "LWE_USERNAME" in message
|
|
|
+ assert "LWE_PASSWORD" in message
|
|
|
|
|
|
|
|
|
def test_get_settings_raises_when_login_does_not_return_phpsessid(
|
|
|
@@ -122,6 +134,7 @@ def test_get_settings_raises_when_login_does_not_return_phpsessid(
|
|
|
mock_response = MagicMock()
|
|
|
mock_response.cookies.get.return_value = None
|
|
|
mock_client = MagicMock()
|
|
|
+ mock_client.headers = {}
|
|
|
mock_client.post.return_value = mock_response
|
|
|
mock_client.__enter__.return_value = mock_client
|
|
|
mock_client.__exit__.return_value = False
|