From 0af4ba71b80783a9bc604b00e4f57b2fdfec19e8 Mon Sep 17 00:00:00 2001 From: Dragon Dave McKee Date: Thu, 8 Feb 2024 17:11:42 +0000 Subject: [PATCH] Auto-unlock if save_locked_judgment_xml fails and unlock was requested --- src/openapi_server/apis/writing_api.py | 18 ++++++++++++------ tests/test_writing_api.py | 21 +++++++++++++++++++++ 2 files changed, 33 insertions(+), 6 deletions(-) diff --git a/src/openapi_server/apis/writing_api.py b/src/openapi_server/apis/writing_api.py index 4743f69..290b228 100644 --- a/src/openapi_server/apis/writing_api.py +++ b/src/openapi_server/apis/writing_api.py @@ -161,12 +161,18 @@ async def judgment_uri_patch( # noqa: PLR0913 message=annotation if annotation else None, ) - with error_handling(): - client.save_locked_judgment_xml( - judgment_uri=judgmentUri, - judgment_xml=bytes_body, - annotation=rich_annotation, - ) + try: + with error_handling(): + client.save_locked_judgment_xml( + judgment_uri=judgmentUri, + judgment_xml=bytes_body, + annotation=rich_annotation, + ) + except Exception: + if unlock: + with error_handling(): + _ml_response = client.checkin_judgment(judgment_uri=judgmentUri) + raise if not unlock: response.status_code = 200 diff --git a/tests/test_writing_api.py b/tests/test_writing_api.py index 9ff1bbb..5d32d7b 100644 --- a/tests/test_writing_api.py +++ b/tests/test_writing_api.py @@ -293,3 +293,24 @@ def test_validation_error_message_in_api_response(mocked_client): assert response.status_code == 422 assert response.json()["detail"] == "a message" + + +@patch("openapi_server.apis.writing_api.client_for_basic_auth") +def test_unlock_on_fail(mocked_client): + # More generally testing errors are passed through from the client + mocked_client.return_value.save_locked_judgment_xml.side_effect = Mock( + side_effect=MarklogicResourceLockedError(), + ) + response = TestClient(app).request( + "PATCH", + "/judgment/uri/path", + auth=("user", "pass"), + data="", + params={"unlock": "1"}, + ) + + mocked_client.return_value.save_locked_judgment_xml.assert_called() + mocked_client.return_value.checkin_judgment.assert_called_with( + judgment_uri="uri/path", + ) + assert response.status_code == 409