3 # This file is part of Koha.
5 # Koha is free software; you can redistribute it and/or modify it under the
6 # terms of the GNU General Public License as published by the Free Software
7 # Foundation; either version 3 of the License, or (at your option) any later
10 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
11 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License along
15 # with Koha; if not, write to the Free Software Foundation, Inc.,
16 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 use Test::More tests => 1;
25 use t::lib::TestBuilder;
28 use C4::Accounts qw(manualinvoice);
30 use Koha::Account::Line;
32 my $schema = Koha::Database->new->schema;
33 my $builder = t::lib::TestBuilder->new;
35 # FIXME: sessionStorage defaults to mysql, but it seems to break transaction handling
36 # this affects the other REST api tests
37 t::lib::Mocks::mock_preference( 'SessionStorage', 'tmp' );
39 my $remote_address = '127.0.0.1';
40 my $t = Test::Mojo->new('Koha::REST::V1');
42 subtest 'get_balance() tests' => sub {
46 $schema->storage->txn_begin;
48 my ( $patron_id, $session_id ) = create_user_and_session({ authorized => 0 });
49 my $patron = Koha::Patrons->find($patron_id);
50 my $account = $patron->account;
52 my $tx = $t->ua->build_tx(GET => "/api/v1/patrons/$patron_id/account");
53 $tx->req->cookies({ name => 'CGISESSID', value => $session_id });
54 $tx->req->env({ REMOTE_ADDR => '127.0.0.1' });
55 $t->request_ok($tx)->status_is(200)->json_is(
57 outstanding_debits => { total => 0, lines => [] },
58 outstanding_credits => { total => 0, lines => [] }
62 my $account_line_1 = Koha::Account::Line->new(
64 borrowernumber => $patron->borrowernumber,
67 description => "A description",
68 accounttype => "N", # New card
69 amountoutstanding => 50,
70 manager_id => $patron->borrowernumber,
73 $account_line_1->discard_changes;
75 my $account_line_2 = Koha::Account::Line->new(
77 borrowernumber => $patron->borrowernumber,
80 description => "A description",
81 accounttype => "N", # New card
82 amountoutstanding => 50.01,
83 manager_id => $patron->borrowernumber,
86 $account_line_2->discard_changes;
88 $tx = $t->ua->build_tx( GET => "/api/v1/patrons/$patron_id/account" );
89 $tx->req->cookies( { name => 'CGISESSID', value => $session_id } );
90 $tx->req->env( { REMOTE_ADDR => '127.0.0.1' } );
91 $t->request_ok($tx)->status_is(200)->json_is(
93 outstanding_debits => {
96 Koha::REST::V1::Patrons::Account::_to_api( $account_line_1->TO_JSON ),
97 Koha::REST::V1::Patrons::Account::_to_api( $account_line_2->TO_JSON )
100 outstanding_credits => {
110 description => 'Finally!',
111 library_id => $patron->branchcode,
112 account_type => 'Pay',
113 offset_type => 'Payment'
117 $tx = $t->ua->build_tx( GET => "/api/v1/patrons/$patron_id/account" );
118 $tx->req->cookies( { name => 'CGISESSID', value => $session_id } );
119 $tx->req->env( { REMOTE_ADDR => '127.0.0.1' } );
120 $t->request_ok($tx)->status_is(200)->json_is(
122 outstanding_debits => { total => 0, lines => [] },
123 outstanding_credits => { total => 0, lines => [] }
128 my $credit_line = $account->add_credit({ amount => 10, user_id => $patron->id });
129 # re-read from the DB
130 $credit_line->discard_changes;
131 $tx = $t->ua->build_tx( GET => "/api/v1/patrons/$patron_id/account" );
132 $tx->req->cookies( { name => 'CGISESSID', value => $session_id } );
133 $tx->req->env( { REMOTE_ADDR => '127.0.0.1' } );
134 $t->request_ok($tx)->status_is(200)->json_is(
136 outstanding_debits => {
140 outstanding_credits => {
142 lines => [ Koha::REST::V1::Patrons::Account::_to_api( $credit_line->TO_JSON ) ]
147 $schema->storage->txn_rollback;
150 sub create_user_and_session {
153 my $flags = ( $args->{authorized} ) ? 16 : 0;
155 my $user = $builder->build(
157 source => 'Borrower',
162 email => 'nobody@example.com',
163 emailpro => 'nobody@example.com',
164 B_email => 'nobody@example.com'
169 # Create a session for the authorized user
170 my $session = C4::Auth::get_session('');
171 $session->param( 'number', $user->{borrowernumber} );
172 $session->param( 'id', $user->{userid} );
173 $session->param( 'ip', '127.0.0.1' );
174 $session->param( 'lasttime', time() );
177 return ( $user->{borrowernumber}, $session->id );