From f34a4283ceebfc64a5bae7a30ea383697cdfc25c Mon Sep 17 00:00:00 2001 From: Jonathan Green Date: Wed, 12 Jun 2024 13:41:10 -0300 Subject: [PATCH] Add parameter to stop refresh of loans feed. (#1899) --- src/palace/manager/api/controller/loan.py | 11 ++++++++- tests/manager/api/controller/test_loan.py | 30 +++++++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/src/palace/manager/api/controller/loan.py b/src/palace/manager/api/controller/loan.py index ff14fca75c..c37cb62cd8 100644 --- a/src/palace/manager/api/controller/loan.py +++ b/src/palace/manager/api/controller/loan.py @@ -6,6 +6,7 @@ from flask import Response, redirect from flask_babel import lazy_gettext as _ from lxml import etree +from pydantic import parse_obj_as from werkzeug import Response as wkResponse from palace.manager.api.circulation_exceptions import ( @@ -49,6 +50,14 @@ def sync(self) -> Response: """ patron: Patron = flask.request.patron # type: ignore[attr-defined] + try: + # Parse the refresh query parameter as a boolean. + refresh = parse_obj_as(bool, flask.request.args.get("refresh", "true")) + except ValueError: + # If we can't parse the refresh query parameter, default to True. + self.log.exception(f"Could not parse refresh query parameter.") + refresh = True + # Save some time if we don't believe the patron's loans or holds have # changed since the last time the client requested this feed. response = self.handle_conditional_request(patron.last_loan_activity_sync) @@ -64,7 +73,7 @@ def sync(self) -> Response: # First synchronize our local list of loans and holds with all # third-party loan providers. - if patron.authorization_identifier: + if patron.authorization_identifier and refresh: header = self.authorization_header() credential = self.manager.auth.get_credential_from_header(header) try: diff --git a/tests/manager/api/controller/test_loan.py b/tests/manager/api/controller/test_loan.py index c5b9009070..8a593b13d4 100644 --- a/tests/manager/api/controller/test_loan.py +++ b/tests/manager/api/controller/test_loan.py @@ -1505,6 +1505,36 @@ def handle_conditional_request(last_modified=None): # patron.last_loan_activity_sync was updated. assert patron.last_loan_activity_sync > new_sync_time + @pytest.mark.parametrize( + "refresh,expected_sync_call_count", + [ + ["true", 1], + ["abc", 1], + ["t", 1], + ["1", 1], + [None, 1], + ["false", 0], + ["f", 0], + ["0", 0], + ], + ) + def test_loans_refresh( + self, + loan_fixture: LoanFixture, + refresh: str | None, + expected_sync_call_count: int, + ): + url = f"loans/?refresh={refresh}" if refresh is not None else "/" + with ( + loan_fixture.request_context_with_library( + url, headers=dict(Authorization=loan_fixture.valid_auth) + ), + patch.object(loan_fixture.manager.d_circulation, "sync_bookshelf") as sync, + ): + loan_fixture.manager.loans.authenticated_patron_from_request() + loan_fixture.manager.loans.sync() + assert sync.call_count == expected_sync_call_count + @pytest.mark.parametrize( "target_loan_duration, " "db_loan_duration, "