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 => 7;
24 use t::lib::TestBuilder;
27 use List::Util qw(min);
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 => 4 }
48 my $password = 'thePassword123';
49 $patron->set_password({ password => $password, skip_validation => 1 });
50 my $userid = $patron->userid;
53 my $library = $builder->build_object({ class => 'Koha::Libraries' });
54 my $another_library = $library->unblessed; # create a copy of $library but make
55 delete $another_library->{branchcode}; # sure branchcode will be regenerated
56 $another_library = $builder->build_object({ class => 'Koha::Libraries', value => $another_library });
58 ## Authorized user tests
59 # Make sure we are returned with the correct amount of libraries
60 $t->get_ok( "//$userid:$password@/api/v1/libraries" )
61 ->status_is( 200, 'SWAGGER3.2.2' );
63 my $response_count = scalar @{ $t->tx->res->json };
64 my $expected_count = min( Koha::Libraries->count, C4::Context->preference('RESTdefaultPageSize') );
65 is( $response_count, $expected_count, 'Results count is paginated');
67 subtest 'query parameters' => sub {
71 address1 => 'branchaddress1',
72 address2 => 'branchaddress2',
73 address3 => 'branchaddress3',
74 postal_code => 'branchzip',
76 state => 'branchstate',
77 country => 'branchcountry',
78 phone => 'branchphone',
80 email => 'branchemail',
81 reply_to_email => 'branchreplyto',
82 return_path_email => 'branchreturnpath',
85 notes => 'branchnotes',
88 my $size = keys %{$fields};
90 plan tests => $size * (2 + 2 * $size);
92 foreach my $field ( keys %{$fields} ) {
93 my $model_field = $fields->{ $field };
94 my $result = $t->get_ok("//$userid:$password@/api/v1/libraries?$field=" . $library->$model_field)
96 foreach my $key ( keys %{$fields} ) {
97 my $key_field = $fields->{ $key };
98 $result->json_is( "/0/$key", $library->$key_field );
99 $result->json_is( "/1/$key", $another_library->$key_field );
104 # Warn on unsupported query parameter
105 $t->get_ok( "//$userid:$password@/api/v1/libraries?library_blah=blah" )
107 ->json_is( [{ path => '/query/library_blah', message => 'Malformed query string'}] );
109 $schema->storage->txn_rollback;
112 subtest 'get() tests' => sub {
116 $schema->storage->txn_begin;
118 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
119 my $patron = $builder->build_object({
120 class => 'Koha::Patrons',
121 value => { flags => 4 }
123 my $password = 'thePassword123';
124 $patron->set_password({ password => $password, skip_validation => 1 });
125 my $userid = $patron->userid;
127 $t->get_ok( "//$userid:$password@/api/v1/libraries/" . $library->branchcode )
128 ->status_is( 200, 'SWAGGER3.2.2' )
129 ->json_is( '' => $library->to_api, 'SWAGGER3.3.2' );
131 $t->get_ok( "//$userid:$password@/api/v1/libraries/"
132 . $library->branchcode => { 'x-koha-embed' => 'cash_registers,desks' } )->status_is(200)
133 ->json_is( { %{ $library->to_api }, desks => [], cash_registers => [] } );
135 my $desk = $builder->build_object( { class => 'Koha::Desks', value => { branchcode => $library->id } } );
137 $builder->build_object( { class => 'Koha::Cash::Registers', value => { branch => $library->id } } );
139 $t->get_ok( "//$userid:$password@/api/v1/libraries/"
140 . $library->branchcode => { 'x-koha-embed' => 'cash_registers,desks' } )->status_is(200)
142 { %{ $library->to_api }, desks => [ $desk->to_api ], cash_registers => [ $cash_register->to_api ] } );
144 my $non_existent_code = $library->branchcode;
147 $t->get_ok( "//$userid:$password@/api/v1/libraries/" . $non_existent_code )
149 ->json_is( '/error' => 'Library not found' );
151 $schema->storage->txn_rollback;
154 subtest 'add() tests' => sub {
158 $schema->storage->txn_begin;
160 my $authorized_patron = $builder->build_object({
161 class => 'Koha::Patrons',
162 value => { flags => 1 }
164 my $password = 'thePassword123';
165 $authorized_patron->set_password({ password => $password, skip_validation => 1 });
166 my $auth_userid = $authorized_patron->userid;
168 my $unauthorized_patron = $builder->build_object({
169 class => 'Koha::Patrons',
170 value => { flags => 4 }
172 $unauthorized_patron->set_password({ password => $password, skip_validation => 1 });
173 my $unauth_userid = $unauthorized_patron->userid;
175 my $library_obj = $builder->build_object({ class => 'Koha::Libraries' });
176 my $library = $library_obj->to_api;
177 $library_obj->delete;
179 # Unauthorized attempt to write
180 $t->post_ok( "//$unauth_userid:$password@/api/v1/libraries" => json => $library )
183 # Authorized attempt to write invalid data
184 my $library_with_invalid_field = { %$library };
185 $library_with_invalid_field->{'branchinvalid'} = 'Library invalid';
187 $t->post_ok( "//$auth_userid:$password@/api/v1/libraries" => json => $library_with_invalid_field )
192 message => "Properties not allowed: branchinvalid.",
198 # Authorized attempt to write
199 $t->post_ok( "//$auth_userid:$password@/api/v1/libraries" => json => $library )
200 ->status_is( 201, 'SWAGGER3.2.1' )
201 ->json_is( '' => $library, 'SWAGGER3.3.1' )
202 ->header_is( Location => '/api/v1/libraries/' . $library->{library_id}, 'SWAGGER3.4.1' );
204 # save the library_id
205 my $library_id = $library->{library_id};
206 # Authorized attempt to create with null id
207 $library->{library_id} = undef;
209 $t->post_ok( "//$auth_userid:$password@/api/v1/libraries" => json => $library )
211 ->json_has('/errors');
213 # Authorized attempt to create with existing id
214 $library->{library_id} = $library_id;
217 $t->post_ok( "//$auth_userid:$password@/api/v1/libraries" => json => $library )
219 ->json_has( '/error' => "Fails when trying to add an existing library_id")
220 ->json_like( '/conflict' => qr/(branches\.)?PRIMARY/ ); }
221 qr/DBD::mysql::st execute failed: Duplicate entry '(.*)' for key '(branches\.)?PRIMARY'/;
223 $schema->storage->txn_rollback;
226 subtest 'update() tests' => sub {
229 $schema->storage->txn_begin;
231 my $authorized_patron = $builder->build_object({
232 class => 'Koha::Patrons',
233 value => { flags => 1 }
235 my $password = 'thePassword123';
236 $authorized_patron->set_password({ password => $password, skip_validation => 1 });
237 my $auth_userid = $authorized_patron->userid;
239 my $unauthorized_patron = $builder->build_object({
240 class => 'Koha::Patrons',
241 value => { flags => 4 }
243 $unauthorized_patron->set_password({ password => $password, skip_validation => 1 });
244 my $unauth_userid = $unauthorized_patron->userid;
246 my $library = $builder->build_object({ class => 'Koha::Libraries' });
247 my $library_id = $library->branchcode;
249 # Unauthorized attempt to update
250 $t->put_ok( "//$unauth_userid:$password@/api/v1/libraries/$library_id"
251 => json => { name => 'New unauthorized name change' } )
254 # Attempt partial update on a PUT
255 my $library_with_missing_field = {
256 address1 => "New library address",
259 $t->put_ok( "//$auth_userid:$password@/api/v1/libraries/$library_id" => json => $library_with_missing_field )
261 ->json_has( "/errors" =>
262 [ { message => "Missing property.", path => "/body/address2" } ]
265 my $deleted_library = $builder->build_object( { class => 'Koha::Libraries' } );
266 my $library_with_updated_field = $deleted_library->to_api;
267 $library_with_updated_field->{library_id} = $library_id;
268 $deleted_library->delete;
270 $t->put_ok( "//$auth_userid:$password@/api/v1/libraries/$library_id" => json => $library_with_updated_field )
271 ->status_is(200, 'SWAGGER3.2.1')
272 ->json_is( '' => $library_with_updated_field, 'SWAGGER3.3.3' );
274 # Authorized attempt to write invalid data
275 my $library_with_invalid_field = { %$library_with_updated_field };
276 $library_with_invalid_field->{'branchinvalid'} = 'Library invalid';
278 $t->put_ok( "//$auth_userid:$password@/api/v1/libraries/$library_id" => json => $library_with_invalid_field )
283 message => "Properties not allowed: branchinvalid.",
289 my $non_existent_code = 'nope'.int(rand(10000));
290 $t->put_ok("//$auth_userid:$password@/api/v1/libraries/$non_existent_code" => json => $library_with_updated_field)
293 $schema->storage->txn_rollback;
296 subtest 'delete() tests' => sub {
299 $schema->storage->txn_begin;
301 my $authorized_patron = $builder->build_object({
302 class => 'Koha::Patrons',
303 value => { flags => 1 }
305 my $password = 'thePassword123';
306 $authorized_patron->set_password({ password => $password, skip_validation => 1 });
307 my $auth_userid = $authorized_patron->userid;
309 my $unauthorized_patron = $builder->build_object({
310 class => 'Koha::Patrons',
311 value => { flags => 4 }
313 $unauthorized_patron->set_password({ password => $password, skip_validation => 1 });
314 my $unauth_userid = $unauthorized_patron->userid;
316 my $library_id = $builder->build( { source => 'Branch' } )->{branchcode};
318 # Unauthorized attempt to delete
319 $t->delete_ok( "//$unauth_userid:$password@/api/v1/libraries/$library_id" )
322 $t->delete_ok( "//$auth_userid:$password@/api/v1/libraries/$library_id" )
323 ->status_is(204, 'SWAGGER3.2.4')
324 ->content_is('', 'SWAGGER3.3.4');
326 $t->delete_ok( "//$auth_userid:$password@/api/v1/libraries/$library_id" )
329 $schema->storage->txn_rollback;
332 subtest 'list_desks() tests' => sub {
336 $schema->storage->txn_begin;
338 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
339 my $patron = $builder->build_object(
341 class => 'Koha::Patrons',
342 value => { flags => 4 }
345 my $password = 'thePassword123';
346 $patron->set_password( { password => $password, skip_validation => 1 } );
347 my $userid = $patron->userid;
349 t::lib::Mocks::mock_preference( 'UseCirculationDesks', 0 );
351 $t->get_ok( "//$userid:$password@/api/v1/libraries/" . $library->branchcode . "/desks" )->status_is(404)
352 ->json_is( '/error' => q{Feature disabled} );
354 my $non_existent_code = $library->branchcode;
357 t::lib::Mocks::mock_preference( 'UseCirculationDesks', 1 );
359 $t->get_ok( "//$userid:$password@/api/v1/libraries/" . $non_existent_code . "/desks" )->status_is(404)
360 ->json_is( '/error' => 'Library not found' );
362 my $desk_1 = $builder->build_object( { class => 'Koha::Desks', value => { branchcode => $library->id } } );
363 my $desk_2 = $builder->build_object( { class => 'Koha::Desks', value => { branchcode => $library->id } } );
365 my $res = $t->get_ok( "//$userid:$password@/api/v1/libraries/" . $library->branchcode . "/desks" )->status_is(200)
366 ->json_is( '/0/desk_id' => $desk_1->id )->json_is( '/1/desk_id' => $desk_2->id )->tx->res->json;
368 is( scalar @{$res}, 2 );
370 $schema->storage->txn_rollback;
373 subtest 'list_cash_registers() tests' => sub {
377 $schema->storage->txn_begin;
379 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
380 my $patron = $builder->build_object(
382 class => 'Koha::Patrons',
383 value => { flags => 4 }
386 my $password = 'thePassword123';
387 $patron->set_password( { password => $password, skip_validation => 1 } );
388 my $userid = $patron->userid;
390 t::lib::Mocks::mock_preference( 'UseCashRegisters', 0 );
392 $t->get_ok( "//$userid:$password@/api/v1/libraries/" . $library->branchcode . "/cash_registers" )->status_is(404)
393 ->json_is( '/error' => q{Feature disabled} );
395 my $non_existent_code = $library->branchcode;
398 t::lib::Mocks::mock_preference( 'UseCashRegisters', 1 );
400 $t->get_ok( "//$userid:$password@/api/v1/libraries/" . $non_existent_code . "/cash_registers" )->status_is(404)
401 ->json_is( '/error' => 'Library not found' );
403 my $cash_register_1 =
404 $builder->build_object( { class => 'Koha::Cash::Registers', value => { branch => $library->id } } );
405 my $cash_register_2 =
406 $builder->build_object( { class => 'Koha::Cash::Registers', value => { branch => $library->id } } );
409 $t->get_ok( "//$userid:$password@/api/v1/libraries/" . $library->branchcode . "/cash_registers" )
410 ->status_is(200)->json_is( '/0/cash_register_id' => $cash_register_1->id )
411 ->json_is( '/1/cash_register_id' => $cash_register_2->id )->tx->res->json;
413 is( scalar @{$res}, 2 );
415 $schema->storage->txn_rollback;