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 my $barcode = $item->barcode;
94 $t->get_ok( "//$userid:$password@/api/v1/items?external_id=" . $item->barcode )
96 ->json_is( '' => [] );
98 $schema->storage->txn_rollback;
102 subtest 'get() tests' => sub {
106 $schema->storage->txn_begin;
108 my $item = $builder->build_sample_item;
109 my $patron = $builder->build_object({
110 class => 'Koha::Patrons',
111 value => { flags => 4 }
114 my $nonprivilegedpatron = $builder->build_object({
115 class => 'Koha::Patrons',
116 value => { flags => 0 }
119 my $password = 'thePassword123';
121 $nonprivilegedpatron->set_password({ password => $password, skip_validation => 1 });
122 my $userid = $nonprivilegedpatron->userid;
124 $t->get_ok( "//$userid:$password@/api/v1/items/" . $item->itemnumber )
126 ->json_is( '/error' => 'Authorization failure. Missing required permission(s).' );
128 $patron->set_password({ password => $password, skip_validation => 1 });
129 $userid = $patron->userid;
131 $t->get_ok( "//$userid:$password@/api/v1/items/" . $item->itemnumber )
132 ->status_is( 200, 'SWAGGER3.2.2' )
133 ->json_is( '' => $item->to_api, 'SWAGGER3.3.2' );
135 my $non_existent_code = $item->itemnumber;
138 $t->get_ok( "//$userid:$password@/api/v1/items/" . $non_existent_code )
140 ->json_is( '/error' => 'Item not found' );
142 t::lib::Mocks::mock_preference( 'item-level_itypes', 0 );
144 my $biblio = $builder->build_sample_biblio;
146 $builder->build_object( { class => 'Koha::ItemTypes' } );
147 $item = $builder->build_sample_item(
148 { biblionumber => $biblio->biblionumber, itype => $itype->itemtype } );
150 isnt( $biblio->itemtype, $itype->itemtype, "Test biblio level itemtype and item level itemtype do not match");
152 $t->get_ok( "//$userid:$password@/api/v1/items/" . $item->itemnumber )
153 ->status_is( 200, 'SWAGGER3.2.2' )
154 ->json_is( '/item_type_id' => $itype->itemtype, 'item-level_itypes:0' )
155 ->json_is( '/effective_item_type_id' => $biblio->itemtype, 'item-level_itypes:0' );
157 t::lib::Mocks::mock_preference( 'item-level_itypes', 1 );
159 $t->get_ok( "//$userid:$password@/api/v1/items/" . $item->itemnumber )
160 ->status_is( 200, 'SWAGGER3.2.2' )
161 ->json_is( '/item_type_id' => $itype->itemtype, 'item-level_itype:1' )
162 ->json_is( '/effective_item_type_id' => $itype->itemtype, 'item-level_itypes:1' );
165 my $biblio_itype = Koha::ItemTypes->find($biblio->itemtype);
166 $biblio_itype->notforloan(3)->store();
167 $itype->notforloan(2)->store();
168 $item->notforloan(1)->store();
170 $t->get_ok( "//$userid:$password@/api/v1/items/" . $item->itemnumber )
171 ->status_is( 200, 'SWAGGER3.2.2' )
172 ->json_is( '/not_for_loan_status' => 1, 'not_for_loan_status is 1' )
173 ->json_is( '/effective_not_for_loan_status' => 1, 'effective_not_for_loan_status picks up item level' );
175 $item->notforloan(0)->store();
176 $t->get_ok( "//$userid:$password@/api/v1/items/" . $item->itemnumber )
177 ->status_is( 200, 'SWAGGER3.2.2' )
178 ->json_is( '/not_for_loan_status' => 0, 'not_for_loan_status is 0' )
179 ->json_is( '/effective_not_for_loan_status' => 2, 'effective_not_for_loan_status now picks up itemtype level - item-level_itypes:1' );
181 t::lib::Mocks::mock_preference( 'item-level_itypes', 0 );
182 $t->get_ok( "//$userid:$password@/api/v1/items/" . $item->itemnumber )
183 ->status_is( 200, 'SWAGGER3.2.2' )
184 ->json_is( '/not_for_loan_status' => 0, 'not_for_loan_status is 0' )
185 ->json_is( '/effective_not_for_loan_status' => 3, 'effective_not_for_loan_status now picks up itemtype level - item-level_itypes:0' );
187 $schema->storage->txn_rollback;
190 subtest 'pickup_locations() tests' => sub {
194 $schema->storage->txn_begin;
196 t::lib::Mocks::mock_preference( 'AllowHoldPolicyOverride', 0 );
198 # Small trick to ease testing
199 Koha::Libraries->search->update({ pickup_location => 0 });
201 my $library_1 = $builder->build_object({ class => 'Koha::Libraries', value => { marcorgcode => 'A', pickup_location => 1 } });
202 my $library_2 = $builder->build_object({ class => 'Koha::Libraries', value => { marcorgcode => 'B', pickup_location => 1 } });
203 my $library_3 = $builder->build_object({ class => 'Koha::Libraries', value => { marcorgcode => 'C', pickup_location => 1 } });
205 my $library_1_api = $library_1->to_api();
206 my $library_2_api = $library_2->to_api();
207 my $library_3_api = $library_3->to_api();
209 $library_1_api->{needs_override} = Mojo::JSON->false;
210 $library_2_api->{needs_override} = Mojo::JSON->false;
211 $library_3_api->{needs_override} = Mojo::JSON->true;
213 my $patron = $builder->build_object(
215 class => 'Koha::Patrons',
216 value => { userid => 'tomasito', flags => 0 }
219 my $password = 'thePassword123';
220 $patron->set_password( { password => $password, skip_validation => 1 } );
221 my $userid = $patron->userid;
224 source => 'UserPermission',
226 borrowernumber => $patron->borrowernumber,
228 code => 'place_holds',
233 my $item = $builder->build_sample_item();
235 my $item_class = Test::MockModule->new('Koha::Item');
239 my ( $self, $params ) = @_;
240 my $mock_patron = $params->{patron};
241 is( $mock_patron->borrowernumber,
242 $patron->borrowernumber, 'Patron passed correctly' );
243 return Koha::Libraries->search(
247 $library_1->branchcode,
248 $library_2->branchcode
252 { # we make sure no surprises in the order of the result
253 order_by => { '-asc' => 'marcorgcode' }
259 $t->get_ok( "//$userid:$password@/api/v1/items/"
261 . "/pickup_locations?patron_id=" . $patron->id )
262 ->json_is( [ $library_1_api, $library_2_api ] );
265 $t->get_ok( "//$userid:$password@/api/v1/items/"
267 . '/pickup_locations?'
268 . 'patron_id=' . $patron->id . '&q={"marc_org_code": { "-like": "A%" }}' )
269 ->json_is( [ $library_1_api ] );
271 t::lib::Mocks::mock_preference( 'AllowHoldPolicyOverride', 1 );
273 my $library_4 = $builder->build_object({ class => 'Koha::Libraries', value => { pickup_location => 0, marcorgcode => 'X' } });
274 my $library_5 = $builder->build_object({ class => 'Koha::Libraries', value => { pickup_location => 1, marcorgcode => 'Y' } });
276 my $library_5_api = $library_5->to_api();
277 $library_5_api->{needs_override} = Mojo::JSON->true;
279 $t->get_ok( "//$userid:$password@/api/v1/items/"
281 . "/pickup_locations?"
282 . "patron_id=" . $patron->id . "&_order_by=marc_org_code" )
283 ->json_is( [ $library_1_api, $library_2_api, $library_3_api, $library_5_api ] );
285 subtest 'Pagination and AllowHoldPolicyOverride tests' => sub {
289 t::lib::Mocks::mock_preference( 'AllowHoldPolicyOverride', 1 );
291 $t->get_ok( "//$userid:$password@/api/v1/items/" . $item->id . "/pickup_locations?" . "patron_id=" . $patron->id . "&_order_by=marc_org_code" . "&_per_page=1" )
292 ->json_is( [$library_1_api] )
293 ->header_is( 'X-Total-Count', '4', '4 is the count for libraries with pickup_location=1' )
294 ->header_is( 'X-Base-Total-Count', '4', '4 is the count for libraries with pickup_location=1' )
295 ->header_unlike( 'Link', qr|rel="prev"| )
296 ->header_like( 'Link', qr#(_per_page=1.*\&_page=2.*|_page=2.*\&_per_page=1.*)>\; rel="next"# )
297 ->header_like( 'Link', qr#(_per_page=1.*\&_page=1.*|_page=1.*\&_per_page=1).*>\; rel="first"# )
298 ->header_like( 'Link', qr#(_per_page=1.*\&_page=4.*|_page=4.*\&_per_page=1).*>\; rel="last"# );
300 $t->get_ok( "//$userid:$password@/api/v1/items/"
302 . "/pickup_locations?"
305 . "&_order_by=marc_org_code"
306 . "&_per_page=1&_page=3" ) # force the needs_override=1 check
307 ->json_is( [$library_3_api] )
308 ->header_is( 'X-Total-Count', '4', '4 is the count for libraries with pickup_location=1' )
309 ->header_is( 'X-Base-Total-Count', '4', '4 is the count for libraries with pickup_location=1' )
310 ->header_like( 'Link', qr#(_per_page=1.*\&_page=2.*|_page=2.*\&_per_page=1.*)>\; rel="prev"# )
311 ->header_like( 'Link', qr#(_per_page=1.*\&_page=4.*|_page=4.*\&_per_page=1.*)>\; rel="next"# )
312 ->header_like( 'Link', qr#(_per_page=1.*\&_page=1.*|_page=1.*\&_per_page=1).*>\; rel="first"# )
313 ->header_like( 'Link', qr#(_per_page=1.*\&_page=4.*|_page=4.*\&_per_page=1).*>\; rel="last"# );
315 t::lib::Mocks::mock_preference( 'AllowHoldPolicyOverride', 0 );
317 $t->get_ok( "//$userid:$password@/api/v1/items/" . $item->id . "/pickup_locations?" . "patron_id=" . $patron->id . "&_order_by=marc_org_code" . "&_per_page=1" )
318 ->json_is( [$library_1_api] )
319 ->header_is( 'X-Total-Count', '2' )
320 ->header_is( 'X-Base-Total-Count', '2' )
321 ->header_unlike( 'Link', qr|rel="prev"| )
322 ->header_like( 'Link', qr#(_per_page=1.*\&_page=2.*|_page=2.*\&_per_page=1.*)>\; rel="next"# )
323 ->header_like( 'Link', qr#(_per_page=1.*\&_page=1.*|_page=1.*\&_per_page=1).*>\; rel="first"# )
324 ->header_like( 'Link', qr#(_per_page=1.*\&_page=2.*|_page=2.*\&_per_page=1).*>\; rel="last"# );
327 my $deleted_patron = $builder->build_object({ class => 'Koha::Patrons' });
328 my $deleted_patron_id = $deleted_patron->id;
329 $deleted_patron->delete;
331 $t->get_ok( "//$userid:$password@/api/v1/items/"
333 . "/pickup_locations?"
334 . "patron_id=" . $deleted_patron_id )
336 ->json_is( '/error' => 'Patron not found' );
340 $t->get_ok( "//$userid:$password@/api/v1/items/"
342 . "/pickup_locations?"
343 . "patron_id=" . $patron->id )
345 ->json_is( '/error' => 'Item not found' );
347 $schema->storage->txn_rollback;