Bug 27931: 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 => 9;
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     $schema->storage->txn_rollback;
143 };
144
145 subtest 'pickup_locations() tests' => sub {
146
147     plan tests => 15;
148
149     $schema->storage->txn_begin;
150
151     t::lib::Mocks::mock_preference( 'AllowHoldPolicyOverride', 0 );
152
153     # Small trick to ease testing
154     Koha::Libraries->search->update({ pickup_location => 0 });
155
156     my $library_1 = $builder->build_object({ class => 'Koha::Libraries', value => { marcorgcode => 'A', pickup_location => 1 } });
157     my $library_2 = $builder->build_object({ class => 'Koha::Libraries', value => { marcorgcode => 'B', pickup_location => 1 } });
158     my $library_3 = $builder->build_object({ class => 'Koha::Libraries', value => { marcorgcode => 'C', pickup_location => 1 } });
159
160     my $library_1_api = $library_1->to_api();
161     my $library_2_api = $library_2->to_api();
162     my $library_3_api = $library_3->to_api();
163
164     $library_1_api->{needs_override} = Mojo::JSON->false;
165     $library_2_api->{needs_override} = Mojo::JSON->false;
166     $library_3_api->{needs_override} = Mojo::JSON->true;
167
168     my $patron = $builder->build_object(
169         {
170             class => 'Koha::Patrons',
171             value => { userid => 'tomasito', flags => 0 }
172         }
173     );
174     my $password = 'thePassword123';
175     $patron->set_password( { password => $password, skip_validation => 1 } );
176     my $userid = $patron->userid;
177     $builder->build(
178         {
179             source => 'UserPermission',
180             value  => {
181                 borrowernumber => $patron->borrowernumber,
182                 module_bit     => 6,
183                 code           => 'place_holds',
184             },
185         }
186     );
187
188     my $item = $builder->build_sample_item();
189
190     my $item_class = Test::MockModule->new('Koha::Item');
191     $item_class->mock(
192         'pickup_locations',
193         sub {
194             my ( $self, $params ) = @_;
195             my $mock_patron = $params->{patron};
196             is( $mock_patron->borrowernumber,
197                 $patron->borrowernumber, 'Patron passed correctly' );
198             return Koha::Libraries->search(
199                 {
200                     branchcode => {
201                         '-in' => [
202                             $library_1->branchcode,
203                             $library_2->branchcode
204                         ]
205                     }
206                 },
207                 {   # we make sure no surprises in the order of the result
208                     order_by => { '-asc' => 'marcorgcode' }
209                 }
210             );
211         }
212     );
213
214     $t->get_ok( "//$userid:$password@/api/v1/items/"
215           . $item->id
216           . "/pickup_locations?patron_id=" . $patron->id )
217       ->json_is( [ $library_1_api, $library_2_api ] );
218
219     # filtering works!
220     $t->get_ok( "//$userid:$password@/api/v1/items/"
221           . $item->id
222           . '/pickup_locations?'
223           . 'patron_id=' . $patron->id . '&q={"marc_org_code": { "-like": "A%" }}' )
224       ->json_is( [ $library_1_api ] );
225
226     t::lib::Mocks::mock_preference( 'AllowHoldPolicyOverride', 1 );
227
228     my $library_4 = $builder->build_object({ class => 'Koha::Libraries', value => { pickup_location => 0, marcorgcode => 'X' } });
229     my $library_5 = $builder->build_object({ class => 'Koha::Libraries', value => { pickup_location => 1, marcorgcode => 'Y' } });
230
231     my $library_5_api = $library_5->to_api();
232     $library_5_api->{needs_override} = Mojo::JSON->true;
233
234     $t->get_ok( "//$userid:$password@/api/v1/items/"
235           . $item->id
236           . "/pickup_locations?"
237           . "patron_id=" . $patron->id . "&_order_by=marc_org_code" )
238       ->json_is( [ $library_1_api, $library_2_api, $library_3_api, $library_5_api ] );
239
240     my $deleted_patron = $builder->build_object({ class => 'Koha::Patrons' });
241     my $deleted_patron_id = $deleted_patron->id;
242     $deleted_patron->delete;
243
244     $t->get_ok( "//$userid:$password@/api/v1/items/"
245           . $item->id
246           . "/pickup_locations?"
247           . "patron_id=" . $deleted_patron_id )
248       ->status_is( 400 )
249       ->json_is( '/error' => 'Patron not found' );
250
251     $item->delete;
252
253     $t->get_ok( "//$userid:$password@/api/v1/items/"
254           . $item->id
255           . "/pickup_locations?"
256           . "patron_id=" . $patron->id )
257       ->status_is( 404 )
258       ->json_is( '/error' => 'Item not found' );
259
260     $schema->storage->txn_rollback;
261 };