From 9b6ab5266b8eb0c0c98a08089f2498317c500491 Mon Sep 17 00:00:00 2001 From: Alex Arnaud Date: Tue, 10 Nov 2015 09:52:45 +0100 Subject: [PATCH] [SIGNED-OFF] Bug 15165 - Add API route to list accountlines GET /accountlines (list) GET /accountlines?borrowernumber=X (list) Test plan: 1. Open a browser tab on Koha staff and log in (to create CGISESSID cookie). You should have permission updatecharges. 2. Go to http://yourlibrary/api/v1/accountlines?borrowernumber=XXX (replace XXX with a borrowernumber that has accountlines) and check you receive correct data 3. Go to http://yourlibrary/api/v1/accountlines and check that you receive all accountlines 4. Run unit tests in t/db_dependent/api/v1/accountlines.t Signed-off-by: Josef Moravec --- Koha/REST/V1/Accountline.pm | 34 ++++++++ api/v1/swagger/definitions.json | 3 + api/v1/swagger/definitions/accountline.json | 56 +++++++++++++ api/v1/swagger/paths.json | 3 + api/v1/swagger/paths/accountlines.json | 35 +++++++++ t/db_dependent/api/v1/accountlines.t | 118 ++++++++++++++++++++++++++++ 6 files changed, 249 insertions(+) create mode 100644 Koha/REST/V1/Accountline.pm create mode 100644 api/v1/swagger/definitions/accountline.json create mode 100644 api/v1/swagger/paths/accountlines.json create mode 100644 t/db_dependent/api/v1/accountlines.t diff --git a/Koha/REST/V1/Accountline.pm b/Koha/REST/V1/Accountline.pm new file mode 100644 index 0000000..4acf294 --- /dev/null +++ b/Koha/REST/V1/Accountline.pm @@ -0,0 +1,34 @@ +package Koha::REST::V1::Accountline; + +# 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 Koha::Account::Lines; + +sub list { + my ($c, $args, $cb) = @_; + + my $params = $c->req->params->to_hash; + my $accountlines = Koha::Account::Lines->search($params); + + return $c->$cb($accountlines->unblessed, 200); +} + +1; diff --git a/api/v1/swagger/definitions.json b/api/v1/swagger/definitions.json index d27167b..9a077a2 100644 --- a/api/v1/swagger/definitions.json +++ b/api/v1/swagger/definitions.json @@ -2,6 +2,9 @@ "city": { "$ref": "definitions/city.json" }, + "accountline": { + "$ref": "definitions/accountline.json" + }, "patron": { "$ref": "definitions/patron.json" }, diff --git a/api/v1/swagger/definitions/accountline.json b/api/v1/swagger/definitions/accountline.json new file mode 100644 index 0000000..3bfe370 --- /dev/null +++ b/api/v1/swagger/definitions/accountline.json @@ -0,0 +1,56 @@ +{ + "type": "object", + "properties": { + "accountlines_id": { + "description": "Internal account line identifier" + }, + "borrowernumber": { + "description": "Internal borrower identifier" + }, + "accountno": { + "description": "?" + }, + "itemnumber": { + "description": "Internal item identifier" + }, + "date": { + "description": "Date when the account line was created" + }, + "time": { + "description": "Time when the account line was created" + }, + "amount": { + "description": "Amount" + }, + "description": { + "description": "Description of account line" + }, + "accounttype": { + "description": "Type of accountline" + }, + "amountoutstanding": { + "description": "Amount outstanding" + }, + "lastincrement": { + "description": "?" + }, + "timestamp": { + "description": "When the account line was last updated" + }, + "notify_id": { + "description": "?" + }, + "notify_level": { + "description": "?" + }, + "note": { + "description": "Accountline note" + }, + "manager_id": { + "description": "Borrowernumber of user that created the account line" + }, + "meansofpayment": { + "description": "Means of payment" + } + } +} diff --git a/api/v1/swagger/paths.json b/api/v1/swagger/paths.json index b1fcf40..3ebaf61 100644 --- a/api/v1/swagger/paths.json +++ b/api/v1/swagger/paths.json @@ -5,6 +5,9 @@ "/cities/{cityid}": { "$ref": "paths/cities.json#/~1cities~1{cityid}" }, + "/accountlines": { + "$ref": "paths/accountlines.json#/~1accountlines" + }, "/holds": { "$ref": "paths/holds.json#/~1holds" }, diff --git a/api/v1/swagger/paths/accountlines.json b/api/v1/swagger/paths/accountlines.json new file mode 100644 index 0000000..d7aff5c --- /dev/null +++ b/api/v1/swagger/paths/accountlines.json @@ -0,0 +1,35 @@ +{ + "/accountlines": { + "get": { + "operationId": "listAccountlines", + "tags": ["accountlines"], + "produces": [ + "application/json" + ], + "responses": { + "200": { + "description": "A list of accountlines", + "schema": { + "type": "array", + "items": { + "$ref": "../definitions.json#/accountline" + } + } + }, + "403": { + "description": "Access forbidden", + "schema": { + "$ref": "../definitions.json#/error" + } + } + }, + "x-koha-authorization": { + "allow-owner": true, + "allow-guarantor": true, + "permissions": { + "updatecharges": "1" + } + } + } + } +} diff --git a/t/db_dependent/api/v1/accountlines.t b/t/db_dependent/api/v1/accountlines.t new file mode 100644 index 0000000..ceafef0 --- /dev/null +++ b/t/db_dependent/api/v1/accountlines.t @@ -0,0 +1,118 @@ +#!/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 => 12; +use Test::Mojo; +use t::lib::TestBuilder; + +use C4::Auth; +use C4::Context; + +use Koha::Database; + +my $builder = t::lib::TestBuilder->new(); + +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 $categorycode = $builder->build({ source => 'Category' })->{ categorycode }; +my $branchcode = $builder->build({ source => 'Branch' })->{ branchcode }; + +$t->get_ok('/api/v1/accountlines') + ->status_is(401); + +my $loggedinuser = $builder->build({ + source => 'Borrower', + value => { + branchcode => $branchcode, + categorycode => $categorycode, + flags => 1024 + } +}); + +my $borrower = $builder->build({ + source => 'Borrower', + value => { + branchcode => $branchcode, + categorycode => $categorycode, + flags => 0, + } +}); + +my $borrower2 = $builder->build({ + source => 'Borrower', + value => { + branchcode => $branchcode, + categorycode => $categorycode, + } +}); +my $borrowernumber = $borrower->{borrowernumber}; +my $borrowernumber2 = $borrower2->{borrowernumber}; + +$dbh->do(q| DELETE FROM accountlines |); +$dbh->do(q| + INSERT INTO accountlines (borrowernumber, amount, accounttype) + VALUES (?, 20, 'A'), (?, 40, 'F'), (?, 80, 'F'), (?, 10, 'F') + |, undef, $borrowernumber, $borrowernumber, $borrowernumber, $borrowernumber2); + +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 $borrowersession = C4::Auth::get_session(''); +$borrowersession->param('number', $borrower->{ borrowernumber }); +$borrowersession->param('id', $borrower->{ userid }); +$borrowersession->param('ip', '127.0.0.1'); +$borrowersession->param('lasttime', time()); +$borrowersession->flush; + +my $tx = $t->ua->build_tx(GET => "/api/v1/accountlines?borrowernumber=$borrowernumber2"); +$tx->req->cookies({name => 'CGISESSID', value => $borrowersession->id}); +$tx->req->env({REMOTE_ADDR => '127.0.0.1'}); +$t->request_ok($tx) + ->status_is(403); + +$tx = $t->ua->build_tx(GET => "/api/v1/accountlines?borrowernumber=$borrowernumber"); +$tx->req->cookies({name => 'CGISESSID', value => $session->id}); +$tx->req->env({REMOTE_ADDR => '127.0.0.1'}); +$t->request_ok($tx) + ->status_is(200); + +my $json = $t->tx->res->json; +ok(ref $json eq 'ARRAY', 'response is a JSON array'); +ok(scalar @$json == 3, 'response array contains 3 elements'); + +$tx = $t->ua->build_tx(GET => "/api/v1/accountlines"); +$tx->req->cookies({name => 'CGISESSID', value => $session->id}); +$tx->req->env({REMOTE_ADDR => '127.0.0.1'}); +$t->request_ok($tx) + ->status_is(200); + +$json = $t->tx->res->json; +ok(ref $json eq 'ARRAY', 'response is a JSON array'); +ok(scalar @$json == 4, 'response array contains 3 elements'); + +$dbh->rollback; -- 2.1.4