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' } )->itemtype;
147 $item = $builder->build_sample_item(
148 { biblionumber => $biblio->biblionumber, itype => $itype } );
150 $t->get_ok( "//$userid:$password@/api/v1/items/" . $item->itemnumber )
151 ->status_is( 200, 'SWAGGER3.2.2' )
152 ->json_is( '/item_type_id' => $itype, 'item-level_itypes:0' )
153 ->json_is( '/effective_item_type_id' => $biblio->itemtype, 'item-level_itypes:0' );
155 t::lib::Mocks::mock_preference( 'item-level_itypes', 1 );
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, 'item-level_itype:1' )
160 ->json_is( '/effective_item_type_id' => $itype, 'item-level_itypes:1' );
162 $schema->storage->txn_rollback;
165 subtest 'pickup_locations() tests' => sub {
169 $schema->storage->txn_begin;
171 t::lib::Mocks::mock_preference( 'AllowHoldPolicyOverride', 0 );
173 # Small trick to ease testing
174 Koha::Libraries->search->update({ pickup_location => 0 });
176 my $library_1 = $builder->build_object({ class => 'Koha::Libraries', value => { marcorgcode => 'A', pickup_location => 1 } });
177 my $library_2 = $builder->build_object({ class => 'Koha::Libraries', value => { marcorgcode => 'B', pickup_location => 1 } });
178 my $library_3 = $builder->build_object({ class => 'Koha::Libraries', value => { marcorgcode => 'C', pickup_location => 1 } });
180 my $library_1_api = $library_1->to_api();
181 my $library_2_api = $library_2->to_api();
182 my $library_3_api = $library_3->to_api();
184 $library_1_api->{needs_override} = Mojo::JSON->false;
185 $library_2_api->{needs_override} = Mojo::JSON->false;
186 $library_3_api->{needs_override} = Mojo::JSON->true;
188 my $patron = $builder->build_object(
190 class => 'Koha::Patrons',
191 value => { userid => 'tomasito', flags => 0 }
194 my $password = 'thePassword123';
195 $patron->set_password( { password => $password, skip_validation => 1 } );
196 my $userid = $patron->userid;
199 source => 'UserPermission',
201 borrowernumber => $patron->borrowernumber,
203 code => 'place_holds',
208 my $item = $builder->build_sample_item();
210 my $item_class = Test::MockModule->new('Koha::Item');
214 my ( $self, $params ) = @_;
215 my $mock_patron = $params->{patron};
216 is( $mock_patron->borrowernumber,
217 $patron->borrowernumber, 'Patron passed correctly' );
218 return Koha::Libraries->search(
222 $library_1->branchcode,
223 $library_2->branchcode
227 { # we make sure no surprises in the order of the result
228 order_by => { '-asc' => 'marcorgcode' }
234 $t->get_ok( "//$userid:$password@/api/v1/items/"
236 . "/pickup_locations?patron_id=" . $patron->id )
237 ->json_is( [ $library_1_api, $library_2_api ] );
240 $t->get_ok( "//$userid:$password@/api/v1/items/"
242 . '/pickup_locations?'
243 . 'patron_id=' . $patron->id . '&q={"marc_org_code": { "-like": "A%" }}' )
244 ->json_is( [ $library_1_api ] );
246 t::lib::Mocks::mock_preference( 'AllowHoldPolicyOverride', 1 );
248 my $library_4 = $builder->build_object({ class => 'Koha::Libraries', value => { pickup_location => 0, marcorgcode => 'X' } });
249 my $library_5 = $builder->build_object({ class => 'Koha::Libraries', value => { pickup_location => 1, marcorgcode => 'Y' } });
251 my $library_5_api = $library_5->to_api();
252 $library_5_api->{needs_override} = Mojo::JSON->true;
254 $t->get_ok( "//$userid:$password@/api/v1/items/"
256 . "/pickup_locations?"
257 . "patron_id=" . $patron->id . "&_order_by=marc_org_code" )
258 ->json_is( [ $library_1_api, $library_2_api, $library_3_api, $library_5_api ] );
260 subtest 'Pagination and AllowHoldPolicyOverride tests' => sub {
264 t::lib::Mocks::mock_preference( 'AllowHoldPolicyOverride', 1 );
266 $t->get_ok( "//$userid:$password@/api/v1/items/" . $item->id . "/pickup_locations?" . "patron_id=" . $patron->id . "&_order_by=marc_org_code" . "&_per_page=1" )
267 ->json_is( [$library_1_api] )
268 ->header_is( 'X-Total-Count', '4', '4 is the count for libraries with pickup_location=1' )
269 ->header_is( 'X-Base-Total-Count', '4', '4 is the count for libraries with pickup_location=1' )
270 ->header_unlike( 'Link', qr|rel="prev"| )
271 ->header_like( 'Link', qr#(_per_page=1.*\&_page=2.*|_page=2.*\&_per_page=1.*)>\; rel="next"# )
272 ->header_like( 'Link', qr#(_per_page=1.*\&_page=1.*|_page=1.*\&_per_page=1).*>\; rel="first"# )
273 ->header_like( 'Link', qr#(_per_page=1.*\&_page=4.*|_page=4.*\&_per_page=1).*>\; rel="last"# );
275 $t->get_ok( "//$userid:$password@/api/v1/items/"
277 . "/pickup_locations?"
280 . "&_order_by=marc_org_code"
281 . "&_per_page=1&_page=3" ) # force the needs_override=1 check
282 ->json_is( [$library_3_api] )
283 ->header_is( 'X-Total-Count', '4', '4 is the count for libraries with pickup_location=1' )
284 ->header_is( 'X-Base-Total-Count', '4', '4 is the count for libraries with pickup_location=1' )
285 ->header_like( 'Link', qr#(_per_page=1.*\&_page=2.*|_page=2.*\&_per_page=1.*)>\; rel="prev"# )
286 ->header_like( 'Link', qr#(_per_page=1.*\&_page=4.*|_page=4.*\&_per_page=1.*)>\; rel="next"# )
287 ->header_like( 'Link', qr#(_per_page=1.*\&_page=1.*|_page=1.*\&_per_page=1).*>\; rel="first"# )
288 ->header_like( 'Link', qr#(_per_page=1.*\&_page=4.*|_page=4.*\&_per_page=1).*>\; rel="last"# );
290 t::lib::Mocks::mock_preference( 'AllowHoldPolicyOverride', 0 );
292 $t->get_ok( "//$userid:$password@/api/v1/items/" . $item->id . "/pickup_locations?" . "patron_id=" . $patron->id . "&_order_by=marc_org_code" . "&_per_page=1" )
293 ->json_is( [$library_1_api] )
294 ->header_is( 'X-Total-Count', '2' )
295 ->header_is( 'X-Base-Total-Count', '2' )
296 ->header_unlike( 'Link', qr|rel="prev"| )
297 ->header_like( 'Link', qr#(_per_page=1.*\&_page=2.*|_page=2.*\&_per_page=1.*)>\; rel="next"# )
298 ->header_like( 'Link', qr#(_per_page=1.*\&_page=1.*|_page=1.*\&_per_page=1).*>\; rel="first"# )
299 ->header_like( 'Link', qr#(_per_page=1.*\&_page=2.*|_page=2.*\&_per_page=1).*>\; rel="last"# );
302 my $deleted_patron = $builder->build_object({ class => 'Koha::Patrons' });
303 my $deleted_patron_id = $deleted_patron->id;
304 $deleted_patron->delete;
306 $t->get_ok( "//$userid:$password@/api/v1/items/"
308 . "/pickup_locations?"
309 . "patron_id=" . $deleted_patron_id )
311 ->json_is( '/error' => 'Patron not found' );
315 $t->get_ok( "//$userid:$password@/api/v1/items/"
317 . "/pickup_locations?"
318 . "patron_id=" . $patron->id )
320 ->json_is( '/error' => 'Item not found' );
322 $schema->storage->txn_rollback;