Skip to content

Commit

Permalink
circulation: fix can_extend check method
Browse files Browse the repository at this point in the history
Sometimes, no loan parameter could be send to `can_extend` method. In
this case, the function skip all checks and return True. Now this
function try to load the loan based on others function arguments to be
more relevant.

Co-Authored-by: Renaud Michotte <[email protected]>
  • Loading branch information
zannkukai committed Jun 24, 2021
1 parent 0564b0b commit 017acd3
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 9 deletions.
8 changes: 5 additions & 3 deletions rero_ils/modules/loans/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,11 @@ def __init__(self, data, model=None):
def can_extend(cls, item, **kwargs):
"""Loan can extend."""
from rero_ils.modules.loans.utils import extend_loan_data_is_valid
if 'loan' not in kwargs: # this method is not relevant
return True, []
loan = kwargs['loan']
loan = kwargs.get('loan')
if loan is None: # try to load the loan from kwargs
loan, _unused_data = item.prior_extend_loan_actions(**kwargs)
if loan is None: # not relevant method :: return True
return True, []
if loan.get('state') != LoanState.ITEM_ON_LOAN:
return False, [_('The loan cannot be extended')]
patron = Patron.get_record_by_pid(loan.get('patron_pid'))
Expand Down
6 changes: 6 additions & 0 deletions tests/api/circulation/scenarios/test_scenario_c.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from utils import get_json, postdata

from rero_ils.modules.items.models import ItemStatus
from rero_ils.modules.loans.api import Loan


def test_circ_scenario_c(
Expand Down Expand Up @@ -113,6 +114,11 @@ def test_circ_scenario_c(
client, 'api_item.checkout', dict(circ_params))
assert res.status_code == 200

# Update loan end_date to allow direct renewal
loan = Loan.get_record_by_pid(data['action_applied']['checkout']['pid'])
loan['end_date'] = loan['start_date']
loan.update(loan, dbcommit=True, reindex=True)

res, data = postdata(
client, 'api_item.extend_loan', dict(circ_params))
assert res.status_code == 200
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ def test_extend_loan(
"""Test frontend extend action."""
login_user_via_session(client, librarian_martigny.user)
item, patron, loan = item_on_loan_martigny_patron_and_loan_on_loan
# Update loan `end_date` to play with "extend" function without problem
loan['end_date'] = loan['start_date']
loan.update(loan, dbcommit=True, reindex=True)

assert item.status == ItemStatus.ON_LOAN

# Test extend for a blocked patron
Expand Down
5 changes: 4 additions & 1 deletion tests/api/items/test_items_rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -641,12 +641,15 @@ def test_items_extend_end_date(client, librarian_martigny,
loan = Loan.get_record_by_pid(loan_pid)
assert not item.get_extension_count()

max_count = get_extension_params(loan=loan, parameter_name='max_count')
renewal_duration_policy = circ_policy_short_martigny['renewal_duration']
renewal_duration = get_extension_params(
loan=loan, parameter_name='duration_default')
assert renewal_duration_policy <= renewal_duration.days

# Update loan end_date to allow direct renewal
loan['end_date'] = loan['start_date']
loan.update(loan, dbcommit=True, reindex=True)

# extend loan
res, data = postdata(
client,
Expand Down
8 changes: 8 additions & 0 deletions tests/api/selfcheck/test_selfcheck.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
from invenio_circulation.search.api import LoansSearch
from utils import flush_index, postdata

from rero_ils.modules.items.api import Item
from rero_ils.modules.loans.api import Loan, LoanAction, LoanState
from rero_ils.modules.notifications.api import Notification, \
NotificationsSearch, number_of_reminders_sent
Expand Down Expand Up @@ -278,6 +279,13 @@ def test_selfcheck_circulation(client, selfcheck_librarian_martigny, document,
assert checkout.is_success
assert checkout.due_date

# Get the loan and update end_date to allow direct renewal
loan_pid = Item.get_loan_pid_with_item_on_loan(item_lib_martigny.pid)
loan = Loan.get_record_by_pid(loan_pid)
assert 'selfcheck_terminal_id' in loan
loan['end_date'] = loan['start_date']
loan.update(loan, dbcommit=True, reindex=True)

# selfcheck renew
renew = selfcheck_renew(
transaction_user_pid=librarian_martigny.pid,
Expand Down
13 changes: 8 additions & 5 deletions tests/ui/circulation/test_actions_extend.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@


import pytest
from invenio_circulation.errors import NoValidTransitionAvailableError
from invenio_circulation.errors import CirculationException
from utils import item_record_to_a_specific_loan_state

from rero_ils.modules.errors import NoCirculationAction
Expand Down Expand Up @@ -60,13 +60,12 @@ def test_extend_on_item_at_desk(
with pytest.raises(NoCirculationAction):
item, actions = item.extend_loan(**params)
assert item.status == ItemStatus.AT_DESK
# test fails if a loan pid is given
params = {
'pid': loan.pid,
'transaction_location_pid': loc_public_martigny.pid,
'transaction_user_pid': librarian_martigny.pid
}
with pytest.raises(NoValidTransitionAvailableError):
with pytest.raises(CirculationException):
item, actions = item.extend_loan(**params)
assert item.status == ItemStatus.AT_DESK

Expand All @@ -80,6 +79,10 @@ def test_extend_on_item_on_loan_with_no_requests(
# for an on_loan item with no requests, the extend action is possible.
item, patron, loan = item_on_loan_martigny_patron_and_loan_on_loan

# Update loan `end_date` to play with "extend" function without problem
loan['end_date'] = loan['start_date']
loan.update(loan, dbcommit=True, reindex=True)

params = {
'transaction_location_pid': loc_public_martigny.pid,
'transaction_user_pid': librarian_martigny.pid
Expand Down Expand Up @@ -147,7 +150,7 @@ def test_extend_on_item_in_transit_for_pickup(
'transaction_location_pid': loc_public_martigny.pid,
'transaction_user_pid': librarian_martigny.pid
}
with pytest.raises(NoValidTransitionAvailableError):
with pytest.raises(CirculationException):
item, actions = item.extend_loan(**params)
assert item.status == ItemStatus.IN_TRANSIT

Expand All @@ -174,6 +177,6 @@ def test_extend_on_item_in_transit_to_house(
'transaction_location_pid': loc_public_martigny.pid,
'transaction_user_pid': librarian_martigny.pid
}
with pytest.raises(NoValidTransitionAvailableError):
with pytest.raises(CirculationException):
item, actions = item.extend_loan(**params)
assert item.status == ItemStatus.IN_TRANSIT

0 comments on commit 017acd3

Please sign in to comment.