From d1864b046ce068c86388002327f900ffd68c828e Mon Sep 17 00:00:00 2001 From: Julian Maurice Date: Mon, 23 Mar 2015 13:10:46 +0100 Subject: [PATCH] Bug 13895: Add API routes for checkouts retrieval and renewal GET /checkouts?borrowernumber={borrowernumber} GET /checkouts/{checkout_id} PUT /checkouts/{checkout_id} + unit tests in t/db_dependent/api/v1/checkouts.t Test plan: 1. Open a browser tab on Koha staff and log in (to create CGISESSID cookie). You should have permission circulate_remaining_permissions. 2. Go to http://yourlibrary/api/v1/checkouts?borrowernumber=XXX (replace XXX with a borrowernumber that has checkouts) and check you receive correct data 3. Go to http://yourlibrary/api/v1/checkouts/YYY (replace YYY with an existing checkout id) and check you receive correct data 4. Send PUT requests to http://yourlibrary/api/v1/checkouts/YYY until the maximum number of renewals is reached (you should have a 403 error) 5. Run unit tests in t/db_dependent/api/v1/checkouts.t Depends on bug 15126 --- Koha/REST/V1/Checkout.pm | 94 +++++++++++++++++++++++ api/v1/definitions/checkout.json | 51 +++++++++++++ api/v1/definitions/checkouts.json | 6 ++ api/v1/definitions/index.json | 2 + api/v1/swagger.json | 94 +++++++++++++++++++++++ t/db_dependent/api/v1/checkouts.t | 154 ++++++++++++++++++++++++++++++++++++++ 6 files changed, 401 insertions(+) create mode 100644 Koha/REST/V1/Checkout.pm create mode 100644 api/v1/definitions/checkout.json create mode 100644 api/v1/definitions/checkouts.json create mode 100644 t/db_dependent/api/v1/checkouts.t diff --git a/Koha/REST/V1/Checkout.pm b/Koha/REST/V1/Checkout.pm new file mode 100644 index 0000000..5dd8128 --- /dev/null +++ b/Koha/REST/V1/Checkout.pm @@ -0,0 +1,94 @@ +package Koha::REST::V1::Checkout; + +# This file is part of Koha. +# +# Koha is free software; you can redistribute it and/or modify it under the +# terms of the GNU General Public License as published by the Free Software +# Foundation; either version 3 of the License, or (at your option) any later +# version. +# +# Koha is distributed in the hope that it will be useful, but WITHOUT ANY +# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR +# A PARTICULAR PURPOSE. See the GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with Koha; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +use Modern::Perl; + +use Mojo::Base 'Mojolicious::Controller'; + +use C4::Auth qw( haspermission ); +use C4::Circulation; +use Koha::Issues; + +sub list { + my ($c, $args, $cb) = @_; + + my $user = $c->stash('koha.user'); + unless ($user && haspermission($user->userid, { circulate => "circulate_remaining_permissions" })) { + return $c->$cb({error => "You don't have the required permission"}, 403); + } + + my $borrowernumber = $c->param('borrowernumber'); + my $checkouts = C4::Circulation::GetIssues({ + borrowernumber => $borrowernumber + }); + + $c->$cb($checkouts, 200); +} + +sub get { + my ($c, $args, $cb) = @_; + + my $user = $c->stash('koha.user'); + unless ($user && haspermission($user->userid, { circulate => "circulate_remaining_permissions" })) { + return $c->$cb({error => "You don't have the required permission"}, 403); + } + + my $checkout_id = $args->{checkout_id}; + my $checkout = Koha::Issues->find($checkout_id); + + if (!$checkout) { + return $c->$cb({ + error => "Checkout doesn't exist" + }, 404); + } + + return $c->$cb($checkout->unblessed, 200); +} + +sub renew { + my ($c, $args, $cb) = @_; + + my $user = $c->stash('koha.user'); + unless ($user && haspermission($user->userid, { circulate => "circulate_remaining_permissions" })) { + return $c->$cb({error => "You don't have the required permission"}, 403); + } + + my $checkout_id = $args->{checkout_id}; + my $checkout = Koha::Issues->find($checkout_id); + + if (!$checkout) { + return $c->$cb({ + error => "Checkout doesn't exists" + }, 404); + } + + $checkout = $checkout->unblessed; + my ($borrowernumber, $itemnumber) = @$checkout{qw(borrowernumber itemnumber)}; + my ($can_renew, $error) = C4::Circulation::CanBookBeRenewed( + $borrowernumber, $itemnumber); + + if (!$can_renew) { + return $c->$cb({error => "Renewal not authorized ($error)"}, 403); + } + + AddRenewal($borrowernumber, $itemnumber, $checkout->{branchcode}); + $checkout = Koha::Issues->find($checkout_id); + + return $c->$cb($checkout->unblessed, 200); +} + +1; diff --git a/api/v1/definitions/checkout.json b/api/v1/definitions/checkout.json new file mode 100644 index 0000000..c56c6df --- /dev/null +++ b/api/v1/definitions/checkout.json @@ -0,0 +1,51 @@ +{ + "type": "object", + "properties": { + "issue_id": { + "type": "string", + "description": "internally assigned checkout identifier" + }, + "borrowernumber": { + "type": "string", + "description": "internally assigned user identifier" + }, + "itemnumber": { + "type": "string", + "description": "internally assigned item identifier" + }, + "date_due": { + "description": "Due date" + }, + "branchcode": { + "type": ["string", "null"], + "description": "code of patron's home branch" + }, + "issuingbranch": { + "description": "Code of the branch where issue was made" + }, + "returndate": { + "description": "Date the item was returned" + }, + "lastreneweddate": { + "description": "Date the item was last renewed" + }, + "return": { + "description": "?" + }, + "renewals": { + "description": "Number of renewals" + }, + "auto_renew": { + "description": "Auto renewal" + }, + "timestamp": { + "description": "Last update time" + }, + "issuedate": { + "description": "Date the item was issued" + }, + "onsite_checkout": { + "description": "On site checkout" + } + } +} diff --git a/api/v1/definitions/checkouts.json b/api/v1/definitions/checkouts.json new file mode 100644 index 0000000..0e26d72 --- /dev/null +++ b/api/v1/definitions/checkouts.json @@ -0,0 +1,6 @@ +{ + "type": "array", + "items": { + "$ref": "./checkout.json" + } +} diff --git a/api/v1/definitions/index.json b/api/v1/definitions/index.json index e940eee..5edf42f 100644 --- a/api/v1/definitions/index.json +++ b/api/v1/definitions/index.json @@ -1,4 +1,6 @@ { "patron": { "$ref": "patron.json" }, + "checkouts": { "$ref": "checkouts.json" }, + "checkout": { "$ref": "checkout.json" }, "error": { "$ref": "error.json" } } diff --git a/api/v1/swagger.json b/api/v1/swagger.json index e821c44..9b84289 100644 --- a/api/v1/swagger.json +++ b/api/v1/swagger.json @@ -73,6 +73,100 @@ } } } + }, + "/checkouts": { + "get": { + "operationId": "listCheckouts", + "tags": ["borrowers", "checkouts"], + "parameters": [ + { + "name": "borrowernumber", + "in": "query", + "description": "Internal patron identifier", + "required": false, + "type": "integer" + } + ], + "produces": [ + "application/json" + ], + "responses": { + "200": { + "description": "A list of checkouts", + "schema": { + "$ref": "#/definitions/checkouts" + } + }, + "403": { + "description": "Access forbidden", + "schema": { "$ref": "#/definitions/error" } + }, + "404": { + "description": "Borrower not found", + "schema": { + "$ref": "#/definitions/error" + } + } + } + } + }, + "/checkouts/{checkout_id}": { + "get": { + "operationId": "getCheckout", + "tags": ["borrowers", "checkouts"], + "parameters": [ + { + "name": "checkout_id", + "in": "path", + "description": "Internal checkout identifier", + "required": true, + "type": "integer" + } + ], + "produces": ["application/json"], + "responses": { + "200": { + "description": "Updated borrower's checkout", + "schema": { "$ref": "#/definitions/checkout" } + }, + "403": { + "description": "Access forbidden", + "schema": { "$ref": "#/definitions/error" } + }, + "404": { + "description": "Checkout not found", + "schema": { "$ref": "#/definitions/error" } + } + } + }, + "put": { + "operationId": "renewCheckout", + "tags": ["borrowers", "checkouts"], + "parameters": [ + { + "name": "checkout_id", + "in": "path", + "description": "Internal checkout identifier", + "required": true, + "type": "integer" + } + ], + "produces": ["application/json"], + "responses": { + "200": { + "description": "Updated borrower's checkout", + "schema": { "$ref": "#/definitions/checkout" } + }, + "403": { + "description": "Cannot renew checkout", + "schema": { "$ref": "#/definitions/error" } + }, + "404": { + "description": "Checkout not found", + "schema": { "$ref": "#/definitions/error" } + } + } + } } }, "definitions": { diff --git a/t/db_dependent/api/v1/checkouts.t b/t/db_dependent/api/v1/checkouts.t new file mode 100644 index 0000000..98defa3 --- /dev/null +++ b/t/db_dependent/api/v1/checkouts.t @@ -0,0 +1,154 @@ +#!/usr/bin/env perl + +# This file is part of Koha. +# +# Koha is free software; you can redistribute it and/or modify it under the +# terms of the GNU General Public License as published by the Free Software +# Foundation; either version 3 of the License, or (at your option) any later +# version. +# +# Koha is distributed in the hope that it will be useful, but WITHOUT ANY +# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR +# A PARTICULAR PURPOSE. See the GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with Koha; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +use Modern::Perl; + +use Test::More tests => 27; +use Test::MockModule; +use Test::Mojo; +use t::lib::TestBuilder; + +use DateTime; +use MARC::Record; + +use C4::Context; +use C4::Biblio; +use C4::Circulation; +use C4::Items; + +use Koha::Database; +use Koha::Patron; + +my $dbh = C4::Context->dbh; +$dbh->{AutoCommit} = 0; +$dbh->{RaiseError} = 1; + +$ENV{REMOTE_ADDR} = '127.0.0.1'; +my $t = Test::Mojo->new('Koha::REST::V1'); + +my $builder = t::lib::TestBuilder->new(); + +my $loggedinuser = $builder->build({ source => 'Borrower' }); + +$dbh->do(q{ + INSERT INTO user_permissions (borrowernumber, module_bit, code) + VALUES (?, 1, 'circulate_remaining_permissions') +}, undef, $loggedinuser->{borrowernumber}); + +my $session = C4::Auth::get_session(''); +$session->param('number', $loggedinuser->{ borrowernumber }); +$session->param('id', $loggedinuser->{ userid }); +$session->param('ip', '127.0.0.1'); +$session->param('lasttime', time()); +$session->flush; + +my $borrower = $builder->build({ source => 'Borrower' }); +my $borrowernumber = $borrower->{borrowernumber}; + +my $tx = $t->ua->build_tx(GET => "/api/v1/checkouts?borrowernumber=$borrowernumber"); +$tx->req->cookies({name => 'CGISESSID', value => $session->id}); +$t->request_ok($tx) + ->status_is(200) + ->json_is([]); + +my $notexisting_borrowernumber = $borrowernumber + 1; +$tx = $t->ua->build_tx(GET => "/api/v1/checkouts?borrowernumber=$notexisting_borrowernumber"); +$tx->req->cookies({name => 'CGISESSID', value => $session->id}); +$t->request_ok($tx) + ->status_is(200) + ->json_is([]); + +my $biblionumber = create_biblio('RESTful Web APIs'); +my $itemnumber1 = create_item($biblionumber, 'TEST000001'); +my $itemnumber2 = create_item($biblionumber, 'TEST000002'); + +my $date_due = DateTime->now->add(weeks => 2); +my $issue1 = C4::Circulation::AddIssue($borrower, 'TEST000001', $date_due); +my $date_due1 = Koha::DateUtils::dt_from_string( $issue1->date_due ); +my $issue2 = C4::Circulation::AddIssue($borrower, 'TEST000002', $date_due); +my $date_due2 = Koha::DateUtils::dt_from_string( $issue2->date_due ); + +$tx = $t->ua->build_tx(GET => "/api/v1/checkouts?borrowernumber=$borrowernumber"); +$tx->req->cookies({name => 'CGISESSID', value => $session->id}); +$t->request_ok($tx) + ->status_is(200) + ->json_is('/0/borrowernumber' => $borrowernumber) + ->json_is('/0/itemnumber' => $itemnumber1) + ->json_is('/0/date_due' => $date_due1->ymd . ' ' . $date_due1->hms) + ->json_is('/1/borrowernumber' => $borrowernumber) + ->json_is('/1/itemnumber' => $itemnumber2) + ->json_is('/1/date_due' => $date_due2->ymd . ' ' . $date_due2->hms) + ->json_hasnt('/2'); + +$tx = $t->ua->build_tx(GET => "/api/v1/checkouts/" . $issue1->issue_id); +$tx->req->cookies({name => 'CGISESSID', value => $session->id}); +$t->request_ok($tx) + ->status_is(200) + ->json_is('/date_due' => $date_due1->ymd . ' ' . $date_due1->hms); + +$tx = $t->ua->build_tx(GET => "/api/v1/checkouts/" . $issue2->issue_id); +$tx->req->cookies({name => 'CGISESSID', value => $session->id}); +$t->request_ok($tx) + ->status_is(200) + ->json_is('/date_due' => $date_due2->ymd . ' ' . $date_due2->hms); + + +$dbh->do('DELETE FROM issuingrules'); +$dbh->do(q{ + INSERT INTO issuingrules (categorycode, branchcode, itemtype, issuelength, renewalsallowed) + VALUES (?, ?, ?, ?, ?) +}, {}, '*', '*', '*', 7, 1); + +my $expected_datedue = DateTime->now->add(days => 7)->set(hour => 23, minute => 59, second => 0); +$tx = $t->ua->build_tx(PUT => "/api/v1/checkouts/" . $issue1->issue_id); +$tx->req->cookies({name => 'CGISESSID', value => $session->id}); +$t->request_ok($tx) + ->status_is(200) + ->json_is('/date_due' => $expected_datedue->ymd . ' ' . $expected_datedue->hms); + +$tx = $t->ua->build_tx(PUT => "/api/v1/checkouts/" . $issue1->issue_id); +$tx->req->cookies({name => 'CGISESSID', value => $session->id}); +$t->request_ok($tx) + ->status_is(403) + ->json_is({ error => 'Renewal not authorized (too_many)' }); + +$dbh->rollback; + +sub create_biblio { + my ($title) = @_; + + my $record = new MARC::Record; + $record->append_fields( + new MARC::Field('200', ' ', ' ', a => $title), + ); + + my ($biblionumber) = C4::Biblio::AddBiblio($record, ''); + + return $biblionumber; +} + +sub create_item { + my ($biblionumber, $barcode) = @_; + + my $item = { + barcode => $barcode, + }; + + my $itemnumber = C4::Items::AddItem($item, $biblionumber); + + return $itemnumber; +} -- 2.1.4