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;
23 use t::lib::TestBuilder;
26 use Koha::Suggestions;
29 my $schema = Koha::Database->new->schema;
30 my $builder = t::lib::TestBuilder->new;
32 my $t = Test::Mojo->new('Koha::REST::V1');
33 t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 );
35 subtest 'list() tests' => sub {
39 $schema->storage->txn_begin;
41 my $librarian = $builder->build_object(
43 class => 'Koha::Patrons',
44 value => { flags => 2 ** 12 } # suggestions flag = 12
47 my $password = 'thePassword123';
48 $librarian->set_password( { password => $password, skip_validation => 1 } );
49 my $userid = $librarian->userid;
51 my $patron = $builder->build_object(
53 class => 'Koha::Patrons',
54 value => { flags => 0 }
58 $patron->set_password( { password => $password, skip_validation => 1 } );
60 my $unauth_userid = $patron->userid;
61 my $patron_id = $patron->id;
63 ## Authorized user tests
64 # No suggestions by patron, so empty array should be returned
65 $t->get_ok("//$userid:$password@/api/v1/suggestions?q={\"suggested_by\":\"$patron_id\"}")
66 ->status_is(200)->json_is( [] );
68 my $suggestion_1 = $builder->build_object(
70 class => 'Koha::Suggestions',
71 value => { suggestedby => $patron_id, STATUS => 'ASKED' }
75 # One city created, should get returned
76 $t->get_ok("//$userid:$password@/api/v1/suggestions?q={\"suggested_by\":\"$patron_id\"}")
77 ->status_is(200)->json_is( [ $suggestion_1->to_api ] );
79 my $suggestion_2 = $builder->build_object(
81 class => 'Koha::Suggestions',
82 value => { suggestedby => $patron_id, STATUS => 'ASKED' }
86 # Two SMTP servers created, they should both be returned
87 $t->get_ok("//$userid:$password@/api/v1/suggestions?q={\"suggested_by\":\"$patron_id\"}")
89 ->json_is( [ $suggestion_1->to_api, $suggestion_2->to_api, ] );
92 $t->get_ok("//$unauth_userid:$password@/api/v1/suggestions")
95 $schema->storage->txn_rollback;
98 subtest 'get() tests' => sub {
102 $schema->storage->txn_begin;
104 my $librarian = $builder->build_object(
106 class => 'Koha::Patrons',
107 value => { flags => 2 ** 12 } # suggestions flag = 12
110 my $password = 'thePassword123';
111 $librarian->set_password( { password => $password, skip_validation => 1 } );
112 my $userid = $librarian->userid;
114 my $patron = $builder->build_object(
116 class => 'Koha::Patrons',
117 value => { flags => 0 }
121 $patron->set_password( { password => $password, skip_validation => 1 } );
122 my $unauth_userid = $patron->userid;
123 my $patron_id = $patron->id;
125 my $suggestion = $builder->build_object(
127 class => 'Koha::Suggestions',
128 value => { suggestedby => $patron_id, STATUS => 'ASKED' }
133 "//$userid:$password@/api/v1/suggestions/" . $suggestion->id )
134 ->status_is(200)->json_is( $suggestion->to_api );
136 $t->get_ok( "//$unauth_userid:$password@/api/v1/suggestions/"
137 . $suggestion->id )->status_is(403);
139 my $suggestion_to_delete = $builder->build_object( { class => 'Koha::Suggestions' } );
140 my $non_existent_id = $suggestion_to_delete->id;
141 $suggestion_to_delete->delete;
144 "//$userid:$password@/api/v1/suggestions/$non_existent_id")
145 ->status_is(404)->json_is( '/error' => 'Suggestion not found.' );
147 $schema->storage->txn_rollback;
150 subtest 'add() tests' => sub {
154 $schema->storage->txn_begin;
156 my $librarian = $builder->build_object(
158 class => 'Koha::Patrons',
159 value => { flags => 2 ** 12 } # suggestions flag = 12
162 my $password = 'thePassword123';
163 $librarian->set_password( { password => $password, skip_validation => 1 } );
164 my $userid = $librarian->userid;
166 my $patron = $builder->build_object(
168 class => 'Koha::Patrons',
169 value => { flags => 0 }
173 $patron->set_password( { password => $password, skip_validation => 1 } );
175 my $unauth_userid = $patron->userid;
176 my $patron_id = $patron->id;
178 my $suggestion = $builder->build_object(
180 class => 'Koha::Suggestions',
181 value => { suggestedby => $patron_id, STATUS => 'ASKED' }
184 my $suggestion_data = $suggestion->to_api;
185 delete $suggestion_data->{suggestion_id};
188 # Unauthorized attempt to write
190 "//$unauth_userid:$password@/api/v1/suggestions" => json =>
191 $suggestion_data )->status_is(403);
193 # Authorized attempt to write invalid data
194 my $suggestion_with_invalid_field = {
198 $t->post_ok( "//$userid:$password@/api/v1/suggestions" => json =>
199 $suggestion_with_invalid_field )->status_is(400)->json_is(
202 message => "Properties not allowed: blah.",
208 # Authorized attempt to write
209 my $generated_suggestion =
210 $t->post_ok( "//$userid:$password@/api/v1/suggestions" => json =>
211 $suggestion_data )->status_is( 201, 'SWAGGER3.2.1' )->header_like(
212 Location => qr|^\/api\/v1\/suggestions\/\d*|,
216 my $suggestion_id = $generated_suggestion->{suggestion_id};
218 $generated_suggestion,
219 Koha::Suggestions->find($suggestion_id)->to_api,
220 'The object is returned'
223 # Authorized attempt to create with null id
224 $suggestion_data->{suggestion_id} = undef;
225 $t->post_ok( "//$userid:$password@/api/v1/suggestions" => json =>
226 $suggestion_data )->status_is(400)->json_has('/errors');
228 # Authorized attempt to create with existing id
229 $suggestion_data->{suggestion_id} = $suggestion_id;
230 $t->post_ok( "//$userid:$password@/api/v1/suggestions" => json =>
231 $suggestion_data )->status_is(400)->json_is(
234 message => "Read-only.",
235 path => "/body/suggestion_id"
240 subtest 'x-koha-override tests' => sub {
244 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
246 t::lib::Mocks::mock_preference( 'MaxTotalSuggestions', 4 );
247 t::lib::Mocks::mock_preference( 'MaxOpenSuggestions', 2 );
248 t::lib::Mocks::mock_preference( 'NumberOfSuggestionDays', 2 );
250 my $suggestion = $builder->build_object(
251 { class => 'Koha::Suggestions',
252 value => { suggestedby => $patron->id, STATUS => 'ACCEPTED' }
256 my $suggestion_data = $suggestion->to_api;
257 delete $suggestion_data->{suggestion_id};
258 delete $suggestion_data->{status};
260 $t->post_ok( "//$userid:$password@/api/v1/suggestions" => json => $suggestion_data )
261 ->status_is( 201, 'First pending suggestion' );
263 $t->post_ok( "//$userid:$password@/api/v1/suggestions" => json => $suggestion_data )
264 ->status_is( 201, 'Second pending suggestion' );
266 $t->post_ok( "//$userid:$password@/api/v1/suggestions" => json => $suggestion_data )
268 ->json_is( '/error_code' => 'max_pending_reached' );
270 $t->post_ok( "//$userid:$password@/api/v1/suggestions"
271 => { 'x-koha-override' => 'max_pending' }
272 => json => $suggestion_data )
273 ->status_is( 201, 'max_pending override does the job' );
275 $t->post_ok( "//$userid:$password@/api/v1/suggestions" => json => $suggestion_data )
277 ->json_is( '/error_code' => 'max_total_reached' );
280 "//$userid:$password@/api/v1/suggestions" => { 'x-koha-override' => 'any' } => json => $suggestion_data )
281 ->status_is( 201, 'any overrides anything' );
284 $schema->storage->txn_rollback;
287 subtest 'update() tests' => sub {
291 $schema->storage->txn_begin;
293 my $librarian = $builder->build_object(
295 class => 'Koha::Patrons',
296 value => { flags => 2 ** 12 } # suggestions flag = 12
299 my $password = 'thePassword123';
300 $librarian->set_password( { password => $password, skip_validation => 1 } );
301 my $userid = $librarian->userid;
303 my $patron = $builder->build_object(
305 class => 'Koha::Patrons',
306 value => { flags => 0 }
310 $patron->set_password( { password => $password, skip_validation => 1 } );
311 my $unauth_userid = $patron->userid;
313 my $suggestion_id = $builder->build_object(
315 class => 'Koha::Suggestions',
316 value => { STATUS => 'ASKED' }
320 # Unauthorized attempt to update
322 "//$unauth_userid:$password@/api/v1/suggestions/$suggestion_id"
323 => json => { name => 'New unauthorized name change' } )
326 # Full object update on PUT
327 my $suggestion_with_updated_field = { reason => "Some reason", };
330 "//$userid:$password@/api/v1/suggestions/$suggestion_id" =>
331 json => $suggestion_with_updated_field )->status_is(200)
332 ->json_is( '/reason' => 'Some reason' );
334 # Authorized attempt to write invalid data
335 my $suggestion_with_invalid_field = {
337 reason => 'Some reason'
341 "//$userid:$password@/api/v1/suggestions/$suggestion_id" =>
342 json => $suggestion_with_invalid_field )->status_is(400)->json_is(
345 message => "Properties not allowed: blah.",
351 my $suggestion_to_delete = $builder->build_object({ class => 'Koha::Suggestions' });
352 my $non_existent_id = $suggestion_to_delete->id;
353 $suggestion_to_delete->delete;
356 "//$userid:$password@/api/v1/suggestions/$non_existent_id" =>
357 json => $suggestion_with_updated_field )->status_is(404);
359 # Wrong method (POST)
360 $suggestion_with_updated_field->{smtp_server_id} = 2;
363 "//$userid:$password@/api/v1/suggestions/$suggestion_id" =>
364 json => $suggestion_with_updated_field )->status_is(404);
366 $schema->storage->txn_rollback;
369 subtest 'delete() tests' => sub {
373 $schema->storage->txn_begin;
375 my $librarian = $builder->build_object(
377 class => 'Koha::Patrons',
378 value => { flags => 2 ** 12 } # suggestions flag = 12
381 my $password = 'thePassword123';
382 $librarian->set_password( { password => $password, skip_validation => 1 } );
383 my $userid = $librarian->userid;
385 my $patron = $builder->build_object(
387 class => 'Koha::Patrons',
388 value => { flags => 0 }
392 $patron->set_password( { password => $password, skip_validation => 1 } );
393 my $unauth_userid = $patron->userid;
395 my $suggestion_id = $builder->build_object({ class => 'Koha::Suggestions' } )->id;
397 # Unauthorized attempt to delete
399 "//$unauth_userid:$password@/api/v1/suggestions/$suggestion_id"
403 "//$userid:$password@/api/v1/suggestions/$suggestion_id")
404 ->status_is( 204, 'SWAGGER3.2.4' )->content_is( q{}, 'SWAGGER3.3.4' );
407 "//$userid:$password@/api/v1/suggestions/$suggestion_id")
410 $schema->storage->txn_rollback;