3 # This file is part of Koha.
5 # Koha is free software; you can redistribute it and/or modify it under the
6 # terms of the GNU General Public License as published by the Free Software
7 # Foundation; either version 3 of the License, or (at your option) any later
10 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
11 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License along
15 # with Koha; if not, write to the Free Software Foundation, Inc.,
16 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 use Test::More tests => 5;
24 use t::lib::TestBuilder;
27 use Koha::Acquisition::Orders;
30 my $schema = Koha::Database->new->schema;
31 my $builder = t::lib::TestBuilder->new;
33 t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 );
35 my $t = Test::Mojo->new('Koha::REST::V1');
37 subtest 'list() tests' => sub {
40 $schema->storage->txn_begin;
42 my $patron = $builder->build_object({
43 class => 'Koha::Patrons',
44 value => { flags => 1 }
46 my $password = 'thePassword123';
47 $patron->set_password({ password => $password, skip_validation => 1 });
48 my $userid = $patron->userid;
50 my $basket = $builder->build_object({ class => 'Koha::Acquisition::Baskets' });
52 my $order = $builder->build_object(
54 class => 'Koha::Acquisition::Orders',
55 value => { basketno => $basket->basketno, orderstatus => 'new' }
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 });
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');
70 subtest 'query parameters' => sub {
73 biblio_id => 'biblionumber',
74 basket_id => 'basketno',
75 fund_id => 'budget_id',
78 my $size = keys %{$fields};
80 plan tests => $size * 3;
82 foreach my $field ( keys %{$fields} ) {
83 my $model_field = $fields->{ $field };
85 $t->get_ok("//$userid:$password@/api/v1/acquisitions/orders?$field=" . $order->$model_field)
87 ->json_is( [ $order->to_api, $another_order->to_api ] );
91 # Warn on unsupported query parameter
92 $t->get_ok( "//$userid:$password@/api/v1/acquisitions/orders?order_blah=blah" )
94 ->json_is( [{ path => '/query/order_blah', message => 'Malformed query string'}] );
96 $schema->storage->txn_rollback;
99 subtest 'get() tests' => sub {
103 $schema->storage->txn_begin;
105 my $order = $builder->build_object(
107 class => 'Koha::Acquisition::Orders',
108 value => { orderstatus => 'new' }
111 my $patron = $builder->build_object({
112 class => 'Koha::Patrons',
113 value => { flags => 2048 }
115 my $password = 'thePassword123';
116 $patron->set_password({ password => $password, skip_validation => 1 });
117 my $userid = $patron->userid;
119 $t->get_ok( "//$userid:$password@/api/v1/acquisitions/orders/" . $order->ordernumber )
120 ->status_is( 200, 'SWAGGER3.2.2' )
121 ->json_is( '' => $order->to_api, 'SWAGGER3.3.2' );
123 my $non_existent_order_id = $order->ordernumber;
126 $t->get_ok( "//$userid:$password@/api/v1/acquisitions/orders/" . $non_existent_order_id )
128 ->json_is( '/error' => 'Order not found' );
130 $schema->storage->txn_rollback;
133 subtest 'add() tests' => sub {
137 $schema->storage->txn_begin;
139 my $authorized_patron = $builder->build_object({
140 class => 'Koha::Patrons',
141 value => { flags => 1 }
143 my $password = 'thePassword123';
144 $authorized_patron->set_password({ password => $password, skip_validation => 1 });
145 my $auth_userid = $authorized_patron->userid;
147 my $unauthorized_patron = $builder->build_object({
148 class => 'Koha::Patrons',
149 value => { flags => 4 }
151 $unauthorized_patron->set_password({ password => $password, skip_validation => 1 });
152 my $unauth_userid = $unauthorized_patron->userid;
154 my $order_obj = $builder->build_object(
156 class => 'Koha::Acquisition::Orders',
158 orderstatus => 'new',
160 replacementprice => 10,
162 quantityreceived => 0,
163 datecancellationprinted => undef,
164 order_internalnote => 'This is a dummy note for testing'
168 my $order = $order_obj->to_api;
170 delete $order->{ordernumber};
171 $order->{uncertain_price} = Mojo::JSON->false;
173 # Unauthorized attempt to write
174 $t->post_ok( "//$unauth_userid:$password@/api/v1/acquisitions/orders" => json => $order )
177 # Authorized attempt to write invalid data
178 my $order_with_invalid_field = { %$order };
179 $order_with_invalid_field->{'orderinvalid'} = 'Order invalid';
181 $t->post_ok( "//$auth_userid:$password@/api/v1/acquisitions/orders" => json => $order_with_invalid_field )
186 message => "Properties not allowed: orderinvalid.",
192 # Authorized attempt to write
193 $t->post_ok( "//$auth_userid:$password@/api/v1/acquisitions/orders" => json => $order )
194 ->status_is( 201, 'SWAGGER3.2.1' )
195 ->json_is( '/internal_note' => $order->{internal_note}, 'SWAGGER3.3.1' )
196 ->header_like( Location => qr/\/api\/v1\/acquisitions\/orders\/\d*/, 'SWAGGER3.4.1' );
199 my $order_id = $order->{order_id};
200 # Authorized attempt to create with null id
201 $order->{order_id} = undef;
203 $t->post_ok( "//$auth_userid:$password@/api/v1/acquisitions/orders" => json => $order )
205 ->json_has('/errors');
207 # Authorized attempt to create with existing id
208 $order->{order_id} = $order_id;
211 $t->post_ok( "//$auth_userid:$password@/api/v1/acquisitions/orders" => json => $order )
213 ->json_has( '/error' => "Fails when trying to add an existing order_id")
214 ->json_like( '/conflict' => qr/(aqorders\.)?PRIMARY/ ); }
215 qr/^DBD::mysql::st execute failed: Duplicate entry '(.*)' for key '(aqorders\.)?PRIMARY'/;
217 $schema->storage->txn_rollback;
220 subtest 'update() tests' => sub {
223 $schema->storage->txn_begin;
225 my $authorized_patron = $builder->build_object({
226 class => 'Koha::Patrons',
227 value => { flags => 1 }
229 my $password = 'thePassword123';
230 $authorized_patron->set_password({ password => $password, skip_validation => 1 });
231 my $auth_userid = $authorized_patron->userid;
233 my $unauthorized_patron = $builder->build_object({
234 class => 'Koha::Patrons',
235 value => { flags => 4 }
237 $unauthorized_patron->set_password({ password => $password, skip_validation => 1 });
238 my $unauth_userid = $unauthorized_patron->userid;
240 my $library = $builder->build_object({ class => 'Koha::Libraries' });
241 my $library_id = $library->branchcode;
243 # Unauthorized attempt to update
244 $t->put_ok( "//$unauth_userid:$password@/api/v1/libraries/$library_id"
245 => json => { name => 'New unauthorized name change' } )
248 # Attempt partial update on a PUT
249 my $library_with_missing_field = {
250 address1 => "New library address",
253 $t->put_ok( "//$auth_userid:$password@/api/v1/libraries/$library_id" => json => $library_with_missing_field )
255 ->json_has( "/errors" =>
256 [ { message => "Missing property.", path => "/body/address2" } ]
259 my $deleted_library = $builder->build_object( { class => 'Koha::Libraries' } );
260 my $library_with_updated_field = $deleted_library->to_api;
261 $library_with_updated_field->{library_id} = $library_id;
262 $deleted_library->delete;
264 $t->put_ok( "//$auth_userid:$password@/api/v1/libraries/$library_id" => json => $library_with_updated_field )
265 ->status_is(200, 'SWAGGER3.2.1')
266 ->json_is( '' => $library_with_updated_field, 'SWAGGER3.3.3' );
268 # Authorized attempt to write invalid data
269 my $library_with_invalid_field = { %$library_with_updated_field };
270 $library_with_invalid_field->{'branchinvalid'} = 'Library invalid';
272 $t->put_ok( "//$auth_userid:$password@/api/v1/libraries/$library_id" => json => $library_with_invalid_field )
277 message => "Properties not allowed: branchinvalid.",
283 my $non_existent_code = 'nope'.int(rand(10000));
284 $t->put_ok("//$auth_userid:$password@/api/v1/libraries/$non_existent_code" => json => $library_with_updated_field)
287 $schema->storage->txn_rollback;
290 subtest 'delete() tests' => sub {
293 $schema->storage->txn_begin;
295 my $authorized_patron = $builder->build_object({
296 class => 'Koha::Patrons',
297 value => { flags => 1 }
299 my $password = 'thePassword123';
300 $authorized_patron->set_password({ password => $password, skip_validation => 1 });
301 my $auth_userid = $authorized_patron->userid;
303 my $unauthorized_patron = $builder->build_object({
304 class => 'Koha::Patrons',
305 value => { flags => 4 }
307 $unauthorized_patron->set_password({ password => $password, skip_validation => 1 });
308 my $unauth_userid = $unauthorized_patron->userid;
310 my $order = $builder->build_object( { class => 'Koha::Acquisition::Orders' } );
312 # Unauthorized attempt to delete
313 $t->delete_ok( "//$unauth_userid:$password@/api/v1/acquisitions/orders/" . $order->ordernumber )
316 $t->delete_ok( "//$auth_userid:$password@/api/v1/acquisitions/orders/" . $order->ordernumber )
317 ->status_is(204, 'SWAGGER3.2.4')
318 ->content_is('', 'SWAGGER3.3.4');
320 $t->delete_ok( "//$auth_userid:$password@/api/v1/acquisitions/orders/" . $order->ordernumber )
323 $schema->storage->txn_rollback;