Skip to content

Commit 6843dcc

Browse files
yt-msMidnighter
authored andcommitted
test: check exception handling for locks
1 parent c8ebf6a commit 6843dcc

File tree

2 files changed

+72
-1
lines changed

2 files changed

+72
-1
lines changed

src/structurizr/api/structurizr_client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,8 +230,8 @@ def lock_workspace(self) -> bool:
230230
request = self._client.build_request("PUT", self._lock_url, params=self._params)
231231
request.headers.update(self._add_headers(request))
232232
response = self._client.send(request)
233-
logger.debug("%r", response.json())
234233
response.raise_for_status()
234+
logger.debug("%r", response.json())
235235
response = APIResponse.parse_raw(response.text)
236236
if not response.success:
237237
logger.error(

tests/unit/api/test_structurizr_client.py

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
from pathlib import Path
2323
from typing import List
2424

25+
import httpx
2526
import pytest
2627
from httpx import URL, Request, Response
2728
from pytest_mock import MockerFixture
@@ -236,3 +237,73 @@ def fake_send(request: Request):
236237
assert requests[0].url.path == "/workspace/19/lock"
237238
assert requests[1].method == "DELETE"
238239
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

Comments
 (0)