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 => 3;
26 use t::lib::TestBuilder;
33 my $schema = Koha::Database->new->schema;
34 my $builder = t::lib::TestBuilder->new;
36 t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 );
38 my $t = Test::Mojo->new('Koha::REST::V1');
40 subtest 'list() tests' => sub {
44 $schema->storage->txn_begin;
46 my $item = $builder->build_sample_item;
47 my $patron = $builder->build_object(
49 class => 'Koha::Patrons',
50 value => { flags => 4 }
54 # Make sure we have at least 10 items
56 $builder->build_sample_item;
59 my $nonprivilegedpatron = $builder->build_object(
61 class => 'Koha::Patrons',
62 value => { flags => 0 }
66 my $password = 'thePassword123';
68 $nonprivilegedpatron->set_password(
69 { password => $password, skip_validation => 1 } );
70 my $userid = $nonprivilegedpatron->userid;
72 $t->get_ok( "//$userid:$password@/api/v1/items" )
75 '/error' => 'Authorization failure. Missing required permission(s).' );
77 $patron->set_password( { password => $password, skip_validation => 1 } );
78 $userid = $patron->userid;
80 $t->get_ok( "//$userid:$password@/api/v1/items?_per_page=10" )
81 ->status_is( 200, 'SWAGGER3.2.2' );
83 my $response_count = scalar @{ $t->tx->res->json };
85 is( $response_count, 10, 'The API returns 10 items' );
87 $t->get_ok( "//$userid:$password@/api/v1/items?external_id=" . $item->barcode )
89 ->json_is( '' => [ $item->to_api ], 'SWAGGER3.3.2');
91 $t->get_ok( "//$userid:$password@/api/v1/items?external_id=" . $item->barcode => {'x-koha-embed' => 'biblio'} )
93 ->json_is( '' => [ { %{$item->to_api}, biblio => $item->biblio->to_api } ], 'SWAGGER3.3.2');
96 my $barcode = $item->barcode;
99 $t->get_ok( "//$userid:$password@/api/v1/items?external_id=" . $item->barcode )
101 ->json_is( '' => [] );
103 $schema->storage->txn_rollback;
107 subtest 'get() tests' => sub {
111 $schema->storage->txn_begin;
113 my $item = $builder->build_sample_item;
114 my $patron = $builder->build_object({
115 class => 'Koha::Patrons',
116 value => { flags => 4 }
119 my $nonprivilegedpatron = $builder->build_object({
120 class => 'Koha::Patrons',
121 value => { flags => 0 }
124 my $password = 'thePassword123';
126 $nonprivilegedpatron->set_password({ password => $password, skip_validation => 1 });
127 my $userid = $nonprivilegedpatron->userid;
129 $t->get_ok( "//$userid:$password@/api/v1/items/" . $item->itemnumber )
131 ->json_is( '/error' => 'Authorization failure. Missing required permission(s).' );
133 $patron->set_password({ password => $password, skip_validation => 1 });
134 $userid = $patron->userid;
136 $t->get_ok( "//$userid:$password@/api/v1/items/" . $item->itemnumber )
137 ->status_is( 200, 'SWAGGER3.2.2' )
138 ->json_is( '' => $item->to_api, 'SWAGGER3.3.2' );
140 my $non_existent_code = $item->itemnumber;
143 $t->get_ok( "//$userid:$password@/api/v1/items/" . $non_existent_code )
145 ->json_is( '/error' => 'Item not found' );
147 t::lib::Mocks::mock_preference( 'item-level_itypes', 0 );
149 my $biblio = $builder->build_sample_biblio;
151 $builder->build_object( { class => 'Koha::ItemTypes' } );
152 $item = $builder->build_sample_item(
153 { biblionumber => $biblio->biblionumber, itype => $itype->itemtype } );
155 isnt( $biblio->itemtype, $itype->itemtype, "Test biblio level itemtype and item level itemtype do not match");
157 $t->get_ok( "//$userid:$password@/api/v1/items/" . $item->itemnumber )
158 ->status_is( 200, 'SWAGGER3.2.2' )
159 ->json_is( '/item_type_id' => $itype->itemtype, 'item-level_itypes:0' )
160 ->json_is( '/effective_item_type_id' => $biblio->itemtype, 'item-level_itypes:0' );
162 t::lib::Mocks::mock_preference( 'item-level_itypes', 1 );
164 $t->get_ok( "//$userid:$password@/api/v1/items/" . $item->itemnumber )
165 ->status_is( 200, 'SWAGGER3.2.2' )
166 ->json_is( '/item_type_id' => $itype->itemtype, 'item-level_itype:1' )
167 ->json_is( '/effective_item_type_id' => $itype->itemtype, 'item-level_itypes:1' );
170 my $biblio_itype = Koha::ItemTypes->find($biblio->itemtype);
171 $biblio_itype->notforloan(3)->store();
172 $itype->notforloan(2)->store();
173 $item->notforloan(1)->store();
175 $t->get_ok( "//$userid:$password@/api/v1/items/" . $item->itemnumber )
176 ->status_is( 200, 'SWAGGER3.2.2' )
177 ->json_is( '/not_for_loan_status' => 1, 'not_for_loan_status is 1' )
178 ->json_is( '/effective_not_for_loan_status' => 1, 'effective_not_for_loan_status picks up item level' );
180 $item->notforloan(0)->store();
181 $t->get_ok( "//$userid:$password@/api/v1/items/" . $item->itemnumber )
182 ->status_is( 200, 'SWAGGER3.2.2' )
183 ->json_is( '/not_for_loan_status' => 0, 'not_for_loan_status is 0' )
184 ->json_is( '/effective_not_for_loan_status' => 2, 'effective_not_for_loan_status now picks up itemtype level - item-level_itypes:1' );
186 $itype->notforloan(undef)->store();
187 $t->get_ok( "//$userid:$password@/api/v1/items/" . $item->itemnumber )->status_is( 200, 'SWAGGER3.2.2' )
188 ->json_is( '/not_for_loan_status' => 0, 'not_for_loan_status is 0' )->json_is(
189 '/effective_not_for_loan_status' => 0,
190 'effective_not_for_loan_status now picks up itemtype level and falls back to 0 because undef'
193 t::lib::Mocks::mock_preference( 'item-level_itypes', 0 );
194 $t->get_ok( "//$userid:$password@/api/v1/items/" . $item->itemnumber )
195 ->status_is( 200, 'SWAGGER3.2.2' )
196 ->json_is( '/not_for_loan_status' => 0, 'not_for_loan_status is 0' )
197 ->json_is( '/effective_not_for_loan_status' => 3, 'effective_not_for_loan_status now picks up itemtype level - item-level_itypes:0' );
199 $schema->storage->txn_rollback;
202 subtest 'pickup_locations() tests' => sub {
206 $schema->storage->txn_begin;
208 t::lib::Mocks::mock_preference( 'AllowHoldPolicyOverride', 0 );
210 # Small trick to ease testing
211 Koha::Libraries->search->update({ pickup_location => 0 });
213 my $library_1 = $builder->build_object({ class => 'Koha::Libraries', value => { marcorgcode => 'A', pickup_location => 1 } });
214 my $library_2 = $builder->build_object({ class => 'Koha::Libraries', value => { marcorgcode => 'B', pickup_location => 1 } });
215 my $library_3 = $builder->build_object({ class => 'Koha::Libraries', value => { marcorgcode => 'C', pickup_location => 1 } });
217 my $library_1_api = $library_1->to_api();
218 my $library_2_api = $library_2->to_api();
219 my $library_3_api = $library_3->to_api();
221 $library_1_api->{needs_override} = Mojo::JSON->false;
222 $library_2_api->{needs_override} = Mojo::JSON->false;
223 $library_3_api->{needs_override} = Mojo::JSON->true;
225 my $patron = $builder->build_object(
227 class => 'Koha::Patrons',
228 value => { userid => 'tomasito', flags => 0 }
231 my $password = 'thePassword123';
232 $patron->set_password( { password => $password, skip_validation => 1 } );
233 my $userid = $patron->userid;
236 source => 'UserPermission',
238 borrowernumber => $patron->borrowernumber,
240 code => 'place_holds',
245 my $item = $builder->build_sample_item();
247 my $item_class = Test::MockModule->new('Koha::Item');
251 my ( $self, $params ) = @_;
252 my $mock_patron = $params->{patron};
253 is( $mock_patron->borrowernumber,
254 $patron->borrowernumber, 'Patron passed correctly' );
255 return Koha::Libraries->search(
259 $library_1->branchcode,
260 $library_2->branchcode
264 { # we make sure no surprises in the order of the result
265 order_by => { '-asc' => 'marcorgcode' }
271 $t->get_ok( "//$userid:$password@/api/v1/items/"
273 . "/pickup_locations?patron_id=" . $patron->id )
274 ->json_is( [ $library_1_api, $library_2_api ] );
277 $t->get_ok( "//$userid:$password@/api/v1/items/"
279 . '/pickup_locations?'
280 . 'patron_id=' . $patron->id . '&q={"marc_org_code": { "-like": "A%" }}' )
281 ->json_is( [ $library_1_api ] );
283 t::lib::Mocks::mock_preference( 'AllowHoldPolicyOverride', 1 );
285 my $library_4 = $builder->build_object({ class => 'Koha::Libraries', value => { pickup_location => 0, marcorgcode => 'X' } });
286 my $library_5 = $builder->build_object({ class => 'Koha::Libraries', value => { pickup_location => 1, marcorgcode => 'Y' } });
288 my $library_5_api = $library_5->to_api();
289 $library_5_api->{needs_override} = Mojo::JSON->true;
291 $t->get_ok( "//$userid:$password@/api/v1/items/"
293 . "/pickup_locations?"
294 . "patron_id=" . $patron->id . "&_order_by=marc_org_code" )
295 ->json_is( [ $library_1_api, $library_2_api, $library_3_api, $library_5_api ] );
297 subtest 'Pagination and AllowHoldPolicyOverride tests' => sub {
301 t::lib::Mocks::mock_preference( 'AllowHoldPolicyOverride', 1 );
303 $t->get_ok( "//$userid:$password@/api/v1/items/" . $item->id . "/pickup_locations?" . "patron_id=" . $patron->id . "&_order_by=marc_org_code" . "&_per_page=1" )
304 ->json_is( [$library_1_api] )
305 ->header_is( 'X-Total-Count', '4', '4 is the count for libraries with pickup_location=1' )
306 ->header_is( 'X-Base-Total-Count', '4', '4 is the count for libraries with pickup_location=1' )
307 ->header_unlike( 'Link', qr|rel="prev"| )
308 ->header_like( 'Link', qr#(_per_page=1.*\&_page=2.*|_page=2.*\&_per_page=1.*)>\; rel="next"# )
309 ->header_like( 'Link', qr#(_per_page=1.*\&_page=1.*|_page=1.*\&_per_page=1).*>\; rel="first"# )
310 ->header_like( 'Link', qr#(_per_page=1.*\&_page=4.*|_page=4.*\&_per_page=1).*>\; rel="last"# );
312 $t->get_ok( "//$userid:$password@/api/v1/items/"
314 . "/pickup_locations?"
317 . "&_order_by=marc_org_code"
318 . "&_per_page=1&_page=3" ) # force the needs_override=1 check
319 ->json_is( [$library_3_api] )
320 ->header_is( 'X-Total-Count', '4', '4 is the count for libraries with pickup_location=1' )
321 ->header_is( 'X-Base-Total-Count', '4', '4 is the count for libraries with pickup_location=1' )
322 ->header_like( 'Link', qr#(_per_page=1.*\&_page=2.*|_page=2.*\&_per_page=1.*)>\; rel="prev"# )
323 ->header_like( 'Link', qr#(_per_page=1.*\&_page=4.*|_page=4.*\&_per_page=1.*)>\; rel="next"# )
324 ->header_like( 'Link', qr#(_per_page=1.*\&_page=1.*|_page=1.*\&_per_page=1).*>\; rel="first"# )
325 ->header_like( 'Link', qr#(_per_page=1.*\&_page=4.*|_page=4.*\&_per_page=1).*>\; rel="last"# );
327 t::lib::Mocks::mock_preference( 'AllowHoldPolicyOverride', 0 );
329 $t->get_ok( "//$userid:$password@/api/v1/items/" . $item->id . "/pickup_locations?" . "patron_id=" . $patron->id . "&_order_by=marc_org_code" . "&_per_page=1" )
330 ->json_is( [$library_1_api] )
331 ->header_is( 'X-Total-Count', '2' )
332 ->header_is( 'X-Base-Total-Count', '2' )
333 ->header_unlike( 'Link', qr|rel="prev"| )
334 ->header_like( 'Link', qr#(_per_page=1.*\&_page=2.*|_page=2.*\&_per_page=1.*)>\; rel="next"# )
335 ->header_like( 'Link', qr#(_per_page=1.*\&_page=1.*|_page=1.*\&_per_page=1).*>\; rel="first"# )
336 ->header_like( 'Link', qr#(_per_page=1.*\&_page=2.*|_page=2.*\&_per_page=1).*>\; rel="last"# );
339 my $deleted_patron = $builder->build_object({ class => 'Koha::Patrons' });
340 my $deleted_patron_id = $deleted_patron->id;
341 $deleted_patron->delete;
343 $t->get_ok( "//$userid:$password@/api/v1/items/"
345 . "/pickup_locations?"
346 . "patron_id=" . $deleted_patron_id )
348 ->json_is( '/error' => 'Patron not found' );
352 $t->get_ok( "//$userid:$password@/api/v1/items/"
354 . "/pickup_locations?"
355 . "patron_id=" . $patron->id )
357 ->json_is( '/error' => 'Item not found' );
359 $schema->storage->txn_rollback;