Skip to content

Commit

Permalink
tests: api: circulation: Add tests for loan request minimum days
Browse files Browse the repository at this point in the history
  • Loading branch information
sakshamarora1 authored and kpsherva committed Feb 19, 2024
1 parent 5cdca97 commit 70c314a
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 6 deletions.
4 changes: 2 additions & 2 deletions invenio_app_ils/circulation/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@
ILS_CIRCULATION_NOTIFICATION_OVERDUE_REMINDER_INTERVAL = 3
#: The maximum duration of a loan request
ILS_CIRCULATION_LOAN_REQUEST_DURATION_DAYS = 60
#: Loan Request Offset Value
ILS_CIRCULATION_LOAD_REQUEST_OFFSET = 0
#: The minimum number of days after which a loan can start from the date of request
ILS_CIRCULATION_LOAN_REQUEST_OFFSET = 0
#: Period of time in days, before loans expire, for notifications etc.
ILS_CIRCULATION_LOAN_WILL_EXPIRE_DAYS = 7
#: Optional delivery methods when requesting a new loan. Set to empty object to
Expand Down
22 changes: 18 additions & 4 deletions invenio_app_ils/circulation/loaders/schemas/json/loan_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class LoanRequestSchemaV1(LoanBaseSchemaV1):

@validates_schema()
def validates_schema(self, data, **kwargs):
"""Validate schema delivery field."""
"""Validate schema fields."""
delivery = data.get("delivery")
# if delivery methods is configured, it has to be a mandatory field
if (
Expand All @@ -77,13 +77,24 @@ def validates_schema(self, data, **kwargs):
):
raise ValidationError("Delivery is required.", "delivery")

if current_app.config["ILS_CIRCULATION_LOAN_REQUEST_OFFSET"] < 0:
raise ValidationError(
{
"ILS_CIRCULATION_LOAN_REQUEST_OFFSET": [
"The minimum days after which a loan can start cannot be negative."
]
}
)

@post_load()
def postload_checks(self, data, **kwargs):
"""Validate dates values."""
start = arrow.get(data["request_start_date"]).date()
end = arrow.get(data["request_expire_date"]).date()
duration_days = current_app.config["ILS_CIRCULATION_LOAN_REQUEST_DURATION_DAYS"]
loan_request_offset = timedelta(days=current_app.config["ILS_CIRCULATION_LOAD_REQUEST_OFFSET"])
loan_request_offset = timedelta(
days=current_app.config["ILS_CIRCULATION_LOAN_REQUEST_OFFSET"]
)
duration = timedelta(days=duration_days)

if end < start:
Expand All @@ -108,11 +119,14 @@ def postload_checks(self, data, **kwargs):
}
)
elif end - start < loan_request_offset:
message = "The request end date can only be {} days after the request start date.".format(loan_request_offset.days)
message = (
"The requested start date must be at least {} days from today.".format(
loan_request_offset.days
)
)
raise ValidationError(
{
"request_start_date": [message],
"request_expire_date": [message],
}
)
return data
70 changes: 70 additions & 0 deletions tests/api/circulation/test_loan_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,3 +226,73 @@ def test_request_loan_with_active_loan_or_loan_request(
params["transaction_user_pid"] = str(user.id)
res = client.post(url, headers=json_headers, data=json.dumps(params))
assert res.status_code == 400


def test_request_loan_minimum_days(app, client, json_headers, users, testdata):
"""Test that a patron can only request a loan after a set minimum days"""
url = url_for("invenio_app_ils_circulation.loan_request")
user = user_login(client, "patron1", users)
app.config["ILS_CIRCULATION_LOAN_REQUEST_OFFSET"] = 4
params = deepcopy(NEW_LOAN)

now = arrow.utcnow()
start_date = now.date().isoformat()
params["request_start_date"] = start_date
params["transaction_user_pid"] = str(user.id)

params["document_pid"] = "docid-4"
end_date = (now + timedelta(days=2)).date().isoformat() # FAIL
params["request_expire_date"] = end_date
res = client.post(url, headers=json_headers, data=json.dumps(params))
assert res.status_code == 400
assert res.get_json()["message"] == "Validation error."

params["document_pid"] = "docid-5"
end_date = (now - timedelta(days=2)).date().isoformat() # FAIL
params["request_expire_date"] = end_date
res = client.post(url, headers=json_headers, data=json.dumps(params))
assert res.status_code == 400
assert res.get_json()["message"] == "Validation error."

params["document_pid"] = "docid-6"
end_date = now.date().isoformat() # FAIL
params["request_expire_date"] = end_date
res = client.post(url, headers=json_headers, data=json.dumps(params))
assert res.status_code == 400
assert res.get_json()["message"] == "Validation error."

params["document_pid"] = "docid-7"
end_date = (now + timedelta(days=4)).date().isoformat() # PASS
params["request_expire_date"] = end_date
res = client.post(url, headers=json_headers, data=json.dumps(params))
assert res.status_code == 202
loan = res.get_json()["metadata"]
assert loan["state"] == "PENDING"
assert loan["document_pid"] == params["document_pid"]
assert loan["transaction_date"]

params["document_pid"] = "docid-8"
end_date = (now + timedelta(days=8)).date().isoformat() # PASS
params["request_expire_date"] = end_date
res = client.post(url, headers=json_headers, data=json.dumps(params))
assert res.status_code == 202
loan = res.get_json()["metadata"]
assert loan["state"] == "PENDING"
assert loan["document_pid"] == params["document_pid"]
assert loan["transaction_date"]

app.config["ILS_CIRCULATION_LOAN_REQUEST_OFFSET"] = -3

params["document_pid"] = "docid-9"
end_date = now.date().isoformat() # FAIL
params["request_expire_date"] = end_date
res = client.post(url, headers=json_headers, data=json.dumps(params))
assert res.status_code == 400
assert res.get_json()["message"] == "Validation error."

params["document_pid"] = "docid-10"
end_date = (now - timedelta(days=2)).date().isoformat() # FAIL
params["request_expire_date"] = end_date
res = client.post(url, headers=json_headers, data=json.dumps(params))
assert res.status_code == 400
assert res.get_json()["message"] == "Validation error."

0 comments on commit 70c314a

Please sign in to comment.