|
1 | 1 | import json |
2 | 2 | import os |
| 3 | +from contextlib import nullcontext as does_not_raise |
3 | 4 | from unittest import mock |
4 | 5 | from unittest.mock import Mock |
5 | 6 |
|
@@ -165,3 +166,42 @@ async def test_on_apply_plugin_service_returns_invalid_spec( |
165 | 166 | with mock.patch("requests.post", return_value=mock_response): |
166 | 167 | with pytest.raises(ServerClientError): |
167 | 168 | policy.on_apply(user.name, project=project.name, spec=spec) |
| 169 | + |
| 170 | + @pytest.mark.asyncio |
| 171 | + @mock.patch.dict(os.environ, {PLUGIN_SERVICE_URI_ENV_VAR_NAME: "http://mock"}) |
| 172 | + @pytest.mark.parametrize("test_db", ["sqlite", "postgres"], indirect=True) |
| 173 | + @pytest.mark.parametrize( |
| 174 | + "spec", ["run_spec", "fleet_spec", "volume_spec", "gateway_spec"], indirect=True |
| 175 | + ) |
| 176 | + @pytest.mark.parametrize( |
| 177 | + ("error", "expectation"), |
| 178 | + [ |
| 179 | + pytest.param(None, does_not_raise(), id="error_none"), |
| 180 | + pytest.param( |
| 181 | + "", |
| 182 | + pytest.raises( |
| 183 | + ServerClientError, match="Plugin service returned an invalid response" |
| 184 | + ), |
| 185 | + id="error_empty_str", |
| 186 | + ), |
| 187 | + pytest.param( |
| 188 | + "validation failed", |
| 189 | + pytest.raises( |
| 190 | + ServerClientError, match="Apply request rejected: validation failed" |
| 191 | + ), |
| 192 | + id="error_non_empty_str", |
| 193 | + ), |
| 194 | + ], |
| 195 | + ) |
| 196 | + async def test_on_apply_plugin_service_error_handling( |
| 197 | + self, test_db, user, project, spec, error, expectation |
| 198 | + ): |
| 199 | + policy = CustomApplyPolicy() |
| 200 | + mock_response = Mock() |
| 201 | + response_dict = {"spec": spec.dict(), "error": error} |
| 202 | + mock_response.text = json.dumps(response_dict) |
| 203 | + mock_response.raise_for_status = Mock() |
| 204 | + with mock.patch("requests.post", return_value=mock_response): |
| 205 | + with expectation: |
| 206 | + result = policy.on_apply(user=user.name, project=project.name, spec=spec) |
| 207 | + assert result == type(spec)(**response_dict["spec"]) |
0 commit comments