Skip to content

Commit

Permalink
Fix error in license accounting when license already checked out. (#2069
Browse files Browse the repository at this point in the history
)
  • Loading branch information
jonathangreen committed Sep 18, 2024
1 parent 90a6f4a commit 998f0f9
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
5 changes: 5 additions & 0 deletions src/palace/manager/api/odl/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,11 @@ def _checkout(
json_response.get("type")
== "http://opds-spec.org/odl/error/checkout/unavailable"
):
# TODO: This would be a good place to do an async availability update, since we know
# the book is unavailable, when we thought it was available. For now, we know that
# the license has no checkouts_available, so we do that update.
license.checkouts_available = 0
licensepool.update_availability_from_licenses()
raise NoAvailableCopies()
raise

Expand Down
22 changes: 20 additions & 2 deletions tests/manager/api/odl/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -738,7 +738,7 @@ def test_checkout_no_available_copies_unknown_to_us(
)

# We think there are copies available.
opds2_with_odl_api_fixture.setup_license(concurrency=1, available=1)
license = opds2_with_odl_api_fixture.setup_license(concurrency=1, available=1)

# But the distributor says there are no available copies.
opds2_with_odl_api_fixture.mock_http.queue_response(
Expand All @@ -751,11 +751,29 @@ def test_checkout_no_available_copies_unknown_to_us(
checkout()

assert db.session.query(Loan).count() == 0
assert license.license_pool.licenses_available == 0
assert license.checkouts_available == 0

def test_checkout_failures(
self,
db: DatabaseTransactionFixture,
opds2_with_odl_api_fixture: OPDS2WithODLApiFixture,
) -> None:
checkout = partial(
opds2_with_odl_api_fixture.api.checkout,
opds2_with_odl_api_fixture.patron,
"pin",
opds2_with_odl_api_fixture.pool,
MagicMock(),
)

# We think there are copies available.
opds2_with_odl_api_fixture.setup_license(concurrency=1, available=1)

# Test the case where we get bad JSON back from the distributor.
opds2_with_odl_api_fixture.mock_http.queue_response(
400,
response_type,
"application/api-problem+json",
content="hot garbage",
)

Expand Down

0 comments on commit 998f0f9

Please sign in to comment.