Bug 30719: (QA follow-up) Rename illbatches endpoint to ill/batches
[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 Koha::Illbatch;
27 use Koha::Illbatches;
28 use Koha::Illrequests;
29 use Koha::IllbatchStatuses;
30 use Koha::Database;
31
32 my $schema  = Koha::Database->new->schema;
33 my $builder = t::lib::TestBuilder->new;
34
35 my $t = Test::Mojo->new('Koha::REST::V1');
36 t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 );
37
38 subtest 'list() tests' => sub {
39
40     plan tests => 19;
41
42     $schema->storage->txn_begin;
43
44     Koha::Illbatches->search->delete;
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 $branch = $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     ## Authorized user tests
62     # No batches, so empty array should be returned
63     $t->get_ok("//$userid:$password@/api/v1/ill/batches")->status_is(200)->json_is( [] );
64
65     my $batch = $builder->build_object(
66         {
67             class => 'Koha::Illbatches',
68             value => {
69                 name           => "PapaPalpatine",
70                 backend        => "Mock",
71                 borrowernumber => $librarian->borrowernumber,
72                 branchcode     => $branch->branchcode
73             }
74         }
75     );
76
77     my $illrq = $builder->build(
78         {
79             source => 'Illrequest',
80             value  => {
81                 borrowernumber => $librarian->borrowernumber,
82                 batch_id       => $batch->id
83             }
84         }
85     );
86
87     # One batch created, should get returned
88     $t->get_ok("//$userid:$password@/api/v1/ill/batches")->status_is(200)->json_has( '/0/batch_id', 'Batch ID' )
89         ->json_has( '/0/name',           'Batch name' )->json_has( '/0/backend', 'Backend name' )
90         ->json_has( '/0/patron_id',      'Borrowernumber' )->json_has( '/0/library_id', 'Branchcode' )
91         ->json_has( '/0/patron',         'patron embedded' )->json_has( '/0/branch', 'branch embedded' )
92         ->json_has( '/0/requests_count', 'request count' );
93
94     # Try to create a second batch with the same name, this should fail
95     my $another_batch = $builder->build_object( { class => 'Koha::Illbatches', value => { name => $batch->name } } );
96
97     # Create a second batch with a different name
98     my $batch_with_another_name = $builder->build_object( { class => 'Koha::Illbatches' } );
99
100     # Two batches created, they should both be returned
101     $t->get_ok("//$userid:$password@/api/v1/ill/batches")->status_is(200)->json_has( '/0', 'has first batch' )
102         ->json_has( '/1', 'has second batch' );
103
104     my $patron = $builder->build_object(
105         {
106             class => 'Koha::Patrons',
107             value => {
108                 cardnumber => 999,
109                 flags      => 0
110             }
111         }
112     );
113
114     $patron->set_password( { password => $password, skip_validation => 1 } );
115     my $unauth_userid = $patron->userid;
116
117     # Unauthorized access
118     $t->get_ok("//$unauth_userid:$password@/api/v1/ill/batches")->status_is(403);
119
120     $schema->storage->txn_rollback;
121 };
122
123 subtest 'get() tests' => sub {
124
125     plan tests => 15;
126
127     $schema->storage->txn_begin;
128
129     my $librarian = $builder->build_object(
130         {
131             class => 'Koha::Patrons',
132             value => { flags => 2**22 }    # 22 => ill
133         }
134     );
135     my $password = 'Rebelz4DaWin';
136     $librarian->set_password( { password => $password, skip_validation => 1 } );
137     my $userid = $librarian->userid;
138
139     my $patron = $builder->build_object(
140         {
141             class => 'Koha::Patrons',
142             value => { flags => 0 }
143         }
144     );
145
146     my $branch = $builder->build_object( { class => 'Koha::Libraries' } );
147
148     my $batch = $builder->build_object(
149         {
150             class => 'Koha::Illbatches',
151             value => {
152                 name           => "LeiaOrgana",
153                 backend        => "Mock",
154                 borrowernumber => $librarian->borrowernumber,
155                 branchcode     => $branch->branchcode
156             }
157         }
158     );
159
160     $patron->set_password( { password => $password, skip_validation => 1 } );
161     my $unauth_userid = $patron->userid;
162
163     $t->get_ok( "//$userid:$password@/api/v1/ill/batches/" . $batch->id )->status_is(200)
164         ->json_has( '/batch_id',   'Batch ID' )->json_has( '/name', 'Batch name' )
165         ->json_has( '/backend',    'Backend name' )->json_has( '/patron_id', 'Borrowernumber' )
166         ->json_has( '/library_id', 'Branchcode' )->json_has( '/patron', 'patron embedded' )
167         ->json_has( '/branch',     'branch embedded' )->json_has( '/requests_count', 'request count' );
168
169     $t->get_ok( "//$unauth_userid:$password@/api/v1/ill/batches/" . $batch->id )->status_is(403);
170
171     my $batch_to_delete = $builder->build_object( { class => 'Koha::Illbatches' } );
172     my $non_existent_id = $batch_to_delete->id;
173     $batch_to_delete->delete;
174
175     $t->get_ok("//$userid:$password@/api/v1/ill/batches/$non_existent_id")->status_is(404)
176         ->json_is( '/error' => 'ILL batch not found' );
177
178     $schema->storage->txn_rollback;
179 };
180
181 subtest 'add() tests' => sub {
182
183     plan tests => 19;
184
185     $schema->storage->txn_begin;
186
187     my $librarian = $builder->build_object(
188         {
189             class => 'Koha::Patrons',
190             value => { flags => 2**22 }    # 22 => ill
191         }
192     );
193     my $password = 'v4d3rRox';
194     $librarian->set_password( { password => $password, skip_validation => 1 } );
195     my $userid = $librarian->userid;
196
197     my $patron = $builder->build_object(
198         {
199             class => 'Koha::Patrons',
200             value => { flags => 0 }
201         }
202     );
203
204     $patron->set_password( { password => $password, skip_validation => 1 } );
205     my $unauth_userid = $patron->userid;
206
207     my $branch = $builder->build_object( { class => 'Koha::Libraries' } );
208
209     my $batch_status = $builder->build_object( { class => 'Koha::IllbatchStatuses' } );
210
211     my $batch_metadata = {
212         name       => "Anakin's requests",
213         backend    => "Mock",
214         cardnumber => $librarian->cardnumber,
215         library_id => $branch->branchcode,
216         statuscode => $batch_status->code
217     };
218
219     # Unauthorized attempt to write
220     $t->post_ok( "//$unauth_userid:$password@/api/v1/ill/batches" => json => $batch_metadata )->status_is(403);
221
222     # Authorized attempt to write invalid data
223     my $batch_with_invalid_field = {
224         %{$batch_metadata},
225         doh => 1
226     };
227
228     $t->post_ok( "//$userid:$password@/api/v1/ill/batches" => json => $batch_with_invalid_field )->status_is(400)
229         ->json_is(
230         "/errors" => [
231             {
232                 message => "Properties not allowed: doh.",
233                 path    => "/body"
234             }
235         ]
236         );
237
238     # Authorized attempt to write
239     my $batch_id =
240         $t->post_ok( "//$userid:$password@/api/v1/ill/batches" => json => $batch_metadata )->status_is(201)
241         ->json_is( '/name'       => $batch_metadata->{name} )->json_is( '/backend' => $batch_metadata->{backend} )
242         ->json_is( '/patron_id'  => $librarian->borrowernumber )
243         ->json_is( '/library_id' => $batch_metadata->{library_id} )->json_is( '/statuscode' => $batch_status->code )
244         ->json_has('/patron')->json_has('/status')->json_has('/requests_count')->json_has('/branch');
245
246     # Authorized attempt to create with null id
247     $batch_metadata->{id} = undef;
248     $t->post_ok( "//$userid:$password@/api/v1/ill/batches" => json => $batch_metadata )->status_is(400)
249         ->json_has('/errors');
250
251     $schema->storage->txn_rollback;
252 };
253
254 subtest 'update() tests' => sub {
255
256     plan tests => 15;
257
258     $schema->storage->txn_begin;
259
260     my $librarian = $builder->build_object(
261         {
262             class => 'Koha::Patrons',
263             value => { flags => 2**22 }    # 22 => ill
264         }
265     );
266     my $password = 'aw3s0m3y0d41z';
267     $librarian->set_password( { password => $password, skip_validation => 1 } );
268     my $userid = $librarian->userid;
269
270     my $patron = $builder->build_object(
271         {
272             class => 'Koha::Patrons',
273             value => { flags => 0 }
274         }
275     );
276
277     $patron->set_password( { password => $password, skip_validation => 1 } );
278     my $unauth_userid = $patron->userid;
279
280     my $branch = $builder->build_object( { class => 'Koha::Libraries' } );
281
282     my $batch_id = $builder->build_object( { class => 'Koha::Illbatches' } )->id;
283
284     # Unauthorized attempt to update
285     $t->put_ok( "//$unauth_userid:$password@/api/v1/ill/batches/$batch_id" => json =>
286             { name => 'These are not the droids you are looking for' } )->status_is(403);
287
288     my $batch_status = $builder->build_object( { class => 'Koha::IllbatchStatuses' } );
289
290     # Attempt partial update on a PUT
291     my $batch_with_missing_field = {
292         backend    => "Mock",
293         patron_id  => $librarian->borrowernumber,
294         library_id => $branch->branchcode,
295         statuscode => $batch_status->code
296     };
297
298     $t->put_ok( "//$userid:$password@/api/v1/ill/batches/$batch_id" => json => $batch_with_missing_field )
299         ->status_is(400)->json_is( "/errors" => [ { message => "Missing property.", path => "/body/name" } ] );
300
301     # Full object update on PUT
302     my $batch_with_updated_field = {
303         name       => "Master Ploo Koon",
304         backend    => "Mock",
305         patron_id  => $librarian->borrowernumber,
306         library_id => $branch->branchcode,
307         statuscode => $batch_status->code
308     };
309
310     $t->put_ok( "//$userid:$password@/api/v1/ill/batches/$batch_id" => json => $batch_with_updated_field )
311         ->status_is(200)->json_is( '/name' => 'Master Ploo Koon' );
312
313     # Authorized attempt to write invalid data
314     my $batch_with_invalid_field = {
315         doh     => 1,
316         name    => "Master Mace Windu",
317         backend => "Mock"
318     };
319
320     $t->put_ok( "//$userid:$password@/api/v1/ill/batches/$batch_id" => json => $batch_with_invalid_field )
321         ->status_is(400)->json_is(
322         "/errors" => [
323             {
324                 message => "Properties not allowed: doh.",
325                 path    => "/body"
326             }
327         ]
328         );
329
330     my $batch_to_delete = $builder->build_object( { class => 'Koha::Cities' } );
331     my $non_existent_id = $batch_to_delete->id;
332     $batch_to_delete->delete;
333
334     $t->put_ok( "//$userid:$password@/api/v1/ill/batches/$non_existent_id" => json => $batch_with_updated_field )
335         ->status_is(404);
336
337     # Wrong method (POST)
338     $batch_with_updated_field->{id} = 2;
339
340     $t->post_ok( "//$userid:$password@/api/v1/ill/batches/$batch_id" => json => $batch_with_updated_field )
341         ->status_is(404);
342
343     $schema->storage->txn_rollback;
344 };
345
346 subtest 'delete() tests' => sub {
347
348     plan tests => 6;
349
350     $schema->storage->txn_begin;
351
352     my $librarian = $builder->build_object(
353         {
354             class => 'Koha::Patrons',
355             value => { flags => 2**22 }    # 22 => ill
356         }
357     );
358     my $password = 's1th43v3r!';
359     $librarian->set_password( { password => $password, skip_validation => 1 } );
360     my $userid = $librarian->userid;
361
362     my $patron = $builder->build_object(
363         {
364             class => 'Koha::Patrons',
365             value => { flags => 0 }
366         }
367     );
368
369     $patron->set_password( { password => $password, skip_validation => 1 } );
370     my $unauth_userid = $patron->userid;
371
372     my $batch_id = $builder->build_object( { class => 'Koha::Illbatches' } )->id;
373
374     # Unauthorized attempt to delete
375     $t->delete_ok("//$unauth_userid:$password@/api/v1/ill/batches/$batch_id")->status_is(403);
376
377     $t->delete_ok("//$userid:$password@/api/v1/ill/batches/$batch_id")->status_is(204);
378
379     $t->delete_ok("//$userid:$password@/api/v1/ill/batches/$batch_id")->status_is(404);
380
381     $schema->storage->txn_rollback;
382 };