|
Lines 17-23
Link Here
|
| 17 |
|
17 |
|
| 18 |
use Modern::Perl; |
18 |
use Modern::Perl; |
| 19 |
|
19 |
|
| 20 |
use Test::More tests => 1; |
20 |
use Test::More tests => 2; |
| 21 |
use Test::Mojo; |
21 |
use Test::Mojo; |
| 22 |
|
22 |
|
| 23 |
use t::lib::TestBuilder; |
23 |
use t::lib::TestBuilder; |
|
Lines 269-277
subtest 'list_rules() tests' => sub {
Link Here
|
| 269 |
|
269 |
|
| 270 |
# First (and only) rule set should match branchcode, default, default. |
270 |
# First (and only) rule set should match branchcode, default, default. |
| 271 |
if ( $index == 0 ) { |
271 |
if ( $index == 0 ) { |
| 272 |
ok( $pointer->get('/context/library_id') eq $branchcode |
272 |
ok( |
|
|
273 |
$pointer->get('/context/library_id') eq $branchcode |
| 273 |
&& $pointer->get('/context/item_type_id') eq '*' |
274 |
&& $pointer->get('/context/item_type_id') eq '*' |
| 274 |
&& $pointer->get('/context/patron_category_id') eq '*', "Branchcode rule set returned when filtered" ); |
275 |
&& $pointer->get('/context/patron_category_id') eq '*', |
|
|
276 |
"Branchcode rule set returned when filtered" |
| 277 |
); |
| 275 |
} |
278 |
} |
| 276 |
|
279 |
|
| 277 |
# Iterate over the list of expected keys for each hash |
280 |
# Iterate over the list of expected keys for each hash |
|
Lines 285-287
subtest 'list_rules() tests' => sub {
Link Here
|
| 285 |
}; |
288 |
}; |
| 286 |
$schema->storage->txn_rollback; |
289 |
$schema->storage->txn_rollback; |
| 287 |
}; |
290 |
}; |
| 288 |
- |
291 |
|
|
|
292 |
subtest 'set_rules() tests' => sub { |
| 293 |
plan tests => 23; |
| 294 |
|
| 295 |
$schema->storage->txn_begin; |
| 296 |
|
| 297 |
my $categorycode = $builder->build( { source => 'Category' } )->{'categorycode'}; |
| 298 |
my $itemtype = $builder->build( { source => 'Itemtype' } )->{'itemtype'}; |
| 299 |
my $branchcode = $builder->build( { source => 'Branch' } )->{'branchcode'}; |
| 300 |
Koha::CirculationRules->delete; |
| 301 |
|
| 302 |
my $librarian = $builder->build_object( |
| 303 |
{ |
| 304 |
class => 'Koha::Patrons', |
| 305 |
value => { flags => 2 } # circulate |
| 306 |
} |
| 307 |
); |
| 308 |
my $password = 'thePassword123'; |
| 309 |
$librarian->set_password( { password => $password, skip_validation => 1 } ); |
| 310 |
my $userid = $librarian->userid; |
| 311 |
|
| 312 |
my $patron = $builder->build_object( |
| 313 |
{ |
| 314 |
class => 'Koha::Patrons', |
| 315 |
value => { flags => 0 } |
| 316 |
} |
| 317 |
); |
| 318 |
|
| 319 |
$patron->set_password( { password => $password, skip_validation => 1 } ); |
| 320 |
my $unauth_userid = $patron->userid; |
| 321 |
|
| 322 |
## Authorized user tests |
| 323 |
note("Authorized user setting rules"); |
| 324 |
|
| 325 |
my $rules_to_set = { |
| 326 |
context => { |
| 327 |
library_id => $branchcode, |
| 328 |
patron_category_id => $categorycode, |
| 329 |
item_type_id => $itemtype, |
| 330 |
}, |
| 331 |
fine => 5, |
| 332 |
finedays => 7, |
| 333 |
}; |
| 334 |
|
| 335 |
$t->put_ok( "//$userid:$password@/api/v1/circulation_rules" => json => $rules_to_set )->status_is(200); |
| 336 |
|
| 337 |
# Verify the rules were set |
| 338 |
my $json = $t->tx->res->json; |
| 339 |
is( $json->{fine}, 5, "Fine rule set correctly" ); |
| 340 |
is( $json->{finedays}, 7, "Finedays rule set correctly" ); |
| 341 |
|
| 342 |
# Invalid item_type_id |
| 343 |
note("Invalid item_type_id"); |
| 344 |
$rules_to_set->{context}->{item_type_id} = 'invalid_itemtype'; |
| 345 |
$t->put_ok( "//$userid:$password@/api/v1/circulation_rules" => json => $rules_to_set )->status_is(400) |
| 346 |
->json_is( '/error_code' => 'invalid_parameter_value', "Handled invalid item_type_id" ); |
| 347 |
|
| 348 |
# Invalid library_id |
| 349 |
note("Invalid library_id"); |
| 350 |
$rules_to_set->{context}->{item_type_id} = $itemtype; |
| 351 |
$rules_to_set->{context}->{library_id} = 'invalid_library'; |
| 352 |
$t->put_ok( "//$userid:$password@/api/v1/circulation_rules" => json => $rules_to_set )->status_is(400) |
| 353 |
->json_is( '/error_code' => 'invalid_parameter_value', "Handled invalid library_id" ); |
| 354 |
|
| 355 |
# Invalid patron_category_id |
| 356 |
note("Invalid patron_category_id"); |
| 357 |
$rules_to_set->{context}->{library_id} = $branchcode; |
| 358 |
$rules_to_set->{context}->{patron_category_id} = 'invalid_category'; |
| 359 |
$t->put_ok( "//$userid:$password@/api/v1/circulation_rules" => json => $rules_to_set )->status_is(400) |
| 360 |
->json_is( '/error_code' => 'invalid_parameter_value', "Handled invalid patron_category_id" ); |
| 361 |
|
| 362 |
# Unauthorized user tests |
| 363 |
note("Unauthorized user trying to set rules"); |
| 364 |
$t->put_ok( "//$unauth_userid:$password@/api/v1/circulation_rules" => json => $rules_to_set )->status_is(403); |
| 365 |
|
| 366 |
# Reset to valid context |
| 367 |
$rules_to_set->{context}->{patron_category_id} = $categorycode; |
| 368 |
|
| 369 |
# Updating existing rules |
| 370 |
note("Updating existing rules"); |
| 371 |
$rules_to_set->{fine} = 10; |
| 372 |
$t->put_ok( "//$userid:$password@/api/v1/circulation_rules" => json => $rules_to_set )->status_is(200); |
| 373 |
|
| 374 |
# Verify the rules were updated |
| 375 |
$json = $t->tx->res->json; |
| 376 |
is( $json->{fine}, 10, "Fine rule updated correctly" ); |
| 377 |
is( $json->{finedays}, 7, "Finedays rule remains the same" ); |
| 378 |
|
| 379 |
# Setting rules with '*' context |
| 380 |
note("Setting rules with '*' context"); |
| 381 |
$rules_to_set->{context}->{library_id} = '*'; |
| 382 |
$rules_to_set->{context}->{patron_category_id} = '*'; |
| 383 |
$rules_to_set->{context}->{item_type_id} = '*'; |
| 384 |
$t->put_ok( "//$userid:$password@/api/v1/circulation_rules" => json => $rules_to_set )->status_is(200); |
| 385 |
|
| 386 |
# Verify the rules were set for wildcard context |
| 387 |
$json = $t->tx->res->json; |
| 388 |
is( $json->{fine}, 10, "Fine rule set correctly for wildcard context" ); |
| 389 |
is( $json->{finedays}, 7, "Finedays rule set correctly for wildcard context" ); |
| 390 |
|
| 391 |
$schema->storage->txn_rollback; |
| 392 |
}; |