From 7d335515b06053736b1cd30330456095ada27131 Mon Sep 17 00:00:00 2001 From: Jonathan Green Date: Tue, 11 Jun 2024 13:17:00 -0300 Subject: [PATCH] Fixes for loans tests --- tests/manager/api/controller/test_loan.py | 53 ++++++++++++++++++++++- tests/mocks/circulation.py | 7 +++ 2 files changed, 58 insertions(+), 2 deletions(-) diff --git a/tests/manager/api/controller/test_loan.py b/tests/manager/api/controller/test_loan.py index 20f6cb771e..c7409dbf0d 100644 --- a/tests/manager/api/controller/test_loan.py +++ b/tests/manager/api/controller/test_loan.py @@ -1,7 +1,7 @@ import datetime from collections.abc import Generator from decimal import Decimal -from unittest.mock import MagicMock, patch +from unittest.mock import MagicMock, create_autospec, patch from urllib.parse import quote import feedparser @@ -68,6 +68,7 @@ from tests.fixtures.library import LibraryFixture from tests.fixtures.redis import RedisFixture from tests.fixtures.services import ServicesFixture +from tests.mocks.circulation import MockPatronActivityCirculationAPI from tests.mocks.mock import DummyHTTPClient @@ -103,6 +104,12 @@ def __init__( self.identifier_identifier = self.identifier.identifier self.identifier_type = self.identifier.type + # Make sure our collection has a PatronActivityCirculationAPI setup, so we can test the + # patron activity sync tasks. + self.manager.d_circulation.add_remote_api( + self.pool, MockPatronActivityCirculationAPI(db.session, self.collection) + ) + @pytest.fixture(scope="function") def loan_fixture( @@ -1107,7 +1114,7 @@ def test_revoke_loan( {"Authorization": loan_fixture.valid_auth} ) - # Create a new loan. + # Create a loan and revoke it with loan_fixture.request_context_with_library("/", headers=headers): patron = loan_fixture.manager.loans.authenticated_patron_from_request() assert isinstance(patron, Patron) @@ -1134,6 +1141,48 @@ def test_revoke_loan( countdown=5, ) + @pytest.mark.parametrize( + *OPDSSerializationTestHelper.PARAMETRIZED_SINGLE_ENTRY_ACCEPT_HEADERS + ) + def test_revoke_loan_no_patron_activity_support( + self, + loan_fixture: LoanFixture, + accept_header: str | None, + expected_content_type: str, + ): + serialization_helper = OPDSSerializationTestHelper( + accept_header, expected_content_type + ) + headers = serialization_helper.merge_accept_header( + {"Authorization": loan_fixture.valid_auth} + ) + + # Create a loan and revoke it + with loan_fixture.request_context_with_library("/", headers=headers): + patron = loan_fixture.manager.loans.authenticated_patron_from_request() + assert isinstance(patron, Patron) + loan, newly_created = loan_fixture.pool.loan_to(patron) + + mock_supports_patron_activity = create_autospec( + loan_fixture.manager.d_circulation.supports_patron_activity, + return_value=False, + ) + loan_fixture.manager.d_circulation.supports_patron_activity = ( + mock_supports_patron_activity + ) + loan_fixture.manager.d_circulation.queue_checkin(loan_fixture.pool) + + with patch( + "palace.manager.api.controller.loan.sync_patron_activity" + ) as sync_task: + response = loan_fixture.manager.loans.revoke(loan_fixture.pool_id) + + assert 200 == response.status_code + serialization_helper.verify_and_get_single_entry_feed_links(response) + + mock_supports_patron_activity.assert_called_once_with(loan_fixture.pool) + sync_task.apply_async.assert_not_called() + def test_revoke_loan_exception( self, loan_fixture: LoanFixture, diff --git a/tests/mocks/circulation.py b/tests/mocks/circulation.py index 98a6e13d3f..e2e0657f14 100644 --- a/tests/mocks/circulation.py +++ b/tests/mocks/circulation.py @@ -190,6 +190,13 @@ def api_for_license_pool(self, licensepool: LicensePool) -> MockBaseCirculationA self.remotes[source] = remote return self.remotes[source] + def add_remote_api( + self, licensepool: LicensePool, api: MockBaseCirculationAPI + ) -> None: + source = licensepool.data_source.name + assert source is not None + self.remotes[source] = api + class MockPatronActivityCirculationAPI( MockBaseCirculationAPI, PatronActivityCirculationAPI