Skip to content

Commit

Permalink
Auto-unlock if save_locked_judgment_xml fails and unlock was requested
Browse files Browse the repository at this point in the history
  • Loading branch information
dragon-dxw committed Apr 10, 2024
1 parent b66686e commit 2b69ebb
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 6 deletions.
18 changes: 12 additions & 6 deletions src/openapi_server/apis/writing_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 21 additions & 0 deletions tests/test_writing_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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="<judgment></judgment>",
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

0 comments on commit 2b69ebb

Please sign in to comment.