|
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 |
| 6 |
# under the terms of the GNU General Public License as published by |
| 7 |
# the Free Software Foundation; either version 3 of the License, or |
| 8 |
# (at your option) any later version. |
| 9 |
# |
| 10 |
# Koha is distributed in the hope that it will be useful, but |
| 11 |
# WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
# GNU General Public License for more details. |
| 14 |
# |
| 15 |
# You should have received a copy of the GNU General Public License |
| 16 |
# along with Koha; if not, see <https://www.gnu.org/licenses>. |
| 17 |
|
| 18 |
use Modern::Perl; |
| 19 |
|
| 20 |
use Test::More tests => 4; |
| 21 |
use Test::NoWarnings; |
| 22 |
|
| 23 |
use Test::Mojo; |
| 24 |
|
| 25 |
use t::lib::TestBuilder; |
| 26 |
use t::lib::Mocks; |
| 27 |
|
| 28 |
use Koha::Account::Lines; |
| 29 |
use Koha::Account::CreditTypes; |
| 30 |
use Koha::Cash::Registers; |
| 31 |
|
| 32 |
my $schema = Koha::Database->new->schema; |
| 33 |
my $builder = t::lib::TestBuilder->new; |
| 34 |
|
| 35 |
t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 ); |
| 36 |
|
| 37 |
my $t = Test::Mojo->new('Koha::REST::V1'); |
| 38 |
|
| 39 |
subtest 'list_credits() tests' => sub { |
| 40 |
|
| 41 |
plan tests => 23; |
| 42 |
|
| 43 |
$schema->storage->txn_begin; |
| 44 |
|
| 45 |
# Create test patron with anonymous_refund permission |
| 46 |
my $librarian = $builder->build_object( |
| 47 |
{ |
| 48 |
class => 'Koha::Patrons', |
| 49 |
value => { flags => 0 } |
| 50 |
} |
| 51 |
); |
| 52 |
my $password = 'thePassword123'; |
| 53 |
$librarian->set_password( { password => $password, skip_validation => 1 } ); |
| 54 |
$builder->build( |
| 55 |
{ |
| 56 |
source => 'UserPermission', |
| 57 |
value => { |
| 58 |
borrowernumber => $librarian->borrowernumber, |
| 59 |
module_bit => 25, # cash_management |
| 60 |
code => 'anonymous_refund', |
| 61 |
}, |
| 62 |
} |
| 63 |
); |
| 64 |
my $userid = $librarian->userid; |
| 65 |
|
| 66 |
# Create test patron without permission |
| 67 |
my $patron = $builder->build_object( |
| 68 |
{ |
| 69 |
class => 'Koha::Patrons', |
| 70 |
value => { flags => 0 } |
| 71 |
} |
| 72 |
); |
| 73 |
my $password2 = 'thePassword321'; |
| 74 |
$patron->set_password( { password => $password2, skip_validation => 1 } ); |
| 75 |
my $unauth_userid = $patron->userid; |
| 76 |
|
| 77 |
# Create test library |
| 78 |
my $library = $builder->build_object( { class => 'Koha::Libraries' } ); |
| 79 |
|
| 80 |
# Mock userenv for creating account lines |
| 81 |
t::lib::Mocks::mock_userenv( { patron => $librarian, branchcode => $library->branchcode } ); |
| 82 |
|
| 83 |
# Create test patrons for account lines |
| 84 |
my $patron_1 = $builder->build_object( { class => 'Koha::Patrons' } ); |
| 85 |
my $patron_2 = $builder->build_object( { class => 'Koha::Patrons' } ); |
| 86 |
|
| 87 |
# Enable credit numbers for PAYMENT type |
| 88 |
my $credit_type = Koha::Account::CreditTypes->find('PAYMENT'); |
| 89 |
$credit_type->credit_number_enabled(1)->store; |
| 90 |
|
| 91 |
# Add credits with credit numbers |
| 92 |
t::lib::Mocks::mock_preference( 'AutoCreditNumber', 'incremental' ); |
| 93 |
my $credit_1 = $patron_1->account->add_credit( |
| 94 |
{ |
| 95 |
amount => 5.00, |
| 96 |
user_id => $librarian->borrowernumber, |
| 97 |
library_id => $library->branchcode, |
| 98 |
interface => 'test', |
| 99 |
type => 'PAYMENT', |
| 100 |
} |
| 101 |
); |
| 102 |
$credit_1->discard_changes; |
| 103 |
|
| 104 |
my $credit_2 = $patron_2->account->add_credit( |
| 105 |
{ |
| 106 |
amount => 10.00, |
| 107 |
user_id => $librarian->borrowernumber, |
| 108 |
library_id => $library->branchcode, |
| 109 |
interface => 'test', |
| 110 |
type => 'PAYMENT', |
| 111 |
} |
| 112 |
); |
| 113 |
$credit_2->discard_changes; |
| 114 |
|
| 115 |
my $credit_3 = $patron_1->account->add_credit( |
| 116 |
{ |
| 117 |
amount => 7.50, |
| 118 |
user_id => $librarian->borrowernumber, |
| 119 |
library_id => $library->branchcode, |
| 120 |
interface => 'test', |
| 121 |
type => 'PAYMENT', |
| 122 |
} |
| 123 |
); |
| 124 |
$credit_3->discard_changes; |
| 125 |
|
| 126 |
# TEST: Unauthorized access (no permission) |
| 127 |
$t->get_ok("//$unauth_userid:$password2@/api/v1/account/credits")->status_is(403); |
| 128 |
|
| 129 |
# TEST: Authorized access - list all credits |
| 130 |
$t->get_ok("//$userid:$password@/api/v1/account/credits")->status_is(200) |
| 131 |
->json_is( '/0/account_line_id', $credit_1->accountlines_id ) |
| 132 |
->json_is( '/1/account_line_id', $credit_2->accountlines_id ) |
| 133 |
->json_is( '/2/account_line_id', $credit_3->accountlines_id ); |
| 134 |
|
| 135 |
# TEST: Search by credit_number using query parameter |
| 136 |
my $credit_number = $credit_1->credit_number; |
| 137 |
$t->get_ok( "//$userid:$password@/api/v1/account/credits" . "?q=" . '{"credit_number":"' . $credit_number . '"}' ) |
| 138 |
->status_is(200)->json_is( '/0/account_line_id', $credit_1->accountlines_id ) |
| 139 |
->json_is( '/0/credit_number', $credit_number ); |
| 140 |
|
| 141 |
# TEST: Search by accountlines_id |
| 142 |
$t->get_ok( "//$userid:$password@/api/v1/account/credits" . "?q=" |
| 143 |
. '{"account_line_id":' |
| 144 |
. $credit_2->accountlines_id |
| 145 |
. '}' )->status_is(200)->json_is( '/0/account_line_id', $credit_2->accountlines_id ); |
| 146 |
|
| 147 |
# TEST: Pagination |
| 148 |
$t->get_ok("//$userid:$password@/api/v1/account/credits?_per_page=1")->status_is(200) |
| 149 |
->json_is( '/0/account_line_id', $credit_1->accountlines_id )->header_is( 'X-Total-Count', 3 ); |
| 150 |
|
| 151 |
# TEST: Ordering |
| 152 |
$t->get_ok("//$userid:$password@/api/v1/account/credits?_order_by=-account_line_id")->status_is(200) |
| 153 |
->json_is( '/0/account_line_id', $credit_3->accountlines_id ) |
| 154 |
->json_is( '/1/account_line_id', $credit_2->accountlines_id ) |
| 155 |
->json_is( '/2/account_line_id', $credit_1->accountlines_id ); |
| 156 |
|
| 157 |
$schema->storage->txn_rollback; |
| 158 |
}; |
| 159 |
|
| 160 |
subtest 'list_credits() with embed tests' => sub { |
| 161 |
|
| 162 |
plan tests => 8; |
| 163 |
|
| 164 |
$schema->storage->txn_begin; |
| 165 |
|
| 166 |
# Create test patron with anonymous_refund permission |
| 167 |
my $librarian = $builder->build_object( |
| 168 |
{ |
| 169 |
class => 'Koha::Patrons', |
| 170 |
value => { flags => 0 } |
| 171 |
} |
| 172 |
); |
| 173 |
my $password = 'thePassword123'; |
| 174 |
$librarian->set_password( { password => $password, skip_validation => 1 } ); |
| 175 |
$builder->build( |
| 176 |
{ |
| 177 |
source => 'UserPermission', |
| 178 |
value => { |
| 179 |
borrowernumber => $librarian->borrowernumber, |
| 180 |
module_bit => 25, # cash_management |
| 181 |
code => 'anonymous_refund', |
| 182 |
}, |
| 183 |
} |
| 184 |
); |
| 185 |
my $userid = $librarian->userid; |
| 186 |
|
| 187 |
# Create test library |
| 188 |
my $library = $builder->build_object( { class => 'Koha::Libraries' } ); |
| 189 |
|
| 190 |
# Mock userenv for creating account lines |
| 191 |
t::lib::Mocks::mock_userenv( { patron => $librarian, branchcode => $library->branchcode } ); |
| 192 |
|
| 193 |
# Create test patron |
| 194 |
my $patron = $builder->build_object( { class => 'Koha::Patrons' } ); |
| 195 |
|
| 196 |
# Add debit |
| 197 |
my $debit = $patron->account->add_debit( |
| 198 |
{ |
| 199 |
amount => 10.00, |
| 200 |
type => 'OVERDUE', |
| 201 |
interface => 'test', |
| 202 |
user_id => $librarian->borrowernumber, |
| 203 |
library_id => $library->branchcode, |
| 204 |
} |
| 205 |
); |
| 206 |
|
| 207 |
# Add credit and apply to debit |
| 208 |
my $credit = $patron->account->add_credit( |
| 209 |
{ |
| 210 |
amount => 10.00, |
| 211 |
user_id => $librarian->borrowernumber, |
| 212 |
library_id => $library->branchcode, |
| 213 |
interface => 'test', |
| 214 |
type => 'PAYMENT', |
| 215 |
} |
| 216 |
); |
| 217 |
$credit->apply( { debits => [$debit] } ); |
| 218 |
|
| 219 |
# TEST: Embed debits |
| 220 |
$t->get_ok( "//$userid:$password@/api/v1/account/credits" . "?q=" |
| 221 |
. '{"account_line_id":' |
| 222 |
. $credit->accountlines_id |
| 223 |
. '}' => { 'x-koha-embed' => 'debits' } )->status_is(200) |
| 224 |
->json_is( '/0/account_line_id', $credit->accountlines_id )->json_has('/0/debits') |
| 225 |
->json_is( '/0/debits/0/account_line_id', $debit->accountlines_id ); |
| 226 |
|
| 227 |
# TEST: Without embed, debits should not be present |
| 228 |
$t->get_ok( |
| 229 |
"//$userid:$password@/api/v1/account/credits" . "?q=" . '{"account_line_id":' . $credit->accountlines_id . '}' ) |
| 230 |
->status_is(200)->json_hasnt('/0/debits'); |
| 231 |
|
| 232 |
$schema->storage->txn_rollback; |
| 233 |
}; |
| 234 |
|
| 235 |
subtest 'list_debits() tests' => sub { |
| 236 |
|
| 237 |
plan tests => 22; |
| 238 |
|
| 239 |
$schema->storage->txn_begin; |
| 240 |
|
| 241 |
# Create test patron with anonymous_refund permission |
| 242 |
my $librarian = $builder->build_object( |
| 243 |
{ |
| 244 |
class => 'Koha::Patrons', |
| 245 |
value => { flags => 0 } |
| 246 |
} |
| 247 |
); |
| 248 |
my $password = 'thePassword123'; |
| 249 |
$librarian->set_password( { password => $password, skip_validation => 1 } ); |
| 250 |
$builder->build( |
| 251 |
{ |
| 252 |
source => 'UserPermission', |
| 253 |
value => { |
| 254 |
borrowernumber => $librarian->borrowernumber, |
| 255 |
module_bit => 25, # cash_management |
| 256 |
code => 'anonymous_refund', |
| 257 |
}, |
| 258 |
} |
| 259 |
); |
| 260 |
my $userid = $librarian->userid; |
| 261 |
|
| 262 |
# Create test patron without permission |
| 263 |
my $patron = $builder->build_object( |
| 264 |
{ |
| 265 |
class => 'Koha::Patrons', |
| 266 |
value => { flags => 0 } |
| 267 |
} |
| 268 |
); |
| 269 |
my $password2 = 'thePassword321'; |
| 270 |
$patron->set_password( { password => $password2, skip_validation => 1 } ); |
| 271 |
my $unauth_userid = $patron->userid; |
| 272 |
|
| 273 |
# Create test library |
| 274 |
my $library = $builder->build_object( { class => 'Koha::Libraries' } ); |
| 275 |
|
| 276 |
# Mock userenv for creating account lines |
| 277 |
t::lib::Mocks::mock_userenv( { patron => $librarian, branchcode => $library->branchcode } ); |
| 278 |
|
| 279 |
# Create test patrons for account lines |
| 280 |
my $patron_1 = $builder->build_object( { class => 'Koha::Patrons' } ); |
| 281 |
my $patron_2 = $builder->build_object( { class => 'Koha::Patrons' } ); |
| 282 |
|
| 283 |
# Add debits |
| 284 |
my $debit_1 = $patron_1->account->add_debit( |
| 285 |
{ |
| 286 |
amount => 5.00, |
| 287 |
type => 'OVERDUE', |
| 288 |
interface => 'test', |
| 289 |
user_id => $librarian->borrowernumber, |
| 290 |
library_id => $library->branchcode, |
| 291 |
} |
| 292 |
); |
| 293 |
|
| 294 |
my $debit_2 = $patron_2->account->add_debit( |
| 295 |
{ |
| 296 |
amount => 10.00, |
| 297 |
type => 'LOST', |
| 298 |
interface => 'test', |
| 299 |
user_id => $librarian->borrowernumber, |
| 300 |
library_id => $library->branchcode, |
| 301 |
} |
| 302 |
); |
| 303 |
|
| 304 |
my $debit_3 = $patron_1->account->add_debit( |
| 305 |
{ |
| 306 |
amount => 7.50, |
| 307 |
type => 'OVERDUE', |
| 308 |
interface => 'test', |
| 309 |
user_id => $librarian->borrowernumber, |
| 310 |
library_id => $library->branchcode, |
| 311 |
} |
| 312 |
); |
| 313 |
|
| 314 |
# TEST: Unauthorized access (no permission) |
| 315 |
$t->get_ok("//$unauth_userid:$password2@/api/v1/account/debits")->status_is(403); |
| 316 |
|
| 317 |
# TEST: Authorized access - list all debits |
| 318 |
$t->get_ok("//$userid:$password@/api/v1/account/debits")->status_is(200) |
| 319 |
->json_is( '/0/account_line_id', $debit_1->accountlines_id ) |
| 320 |
->json_is( '/1/account_line_id', $debit_2->accountlines_id ) |
| 321 |
->json_is( '/2/account_line_id', $debit_3->accountlines_id ); |
| 322 |
|
| 323 |
# TEST: Search by accountlines_id |
| 324 |
$t->get_ok( |
| 325 |
"//$userid:$password@/api/v1/account/debits" . "?q=" . '{"account_line_id":' . $debit_2->accountlines_id . '}' ) |
| 326 |
->status_is(200)->json_is( '/0/account_line_id', $debit_2->accountlines_id ); |
| 327 |
|
| 328 |
# TEST: Search by type |
| 329 |
$t->get_ok( "//$userid:$password@/api/v1/account/debits" . "?q=" . '{"type":"LOST"}' )->status_is(200) |
| 330 |
->json_is( '/0/account_line_id', $debit_2->accountlines_id ); |
| 331 |
|
| 332 |
# TEST: Pagination |
| 333 |
$t->get_ok("//$userid:$password@/api/v1/account/debits?_per_page=1")->status_is(200) |
| 334 |
->json_is( '/0/account_line_id', $debit_1->accountlines_id )->header_is( 'X-Total-Count', 3 ); |
| 335 |
|
| 336 |
# TEST: Ordering |
| 337 |
$t->get_ok("//$userid:$password@/api/v1/account/debits?_order_by=-account_line_id")->status_is(200) |
| 338 |
->json_is( '/0/account_line_id', $debit_3->accountlines_id ) |
| 339 |
->json_is( '/1/account_line_id', $debit_2->accountlines_id ) |
| 340 |
->json_is( '/2/account_line_id', $debit_1->accountlines_id ); |
| 341 |
|
| 342 |
$schema->storage->txn_rollback; |
| 343 |
}; |