Bug 21786: Unit tests
[koha.git] / t / db_dependent / api / v1 / patrons_accounts.t
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
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
8 # version.
9 #
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.
13 #
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.
17
18 use Modern::Perl;
19
20 use Test::More tests => 2;
21
22 use Test::Mojo;
23 use Test::Warn;
24
25 use t::lib::TestBuilder;
26 use t::lib::Mocks;
27
28 use C4::Accounts qw(manualinvoice);
29 use C4::Auth;
30 use Koha::Account::Line;
31
32 my $schema  = Koha::Database->new->schema;
33 my $builder = t::lib::TestBuilder->new;
34
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' );
38
39 my $remote_address = '127.0.0.1';
40 my $t              = Test::Mojo->new('Koha::REST::V1');
41
42 subtest 'get_balance() tests' => sub {
43
44     plan tests => 12;
45
46     $schema->storage->txn_begin;
47
48     my ( $patron, $session_id ) = create_user_and_session({ authorized => 0 });
49     my $library   = $builder->build_object({ class => 'Koha::Libraries' });
50     my $patron_id = $patron->id;
51     my $account   = $patron->account;
52
53     my $tx = $t->ua->build_tx(GET => "/api/v1/patrons/$patron_id/account");
54     $tx->req->cookies({ name => 'CGISESSID', value => $session_id });
55     $tx->req->env({ REMOTE_ADDR => '127.0.0.1' });
56     $t->request_ok($tx)->status_is(200)->json_is(
57         {   balance             => 0.00,
58             outstanding_debits  => { total => 0, lines => [] },
59             outstanding_credits => { total => 0, lines => [] }
60         }
61     );
62
63     my $account_line_1 = Koha::Account::Line->new(
64         {
65             borrowernumber    => $patron->borrowernumber,
66             date              => \'NOW()',
67             amount            => 50,
68             description       => "A description",
69             accounttype       => "N", # New card
70             amountoutstanding => 50,
71             manager_id        => $patron->borrowernumber,
72             branchcode        => $library->id
73         }
74     )->store();
75     $account_line_1->discard_changes;
76
77     my $account_line_2 = Koha::Account::Line->new(
78         {
79             borrowernumber    => $patron->borrowernumber,
80             date              => \'NOW()',
81             amount            => 50.01,
82             description       => "A description",
83             accounttype       => "N", # New card
84             amountoutstanding => 50.01,
85             manager_id        => $patron->borrowernumber,
86             branchcode        => $library->id
87         }
88     )->store();
89     $account_line_2->discard_changes;
90
91     $tx = $t->ua->build_tx( GET => "/api/v1/patrons/$patron_id/account" );
92     $tx->req->cookies( { name => 'CGISESSID', value => $session_id } );
93     $tx->req->env( { REMOTE_ADDR => '127.0.0.1' } );
94     $t->request_ok($tx)->status_is(200)->json_is(
95         {   balance            => 100.01,
96             outstanding_debits => {
97                 total => 100.01,
98                 lines => [
99                     Koha::REST::V1::Patrons::Account::_to_api( $account_line_1->TO_JSON ),
100                     Koha::REST::V1::Patrons::Account::_to_api( $account_line_2->TO_JSON )
101                 ]
102             },
103             outstanding_credits => {
104                 total => 0,
105                 lines => []
106             }
107         }
108     );
109
110     $account->pay(
111         {   amount       => 100.01,
112             note         => 'He paid!',
113             description  => 'Finally!',
114             library_id   => $patron->branchcode,
115             account_type => 'Pay',
116             offset_type  => 'Payment'
117         }
118     );
119
120     $tx = $t->ua->build_tx( GET => "/api/v1/patrons/$patron_id/account" );
121     $tx->req->cookies( { name => 'CGISESSID', value => $session_id } );
122     $tx->req->env( { REMOTE_ADDR => '127.0.0.1' } );
123     $t->request_ok($tx)->status_is(200)->json_is(
124         {   balance             => 0,
125             outstanding_debits  => { total => 0, lines => [] },
126             outstanding_credits => { total => 0, lines => [] }
127         }
128     );
129
130     # add a credit
131     my $credit_line = $account->add_credit(
132         { amount => 10, user_id => $patron->id, library_id => $library->id } );
133     # re-read from the DB
134     $credit_line->discard_changes;
135     $tx = $t->ua->build_tx( GET => "/api/v1/patrons/$patron_id/account" );
136     $tx->req->cookies( { name => 'CGISESSID', value => $session_id } );
137     $tx->req->env( { REMOTE_ADDR => '127.0.0.1' } );
138     $t->request_ok($tx)->status_is(200)->json_is(
139         {   balance            => -10,
140             outstanding_debits => {
141                 total => 0,
142                 lines => []
143             },
144             outstanding_credits => {
145                 total => -10,
146                 lines => [ Koha::REST::V1::Patrons::Account::_to_api( $credit_line->TO_JSON ) ]
147             }
148         }
149     );
150
151     $schema->storage->txn_rollback;
152 };
153
154 subtest 'add_credit() tests' => sub {
155
156     plan tests => 18;
157
158     $schema->storage->txn_begin;
159
160     my ( $patron, $session_id ) = create_user_and_session( { authorized => 1 } );
161     my $library   = $builder->build_object({ class => 'Koha::Libraries' });
162     my $patron_id = $patron->id;
163     my $account   = $patron->account;
164
165     is( $account->outstanding_debits->count,  0, 'No outstanding debits for patron' );
166     is( $account->outstanding_credits->count, 0, 'No outstanding credits for patron' );
167
168     my $credit = { amount => 100 };
169
170     my $tx = $t->ua->build_tx(
171         POST => "/api/v1/patrons/$patron_id/account/credits" => json => $credit );
172     $tx->req->cookies( { name => 'CGISESSID', value => $session_id } );
173     $tx->req->env( { REMOTE_ADDR => '127.0.0.1' } );
174     $t->request_ok($tx)->status_is(200)->json_has('/account_line_id');
175
176     my $outstanding_credits = $account->outstanding_credits;
177     is( $outstanding_credits->count,             1 );
178     is( $outstanding_credits->total_outstanding, -100 );
179
180     my $debit_1 = Koha::Account::Line->new(
181         {   borrowernumber    => $patron->borrowernumber,
182             date              => \'NOW()',
183             amount            => 10,
184             description       => "A description",
185             accounttype       => "N",                       # New card
186             amountoutstanding => 10,
187             manager_id        => $patron->borrowernumber,
188         }
189     )->store();
190     my $debit_2 = Koha::Account::Line->new(
191         {   borrowernumber    => $patron->borrowernumber,
192             date              => \'NOW()',
193             amount            => 15,
194             description       => "A description",
195             accounttype       => "N",                       # New card
196             amountoutstanding => 15,
197             manager_id        => $patron->borrowernumber,
198         }
199     )->store();
200
201     is( $account->outstanding_debits->total_outstanding, 25 );
202     $credit->{library_id} = $library->id;
203     $tx = $t->ua->build_tx(
204         POST => "/api/v1/patrons/$patron_id/account/credits" => json => $credit );
205     $tx->req->cookies( { name => 'CGISESSID', value => $session_id } );
206     $tx->req->env( { REMOTE_ADDR => '127.0.0.1' } );
207     $t->request_ok($tx)->status_is(200)->json_has('/account_line_id');
208
209     my $account_line_id = $tx->res->json->{account_line_id};
210     is( Koha::Account::Lines->find($account_line_id)->branchcode,
211         $library->id, 'Library id is sored correctly' );
212
213     is( $account->outstanding_debits->total_outstanding,
214         0, "Debits have been cancelled automatically" );
215
216     my $debit_3 = Koha::Account::Line->new(
217         {   borrowernumber    => $patron->borrowernumber,
218             date              => \'NOW()',
219             amount            => 100,
220             description       => "A description",
221             accounttype       => "N",                       # New card
222             amountoutstanding => 100,
223             manager_id        => $patron->borrowernumber,
224         }
225     )->store();
226
227     $credit = {
228         amount            => 35,
229         account_lines_ids => [ $debit_1->id, $debit_2->id, $debit_3->id ]
230     };
231
232     $tx = $t->ua->build_tx(
233         POST => "/api/v1/patrons/$patron_id/account/credits" => json => $credit );
234     $tx->req->cookies( { name => 'CGISESSID', value => $session_id } );
235     $tx->req->env( { REMOTE_ADDR => '127.0.0.1' } );
236     $t->request_ok($tx)->status_is(200)->json_has('/account_line_id');
237
238     my $outstanding_debits = $account->outstanding_debits;
239     is( $outstanding_debits->total_outstanding, 65 );
240     is( $outstanding_debits->count,             1 );
241
242     $schema->storage->txn_rollback;
243 };
244
245 sub create_user_and_session {
246
247     my $args  = shift;
248     my $flags = ( $args->{authorized} ) ? 2**10 : 0;
249
250     my $patron = $builder->build_object(
251         {
252             class => 'Koha::Patrons',
253             value  => {
254                 flags         => $flags
255             }
256         }
257     );
258
259     # Create a session for the authorized user
260     my $session = C4::Auth::get_session('');
261     $session->param( 'number',   $patron->id );
262     $session->param( 'id',       $patron->userid );
263     $session->param( 'ip',       '127.0.0.1' );
264     $session->param( 'lasttime', time() );
265     $session->flush;
266
267     return ( $patron, $session->id );
268 }