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);
51 my $tx = $t->ua->build_tx(GET => "/api/v1/patrons/$patron_id/account");
52 $tx->req->cookies({ name => 'CGISESSID', value => $session_id });
53 $tx->req->env({ REMOTE_ADDR => '127.0.0.1' });
56 ->json_is( { balance => 0.00 } );
58 my $account_line_1 = Koha::Account::Line->new(
60 borrowernumber => $patron->borrowernumber,
63 description => "A description",
64 accounttype => "N", # New card
65 amountoutstanding => 50,
66 manager_id => $patron->borrowernumber,
69 $account_line_1->discard_changes;
71 my $account_line_2 = Koha::Account::Line->new(
73 borrowernumber => $patron->borrowernumber,
76 description => "A description",
77 accounttype => "N", # New card
78 amountoutstanding => 50.01,
79 manager_id => $patron->borrowernumber,
82 $account_line_2->discard_changes;
84 $tx = $t->ua->build_tx( GET => "/api/v1/patrons/$patron_id/account" );
85 $tx->req->cookies( { name => 'CGISESSID', value => $session_id } );
86 $tx->req->env( { REMOTE_ADDR => '127.0.0.1' } );
87 $t->request_ok($tx)->status_is(200)->json_is(
89 outstanding_lines => [
90 Koha::REST::V1::Patrons::Account::_to_api( $account_line_1->TO_JSON ),
91 Koha::REST::V1::Patrons::Account::_to_api( $account_line_2->TO_JSON )
97 Koha::Account->new({ patron_id => $patron_id })->pay(
100 description => 'Finally!',
101 library_id => $patron->branchcode,
102 account_type => 'Pay',
103 offset_type => 'Payment'
107 $tx = $t->ua->build_tx( GET => "/api/v1/patrons/$patron_id/account" );
108 $tx->req->cookies( { name => 'CGISESSID', value => $session_id } );
109 $tx->req->env( { REMOTE_ADDR => '127.0.0.1' } );
110 $t->request_ok($tx)->status_is(200)->json_is( { balance => 0 } );
112 $schema->storage->txn_rollback;
115 sub create_user_and_session {
118 my $flags = ( $args->{authorized} ) ? 16 : 0;
120 my $user = $builder->build(
122 source => 'Borrower',
127 email => 'nobody@example.com',
128 emailpro => 'nobody@example.com',
129 B_email => 'nobody@example.com'
134 # Create a session for the authorized user
135 my $session = C4::Auth::get_session('');
136 $session->param( 'number', $user->{borrowernumber} );
137 $session->param( 'id', $user->{userid} );
138 $session->param( 'ip', '127.0.0.1' );
139 $session->param( 'lasttime', time() );
142 return ( $user->{borrowernumber}, $session->id );