]> git.koha-community.org Git - koha.git/blob - t/db_dependent/api/v1/items.t
Bug 35167: Regression tests
[koha.git] / t / db_dependent / api / v1 / items.t
1 #!/usr/bin/env perl
2
3 # Copyright 2016 Koha-Suomi
4 #
5 # This file is part of Koha.
6 #
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.
11 #
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.
16 #
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>.
19
20 use Modern::Perl;
21
22 use Test::More tests => 3;
23 use Test::Mojo;
24 use Test::Warn;
25
26 use t::lib::TestBuilder;
27 use t::lib::Mocks;
28
29 use C4::Auth;
30 use Koha::Items;
31 use Koha::Database;
32
33 my $schema  = Koha::Database->new->schema;
34 my $builder = t::lib::TestBuilder->new;
35
36 t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 );
37
38 my $t = Test::Mojo->new('Koha::REST::V1');
39
40 subtest 'list() tests' => sub {
41
42     plan tests => 15;
43
44     $schema->storage->txn_begin;
45
46     my $item   = $builder->build_sample_item;
47     my $patron = $builder->build_object(
48         {
49             class => 'Koha::Patrons',
50             value => { flags => 4 }
51         }
52     );
53
54     # Make sure we have at least 10 items
55     for ( 1..10 ) {
56         $builder->build_sample_item;
57     }
58
59     my $nonprivilegedpatron = $builder->build_object(
60         {
61             class => 'Koha::Patrons',
62             value => { flags => 0 }
63         }
64     );
65
66     my $password = 'thePassword123';
67
68     $nonprivilegedpatron->set_password(
69         { password => $password, skip_validation => 1 } );
70     my $userid = $nonprivilegedpatron->userid;
71
72     $t->get_ok( "//$userid:$password@/api/v1/items" )
73       ->status_is(403)
74       ->json_is(
75         '/error' => 'Authorization failure. Missing required permission(s).' );
76
77     $patron->set_password( { password => $password, skip_validation => 1 } );
78     $userid = $patron->userid;
79
80     $t->get_ok( "//$userid:$password@/api/v1/items?_per_page=10" )
81       ->status_is( 200, 'SWAGGER3.2.2' );
82
83     my $response_count = scalar @{ $t->tx->res->json };
84
85     is( $response_count, 10, 'The API returns 10 items' );
86
87     $t->get_ok( "//$userid:$password@/api/v1/items?external_id=" . $item->barcode )
88       ->status_is(200)
89       ->json_is( '' => [ $item->to_api ], 'SWAGGER3.3.2');
90
91     $t->get_ok( "//$userid:$password@/api/v1/items?external_id=" . $item->barcode => {'x-koha-embed' => 'biblio'} )
92       ->status_is(200)
93       ->json_is( '' => [ { %{$item->to_api}, biblio => $item->biblio->to_api } ], 'SWAGGER3.3.2');
94
95
96     my $barcode = $item->barcode;
97     $item->delete;
98
99     $t->get_ok( "//$userid:$password@/api/v1/items?external_id=" . $item->barcode )
100       ->status_is(200)
101       ->json_is( '' => [] );
102
103     $schema->storage->txn_rollback;
104 };
105
106
107 subtest 'get() tests' => sub {
108
109     plan tests => 34;
110
111     $schema->storage->txn_begin;
112
113     my $item = $builder->build_sample_item;
114     my $patron = $builder->build_object({
115         class => 'Koha::Patrons',
116         value => { flags => 4 }
117     });
118
119     my $nonprivilegedpatron = $builder->build_object({
120         class => 'Koha::Patrons',
121         value => { flags => 0 }
122     });
123
124     my $password = 'thePassword123';
125
126     $nonprivilegedpatron->set_password({ password => $password, skip_validation => 1 });
127     my $userid = $nonprivilegedpatron->userid;
128
129     $t->get_ok( "//$userid:$password@/api/v1/items/" . $item->itemnumber )
130       ->status_is(403)
131       ->json_is( '/error' => 'Authorization failure. Missing required permission(s).' );
132
133     $patron->set_password({ password => $password, skip_validation => 1 });
134     $userid = $patron->userid;
135
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' );
139
140     my $non_existent_code = $item->itemnumber;
141     $item->delete;
142
143     $t->get_ok( "//$userid:$password@/api/v1/items/" . $non_existent_code )
144       ->status_is(404)
145       ->json_is( '/error' => 'Item not found' );
146
147     t::lib::Mocks::mock_preference( 'item-level_itypes', 0 );
148
149     my $biblio = $builder->build_sample_biblio;
150     my $itype =
151       $builder->build_object( { class => 'Koha::ItemTypes' } );
152     $item = $builder->build_sample_item(
153         { biblionumber => $biblio->biblionumber, itype => $itype->itemtype } );
154
155     isnt( $biblio->itemtype, $itype->itemtype, "Test biblio level itemtype and item level itemtype do not match");
156
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' );
161
162     t::lib::Mocks::mock_preference( 'item-level_itypes', 1 );
163
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' );
168
169
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();
174
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' );
179
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' );
185
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'
191         );
192
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' );
198
199     $schema->storage->txn_rollback;
200 };
201
202 subtest 'pickup_locations() tests' => sub {
203
204     plan tests => 16;
205
206     $schema->storage->txn_begin;
207
208     t::lib::Mocks::mock_preference( 'AllowHoldPolicyOverride', 0 );
209
210     # Small trick to ease testing
211     Koha::Libraries->search->update({ pickup_location => 0 });
212
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 } });
216
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();
220
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;
224
225     my $patron = $builder->build_object(
226         {
227             class => 'Koha::Patrons',
228             value => { userid => 'tomasito', flags => 0 }
229         }
230     );
231     my $password = 'thePassword123';
232     $patron->set_password( { password => $password, skip_validation => 1 } );
233     my $userid = $patron->userid;
234     $builder->build(
235         {
236             source => 'UserPermission',
237             value  => {
238                 borrowernumber => $patron->borrowernumber,
239                 module_bit     => 6,
240                 code           => 'place_holds',
241             },
242         }
243     );
244
245     my $item = $builder->build_sample_item();
246
247     my $item_class = Test::MockModule->new('Koha::Item');
248     $item_class->mock(
249         'pickup_locations',
250         sub {
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(
256                 {
257                     branchcode => {
258                         '-in' => [
259                             $library_1->branchcode,
260                             $library_2->branchcode
261                         ]
262                     }
263                 },
264                 {   # we make sure no surprises in the order of the result
265                     order_by => { '-asc' => 'marcorgcode' }
266                 }
267             );
268         }
269     );
270
271     $t->get_ok( "//$userid:$password@/api/v1/items/"
272           . $item->id
273           . "/pickup_locations?patron_id=" . $patron->id )
274       ->json_is( [ $library_1_api, $library_2_api ] );
275
276     # filtering works!
277     $t->get_ok( "//$userid:$password@/api/v1/items/"
278           . $item->id
279           . '/pickup_locations?'
280           . 'patron_id=' . $patron->id . '&q={"marc_org_code": { "-like": "A%" }}' )
281       ->json_is( [ $library_1_api ] );
282
283     t::lib::Mocks::mock_preference( 'AllowHoldPolicyOverride', 1 );
284
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' } });
287
288     my $library_5_api = $library_5->to_api();
289     $library_5_api->{needs_override} = Mojo::JSON->true;
290
291     $t->get_ok( "//$userid:$password@/api/v1/items/"
292           . $item->id
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 ] );
296
297     subtest 'Pagination and AllowHoldPolicyOverride tests' => sub {
298
299         plan tests => 27;
300
301         t::lib::Mocks::mock_preference( 'AllowHoldPolicyOverride', 1 );
302
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"# );
311
312         $t->get_ok( "//$userid:$password@/api/v1/items/"
313               . $item->id
314               . "/pickup_locations?"
315               . "patron_id="
316               . $patron->id
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"# );
326
327         t::lib::Mocks::mock_preference( 'AllowHoldPolicyOverride', 0 );
328
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"# );
337     };
338
339     my $deleted_patron = $builder->build_object({ class => 'Koha::Patrons' });
340     my $deleted_patron_id = $deleted_patron->id;
341     $deleted_patron->delete;
342
343     $t->get_ok( "//$userid:$password@/api/v1/items/"
344           . $item->id
345           . "/pickup_locations?"
346           . "patron_id=" . $deleted_patron_id )
347       ->status_is( 400 )
348       ->json_is( '/error' => 'Patron not found' );
349
350     $item->delete;
351
352     $t->get_ok( "//$userid:$password@/api/v1/items/"
353           . $item->id
354           . "/pickup_locations?"
355           . "patron_id=" . $patron->id )
356       ->status_is( 404 )
357       ->json_is( '/error' => 'Item not found' );
358
359     $schema->storage->txn_rollback;
360 };