Exemplo n.º 1
0
def test_api_automatic_loan_valid_action(app, db, json_headers, params):
    """Test API valid action on loan."""
    loan = Loan.create({})
    minted_loan = loan_pid_minter(loan.id, loan)
    loan = current_circulation.circulation.trigger(
        loan,
        **dict(params,
               trigger='request',
               pickup_location_pid='pickup_location_pid'))
    db.session.commit()
    assert loan['state'] == 'PENDING'

    app.config[
        'CIRCULATION_ITEM_LOCATION_RETRIEVER'] = lambda x: 'pickup_location_pid'

    loan_pid = loan_pid_fetcher(loan.id, loan)
    assert minted_loan.pid_value == loan_pid.pid_value

    with app.test_client() as client:
        url = url_for('invenio_circulation.loan_pid_actions',
                      pid_value=loan_pid.pid_value,
                      action='next')
        res = client.post(url, headers=json_headers, data=json.dumps(params))

        assert res.status_code == HTTP_CODES['accepted']
        loan_dict = json.loads(res.data.decode('utf-8'))
        assert loan_dict['metadata']['state'] == 'ITEM_AT_DESK'
Exemplo n.º 2
0
def test_api_loan_invalid_action(app, db, json_headers, params,
                                 mock_is_item_available):
    """Test API invalid action on loan."""
    loan = Loan.create({})
    minted_loan = loan_pid_minter(loan.id, loan)

    loan = current_circulation.circulation.trigger(
        loan,
        **dict(params,
               trigger='request',
               pickup_location_pid='pickup_location_pid'))
    db.session.commit()
    assert loan['state'] == 'PENDING'

    loan_pid = loan_pid_fetcher(loan.id, loan)
    assert minted_loan.pid_value == loan_pid.pid_value

    with app.test_client() as client:
        url = url_for('invenio_circulation.loan_pid_actions',
                      pid_value=loan_pid.pid_value,
                      action='checkout')
        res = client.post(url, headers=json_headers, data=json.dumps(params))
        assert res.status_code == HTTP_CODES['method_not_allowed']
        error_dict = json.loads(res.data.decode('utf-8'))
        assert 'message' in error_dict
Exemplo n.º 3
0
def test_api_get_loan(app, db, json_headers):
    """Test API GET call to fetch a loan by PID."""
    loan = Loan.create({})
    minted_loan = loan_pid_minter(loan.id, loan)
    db.session.commit()

    loan_pid = loan_pid_fetcher(loan.id, loan)
    assert minted_loan.pid_value == loan_pid.pid_value

    with app.test_client() as client:
        url = url_for('invenio_records_rest.loan_pid_item',
                      pid_value=loan_pid.pid_value)
        res = client.get(url, headers=json_headers)
        assert res.status_code == 200
        loan_dict = json.loads(res.data.decode('utf-8'))
        assert loan_dict['metadata']['state'] == loan['state']
Exemplo n.º 4
0
def test_api_explicit_loan_valid_action(app, db, json_headers, params):
    """Test API valid action on loan."""
    loan = Loan.create({})
    minted_loan = loan_pid_minter(loan.id, loan)
    db.session.commit()

    loan_pid = loan_pid_fetcher(loan.id, loan)
    assert minted_loan.pid_value == loan_pid.pid_value

    with app.test_client() as client:
        url = url_for('invenio_circulation.loan_pid_actions',
                      pid_value=loan_pid.pid_value,
                      action='checkout')
        res = client.post(url, headers=json_headers, data=json.dumps(params))
        assert res.status_code == HTTP_CODES['accepted']
        loan_dict = json.loads(res.data.decode('utf-8'))
        assert loan_dict['metadata']['state'] == 'ITEM_ON_LOAN'
Exemplo n.º 5
0
def test_api_loans_links_factory(app, db, json_headers, params):
    """Test API GET call to fetch a loan by PID."""
    loan = Loan.create({})
    minted_loan = loan_pid_minter(loan.id, loan)
    db.session.commit()
    loan_pid = loan_pid_fetcher(loan.id, loan)

    expected_links = {
        'available_actions': {
            'request': build_url_action_for_pid(loan_pid, 'request'),
            'checkout': build_url_action_for_pid(loan_pid, 'checkout')
        }
    }
    with app.test_client() as client:
        url = url_for('invenio_records_rest.loan_pid_item',
                      pid_value=loan_pid.pid_value)
        res = client.get(url, headers=json_headers)
        assert res.status_code == 200
        loan_dict = json.loads(res.data.decode('utf-8'))
        assert loan_dict['links'] == expected_links
Exemplo n.º 6
0
    def post(self, **kwargs):
        """Loan request view."""
        loan = Loan.create({})
        pid = loan_pid_minter(loan.id, loan)
        params = request.get_json()
        try:
            loan = current_circulation.circulation.trigger(
                loan, **dict(params, trigger="request")
            )
            db.session.commit()
        except (
            ItemNotAvailable,
            InvalidCirculationPermission,
            NoValidTransitionAvailable,
        ) as ex:
            current_app.logger.exception(ex.msg)
            raise LoanActionError(ex)

        return self.make_response(
            pid, loan, 202, links_factory=self.links_factory
        )
Exemplo n.º 7
0
def loans():
    """Load test data fixture."""
    loan = Loan.create({})
    pid = loan_pid_minter(loan.id, loan)
    db.session.commit()
    click.secho("Loan #{} created.".format(pid.pid_value), fg="green")