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 $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 # While suspended_until is date-time, it's always set to midnight.
215 my $expected_suspended_until = $suspended_until->strftime('%FT00:00:00%z');
216 substr($expected_suspended_until, -2, 0, ':');
218 $t->put_ok( "//$userid_1:$password@/api/v1/holds/$reserve_id" => json => $put_data )
220 ->json_is( '/hold_id', $reserve_id )
221 ->json_is( '/suspended_until', $expected_suspended_until )
222 ->json_is( '/priority', 2 );
224 $t->delete_ok( "//$userid_3:$password@/api/v1/holds/$reserve_id" )
227 $t->put_ok( "//$userid_3:$password@/api/v1/holds/$reserve_id" => json => $put_data )
229 ->json_has('/error');
231 $t->delete_ok( "//$userid_3:$password@/api/v1/holds/$reserve_id" )
233 ->json_has('/error');
235 $t->get_ok( "//$userid_2:$password@/api/v1/holds?patron_id=" . $patron_1->borrowernumber )
239 my $inexisting_borrowernumber = $patron_2->borrowernumber * 2;
240 $t->get_ok( "//$userid_1:$password@/api/v1/holds?patron_id=$inexisting_borrowernumber")
244 $t->delete_ok( "//$userid_3:$password@/api/v1/holds/$reserve_id2" )
247 $t->post_ok( "//$userid_3:$password@/api/v1/holds" => json => $post_data )
249 ->json_has('/hold_id');
250 # Get id from response
251 $reserve_id = $t->tx->res->json->{hold_id};
253 $t->get_ok( "//$userid_1:$password@/api/v1/holds?patron_id=" . $patron_1->borrowernumber )
255 ->json_is('/0/hold_id', $reserve_id)
256 ->json_is('/0/expiration_date', output_pref({ dt => $expiration_date, dateformat => 'rfc3339', dateonly => 1 }))
257 ->json_is('/0/pickup_library_id', $branchcode);
259 $t->post_ok( "//$userid_3:$password@/api/v1/holds" => json => $post_data )
261 ->json_like('/error', qr/itemAlreadyOnHold/);
263 $post_data->{biblionumber} = int($biblio_2->biblionumber);
264 $post_data->{itemnumber} = int($item_2->itemnumber);
266 $t->post_ok( "//$userid_3:$password@/api/v1/holds" => json => $post_data )
268 ->json_like('/error', qr/itemAlreadyOnHold/);
271 subtest 'Reserves with itemtype' => sub {
275 patron_id => int($patron_1->borrowernumber),
276 biblio_id => int($biblio_1->biblionumber),
277 pickup_library_id => $branchcode,
278 item_type => $itemtype,
281 $t->delete_ok( "//$userid_3:$password@/api/v1/holds/$reserve_id" )
284 $t->post_ok( "//$userid_3:$password@/api/v1/holds" => json => $post_data )
286 ->json_has('/hold_id');
288 $reserve_id = $t->tx->res->json->{hold_id};
290 $t->get_ok( "//$userid_1:$password@/api/v1/holds?patron_id=" . $patron_1->borrowernumber )
292 ->json_is('/0/hold_id', $reserve_id)
293 ->json_is('/0/item_type', $itemtype);
297 subtest 'test AllowHoldDateInFuture' => sub {
301 $dbh->do('DELETE FROM reserves');
303 my $future_hold_date = DateTime->now->add(days => 10)->truncate( to => 'day' );
306 patron_id => int($patron_1->borrowernumber),
307 biblio_id => int($biblio_1->biblionumber),
308 item_id => int($item_1->itemnumber),
309 pickup_library_id => $branchcode,
310 expiration_date => output_pref({ dt => $expiration_date, dateformat => 'rfc3339', dateonly => 1 }),
311 hold_date => output_pref({ dt => $future_hold_date, dateformat => 'rfc3339', dateonly => 1 }),
315 t::lib::Mocks::mock_preference( 'AllowHoldDateInFuture', 0 );
317 $t->post_ok( "//$userid_3:$password@/api/v1/holds" => json => $post_data )
319 ->json_has('/error');
321 t::lib::Mocks::mock_preference( 'AllowHoldDateInFuture', 1 );
323 $t->post_ok( "//$userid_3:$password@/api/v1/holds" => json => $post_data )
325 ->json_is('/hold_date', output_pref({ dt => $future_hold_date, dateformat => 'rfc3339', dateonly => 1 }));
328 subtest 'test AllowHoldPolicyOverride' => sub {
332 $dbh->do('DELETE FROM reserves');
334 Koha::CirculationRules->set_rules(
344 t::lib::Mocks::mock_preference( 'AllowHoldPolicyOverride', 0 );
346 $t->post_ok( "//$userid_3:$password@/api/v1/holds" => json => $post_data )
348 ->json_has('/error');
350 t::lib::Mocks::mock_preference( 'AllowHoldPolicyOverride', 1 );
352 $t->post_ok( "//$userid_3:$password@/api/v1/holds" => json => $post_data )
356 $schema->storage->txn_rollback;
358 subtest 'suspend and resume tests' => sub {
362 $schema->storage->txn_begin;
364 my $password = 'AbcdEFG123';
366 my $patron = $builder->build_object(
367 { class => 'Koha::Patrons', value => { userid => 'tomasito', flags => 1 } } );
368 $patron->set_password({ password => $password, skip_validation => 1 });
369 my $userid = $patron->userid;
372 t::lib::Mocks::mock_preference( 'HoldsLog', 0 );
373 t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 );
375 my $hold = $builder->build_object(
376 { class => 'Koha::Holds',
377 value => { suspend => 0, suspend_until => undef, waitingdate => undef }
381 ok( !$hold->is_suspended, 'Hold is not suspended' );
382 $t->post_ok( "//$userid:$password@/api/v1/holds/" . $hold->id . "/suspension" )
383 ->status_is( 201, 'Hold suspension created' );
385 $hold->discard_changes; # refresh object
387 ok( $hold->is_suspended, 'Hold is suspended' );
391 { dt => dt_from_string( $hold->suspend_until ),
392 dateformat => 'rfc3339',
398 $t->delete_ok( "//$userid:$password@/api/v1/holds/" . $hold->id . "/suspension" )
399 ->status_is( 204, "Correct status when deleting a resource" )
402 # Pass a an expiration date for the suspension
403 my $date = dt_from_string()->add( days => 5 );
405 "//$userid:$password@/api/v1/holds/"
407 . "/suspension" => json => {
409 output_pref( { dt => $date, dateformat => 'rfc3339', dateonly => 1 } )
411 )->status_is( 201, 'Hold suspension created' )
412 ->json_is( '/end_date',
413 output_pref( { dt => $date, dateformat => 'rfc3339', dateonly => 1 } ) )
414 ->header_is( Location => "/api/v1/holds/" . $hold->id . "/suspension", 'The Location header is set' );
416 $t->delete_ok( "//$userid:$password@/api/v1/holds/" . $hold->id . "/suspension" )
417 ->status_is( 204, "Correct status when deleting a resource" )
420 $hold->set_waiting->discard_changes;
422 $t->post_ok( "//$userid:$password@/api/v1/holds/" . $hold->id . "/suspension" )
423 ->status_is( 400, 'Cannot suspend waiting hold' )
424 ->json_is( '/error', 'Found hold cannot be suspended. Status=W' );
426 $hold->set_waiting(1)->discard_changes;
428 $t->post_ok( "//$userid:$password@/api/v1/holds/" . $hold->id . "/suspension" )
429 ->status_is( 400, 'Cannot suspend waiting hold' )
430 ->json_is( '/error', 'Found hold cannot be suspended. Status=T' );
432 $schema->storage->txn_rollback;
435 subtest 'PUT /holds/{hold_id}/priority tests' => sub {
439 $schema->storage->txn_begin;
441 my $password = 'AbcdEFG123';
443 my $library = $builder->build_object({ class => 'Koha::Libraries' });
444 my $patron_np = $builder->build_object(
445 { class => 'Koha::Patrons', value => { flags => 0 } } );
446 $patron_np->set_password( { password => $password, skip_validation => 1 } );
447 my $userid_np = $patron_np->userid;
449 my $patron = $builder->build_object(
450 { class => 'Koha::Patrons', value => { flags => 0 } } );
451 $patron->set_password( { password => $password, skip_validation => 1 } );
452 my $userid = $patron->userid;
455 source => 'UserPermission',
457 borrowernumber => $patron->borrowernumber,
459 code => 'modify_holds_priority',
465 t::lib::Mocks::mock_preference( 'HoldsLog', 0 );
466 t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 );
468 my $biblio = $builder->build_sample_biblio;
469 my $patron_1 = $builder->build_object(
471 class => 'Koha::Patrons',
472 value => { branchcode => $library->branchcode }
475 my $patron_2 = $builder->build_object(
477 class => 'Koha::Patrons',
478 value => { branchcode => $library->branchcode }
481 my $patron_3 = $builder->build_object(
483 class => 'Koha::Patrons',
484 value => { branchcode => $library->branchcode }
488 my $hold_1 = Koha::Holds->find(
491 branchcode => $library->branchcode,
492 borrowernumber => $patron_1->borrowernumber,
493 biblionumber => $biblio->biblionumber,
498 my $hold_2 = Koha::Holds->find(
501 branchcode => $library->branchcode,
502 borrowernumber => $patron_2->borrowernumber,
503 biblionumber => $biblio->biblionumber,
508 my $hold_3 = Koha::Holds->find(
511 branchcode => $library->branchcode,
512 borrowernumber => $patron_3->borrowernumber,
513 biblionumber => $biblio->biblionumber,
519 $t->put_ok( "//$userid_np:$password@/api/v1/holds/"
521 . "/priority" => json => 1 )->status_is(403);
523 $t->put_ok( "//$userid:$password@/api/v1/holds/"
525 . "/priority" => json => 1 )->status_is(200)->json_is(1);
527 is( $hold_1->discard_changes->priority, 2, 'Priority adjusted correctly' );
528 is( $hold_2->discard_changes->priority, 3, 'Priority adjusted correctly' );
529 is( $hold_3->discard_changes->priority, 1, 'Priority adjusted correctly' );
531 $t->put_ok( "//$userid:$password@/api/v1/holds/"
533 . "/priority" => json => 3 )->status_is(200)->json_is(3);
535 is( $hold_1->discard_changes->priority, 1, 'Priority adjusted correctly' );
536 is( $hold_2->discard_changes->priority, 2, 'Priority adjusted correctly' );
537 is( $hold_3->discard_changes->priority, 3, 'Priority adjusted correctly' );
539 $schema->storage->txn_rollback;