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 use Data::Printer colored => 1;
31 my $schema = Koha::Database->new->schema;
32 my $builder = t::lib::TestBuilder->new;
34 my $t = Test::Mojo->new('Koha::REST::V1');
35 t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 );
37 subtest 'list() tests' => sub {
41 $schema->storage->txn_begin;
43 my $librarian = $builder->build_object(
45 class => 'Koha::Patrons',
46 value => { flags => 2 ** 12 } # suggestions flag = 12
49 my $password = 'thePassword123';
50 $librarian->set_password( { password => $password, skip_validation => 1 } );
51 my $userid = $librarian->userid;
53 my $patron = $builder->build_object(
55 class => 'Koha::Patrons',
56 value => { flags => 0 }
60 $patron->set_password( { password => $password, skip_validation => 1 } );
62 my $unauth_userid = $patron->userid;
63 my $patron_id = $patron->id;
65 ## Authorized user tests
66 # No suggestions by patron, so empty array should be returned
67 $t->get_ok("//$userid:$password@/api/v1/suggestions?q={\"suggested_by\":\"$patron_id\"}")
68 ->status_is(200)->json_is( [] );
70 my $suggestion_1 = $builder->build_object(
72 class => 'Koha::Suggestions',
73 value => { suggestedby => $patron_id, STATUS => 'ASKED' }
77 # One city created, should get returned
78 $t->get_ok("//$userid:$password@/api/v1/suggestions?q={\"suggested_by\":\"$patron_id\"}")
79 ->status_is(200)->json_is( [ $suggestion_1->to_api ] );
81 my $suggestion_2 = $builder->build_object(
83 class => 'Koha::Suggestions',
84 value => { suggestedby => $patron_id, STATUS => 'ASKED' }
88 # Two SMTP servers created, they should both be returned
89 $t->get_ok("//$userid:$password@/api/v1/suggestions?q={\"suggested_by\":\"$patron_id\"}")
91 ->json_is( [ $suggestion_1->to_api, $suggestion_2->to_api, ] );
94 $t->get_ok("//$unauth_userid:$password@/api/v1/suggestions")
97 $schema->storage->txn_rollback;
100 subtest 'get() tests' => sub {
104 $schema->storage->txn_begin;
106 my $librarian = $builder->build_object(
108 class => 'Koha::Patrons',
109 value => { flags => 2 ** 12 } # suggestions flag = 12
112 my $password = 'thePassword123';
113 $librarian->set_password( { password => $password, skip_validation => 1 } );
114 my $userid = $librarian->userid;
116 my $patron = $builder->build_object(
118 class => 'Koha::Patrons',
119 value => { flags => 0 }
123 $patron->set_password( { password => $password, skip_validation => 1 } );
124 my $unauth_userid = $patron->userid;
125 my $patron_id = $patron->id;
127 my $suggestion = $builder->build_object(
129 class => 'Koha::Suggestions',
130 value => { suggestedby => $patron_id, STATUS => 'ASKED' }
135 "//$userid:$password@/api/v1/suggestions/" . $suggestion->id )
136 ->status_is(200)->json_is( $suggestion->to_api );
138 $t->get_ok( "//$unauth_userid:$password@/api/v1/suggestions/"
139 . $suggestion->id )->status_is(403);
141 my $suggestion_to_delete = $builder->build_object( { class => 'Koha::Suggestions' } );
142 my $non_existent_id = $suggestion_to_delete->id;
143 $suggestion_to_delete->delete;
146 "//$userid:$password@/api/v1/suggestions/$non_existent_id")
147 ->status_is(404)->json_is( '/error' => 'Suggestion not found.' );
149 $schema->storage->txn_rollback;
152 subtest 'add() tests' => sub {
156 $schema->storage->txn_begin;
158 my $librarian = $builder->build_object(
160 class => 'Koha::Patrons',
161 value => { flags => 2 ** 12 } # suggestions flag = 12
164 my $password = 'thePassword123';
165 $librarian->set_password( { password => $password, skip_validation => 1 } );
166 my $userid = $librarian->userid;
168 my $patron = $builder->build_object(
170 class => 'Koha::Patrons',
171 value => { flags => 0 }
175 $patron->set_password( { password => $password, skip_validation => 1 } );
177 my $unauth_userid = $patron->userid;
178 my $patron_id = $patron->id;
180 my $suggestion = $builder->build_object(
182 class => 'Koha::Suggestions',
183 value => { suggestedby => $patron_id, STATUS => 'ASKED' }
186 my $suggestion_data = $suggestion->to_api;
187 delete $suggestion_data->{suggestion_id};
190 # Unauthorized attempt to write
192 "//$unauth_userid:$password@/api/v1/suggestions" => json =>
193 $suggestion_data )->status_is(403);
195 # Authorized attempt to write invalid data
196 my $suggestion_with_invalid_field = {
200 $t->post_ok( "//$userid:$password@/api/v1/suggestions" => json =>
201 $suggestion_with_invalid_field )->status_is(400)->json_is(
204 message => "Properties not allowed: blah.",
210 # Authorized attempt to write
211 my $generated_suggestion =
212 $t->post_ok( "//$userid:$password@/api/v1/suggestions" => json =>
213 $suggestion_data )->status_is( 201, 'SWAGGER3.2.1' )->header_like(
214 Location => qr|^\/api\/v1\/suggestions\/\d*|,
218 my $suggestion_id = $generated_suggestion->{suggestion_id};
220 $generated_suggestion,
221 Koha::Suggestions->find($suggestion_id)->to_api,
222 'The object is returned'
225 # Authorized attempt to create with null id
226 $suggestion_data->{suggestion_id} = undef;
227 $t->post_ok( "//$userid:$password@/api/v1/suggestions" => json =>
228 $suggestion_data )->status_is(400)->json_has('/errors');
230 # Authorized attempt to create with existing id
231 $suggestion_data->{suggestion_id} = $suggestion_id;
232 $t->post_ok( "//$userid:$password@/api/v1/suggestions" => json =>
233 $suggestion_data )->status_is(400)->json_is(
236 message => "Read-only.",
237 path => "/body/suggestion_id"
242 $schema->storage->txn_rollback;
245 subtest 'update() tests' => sub {
249 $schema->storage->txn_begin;
251 my $librarian = $builder->build_object(
253 class => 'Koha::Patrons',
254 value => { flags => 2 ** 12 } # suggestions flag = 12
257 my $password = 'thePassword123';
258 $librarian->set_password( { password => $password, skip_validation => 1 } );
259 my $userid = $librarian->userid;
261 my $patron = $builder->build_object(
263 class => 'Koha::Patrons',
264 value => { flags => 0 }
268 $patron->set_password( { password => $password, skip_validation => 1 } );
269 my $unauth_userid = $patron->userid;
271 my $suggestion_id = $builder->build_object(
273 class => 'Koha::Suggestions',
274 value => { STATUS => 'ASKED' }
278 # Unauthorized attempt to update
280 "//$unauth_userid:$password@/api/v1/suggestions/$suggestion_id"
281 => json => { name => 'New unauthorized name change' } )
284 # Full object update on PUT
285 my $suggestion_with_updated_field = { reason => "Some reason", };
288 "//$userid:$password@/api/v1/suggestions/$suggestion_id" =>
289 json => $suggestion_with_updated_field )->status_is(200)
290 ->json_is( '/reason' => 'Some reason' );
292 # Authorized attempt to write invalid data
293 my $suggestion_with_invalid_field = {
295 reason => 'Some reason'
299 "//$userid:$password@/api/v1/suggestions/$suggestion_id" =>
300 json => $suggestion_with_invalid_field )->status_is(400)->json_is(
303 message => "Properties not allowed: blah.",
309 my $suggestion_to_delete = $builder->build_object({ class => 'Koha::Suggestions' });
310 my $non_existent_id = $suggestion_to_delete->id;
311 $suggestion_to_delete->delete;
314 "//$userid:$password@/api/v1/suggestions/$non_existent_id" =>
315 json => $suggestion_with_updated_field )->status_is(404);
317 # Wrong method (POST)
318 $suggestion_with_updated_field->{smtp_server_id} = 2;
321 "//$userid:$password@/api/v1/suggestions/$suggestion_id" =>
322 json => $suggestion_with_updated_field )->status_is(404);
324 $schema->storage->txn_rollback;
327 subtest 'delete() tests' => sub {
331 $schema->storage->txn_begin;
333 my $librarian = $builder->build_object(
335 class => 'Koha::Patrons',
336 value => { flags => 2 ** 12 } # suggestions flag = 12
339 my $password = 'thePassword123';
340 $librarian->set_password( { password => $password, skip_validation => 1 } );
341 my $userid = $librarian->userid;
343 my $patron = $builder->build_object(
345 class => 'Koha::Patrons',
346 value => { flags => 0 }
350 $patron->set_password( { password => $password, skip_validation => 1 } );
351 my $unauth_userid = $patron->userid;
353 my $suggestion_id = $builder->build_object({ class => 'Koha::Suggestions' } )->id;
355 # Unauthorized attempt to delete
357 "//$unauth_userid:$password@/api/v1/suggestions/$suggestion_id"
361 "//$userid:$password@/api/v1/suggestions/$suggestion_id")
362 ->status_is( 204, 'SWAGGER3.2.4' )->content_is( q{}, 'SWAGGER3.3.4' );
365 "//$userid:$password@/api/v1/suggestions/$suggestion_id")
368 $schema->storage->txn_rollback;