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

(-)a/t/db_dependent/api/v1/patrons_accounts.t (-72 / +46 lines)
Lines 20-42 use Modern::Perl; Link Here
20
use Test::More tests => 2;
20
use Test::More tests => 2;
21
21
22
use Test::Mojo;
22
use Test::Mojo;
23
use Test::Warn;
24
23
25
use t::lib::TestBuilder;
24
use t::lib::TestBuilder;
26
use t::lib::Mocks;
25
use t::lib::Mocks;
27
26
28
use C4::Auth;
27
use Koha::Account::Lines;
29
use Koha::Account::Line;
30
28
31
my $schema  = Koha::Database->new->schema;
29
my $schema  = Koha::Database->new->schema;
32
my $builder = t::lib::TestBuilder->new;
30
my $builder = t::lib::TestBuilder->new;
33
31
34
# FIXME: sessionStorage defaults to mysql, but it seems to break transaction handling
32
t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 );
35
# this affects the other REST api tests
36
t::lib::Mocks::mock_preference( 'SessionStorage', 'tmp' );
37
33
38
my $remote_address = '127.0.0.1';
34
my $t = Test::Mojo->new('Koha::REST::V1');
39
my $t              = Test::Mojo->new('Koha::REST::V1');
40
35
41
subtest 'get_balance() tests' => sub {
36
subtest 'get_balance() tests' => sub {
42
37
Lines 44-58 subtest 'get_balance() tests' => sub { Link Here
44
39
45
    $schema->storage->txn_begin;
40
    $schema->storage->txn_begin;
46
41
47
    my ( $patron, $session_id ) = create_user_and_session({ authorized => 1 });
42
    my $patron = $builder->build_object({
43
        class => 'Koha::Patrons',
44
        value => { flags => 1 }
45
    });
46
    my $password = 'thePassword123';
47
    $patron->set_password({ password => $password, skip_validation => 1 });
48
    my $userid = $patron->userid;
49
48
    my $library   = $builder->build_object({ class => 'Koha::Libraries' });
50
    my $library   = $builder->build_object({ class => 'Koha::Libraries' });
49
    my $patron_id = $patron->id;
51
    my $patron_id = $patron->id;
50
    my $account   = $patron->account;
52
    my $account   = $patron->account;
51
53
52
    my $tx = $t->ua->build_tx(GET => "/api/v1/patrons/$patron_id/account");
54
    $t->get_ok("//$userid:$password@/api/v1/patrons/$patron_id/account")
53
    $tx->req->cookies({ name => 'CGISESSID', value => $session_id });
55
      ->status_is(200)
54
    $tx->req->env({ REMOTE_ADDR => '127.0.0.1' });
56
      ->json_is(
55
    $t->request_ok($tx)->status_is(200)->json_is(
56
        {   balance             => 0.00,
57
        {   balance             => 0.00,
57
            outstanding_debits  => { total => 0, lines => [] },
58
            outstanding_debits  => { total => 0, lines => [] },
58
            outstanding_credits => { total => 0, lines => [] }
59
            outstanding_credits => { total => 0, lines => [] }
Lines 89-98 subtest 'get_balance() tests' => sub { Link Here
89
    )->store();
90
    )->store();
90
    $account_line_2->discard_changes;
91
    $account_line_2->discard_changes;
91
92
92
    $tx = $t->ua->build_tx( GET => "/api/v1/patrons/$patron_id/account" );
93
    $t->get_ok("//$userid:$password@/api/v1/patrons/$patron_id/account")
93
    $tx->req->cookies( { name => 'CGISESSID', value => $session_id } );
94
      ->status_is(200)
94
    $tx->req->env( { REMOTE_ADDR => '127.0.0.1' } );
95
      ->json_is(
95
    $t->request_ok($tx)->status_is(200)->json_is(
96
        {   balance            => 100.01,
96
        {   balance            => 100.01,
97
            outstanding_debits => {
97
            outstanding_debits => {
98
                total => 100.01,
98
                total => 100.01,
Lines 118-127 subtest 'get_balance() tests' => sub { Link Here
118
        }
118
        }
119
    );
119
    );
120
120
121
    $tx = $t->ua->build_tx( GET => "/api/v1/patrons/$patron_id/account" );
121
    $t->get_ok("//$userid:$password@/api/v1/patrons/$patron_id/account")
122
    $tx->req->cookies( { name => 'CGISESSID', value => $session_id } );
122
      ->status_is(200)
123
    $tx->req->env( { REMOTE_ADDR => '127.0.0.1' } );
123
      ->json_is(
124
    $t->request_ok($tx)->status_is(200)->json_is(
125
        {   balance             => 0,
124
        {   balance             => 0,
126
            outstanding_debits  => { total => 0, lines => [] },
125
            outstanding_debits  => { total => 0, lines => [] },
127
            outstanding_credits => { total => 0, lines => [] }
126
            outstanding_credits => { total => 0, lines => [] }
Lines 133-142 subtest 'get_balance() tests' => sub { Link Here
133
        { amount => 10, user_id => $patron->id, library_id => $library->id, interface => 'test' } );
132
        { amount => 10, user_id => $patron->id, library_id => $library->id, interface => 'test' } );
134
    # re-read from the DB
133
    # re-read from the DB
135
    $credit_line->discard_changes;
134
    $credit_line->discard_changes;
136
    $tx = $t->ua->build_tx( GET => "/api/v1/patrons/$patron_id/account" );
135
137
    $tx->req->cookies( { name => 'CGISESSID', value => $session_id } );
136
    $t->get_ok("//$userid:$password@/api/v1/patrons/$patron_id/account")
138
    $tx->req->env( { REMOTE_ADDR => '127.0.0.1' } );
137
      ->status_is(200)
139
    $t->request_ok($tx)->status_is(200)->json_is(
138
      ->json_is(
140
        {   balance            => -10,
139
        {   balance            => -10,
141
            outstanding_debits => {
140
            outstanding_debits => {
142
                total => 0,
141
                total => 0,
Lines 165-174 subtest 'get_balance() tests' => sub { Link Here
165
    )->store();
164
    )->store();
166
    $account_line_3->discard_changes;
165
    $account_line_3->discard_changes;
167
166
168
    $tx = $t->ua->build_tx( GET => "/api/v1/patrons/$patron_id/account" );
167
    $t->get_ok("//$userid:$password@/api/v1/patrons/$patron_id/account")
169
    $tx->req->cookies( { name => 'CGISESSID', value => $session_id } );
168
      ->status_is(200)
170
    $tx->req->env( { REMOTE_ADDR => '127.0.0.1' } );
169
      ->json_is(
171
    $t->request_ok($tx)->status_is(200)->json_is(
172
        {   balance            => 40.00,
170
        {   balance            => 40.00,
173
            outstanding_debits => {
171
            outstanding_debits => {
174
                total => 50.00,
172
                total => 50.00,
Lines 192-198 subtest 'add_credit() tests' => sub { Link Here
192
190
193
    $schema->storage->txn_begin;
191
    $schema->storage->txn_begin;
194
192
195
    my ( $patron, $session_id ) = create_user_and_session( { authorized => 1 } );
193
    my $patron = $builder->build_object({
194
        class => 'Koha::Patrons',
195
        value => { flags => 1 }
196
    });
197
    my $password = 'thePassword123';
198
    $patron->set_password({ password => $password, skip_validation => 1 });
199
    my $userid = $patron->userid;
200
196
    my $library   = $builder->build_object({ class => 'Koha::Libraries' });
201
    my $library   = $builder->build_object({ class => 'Koha::Libraries' });
197
    my $patron_id = $patron->id;
202
    my $patron_id = $patron->id;
198
    my $account   = $patron->account;
203
    my $account   = $patron->account;
Lines 202-212 subtest 'add_credit() tests' => sub { Link Here
202
207
203
    my $credit = { amount => 100 };
208
    my $credit = { amount => 100 };
204
209
205
    my $tx = $t->ua->build_tx(
210
    $t->post_ok("//$userid:$password@/api/v1/patrons/$patron_id/account/credits" => json => $credit)
206
        POST => "/api/v1/patrons/$patron_id/account/credits" => json => $credit );
211
      ->status_is(200)
207
    $tx->req->cookies( { name => 'CGISESSID', value => $session_id } );
212
      ->json_has('/account_line_id');
208
    $tx->req->env( { REMOTE_ADDR => '127.0.0.1' } );
209
    $t->request_ok($tx)->status_is(200)->json_has('/account_line_id');
210
213
211
    my $outstanding_credits = $account->outstanding_credits;
214
    my $outstanding_credits = $account->outstanding_credits;
212
    is( $outstanding_credits->count,             1 );
215
    is( $outstanding_credits->count,             1 );
Lines 237-249 subtest 'add_credit() tests' => sub { Link Here
237
240
238
    is( $account->outstanding_debits->total_outstanding, 25 );
241
    is( $account->outstanding_debits->total_outstanding, 25 );
239
    $credit->{library_id} = $library->id;
242
    $credit->{library_id} = $library->id;
240
    $tx = $t->ua->build_tx(
241
        POST => "/api/v1/patrons/$patron_id/account/credits" => json => $credit );
242
    $tx->req->cookies( { name => 'CGISESSID', value => $session_id } );
243
    $tx->req->env( { REMOTE_ADDR => '127.0.0.1' } );
244
    $t->request_ok($tx)->status_is(200)->json_has('/account_line_id');
245
243
246
    my $account_line_id = $tx->res->json->{account_line_id};
244
    $t->post_ok("//$userid:$password@/api/v1/patrons/$patron_id/account/credits" => json => $credit)
245
      ->status_is(200)
246
      ->json_has('/account_line_id');
247
248
    my $account_line_id = $t->tx->res->json->{account_line_id};
247
    is( Koha::Account::Lines->find($account_line_id)->branchcode,
249
    is( Koha::Account::Lines->find($account_line_id)->branchcode,
248
        $library->id, 'Library id is sored correctly' );
250
        $library->id, 'Library id is sored correctly' );
249
251
Lines 267-277 subtest 'add_credit() tests' => sub { Link Here
267
        account_lines_ids => [ $debit_1->id, $debit_2->id, $debit_3->id ]
269
        account_lines_ids => [ $debit_1->id, $debit_2->id, $debit_3->id ]
268
    };
270
    };
269
271
270
    $tx = $t->ua->build_tx(
272
    $t->post_ok("//$userid:$password@/api/v1/patrons/$patron_id/account/credits" => json => $credit)
271
        POST => "/api/v1/patrons/$patron_id/account/credits" => json => $credit );
273
      ->status_is(200)
272
    $tx->req->cookies( { name => 'CGISESSID', value => $session_id } );
274
      ->json_has('/account_line_id');
273
    $tx->req->env( { REMOTE_ADDR => '127.0.0.1' } );
274
    $t->request_ok($tx)->status_is(200)->json_has('/account_line_id');
275
275
276
    my $outstanding_debits = $account->outstanding_debits;
276
    my $outstanding_debits = $account->outstanding_debits;
277
    is( $outstanding_debits->total_outstanding, 65 );
277
    is( $outstanding_debits->total_outstanding, 65 );
Lines 279-306 subtest 'add_credit() tests' => sub { Link Here
279
279
280
    $schema->storage->txn_rollback;
280
    $schema->storage->txn_rollback;
281
};
281
};
282
283
sub create_user_and_session {
284
285
    my $args  = shift;
286
    my $flags = ( $args->{authorized} ) ? 1 : 0;
287
288
    my $patron = $builder->build_object(
289
        {
290
            class => 'Koha::Patrons',
291
            value  => {
292
                flags         => $flags
293
            }
294
        }
295
    );
296
297
    # Create a session for the authorized user
298
    my $session = C4::Auth::get_session('');
299
    $session->param( 'number',   $patron->id );
300
    $session->param( 'id',       $patron->userid );
301
    $session->param( 'ip',       '127.0.0.1' );
302
    $session->param( 'lasttime', time() );
303
    $session->flush;
304
305
    return ( $patron, $session->id );
306
}
307
- 

Return to bug 27587