Bug 35581: Illbatchstatus* -> ILL::Batch::Status*
[koha.git] / t / db_dependent / api / v1 / ill_batches.t
1 #!/usr/bin/env perl
2
3 # This file is part of Koha.
4 #
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18 use Modern::Perl;
19
20 use Test::More tests => 5;
21 use Test::Mojo;
22
23 use t::lib::TestBuilder;
24 use t::lib::Mocks;
25
26 use JSON qw(encode_json);
27
28 use Koha::ILL::Batch;
29 use Koha::ILL::Batches;
30 use Koha::Illrequests;
31 use Koha::ILL::Batch::Statuses;
32 use Koha::Database;
33
34 my $schema  = Koha::Database->new->schema;
35 my $builder = t::lib::TestBuilder->new;
36
37 my $t = Test::Mojo->new('Koha::REST::V1');
38 t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 );
39
40 subtest 'list() tests' => sub {
41
42     plan tests => 21;
43
44     $schema->storage->txn_begin;
45
46     my $librarian = $builder->build_object(
47         {
48             class => 'Koha::Patrons',
49             value => {
50                 flags => 2**22    # 22 => ill
51             }
52         }
53     );
54
55     my $library = $builder->build_object( { class => 'Koha::Libraries' } );
56
57     my $password = 'sheev_is_da_boss!';
58     $librarian->set_password( { password => $password, skip_validation => 1 } );
59     my $userid = $librarian->userid;
60
61     my $batch_to_delete  = $builder->build_object( { class => 'Koha::ILL::Batches' } );
62     my $deleted_batch_id = $batch_to_delete->id;
63     $batch_to_delete->delete;
64
65     my $query = { ill_batch_id => [$deleted_batch_id] };
66
67     ## Authorized user tests
68     # No batches, so empty array should be returned
69     $t->get_ok( "//$userid:$password@/api/v1/ill/batches?q=" . encode_json($query) )->status_is(200)->json_is( [] );
70
71     my $batch_1 = $builder->build_object(
72         {
73             class => 'Koha::ILL::Batches',
74             value => {
75                 backend    => "Mock",
76                 patron_id  => $librarian->id,
77                 library_id => $library->id,
78             }
79         }
80     );
81
82     my $illrq = $builder->build_object(
83         {
84             class => 'Koha::Illrequests',
85             value => {
86                 batch_id       => $batch_1->id,
87                 borrowernumber => $librarian->id,
88             }
89         }
90     );
91
92     $query = { ill_batch_id => [ $batch_1->id ] };
93
94     # One batch created, should get returned
95     $t->get_ok( "//$userid:$password@/api/v1/ill/batches?q="
96             . encode_json($query) => { 'x-koha-embed' => '+strings,requests+count,patron,library' } )->status_is(200)
97         ->json_has( '/0/ill_batch_id', 'Batch ID' )->json_has( '/0/name', 'Batch name' )
98         ->json_has( '/0/backend',      'Backend name' )->json_has( '/0/patron_id', 'Borrowernumber' )
99         ->json_has( '/0/library_id',   'Branchcode' )->json_has( '/0/patron', 'patron embedded' )
100         ->json_has( '/0/library',      'branch embedded' )->json_has( '/0/requests_count', 'request count' );
101
102     # Create a second batch with a different name
103     my $batch_2 = $builder->build_object( { class => 'Koha::ILL::Batches' } );
104
105     $query = { ill_batch_id => [ $batch_1->id, $batch_2->id ] };
106
107     # Two batches created, they should both be returned
108     $t->get_ok( "//$userid:$password@/api/v1/ill/batches?q=" . encode_json($query) )->status_is(200)
109         ->json_has( '/0', 'has first batch' )->json_is( '/0/ill_batch_id', $batch_1->id )
110         ->json_has( '/1', 'has second batch' )->json_is( '/1/ill_batch_id', $batch_2->id );
111
112     my $patron = $builder->build_object(
113         {
114             class => 'Koha::Patrons',
115             value => {
116                 cardnumber => 999,
117                 flags      => 0
118             }
119         }
120     );
121
122     $patron->set_password( { password => $password, skip_validation => 1 } );
123     my $unauth_userid = $patron->userid;
124
125     # Unauthorized access
126     $t->get_ok("//$unauth_userid:$password@/api/v1/ill/batches")->status_is(403);
127
128     $schema->storage->txn_rollback;
129 };
130
131 subtest 'get() tests' => sub {
132
133     plan tests => 15;
134
135     $schema->storage->txn_begin;
136
137     my $librarian = $builder->build_object(
138         {
139             class => 'Koha::Patrons',
140             value => { flags => 2**22 }    # 22 => ill
141         }
142     );
143     my $password = 'Rebelz4DaWin';
144     $librarian->set_password( { password => $password, skip_validation => 1 } );
145     my $userid = $librarian->userid;
146
147     my $patron = $builder->build_object(
148         {
149             class => 'Koha::Patrons',
150             value => { flags => 0 }
151         }
152     );
153
154     my $library = $builder->build_object( { class => 'Koha::Libraries' } );
155
156     my $batch = $builder->build_object(
157         {
158             class => 'Koha::ILL::Batches',
159             value => {
160                 backend    => "Mock",
161                 patron_id  => $librarian->id,
162                 library_id => $library->id,
163             }
164         }
165     );
166
167     $patron->set_password( { password => $password, skip_validation => 1 } );
168     my $unauth_userid = $patron->userid;
169
170     $t->get_ok( "//$userid:$password@/api/v1/ill/batches/"
171             . $batch->id => { 'x-koha-embed' => '+strings,requests+count,patron,library' } )->status_is(200)
172         ->json_has( '/ill_batch_id', 'Batch ID' )->json_has( '/name', 'Batch name' )
173         ->json_has( '/backend',      'Backend name' )->json_has( '/patron_id', 'Borrowernumber' )
174         ->json_has( '/library_id',   'Branchcode' )->json_has( '/patron', 'patron embedded' )
175         ->json_has( '/library',      'library embedded' )->json_has( '/requests_count', 'request count' );
176
177     $t->get_ok( "//$unauth_userid:$password@/api/v1/ill/batches/" . $batch->id )->status_is(403);
178
179     my $batch_to_delete = $builder->build_object( { class => 'Koha::ILL::Batches' } );
180     my $non_existent_id = $batch_to_delete->id;
181     $batch_to_delete->delete;
182
183     $t->get_ok("//$userid:$password@/api/v1/ill/batches/$non_existent_id")->status_is(404)
184         ->json_is( '/error' => 'ILL batch not found' );
185
186     $schema->storage->txn_rollback;
187 };
188
189 subtest 'add() tests' => sub {
190
191     plan tests => 20;
192
193     $schema->storage->txn_begin;
194
195     my $librarian = $builder->build_object(
196         {
197             class => 'Koha::Patrons',
198             value => { flags => 2**22 }    # 22 => ill
199         }
200     );
201     my $password = 'v4d3rRox';
202     $librarian->set_password( { password => $password, skip_validation => 1 } );
203     my $userid = $librarian->userid;
204
205     my $patron = $builder->build_object(
206         {
207             class => 'Koha::Patrons',
208             value => { flags => 0 }
209         }
210     );
211
212     $patron->set_password( { password => $password, skip_validation => 1 } );
213     my $unauth_userid = $patron->userid;
214
215     my $library = $builder->build_object( { class => 'Koha::Libraries' } );
216
217     my $batch_status = $builder->build_object( { class => 'Koha::ILL::Batch::Statuses' } );
218
219     my $batch_metadata = {
220         name        => "Anakin's requests",
221         backend     => "Mock",
222         cardnumber  => $librarian->cardnumber,
223         library_id  => $library->branchcode,
224         status_code => $batch_status->code
225     };
226
227     # Unauthorized attempt to write
228     $t->post_ok( "//$unauth_userid:$password@/api/v1/ill/batches" => json => $batch_metadata )->status_is(403);
229
230     # Authorized attempt to write invalid data
231     my $batch_with_invalid_field = {
232         %{$batch_metadata},
233         doh => 1
234     };
235
236     $t->post_ok( "//$userid:$password@/api/v1/ill/batches" => json => $batch_with_invalid_field )->status_is(400)
237         ->json_is(
238         "/errors" => [
239             {
240                 message => "Properties not allowed: doh.",
241                 path    => "/body"
242             }
243         ]
244         );
245
246     # Authorized attempt to write
247     my $batch_id =
248         $t->post_ok(
249         "//$userid:$password@/api/v1/ill/batches" => { 'x-koha-embed' => '+strings,requests+count,patron,library' } =>
250             json => $batch_metadata )->status_is(201)->json_is( '/name' => $batch_metadata->{name} )
251         ->json_is( '/backend'    => $batch_metadata->{backend} )->json_is( '/patron_id' => $librarian->borrowernumber )
252         ->json_is( '/library_id' => $batch_metadata->{library_id} )->json_is( '/status_code' => $batch_status->code )
253         ->json_has('/patron')->json_has('/_strings/status_code')->json_has('/_strings/library_id')
254         ->json_has('/requests_count')->json_has('/library');
255
256     # Authorized attempt to create with null id
257     $batch_metadata->{id} = undef;
258     $t->post_ok( "//$userid:$password@/api/v1/ill/batches" => json => $batch_metadata )->status_is(400)
259         ->json_has('/errors');
260
261     $schema->storage->txn_rollback;
262 };
263
264 subtest 'update() tests' => sub {
265
266     plan tests => 15;
267
268     $schema->storage->txn_begin;
269
270     my $librarian = $builder->build_object(
271         {
272             class => 'Koha::Patrons',
273             value => { flags => 2**22 }    # 22 => ill
274         }
275     );
276     my $password = 'aw3s0m3y0d41z';
277     $librarian->set_password( { password => $password, skip_validation => 1 } );
278     my $userid = $librarian->userid;
279
280     my $patron = $builder->build_object(
281         {
282             class => 'Koha::Patrons',
283             value => { flags => 0 }
284         }
285     );
286
287     $patron->set_password( { password => $password, skip_validation => 1 } );
288     my $unauth_userid = $patron->userid;
289
290     my $library  = $builder->build_object( { class => 'Koha::Libraries' } );
291     my $batch_id = $builder->build_object( { class => 'Koha::ILL::Batches' } )->id;
292
293     # Unauthorized attempt to update
294     $t->put_ok( "//$unauth_userid:$password@/api/v1/ill/batches/$batch_id" => json =>
295             { name => 'These are not the droids you are looking for' } )->status_is(403);
296
297     my $batch_status = $builder->build_object( { class => 'Koha::ILL::Batch::Statuses' } );
298
299     # Attempt partial update on a PUT
300     my $batch_with_missing_field = {
301         backend     => "Mock",
302         patron_id   => $librarian->borrowernumber,
303         library_id  => $library->branchcode,
304         status_code => $batch_status->code
305     };
306
307     $t->put_ok( "//$userid:$password@/api/v1/ill/batches/$batch_id" => json => $batch_with_missing_field )
308         ->status_is(400)->json_is( "/errors" => [ { message => "Missing property.", path => "/body/name" } ] );
309
310     # Full object update on PUT
311     my $batch_with_updated_field = {
312         name        => "Master Ploo Koon",
313         backend     => "Mock",
314         patron_id   => $librarian->borrowernumber,
315         library_id  => $library->branchcode,
316         status_code => $batch_status->code
317     };
318
319     $t->put_ok( "//$userid:$password@/api/v1/ill/batches/$batch_id" => json => $batch_with_updated_field )
320         ->status_is(200)->json_is( '/name' => 'Master Ploo Koon' );
321
322     # Authorized attempt to write invalid data
323     my $batch_with_invalid_field = {
324         doh     => 1,
325         name    => "Master Mace Windu",
326         backend => "Mock"
327     };
328
329     $t->put_ok( "//$userid:$password@/api/v1/ill/batches/$batch_id" => json => $batch_with_invalid_field )
330         ->status_is(400)->json_is(
331         "/errors" => [
332             {
333                 message => "Properties not allowed: doh.",
334                 path    => "/body"
335             }
336         ]
337         );
338
339     my $batch_to_delete = $builder->build_object( { class => 'Koha::ILL::Batches' } );
340     my $non_existent_id = $batch_to_delete->id;
341     $batch_to_delete->delete;
342
343     $t->put_ok( "//$userid:$password@/api/v1/ill/batches/$non_existent_id" => json => $batch_with_updated_field )
344         ->status_is(404);
345
346     # Wrong method (POST)
347     $batch_with_updated_field->{id} = 2;
348
349     $t->post_ok( "//$userid:$password@/api/v1/ill/batches/$batch_id" => json => $batch_with_updated_field )
350         ->status_is(404);
351
352     $schema->storage->txn_rollback;
353 };
354
355 subtest 'delete() tests' => sub {
356
357     plan tests => 6;
358
359     $schema->storage->txn_begin;
360
361     my $librarian = $builder->build_object(
362         {
363             class => 'Koha::Patrons',
364             value => { flags => 2**22 }    # 22 => ill
365         }
366     );
367     my $password = 's1th43v3r!';
368     $librarian->set_password( { password => $password, skip_validation => 1 } );
369     my $userid = $librarian->userid;
370
371     my $patron = $builder->build_object(
372         {
373             class => 'Koha::Patrons',
374             value => { flags => 0 }
375         }
376     );
377
378     $patron->set_password( { password => $password, skip_validation => 1 } );
379     my $unauth_userid = $patron->userid;
380
381     my $batch_id = $builder->build_object( { class => 'Koha::ILL::Batches' } )->id;
382
383     # Unauthorized attempt to delete
384     $t->delete_ok("//$unauth_userid:$password@/api/v1/ill/batches/$batch_id")->status_is(403);
385
386     $t->delete_ok("//$userid:$password@/api/v1/ill/batches/$batch_id")->status_is(204);
387
388     $t->delete_ok("//$userid:$password@/api/v1/ill/batches/$batch_id")->status_is(404);
389
390     $schema->storage->txn_rollback;
391 };