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 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',
86 opac_info => 'opac_info',
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/libraries?$field=" . $library->$model_field)
97 foreach my $key ( keys %{$fields} ) {
98 my $key_field = $fields->{ $key };
99 $result->json_is( "/0/$key", $library->$key_field );
100 $result->json_is( "/1/$key", $another_library->$key_field );
105 # Warn on unsupported query parameter
106 $t->get_ok( "//$userid:$password@/api/v1/libraries?library_blah=blah" )
108 ->json_is( [{ path => '/query/library_blah', message => 'Malformed query string'}] );
110 $schema->storage->txn_rollback;
113 subtest 'get() tests' => sub {
117 $schema->storage->txn_begin;
119 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
120 my $patron = $builder->build_object({
121 class => 'Koha::Patrons',
122 value => { flags => 4 }
124 my $password = 'thePassword123';
125 $patron->set_password({ password => $password, skip_validation => 1 });
126 my $userid = $patron->userid;
128 $t->get_ok( "//$userid:$password@/api/v1/libraries/" . $library->branchcode )
129 ->status_is( 200, 'SWAGGER3.2.2' )
130 ->json_is( '' => Koha::REST::V1::Library::_to_api( $library->TO_JSON ), 'SWAGGER3.3.2' );
132 my $non_existent_code = $library->branchcode;
135 $t->get_ok( "//$userid:$password@/api/v1/libraries/" . $non_existent_code )
137 ->json_is( '/error' => 'Library not found' );
139 $schema->storage->txn_rollback;
142 subtest 'add() tests' => sub {
146 $schema->storage->txn_begin;
148 my $authorized_patron = $builder->build_object({
149 class => 'Koha::Patrons',
150 value => { flags => 1 }
152 my $password = 'thePassword123';
153 $authorized_patron->set_password({ password => $password, skip_validation => 1 });
154 my $auth_userid = $authorized_patron->userid;
156 my $unauthorized_patron = $builder->build_object({
157 class => 'Koha::Patrons',
158 value => { flags => 4 }
160 $unauthorized_patron->set_password({ password => $password, skip_validation => 1 });
161 my $unauth_userid = $unauthorized_patron->userid;
163 my $library_obj = $builder->build_object({ class => 'Koha::Libraries' });
164 my $library = Koha::REST::V1::Library::_to_api( $library_obj->TO_JSON );
165 $library_obj->delete;
167 # Unauthorized attempt to write
168 $t->post_ok( "//$unauth_userid:$password@/api/v1/libraries" => json => $library )
171 # Authorized attempt to write invalid data
172 my $library_with_invalid_field = { %$library };
173 $library_with_invalid_field->{'branchinvalid'} = 'Library invalid';
175 $t->post_ok( "//$auth_userid:$password@/api/v1/libraries" => json => $library_with_invalid_field )
180 message => "Properties not allowed: branchinvalid.",
186 # Authorized attempt to write
187 $t->post_ok( "//$auth_userid:$password@/api/v1/libraries" => json => $library )
188 ->status_is( 201, 'SWAGGER3.2.1' )
189 ->json_is( '' => $library, 'SWAGGER3.3.1' )
190 ->header_is( Location => '/api/v1/libraries/' . $library->{library_id}, 'SWAGGER3.4.1' );
192 # save the library_id
193 my $library_id = $library->{library_id};
194 # Authorized attempt to create with null id
195 $library->{library_id} = undef;
197 $t->post_ok( "//$auth_userid:$password@/api/v1/libraries" => json => $library )
199 ->json_has('/errors');
201 # Authorized attempt to create with existing id
202 $library->{library_id} = $library_id;
205 $t->post_ok( "//$auth_userid:$password@/api/v1/libraries" => json => $library )
207 ->json_has( '/error' => "Fails when trying to add an existing library_id")
208 ->json_is( '/conflict', 'PRIMARY' ); } # WTF
209 qr/^DBD::mysql::st execute failed: Duplicate entry '(.*)' for key 'PRIMARY'/;
211 $schema->storage->txn_rollback;
214 subtest 'update() tests' => sub {
217 $schema->storage->txn_begin;
219 my $authorized_patron = $builder->build_object({
220 class => 'Koha::Patrons',
221 value => { flags => 1 }
223 my $password = 'thePassword123';
224 $authorized_patron->set_password({ password => $password, skip_validation => 1 });
225 my $auth_userid = $authorized_patron->userid;
227 my $unauthorized_patron = $builder->build_object({
228 class => 'Koha::Patrons',
229 value => { flags => 4 }
231 $unauthorized_patron->set_password({ password => $password, skip_validation => 1 });
232 my $unauth_userid = $unauthorized_patron->userid;
234 my $library = $builder->build_object({ class => 'Koha::Libraries' });
235 my $library_id = $library->branchcode;
237 # Unauthorized attempt to update
238 $t->put_ok( "//$unauth_userid:$password@/api/v1/libraries/$library_id"
239 => json => { name => 'New unauthorized name change' } )
242 # Attempt partial update on a PUT
243 my $library_with_missing_field = {
244 address1 => "New library address",
247 $t->put_ok( "//$auth_userid:$password@/api/v1/libraries/$library_id" => json => $library_with_missing_field )
249 ->json_has( "/errors" =>
250 [ { message => "Missing property.", path => "/body/address2" } ]
253 my $deleted_library = $builder->build_object( { class => 'Koha::Libraries' } );
254 my $library_with_updated_field = Koha::REST::V1::Library::_to_api( $deleted_library->TO_JSON );
255 $library_with_updated_field->{library_id} = $library_id;
256 $deleted_library->delete;
258 $t->put_ok( "//$auth_userid:$password@/api/v1/libraries/$library_id" => json => $library_with_updated_field )
259 ->status_is(200, 'SWAGGER3.2.1')
260 ->json_is( '' => $library_with_updated_field, 'SWAGGER3.3.3' );
262 # Authorized attempt to write invalid data
263 my $library_with_invalid_field = { %$library_with_updated_field };
264 $library_with_invalid_field->{'branchinvalid'} = 'Library invalid';
266 $t->put_ok( "//$auth_userid:$password@/api/v1/libraries/$library_id" => json => $library_with_invalid_field )
271 message => "Properties not allowed: branchinvalid.",
277 my $non_existent_code = 'nope'.int(rand(10000));
278 $t->put_ok("//$auth_userid:$password@/api/v1/libraries/$non_existent_code" => json => $library_with_updated_field)
281 $schema->storage->txn_rollback;
284 subtest 'delete() tests' => sub {
287 $schema->storage->txn_begin;
289 my $authorized_patron = $builder->build_object({
290 class => 'Koha::Patrons',
291 value => { flags => 1 }
293 my $password = 'thePassword123';
294 $authorized_patron->set_password({ password => $password, skip_validation => 1 });
295 my $auth_userid = $authorized_patron->userid;
297 my $unauthorized_patron = $builder->build_object({
298 class => 'Koha::Patrons',
299 value => { flags => 4 }
301 $unauthorized_patron->set_password({ password => $password, skip_validation => 1 });
302 my $unauth_userid = $unauthorized_patron->userid;
304 my $library_id = $builder->build( { source => 'Branch' } )->{branchcode};
306 # Unauthorized attempt to delete
307 $t->delete_ok( "//$unauth_userid:$password@/api/v1/libraries/$library_id" )
310 $t->delete_ok( "//$auth_userid:$password@/api/v1/libraries/$library_id" )
311 ->status_is(204, 'SWAGGER3.2.4')
312 ->content_is('', 'SWAGGER3.3.4');
314 $t->delete_ok( "//$auth_userid:$password@/api/v1/libraries/$library_id" )
317 $schema->storage->txn_rollback;