View | Details | Raw Unified | Return to bug 20942
Collapse All | Expand All

(-)a/t/db_dependent/api/v1/patrons_accounts.t (-1 / +143 lines)
Line 0 Link Here
0
- 
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 => 1;
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 => 9;
45
46
    $schema->storage->txn_begin;
47
48
    my ( $patron_id, $session_id ) = create_user_and_session({ authorized => 0 });
49
    my $patron = Koha::Patrons->find($patron_id);
50
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' });
54
    $t->request_ok($tx)
55
      ->status_is(200)
56
      ->json_is( { balance => 0.00 } );
57
58
    my $account_line_1 = Koha::Account::Line->new(
59
        {
60
            borrowernumber    => $patron->borrowernumber,
61
            date              => \'NOW()',
62
            amount            => 50,
63
            description       => "A description",
64
            accounttype       => "N", # New card
65
            amountoutstanding => 50,
66
            manager_id        => $patron->borrowernumber,
67
        }
68
    )->store();
69
    $account_line_1->discard_changes;
70
71
    my $account_line_2 = Koha::Account::Line->new(
72
        {
73
            borrowernumber    => $patron->borrowernumber,
74
            date              => \'NOW()',
75
            amount            => 50.01,
76
            description       => "A description",
77
            accounttype       => "N", # New card
78
            amountoutstanding => 50.01,
79
            manager_id        => $patron->borrowernumber,
80
        }
81
    )->store();
82
    $account_line_2->discard_changes;
83
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(
88
        {   balance           => 100.01,
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 )
92
93
            ]
94
        }
95
    );
96
97
    Koha::Account->new({ patron_id => $patron_id })->pay(
98
        {   amount       => 100.01,
99
            note         => 'He paid!',
100
            description  => 'Finally!',
101
            library_id   => $patron->branchcode,
102
            account_type => 'Pay',
103
            offset_type  => 'Payment'
104
        }
105
    );
106
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 } );
111
112
    $schema->storage->txn_rollback;
113
};
114
115
sub create_user_and_session {
116
117
    my $args  = shift;
118
    my $flags = ( $args->{authorized} ) ? 16 : 0;
119
120
    my $user = $builder->build(
121
        {
122
            source => 'Borrower',
123
            value  => {
124
                flags => $flags,
125
                gonenoaddress => 0,
126
                lost => 0,
127
                email => 'nobody@example.com',
128
                emailpro => 'nobody@example.com',
129
                B_email => 'nobody@example.com'
130
            }
131
        }
132
    );
133
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() );
140
    $session->flush;
141
142
    return ( $user->{borrowernumber}, $session->id );
143
}

Return to bug 20942