|
| 1 | +"""Tests for the settings module.""" |
| 2 | + |
| 3 | +import os |
| 4 | +from pathlib import Path |
| 5 | +from typing import cast |
| 6 | +from unittest.mock import MagicMock, patch |
| 7 | + |
| 8 | +import pytest |
| 9 | +from pydantic import SecretStr |
| 10 | +from pydantic_settings import SettingsConfigDict |
| 11 | + |
| 12 | +from aignostics_foundry_core.settings import ( |
| 13 | + UNHIDE_SENSITIVE_INFO, |
| 14 | + OpaqueSettings, |
| 15 | + load_settings, |
| 16 | + strip_to_none_before_validator, |
| 17 | +) |
| 18 | + |
| 19 | +_SECRET_VALUE = "sensitive" # noqa: S105 |
| 20 | +_MASKED_VALUE = "**********" |
| 21 | + |
| 22 | + |
| 23 | +class _TheTestSettings(OpaqueSettings): |
| 24 | + """Test settings class.""" |
| 25 | + |
| 26 | + test_value: str = "default" |
| 27 | + secret_value: SecretStr | None = None |
| 28 | + required_value: str |
| 29 | + |
| 30 | + |
| 31 | +class _TheTestSettingsWithEnvPrefix(OpaqueSettings): |
| 32 | + """Test settings class with an environment prefix.""" |
| 33 | + |
| 34 | + model_config = SettingsConfigDict(env_prefix="TEST_") |
| 35 | + |
| 36 | + value: str |
| 37 | + |
| 38 | + |
| 39 | +def _make_info(context: dict | None) -> MagicMock: # type: ignore[type-arg] |
| 40 | + """Create a mock FieldSerializationInfo with the given context.""" |
| 41 | + info = MagicMock() |
| 42 | + info.context = context |
| 43 | + return info |
| 44 | + |
| 45 | + |
| 46 | +class TestStripToNoneBeforeValidator: |
| 47 | + """Tests for strip_to_none_before_validator.""" |
| 48 | + |
| 49 | + @pytest.mark.unit |
| 50 | + def test_none_returns_none(self) -> None: |
| 51 | + """Test that None is returned when None is passed.""" |
| 52 | + assert strip_to_none_before_validator(None) is None |
| 53 | + |
| 54 | + @pytest.mark.unit |
| 55 | + def test_empty_string_returns_none(self) -> None: |
| 56 | + """Test that None is returned when an empty string is passed.""" |
| 57 | + assert strip_to_none_before_validator("") is None |
| 58 | + |
| 59 | + @pytest.mark.unit |
| 60 | + def test_whitespace_returns_none(self) -> None: |
| 61 | + """Test that None is returned when a whitespace string is passed.""" |
| 62 | + assert strip_to_none_before_validator(" \t\n ") is None |
| 63 | + |
| 64 | + @pytest.mark.unit |
| 65 | + def test_valid_string_returns_stripped(self) -> None: |
| 66 | + """Test that a stripped string is returned when a valid string is passed.""" |
| 67 | + assert strip_to_none_before_validator(" test ") == "test" |
| 68 | + |
| 69 | + |
| 70 | +class TestOpaqueSettings: |
| 71 | + """Tests for OpaqueSettings static serializers.""" |
| 72 | + |
| 73 | + @pytest.mark.unit |
| 74 | + def test_serialize_sensitive_info_unhide_true(self) -> None: |
| 75 | + """Test that sensitive info is revealed when unhide_sensitive_info is True.""" |
| 76 | + secret = SecretStr(_SECRET_VALUE) |
| 77 | + result = OpaqueSettings.serialize_sensitive_info(secret, _make_info({UNHIDE_SENSITIVE_INFO: True})) |
| 78 | + assert result == _SECRET_VALUE |
| 79 | + |
| 80 | + @pytest.mark.unit |
| 81 | + def test_serialize_sensitive_info_unhide_false(self) -> None: |
| 82 | + """Test that sensitive info is hidden when unhide_sensitive_info is False.""" |
| 83 | + secret = SecretStr(_SECRET_VALUE) |
| 84 | + result = OpaqueSettings.serialize_sensitive_info(secret, _make_info({UNHIDE_SENSITIVE_INFO: False})) |
| 85 | + assert result == _MASKED_VALUE |
| 86 | + |
| 87 | + @pytest.mark.unit |
| 88 | + def test_serialize_sensitive_info_empty_secret(self) -> None: |
| 89 | + """Test that None is returned when the SecretStr is empty.""" |
| 90 | + result = OpaqueSettings.serialize_sensitive_info(SecretStr(""), _make_info({})) |
| 91 | + assert result is None |
| 92 | + |
| 93 | + @pytest.mark.unit |
| 94 | + def test_serialize_sensitive_info_none_input(self) -> None: |
| 95 | + """Test that None is returned when input_value is None.""" |
| 96 | + result = OpaqueSettings.serialize_sensitive_info(cast("SecretStr", None), _make_info({})) |
| 97 | + assert result is None |
| 98 | + |
| 99 | + @pytest.mark.unit |
| 100 | + def test_serialize_sensitive_info_no_context(self) -> None: |
| 101 | + """Test that sensitive info is hidden when no context is provided.""" |
| 102 | + secret = SecretStr(_SECRET_VALUE) |
| 103 | + result = OpaqueSettings.serialize_sensitive_info(secret, _make_info(None)) |
| 104 | + assert result == _MASKED_VALUE |
| 105 | + |
| 106 | + @pytest.mark.unit |
| 107 | + def test_serialize_path_resolve(self, tmp_path: Path) -> None: |
| 108 | + """Test that Path is resolved correctly.""" |
| 109 | + test_path = tmp_path / "test_file.txt" |
| 110 | + test_path.touch() |
| 111 | + result = OpaqueSettings.serialize_path_resolve(test_path, _make_info(None)) |
| 112 | + assert result == str(test_path.resolve()) |
| 113 | + |
| 114 | + @pytest.mark.unit |
| 115 | + def test_serialize_path_resolve_none(self) -> None: |
| 116 | + """Test that None is returned when Path is None.""" |
| 117 | + result = OpaqueSettings.serialize_path_resolve(cast("Path", None), _make_info(None)) |
| 118 | + assert result is None |
| 119 | + |
| 120 | + |
| 121 | +class TestLoadSettings: |
| 122 | + """Tests for load_settings.""" |
| 123 | + |
| 124 | + @pytest.mark.unit |
| 125 | + @patch.dict(os.environ, {"REQUIRED_VALUE": "test_value"}) |
| 126 | + def test_load_settings_success(self) -> None: |
| 127 | + """Test successful settings loading.""" |
| 128 | + settings = load_settings(_TheTestSettings) |
| 129 | + assert settings.test_value == "default" |
| 130 | + assert settings.required_value == "test_value" |
| 131 | + |
| 132 | + @pytest.mark.unit |
| 133 | + @patch.dict(os.environ, {"TEST_VALUE": "prefixed_value"}) |
| 134 | + def test_load_settings_with_env_prefix(self) -> None: |
| 135 | + """Test that settings with environment prefix work correctly.""" |
| 136 | + settings = load_settings(_TheTestSettingsWithEnvPrefix) |
| 137 | + assert settings.value == "prefixed_value" |
| 138 | + |
| 139 | + @pytest.mark.unit |
| 140 | + @patch("sys.exit") |
| 141 | + @patch("aignostics_foundry_core.settings.console.print") |
| 142 | + def test_load_settings_validation_error_exits(self, mock_console_print: MagicMock, mock_exit: MagicMock) -> None: |
| 143 | + """Test that validation error prints a Rich Panel and calls sys.exit(78).""" |
| 144 | + from rich.panel import Panel |
| 145 | + |
| 146 | + load_settings(_TheTestSettings) |
| 147 | + |
| 148 | + mock_exit.assert_called_once_with(78) |
| 149 | + assert mock_console_print.call_count == 1 |
| 150 | + panel_arg = mock_console_print.call_args[0][0] |
| 151 | + assert isinstance(panel_arg, Panel) |
0 commit comments