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 $dbh->do('DELETE FROM issuingrules');
119 INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed)
121 }, {}, '*', '*', '*', 1);
123 my $reserve_id = C4
::Reserves
::AddReserve
($branchcode, $patron_1->borrowernumber,
124 $biblio_1->biblionumber, undef, 1, undef, undef, undef, '', $item_1->itemnumber);
126 # Add another reserve to be able to change first reserve's rank
127 my $reserve_id2 = C4
::Reserves
::AddReserve
($branchcode, $patron_2->borrowernumber,
128 $biblio_1->biblionumber, undef, 2, undef, undef, undef, '', $item_1->itemnumber);
130 my $suspended_until = DateTime
->now->add(days
=> 10)->truncate( to
=> 'day' );
131 my $expiration_date = DateTime
->now->add(days
=> 10)->truncate( to
=> 'day' );
134 patron_id
=> int($patron_1->borrowernumber),
135 biblio_id
=> int($biblio_1->biblionumber),
136 item_id
=> int($item_1->itemnumber),
137 pickup_library_id
=> $branchcode,
138 expiration_date
=> output_pref
({ dt
=> $expiration_date, dateformat
=> 'rfc3339', dateonly
=> 1 }),
143 suspended_until
=> output_pref
({ dt
=> $suspended_until, dateformat
=> 'rfc3339' }),
146 subtest
"Test endpoints without authentication" => sub {
148 $t->get_ok('/api/v1/holds')
150 $t->post_ok('/api/v1/holds')
152 $t->put_ok('/api/v1/holds/0')
154 $t->delete_ok('/api/v1/holds/0')
158 subtest
"Test endpoints without permission" => sub {
162 $t->get_ok( "//$nopermission_userid:$password@/api/v1/holds?patron_id=" . $patron_1->borrowernumber ) # no permission
165 $t->get_ok( "//$userid_3:$password@/api/v1/holds?patron_id=" . $patron_1->borrowernumber ) # no permission
168 $t->post_ok( "//$nopermission_userid:$password@/api/v1/holds" => json
=> $post_data )
171 $t->put_ok( "//$nopermission_userid:$password@/api/v1/holds/0" => json
=> $put_data )
174 $t->delete_ok( "//$nopermission_userid:$password@/api/v1/holds/0" )
178 subtest
"Test endpoints with permission" => sub {
182 $t->get_ok( "//$userid_1:$password@/api/v1/holds" )
188 $t->get_ok( "//$userid_1:$password@/api/v1/holds?priority=2" )
190 ->json_is('/0/patron_id', $patron_2->borrowernumber)
193 $t->put_ok( "//$userid_1:$password@/api/v1/holds/$reserve_id" => json
=> $put_data )
195 ->json_is( '/hold_id', $reserve_id )
196 ->json_is( '/suspended_until', output_pref
({ dt
=> $suspended_until, dateformat
=> 'rfc3339' }) )
197 ->json_is( '/priority', 2 );
199 $t->delete_ok( "//$userid_3:$password@/api/v1/holds/$reserve_id" )
202 $t->put_ok( "//$userid_3:$password@/api/v1/holds/$reserve_id" => json
=> $put_data )
204 ->json_has('/error');
206 $t->delete_ok( "//$userid_3:$password@/api/v1/holds/$reserve_id" )
208 ->json_has('/error');
210 $t->get_ok( "//$userid_2:$password@/api/v1/holds?patron_id=" . $patron_1->borrowernumber )
214 my $inexisting_borrowernumber = $patron_2->borrowernumber * 2;
215 $t->get_ok( "//$userid_1:$password@/api/v1/holds?patron_id=$inexisting_borrowernumber")
219 $t->delete_ok( "//$userid_3:$password@/api/v1/holds/$reserve_id2" )
222 $t->post_ok( "//$userid_3:$password@/api/v1/holds" => json
=> $post_data )
224 ->json_has('/hold_id');
225 # Get id from response
226 $reserve_id = $t->tx->res->json->{hold_id
};
228 $t->get_ok( "//$userid_1:$password@/api/v1/holds?patron_id=" . $patron_1->borrowernumber )
230 ->json_is('/0/hold_id', $reserve_id)
231 ->json_is('/0/expiration_date', output_pref
({ dt
=> $expiration_date, dateformat
=> 'rfc3339', dateonly
=> 1 }))
232 ->json_is('/0/pickup_library_id', $branchcode);
234 $t->post_ok( "//$userid_3:$password@/api/v1/holds" => json
=> $post_data )
236 ->json_like('/error', qr
/itemAlreadyOnHold
/);
238 $post_data->{biblionumber
} = int($biblio_2->biblionumber);
239 $post_data->{itemnumber
} = int($item_2->itemnumber);
241 $t->post_ok( "//$userid_3:$password@/api/v1/holds" => json
=> $post_data )
243 ->json_like('/error', qr
/itemAlreadyOnHold
/);
246 subtest
'Reserves with itemtype' => sub {
250 patron_id
=> int($patron_1->borrowernumber),
251 biblio_id
=> int($biblio_1->biblionumber),
252 pickup_library_id
=> $branchcode,
253 item_type
=> $itemtype,
256 $t->delete_ok( "//$userid_3:$password@/api/v1/holds/$reserve_id" )
259 $t->post_ok( "//$userid_3:$password@/api/v1/holds" => json
=> $post_data )
261 ->json_has('/hold_id');
263 $reserve_id = $t->tx->res->json->{hold_id
};
265 $t->get_ok( "//$userid_1:$password@/api/v1/holds?patron_id=" . $patron_1->borrowernumber )
267 ->json_is('/0/hold_id', $reserve_id)
268 ->json_is('/0/item_type', $itemtype);
272 subtest
'test AllowHoldDateInFuture' => sub {
276 $dbh->do('DELETE FROM reserves');
278 my $future_hold_date = DateTime
->now->add(days
=> 10)->truncate( to
=> 'day' );
281 patron_id
=> int($patron_1->borrowernumber),
282 biblio_id
=> int($biblio_1->biblionumber),
283 item_id
=> int($item_1->itemnumber),
284 pickup_library_id
=> $branchcode,
285 expiration_date
=> output_pref
({ dt
=> $expiration_date, dateformat
=> 'rfc3339', dateonly
=> 1 }),
286 hold_date
=> output_pref
({ dt
=> $future_hold_date, dateformat
=> 'rfc3339', dateonly
=> 1 }),
290 t
::lib
::Mocks
::mock_preference
( 'AllowHoldDateInFuture', 0 );
292 $t->post_ok( "//$userid_3:$password@/api/v1/holds" => json
=> $post_data )
294 ->json_has('/error');
296 t
::lib
::Mocks
::mock_preference
( 'AllowHoldDateInFuture', 1 );
298 $t->post_ok( "//$userid_3:$password@/api/v1/holds" => json
=> $post_data )
300 ->json_is('/hold_date', output_pref
({ dt
=> $future_hold_date, dateformat
=> 'rfc3339', dateonly
=> 1 }));
303 subtest
'test AllowHoldPolicyOverride' => sub {
307 $dbh->do('DELETE FROM reserves');
309 Koha
::CirculationRules
->set_rules(
311 categorycode
=> undef,
320 t
::lib
::Mocks
::mock_preference
( 'AllowHoldPolicyOverride', 0 );
322 $t->post_ok( "//$userid_3:$password@/api/v1/holds" => json
=> $post_data )
324 ->json_has('/error');
326 t
::lib
::Mocks
::mock_preference
( 'AllowHoldPolicyOverride', 1 );
328 $t->post_ok( "//$userid_3:$password@/api/v1/holds" => json
=> $post_data )
332 $schema->storage->txn_rollback;
334 subtest
'suspend and resume tests' => sub {
338 $schema->storage->txn_begin;
340 my $password = 'AbcdEFG123';
342 my $patron = $builder->build_object(
343 { class => 'Koha::Patrons', value
=> { userid
=> 'tomasito', flags
=> 1 } } );
344 $patron->set_password({ password
=> $password, skip_validation
=> 1 });
345 my $userid = $patron->userid;
348 t
::lib
::Mocks
::mock_preference
( 'HoldsLog', 0 );
349 t
::lib
::Mocks
::mock_preference
( 'RESTBasicAuth', 1 );
351 my $hold = $builder->build_object(
352 { class => 'Koha::Holds',
353 value
=> { suspend
=> 0, suspend_until
=> undef, waitingdate
=> undef }
357 ok
( !$hold->is_suspended, 'Hold is not suspended' );
358 $t->post_ok( "//$userid:$password@/api/v1/holds/" . $hold->id . "/suspension" )
359 ->status_is( 201, 'Hold suspension created' );
361 $hold->discard_changes; # refresh object
363 ok
( $hold->is_suspended, 'Hold is suspended' );
367 { dt
=> dt_from_string
( $hold->suspend_until ),
368 dateformat
=> 'rfc3339',
374 $t->delete_ok( "//$userid:$password@/api/v1/holds/" . $hold->id . "/suspension" )
375 ->status_is( 204, "Correct status when deleting a resource" )
378 # Pass a an expiration date for the suspension
379 my $date = dt_from_string
()->add( days
=> 5 );
381 "//$userid:$password@/api/v1/holds/"
383 . "/suspension" => json
=> {
385 output_pref
( { dt
=> $date, dateformat
=> 'rfc3339', dateonly
=> 1 } )
387 )->status_is( 201, 'Hold suspension created' )
388 ->json_is( '/end_date',
389 output_pref
( { dt
=> $date, dateformat
=> 'rfc3339', dateonly
=> 1 } ) )
390 ->header_is( Location
=> "/api/v1/holds/" . $hold->id . "/suspension", 'The Location header is set' );
392 $t->delete_ok( "//$userid:$password@/api/v1/holds/" . $hold->id . "/suspension" )
393 ->status_is( 204, "Correct status when deleting a resource" )
396 $hold->set_waiting->discard_changes;
398 $t->post_ok( "//$userid:$password@/api/v1/holds/" . $hold->id . "/suspension" )
399 ->status_is( 400, 'Cannot suspend waiting hold' )
400 ->json_is( '/error', 'Found hold cannot be suspended. Status=W' );
402 $hold->set_waiting(1)->discard_changes;
404 $t->post_ok( "//$userid:$password@/api/v1/holds/" . $hold->id . "/suspension" )
405 ->status_is( 400, 'Cannot suspend waiting hold' )
406 ->json_is( '/error', 'Found hold cannot be suspended. Status=T' );
408 $schema->storage->txn_rollback;
411 subtest
'PUT /holds/{hold_id}/priority tests' => sub {
415 $schema->storage->txn_begin;
417 my $password = 'AbcdEFG123';
419 my $library = $builder->build_object({ class => 'Koha::Libraries' });
420 my $patron_np = $builder->build_object(
421 { class => 'Koha::Patrons', value
=> { flags
=> 0 } } );
422 $patron_np->set_password( { password
=> $password, skip_validation
=> 1 } );
423 my $userid_np = $patron_np->userid;
425 my $patron = $builder->build_object(
426 { class => 'Koha::Patrons', value
=> { flags
=> 0 } } );
427 $patron->set_password( { password
=> $password, skip_validation
=> 1 } );
428 my $userid = $patron->userid;
431 source
=> 'UserPermission',
433 borrowernumber
=> $patron->borrowernumber,
435 code
=> 'modify_holds_priority',
441 t
::lib
::Mocks
::mock_preference
( 'HoldsLog', 0 );
442 t
::lib
::Mocks
::mock_preference
( 'RESTBasicAuth', 1 );
444 my $biblio = $builder->build_sample_biblio;
445 my $patron_1 = $builder->build_object(
447 class => 'Koha::Patrons',
448 value
=> { branchcode
=> $library->branchcode }
451 my $patron_2 = $builder->build_object(
453 class => 'Koha::Patrons',
454 value
=> { branchcode
=> $library->branchcode }
457 my $patron_3 = $builder->build_object(
459 class => 'Koha::Patrons',
460 value
=> { branchcode
=> $library->branchcode }
464 my $hold_1 = Koha
::Holds
->find(
466 $library->branchcode, $patron_1->borrowernumber,
467 $biblio->biblionumber, undef,
471 my $hold_2 = Koha
::Holds
->find(
473 $library->branchcode, $patron_2->borrowernumber,
474 $biblio->biblionumber, undef,
478 my $hold_3 = Koha
::Holds
->find(
480 $library->branchcode, $patron_3->borrowernumber,
481 $biblio->biblionumber, undef,
486 $t->put_ok( "//$userid_np:$password@/api/v1/holds/"
488 . "/priority" => json
=> 1 )->status_is(403);
490 $t->put_ok( "//$userid:$password@/api/v1/holds/"
492 . "/priority" => json
=> 1 )->status_is(200)->json_is(1);
494 is
( $hold_1->discard_changes->priority, 2, 'Priority adjusted correctly' );
495 is
( $hold_2->discard_changes->priority, 3, 'Priority adjusted correctly' );
496 is
( $hold_3->discard_changes->priority, 1, 'Priority adjusted correctly' );
498 $t->put_ok( "//$userid:$password@/api/v1/holds/"
500 . "/priority" => json
=> 3 )->status_is(200)->json_is(3);
502 is
( $hold_1->discard_changes->priority, 1, 'Priority adjusted correctly' );
503 is
( $hold_2->discard_changes->priority, 2, 'Priority adjusted correctly' );
504 is
( $hold_3->discard_changes->priority, 3, 'Priority adjusted correctly' );
506 $schema->storage->txn_rollback;