Bug 25513: Regression tests
[koha.git] / t / db_dependent / api / v1 / acquisitions_orders.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 use Test::Warn;
23
24 use t::lib::TestBuilder;
25 use t::lib::Mocks;
26
27 use Koha::Acquisition::Orders;
28 use Koha::Database;
29
30 my $schema  = Koha::Database->new->schema;
31 my $builder = t::lib::TestBuilder->new;
32
33 t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 );
34
35 my $t = Test::Mojo->new('Koha::REST::V1');
36
37 subtest 'list() tests' => sub {
38     plan tests => 8;
39
40     $schema->storage->txn_begin;
41
42     my $patron = $builder->build_object({
43         class => 'Koha::Patrons',
44         value => { flags => 1 }
45     });
46     my $password = 'thePassword123';
47     $patron->set_password({ password => $password, skip_validation => 1 });
48     my $userid = $patron->userid;
49
50     my $basket = $builder->build_object({ class => 'Koha::Acquisition::Baskets' });
51     # Create test context
52     my $order = $builder->build_object(
53         {
54             class => 'Koha::Acquisition::Orders',
55             value => { basketno => $basket->basketno, orderstatus => 'new' }
56         }
57     );
58     my $another_order = $order->unblessed; # create a copy of $order but make
59     delete $another_order->{ordernumber};  # sure ordernumber will be regenerated
60     $another_order = $builder->build_object({ class => 'Koha::Acquisition::Orders', value => $another_order });
61
62     ## Authorized user tests
63     my $count_of_orders = Koha::Acquisition::Orders->search->count;
64     # Make sure we are returned with the correct amount of orders
65     $t->get_ok( "//$userid:$password@/api/v1/acquisitions/orders" )
66       ->status_is( 200, 'SWAGGER3.2.2' )
67       ->json_has('/'.($count_of_orders-1).'/order_id')
68       ->json_hasnt('/'.($count_of_orders).'/order_id');
69
70     subtest 'query parameters' => sub {
71
72         my $fields = {
73             biblio_id => 'biblionumber',
74             basket_id => 'basketno',
75             fund_id   => 'budget_id',
76         };
77
78         my $size = keys %{$fields};
79
80         plan tests => $size * (2 + 2 * $size);
81
82         foreach my $field ( keys %{$fields} ) {
83             my $model_field = $fields->{ $field };
84             my $result = $t->get_ok("//$userid:$password@/api/v1/acquisitions/orders?$field=" . $order->$model_field)
85               ->status_is(200);
86
87             foreach my $key ( keys %{$fields} ) {
88               my $key_field = $fields->{ $key };
89               # Check the result order first since it's not predefined.
90               if ($result->tx->res->json->[0]->{$key} eq $order->$key_field) {
91                 $result->json_is( "/0/$key", $order->$key_field );
92                 $result->json_is( "/1/$key", $another_order->$key_field );
93               } else {
94                 $result->json_is( "/0/$key", $another_order->$key_field );
95                 $result->json_is( "/1/$key", $order->$key_field );
96               }
97             }
98         }
99     };
100
101     # Warn on unsupported query parameter
102     $t->get_ok( "//$userid:$password@/api/v1/acquisitions/orders?order_blah=blah" )
103       ->status_is(400)
104       ->json_is( [{ path => '/query/order_blah', message => 'Malformed query string'}] );
105
106     $schema->storage->txn_rollback;
107 };
108
109 subtest 'get() tests' => sub {
110
111     plan tests => 8;
112
113     $schema->storage->txn_begin;
114
115     my $order = $builder->build_object(
116         {
117             class => 'Koha::Acquisition::Orders',
118             value => { orderstatus => 'new' }
119         }
120     );
121     my $patron = $builder->build_object({
122         class => 'Koha::Patrons',
123         value => { flags => 2048 }
124     });
125     my $password = 'thePassword123';
126     $patron->set_password({ password => $password, skip_validation => 1 });
127     my $userid = $patron->userid;
128
129     $t->get_ok( "//$userid:$password@/api/v1/acquisitions/orders/" . $order->ordernumber )
130       ->status_is( 200, 'SWAGGER3.2.2' )
131       ->json_is( '' => $order->to_api, 'SWAGGER3.3.2' );
132
133     my $non_existent_order_id = $order->ordernumber;
134     $order->delete;
135
136     $t->get_ok( "//$userid:$password@/api/v1/acquisitions/orders/" . $non_existent_order_id )
137       ->status_is(404)
138       ->json_is( '/error' => 'Order not found' );
139
140     # Regression tests for bug 25513
141     # Pick a high value that could be transformed into exponential
142     # representation and not considered a number by buggy DBD::mysql versions
143     $order = $builder->build_object(
144         {
145             class => 'Koha::Acquisition::Orders',
146             value => {
147                 orderstatus => 'new',
148                 ecost_tax_excluded => 9963405519357589504,
149                 unitprice => 10177559957753600000
150             }
151         }
152     );
153
154     $t->get_ok( "//$userid:$password@/api/v1/acquisitions/orders/" . $order->ordernumber )
155       ->json_is( '' => $order->to_api, 'Number representation should be consistent' );
156
157     $schema->storage->txn_rollback;
158 };
159
160 subtest 'add() tests' => sub {
161
162     plan tests => 17;
163
164     $schema->storage->txn_begin;
165
166     my $authorized_patron = $builder->build_object({
167         class => 'Koha::Patrons',
168         value => { flags => 1 }
169     });
170     my $password = 'thePassword123';
171     $authorized_patron->set_password({ password => $password, skip_validation => 1 });
172     my $auth_userid = $authorized_patron->userid;
173
174     my $unauthorized_patron = $builder->build_object({
175         class => 'Koha::Patrons',
176         value => { flags => 4 }
177     });
178     $unauthorized_patron->set_password({ password => $password, skip_validation => 1 });
179     my $unauth_userid = $unauthorized_patron->userid;
180
181     my $order_obj = $builder->build_object(
182         {
183             class => 'Koha::Acquisition::Orders',
184             value => {
185                 orderstatus => 'new',
186                 unitprice   => 10,
187                 replacementprice => 10,
188                 quantity    => 2,
189                 quantityreceived => 0,
190                 datecancellationprinted => undef,
191                 order_internalnote => 'This is a dummy note for testing'
192             }
193         }
194     );
195     my $order = $order_obj->to_api;
196     $order_obj->delete;
197     delete $order->{ordernumber};
198     $order->{uncertain_price} = Mojo::JSON->false;
199
200     # Unauthorized attempt to write
201     $t->post_ok( "//$unauth_userid:$password@/api/v1/acquisitions/orders" => json => $order )
202       ->status_is(403);
203
204     # Authorized attempt to write invalid data
205     my $order_with_invalid_field = { %$order };
206     $order_with_invalid_field->{'orderinvalid'} = 'Order invalid';
207
208     $t->post_ok( "//$auth_userid:$password@/api/v1/acquisitions/orders" => json => $order_with_invalid_field )
209       ->status_is(400)
210       ->json_is(
211         "/errors" => [
212             {
213                 message => "Properties not allowed: orderinvalid.",
214                 path    => "/body"
215             }
216         ]
217     );
218
219     # Authorized attempt to write
220     $t->post_ok( "//$auth_userid:$password@/api/v1/acquisitions/orders" => json => $order )
221       ->status_is( 201, 'SWAGGER3.2.1' )
222       ->json_is( '/internal_note' => $order->{internal_note}, 'SWAGGER3.3.1' )
223       ->header_like( Location => qr/\/api\/v1\/acquisitions\/orders\/\d*/, 'SWAGGER3.4.1' );
224
225     # save the order_id
226     my $order_id = $order->{order_id};
227     # Authorized attempt to create with null id
228     $order->{order_id} = undef;
229
230     $t->post_ok( "//$auth_userid:$password@/api/v1/acquisitions/orders" => json => $order )
231       ->status_is(400)
232       ->json_has('/errors');
233
234     # Authorized attempt to create with existing id
235     $order->{order_id} = $order_id;
236
237     warning_like {
238         $t->post_ok( "//$auth_userid:$password@/api/v1/acquisitions/orders" => json => $order )
239           ->status_is(409)
240           ->json_has( '/error' => "Fails when trying to add an existing order_id")
241           ->json_like( '/conflict' => qr/(aqorders\.)?PRIMARY/ ); }
242         qr/DBD::mysql::st execute failed: Duplicate entry '(.*)' for key '(aqorders\.)?PRIMARY'/;
243
244     $schema->storage->txn_rollback;
245 };
246
247 subtest 'update() tests' => sub {
248     plan tests => 13;
249
250     $schema->storage->txn_begin;
251
252     my $authorized_patron = $builder->build_object({
253         class => 'Koha::Patrons',
254         value => { flags => 1 }
255     });
256     my $password = 'thePassword123';
257     $authorized_patron->set_password({ password => $password, skip_validation => 1 });
258     my $auth_userid = $authorized_patron->userid;
259
260     my $unauthorized_patron = $builder->build_object({
261         class => 'Koha::Patrons',
262         value => { flags => 4 }
263     });
264     $unauthorized_patron->set_password({ password => $password, skip_validation => 1 });
265     my $unauth_userid = $unauthorized_patron->userid;
266
267     my $library    = $builder->build_object({ class => 'Koha::Libraries' });
268     my $library_id = $library->branchcode;
269
270     # Unauthorized attempt to update
271     $t->put_ok( "//$unauth_userid:$password@/api/v1/libraries/$library_id"
272                     => json => { name => 'New unauthorized name change' } )
273       ->status_is(403);
274
275     # Attempt partial update on a PUT
276     my $library_with_missing_field = {
277         address1 => "New library address",
278     };
279
280     my $result = $t->put_ok( "//$auth_userid:$password@/api/v1/libraries/$library_id" => json => $library_with_missing_field )
281       ->status_is(400);
282     # Check the result order first since it's not predefined.
283     if ($result->tx->res->json->{errors}->[0]->{path} eq '/body/name') {
284       $result->json_is(
285         "/errors",
286         [
287           {message => "Missing property.", path => "/body/name"},
288           {message => "Missing property.", path => "/body/library_id"}
289         ]
290       );
291     } else {
292       $result->json_is(
293         "/errors",
294         [
295           {message => "Missing property.", path => "/body/library_id"},
296           {message => "Missing property.", path => "/body/name"}
297         ]
298       );
299     }
300
301     my $deleted_library = $builder->build_object( { class => 'Koha::Libraries' } );
302     my $library_with_updated_field = $deleted_library->to_api;
303     $library_with_updated_field->{library_id} = $library_id;
304     $deleted_library->delete;
305
306     $t->put_ok( "//$auth_userid:$password@/api/v1/libraries/$library_id" => json => $library_with_updated_field )
307       ->status_is(200, 'SWAGGER3.2.1')
308       ->json_is( '' => $library_with_updated_field, 'SWAGGER3.3.3' );
309
310     # Authorized attempt to write invalid data
311     my $library_with_invalid_field = { %$library_with_updated_field };
312     $library_with_invalid_field->{'branchinvalid'} = 'Library invalid';
313
314     $t->put_ok( "//$auth_userid:$password@/api/v1/libraries/$library_id" => json => $library_with_invalid_field )
315       ->status_is(400)
316       ->json_is(
317         "/errors" => [
318             {
319                 message => "Properties not allowed: branchinvalid.",
320                 path    => "/body"
321             }
322         ]
323     );
324
325     my $non_existent_code = 'nope'.int(rand(10000));
326     $t->put_ok("//$auth_userid:$password@/api/v1/libraries/$non_existent_code" => json => $library_with_updated_field)
327       ->status_is(404);
328
329     $schema->storage->txn_rollback;
330 };
331
332 subtest 'delete() tests' => sub {
333     plan tests => 7;
334
335     $schema->storage->txn_begin;
336
337     my $authorized_patron = $builder->build_object({
338         class => 'Koha::Patrons',
339         value => { flags => 1 }
340     });
341     my $password = 'thePassword123';
342     $authorized_patron->set_password({ password => $password, skip_validation => 1 });
343     my $auth_userid = $authorized_patron->userid;
344
345     my $unauthorized_patron = $builder->build_object({
346         class => 'Koha::Patrons',
347         value => { flags => 4 }
348     });
349     $unauthorized_patron->set_password({ password => $password, skip_validation => 1 });
350     my $unauth_userid = $unauthorized_patron->userid;
351
352     my $order = $builder->build_object( { class => 'Koha::Acquisition::Orders' } );
353
354     # Unauthorized attempt to delete
355     $t->delete_ok( "//$unauth_userid:$password@/api/v1/acquisitions/orders/" . $order->ordernumber )
356       ->status_is(403);
357
358     $t->delete_ok( "//$auth_userid:$password@/api/v1/acquisitions/orders/" . $order->ordernumber )
359       ->status_is(204, 'SWAGGER3.2.4')
360       ->content_is('', 'SWAGGER3.3.4');
361
362     $t->delete_ok( "//$auth_userid:$password@/api/v1/acquisitions/orders/" . $order->ordernumber )
363       ->status_is(404);
364
365     $schema->storage->txn_rollback;
366 };