Skip to content

Commit

Permalink
Fix API operations to be happy with new API client
Browse files Browse the repository at this point in the history
This now makes requests using the new structured `SearchParameters` and
`VersionAnnotation` classes.
  • Loading branch information
jacksonj04 committed Nov 9, 2023
1 parent 35b3b9c commit c9e0158
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 26 deletions.
10 changes: 6 additions & 4 deletions src/openapi_server/apis/reading_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
)
from openapi_server.models.extra_models import TokenModel # noqa: F401
from openapi_server.security_api import get_token_basic

from caselawclient.search_parameters import SearchParameters
from openapi_server.connect import client_for_basic_auth

from requests_toolbelt.multipart import decoder
Expand Down Expand Up @@ -95,9 +95,11 @@ async def list_unpublished_get_get(
return {"status": "Not allowed to see unpublished documents"}

response = client.advanced_search(
page=page,
show_unpublished=True,
only_unpublished=True,
SearchParameters(
page=page,
show_unpublished=True,
only_unpublished=True,
)
)

xml = decode_multipart_response(response)
Expand Down
19 changes: 11 additions & 8 deletions src/openapi_server/apis/writing_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from typing import Dict, List, Optional # noqa: F401

from caselawclient.models.documents import DocumentURIString

from caselawclient.client_helpers import VersionAnnotation, VersionType
from fastapi import ( # noqa: F401
APIRouter,
Body,
Expand Down Expand Up @@ -149,11 +149,11 @@ async def judgment_uri_patch(
request: Request,
response: Response,
judgmentUri: DocumentURIString,
annotation: str = "",
if_match: str = Header(
None, description="The last known version number of the document"
),
token_basic: TokenModel = Security(get_token_basic),
annotation: str = "",
unlock: bool = False,
) -> Dict[str, str]:
"""Write a complete new version of the document to the database,
Expand All @@ -162,14 +162,17 @@ async def judgment_uri_patch(
client = client_for_basic_auth(token_basic)
bytes_body = await request.body()

rich_annotation = VersionAnnotation(
version_type=VersionType.ENRICHMENT,
automated=True,
message=annotation if annotation else None,
)

with error_handling():
client.save_locked_judgment_xml(
# judgment_uri=judgmentUri,
# judgment_xml=body,
# annotation=annotation,
judgmentUri,
bytes_body,
annotation,
judgment_uri=judgmentUri,
judgment_xml=bytes_body,
annotation=rich_annotation,
)

if not unlock:
Expand Down
18 changes: 11 additions & 7 deletions tests/test_reading_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from unittest import TestCase

from caselawclient.Client import MarklogicResourceNotFoundError

from caselawclient.search_parameters import SearchParameters
from fastapi.testclient import TestClient
from openapi_server.main import app
from openapi_server.apis.reading_api import unpack_list
Expand Down Expand Up @@ -88,9 +88,11 @@ def test_get_list_unpublished(mocked_client):
"GET", "/list/unpublished", auth=("user", "pass")
)
mocked_client.return_value.advanced_search.assert_called_with(
page=1,
show_unpublished=True,
only_unpublished=True,
SearchParameters(
page=1,
show_unpublished=True,
only_unpublished=True,
)
)
mocked_client.return_value.user_can_view_unpublished_judgments.assert_called_with(
"user"
Expand Down Expand Up @@ -122,9 +124,11 @@ def test_get_list_unpublished_xml(mocked_client):
headers={"Content-Type": "application/xml"},
)
mocked_client.return_value.advanced_search.assert_called_with(
page=6,
show_unpublished=True,
only_unpublished=True,
SearchParameters(
page=6,
show_unpublished=True,
only_unpublished=True,
)
)
mocked_client.return_value.user_can_view_unpublished_judgments.assert_called_with(
"user"
Expand Down
45 changes: 38 additions & 7 deletions tests/test_writing_api.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
# coding: utf-8

from fastapi.testclient import TestClient
from openapi_server.main import app
from unittest.mock import patch, Mock
from unittest.mock import patch, Mock, ANY
from caselawclient.Client import (
MarklogicUnauthorizedError,
MarklogicResourceLockedError,
Expand Down Expand Up @@ -157,10 +156,18 @@ def test_update_judgment_annotation_ok(mocked_client):
"/judgment/is-a-judgment/uri",
auth=("user", "pass"),
data="<judgment></judgment>",
params={"annotation": "hey"},
params={"annotation": "Test annotation message"},
)
mocked_client.return_value.save_locked_judgment_xml.assert_called_with(
"is-a-judgment/uri", b"<judgment></judgment>", "hey"
judgment_uri="is-a-judgment/uri",
judgment_xml=b"<judgment></judgment>",
annotation=ANY,
)
assert (
mocked_client.return_value.save_locked_judgment_xml.mock_calls[0]
.kwargs["annotation"]
.message
== "Test annotation message"
)
assert response.status_code == 200
assert "not unlocked" in response.text
Expand All @@ -178,7 +185,15 @@ def test_update_judgment_unlock_ok(mocked_client):
params={"unlock": "1"},
)
mocked_client.return_value.save_locked_judgment_xml.assert_called_with(
"is-a-judgment/uri", b"<judgment></judgment>", ""
judgment_uri="is-a-judgment/uri",
judgment_xml=b"<judgment></judgment>",
annotation=ANY,
)
assert (
mocked_client.return_value.save_locked_judgment_xml.mock_calls[0]
.kwargs["annotation"]
.message
is None
)
assert response.status_code == 200
assert "Uploaded and unlocked" in response.text
Expand All @@ -204,7 +219,15 @@ def test_default_message_in_api_response(mocked_client):
data="<judgment></judgment>",
)
mocked_client.return_value.save_locked_judgment_xml.assert_called_with(
"is-a-judgment/uri", b"<judgment></judgment>", ""
judgment_uri="is-a-judgment/uri",
judgment_xml=b"<judgment></judgment>",
annotation=ANY,
)
assert (
mocked_client.return_value.save_locked_judgment_xml.mock_calls[0]
.kwargs["annotation"]
.message
is None
)
assert response.status_code == 409
assert (
Expand Down Expand Up @@ -234,7 +257,15 @@ def test_validation_error_message_in_api_response(mocked_client):
data="<judgment></judgment>",
)
mocked_client.return_value.save_locked_judgment_xml.assert_called_with(
"is-a-judgment/uri", b"<judgment></judgment>", ""
judgment_uri="is-a-judgment/uri",
judgment_xml=b"<judgment></judgment>",
annotation=ANY,
)
assert (
mocked_client.return_value.save_locked_judgment_xml.mock_calls[0]
.kwargs["annotation"]
.message
is None
)

assert response.status_code == 422
Expand Down

0 comments on commit c9e0158

Please sign in to comment.