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 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 my $non_existent_code = $library->branchcode;
134 $t->get_ok( "//$userid:$password@/api/v1/libraries/" . $non_existent_code )
136 ->json_is( '/error' => 'Library not found' );
138 $schema->storage->txn_rollback;
141 subtest 'add() tests' => sub {
145 $schema->storage->txn_begin;
147 my $authorized_patron = $builder->build_object({
148 class => 'Koha::Patrons',
149 value => { flags => 1 }
151 my $password = 'thePassword123';
152 $authorized_patron->set_password({ password => $password, skip_validation => 1 });
153 my $auth_userid = $authorized_patron->userid;
155 my $unauthorized_patron = $builder->build_object({
156 class => 'Koha::Patrons',
157 value => { flags => 4 }
159 $unauthorized_patron->set_password({ password => $password, skip_validation => 1 });
160 my $unauth_userid = $unauthorized_patron->userid;
162 my $library_obj = $builder->build_object({ class => 'Koha::Libraries' });
163 my $library = $library_obj->to_api;
164 $library_obj->delete;
166 # Unauthorized attempt to write
167 $t->post_ok( "//$unauth_userid:$password@/api/v1/libraries" => json => $library )
170 # Authorized attempt to write invalid data
171 my $library_with_invalid_field = { %$library };
172 $library_with_invalid_field->{'branchinvalid'} = 'Library invalid';
174 $t->post_ok( "//$auth_userid:$password@/api/v1/libraries" => json => $library_with_invalid_field )
179 message => "Properties not allowed: branchinvalid.",
185 # Authorized attempt to write
186 $t->post_ok( "//$auth_userid:$password@/api/v1/libraries" => json => $library )
187 ->status_is( 201, 'SWAGGER3.2.1' )
188 ->json_is( '' => $library, 'SWAGGER3.3.1' )
189 ->header_is( Location => '/api/v1/libraries/' . $library->{library_id}, 'SWAGGER3.4.1' );
191 # save the library_id
192 my $library_id = $library->{library_id};
193 # Authorized attempt to create with null id
194 $library->{library_id} = undef;
196 $t->post_ok( "//$auth_userid:$password@/api/v1/libraries" => json => $library )
198 ->json_has('/errors');
200 # Authorized attempt to create with existing id
201 $library->{library_id} = $library_id;
204 $t->post_ok( "//$auth_userid:$password@/api/v1/libraries" => json => $library )
206 ->json_has( '/error' => "Fails when trying to add an existing library_id")
207 ->json_like( '/conflict' => qr/(branches\.)?PRIMARY/ ); }
208 qr/DBD::mysql::st execute failed: Duplicate entry '(.*)' for key '(branches\.)?PRIMARY'/;
210 $schema->storage->txn_rollback;
213 subtest 'update() tests' => sub {
216 $schema->storage->txn_begin;
218 my $authorized_patron = $builder->build_object({
219 class => 'Koha::Patrons',
220 value => { flags => 1 }
222 my $password = 'thePassword123';
223 $authorized_patron->set_password({ password => $password, skip_validation => 1 });
224 my $auth_userid = $authorized_patron->userid;
226 my $unauthorized_patron = $builder->build_object({
227 class => 'Koha::Patrons',
228 value => { flags => 4 }
230 $unauthorized_patron->set_password({ password => $password, skip_validation => 1 });
231 my $unauth_userid = $unauthorized_patron->userid;
233 my $library = $builder->build_object({ class => 'Koha::Libraries' });
234 my $library_id = $library->branchcode;
236 # Unauthorized attempt to update
237 $t->put_ok( "//$unauth_userid:$password@/api/v1/libraries/$library_id"
238 => json => { name => 'New unauthorized name change' } )
241 # Attempt partial update on a PUT
242 my $library_with_missing_field = {
243 address1 => "New library address",
246 $t->put_ok( "//$auth_userid:$password@/api/v1/libraries/$library_id" => json => $library_with_missing_field )
248 ->json_has( "/errors" =>
249 [ { message => "Missing property.", path => "/body/address2" } ]
252 my $deleted_library = $builder->build_object( { class => 'Koha::Libraries' } );
253 my $library_with_updated_field = $deleted_library->to_api;
254 $library_with_updated_field->{library_id} = $library_id;
255 $deleted_library->delete;
257 $t->put_ok( "//$auth_userid:$password@/api/v1/libraries/$library_id" => json => $library_with_updated_field )
258 ->status_is(200, 'SWAGGER3.2.1')
259 ->json_is( '' => $library_with_updated_field, 'SWAGGER3.3.3' );
261 # Authorized attempt to write invalid data
262 my $library_with_invalid_field = { %$library_with_updated_field };
263 $library_with_invalid_field->{'branchinvalid'} = 'Library invalid';
265 $t->put_ok( "//$auth_userid:$password@/api/v1/libraries/$library_id" => json => $library_with_invalid_field )
270 message => "Properties not allowed: branchinvalid.",
276 my $non_existent_code = 'nope'.int(rand(10000));
277 $t->put_ok("//$auth_userid:$password@/api/v1/libraries/$non_existent_code" => json => $library_with_updated_field)
280 $schema->storage->txn_rollback;
283 subtest 'delete() tests' => sub {
286 $schema->storage->txn_begin;
288 my $authorized_patron = $builder->build_object({
289 class => 'Koha::Patrons',
290 value => { flags => 1 }
292 my $password = 'thePassword123';
293 $authorized_patron->set_password({ password => $password, skip_validation => 1 });
294 my $auth_userid = $authorized_patron->userid;
296 my $unauthorized_patron = $builder->build_object({
297 class => 'Koha::Patrons',
298 value => { flags => 4 }
300 $unauthorized_patron->set_password({ password => $password, skip_validation => 1 });
301 my $unauth_userid = $unauthorized_patron->userid;
303 my $library_id = $builder->build( { source => 'Branch' } )->{branchcode};
305 # Unauthorized attempt to delete
306 $t->delete_ok( "//$unauth_userid:$password@/api/v1/libraries/$library_id" )
309 $t->delete_ok( "//$auth_userid:$password@/api/v1/libraries/$library_id" )
310 ->status_is(204, 'SWAGGER3.2.4')
311 ->content_is('', 'SWAGGER3.3.4');
313 $t->delete_ok( "//$auth_userid:$password@/api/v1/libraries/$library_id" )
316 $schema->storage->txn_rollback;