Lines 41-59
my $t = Test::Mojo->new('Koha::REST::V1');
Link Here
|
41 |
|
41 |
|
42 |
subtest 'get_balance() tests' => sub { |
42 |
subtest 'get_balance() tests' => sub { |
43 |
|
43 |
|
44 |
plan tests => 9; |
44 |
plan tests => 12; |
45 |
|
45 |
|
46 |
$schema->storage->txn_begin; |
46 |
$schema->storage->txn_begin; |
47 |
|
47 |
|
48 |
my ( $patron_id, $session_id ) = create_user_and_session({ authorized => 0 }); |
48 |
my ( $patron_id, $session_id ) = create_user_and_session({ authorized => 0 }); |
49 |
my $patron = Koha::Patrons->find($patron_id); |
49 |
my $patron = Koha::Patrons->find($patron_id); |
|
|
50 |
my $account = $patron->account; |
50 |
|
51 |
|
51 |
my $tx = $t->ua->build_tx(GET => "/api/v1/patrons/$patron_id/account"); |
52 |
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->cookies({ name => 'CGISESSID', value => $session_id }); |
53 |
$tx->req->env({ REMOTE_ADDR => '127.0.0.1' }); |
54 |
$tx->req->env({ REMOTE_ADDR => '127.0.0.1' }); |
54 |
$t->request_ok($tx) |
55 |
$t->request_ok($tx)->status_is(200)->json_is( |
55 |
->status_is(200) |
56 |
{ balance => 0.00, |
56 |
->json_is( { balance => 0.00 } ); |
57 |
outstanding_debits => { total => 0, lines => [] }, |
|
|
58 |
outstanding_credits => { total => 0, lines => [] } |
59 |
} |
60 |
); |
57 |
|
61 |
|
58 |
my $account_line_1 = Koha::Account::Line->new( |
62 |
my $account_line_1 = Koha::Account::Line->new( |
59 |
{ |
63 |
{ |
Lines 85-100
subtest 'get_balance() tests' => sub {
Link Here
|
85 |
$tx->req->cookies( { name => 'CGISESSID', value => $session_id } ); |
89 |
$tx->req->cookies( { name => 'CGISESSID', value => $session_id } ); |
86 |
$tx->req->env( { REMOTE_ADDR => '127.0.0.1' } ); |
90 |
$tx->req->env( { REMOTE_ADDR => '127.0.0.1' } ); |
87 |
$t->request_ok($tx)->status_is(200)->json_is( |
91 |
$t->request_ok($tx)->status_is(200)->json_is( |
88 |
{ balance => 100.01, |
92 |
{ balance => 100.01, |
89 |
outstanding_lines => [ |
93 |
outstanding_debits => { |
90 |
Koha::REST::V1::Patrons::Account::_to_api( $account_line_1->TO_JSON ), |
94 |
total => 100.01, |
91 |
Koha::REST::V1::Patrons::Account::_to_api( $account_line_2->TO_JSON ) |
95 |
lines => [ |
92 |
|
96 |
Koha::REST::V1::Patrons::Account::_to_api( $account_line_1->TO_JSON ), |
93 |
] |
97 |
Koha::REST::V1::Patrons::Account::_to_api( $account_line_2->TO_JSON ) |
|
|
98 |
] |
99 |
}, |
100 |
outstanding_credits => { |
101 |
total => 0, |
102 |
lines => [] |
103 |
} |
94 |
} |
104 |
} |
95 |
); |
105 |
); |
96 |
|
106 |
|
97 |
Koha::Account->new({ patron_id => $patron_id })->pay( |
107 |
$account->pay( |
98 |
{ amount => 100.01, |
108 |
{ amount => 100.01, |
99 |
note => 'He paid!', |
109 |
note => 'He paid!', |
100 |
description => 'Finally!', |
110 |
description => 'Finally!', |
Lines 107-113
subtest 'get_balance() tests' => sub {
Link Here
|
107 |
$tx = $t->ua->build_tx( GET => "/api/v1/patrons/$patron_id/account" ); |
117 |
$tx = $t->ua->build_tx( GET => "/api/v1/patrons/$patron_id/account" ); |
108 |
$tx->req->cookies( { name => 'CGISESSID', value => $session_id } ); |
118 |
$tx->req->cookies( { name => 'CGISESSID', value => $session_id } ); |
109 |
$tx->req->env( { REMOTE_ADDR => '127.0.0.1' } ); |
119 |
$tx->req->env( { REMOTE_ADDR => '127.0.0.1' } ); |
110 |
$t->request_ok($tx)->status_is(200)->json_is( { balance => 0 } ); |
120 |
$t->request_ok($tx)->status_is(200)->json_is( |
|
|
121 |
{ balance => 0, |
122 |
outstanding_debits => { total => 0, lines => [] }, |
123 |
outstanding_credits => { total => 0, lines => [] } |
124 |
} |
125 |
); |
126 |
|
127 |
# add a credit |
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( |
135 |
{ balance => -10, |
136 |
outstanding_debits => { |
137 |
total => 0, |
138 |
lines => [] |
139 |
}, |
140 |
outstanding_credits => { |
141 |
total => -10, |
142 |
lines => [ Koha::REST::V1::Patrons::Account::_to_api( $credit_line->TO_JSON ) ] |
143 |
} |
144 |
} |
145 |
); |
111 |
|
146 |
|
112 |
$schema->storage->txn_rollback; |
147 |
$schema->storage->txn_rollback; |
113 |
}; |
148 |
}; |
114 |
- |
|
|