Esempio n. 1
0
 def _update_brwreq_with_new_status(pid, status):
     rec = BorrowingRequest.get_record_by_pid(pid)
     rec["status"] = status
     if status == "CANCELLED":
         rec["cancel_reason"] = "OTHER"
     rec.commit()
     db.session.commit()
def check_user_activity(app, user_pid, client, json_headers):
    """ Check if there are records related to the user. """
    # wait ES refresh
    current_search.flush_and_refresh(index="*")

    AnonymousPatron = app.config["ILS_PATRON_ANONYMOUS_CLASS"]
    anonymous_patron_fields = AnonymousPatron().dumps_loader()

    loans = get_loans_by_patron_pid(user_pid).scan()
    for hit in loans:
        # test ES
        assert hit["patron"] == anonymous_patron_fields
        # test DB
        loan = Loan.get_record_by_pid(hit.pid)
        assert loan["patron"] == anonymous_patron_fields
        # test REST
        url = url_for("invenio_records_rest.loanid_item", pid_value=hit.pid)
        res = client.get(url, headers=json_headers)
        assert res.get_json()["metadata"]["patron"] == anonymous_patron_fields

    borrowing_requests = (
        BorrowingRequestsSearch().search_by_patron_pid(user_pid).scan())
    for hit in borrowing_requests:
        # test ES
        assert hit["patron"] == anonymous_patron_fields
        # test DB
        borrowing_request = BorrowingRequest.get_record_by_pid(hit.pid)
        assert borrowing_request["patron"] == anonymous_patron_fields
        # test REST
        url = url_for("invenio_records_rest.illbid_item", pid_value=hit.pid)
        res = client.get(url, headers=json_headers)
        assert res.get_json()["metadata"]["patron"] == anonymous_patron_fields

    document_requests = (
        DocumentRequestSearch().search_by_patron_pid(user_pid).scan())
    for hit in document_requests:
        # test ES
        assert hit["patron"] == anonymous_patron_fields
        # test DB
        document_request = DocumentRequest.get_record_by_pid(hit.pid)
        assert document_request["patron"] == anonymous_patron_fields
        # test REST
        url = url_for("invenio_records_rest.dreqid_item", pid_value=hit.pid)
        res = client.get(url, headers=json_headers)
        assert res.get_json()["metadata"]["patron"] == anonymous_patron_fields

    acquisitions = OrderSearch().search_by_patron_pid(user_pid).scan()
    for hit in acquisitions:
        # test ES
        assert hit["patron"] == anonymous_patron_fields
        # test DB
        acquisition = Order.get_record_by_pid(hit.pid)
        assert acquisition["patron"] == anonymous_patron_fields
        # test REST
        url = url_for("invenio_records_rest.acqoid_item", pid_value=hit.pid)
        res = client.get(url, headers=json_headers)
        assert res.get_json()["metadata"]["patron"] == anonymous_patron_fields
def test_brw_req_refs(app, testdata):
    """Test creation of a borrowing request."""
    brw_req = BorrowingRequest.create(
        dict(
            pid="illlid-99",
            document_pid="docid-1",
            patron_pid="1",
            provider_pid="ill-provid-1",
            status="PENDING",
            type="PHYSICAL_COPY",
        ))
    assert "$schema" in brw_req
    assert "provider" in brw_req and "$ref" in brw_req["provider"]
    assert "document" in brw_req and "$ref" in brw_req["document"]
    assert "patron" in brw_req and "$ref" in brw_req["patron"]

    brw_req = BorrowingRequest.get_record_by_pid("illbid-1")
    brw_req = brw_req.replace_refs()
    assert "provider" in brw_req and brw_req["provider"]["name"]
    assert "document" in brw_req and brw_req["document"]["title"]
    assert "patron" in brw_req and brw_req["patron"]["name"]
def test_ill_brw_validation(db, testdata):
    """Test validation when updating a borrowing request."""
    borrowing_request_pid = testdata["ill_brw_reqs"][0]["pid"]

    # change document pid
    borrowing_request = BorrowingRequest.get_record_by_pid(
        borrowing_request_pid)
    borrowing_request["document_pid"] = "not_found_doc"
    with pytest.raises(DocumentNotFoundError):
        borrowing_request.commit()

    # change provider pid
    borrowing_request = BorrowingRequest.get_record_by_pid(
        borrowing_request_pid)
    borrowing_request["provider_pid"] = "not_found_lib"
    with pytest.raises(ProviderNotFoundError):
        borrowing_request.commit()

    # change patron pid
    borrowing_request = BorrowingRequest.get_record_by_pid(
        borrowing_request_pid)
    borrowing_request["patron_pid"] = "9999"
    with pytest.raises(PatronNotFoundError):
        borrowing_request.commit()

    # add cancel reason without status CANCELLED
    borrowing_request = BorrowingRequest.get_record_by_pid(
        borrowing_request_pid)
    borrowing_request["cancel_reason"] = "USER_CANCEL"
    with pytest.raises(ILLError):
        borrowing_request.commit()

    # update status to CANCELLED without cancel_reason
    borrowing_request = BorrowingRequest.get_record_by_pid(
        borrowing_request_pid)
    borrowing_request["status"] = "CANCELLED"
    with pytest.raises(ILLError):
        borrowing_request.commit()
def test_brw_reqs_create_loan_fails_on_loan_pid_already_attached(
    db, client, testdata, json_headers, users
):
    """Test borrowing requests create loan action fails on loan_pid already."""
    login_user_via_session(client, user=users["librarian"])

    # demo data "illbid-2" has the valid state `REQUESTED`
    pid = "illbid-2"

    rec = BorrowingRequest.get_record_by_pid(pid)
    rec["loan_pid"] = "loanid-3"
    rec.commit()
    db.session.commit()

    # already with a loan pid for some reasons
    future = arrow.utcnow() + timedelta(days=5)
    data = dict(loan_end_date=future.date().isoformat())
    _assert_create_loan_action_fails(pid, data, client, json_headers)
def test_brwreq_create_loan_fails_on_loan_pid_already_attached(
        db, client, testdata, json_headers, users):
    """Test borrowing requests create loan action fails on loan_pid already."""
    user_login(client, "librarian", users)

    # demo data "illbid-2" has the valid state `REQUESTED`
    pid = "illbid-2"

    rec = BorrowingRequest.get_record_by_pid(pid)
    rec.setdefault("patron_loan", {})
    rec["patron_loan"]["pid"] = "loanid-3"
    rec.commit()
    db.session.commit()

    # already with a loan pid for some reasons
    now = arrow.utcnow()
    future = now + timedelta(days=5)
    data = dict(
        loan_start_date=now.date().isoformat(),
        loan_end_date=future.date().isoformat(),
    )
    _assert_create_loan_action_fails(pid, data, client, json_headers)
 def _update_brwreq_with_new_status(pid, status):
     rec = BorrowingRequest.get_record_by_pid(pid)
     rec["status"] = status
     rec.commit()
     db.session.commit()