3 # This file is part of Koha.
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.
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.
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>.
20 use Test::More tests => 5;
24 use t::lib::TestBuilder;
27 use JSON qw( encode_json );
29 use Koha::Acquisition::Orders;
32 my $schema = Koha::Database->new->schema;
33 my $builder = t::lib::TestBuilder->new;
35 t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 );
37 my $t = Test::Mojo->new('Koha::REST::V1');
39 subtest 'list() tests' => sub {
42 $schema->storage->txn_begin;
44 my $patron = $builder->build_object({
45 class => 'Koha::Patrons',
46 value => { flags => 1 }
48 my $password = 'thePassword123';
49 $patron->set_password({ password => $password, skip_validation => 1 });
50 my $userid = $patron->userid;
52 my $basket = $builder->build_object({ class => 'Koha::Acquisition::Baskets', value => { is_standing => 1 } });
54 my $biblio = $builder->build_sample_biblio;
55 my $biblioitem = $biblio->biblioitem;
56 $biblioitem->set({ isbn => 'SOMETHING' })->store;
59 my $order = $builder->build_object(
61 class => 'Koha::Acquisition::Orders',
63 basketno => $basket->basketno,
65 biblionumber => $biblio->biblionumber
69 my $another_order = $order->unblessed; # create a copy of $order but make
70 delete $another_order->{ordernumber}; # sure ordernumber will be regenerated
71 $another_order = $builder->build_object({ class => 'Koha::Acquisition::Orders', value => $another_order });
73 ## Authorized user tests
74 my $count_of_orders = Koha::Acquisition::Orders->search->count;
75 # Make sure we are returned with the correct amount of orders
76 $t->get_ok( "//$userid:$password@/api/v1/acquisitions/orders" )
77 ->status_is( 200, 'SWAGGER3.2.2' )
78 ->json_has('/'.($count_of_orders-1).'/order_id')
79 ->json_hasnt('/'.($count_of_orders).'/order_id');
81 subtest 'query parameters' => sub {
84 biblio_id => 'biblionumber',
85 basket_id => 'basketno',
86 fund_id => 'budget_id',
89 my $size = keys %{$fields};
91 plan tests => $size * (2 + 2 * $size);
93 foreach my $field ( keys %{$fields} ) {
94 my $model_field = $fields->{ $field };
95 my $result = $t->get_ok("//$userid:$password@/api/v1/acquisitions/orders?$field=" . $order->$model_field)
98 foreach my $key ( keys %{$fields} ) {
99 my $key_field = $fields->{ $key };
100 # Check the result order first since it's not predefined.
101 if ($result->tx->res->json->[0]->{$key} eq $order->$key_field) {
102 $result->json_is( "/0/$key", $order->$key_field );
103 $result->json_is( "/1/$key", $another_order->$key_field );
105 $result->json_is( "/0/$key", $another_order->$key_field );
106 $result->json_is( "/1/$key", $order->$key_field );
112 my $query = { "biblio.isbn" => { "-like" => "\%SOMETHING\%" } };
114 $t->get_ok( "//$userid:$password@/api/v1/acquisitions/orders?q=" . encode_json($query) => {'x-koha-embed' => 'biblio'} )
115 ->status_is( 200, "Embeddig biblio.isbn doesn't make it explode" )
116 ->json_has( "/0/biblio", "biblio object correctly embedded" )
117 ->json_is( "/0/biblio/isbn", 'SOMETHING', 'Filtering by a biblioitems column works!' );
119 my $result = $t->get_ok( "//$userid:$password@/api/v1/acquisitions/orders?biblio_id=" . $biblio->biblionumber )
122 is( scalar @{ $result->tx->res->json}, 2, 'Two orders fetched' );
124 # Mark $another_order as cancelled
125 $another_order->set({ orderstatus => 'cancelled' })->store;
127 $result = $t->get_ok( "//$userid:$password@/api/v1/acquisitions/orders?only_active=1&biblio_id=" . $biblio->biblionumber )
128 ->status_is( 200, "only_active parameter accepted" );
130 is( scalar @{ $result->tx->res->json}, 1, 'Only one order is active' );
132 # Warn on unsupported query parameter
133 $t->get_ok( "//$userid:$password@/api/v1/acquisitions/orders?order_blah=blah" )
135 ->json_is( [{ path => '/query/order_blah', message => 'Malformed query string'}] );
137 $schema->storage->txn_rollback;
140 subtest 'get() tests' => sub {
144 $schema->storage->txn_begin;
146 my $order = $builder->build_object(
148 class => 'Koha::Acquisition::Orders',
149 value => { orderstatus => 'new' }
152 my $patron = $builder->build_object({
153 class => 'Koha::Patrons',
154 value => { flags => 2048 }
156 my $password = 'thePassword123';
157 $patron->set_password({ password => $password, skip_validation => 1 });
158 my $userid = $patron->userid;
160 $t->get_ok( "//$userid:$password@/api/v1/acquisitions/orders/" . $order->ordernumber )
161 ->status_is( 200, 'SWAGGER3.2.2' )
162 ->json_is( '' => $order->to_api, 'SWAGGER3.3.2' );
164 my $non_existent_order_id = $order->ordernumber;
167 $t->get_ok( "//$userid:$password@/api/v1/acquisitions/orders/" . $non_existent_order_id )
169 ->json_is( '/error' => 'Order not found' );
171 # FIXME This does not work on all the OS we support
172 ## Regression tests for bug 25513
173 ## Pick a high value that could be transformed into exponential
174 ## representation and not considered a number by buggy DBD::mysql versions
175 #$order = $builder->build_object(
177 # class => 'Koha::Acquisition::Orders',
179 # orderstatus => 'new',
180 # ecost_tax_excluded => 9963405519357589504,
181 # unitprice => 10177559957753600000
186 #$t->get_ok( "//$userid:$password@/api/v1/acquisitions/orders/" . $order->ordernumber )
187 # ->json_is( '' => $order->to_api, 'Number representation should be consistent' );
189 $schema->storage->txn_rollback;
192 subtest 'add() tests' => sub {
196 $schema->storage->txn_begin;
198 my $authorized_patron = $builder->build_object({
199 class => 'Koha::Patrons',
200 value => { flags => 1 }
202 my $password = 'thePassword123';
203 $authorized_patron->set_password({ password => $password, skip_validation => 1 });
204 my $auth_userid = $authorized_patron->userid;
206 my $unauthorized_patron = $builder->build_object({
207 class => 'Koha::Patrons',
208 value => { flags => 4 }
210 $unauthorized_patron->set_password({ password => $password, skip_validation => 1 });
211 my $unauth_userid = $unauthorized_patron->userid;
213 my $order_obj = $builder->build_object(
215 class => 'Koha::Acquisition::Orders',
217 orderstatus => 'new',
219 replacementprice => 10,
221 quantityreceived => 0,
222 datecancellationprinted => undef,
223 order_internalnote => 'This is a dummy note for testing'
227 my $order = $order_obj->to_api;
229 delete $order->{ordernumber};
230 $order->{uncertain_price} = Mojo::JSON->false;
232 # Unauthorized attempt to write
233 $t->post_ok( "//$unauth_userid:$password@/api/v1/acquisitions/orders" => json => $order )
236 # Authorized attempt to write invalid data
237 my $order_with_invalid_field = { %$order };
238 $order_with_invalid_field->{'orderinvalid'} = 'Order invalid';
240 $t->post_ok( "//$auth_userid:$password@/api/v1/acquisitions/orders" => json => $order_with_invalid_field )
245 message => "Properties not allowed: orderinvalid.",
251 # Authorized attempt to write
252 $t->post_ok( "//$auth_userid:$password@/api/v1/acquisitions/orders" => json => $order )
253 ->status_is( 201, 'SWAGGER3.2.1' )
254 ->json_is( '/internal_note' => $order->{internal_note}, 'SWAGGER3.3.1' )
255 ->header_like( Location => qr/\/api\/v1\/acquisitions\/orders\/\d*/, 'SWAGGER3.4.1' );
258 my $order_id = $order->{order_id};
259 # Authorized attempt to create with null id
260 $order->{order_id} = undef;
262 $t->post_ok( "//$auth_userid:$password@/api/v1/acquisitions/orders" => json => $order )
264 ->json_has('/errors');
266 # Authorized attempt to create with existing id
267 $order->{order_id} = $order_id;
270 $t->post_ok( "//$auth_userid:$password@/api/v1/acquisitions/orders" => json => $order )
272 ->json_has( '/error' => "Fails when trying to add an existing order_id")
273 ->json_like( '/conflict' => qr/(aqorders\.)?PRIMARY/ ); }
274 qr/DBD::mysql::st execute failed: Duplicate entry '(.*)' for key '(aqorders\.)?PRIMARY'/;
276 $schema->storage->txn_rollback;
279 subtest 'update() tests' => sub {
282 $schema->storage->txn_begin;
284 my $authorized_patron = $builder->build_object({
285 class => 'Koha::Patrons',
286 value => { flags => 1 }
288 my $password = 'thePassword123';
289 $authorized_patron->set_password({ password => $password, skip_validation => 1 });
290 my $auth_userid = $authorized_patron->userid;
292 my $unauthorized_patron = $builder->build_object({
293 class => 'Koha::Patrons',
294 value => { flags => 4 }
296 $unauthorized_patron->set_password({ password => $password, skip_validation => 1 });
297 my $unauth_userid = $unauthorized_patron->userid;
299 my $library = $builder->build_object({ class => 'Koha::Libraries' });
300 my $library_id = $library->branchcode;
302 # Unauthorized attempt to update
303 $t->put_ok( "//$unauth_userid:$password@/api/v1/libraries/$library_id"
304 => json => { name => 'New unauthorized name change' } )
307 # Attempt partial update on a PUT
308 my $library_with_missing_field = {
309 address1 => "New library address",
312 my $result = $t->put_ok( "//$auth_userid:$password@/api/v1/libraries/$library_id" => json => $library_with_missing_field )
314 # Check the result order first since it's not predefined.
315 if ($result->tx->res->json->{errors}->[0]->{path} eq '/body/name') {
319 {message => "Missing property.", path => "/body/name"},
320 {message => "Missing property.", path => "/body/library_id"}
327 {message => "Missing property.", path => "/body/library_id"},
328 {message => "Missing property.", path => "/body/name"}
333 my $deleted_library = $builder->build_object( { class => 'Koha::Libraries' } );
334 my $library_with_updated_field = $deleted_library->to_api;
335 $library_with_updated_field->{library_id} = $library_id;
336 $deleted_library->delete;
338 $t->put_ok( "//$auth_userid:$password@/api/v1/libraries/$library_id" => json => $library_with_updated_field )
339 ->status_is(200, 'SWAGGER3.2.1')
340 ->json_is( '' => $library_with_updated_field, 'SWAGGER3.3.3' );
342 # Authorized attempt to write invalid data
343 my $library_with_invalid_field = { %$library_with_updated_field };
344 $library_with_invalid_field->{'branchinvalid'} = 'Library invalid';
346 $t->put_ok( "//$auth_userid:$password@/api/v1/libraries/$library_id" => json => $library_with_invalid_field )
351 message => "Properties not allowed: branchinvalid.",
357 my $non_existent_code = 'nope'.int(rand(10000));
358 $t->put_ok("//$auth_userid:$password@/api/v1/libraries/$non_existent_code" => json => $library_with_updated_field)
361 $schema->storage->txn_rollback;
364 subtest 'delete() tests' => sub {
367 $schema->storage->txn_begin;
369 my $authorized_patron = $builder->build_object({
370 class => 'Koha::Patrons',
371 value => { flags => 1 }
373 my $password = 'thePassword123';
374 $authorized_patron->set_password({ password => $password, skip_validation => 1 });
375 my $auth_userid = $authorized_patron->userid;
377 my $unauthorized_patron = $builder->build_object({
378 class => 'Koha::Patrons',
379 value => { flags => 4 }
381 $unauthorized_patron->set_password({ password => $password, skip_validation => 1 });
382 my $unauth_userid = $unauthorized_patron->userid;
384 my $order = $builder->build_object( { class => 'Koha::Acquisition::Orders' } );
386 # Unauthorized attempt to delete
387 $t->delete_ok( "//$unauth_userid:$password@/api/v1/acquisitions/orders/" . $order->ordernumber )
390 $t->delete_ok( "//$auth_userid:$password@/api/v1/acquisitions/orders/" . $order->ordernumber )
391 ->status_is(204, 'SWAGGER3.2.4')
392 ->content_is('', 'SWAGGER3.3.4');
394 $t->delete_ok( "//$auth_userid:$password@/api/v1/acquisitions/orders/" . $order->ordernumber )
397 $schema->storage->txn_rollback;