3 # This file is part of Koha.
5 # Koha is free software; you can redistribute it and/or modify it under the
6 # terms of the GNU General Public License as published by the Free Software
7 # Foundation; either version 3 of the License, or (at your option) any later
10 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
11 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License along
15 # with Koha; if not, write to the Free Software Foundation, Inc.,
16 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
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 $itemtype = $builder->build({ source => 'Itemtype' })->{itemtype};
52 # Generic password for everyone
53 my $password = 'thePassword123';
55 # User without any permissions
56 my $nopermission = $builder->build_object({
57 class => 'Koha::Patrons',
59 branchcode => $branchcode,
60 categorycode => $categorycode,
64 $nopermission->set_password( { password => $password, skip_validation => 1 } );
65 my $nopermission_userid = $nopermission->userid;
67 my $patron_1 = $builder->build_object(
69 class => 'Koha::Patrons',
71 categorycode => $categorycode,
72 branchcode => $branchcode,
73 surname => 'Test Surname',
74 flags => 80, #borrowers and reserveforothers flags
78 $patron_1->set_password( { password => $password, skip_validation => 1 } );
79 my $userid_1 = $patron_1->userid;
81 my $patron_2 = $builder->build_object(
83 class => 'Koha::Patrons',
85 categorycode => $categorycode,
86 branchcode => $branchcode,
87 surname => 'Test Surname 2',
88 flags => 16, # borrowers flag
92 $patron_2->set_password( { password => $password, skip_validation => 1 } );
93 my $userid_2 = $patron_2->userid;
95 my $patron_3 = $builder->build_object(
97 class => 'Koha::Patrons',
99 categorycode => $categorycode,
100 branchcode => $branchcode,
101 surname => 'Test Surname 3',
102 flags => 64, # reserveforothers flag
106 $patron_3->set_password( { password => $password, skip_validation => 1 } );
107 my $userid_3 = $patron_3->userid;
109 my $biblio_1 = $builder->build_sample_biblio;
110 my $item_1 = $builder->build_sample_item({ biblionumber => $biblio_1->biblionumber, itype => $itemtype });
112 my $biblio_2 = $builder->build_sample_biblio;
113 my $item_2 = $builder->build_sample_item({ biblionumber => $biblio_2->biblionumber, itype => $itemtype });
115 my $dbh = C4::Context->dbh;
116 $dbh->do('DELETE FROM reserves');
117 Koha::CirculationRules->search()->delete();
118 Koha::CirculationRules->set_rules(
120 categorycode => undef,
124 reservesallowed => 1,
125 holds_per_record => 99
130 my $reserve_id = C4::Reserves::AddReserve(
132 branchcode => $branchcode,
133 borrowernumber => $patron_1->borrowernumber,
134 biblionumber => $biblio_1->biblionumber,
136 itemnumber => $item_1->itemnumber,
140 # Add another reserve to be able to change first reserve's rank
141 my $reserve_id2 = C4::Reserves::AddReserve(
143 branchcode => $branchcode,
144 borrowernumber => $patron_2->borrowernumber,
145 biblionumber => $biblio_1->biblionumber,
147 itemnumber => $item_1->itemnumber,
151 my $suspended_until = DateTime->now->add(days => 10)->truncate( to => 'day' );
152 my $expiration_date = DateTime->now->add(days => 10)->truncate( to => 'day' );
155 patron_id => int($patron_1->borrowernumber),
156 biblio_id => int($biblio_1->biblionumber),
157 item_id => int($item_1->itemnumber),
158 pickup_library_id => $branchcode,
159 expiration_date => output_pref({ dt => $expiration_date, dateformat => 'rfc3339', dateonly => 1 }),
164 suspended_until => output_pref({ dt => $suspended_until, dateformat => 'rfc3339' }),
167 subtest "Test endpoints without authentication" => sub {
169 $t->get_ok('/api/v1/holds')
171 $t->post_ok('/api/v1/holds')
173 $t->put_ok('/api/v1/holds/0')
175 $t->delete_ok('/api/v1/holds/0')
179 subtest "Test endpoints without permission" => sub {
183 $t->get_ok( "//$nopermission_userid:$password@/api/v1/holds?patron_id=" . $patron_1->borrowernumber ) # no permission
186 $t->get_ok( "//$userid_3:$password@/api/v1/holds?patron_id=" . $patron_1->borrowernumber ) # no permission
189 $t->post_ok( "//$nopermission_userid:$password@/api/v1/holds" => json => $post_data )
192 $t->put_ok( "//$nopermission_userid:$password@/api/v1/holds/0" => json => $put_data )
195 $t->delete_ok( "//$nopermission_userid:$password@/api/v1/holds/0" )
199 subtest "Test endpoints with permission" => sub {
203 $t->get_ok( "//$userid_1:$password@/api/v1/holds" )
209 $t->get_ok( "//$userid_1:$password@/api/v1/holds?priority=2" )
211 ->json_is('/0/patron_id', $patron_2->borrowernumber)
214 $t->put_ok( "//$userid_1:$password@/api/v1/holds/$reserve_id" => json => $put_data )
216 ->json_is( '/hold_id', $reserve_id )
217 ->json_is( '/suspended_until', output_pref({ dt => $suspended_until, dateformat => 'rfc3339' }) )
218 ->json_is( '/priority', 2 );
220 $t->delete_ok( "//$userid_3:$password@/api/v1/holds/$reserve_id" )
223 $t->put_ok( "//$userid_3:$password@/api/v1/holds/$reserve_id" => json => $put_data )
225 ->json_has('/error');
227 $t->delete_ok( "//$userid_3:$password@/api/v1/holds/$reserve_id" )
229 ->json_has('/error');
231 $t->get_ok( "//$userid_2:$password@/api/v1/holds?patron_id=" . $patron_1->borrowernumber )
235 my $inexisting_borrowernumber = $patron_2->borrowernumber * 2;
236 $t->get_ok( "//$userid_1:$password@/api/v1/holds?patron_id=$inexisting_borrowernumber")
240 $t->delete_ok( "//$userid_3:$password@/api/v1/holds/$reserve_id2" )
243 $t->post_ok( "//$userid_3:$password@/api/v1/holds" => json => $post_data )
245 ->json_has('/hold_id');
246 # Get id from response
247 $reserve_id = $t->tx->res->json->{hold_id};
249 $t->get_ok( "//$userid_1:$password@/api/v1/holds?patron_id=" . $patron_1->borrowernumber )
251 ->json_is('/0/hold_id', $reserve_id)
252 ->json_is('/0/expiration_date', output_pref({ dt => $expiration_date, dateformat => 'rfc3339', dateonly => 1 }))
253 ->json_is('/0/pickup_library_id', $branchcode);
255 $t->post_ok( "//$userid_3:$password@/api/v1/holds" => json => $post_data )
257 ->json_like('/error', qr/itemAlreadyOnHold/);
259 $post_data->{biblionumber} = int($biblio_2->biblionumber);
260 $post_data->{itemnumber} = int($item_2->itemnumber);
262 $t->post_ok( "//$userid_3:$password@/api/v1/holds" => json => $post_data )
264 ->json_like('/error', qr/itemAlreadyOnHold/);
267 subtest 'Reserves with itemtype' => sub {
271 patron_id => int($patron_1->borrowernumber),
272 biblio_id => int($biblio_1->biblionumber),
273 pickup_library_id => $branchcode,
274 item_type => $itemtype,
277 $t->delete_ok( "//$userid_3:$password@/api/v1/holds/$reserve_id" )
280 $t->post_ok( "//$userid_3:$password@/api/v1/holds" => json => $post_data )
282 ->json_has('/hold_id');
284 $reserve_id = $t->tx->res->json->{hold_id};
286 $t->get_ok( "//$userid_1:$password@/api/v1/holds?patron_id=" . $patron_1->borrowernumber )
288 ->json_is('/0/hold_id', $reserve_id)
289 ->json_is('/0/item_type', $itemtype);
293 subtest 'test AllowHoldDateInFuture' => sub {
297 $dbh->do('DELETE FROM reserves');
299 my $future_hold_date = DateTime->now->add(days => 10)->truncate( to => 'day' );
302 patron_id => int($patron_1->borrowernumber),
303 biblio_id => int($biblio_1->biblionumber),
304 item_id => int($item_1->itemnumber),
305 pickup_library_id => $branchcode,
306 expiration_date => output_pref({ dt => $expiration_date, dateformat => 'rfc3339', dateonly => 1 }),
307 hold_date => output_pref({ dt => $future_hold_date, dateformat => 'rfc3339', dateonly => 1 }),
311 t::lib::Mocks::mock_preference( 'AllowHoldDateInFuture', 0 );
313 $t->post_ok( "//$userid_3:$password@/api/v1/holds" => json => $post_data )
315 ->json_has('/error');
317 t::lib::Mocks::mock_preference( 'AllowHoldDateInFuture', 1 );
319 $t->post_ok( "//$userid_3:$password@/api/v1/holds" => json => $post_data )
321 ->json_is('/hold_date', output_pref({ dt => $future_hold_date, dateformat => 'rfc3339', dateonly => 1 }));
324 subtest 'test AllowHoldPolicyOverride' => sub {
328 $dbh->do('DELETE FROM reserves');
330 Koha::CirculationRules->set_rules(
340 t::lib::Mocks::mock_preference( 'AllowHoldPolicyOverride', 0 );
342 $t->post_ok( "//$userid_3:$password@/api/v1/holds" => json => $post_data )
344 ->json_has('/error');
346 t::lib::Mocks::mock_preference( 'AllowHoldPolicyOverride', 1 );
348 $t->post_ok( "//$userid_3:$password@/api/v1/holds" => json => $post_data )
352 $schema->storage->txn_rollback;
354 subtest 'suspend and resume tests' => sub {
358 $schema->storage->txn_begin;
360 my $password = 'AbcdEFG123';
362 my $patron = $builder->build_object(
363 { class => 'Koha::Patrons', value => { userid => 'tomasito', flags => 1 } } );
364 $patron->set_password({ password => $password, skip_validation => 1 });
365 my $userid = $patron->userid;
368 t::lib::Mocks::mock_preference( 'HoldsLog', 0 );
369 t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 );
371 my $hold = $builder->build_object(
372 { class => 'Koha::Holds',
373 value => { suspend => 0, suspend_until => undef, waitingdate => undef }
377 ok( !$hold->is_suspended, 'Hold is not suspended' );
378 $t->post_ok( "//$userid:$password@/api/v1/holds/" . $hold->id . "/suspension" )
379 ->status_is( 201, 'Hold suspension created' );
381 $hold->discard_changes; # refresh object
383 ok( $hold->is_suspended, 'Hold is suspended' );
387 { dt => dt_from_string( $hold->suspend_until ),
388 dateformat => 'rfc3339',
394 $t->delete_ok( "//$userid:$password@/api/v1/holds/" . $hold->id . "/suspension" )
395 ->status_is( 204, "Correct status when deleting a resource" )
398 # Pass a an expiration date for the suspension
399 my $date = dt_from_string()->add( days => 5 );
401 "//$userid:$password@/api/v1/holds/"
403 . "/suspension" => json => {
405 output_pref( { dt => $date, dateformat => 'rfc3339', dateonly => 1 } )
407 )->status_is( 201, 'Hold suspension created' )
408 ->json_is( '/end_date',
409 output_pref( { dt => $date, dateformat => 'rfc3339', dateonly => 1 } ) )
410 ->header_is( Location => "/api/v1/holds/" . $hold->id . "/suspension", 'The Location header is set' );
412 $t->delete_ok( "//$userid:$password@/api/v1/holds/" . $hold->id . "/suspension" )
413 ->status_is( 204, "Correct status when deleting a resource" )
416 $hold->set_waiting->discard_changes;
418 $t->post_ok( "//$userid:$password@/api/v1/holds/" . $hold->id . "/suspension" )
419 ->status_is( 400, 'Cannot suspend waiting hold' )
420 ->json_is( '/error', 'Found hold cannot be suspended. Status=W' );
422 $hold->set_waiting(1)->discard_changes;
424 $t->post_ok( "//$userid:$password@/api/v1/holds/" . $hold->id . "/suspension" )
425 ->status_is( 400, 'Cannot suspend waiting hold' )
426 ->json_is( '/error', 'Found hold cannot be suspended. Status=T' );
428 $schema->storage->txn_rollback;
431 subtest 'PUT /holds/{hold_id}/priority tests' => sub {
435 $schema->storage->txn_begin;
437 my $password = 'AbcdEFG123';
439 my $library = $builder->build_object({ class => 'Koha::Libraries' });
440 my $patron_np = $builder->build_object(
441 { class => 'Koha::Patrons', value => { flags => 0 } } );
442 $patron_np->set_password( { password => $password, skip_validation => 1 } );
443 my $userid_np = $patron_np->userid;
445 my $patron = $builder->build_object(
446 { class => 'Koha::Patrons', value => { flags => 0 } } );
447 $patron->set_password( { password => $password, skip_validation => 1 } );
448 my $userid = $patron->userid;
451 source => 'UserPermission',
453 borrowernumber => $patron->borrowernumber,
455 code => 'modify_holds_priority',
461 t::lib::Mocks::mock_preference( 'HoldsLog', 0 );
462 t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 );
464 my $biblio = $builder->build_sample_biblio;
465 my $patron_1 = $builder->build_object(
467 class => 'Koha::Patrons',
468 value => { branchcode => $library->branchcode }
471 my $patron_2 = $builder->build_object(
473 class => 'Koha::Patrons',
474 value => { branchcode => $library->branchcode }
477 my $patron_3 = $builder->build_object(
479 class => 'Koha::Patrons',
480 value => { branchcode => $library->branchcode }
484 my $hold_1 = Koha::Holds->find(
487 branchcode => $library->branchcode,
488 borrowernumber => $patron_1->borrowernumber,
489 biblionumber => $biblio->biblionumber,
494 my $hold_2 = Koha::Holds->find(
497 branchcode => $library->branchcode,
498 borrowernumber => $patron_2->borrowernumber,
499 biblionumber => $biblio->biblionumber,
504 my $hold_3 = Koha::Holds->find(
507 branchcode => $library->branchcode,
508 borrowernumber => $patron_3->borrowernumber,
509 biblionumber => $biblio->biblionumber,
515 $t->put_ok( "//$userid_np:$password@/api/v1/holds/"
517 . "/priority" => json => 1 )->status_is(403);
519 $t->put_ok( "//$userid:$password@/api/v1/holds/"
521 . "/priority" => json => 1 )->status_is(200)->json_is(1);
523 is( $hold_1->discard_changes->priority, 2, 'Priority adjusted correctly' );
524 is( $hold_2->discard_changes->priority, 3, 'Priority adjusted correctly' );
525 is( $hold_3->discard_changes->priority, 1, 'Priority adjusted correctly' );
527 $t->put_ok( "//$userid:$password@/api/v1/holds/"
529 . "/priority" => json => 3 )->status_is(200)->json_is(3);
531 is( $hold_1->discard_changes->priority, 1, 'Priority adjusted correctly' );
532 is( $hold_2->discard_changes->priority, 2, 'Priority adjusted correctly' );
533 is( $hold_3->discard_changes->priority, 3, 'Priority adjusted correctly' );
535 $schema->storage->txn_rollback;