3 # This file is part of Koha.
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.
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.
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
20 use Test::More tests => 8;
22 use t::lib::TestBuilder;
35 use Koha::Biblioitems;
37 use Koha::CirculationRules;
39 my $schema = Koha::Database->new->schema;
40 my $builder = t::lib::TestBuilder->new();
42 $schema->storage->txn_begin;
44 t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 );
46 my $t = Test::Mojo->new('Koha::REST::V1');
48 my $categorycode = $builder->build({ source => 'Category' })->{categorycode};
49 my $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
50 my $branchcode2 = $builder->build({ source => 'Branch' })->{branchcode};
51 my $itemtype = $builder->build({ source => 'Itemtype' })->{itemtype};
53 # Generic password for everyone
54 my $password = 'thePassword123';
56 # User without any permissions
57 my $nopermission = $builder->build_object({
58 class => 'Koha::Patrons',
60 branchcode => $branchcode,
61 categorycode => $categorycode,
65 $nopermission->set_password( { password => $password, skip_validation => 1 } );
66 my $nopermission_userid = $nopermission->userid;
68 my $patron_1 = $builder->build_object(
70 class => 'Koha::Patrons',
72 categorycode => $categorycode,
73 branchcode => $branchcode,
74 surname => 'Test Surname',
75 flags => 80, #borrowers and reserveforothers flags
79 $patron_1->set_password( { password => $password, skip_validation => 1 } );
80 my $userid_1 = $patron_1->userid;
82 my $patron_2 = $builder->build_object(
84 class => 'Koha::Patrons',
86 categorycode => $categorycode,
87 branchcode => $branchcode,
88 surname => 'Test Surname 2',
89 flags => 16, # borrowers flag
93 $patron_2->set_password( { password => $password, skip_validation => 1 } );
94 my $userid_2 = $patron_2->userid;
96 my $patron_3 = $builder->build_object(
98 class => 'Koha::Patrons',
100 categorycode => $categorycode,
101 branchcode => $branchcode,
102 surname => 'Test Surname 3',
103 flags => 64, # reserveforothers flag
107 $patron_3->set_password( { password => $password, skip_validation => 1 } );
108 my $userid_3 = $patron_3->userid;
110 my $biblio_1 = $builder->build_sample_biblio;
111 my $item_1 = $builder->build_sample_item({ biblionumber => $biblio_1->biblionumber, itype => $itemtype });
113 my $biblio_2 = $builder->build_sample_biblio;
114 my $item_2 = $builder->build_sample_item({ biblionumber => $biblio_2->biblionumber, itype => $itemtype });
116 my $dbh = C4::Context->dbh;
117 $dbh->do('DELETE FROM reserves');
118 Koha::CirculationRules->search()->delete();
119 Koha::CirculationRules->set_rules(
121 categorycode => undef,
125 reservesallowed => 1,
126 holds_per_record => 99
131 my $reserve_id = C4::Reserves::AddReserve(
133 branchcode => $branchcode,
134 borrowernumber => $patron_1->borrowernumber,
135 biblionumber => $biblio_1->biblionumber,
137 itemnumber => $item_1->itemnumber,
141 # Add another reserve to be able to change first reserve's rank
142 my $reserve_id2 = C4::Reserves::AddReserve(
144 branchcode => $branchcode,
145 borrowernumber => $patron_2->borrowernumber,
146 biblionumber => $biblio_1->biblionumber,
148 itemnumber => $item_1->itemnumber,
152 my $suspended_until = DateTime->now->add(days => 10)->truncate( to => 'day' );
153 my $expiration_date = DateTime->now->add(days => 10)->truncate( to => 'day' );
156 patron_id => int($patron_1->borrowernumber),
157 biblio_id => int($biblio_1->biblionumber),
158 item_id => int($item_1->itemnumber),
159 pickup_library_id => $branchcode,
160 expiration_date => output_pref({ dt => $expiration_date, dateformat => 'rfc3339', dateonly => 1 }),
165 suspended_until => output_pref({ dt => $suspended_until, dateformat => 'rfc3339' }),
168 subtest "Test endpoints without authentication" => sub {
170 $t->get_ok('/api/v1/holds')
172 $t->post_ok('/api/v1/holds')
174 $t->put_ok('/api/v1/holds/0')
176 $t->delete_ok('/api/v1/holds/0')
180 subtest "Test endpoints without permission" => sub {
184 $t->get_ok( "//$nopermission_userid:$password@/api/v1/holds?patron_id=" . $patron_1->borrowernumber ) # no permission
187 $t->get_ok( "//$userid_3:$password@/api/v1/holds?patron_id=" . $patron_1->borrowernumber ) # no permission
190 $t->post_ok( "//$nopermission_userid:$password@/api/v1/holds" => json => $post_data )
193 $t->put_ok( "//$nopermission_userid:$password@/api/v1/holds/0" => json => $put_data )
196 $t->delete_ok( "//$nopermission_userid:$password@/api/v1/holds/0" )
200 subtest "Test endpoints with permission" => sub {
204 $t->get_ok( "//$userid_1:$password@/api/v1/holds" )
210 $t->get_ok( "//$userid_1:$password@/api/v1/holds?priority=2" )
212 ->json_is('/0/patron_id', $patron_2->borrowernumber)
215 # While suspended_until is date-time, it's always set to midnight.
216 my $expected_suspended_until = $suspended_until->strftime('%FT00:00:00%z');
217 substr($expected_suspended_until, -2, 0, ':');
219 $t->put_ok( "//$userid_1:$password@/api/v1/holds/$reserve_id" => json => $put_data )
221 ->json_is( '/hold_id', $reserve_id )
222 ->json_is( '/suspended_until', $expected_suspended_until )
223 ->json_is( '/priority', 2 )
224 ->json_is( '/pickup_library_id', $branchcode );
226 # Change only pickup library, everything else should remain
227 $t->put_ok( "//$userid_1:$password@/api/v1/holds/$reserve_id" => json => { pickup_library_id => $branchcode2 } )
229 ->json_is( '/hold_id', $reserve_id )
230 ->json_is( '/suspended_until', $expected_suspended_until )
231 ->json_is( '/priority', 2 )
232 ->json_is( '/pickup_library_id', $branchcode2 );
234 # Reset suspended_until, everything else should remain
235 $t->put_ok( "//$userid_1:$password@/api/v1/holds/$reserve_id" => json => { suspended_until => undef } )
237 ->json_is( '/hold_id', $reserve_id )
238 ->json_is( '/suspended_until', undef )
239 ->json_is( '/priority', 2 )
240 ->json_is( '/pickup_library_id', $branchcode2 );
242 $t->delete_ok( "//$userid_3:$password@/api/v1/holds/$reserve_id" )
245 $t->put_ok( "//$userid_3:$password@/api/v1/holds/$reserve_id" => json => $put_data )
247 ->json_has('/error');
249 $t->delete_ok( "//$userid_3:$password@/api/v1/holds/$reserve_id" )
251 ->json_has('/error');
253 $t->get_ok( "//$userid_2:$password@/api/v1/holds?patron_id=" . $patron_1->borrowernumber )
257 my $inexisting_borrowernumber = $patron_2->borrowernumber * 2;
258 $t->get_ok( "//$userid_1:$password@/api/v1/holds?patron_id=$inexisting_borrowernumber")
262 $t->delete_ok( "//$userid_3:$password@/api/v1/holds/$reserve_id2" )
265 $t->post_ok( "//$userid_3:$password@/api/v1/holds" => json => $post_data )
267 ->json_has('/hold_id');
268 # Get id from response
269 $reserve_id = $t->tx->res->json->{hold_id};
271 $t->get_ok( "//$userid_1:$password@/api/v1/holds?patron_id=" . $patron_1->borrowernumber )
273 ->json_is('/0/hold_id', $reserve_id)
274 ->json_is('/0/expiration_date', output_pref({ dt => $expiration_date, dateformat => 'rfc3339', dateonly => 1 }))
275 ->json_is('/0/pickup_library_id', $branchcode);
277 $t->post_ok( "//$userid_3:$password@/api/v1/holds" => json => $post_data )
279 ->json_like('/error', qr/itemAlreadyOnHold/);
281 $post_data->{biblionumber} = int($biblio_2->biblionumber);
282 $post_data->{itemnumber} = int($item_2->itemnumber);
284 $t->post_ok( "//$userid_3:$password@/api/v1/holds" => json => $post_data )
286 ->json_like('/error', qr/itemAlreadyOnHold/);
289 subtest 'Reserves with itemtype' => sub {
293 patron_id => int($patron_1->borrowernumber),
294 biblio_id => int($biblio_1->biblionumber),
295 pickup_library_id => $branchcode,
296 item_type => $itemtype,
299 $t->delete_ok( "//$userid_3:$password@/api/v1/holds/$reserve_id" )
302 $t->post_ok( "//$userid_3:$password@/api/v1/holds" => json => $post_data )
304 ->json_has('/hold_id');
306 $reserve_id = $t->tx->res->json->{hold_id};
308 $t->get_ok( "//$userid_1:$password@/api/v1/holds?patron_id=" . $patron_1->borrowernumber )
310 ->json_is('/0/hold_id', $reserve_id)
311 ->json_is('/0/item_type', $itemtype);
315 subtest 'test AllowHoldDateInFuture' => sub {
319 $dbh->do('DELETE FROM reserves');
321 my $future_hold_date = DateTime->now->add(days => 10)->truncate( to => 'day' );
324 patron_id => int($patron_1->borrowernumber),
325 biblio_id => int($biblio_1->biblionumber),
326 item_id => int($item_1->itemnumber),
327 pickup_library_id => $branchcode,
328 expiration_date => output_pref({ dt => $expiration_date, dateformat => 'rfc3339', dateonly => 1 }),
329 hold_date => output_pref({ dt => $future_hold_date, dateformat => 'rfc3339', dateonly => 1 }),
333 t::lib::Mocks::mock_preference( 'AllowHoldDateInFuture', 0 );
335 $t->post_ok( "//$userid_3:$password@/api/v1/holds" => json => $post_data )
337 ->json_has('/error');
339 t::lib::Mocks::mock_preference( 'AllowHoldDateInFuture', 1 );
341 $t->post_ok( "//$userid_3:$password@/api/v1/holds" => json => $post_data )
343 ->json_is('/hold_date', output_pref({ dt => $future_hold_date, dateformat => 'rfc3339', dateonly => 1 }));
346 subtest 'test AllowHoldPolicyOverride' => sub {
350 $dbh->do('DELETE FROM reserves');
352 Koha::CirculationRules->set_rules(
362 t::lib::Mocks::mock_preference( 'AllowHoldPolicyOverride', 0 );
364 $t->post_ok( "//$userid_3:$password@/api/v1/holds" => json => $post_data )
366 ->json_has('/error');
368 t::lib::Mocks::mock_preference( 'AllowHoldPolicyOverride', 1 );
370 $t->post_ok( "//$userid_3:$password@/api/v1/holds" => json => $post_data )
374 $schema->storage->txn_rollback;
376 subtest 'suspend and resume tests' => sub {
380 $schema->storage->txn_begin;
382 my $password = 'AbcdEFG123';
384 my $patron = $builder->build_object(
385 { class => 'Koha::Patrons', value => { userid => 'tomasito', flags => 1 } } );
386 $patron->set_password({ password => $password, skip_validation => 1 });
387 my $userid = $patron->userid;
390 t::lib::Mocks::mock_preference( 'HoldsLog', 0 );
391 t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 );
393 my $hold = $builder->build_object(
394 { class => 'Koha::Holds',
395 value => { suspend => 0, suspend_until => undef, waitingdate => undef }
399 ok( !$hold->is_suspended, 'Hold is not suspended' );
400 $t->post_ok( "//$userid:$password@/api/v1/holds/" . $hold->id . "/suspension" )
401 ->status_is( 201, 'Hold suspension created' );
403 $hold->discard_changes; # refresh object
405 ok( $hold->is_suspended, 'Hold is suspended' );
406 $t->json_is('/end_date', undef, 'Hold suspension has no end date');
408 my $end_date = output_pref({
409 dt => dt_from_string( undef ),
410 dateformat => 'rfc3339',
414 $t->post_ok( "//$userid:$password@/api/v1/holds/" . $hold->id . "/suspension" => json => { end_date => $end_date } );
416 $hold->discard_changes; # refresh object
418 ok( $hold->is_suspended, 'Hold is suspended' );
422 dt => dt_from_string( $hold->suspend_until ),
423 dateformat => 'rfc3339',
426 'Hold suspension has correct end date'
429 $t->delete_ok( "//$userid:$password@/api/v1/holds/" . $hold->id . "/suspension" )
430 ->status_is( 204, "Correct status when deleting a resource" )
433 # Pass a an expiration date for the suspension
434 my $date = dt_from_string()->add( days => 5 );
436 "//$userid:$password@/api/v1/holds/"
438 . "/suspension" => json => {
440 output_pref( { dt => $date, dateformat => 'rfc3339', dateonly => 1 } )
442 )->status_is( 201, 'Hold suspension created' )
443 ->json_is( '/end_date',
444 output_pref( { dt => $date, dateformat => 'rfc3339', dateonly => 1 } ) )
445 ->header_is( Location => "/api/v1/holds/" . $hold->id . "/suspension", 'The Location header is set' );
447 $t->delete_ok( "//$userid:$password@/api/v1/holds/" . $hold->id . "/suspension" )
448 ->status_is( 204, "Correct status when deleting a resource" )
451 $hold->set_waiting->discard_changes;
453 $t->post_ok( "//$userid:$password@/api/v1/holds/" . $hold->id . "/suspension" )
454 ->status_is( 400, 'Cannot suspend waiting hold' )
455 ->json_is( '/error', 'Found hold cannot be suspended. Status=W' );
457 $hold->set_waiting(1)->discard_changes;
459 $t->post_ok( "//$userid:$password@/api/v1/holds/" . $hold->id . "/suspension" )
460 ->status_is( 400, 'Cannot suspend waiting hold' )
461 ->json_is( '/error', 'Found hold cannot be suspended. Status=T' );
463 $schema->storage->txn_rollback;
466 subtest 'PUT /holds/{hold_id}/priority tests' => sub {
470 $schema->storage->txn_begin;
472 my $password = 'AbcdEFG123';
474 my $library = $builder->build_object({ class => 'Koha::Libraries' });
475 my $patron_np = $builder->build_object(
476 { class => 'Koha::Patrons', value => { flags => 0 } } );
477 $patron_np->set_password( { password => $password, skip_validation => 1 } );
478 my $userid_np = $patron_np->userid;
480 my $patron = $builder->build_object(
481 { class => 'Koha::Patrons', value => { flags => 0 } } );
482 $patron->set_password( { password => $password, skip_validation => 1 } );
483 my $userid = $patron->userid;
486 source => 'UserPermission',
488 borrowernumber => $patron->borrowernumber,
490 code => 'modify_holds_priority',
496 t::lib::Mocks::mock_preference( 'HoldsLog', 0 );
497 t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 );
499 my $biblio = $builder->build_sample_biblio;
500 my $patron_1 = $builder->build_object(
502 class => 'Koha::Patrons',
503 value => { branchcode => $library->branchcode }
506 my $patron_2 = $builder->build_object(
508 class => 'Koha::Patrons',
509 value => { branchcode => $library->branchcode }
512 my $patron_3 = $builder->build_object(
514 class => 'Koha::Patrons',
515 value => { branchcode => $library->branchcode }
519 my $hold_1 = Koha::Holds->find(
522 branchcode => $library->branchcode,
523 borrowernumber => $patron_1->borrowernumber,
524 biblionumber => $biblio->biblionumber,
529 my $hold_2 = Koha::Holds->find(
532 branchcode => $library->branchcode,
533 borrowernumber => $patron_2->borrowernumber,
534 biblionumber => $biblio->biblionumber,
539 my $hold_3 = Koha::Holds->find(
542 branchcode => $library->branchcode,
543 borrowernumber => $patron_3->borrowernumber,
544 biblionumber => $biblio->biblionumber,
550 $t->put_ok( "//$userid_np:$password@/api/v1/holds/"
552 . "/priority" => json => 1 )->status_is(403);
554 $t->put_ok( "//$userid:$password@/api/v1/holds/"
556 . "/priority" => json => 1 )->status_is(200)->json_is(1);
558 is( $hold_1->discard_changes->priority, 2, 'Priority adjusted correctly' );
559 is( $hold_2->discard_changes->priority, 3, 'Priority adjusted correctly' );
560 is( $hold_3->discard_changes->priority, 1, 'Priority adjusted correctly' );
562 $t->put_ok( "//$userid:$password@/api/v1/holds/"
564 . "/priority" => json => 3 )->status_is(200)->json_is(3);
566 is( $hold_1->discard_changes->priority, 1, 'Priority adjusted correctly' );
567 is( $hold_2->discard_changes->priority, 2, 'Priority adjusted correctly' );
568 is( $hold_3->discard_changes->priority, 3, 'Priority adjusted correctly' );
570 $schema->storage->txn_rollback;