|
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 285-287
subtest 'list_rules() tests' => sub {
Link Here
|
| 285 |
}; |
285 |
}; |
| 286 |
$schema->storage->txn_rollback; |
286 |
$schema->storage->txn_rollback; |
| 287 |
}; |
287 |
}; |
| 288 |
- |
288 |
|
|
|
289 |
subtest 'set_rules() tests' => sub { |
| 290 |
plan tests => 23; |
| 291 |
|
| 292 |
$schema->storage->txn_begin; |
| 293 |
|
| 294 |
my $categorycode = $builder->build( { source => 'Category' } )->{'categorycode'}; |
| 295 |
my $itemtype = $builder->build( { source => 'Itemtype' } )->{'itemtype'}; |
| 296 |
my $branchcode = $builder->build( { source => 'Branch' } )->{'branchcode'}; |
| 297 |
Koha::CirculationRules->delete; |
| 298 |
|
| 299 |
my $librarian = $builder->build_object( |
| 300 |
{ |
| 301 |
class => 'Koha::Patrons', |
| 302 |
value => { flags => 2 } # circulate |
| 303 |
} |
| 304 |
); |
| 305 |
my $password = 'thePassword123'; |
| 306 |
$librarian->set_password( { password => $password, skip_validation => 1 } ); |
| 307 |
my $userid = $librarian->userid; |
| 308 |
|
| 309 |
my $patron = $builder->build_object( |
| 310 |
{ |
| 311 |
class => 'Koha::Patrons', |
| 312 |
value => { flags => 0 } |
| 313 |
} |
| 314 |
); |
| 315 |
|
| 316 |
$patron->set_password( { password => $password, skip_validation => 1 } ); |
| 317 |
my $unauth_userid = $patron->userid; |
| 318 |
|
| 319 |
## Authorized user tests |
| 320 |
note("Authorized user setting rules"); |
| 321 |
|
| 322 |
my $rules_to_set = { |
| 323 |
context => { |
| 324 |
library_id => $branchcode, |
| 325 |
patron_category_id => $categorycode, |
| 326 |
item_type_id => $itemtype, |
| 327 |
}, |
| 328 |
fine => 5, |
| 329 |
finedays => 7, |
| 330 |
}; |
| 331 |
|
| 332 |
$t->put_ok( "//$userid:$password@/api/v1/circulation_rules" => json => $rules_to_set )->status_is(200); |
| 333 |
|
| 334 |
# Verify the rules were set |
| 335 |
my $json = $t->tx->res->json; |
| 336 |
is( $json->{fine}, 5, "Fine rule set correctly" ); |
| 337 |
is( $json->{finedays}, 7, "Finedays rule set correctly" ); |
| 338 |
|
| 339 |
# Invalid item_type_id |
| 340 |
note("Invalid item_type_id"); |
| 341 |
$rules_to_set->{context}->{item_type_id} = 'invalid_itemtype'; |
| 342 |
$t->put_ok( "//$userid:$password@/api/v1/circulation_rules" => json => $rules_to_set )->status_is(400) |
| 343 |
->json_is( '/error_code' => 'invalid_parameter_value', "Handled invalid item_type_id" ); |
| 344 |
|
| 345 |
# Invalid library_id |
| 346 |
note("Invalid library_id"); |
| 347 |
$rules_to_set->{context}->{item_type_id} = $itemtype; |
| 348 |
$rules_to_set->{context}->{library_id} = 'invalid_library'; |
| 349 |
$t->put_ok( "//$userid:$password@/api/v1/circulation_rules" => json => $rules_to_set )->status_is(400) |
| 350 |
->json_is( '/error_code' => 'invalid_parameter_value', "Handled invalid library_id" ); |
| 351 |
|
| 352 |
# Invalid patron_category_id |
| 353 |
note("Invalid patron_category_id"); |
| 354 |
$rules_to_set->{context}->{library_id} = $branchcode; |
| 355 |
$rules_to_set->{context}->{patron_category_id} = 'invalid_category'; |
| 356 |
$t->put_ok( "//$userid:$password@/api/v1/circulation_rules" => json => $rules_to_set )->status_is(400) |
| 357 |
->json_is( '/error_code' => 'invalid_parameter_value', "Handled invalid patron_category_id" ); |
| 358 |
|
| 359 |
# Unauthorized user tests |
| 360 |
note("Unauthorized user trying to set rules"); |
| 361 |
$t->put_ok( "//$unauth_userid:$password@/api/v1/circulation_rules" => json => $rules_to_set )->status_is(403); |
| 362 |
|
| 363 |
# Reset to valid context |
| 364 |
$rules_to_set->{context}->{patron_category_id} = $categorycode; |
| 365 |
|
| 366 |
# Updating existing rules |
| 367 |
note("Updating existing rules"); |
| 368 |
$rules_to_set->{fine} = 10; |
| 369 |
$t->put_ok( "//$userid:$password@/api/v1/circulation_rules" => json => $rules_to_set )->status_is(200); |
| 370 |
|
| 371 |
# Verify the rules were updated |
| 372 |
$json = $t->tx->res->json; |
| 373 |
is( $json->{fine}, 10, "Fine rule updated correctly" ); |
| 374 |
is( $json->{finedays}, 7, "Finedays rule remains the same" ); |
| 375 |
|
| 376 |
# Setting rules with '*' context |
| 377 |
note("Setting rules with '*' context"); |
| 378 |
$rules_to_set->{context}->{library_id} = '*'; |
| 379 |
$rules_to_set->{context}->{patron_category_id} = '*'; |
| 380 |
$rules_to_set->{context}->{item_type_id} = '*'; |
| 381 |
$t->put_ok( "//$userid:$password@/api/v1/circulation_rules" => json => $rules_to_set )->status_is(200); |
| 382 |
|
| 383 |
# Verify the rules were set for wildcard context |
| 384 |
$json = $t->tx->res->json; |
| 385 |
is( $json->{fine}, 10, "Fine rule set correctly for wildcard context" ); |
| 386 |
is( $json->{finedays}, 7, "Finedays rule set correctly for wildcard context" ); |
| 387 |
|
| 388 |
$schema->storage->txn_rollback; |
| 389 |
}; |