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::ERM::Licenses;
29 my $schema = Koha::Database->new->schema;
30 my $builder = t::lib::TestBuilder->new;
32 my $t = Test::Mojo->new('Koha::REST::V1');
34 subtest 'list() tests' => sub {
38 $schema->storage->txn_begin;
40 Koha::ERM::Licenses->search->delete;
42 my $librarian = $builder->build_object(
44 class => 'Koha::Patrons',
45 value => { flags => 2**28 }
48 my $password = 'thePassword123';
49 $librarian->set_password( { password => $password, skip_validation => 1 } );
50 my $userid = $librarian->userid;
52 my $patron = $builder->build_object(
54 class => 'Koha::Patrons',
55 value => { flags => 0 }
59 $patron->set_password( { password => $password, skip_validation => 1 } );
60 my $unauth_userid = $patron->userid;
62 ## Authorized user tests
63 # No licenses, so empty array should be returned
64 $t->get_ok("//$userid:$password@/api/v1/erm/licenses")->status_is(200)
68 $builder->build_object( { class => 'Koha::ERM::Licenses' } );
70 # One license created, should get returned
71 $t->get_ok("//$userid:$password@/api/v1/erm/licenses")->status_is(200)
72 ->json_is( [ $license->to_api ] );
74 my $another_license = $builder->build_object(
76 class => 'Koha::ERM::Licenses',
77 value => { vendor_id => $license->vendor_id }
80 my $license_with_another_vendor_id =
81 $builder->build_object( { class => 'Koha::ERM::Licenses' } );
83 # Two licenses created, they should both be returned
84 $t->get_ok("//$userid:$password@/api/v1/erm/licenses")->status_is(200)
88 $another_license->to_api,
89 $license_with_another_vendor_id->to_api
93 # Filtering works, two licenses sharing vendor_id
94 $t->get_ok( "//$userid:$password@/api/v1/erm/licenses?vendor_id="
95 . $license->vendor_id )->status_is(200)
96 ->json_is( [ $license->to_api, $another_license->to_api ] );
98 # Attempt to search by name like 'ko'
100 $another_license->delete;
101 $license_with_another_vendor_id->delete;
102 $t->get_ok( qq~//$userid:$password@/api/v1/erm/licenses?q=[{"me.name":{"like":"%ko%"}}]~)
106 my $license_to_search = $builder->build_object(
108 class => 'Koha::ERM::Licenses',
115 # Search works, searching for name like 'ko'
116 $t->get_ok( qq~//$userid:$password@/api/v1/erm/licenses?q=[{"me.name":{"like":"%ko%"}}]~)
118 ->json_is( [ $license_to_search->to_api ] );
120 # Warn on unsupported query parameter
121 $t->get_ok("//$userid:$password@/api/v1/erm/licenses?blah=blah")
124 [ { path => '/query/blah', message => 'Malformed query string' } ] );
126 # Unauthorized access
127 $t->get_ok("//$unauth_userid:$password@/api/v1/erm/licenses")
130 $schema->storage->txn_rollback;
133 subtest 'get() tests' => sub {
137 $schema->storage->txn_begin;
140 $builder->build_object( { class => 'Koha::ERM::Licenses' } );
141 my $librarian = $builder->build_object(
143 class => 'Koha::Patrons',
144 value => { flags => 2**28 }
147 my $password = 'thePassword123';
148 $librarian->set_password( { password => $password, skip_validation => 1 } );
149 my $userid = $librarian->userid;
151 my $patron = $builder->build_object(
153 class => 'Koha::Patrons',
154 value => { flags => 0 }
158 $patron->set_password( { password => $password, skip_validation => 1 } );
159 my $unauth_userid = $patron->userid;
161 # This license exists, should get returned
162 $t->get_ok( "//$userid:$password@/api/v1/erm/licenses/"
163 . $license->license_id )->status_is(200)
164 ->json_is( $license->to_api );
166 # Return one license with embed
167 $t->get_ok( "//$userid:$password@/api/v1/erm/licenses/"
168 . $license->license_id => {'x-koha-embed' => 'documents'} )->status_is(200)
169 ->json_is( { %{ $license->to_api }, documents => [] });
171 # Unauthorized access
172 $t->get_ok( "//$unauth_userid:$password@/api/v1/erm/licenses/"
173 . $license->license_id )->status_is(403);
175 # Attempt to get non-existent license
176 my $license_to_delete =
177 $builder->build_object( { class => 'Koha::ERM::Licenses' } );
178 my $non_existent_id = $license_to_delete->license_id;
179 $license_to_delete->delete;
181 $t->get_ok("//$userid:$password@/api/v1/erm/licenses/$non_existent_id")
182 ->status_is(404)->json_is( '/error' => 'License not found' );
184 $schema->storage->txn_rollback;
187 subtest 'add() tests' => sub {
191 $schema->storage->txn_begin;
193 my $librarian = $builder->build_object(
195 class => 'Koha::Patrons',
196 value => { flags => 2**28 }
199 my $password = 'thePassword123';
200 $librarian->set_password( { password => $password, skip_validation => 1 } );
201 my $userid = $librarian->userid;
203 my $patron = $builder->build_object(
205 class => 'Koha::Patrons',
206 value => { flags => 0 }
210 $patron->set_password( { password => $password, skip_validation => 1 } );
211 my $unauth_userid = $patron->userid;
214 name => "License name",
215 description => "License description",
222 # Unauthorized attempt to write
223 $t->post_ok( "//$unauth_userid:$password@/api/v1/erm/licenses" => json =>
224 $license )->status_is(403);
226 # Authorized attempt to write invalid data
227 my $license_with_invalid_field = {
228 blah => "License Blah",
229 name => "License name",
230 description => "License description",
237 $t->post_ok( "//$userid:$password@/api/v1/erm/licenses" => json =>
238 $license_with_invalid_field )->status_is(400)->json_is(
241 message => "Properties not allowed: blah.",
247 # Authorized attempt to write
250 "//$userid:$password@/api/v1/erm/licenses" => json => $license )
251 ->status_is( 201, 'SWAGGER3.2.1' )->header_like(
252 Location => qr|^/api/v1/erm/licenses/\d*|,
254 )->json_is( '/name' => $license->{name} )
255 ->json_is( '/description' => $license->{description} )
256 ->json_is( '/type' => $license->{type} )
257 ->json_is( '/status' => $license->{status} )
258 ->tx->res->json->{license_id};
260 # Authorized attempt to create with null id
261 $license->{license_id} = undef;
263 "//$userid:$password@/api/v1/erm/licenses" => json => $license )
264 ->status_is(400)->json_has('/errors');
266 # Authorized attempt to create with existing id
267 $license->{license_id} = $license_id;
269 "//$userid:$password@/api/v1/erm/licenses" => json => $license )
270 ->status_is(400)->json_is(
273 message => "Read-only.",
274 path => "/body/license_id"
279 $schema->storage->txn_rollback;
282 subtest 'update() tests' => sub {
286 $schema->storage->txn_begin;
288 my $librarian = $builder->build_object(
290 class => 'Koha::Patrons',
291 value => { flags => 2**28 }
294 my $password = 'thePassword123';
295 $librarian->set_password( { password => $password, skip_validation => 1 } );
296 my $userid = $librarian->userid;
298 my $patron = $builder->build_object(
300 class => 'Koha::Patrons',
301 value => { flags => 0 }
305 $patron->set_password( { password => $password, skip_validation => 1 } );
306 my $unauth_userid = $patron->userid;
309 $builder->build_object( { class => 'Koha::ERM::Licenses' } )->license_id;
311 # Unauthorized attempt to update
313 "//$unauth_userid:$password@/api/v1/erm/licenses/$license_id" =>
314 json => { name => 'New unauthorized name change' } )->status_is(403);
316 # Attempt partial update on a PUT
317 my $license_with_missing_field = {
318 description => 'New description',
326 "//$userid:$password@/api/v1/erm/licenses/$license_id" => json =>
327 $license_with_missing_field )->status_is(400)
328 ->json_is( "/errors" =>
329 [ { message => "Missing property.", path => "/body/name" } ] );
331 # Full object update on PUT
332 my $license_with_updated_field = {
334 description => 'New description',
342 "//$userid:$password@/api/v1/erm/licenses/$license_id" => json =>
343 $license_with_updated_field )->status_is(200)
344 ->json_is( '/name' => 'New name' );
346 # Authorized attempt to write invalid data
347 my $license_with_invalid_field = {
348 blah => "License Blah",
349 name => "License name",
350 description => "License description",
358 "//$userid:$password@/api/v1/erm/licenses/$license_id" => json =>
359 $license_with_invalid_field )->status_is(400)->json_is(
362 message => "Properties not allowed: blah.",
368 # Attempt to update non-existent license
369 my $license_to_delete =
370 $builder->build_object( { class => 'Koha::ERM::Licenses' } );
371 my $non_existent_id = $license_to_delete->id;
372 $license_to_delete->delete;
374 $t->put_ok( "//$userid:$password@/api/v1/erm/licenses/$non_existent_id" =>
375 json => $license_with_updated_field )->status_is(404);
377 # Wrong method (POST)
378 $license_with_updated_field->{license_id} = 2;
381 "//$userid:$password@/api/v1/erm/licenses/$license_id" => json =>
382 $license_with_updated_field )->status_is(404);
384 $schema->storage->txn_rollback;
387 subtest 'delete() tests' => sub {
391 $schema->storage->txn_begin;
393 my $librarian = $builder->build_object(
395 class => 'Koha::Patrons',
396 value => { flags => 2**28 }
399 my $password = 'thePassword123';
400 $librarian->set_password( { password => $password, skip_validation => 1 } );
401 my $userid = $librarian->userid;
403 my $patron = $builder->build_object(
405 class => 'Koha::Patrons',
406 value => { flags => 0 }
410 $patron->set_password( { password => $password, skip_validation => 1 } );
411 my $unauth_userid = $patron->userid;
414 $builder->build_object( { class => 'Koha::ERM::Licenses' } )->license_id;
416 # Unauthorized attempt to delete
418 "//$unauth_userid:$password@/api/v1/erm/licenses/$license_id")
421 # Delete existing license
422 $t->delete_ok("//$userid:$password@/api/v1/erm/licenses/$license_id")
423 ->status_is( 204, 'SWAGGER3.2.4' )->content_is( '', 'SWAGGER3.3.4' );
425 # Attempt to delete non-existent license
426 $t->delete_ok("//$userid:$password@/api/v1/erm/licenses/$license_id")
429 $schema->storage->txn_rollback;