3 # Copyright 2016 Koha-Suomi
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
22 use Test::More tests => 5;
27 use t::lib::TestBuilder;
34 my $schema = Koha::Database->new->schema;
35 my $builder = t::lib::TestBuilder->new;
37 t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 );
39 my $t = Test::Mojo->new('Koha::REST::V1');
41 subtest 'list() tests' => sub {
45 $schema->storage->txn_begin;
47 my $item = $builder->build_sample_item;
48 my $patron = $builder->build_object(
50 class => 'Koha::Patrons',
51 value => { flags => 4 }
55 # Make sure we have at least 10 items
57 $builder->build_sample_item;
60 my $nonprivilegedpatron = $builder->build_object(
62 class => 'Koha::Patrons',
63 value => { flags => 0 }
67 my $password = 'thePassword123';
69 $nonprivilegedpatron->set_password(
70 { password => $password, skip_validation => 1 } );
71 my $userid = $nonprivilegedpatron->userid;
73 $t->get_ok( "//$userid:$password@/api/v1/items" )
76 '/error' => 'Authorization failure. Missing required permission(s).' );
78 $patron->set_password( { password => $password, skip_validation => 1 } );
79 $userid = $patron->userid;
81 $t->get_ok( "//$userid:$password@/api/v1/items?_per_page=10" )
82 ->status_is( 200, 'SWAGGER3.2.2' );
84 my $response_count = scalar @{ $t->tx->res->json };
86 is( $response_count, 10, 'The API returns 10 items' );
88 $t->get_ok( "//$userid:$password@/api/v1/items?external_id=" . $item->barcode )
90 ->json_is( '' => [ $item->to_api ], 'SWAGGER3.3.2');
92 my $barcode = $item->barcode;
95 $t->get_ok( "//$userid:$password@/api/v1/items?external_id=" . $item->barcode )
97 ->json_is( '' => [] );
99 $schema->storage->txn_rollback;
102 subtest 'list_public() tests' => sub {
106 $schema->storage->txn_begin;
108 # Clean out all demo items
109 Koha::Items->delete();
111 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
112 my $mocked_category = Test::MockModule->new('Koha::Patron::Category');
114 $mocked_category->mock( 'override_hidden_items', sub {
118 my $password = 'thePassword123';
119 $patron->set_password( { password => $password, skip_validation => 1 } );
120 my $userid = $patron->userid;
122 # have a fresh biblio
123 my $biblio = $builder->build_sample_biblio;
125 my $itype_1 = $builder->build_object({ class => 'Koha::ItemTypes' });
126 my $itype_2 = $builder->build_object({ class => 'Koha::ItemTypes' });
127 # have 5 items on that biblio
128 my $item_1 = $builder->build_sample_item(
130 biblionumber => $biblio->biblionumber,
132 itype => $itype_1->itemtype,
137 my $item_2 = $builder->build_sample_item(
139 biblionumber => $biblio->biblionumber,
141 itype => $itype_2->itemtype,
146 my $item_3 = $builder->build_sample_item(
148 biblionumber => $biblio->biblionumber,
150 itype => $itype_1->itemtype,
155 my $item_4 = $builder->build_sample_item(
157 biblionumber => $biblio->biblionumber,
159 itype => $itype_2->itemtype,
164 my $item_5 = $builder->build_sample_item(
166 biblionumber => $biblio->biblionumber,
168 itype => $itype_1->itemtype,
173 my $item_6 = $builder->build_sample_item(
175 biblionumber => $biblio->biblionumber,
177 itype => $itype_1->itemtype,
184 my $mocked_context = Test::MockModule->new('C4::Context');
185 $mocked_context->mock( 'yaml_preference', sub {
189 subtest 'anonymous access' => sub {
192 t::lib::Mocks::mock_preference( 'hidelostitems', 0 );
193 my $res = $t->get_ok( "/api/v1/public/items" )->status_is(200)->tx->res->json;
194 is( scalar @{ $res }, 6, 'No rules set, hidelostitems unset, all items returned');
196 t::lib::Mocks::mock_preference( 'hidelostitems', 1 );
197 $res = $t->get_ok( "/api/v1/public/items" )->status_is(200)->tx->res->json;
198 is( scalar @{ $res }, 3, 'No rules set, hidelostitems set, 3 items hidden');
200 t::lib::Mocks::mock_preference( 'hidelostitems', 0 );
201 $rules = { biblionumber => [ $biblio->biblionumber ] };
202 $res = $t->get_ok( "/api/v1/public/items" )->status_is(200)->tx->res->json;
203 is( scalar @{ $res }, 0, 'Biblionumber rule set, hidelostitems unset, all items hidden');
205 $rules = { withdrawn => [ 1, 2 ] };
206 $res = $t->get_ok( "/api/v1/public/items" )->status_is(200)->tx->res->json;
207 is( scalar @{ $res }, 4, 'Withdrawn rule set, hidelostitems unset, 2 items hidden');
209 $rules = { itype => [ $itype_1->itemtype ] };
210 $res = $t->get_ok( "/api/v1/public/items" )->status_is(200)->tx->res->json;
211 is( scalar @{ $res }, 2, 'Itype rule set, hidelostitems unset, 4 items hidden');
213 $rules = { withdrawn => [ 1 ] };
214 $res = $t->get_ok( "/api/v1/public/items?external_id=" . $item_1->barcode )
215 ->status_is(200)->tx->res->json;
216 is( scalar @{ $res }, 0, 'Withdrawn rule set, hidelostitems unset, search on barcode returns no item');
219 $t->get_ok( "/api/v1/public/items?external_id=" . $item_1->barcode )
220 ->status_is(200)->json_is(
221 '/0' => $item_1->to_api( { public => 1 } ),
222 'No rules set, hidelostitems unset, public form of item returned on barcode search'
226 subtest 'logged in user access' => sub {
229 t::lib::Mocks::mock_preference( 'hidelostitems', 1 );
230 $rules = { withdrawn => [ 1, 2 ] };
231 my $res = $t->get_ok("//$userid:$password@/api/v1/public/items")
232 ->status_is(200)->tx->res->json;
236 'Rules on withdrawn but patron with override passed, hidelostitems set'
240 $schema->storage->txn_rollback;
243 subtest 'get() tests' => sub {
247 $schema->storage->txn_begin;
249 my $item = $builder->build_sample_item;
250 my $patron = $builder->build_object({
251 class => 'Koha::Patrons',
252 value => { flags => 4 }
255 my $nonprivilegedpatron = $builder->build_object({
256 class => 'Koha::Patrons',
257 value => { flags => 0 }
260 my $password = 'thePassword123';
262 $nonprivilegedpatron->set_password({ password => $password, skip_validation => 1 });
263 my $userid = $nonprivilegedpatron->userid;
265 $t->get_ok( "//$userid:$password@/api/v1/items/" . $item->itemnumber )
267 ->json_is( '/error' => 'Authorization failure. Missing required permission(s).' );
269 $patron->set_password({ password => $password, skip_validation => 1 });
270 $userid = $patron->userid;
272 $t->get_ok( "//$userid:$password@/api/v1/items/" . $item->itemnumber )
273 ->status_is( 200, 'SWAGGER3.2.2' )
274 ->json_is( '' => $item->to_api, 'SWAGGER3.3.2' );
276 my $non_existent_code = $item->itemnumber;
279 $t->get_ok( "//$userid:$password@/api/v1/items/" . $non_existent_code )
281 ->json_is( '/error' => 'Item not found' );
283 t::lib::Mocks::mock_preference( 'item-level_itypes', 0 );
285 my $biblio = $builder->build_sample_biblio;
287 $builder->build_object( { class => 'Koha::ItemTypes' } );
288 $item = $builder->build_sample_item(
289 { biblionumber => $biblio->biblionumber, itype => $itype->itemtype } );
291 isnt( $biblio->itemtype, $itype->itemtype, "Test biblio level itemtype and item level itemtype do not match");
293 $t->get_ok( "//$userid:$password@/api/v1/items/" . $item->itemnumber )
294 ->status_is( 200, 'SWAGGER3.2.2' )
295 ->json_is( '/item_type_id' => $itype->itemtype, 'item-level_itypes:0' )
296 ->json_is( '/effective_item_type_id' => $biblio->itemtype, 'item-level_itypes:0' );
298 t::lib::Mocks::mock_preference( 'item-level_itypes', 1 );
300 $t->get_ok( "//$userid:$password@/api/v1/items/" . $item->itemnumber )
301 ->status_is( 200, 'SWAGGER3.2.2' )
302 ->json_is( '/item_type_id' => $itype->itemtype, 'item-level_itype:1' )
303 ->json_is( '/effective_item_type_id' => $itype->itemtype, 'item-level_itypes:1' );
306 my $biblio_itype = Koha::ItemTypes->find($biblio->itemtype);
307 $biblio_itype->notforloan(3)->store();
308 $itype->notforloan(2)->store();
309 $item->notforloan(1)->store();
311 $t->get_ok( "//$userid:$password@/api/v1/items/" . $item->itemnumber )
312 ->status_is( 200, 'SWAGGER3.2.2' )
313 ->json_is( '/not_for_loan_status' => 1, 'not_for_loan_status is 1' )
314 ->json_is( '/effective_not_for_loan_status' => 1, 'effective_not_for_loan_status picks up item level' );
316 $item->notforloan(0)->store();
317 $t->get_ok( "//$userid:$password@/api/v1/items/" . $item->itemnumber )
318 ->status_is( 200, 'SWAGGER3.2.2' )
319 ->json_is( '/not_for_loan_status' => 0, 'not_for_loan_status is 0' )
320 ->json_is( '/effective_not_for_loan_status' => 2, 'effective_not_for_loan_status now picks up itemtype level - item-level_itypes:1' );
322 t::lib::Mocks::mock_preference( 'item-level_itypes', 0 );
323 $t->get_ok( "//$userid:$password@/api/v1/items/" . $item->itemnumber )
324 ->status_is( 200, 'SWAGGER3.2.2' )
325 ->json_is( '/not_for_loan_status' => 0, 'not_for_loan_status is 0' )
326 ->json_is( '/effective_not_for_loan_status' => 3, 'effective_not_for_loan_status now picks up itemtype level - item-level_itypes:0' );
328 $schema->storage->txn_rollback;
331 subtest 'delete() tests' => sub {
335 $schema->storage->txn_begin;
340 # we want to control all the safe_to_delete use cases
341 my $item_class = Test::MockModule->new('Koha::Item');
342 $item_class->mock( 'safe_to_delete', sub {
344 return Koha::Result::Boolean->new(0)->add_message({ message => $expected_error });
347 return Koha::Result::Boolean->new(1);
351 my $librarian = $builder->build_object(
353 class => 'Koha::Patrons',
354 value => { flags => 2**9 } # catalogue flag = 2
357 my $password = 'thePassword123';
358 $librarian->set_password( { password => $password, skip_validation => 1 } );
359 my $userid = $librarian->userid;
361 my $item = $builder->build_sample_item;
364 book_on_loan => { code => 'checked_out', description => 'The item is checked out' },
365 book_reserved => { code => 'found_hold', description => 'Waiting or in-transit hold for the item' },
366 last_item_for_hold => { code => 'last_item_for_hold', description => 'The item is the last one on a record on which a biblio-level hold is placed' },
367 linked_analytics => { code => 'linked_analytics', description => 'The item has linked analytic records' },
368 not_same_branch => { code => 'not_same_branch', description => 'The item is blocked by independent branches' },
373 foreach my $error_code ( keys %{$errors} ) {
375 $expected_error = $error_code;
377 $t->delete_ok( "//$userid:$password@/api/v1/items/" . $item->id )
380 { error => $errors->{$error_code}->{description},
381 error_code => $errors->{$error_code}->{code},
386 $expected_error = 'unknown_error';
387 $t->delete_ok( "//$userid:$password@/api/v1/items/" . $item->id )
388 ->status_is(500, 'unhandled error case generated default unhandled exception message')
390 { error => 'Something went wrong, check Koha logs for details.',
391 error_code => 'internal_server_error',
397 $t->delete_ok("//$userid:$password@/api/v1/items/" . $item->id)
398 ->status_is(204, 'SWAGGER3.2.4')
399 ->content_is('', 'SWAGGER3.3.4');
401 $t->delete_ok("//$userid:$password@/api/v1/items/" . $item->id)
404 $schema->storage->txn_rollback;
407 subtest 'pickup_locations() tests' => sub {
411 $schema->storage->txn_begin;
413 t::lib::Mocks::mock_preference( 'AllowHoldPolicyOverride', 0 );
415 # Small trick to ease testing
416 Koha::Libraries->search->update({ pickup_location => 0 });
418 my $library_1 = $builder->build_object({ class => 'Koha::Libraries', value => { marcorgcode => 'A', pickup_location => 1 } });
419 my $library_2 = $builder->build_object({ class => 'Koha::Libraries', value => { marcorgcode => 'B', pickup_location => 1 } });
420 my $library_3 = $builder->build_object({ class => 'Koha::Libraries', value => { marcorgcode => 'C', pickup_location => 1 } });
422 my $library_1_api = $library_1->to_api();
423 my $library_2_api = $library_2->to_api();
424 my $library_3_api = $library_3->to_api();
426 $library_1_api->{needs_override} = Mojo::JSON->false;
427 $library_2_api->{needs_override} = Mojo::JSON->false;
428 $library_3_api->{needs_override} = Mojo::JSON->true;
430 my $patron = $builder->build_object(
432 class => 'Koha::Patrons',
433 value => { userid => 'tomasito', flags => 0 }
436 my $password = 'thePassword123';
437 $patron->set_password( { password => $password, skip_validation => 1 } );
438 my $userid = $patron->userid;
441 source => 'UserPermission',
443 borrowernumber => $patron->borrowernumber,
445 code => 'place_holds',
450 my $item = $builder->build_sample_item();
452 my $item_class = Test::MockModule->new('Koha::Item');
456 my ( $self, $params ) = @_;
457 my $mock_patron = $params->{patron};
458 is( $mock_patron->borrowernumber,
459 $patron->borrowernumber, 'Patron passed correctly' );
460 return Koha::Libraries->search(
464 $library_1->branchcode,
465 $library_2->branchcode
469 { # we make sure no surprises in the order of the result
470 order_by => { '-asc' => 'marcorgcode' }
476 $t->get_ok( "//$userid:$password@/api/v1/items/"
478 . "/pickup_locations?patron_id=" . $patron->id )
479 ->json_is( [ $library_1_api, $library_2_api ] );
482 $t->get_ok( "//$userid:$password@/api/v1/items/"
484 . '/pickup_locations?'
485 . 'patron_id=' . $patron->id . '&q={"marc_org_code": { "-like": "A%" }}' )
486 ->json_is( [ $library_1_api ] );
488 t::lib::Mocks::mock_preference( 'AllowHoldPolicyOverride', 1 );
490 my $library_4 = $builder->build_object({ class => 'Koha::Libraries', value => { pickup_location => 0, marcorgcode => 'X' } });
491 my $library_5 = $builder->build_object({ class => 'Koha::Libraries', value => { pickup_location => 1, marcorgcode => 'Y' } });
493 my $library_5_api = $library_5->to_api();
494 $library_5_api->{needs_override} = Mojo::JSON->true;
496 $t->get_ok( "//$userid:$password@/api/v1/items/"
498 . "/pickup_locations?"
499 . "patron_id=" . $patron->id . "&_order_by=marc_org_code" )
500 ->json_is( [ $library_1_api, $library_2_api, $library_3_api, $library_5_api ] );
502 subtest 'Pagination and AllowHoldPolicyOverride tests' => sub {
506 t::lib::Mocks::mock_preference( 'AllowHoldPolicyOverride', 1 );
508 $t->get_ok( "//$userid:$password@/api/v1/items/" . $item->id . "/pickup_locations?" . "patron_id=" . $patron->id . "&_order_by=marc_org_code" . "&_per_page=1" )
509 ->json_is( [$library_1_api] )
510 ->header_is( 'X-Total-Count', '4', '4 is the count for libraries with pickup_location=1' )
511 ->header_is( 'X-Base-Total-Count', '4', '4 is the count for libraries with pickup_location=1' )
512 ->header_unlike( 'Link', qr|rel="prev"| )
513 ->header_like( 'Link', qr#(_per_page=1.*\&_page=2.*|_page=2.*\&_per_page=1.*)>\; rel="next"# )
514 ->header_like( 'Link', qr#(_per_page=1.*\&_page=1.*|_page=1.*\&_per_page=1).*>\; rel="first"# )
515 ->header_like( 'Link', qr#(_per_page=1.*\&_page=4.*|_page=4.*\&_per_page=1).*>\; rel="last"# );
517 $t->get_ok( "//$userid:$password@/api/v1/items/"
519 . "/pickup_locations?"
522 . "&_order_by=marc_org_code"
523 . "&_per_page=1&_page=3" ) # force the needs_override=1 check
524 ->json_is( [$library_3_api] )
525 ->header_is( 'X-Total-Count', '4', '4 is the count for libraries with pickup_location=1' )
526 ->header_is( 'X-Base-Total-Count', '4', '4 is the count for libraries with pickup_location=1' )
527 ->header_like( 'Link', qr#(_per_page=1.*\&_page=2.*|_page=2.*\&_per_page=1.*)>\; rel="prev"# )
528 ->header_like( 'Link', qr#(_per_page=1.*\&_page=4.*|_page=4.*\&_per_page=1.*)>\; rel="next"# )
529 ->header_like( 'Link', qr#(_per_page=1.*\&_page=1.*|_page=1.*\&_per_page=1).*>\; rel="first"# )
530 ->header_like( 'Link', qr#(_per_page=1.*\&_page=4.*|_page=4.*\&_per_page=1).*>\; rel="last"# );
532 t::lib::Mocks::mock_preference( 'AllowHoldPolicyOverride', 0 );
534 $t->get_ok( "//$userid:$password@/api/v1/items/" . $item->id . "/pickup_locations?" . "patron_id=" . $patron->id . "&_order_by=marc_org_code" . "&_per_page=1" )
535 ->json_is( [$library_1_api] )
536 ->header_is( 'X-Total-Count', '2' )
537 ->header_is( 'X-Base-Total-Count', '2' )
538 ->header_unlike( 'Link', qr|rel="prev"| )
539 ->header_like( 'Link', qr#(_per_page=1.*\&_page=2.*|_page=2.*\&_per_page=1.*)>\; rel="next"# )
540 ->header_like( 'Link', qr#(_per_page=1.*\&_page=1.*|_page=1.*\&_per_page=1).*>\; rel="first"# )
541 ->header_like( 'Link', qr#(_per_page=1.*\&_page=2.*|_page=2.*\&_per_page=1).*>\; rel="last"# );
544 my $deleted_patron = $builder->build_object({ class => 'Koha::Patrons' });
545 my $deleted_patron_id = $deleted_patron->id;
546 $deleted_patron->delete;
548 $t->get_ok( "//$userid:$password@/api/v1/items/"
550 . "/pickup_locations?"
551 . "patron_id=" . $deleted_patron_id )
553 ->json_is( '/error' => 'Patron not found' );
557 $t->get_ok( "//$userid:$password@/api/v1/items/"
559 . "/pickup_locations?"
560 . "patron_id=" . $patron->id )
562 ->json_is( '/error' => 'Item not found' );
564 $schema->storage->txn_rollback;