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;
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 => 4 }
46 my $password = 'thePassword123';
47 $patron->set_password({ password => $password, skip_validation => 1 });
48 my $userid = $patron->userid;
51 my $library = $builder->build_object({ class => 'Koha::Libraries' });
52 my $another_library = $library->unblessed; # create a copy of $library but make
53 delete $another_library->{branchcode}; # sure branchcode will be regenerated
54 $another_library = $builder->build_object({ class => 'Koha::Libraries', value => $another_library });
56 ## Authorized user tests
57 my $count_of_libraries = Koha::Libraries->search->count;
58 # Make sure we are returned with the correct amount of libraries
59 $t->get_ok( "//$userid:$password@/api/v1/libraries" )
60 ->status_is( 200, 'SWAGGER3.2.2' )
61 ->json_has('/'.($count_of_libraries-1).'/library_id')
62 ->json_hasnt('/'.($count_of_libraries).'/library_id');
64 subtest 'query parameters' => sub {
68 address1 => 'branchaddress1',
69 address2 => 'branchaddress2',
70 address3 => 'branchaddress3',
71 postal_code => 'branchzip',
73 state => 'branchstate',
74 country => 'branchcountry',
75 phone => 'branchphone',
77 email => 'branchemail',
78 reply_to_email => 'branchreplyto',
79 return_path_email => 'branchreturnpath',
82 notes => 'branchnotes',
83 opac_info => 'opac_info',
86 my $size = keys %{$fields};
88 plan tests => $size * 3;
90 foreach my $field ( keys %{$fields} ) {
91 my $model_field = $fields->{ $field };
93 $t->get_ok("//$userid:$password@/api/v1/libraries?$field=" . $library->$model_field)
95 ->json_has( [ $library, $another_library ] );
99 # Warn on unsupported query parameter
100 $t->get_ok( "//$userid:$password@/api/v1/libraries?library_blah=blah" )
102 ->json_is( [{ path => '/query/library_blah', message => 'Malformed query string'}] );
104 $schema->storage->txn_rollback;
107 subtest 'get() tests' => sub {
111 $schema->storage->txn_begin;
113 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
114 my $patron = $builder->build_object({
115 class => 'Koha::Patrons',
116 value => { flags => 4 }
118 my $password = 'thePassword123';
119 $patron->set_password({ password => $password, skip_validation => 1 });
120 my $userid = $patron->userid;
122 $t->get_ok( "//$userid:$password@/api/v1/libraries/" . $library->branchcode )
123 ->status_is( 200, 'SWAGGER3.2.2' )
124 ->json_is( '' => $library->to_api, 'SWAGGER3.3.2' );
126 my $non_existent_code = $library->branchcode;
129 $t->get_ok( "//$userid:$password@/api/v1/libraries/" . $non_existent_code )
131 ->json_is( '/error' => 'Library not found' );
133 $schema->storage->txn_rollback;
136 subtest 'add() tests' => sub {
140 $schema->storage->txn_begin;
142 my $authorized_patron = $builder->build_object({
143 class => 'Koha::Patrons',
144 value => { flags => 1 }
146 my $password = 'thePassword123';
147 $authorized_patron->set_password({ password => $password, skip_validation => 1 });
148 my $auth_userid = $authorized_patron->userid;
150 my $unauthorized_patron = $builder->build_object({
151 class => 'Koha::Patrons',
152 value => { flags => 4 }
154 $unauthorized_patron->set_password({ password => $password, skip_validation => 1 });
155 my $unauth_userid = $unauthorized_patron->userid;
157 my $library_obj = $builder->build_object({ class => 'Koha::Libraries' });
158 my $library = $library_obj->to_api;
159 $library_obj->delete;
161 # Unauthorized attempt to write
162 $t->post_ok( "//$unauth_userid:$password@/api/v1/libraries" => json => $library )
165 # Authorized attempt to write invalid data
166 my $library_with_invalid_field = { %$library };
167 $library_with_invalid_field->{'branchinvalid'} = 'Library invalid';
169 $t->post_ok( "//$auth_userid:$password@/api/v1/libraries" => json => $library_with_invalid_field )
174 message => "Properties not allowed: branchinvalid.",
180 # Authorized attempt to write
181 $t->post_ok( "//$auth_userid:$password@/api/v1/libraries" => json => $library )
182 ->status_is( 201, 'SWAGGER3.2.1' )
183 ->json_is( '' => $library, 'SWAGGER3.3.1' )
184 ->header_is( Location => '/api/v1/libraries/' . $library->{library_id}, 'SWAGGER3.4.1' );
186 # save the library_id
187 my $library_id = $library->{library_id};
188 # Authorized attempt to create with null id
189 $library->{library_id} = undef;
191 $t->post_ok( "//$auth_userid:$password@/api/v1/libraries" => json => $library )
193 ->json_has('/errors');
195 # Authorized attempt to create with existing id
196 $library->{library_id} = $library_id;
199 $t->post_ok( "//$auth_userid:$password@/api/v1/libraries" => json => $library )
201 ->json_has( '/error' => "Fails when trying to add an existing library_id")
202 ->json_like( '/conflict' => qr/(branches\.)?PRIMARY/ ); }
203 qr/^DBD::mysql::st execute failed: Duplicate entry '(.*)' for key '(branches\.)?PRIMARY'/;
205 $schema->storage->txn_rollback;
208 subtest 'update() tests' => sub {
211 $schema->storage->txn_begin;
213 my $authorized_patron = $builder->build_object({
214 class => 'Koha::Patrons',
215 value => { flags => 1 }
217 my $password = 'thePassword123';
218 $authorized_patron->set_password({ password => $password, skip_validation => 1 });
219 my $auth_userid = $authorized_patron->userid;
221 my $unauthorized_patron = $builder->build_object({
222 class => 'Koha::Patrons',
223 value => { flags => 4 }
225 $unauthorized_patron->set_password({ password => $password, skip_validation => 1 });
226 my $unauth_userid = $unauthorized_patron->userid;
228 my $library = $builder->build_object({ class => 'Koha::Libraries' });
229 my $library_id = $library->branchcode;
231 # Unauthorized attempt to update
232 $t->put_ok( "//$unauth_userid:$password@/api/v1/libraries/$library_id"
233 => json => { name => 'New unauthorized name change' } )
236 # Attempt partial update on a PUT
237 my $library_with_missing_field = {
238 address1 => "New library address",
241 $t->put_ok( "//$auth_userid:$password@/api/v1/libraries/$library_id" => json => $library_with_missing_field )
243 ->json_has( "/errors" =>
244 [ { message => "Missing property.", path => "/body/address2" } ]
247 my $deleted_library = $builder->build_object( { class => 'Koha::Libraries' } );
248 my $library_with_updated_field = $deleted_library->to_api;
249 $library_with_updated_field->{library_id} = $library_id;
250 $deleted_library->delete;
252 $t->put_ok( "//$auth_userid:$password@/api/v1/libraries/$library_id" => json => $library_with_updated_field )
253 ->status_is(200, 'SWAGGER3.2.1')
254 ->json_is( '' => $library_with_updated_field, 'SWAGGER3.3.3' );
256 # Authorized attempt to write invalid data
257 my $library_with_invalid_field = { %$library_with_updated_field };
258 $library_with_invalid_field->{'branchinvalid'} = 'Library invalid';
260 $t->put_ok( "//$auth_userid:$password@/api/v1/libraries/$library_id" => json => $library_with_invalid_field )
265 message => "Properties not allowed: branchinvalid.",
271 my $non_existent_code = 'nope'.int(rand(10000));
272 $t->put_ok("//$auth_userid:$password@/api/v1/libraries/$non_existent_code" => json => $library_with_updated_field)
275 $schema->storage->txn_rollback;
278 subtest 'delete() tests' => sub {
281 $schema->storage->txn_begin;
283 my $authorized_patron = $builder->build_object({
284 class => 'Koha::Patrons',
285 value => { flags => 1 }
287 my $password = 'thePassword123';
288 $authorized_patron->set_password({ password => $password, skip_validation => 1 });
289 my $auth_userid = $authorized_patron->userid;
291 my $unauthorized_patron = $builder->build_object({
292 class => 'Koha::Patrons',
293 value => { flags => 4 }
295 $unauthorized_patron->set_password({ password => $password, skip_validation => 1 });
296 my $unauth_userid = $unauthorized_patron->userid;
298 my $library_id = $builder->build( { source => 'Branch' } )->{branchcode};
300 # Unauthorized attempt to delete
301 $t->delete_ok( "//$unauth_userid:$password@/api/v1/libraries/$library_id" )
304 $t->delete_ok( "//$auth_userid:$password@/api/v1/libraries/$library_id" )
305 ->status_is(204, 'SWAGGER3.2.4')
306 ->content_is('', 'SWAGGER3.3.4');
308 $t->delete_ok( "//$auth_userid:$password@/api/v1/libraries/$library_id" )
311 $schema->storage->txn_rollback;