|
1 | 1 | """Tests for the platform service module.""" |
2 | 2 |
|
3 | | -from unittest.mock import MagicMock |
| 3 | +from http import HTTPStatus |
| 4 | +from unittest.mock import MagicMock, patch |
4 | 5 |
|
5 | 6 | import pytest |
6 | 7 |
|
7 | 8 | from aignostics.platform._service import Service, UserInfo |
| 9 | +from aignostics.utils import Health |
| 10 | + |
| 11 | +_PATCH_AUTH_GETTER = "aignostics.platform._service.get_token" |
8 | 12 |
|
9 | 13 |
|
10 | 14 | @pytest.mark.unit |
@@ -42,6 +46,117 @@ def test_http_pool_singleton() -> None: |
42 | 46 | assert pool_from_service1 is pool_from_service2, "Service instances should share the same HTTP pool" |
43 | 47 |
|
44 | 48 |
|
| 49 | +@pytest.mark.unit |
| 50 | +def test_determine_api_authenticated_health_success() -> None: |
| 51 | + """Health.UP returned when the dedicated pool responds 200 with auth token.""" |
| 52 | + mock_response = MagicMock() |
| 53 | + mock_response.status = HTTPStatus.OK |
| 54 | + |
| 55 | + mock_pool = MagicMock() |
| 56 | + mock_pool.request.return_value = mock_response |
| 57 | + |
| 58 | + with ( |
| 59 | + patch.object(Service, "_get_http_pool", return_value=mock_pool), |
| 60 | + patch(_PATCH_AUTH_GETTER, return_value="test-token"), |
| 61 | + ): |
| 62 | + result = Service()._determine_api_authenticated_health() |
| 63 | + |
| 64 | + assert result.status == Health.Code.UP |
| 65 | + |
| 66 | + |
| 67 | +@pytest.mark.unit |
| 68 | +def test_determine_api_authenticated_health_non_200() -> None: |
| 69 | + """Health.DOWN returned when the dedicated pool responds with non-200.""" |
| 70 | + mock_response = MagicMock() |
| 71 | + mock_response.status = HTTPStatus.SERVICE_UNAVAILABLE |
| 72 | + |
| 73 | + mock_pool = MagicMock() |
| 74 | + mock_pool.request.return_value = mock_response |
| 75 | + |
| 76 | + with ( |
| 77 | + patch.object(Service, "_get_http_pool", return_value=mock_pool), |
| 78 | + patch(_PATCH_AUTH_GETTER, return_value="test-token"), |
| 79 | + ): |
| 80 | + result = Service()._determine_api_authenticated_health() |
| 81 | + |
| 82 | + assert result.status == Health.Code.DOWN |
| 83 | + assert result.reason is not None |
| 84 | + |
| 85 | + |
| 86 | +@pytest.mark.unit |
| 87 | +def test_determine_api_authenticated_health_handles_exception() -> None: |
| 88 | + """Health.DOWN with reason when get_token raises.""" |
| 89 | + with patch(_PATCH_AUTH_GETTER, side_effect=RuntimeError("no auth")): |
| 90 | + result = Service()._determine_api_authenticated_health() |
| 91 | + |
| 92 | + assert result.status == Health.Code.DOWN |
| 93 | + assert result.reason is not None |
| 94 | + |
| 95 | + |
| 96 | +@pytest.mark.unit |
| 97 | +def test_determine_api_public_health_success() -> None: |
| 98 | + """Health.UP returned when the public pool responds 200.""" |
| 99 | + mock_response = MagicMock() |
| 100 | + mock_response.status = HTTPStatus.OK |
| 101 | + |
| 102 | + mock_pool = MagicMock() |
| 103 | + mock_pool.request.return_value = mock_response |
| 104 | + |
| 105 | + with patch.object(Service, "_get_http_pool", return_value=mock_pool): |
| 106 | + result = Service()._determine_api_public_health() |
| 107 | + |
| 108 | + assert result.status == Health.Code.UP |
| 109 | + |
| 110 | + |
| 111 | +@pytest.mark.unit |
| 112 | +def test_determine_api_public_health_non_200() -> None: |
| 113 | + """Health.DOWN returned when the public pool responds with non-200.""" |
| 114 | + mock_response = MagicMock() |
| 115 | + mock_response.status = HTTPStatus.SERVICE_UNAVAILABLE |
| 116 | + |
| 117 | + mock_pool = MagicMock() |
| 118 | + mock_pool.request.return_value = mock_response |
| 119 | + |
| 120 | + with patch.object(Service, "_get_http_pool", return_value=mock_pool): |
| 121 | + result = Service()._determine_api_public_health() |
| 122 | + |
| 123 | + assert result.status == Health.Code.DOWN |
| 124 | + assert result.reason is not None |
| 125 | + |
| 126 | + |
| 127 | +@pytest.mark.unit |
| 128 | +def test_determine_api_public_health_handles_exception() -> None: |
| 129 | + """Health.DOWN returned when the public pool raises.""" |
| 130 | + mock_pool = MagicMock() |
| 131 | + mock_pool.request.side_effect = ConnectionError("unreachable") |
| 132 | + |
| 133 | + with patch.object(Service, "_get_http_pool", return_value=mock_pool): |
| 134 | + result = Service()._determine_api_public_health() |
| 135 | + |
| 136 | + assert result.status == Health.Code.DOWN |
| 137 | + assert result.reason is not None |
| 138 | + |
| 139 | + |
| 140 | +@pytest.mark.unit |
| 141 | +def test_health_returns_both_components() -> None: |
| 142 | + """health() aggregates api_public and api_authenticated component keys.""" |
| 143 | + public_health = Health(status=Health.Code.UP) |
| 144 | + auth_health = Health(status=Health.Code.UP) |
| 145 | + |
| 146 | + service = Service() |
| 147 | + with ( |
| 148 | + patch.object(service, "_determine_api_public_health", return_value=public_health), |
| 149 | + patch.object(service, "_determine_api_authenticated_health", return_value=auth_health), |
| 150 | + ): |
| 151 | + result = service.health() |
| 152 | + |
| 153 | + assert result.components is not None |
| 154 | + assert "api_public" in result.components |
| 155 | + assert "api_authenticated" in result.components |
| 156 | + assert result.components["api_public"] is public_health |
| 157 | + assert result.components["api_authenticated"] is auth_health |
| 158 | + |
| 159 | + |
45 | 160 | @pytest.mark.unit |
46 | 161 | @pytest.mark.parametrize( |
47 | 162 | ("organization_name", "is_internal"), |
|
0 commit comments