Bug 31128: Unit 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 => 12;
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     my $barcode = $item->barcode;
92     $item->delete;
93
94     $t->get_ok( "//$userid:$password@/api/v1/items?external_id=" . $item->barcode )
95       ->status_is(200)
96       ->json_is( '' => [] );
97
98     $schema->storage->txn_rollback;
99 };
100
101
102 subtest 'get() tests' => sub {
103
104     plan tests => 30;
105
106     $schema->storage->txn_begin;
107
108     my $item = $builder->build_sample_item;
109     my $patron = $builder->build_object({
110         class => 'Koha::Patrons',
111         value => { flags => 4 }
112     });
113
114     my $nonprivilegedpatron = $builder->build_object({
115         class => 'Koha::Patrons',
116         value => { flags => 0 }
117     });
118
119     my $password = 'thePassword123';
120
121     $nonprivilegedpatron->set_password({ password => $password, skip_validation => 1 });
122     my $userid = $nonprivilegedpatron->userid;
123
124     $t->get_ok( "//$userid:$password@/api/v1/items/" . $item->itemnumber )
125       ->status_is(403)
126       ->json_is( '/error' => 'Authorization failure. Missing required permission(s).' );
127
128     $patron->set_password({ password => $password, skip_validation => 1 });
129     $userid = $patron->userid;
130
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' );
134
135     my $non_existent_code = $item->itemnumber;
136     $item->delete;
137
138     $t->get_ok( "//$userid:$password@/api/v1/items/" . $non_existent_code )
139       ->status_is(404)
140       ->json_is( '/error' => 'Item not found' );
141
142     t::lib::Mocks::mock_preference( 'item-level_itypes', 0 );
143
144     my $biblio = $builder->build_sample_biblio;
145     my $itype =
146       $builder->build_object( { class => 'Koha::ItemTypes' } );
147     $item = $builder->build_sample_item(
148         { biblionumber => $biblio->biblionumber, itype => $itype->itemtype } );
149
150     isnt( $biblio->itemtype, $itype->itemtype, "Test biblio level itemtype and item level itemtype do not match");
151
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' );
156
157     t::lib::Mocks::mock_preference( 'item-level_itypes', 1 );
158
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' );
163
164
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();
169
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' );
174
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' );
180
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' );
186
187     $schema->storage->txn_rollback;
188 };
189
190 subtest 'pickup_locations() tests' => sub {
191
192     plan tests => 16;
193
194     $schema->storage->txn_begin;
195
196     t::lib::Mocks::mock_preference( 'AllowHoldPolicyOverride', 0 );
197
198     # Small trick to ease testing
199     Koha::Libraries->search->update({ pickup_location => 0 });
200
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 } });
204
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();
208
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;
212
213     my $patron = $builder->build_object(
214         {
215             class => 'Koha::Patrons',
216             value => { userid => 'tomasito', flags => 0 }
217         }
218     );
219     my $password = 'thePassword123';
220     $patron->set_password( { password => $password, skip_validation => 1 } );
221     my $userid = $patron->userid;
222     $builder->build(
223         {
224             source => 'UserPermission',
225             value  => {
226                 borrowernumber => $patron->borrowernumber,
227                 module_bit     => 6,
228                 code           => 'place_holds',
229             },
230         }
231     );
232
233     my $item = $builder->build_sample_item();
234
235     my $item_class = Test::MockModule->new('Koha::Item');
236     $item_class->mock(
237         'pickup_locations',
238         sub {
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(
244                 {
245                     branchcode => {
246                         '-in' => [
247                             $library_1->branchcode,
248                             $library_2->branchcode
249                         ]
250                     }
251                 },
252                 {   # we make sure no surprises in the order of the result
253                     order_by => { '-asc' => 'marcorgcode' }
254                 }
255             );
256         }
257     );
258
259     $t->get_ok( "//$userid:$password@/api/v1/items/"
260           . $item->id
261           . "/pickup_locations?patron_id=" . $patron->id )
262       ->json_is( [ $library_1_api, $library_2_api ] );
263
264     # filtering works!
265     $t->get_ok( "//$userid:$password@/api/v1/items/"
266           . $item->id
267           . '/pickup_locations?'
268           . 'patron_id=' . $patron->id . '&q={"marc_org_code": { "-like": "A%" }}' )
269       ->json_is( [ $library_1_api ] );
270
271     t::lib::Mocks::mock_preference( 'AllowHoldPolicyOverride', 1 );
272
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' } });
275
276     my $library_5_api = $library_5->to_api();
277     $library_5_api->{needs_override} = Mojo::JSON->true;
278
279     $t->get_ok( "//$userid:$password@/api/v1/items/"
280           . $item->id
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 ] );
284
285     subtest 'Pagination and AllowHoldPolicyOverride tests' => sub {
286
287         plan tests => 27;
288
289         t::lib::Mocks::mock_preference( 'AllowHoldPolicyOverride', 1 );
290
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"# );
299
300         $t->get_ok( "//$userid:$password@/api/v1/items/"
301               . $item->id
302               . "/pickup_locations?"
303               . "patron_id="
304               . $patron->id
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"# );
314
315         t::lib::Mocks::mock_preference( 'AllowHoldPolicyOverride', 0 );
316
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"# );
325     };
326
327     my $deleted_patron = $builder->build_object({ class => 'Koha::Patrons' });
328     my $deleted_patron_id = $deleted_patron->id;
329     $deleted_patron->delete;
330
331     $t->get_ok( "//$userid:$password@/api/v1/items/"
332           . $item->id
333           . "/pickup_locations?"
334           . "patron_id=" . $deleted_patron_id )
335       ->status_is( 400 )
336       ->json_is( '/error' => 'Patron not found' );
337
338     $item->delete;
339
340     $t->get_ok( "//$userid:$password@/api/v1/items/"
341           . $item->id
342           . "/pickup_locations?"
343           . "patron_id=" . $patron->id )
344       ->status_is( 404 )
345       ->json_is( '/error' => 'Item not found' );
346
347     $schema->storage->txn_rollback;
348 };