Bug 32030: ERM - Add more API tests
[srvgit] / t / db_dependent / api / v1 / erm_eholdings_titles.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::EHoldings::Titles;
27 use Koha::ERM::EHoldings::Packages;
28 use Koha::Virtualshelves;
29 use Koha::Database;
30
31 my $schema  = Koha::Database->new->schema;
32 my $builder = t::lib::TestBuilder->new;
33
34 my $t = Test::Mojo->new('Koha::REST::V1');
35
36 subtest 'list() tests' => sub {
37
38     plan tests => 23;
39
40     $schema->storage->txn_begin;
41
42     Koha::ERM::EHoldings::Titles->search->delete;
43
44     my $librarian = $builder->build_object(
45         {
46             class => 'Koha::Patrons',
47             value => { flags => 2**28 }
48         }
49     );
50     my $password = 'thePassword123';
51     $librarian->set_password( { password => $password, skip_validation => 1 } );
52     my $userid = $librarian->userid;
53
54     my $patron = $builder->build_object(
55         {
56             class => 'Koha::Patrons',
57             value => { flags => 0 }
58         }
59     );
60
61     $patron->set_password( { password => $password, skip_validation => 1 } );
62     my $unauth_userid = $patron->userid;
63
64     ## Authorized user tests
65     # No EHoldings title, so empty array should be returned
66     $t->get_ok("//$userid:$password@/api/v1/erm/eholdings/local/titles")
67       ->status_is(200)->json_is( [] );
68
69     my $ehtitle =
70       $builder->build_object( { class => 'Koha::ERM::EHoldings::Titles' } );
71
72     # One EHoldings title created, should get returned
73     $t->get_ok("//$userid:$password@/api/v1/erm/eholdings/local/titles")
74       ->status_is(200)->json_is( [ $ehtitle->to_api ] );
75
76     my $another_ehtitle = $builder->build_object(
77         {
78             class => 'Koha::ERM::EHoldings::Titles',
79             value => { publication_type => $ehtitle->publication_type }
80         }
81     );
82     my $ehtitle_with_another_publication_type =
83       $builder->build_object( { class => 'Koha::ERM::EHoldings::Titles' } );
84
85     # Two EHoldings titles created, they should both be returned
86     $t->get_ok("//$userid:$password@/api/v1/erm/eholdings/local/titles")
87       ->status_is(200)->json_is(
88         [
89             $ehtitle->to_api,
90             $another_ehtitle->to_api,
91             $ehtitle_with_another_publication_type->to_api
92         ]
93       );
94
95     # Filtering works, two EHoldings titles sharing publication_type
96     $t->get_ok(
97 "//$userid:$password@/api/v1/erm/eholdings/local/titles?publication_type="
98           . $ehtitle->publication_type )->status_is(200)
99       ->json_is( [ $ehtitle->to_api, $another_ehtitle->to_api ] );
100
101     # Attempt to search by publication_title like 'ko'
102     $ehtitle->delete;
103     $another_ehtitle->delete;
104     $ehtitle_with_another_publication_type->delete;
105     $t->get_ok(qq~//$userid:$password@/api/v1/erm/eholdings/local/titles?q=[{"me.publication_title":{"like":"%ko%"}}]~)
106       ->status_is(200)->json_is( [] );
107
108     my $ehtitle_to_search = $builder->build_object(
109         {
110             class => 'Koha::ERM::EHoldings::Titles',
111             value => {
112                 publication_title => 'koha',
113             }
114         }
115     );
116
117     # Search works, searching for publication_title like 'ko'
118     $t->get_ok(qq~//$userid:$password@/api/v1/erm/eholdings/local/titles?q=[{"me.publication_title":{"like":"%ko%"}}]~)
119       ->status_is(200)->json_is( [ $ehtitle_to_search->to_api ] );
120
121     # Warn on unsupported query parameter
122     $t->get_ok(
123         "//$userid:$password@/api/v1/erm/eholdings/local/titles?blah=blah")
124       ->status_is(400)
125       ->json_is(
126         [ { path => '/query/blah', message => 'Malformed query string' } ] );
127
128     # Unauthorized access
129     $t->get_ok("//$unauth_userid:$password@/api/v1/erm/eholdings/local/titles")
130       ->status_is(403);
131
132     $schema->storage->txn_rollback;
133 };
134
135 subtest 'get() tests' => sub {
136
137     plan tests => 11;
138
139     $schema->storage->txn_begin;
140
141     my $ehtitle =
142       $builder->build_object( { class => 'Koha::ERM::EHoldings::Titles' } );
143     my $librarian = $builder->build_object(
144         {
145             class => 'Koha::Patrons',
146             value => { flags => 2**28 }
147         }
148     );
149     my $password = 'thePassword123';
150     $librarian->set_password( { password => $password, skip_validation => 1 } );
151     my $userid = $librarian->userid;
152
153     my $patron = $builder->build_object(
154         {
155             class => 'Koha::Patrons',
156             value => { flags => 0 }
157         }
158     );
159
160     $patron->set_password( { password => $password, skip_validation => 1 } );
161     my $unauth_userid = $patron->userid;
162
163     # This EHoldings title exists, should get returned
164     $t->get_ok( "//$userid:$password@/api/v1/erm/eholdings/local/titles/"
165           . $ehtitle->title_id )->status_is(200)->json_is( $ehtitle->to_api );
166
167     # Return one EHoldings title with embed
168     $t->get_ok( "//$userid:$password@/api/v1/erm/eholdings/local/titles/"
169           . $ehtitle->title_id =>
170           { 'x-koha-embed' => 'resources,resources.package' } )->status_is(200)
171       ->json_is( { %{ $ehtitle->to_api }, resources => [] } );
172
173     # Unauthorized access
174     $t->get_ok( "//$unauth_userid:$password@/api/v1/erm/eholdings/local/titles/"
175           . $ehtitle->title_id )->status_is(403);
176
177     # Attempt to get non-existent EHoldings title
178     my $ehtitle_to_delete =
179       $builder->build_object( { class => 'Koha::ERM::EHoldings::Titles' } );
180     my $non_existent_id = $ehtitle_to_delete->title_id;
181     $ehtitle_to_delete->delete;
182
183     $t->get_ok(
184 "//$userid:$password@/api/v1/erm/eholdings/local/titles/$non_existent_id"
185     )->status_is(404)->json_is( '/error' => 'eHolding title not found' );
186
187     $schema->storage->txn_rollback;
188 };
189
190 subtest 'add() tests' => sub {
191
192     plan tests => 24;
193
194     $schema->storage->txn_begin;
195
196     my $librarian = $builder->build_object(
197         {
198             class => 'Koha::Patrons',
199             value => { flags => 2**28 }
200         }
201     );
202     my $password = 'thePassword123';
203     $librarian->set_password( { password => $password, skip_validation => 1 } );
204     my $userid = $librarian->userid;
205
206     my $patron = $builder->build_object(
207         {
208             class => 'Koha::Patrons',
209             value => { flags => 0 }
210         }
211     );
212
213     $patron->set_password( { password => $password, skip_validation => 1 } );
214     my $unauth_userid = $patron->userid;
215
216     my $ehtitle = {
217         publication_title => "Publication title",
218         print_identifier  => "Print-format identifier",
219         online_identifier => "Online-format identifier",
220         date_first_issue_online =>
221           "Date of first serial issue available online",
222         num_first_vol_online   => "Number of first volume available online",
223         num_first_issue_online => "Number of first issue available online",
224         date_last_issue_online => "Date of last issue available online",
225         num_last_vol_online    => "Number of last volume available online",
226         num_last_issue_online  => "Number of last issue available online",
227         title_url              => "Title-level URL",
228         first_author           => "First author",
229         embargo_info           => "Embargo information",
230         coverage_depth         => "Coverage depth",
231         notes                  => "Notes",
232         publisher_name         => "Publisher name",
233         publication_type       => "Book",
234         date_monograph_published_print =>
235           "Date the monograph is first published in print",
236         date_monograph_published_online =>
237           "Date the monograph is first published online",
238         monograph_volume  => "Number of volume for monograph",
239         monograph_edition => "Edition of the monograph",
240         first_editor      => "First editor",
241         parent_publication_title_id =>
242           "Title identifier of the parent publication",
243         preceeding_publication_title_id =>
244           "Title identifier of any preceding publication title",
245         access_type => "Access type"
246     };
247
248     # Unauthorized attempt to write
249     $t->post_ok(
250         "//$unauth_userid:$password@/api/v1/erm/eholdings/local/titles" =>
251           json => $ehtitle )->status_is(403);
252
253     # Authorized attempt to write invalid data
254     my $ehtitle_with_invalid_field = {
255         blah              => "EHolding Title Blah",
256         publication_title => "Publication title",
257         print_identifier  => "Print-format identifier"
258     };
259
260     $t->post_ok(
261         "//$userid:$password@/api/v1/erm/eholdings/local/titles" => json =>
262           $ehtitle_with_invalid_field )->status_is(400)->json_is(
263         "/errors" => [
264             {
265                 message => "Properties not allowed: blah.",
266                 path    => "/body"
267             }
268         ]
269           );
270
271     # Authorized attempt to write
272     my $ehtitle_id =
273       $t->post_ok(
274         "//$userid:$password@/api/v1/erm/eholdings/local/titles" => json =>
275           $ehtitle )->status_is( 201, 'SWAGGER3.2.1' )->header_like(
276         Location => qr|^/api/v1/erm/eholdings/local/titles/\d*|,
277         'SWAGGER3.4.1'
278     )->json_is( '/publication_title' => $ehtitle->{publication_title} )
279       ->json_is( '/print_identifier' => $ehtitle->{print_identifier} )
280       ->json_is( '/notes'            => $ehtitle->{notes} )
281       ->json_is( '/publisher_name'   => $ehtitle->{publisher_name} )
282       ->tx->res->json->{title_id};
283
284     # Import titles from virtualshelf to package
285     my $ehpackage_id =
286       $builder->build_object( { class => 'Koha::ERM::EHoldings::Packages' } )
287       ->package_id;
288
289     my $virtual_shelf =
290       $builder->build_object(
291         {
292           class => 'Koha::Virtualshelves',
293       } );
294     $virtual_shelf->transfer_ownership($librarian->borrowernumber);
295      my $virtual_shelf_id = $virtual_shelf->shelfnumber;
296
297     my $import_request =
298     {
299         list_id  => $virtual_shelf_id,
300         package_id => $ehpackage_id
301     };
302
303     $t->post_ok(
304     "//$userid:$password@/api/v1/erm/eholdings/local/titles/import" => json =>
305       $import_request )->status_is(201)->json_has('/job_id');
306
307     # Attempt to import titles from a virtualshelf that doesn't exist
308     $virtual_shelf->delete;
309     $t->post_ok(
310     "//$userid:$password@/api/v1/erm/eholdings/local/titles/import" => json =>
311       $import_request )->status_is(404)->json_is(
312         { error => 'List not found' }
313       );
314
315     # Authorized attempt to create with null id
316     $ehtitle->{title_id} = undef;
317     $t->post_ok(
318         "//$userid:$password@/api/v1/erm/eholdings/local/titles" => json =>
319           $ehtitle )->status_is(400)->json_has('/errors');
320
321     # Authorized attempt to create with existing id
322     $ehtitle->{title_id} = $ehtitle_id;
323     $t->post_ok(
324         "//$userid:$password@/api/v1/erm/eholdings/local/titles" => json =>
325           $ehtitle )->status_is(400)->json_is(
326         "/errors" => [
327             {
328                 message => "Read-only.",
329                 path    => "/body/title_id"
330             }
331         ]
332           );
333
334     $schema->storage->txn_rollback;
335 };
336
337 subtest 'update() tests' => sub {
338
339     plan tests => 15;
340
341     $schema->storage->txn_begin;
342
343     my $librarian = $builder->build_object(
344         {
345             class => 'Koha::Patrons',
346             value => { flags => 2**28 }
347         }
348     );
349     my $password = 'thePassword123';
350     $librarian->set_password( { password => $password, skip_validation => 1 } );
351     my $userid = $librarian->userid;
352
353     my $patron = $builder->build_object(
354         {
355             class => 'Koha::Patrons',
356             value => { flags => 0 }
357         }
358     );
359
360     $patron->set_password( { password => $password, skip_validation => 1 } );
361     my $unauth_userid = $patron->userid;
362
363     my $ehtitle_id =
364       $builder->build_object( { class => 'Koha::ERM::EHoldings::Titles' } )
365       ->title_id;
366
367     # Unauthorized attempt to update
368     $t->put_ok(
369 "//$unauth_userid:$password@/api/v1/erm/eholdings/local/titles/$ehtitle_id"
370           => json =>
371           { publication_title => 'New unauthorized publication_title change' } )
372       ->status_is(403);
373
374     # Attempt partial update on a PUT
375     my $ehtitle_with_missing_field = { date_first_issue_online =>
376           "Date of first serial issue available online", };
377
378     $t->put_ok(
379         "//$userid:$password@/api/v1/erm/eholdings/local/titles/$ehtitle_id" =>
380           json => $ehtitle_with_missing_field )->status_is(400)->json_is(
381         "/errors" => [
382             {
383                 message => "Missing property.",
384                 path    => "/body/publication_title"
385             }
386         ]
387           );
388
389     # Full object update on PUT
390     my $ehtitle_with_updated_field = {
391         publication_title => "Publication title",
392         print_identifier  => "Print-format identifier",
393         online_identifier => "Online-format identifier",
394         date_first_issue_online =>
395           "Date of first serial issue available online",
396         num_first_vol_online   => "Number of first volume available online",
397         num_first_issue_online => "Number of first issue available online",
398         date_last_issue_online => "Date of last issue available online",
399         num_last_vol_online    => "Number of last volume available online",
400         num_last_issue_online  => "Number of last issue available online",
401         title_url              => "Title-level URL",
402         first_author           => "First author",
403         embargo_info           => "Embargo information",
404         coverage_depth         => "Coverage depth",
405         notes                  => "Notes",
406         publisher_name         => "Publisher name",
407         publication_type       => "Book",
408         date_monograph_published_print =>
409           "Date the monograph is first published in print",
410         date_monograph_published_online =>
411           "Date the monograph is first published online",
412         monograph_volume  => "Number of volume for monograph",
413         monograph_edition => "Edition of the monograph",
414         first_editor      => "First editor",
415         parent_publication_title_id =>
416           "Title identifier of the parent publication",
417         preceeding_publication_title_id =>
418           "Title identifier of any preceding publication title",
419         access_type => "Access type"
420     };
421
422     $t->put_ok(
423         "//$userid:$password@/api/v1/erm/eholdings/local/titles/$ehtitle_id" =>
424           json => $ehtitle_with_updated_field )->status_is(200)
425       ->json_is( '/publication_title' => 'Publication title' );
426
427     # Authorized attempt to write invalid data
428     my $ehtitle_with_invalid_field = {
429         blah              => "EHolding Title Blah",
430         publication_title => "Publication title",
431         print_identifier  => "Print-format identifier"
432     };
433
434     $t->put_ok(
435         "//$userid:$password@/api/v1/erm/eholdings/local/titles/$ehtitle_id" =>
436           json => $ehtitle_with_invalid_field )->status_is(400)->json_is(
437         "/errors" => [
438             {
439                 message => "Properties not allowed: blah.",
440                 path    => "/body"
441             }
442         ]
443           );
444
445     # Attempt to update non-existent EHolding title
446     my $ehtitle_to_delete =
447       $builder->build_object( { class => 'Koha::ERM::EHoldings::Titles' } );
448     my $non_existent_id = $ehtitle_to_delete->title_id;
449     $ehtitle_to_delete->delete;
450
451     $t->put_ok(
452 "//$userid:$password@/api/v1/erm/eholdings/local/titles/$non_existent_id"
453           => json => $ehtitle_with_updated_field )->status_is(404);
454
455     # Wrong method (POST)
456     $ehtitle_with_updated_field->{title_id} = 2;
457
458     $t->post_ok(
459         "//$userid:$password@/api/v1/erm/eholdings/local/titles/$ehtitle_id" =>
460           json => $ehtitle_with_updated_field )->status_is(404);
461
462     $schema->storage->txn_rollback;
463 };
464
465 subtest 'delete() tests' => sub {
466
467     plan tests => 7;
468
469     $schema->storage->txn_begin;
470
471     my $librarian = $builder->build_object(
472         {
473             class => 'Koha::Patrons',
474             value => { flags => 2**28 }
475         }
476     );
477     my $password = 'thePassword123';
478     $librarian->set_password( { password => $password, skip_validation => 1 } );
479     my $userid = $librarian->userid;
480
481     my $patron = $builder->build_object(
482         {
483             class => 'Koha::Patrons',
484             value => { flags => 0 }
485         }
486     );
487
488     $patron->set_password( { password => $password, skip_validation => 1 } );
489     my $unauth_userid = $patron->userid;
490
491     my $ehtitle_id =
492       $builder->build_object( { class => 'Koha::ERM::EHoldings::Titles' } )
493       ->title_id;
494
495     # Unauthorized attempt to delete
496     $t->delete_ok(
497 "//$unauth_userid:$password@/api/v1/erm/eholdings/local/titles/$ehtitle_id"
498     )->status_is(403);
499
500     # Delete existing EHolding title
501     $t->delete_ok(
502         "//$userid:$password@/api/v1/erm/eholdings/local/titles/$ehtitle_id")
503       ->status_is( 204, 'SWAGGER3.2.4' )->content_is( '', 'SWAGGER3.3.4' );
504
505     # Attempt to delete non-existent EHolding title
506     $t->delete_ok(
507         "//$userid:$password@/api/v1/erm/eholdings/local/titles/$ehtitle_id")
508       ->status_is(404);
509
510     $schema->storage->txn_rollback;
511 };
512