|
22 | 22 | from pathlib import Path |
23 | 23 | from typing import List |
24 | 24 |
|
| 25 | +import httpx |
25 | 26 | import pytest |
26 | 27 | from httpx import URL, Request, Response |
27 | 28 | from pytest_mock import MockerFixture |
@@ -236,3 +237,73 @@ def fake_send(request: Request): |
236 | 237 | assert requests[0].url.path == "/workspace/19/lock" |
237 | 238 | assert requests[1].method == "DELETE" |
238 | 239 | assert requests[1].url.path == "/workspace/19/lock" |
| 240 | + |
| 241 | + |
| 242 | +def test_failed_lock_raises_exception(client: StructurizrClient, mocker: MockerFixture): |
| 243 | + """Check failing to lock raises an exception. |
| 244 | +
|
| 245 | + Trying to lock a workspace which is already locked by someone else actually |
| 246 | + returns a 200 status, but with success as false in the message. |
| 247 | + """ |
| 248 | + |
| 249 | + def fake_send(request: Request): |
| 250 | + msg = '{"success": false, "message": "The workspace is already locked"}' |
| 251 | + return Response( |
| 252 | + 200, |
| 253 | + content=msg.encode("ascii"), |
| 254 | + request=request, |
| 255 | + ) |
| 256 | + |
| 257 | + mocker.patch.object(client._client, "send", new=fake_send) |
| 258 | + with pytest.raises(StructurizrClientException, match="Failed to lock"): |
| 259 | + with client.lock(): |
| 260 | + pass |
| 261 | + |
| 262 | + |
| 263 | +def test_failed_unlock_raises_exception( |
| 264 | + client: StructurizrClient, mocker: MockerFixture |
| 265 | +): |
| 266 | + """Check failing to unlock raises an exception. |
| 267 | +
|
| 268 | + Not quite sure how this could occur, but check the handling anyway. |
| 269 | + """ |
| 270 | + |
| 271 | + def fake_send(request: Request): |
| 272 | + if request.method == "PUT": |
| 273 | + return Response( |
| 274 | + 200, |
| 275 | + content='{"success": true, "message": "OK"}'.encode("ascii"), |
| 276 | + request=request, |
| 277 | + ) |
| 278 | + else: |
| 279 | + return Response( |
| 280 | + 200, |
| 281 | + content='{"success": false, "message": "Not OK"}'.encode("ascii"), |
| 282 | + request=request, |
| 283 | + ) |
| 284 | + |
| 285 | + mocker.patch.object(client._client, "send", new=fake_send) |
| 286 | + with pytest.raises(StructurizrClientException, match="Failed to unlock"): |
| 287 | + with client.lock(): |
| 288 | + pass |
| 289 | + |
| 290 | + |
| 291 | +def test_failed_lock_bad_http_code(client: StructurizrClient, mocker: MockerFixture): |
| 292 | + """Check getting a non-200 HTTP response raises an HTTPX exception. |
| 293 | +
|
| 294 | + Trying to lock a workspace which is already locked by someone else actually |
| 295 | + returns a 200 status, but with success as false in the message. |
| 296 | + """ |
| 297 | + |
| 298 | + def fake_send(request: Request): |
| 299 | + msg = "Server failure" |
| 300 | + return Response( |
| 301 | + 500, |
| 302 | + content=msg.encode("ascii"), |
| 303 | + request=request, |
| 304 | + ) |
| 305 | + |
| 306 | + mocker.patch.object(client._client, "send", new=fake_send) |
| 307 | + with pytest.raises(httpx.HTTPStatusError): |
| 308 | + with client.lock(): |
| 309 | + pass |
0 commit comments