Bug 32030: ERM - Add more API tests
[koha-ffzg.git] / t / db_dependent / api / v1 / erm_licenses.t
1 #!/usr/bin/env perl
2
3 # This file is part of Koha.
4 #
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.
9 #
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.
14 #
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>.
17
18 use Modern::Perl;
19
20 use Test::More tests => 5;
21 use Test::Mojo;
22
23 use t::lib::TestBuilder;
24 use t::lib::Mocks;
25
26 use Koha::ERM::Licenses;
27 use Koha::Database;
28
29 my $schema  = Koha::Database->new->schema;
30 my $builder = t::lib::TestBuilder->new;
31
32 my $t = Test::Mojo->new('Koha::REST::V1');
33
34 subtest 'list() tests' => sub {
35
36     plan tests => 23;
37
38     $schema->storage->txn_begin;
39
40     Koha::ERM::Licenses->search->delete;
41
42     my $librarian = $builder->build_object(
43         {
44             class => 'Koha::Patrons',
45             value => { flags => 2**28 }
46         }
47     );
48     my $password = 'thePassword123';
49     $librarian->set_password( { password => $password, skip_validation => 1 } );
50     my $userid = $librarian->userid;
51
52     my $patron = $builder->build_object(
53         {
54             class => 'Koha::Patrons',
55             value => { flags => 0 }
56         }
57     );
58
59     $patron->set_password( { password => $password, skip_validation => 1 } );
60     my $unauth_userid = $patron->userid;
61
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)
65       ->json_is( [] );
66
67     my $license =
68       $builder->build_object( { class => 'Koha::ERM::Licenses' } );
69
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 ] );
73
74     my $another_license = $builder->build_object(
75         {
76             class => 'Koha::ERM::Licenses',
77             value => { vendor_id => $license->vendor_id }
78         }
79     );
80     my $license_with_another_vendor_id =
81       $builder->build_object( { class => 'Koha::ERM::Licenses' } );
82
83     # Two licenses created, they should both be returned
84     $t->get_ok("//$userid:$password@/api/v1/erm/licenses")->status_is(200)
85       ->json_is(
86         [
87             $license->to_api,
88             $another_license->to_api,
89             $license_with_another_vendor_id->to_api
90         ]
91       );
92
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 ] );
97
98     # Attempt to search by name like 'ko'
99     $license->delete;
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%"}}]~)
103       ->status_is(200)
104       ->json_is( [] );
105
106     my $license_to_search = $builder->build_object(
107         {
108             class => 'Koha::ERM::Licenses',
109             value => {
110                 name => 'koha',
111             }
112         }
113     );
114
115     # Search works, searching for name like 'ko'
116     $t->get_ok( qq~//$userid:$password@/api/v1/erm/licenses?q=[{"me.name":{"like":"%ko%"}}]~)
117       ->status_is(200)
118       ->json_is( [ $license_to_search->to_api ] );
119
120     # Warn on unsupported query parameter
121     $t->get_ok("//$userid:$password@/api/v1/erm/licenses?blah=blah")
122       ->status_is(400)
123       ->json_is(
124         [ { path => '/query/blah', message => 'Malformed query string' } ] );
125
126     # Unauthorized access
127     $t->get_ok("//$unauth_userid:$password@/api/v1/erm/licenses")
128       ->status_is(403);
129
130     $schema->storage->txn_rollback;
131 };
132
133 subtest 'get() tests' => sub {
134
135     plan tests => 11;
136
137     $schema->storage->txn_begin;
138
139     my $license =
140       $builder->build_object( { class => 'Koha::ERM::Licenses' } );
141     my $librarian = $builder->build_object(
142         {
143             class => 'Koha::Patrons',
144             value => { flags => 2**28 }
145         }
146     );
147     my $password = 'thePassword123';
148     $librarian->set_password( { password => $password, skip_validation => 1 } );
149     my $userid = $librarian->userid;
150
151     my $patron = $builder->build_object(
152         {
153             class => 'Koha::Patrons',
154             value => { flags => 0 }
155         }
156     );
157
158     $patron->set_password( { password => $password, skip_validation => 1 } );
159     my $unauth_userid = $patron->userid;
160
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 );
165
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 => [] });
170
171     # Unauthorized access
172     $t->get_ok( "//$unauth_userid:$password@/api/v1/erm/licenses/"
173           . $license->license_id )->status_is(403);
174
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;
180
181     $t->get_ok("//$userid:$password@/api/v1/erm/licenses/$non_existent_id")
182       ->status_is(404)->json_is( '/error' => 'License not found' );
183
184     $schema->storage->txn_rollback;
185 };
186
187 subtest 'add() tests' => sub {
188
189     plan tests => 18;
190
191     $schema->storage->txn_begin;
192
193     my $librarian = $builder->build_object(
194         {
195             class => 'Koha::Patrons',
196             value => { flags => 2**28 }
197         }
198     );
199     my $password = 'thePassword123';
200     $librarian->set_password( { password => $password, skip_validation => 1 } );
201     my $userid = $librarian->userid;
202
203     my $patron = $builder->build_object(
204         {
205             class => 'Koha::Patrons',
206             value => { flags => 0 }
207         }
208     );
209
210     $patron->set_password( { password => $password, skip_validation => 1 } );
211     my $unauth_userid = $patron->userid;
212
213     my $license = {
214         name             => "License name",
215         description      => "License description",
216         type             => 'local',
217         status           => "active",
218         started_on       => undef,
219         ended_on         => undef,
220     };
221
222     # Unauthorized attempt to write
223     $t->post_ok( "//$unauth_userid:$password@/api/v1/erm/licenses" => json =>
224           $license )->status_is(403);
225
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",
231         type             => 'local',
232         status           => "active",
233         started_on       => undef,
234         ended_on         => undef,
235     };
236
237     $t->post_ok( "//$userid:$password@/api/v1/erm/licenses" => json =>
238           $license_with_invalid_field )->status_is(400)->json_is(
239         "/errors" => [
240             {
241                 message => "Properties not allowed: blah.",
242                 path    => "/body"
243             }
244         ]
245           );
246
247     # Authorized attempt to write
248     my $license_id =
249       $t->post_ok(
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*|,
253         'SWAGGER3.4.1'
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};
259
260     # Authorized attempt to create with null id
261     $license->{license_id} = undef;
262     $t->post_ok(
263         "//$userid:$password@/api/v1/erm/licenses" => json => $license )
264       ->status_is(400)->json_has('/errors');
265
266     # Authorized attempt to create with existing id
267     $license->{license_id} = $license_id;
268     $t->post_ok(
269         "//$userid:$password@/api/v1/erm/licenses" => json => $license )
270       ->status_is(400)->json_is(
271         "/errors" => [
272             {
273                 message => "Read-only.",
274                 path    => "/body/license_id"
275             }
276         ]
277       );
278
279     $schema->storage->txn_rollback;
280 };
281
282 subtest 'update() tests' => sub {
283
284     plan tests => 15;
285
286     $schema->storage->txn_begin;
287
288     my $librarian = $builder->build_object(
289         {
290             class => 'Koha::Patrons',
291             value => { flags => 2**28 }
292         }
293     );
294     my $password = 'thePassword123';
295     $librarian->set_password( { password => $password, skip_validation => 1 } );
296     my $userid = $librarian->userid;
297
298     my $patron = $builder->build_object(
299         {
300             class => 'Koha::Patrons',
301             value => { flags => 0 }
302         }
303     );
304
305     $patron->set_password( { password => $password, skip_validation => 1 } );
306     my $unauth_userid = $patron->userid;
307
308     my $license_id =
309       $builder->build_object( { class => 'Koha::ERM::Licenses' } )->license_id;
310
311     # Unauthorized attempt to update
312     $t->put_ok(
313         "//$unauth_userid:$password@/api/v1/erm/licenses/$license_id" =>
314           json => { name => 'New unauthorized name change' } )->status_is(403);
315
316     # Attempt partial update on a PUT
317     my $license_with_missing_field = {
318         description      => 'New description',
319         type             => 'national',
320         status           => 'expired',
321         started_on       => undef,
322         ended_on         => undef,
323     };
324
325     $t->put_ok(
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" } ] );
330
331     # Full object update on PUT
332     my $license_with_updated_field = {
333         name             => 'New name',
334         description      => 'New description',
335         type             => 'national',
336         status           => 'expired',
337         started_on       => undef,
338         ended_on         => undef,
339     };
340
341     $t->put_ok(
342         "//$userid:$password@/api/v1/erm/licenses/$license_id" => json =>
343           $license_with_updated_field )->status_is(200)
344       ->json_is( '/name' => 'New name' );
345
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",
351         type             => 'national',
352         status           => 'expired',
353         started_on       => undef,
354         ended_on         => undef,
355     };
356
357     $t->put_ok(
358         "//$userid:$password@/api/v1/erm/licenses/$license_id" => json =>
359           $license_with_invalid_field )->status_is(400)->json_is(
360         "/errors" => [
361             {
362                 message => "Properties not allowed: blah.",
363                 path    => "/body"
364             }
365         ]
366           );
367
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;
373
374     $t->put_ok( "//$userid:$password@/api/v1/erm/licenses/$non_existent_id" =>
375           json => $license_with_updated_field )->status_is(404);
376
377     # Wrong method (POST)
378     $license_with_updated_field->{license_id} = 2;
379
380     $t->post_ok(
381         "//$userid:$password@/api/v1/erm/licenses/$license_id" => json =>
382           $license_with_updated_field )->status_is(404);
383
384     $schema->storage->txn_rollback;
385 };
386
387 subtest 'delete() tests' => sub {
388
389     plan tests => 7;
390
391     $schema->storage->txn_begin;
392
393     my $librarian = $builder->build_object(
394         {
395             class => 'Koha::Patrons',
396             value => { flags => 2**28 }
397         }
398     );
399     my $password = 'thePassword123';
400     $librarian->set_password( { password => $password, skip_validation => 1 } );
401     my $userid = $librarian->userid;
402
403     my $patron = $builder->build_object(
404         {
405             class => 'Koha::Patrons',
406             value => { flags => 0 }
407         }
408     );
409
410     $patron->set_password( { password => $password, skip_validation => 1 } );
411     my $unauth_userid = $patron->userid;
412
413     my $license_id =
414       $builder->build_object( { class => 'Koha::ERM::Licenses' } )->license_id;
415
416     # Unauthorized attempt to delete
417     $t->delete_ok(
418         "//$unauth_userid:$password@/api/v1/erm/licenses/$license_id")
419       ->status_is(403);
420
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' );
424
425     # Attempt to delete non-existent license
426     $t->delete_ok("//$userid:$password@/api/v1/erm/licenses/$license_id")
427       ->status_is(404);
428
429     $schema->storage->txn_rollback;
430 };
431