Bug 18936: (QA follow-up) Fix failing test in api/v1/holds.t
[koha.git] / t / db_dependent / api / v1 / holds.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 under the
6 # terms of the GNU General Public License as published by the Free Software
7 # Foundation; either version 3 of the License, or (at your option) any later
8 # version.
9 #
10 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
11 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License along
15 # with Koha; if not, write to the Free Software Foundation, Inc.,
16 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17
18 use Modern::Perl;
19
20 use Test::More tests => 8;
21 use Test::Mojo;
22 use t::lib::TestBuilder;
23 use t::lib::Mocks;
24
25 use DateTime;
26
27 use C4::Context;
28 use Koha::Patrons;
29 use C4::Reserves;
30 use C4::Items;
31
32 use Koha::Database;
33 use Koha::DateUtils;
34 use Koha::Biblios;
35 use Koha::Biblioitems;
36 use Koha::Items;
37 use Koha::CirculationRules;
38
39 my $schema  = Koha::Database->new->schema;
40 my $builder = t::lib::TestBuilder->new();
41
42 $schema->storage->txn_begin;
43
44 t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 );
45
46 my $t = Test::Mojo->new('Koha::REST::V1');
47
48 my $categorycode = $builder->build({ source => 'Category' })->{categorycode};
49 my $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
50 my $itemtype = $builder->build({ source => 'Itemtype' })->{itemtype};
51
52 # Generic password for everyone
53 my $password = 'thePassword123';
54
55 # User without any permissions
56 my $nopermission = $builder->build_object({
57     class => 'Koha::Patrons',
58     value => {
59         branchcode   => $branchcode,
60         categorycode => $categorycode,
61         flags        => 0
62     }
63 });
64 $nopermission->set_password( { password => $password, skip_validation => 1 } );
65 my $nopermission_userid = $nopermission->userid;
66
67 my $patron_1 = $builder->build_object(
68     {
69         class => 'Koha::Patrons',
70         value => {
71             categorycode => $categorycode,
72             branchcode   => $branchcode,
73             surname      => 'Test Surname',
74             flags        => 80, #borrowers and reserveforothers flags
75         }
76     }
77 );
78 $patron_1->set_password( { password => $password, skip_validation => 1 } );
79 my $userid_1 = $patron_1->userid;
80
81 my $patron_2 = $builder->build_object(
82     {
83         class => 'Koha::Patrons',
84         value => {
85             categorycode => $categorycode,
86             branchcode   => $branchcode,
87             surname      => 'Test Surname 2',
88             flags        => 16, # borrowers flag
89         }
90     }
91 );
92 $patron_2->set_password( { password => $password, skip_validation => 1 } );
93 my $userid_2 = $patron_2->userid;
94
95 my $patron_3 = $builder->build_object(
96     {
97         class => 'Koha::Patrons',
98         value => {
99             categorycode => $categorycode,
100             branchcode   => $branchcode,
101             surname      => 'Test Surname 3',
102             flags        => 64, # reserveforothers flag
103         }
104     }
105 );
106 $patron_3->set_password( { password => $password, skip_validation => 1 } );
107 my $userid_3 = $patron_3->userid;
108
109 my $biblio_1 = $builder->build_sample_biblio;
110 my $item_1   = $builder->build_sample_item({ biblionumber => $biblio_1->biblionumber, itype => $itemtype });
111
112 my $biblio_2 = $builder->build_sample_biblio;
113 my $item_2   = $builder->build_sample_item({ biblionumber => $biblio_2->biblionumber, itype => $itemtype });
114
115 my $dbh = C4::Context->dbh;
116 $dbh->do('DELETE FROM reserves');
117 Koha::CirculationRules->search()->delete();
118 Koha::CirculationRules->set_rules(
119     {
120         categorycode => undef,
121         branchcode   => undef,
122         itemtype     => undef,
123         rules        => {
124             reservesallowed => 1,
125             holds_per_record => 99
126         }
127     }
128 );
129
130 my $reserve_id = C4::Reserves::AddReserve($branchcode, $patron_1->borrowernumber,
131     $biblio_1->biblionumber, undef, 1, undef, undef, undef, '', $item_1->itemnumber);
132
133 # Add another reserve to be able to change first reserve's rank
134 my $reserve_id2 = C4::Reserves::AddReserve($branchcode, $patron_2->borrowernumber,
135     $biblio_1->biblionumber, undef, 2, undef, undef, undef, '', $item_1->itemnumber);
136
137 my $suspended_until = DateTime->now->add(days => 10)->truncate( to => 'day' );
138 my $expiration_date = DateTime->now->add(days => 10)->truncate( to => 'day' );
139
140 my $post_data = {
141     patron_id => int($patron_1->borrowernumber),
142     biblio_id => int($biblio_1->biblionumber),
143     item_id => int($item_1->itemnumber),
144     pickup_library_id => $branchcode,
145     expiration_date => output_pref({ dt => $expiration_date, dateformat => 'rfc3339', dateonly => 1 }),
146     priority => 2,
147 };
148 my $put_data = {
149     priority => 2,
150     suspended_until => output_pref({ dt => $suspended_until, dateformat => 'rfc3339' }),
151 };
152
153 subtest "Test endpoints without authentication" => sub {
154     plan tests => 8;
155     $t->get_ok('/api/v1/holds')
156       ->status_is(401);
157     $t->post_ok('/api/v1/holds')
158       ->status_is(401);
159     $t->put_ok('/api/v1/holds/0')
160       ->status_is(401);
161     $t->delete_ok('/api/v1/holds/0')
162       ->status_is(401);
163 };
164
165 subtest "Test endpoints without permission" => sub {
166
167     plan tests => 10;
168
169     $t->get_ok( "//$nopermission_userid:$password@/api/v1/holds?patron_id=" . $patron_1->borrowernumber ) # no permission
170       ->status_is(403);
171
172     $t->get_ok( "//$userid_3:$password@/api/v1/holds?patron_id=" . $patron_1->borrowernumber )    # no permission
173       ->status_is(403);
174
175     $t->post_ok( "//$nopermission_userid:$password@/api/v1/holds" => json => $post_data )
176       ->status_is(403);
177
178     $t->put_ok( "//$nopermission_userid:$password@/api/v1/holds/0" => json => $put_data )
179       ->status_is(403);
180
181     $t->delete_ok( "//$nopermission_userid:$password@/api/v1/holds/0" )
182       ->status_is(403);
183 };
184
185 subtest "Test endpoints with permission" => sub {
186
187     plan tests => 44;
188
189     $t->get_ok( "//$userid_1:$password@/api/v1/holds" )
190       ->status_is(200)
191       ->json_has('/0')
192       ->json_has('/1')
193       ->json_hasnt('/2');
194
195     $t->get_ok( "//$userid_1:$password@/api/v1/holds?priority=2" )
196       ->status_is(200)
197       ->json_is('/0/patron_id', $patron_2->borrowernumber)
198       ->json_hasnt('/1');
199
200     $t->put_ok( "//$userid_1:$password@/api/v1/holds/$reserve_id" => json => $put_data )
201       ->status_is(200)
202       ->json_is( '/hold_id', $reserve_id )
203       ->json_is( '/suspended_until', output_pref({ dt => $suspended_until, dateformat => 'rfc3339' }) )
204       ->json_is( '/priority', 2 );
205
206     $t->delete_ok( "//$userid_3:$password@/api/v1/holds/$reserve_id" )
207       ->status_is(200);
208
209     $t->put_ok( "//$userid_3:$password@/api/v1/holds/$reserve_id" => json => $put_data )
210       ->status_is(404)
211       ->json_has('/error');
212
213     $t->delete_ok( "//$userid_3:$password@/api/v1/holds/$reserve_id" )
214       ->status_is(404)
215       ->json_has('/error');
216
217     $t->get_ok( "//$userid_2:$password@/api/v1/holds?patron_id=" . $patron_1->borrowernumber )
218       ->status_is(200)
219       ->json_is([]);
220
221     my $inexisting_borrowernumber = $patron_2->borrowernumber * 2;
222     $t->get_ok( "//$userid_1:$password@/api/v1/holds?patron_id=$inexisting_borrowernumber")
223       ->status_is(200)
224       ->json_is([]);
225
226     $t->delete_ok( "//$userid_3:$password@/api/v1/holds/$reserve_id2" )
227       ->status_is(200);
228
229     $t->post_ok( "//$userid_3:$password@/api/v1/holds" => json => $post_data )
230       ->status_is(201)
231       ->json_has('/hold_id');
232     # Get id from response
233     $reserve_id = $t->tx->res->json->{hold_id};
234
235     $t->get_ok( "//$userid_1:$password@/api/v1/holds?patron_id=" . $patron_1->borrowernumber )
236       ->status_is(200)
237       ->json_is('/0/hold_id', $reserve_id)
238       ->json_is('/0/expiration_date', output_pref({ dt => $expiration_date, dateformat => 'rfc3339', dateonly => 1 }))
239       ->json_is('/0/pickup_library_id', $branchcode);
240
241     $t->post_ok( "//$userid_3:$password@/api/v1/holds" => json => $post_data )
242       ->status_is(403)
243       ->json_like('/error', qr/itemAlreadyOnHold/);
244
245     $post_data->{biblionumber} = int($biblio_2->biblionumber);
246     $post_data->{itemnumber}   = int($item_2->itemnumber);
247
248     $t->post_ok( "//$userid_3:$password@/api/v1/holds" => json => $post_data )
249       ->status_is(403)
250       ->json_like('/error', qr/itemAlreadyOnHold/);
251 };
252
253 subtest 'Reserves with itemtype' => sub {
254     plan tests => 9;
255
256     my $post_data = {
257         patron_id => int($patron_1->borrowernumber),
258         biblio_id => int($biblio_1->biblionumber),
259         pickup_library_id => $branchcode,
260         item_type => $itemtype,
261     };
262
263     $t->delete_ok( "//$userid_3:$password@/api/v1/holds/$reserve_id" )
264       ->status_is(200);
265
266     $t->post_ok( "//$userid_3:$password@/api/v1/holds" => json => $post_data )
267       ->status_is(201)
268       ->json_has('/hold_id');
269
270     $reserve_id = $t->tx->res->json->{hold_id};
271
272     $t->get_ok( "//$userid_1:$password@/api/v1/holds?patron_id=" . $patron_1->borrowernumber )
273       ->status_is(200)
274       ->json_is('/0/hold_id', $reserve_id)
275       ->json_is('/0/item_type', $itemtype);
276 };
277
278
279 subtest 'test AllowHoldDateInFuture' => sub {
280
281     plan tests => 6;
282
283     $dbh->do('DELETE FROM reserves');
284
285     my $future_hold_date = DateTime->now->add(days => 10)->truncate( to => 'day' );
286
287     my $post_data = {
288         patron_id => int($patron_1->borrowernumber),
289         biblio_id => int($biblio_1->biblionumber),
290         item_id => int($item_1->itemnumber),
291         pickup_library_id => $branchcode,
292         expiration_date => output_pref({ dt => $expiration_date, dateformat => 'rfc3339', dateonly => 1 }),
293         hold_date => output_pref({ dt => $future_hold_date, dateformat => 'rfc3339', dateonly => 1 }),
294         priority => 2,
295     };
296
297     t::lib::Mocks::mock_preference( 'AllowHoldDateInFuture', 0 );
298
299     $t->post_ok( "//$userid_3:$password@/api/v1/holds" => json => $post_data )
300       ->status_is(400)
301       ->json_has('/error');
302
303     t::lib::Mocks::mock_preference( 'AllowHoldDateInFuture', 1 );
304
305     $t->post_ok( "//$userid_3:$password@/api/v1/holds" => json => $post_data )
306       ->status_is(201)
307       ->json_is('/hold_date', output_pref({ dt => $future_hold_date, dateformat => 'rfc3339', dateonly => 1 }));
308 };
309
310 subtest 'test AllowHoldPolicyOverride' => sub {
311
312     plan tests => 5;
313
314     $dbh->do('DELETE FROM reserves');
315
316     Koha::CirculationRules->set_rules(
317         {
318             itemtype     => undef,
319             branchcode   => undef,
320             rules        => {
321                 holdallowed              => 1
322             }
323         }
324     );
325
326     t::lib::Mocks::mock_preference( 'AllowHoldPolicyOverride', 0 );
327
328     $t->post_ok( "//$userid_3:$password@/api/v1/holds" => json => $post_data )
329       ->status_is(403)
330       ->json_has('/error');
331
332     t::lib::Mocks::mock_preference( 'AllowHoldPolicyOverride', 1 );
333
334     $t->post_ok( "//$userid_3:$password@/api/v1/holds" => json => $post_data )
335       ->status_is(201);
336 };
337
338 $schema->storage->txn_rollback;
339
340 subtest 'suspend and resume tests' => sub {
341
342     plan tests => 21;
343
344     $schema->storage->txn_begin;
345
346     my $password = 'AbcdEFG123';
347
348     my $patron = $builder->build_object(
349         { class => 'Koha::Patrons', value => { userid => 'tomasito', flags => 1 } } );
350     $patron->set_password({ password => $password, skip_validation => 1 });
351     my $userid = $patron->userid;
352
353     # Disable logging
354     t::lib::Mocks::mock_preference( 'HoldsLog',      0 );
355     t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 );
356
357     my $hold = $builder->build_object(
358         {   class => 'Koha::Holds',
359             value => { suspend => 0, suspend_until => undef, waitingdate => undef }
360         }
361     );
362
363     ok( !$hold->is_suspended, 'Hold is not suspended' );
364     $t->post_ok( "//$userid:$password@/api/v1/holds/" . $hold->id . "/suspension" )
365         ->status_is( 201, 'Hold suspension created' );
366
367     $hold->discard_changes;    # refresh object
368
369     ok( $hold->is_suspended, 'Hold is suspended' );
370     $t->json_is(
371         '/end_date',
372         output_pref(
373             {   dt         => dt_from_string( $hold->suspend_until ),
374                 dateformat => 'rfc3339',
375                 dateonly   => 1
376             }
377         )
378     );
379
380     $t->delete_ok( "//$userid:$password@/api/v1/holds/" . $hold->id . "/suspension" )
381       ->status_is( 204, "Correct status when deleting a resource" )
382       ->json_is( undef );
383
384     # Pass a an expiration date for the suspension
385     my $date = dt_from_string()->add( days => 5 );
386     $t->post_ok(
387               "//$userid:$password@/api/v1/holds/"
388             . $hold->id
389             . "/suspension" => json => {
390             end_date =>
391                 output_pref( { dt => $date, dateformat => 'rfc3339', dateonly => 1 } )
392             }
393     )->status_is( 201, 'Hold suspension created' )
394         ->json_is( '/end_date',
395         output_pref( { dt => $date, dateformat => 'rfc3339', dateonly => 1 } ) )
396         ->header_is( Location => "/api/v1/holds/" . $hold->id . "/suspension", 'The Location header is set' );
397
398     $t->delete_ok( "//$userid:$password@/api/v1/holds/" . $hold->id . "/suspension" )
399       ->status_is( 204, "Correct status when deleting a resource" )
400       ->json_is( undef );
401
402     $hold->set_waiting->discard_changes;
403
404     $t->post_ok( "//$userid:$password@/api/v1/holds/" . $hold->id . "/suspension" )
405       ->status_is( 400, 'Cannot suspend waiting hold' )
406       ->json_is( '/error', 'Found hold cannot be suspended. Status=W' );
407
408     $hold->set_waiting(1)->discard_changes;
409
410     $t->post_ok( "//$userid:$password@/api/v1/holds/" . $hold->id . "/suspension" )
411       ->status_is( 400, 'Cannot suspend waiting hold' )
412       ->json_is( '/error', 'Found hold cannot be suspended. Status=T' );
413
414     $schema->storage->txn_rollback;
415 };
416
417 subtest 'PUT /holds/{hold_id}/priority tests' => sub {
418
419     plan tests => 14;
420
421     $schema->storage->txn_begin;
422
423     my $password = 'AbcdEFG123';
424
425     my $library  = $builder->build_object({ class => 'Koha::Libraries' });
426     my $patron_np = $builder->build_object(
427         { class => 'Koha::Patrons', value => { flags => 0 } } );
428     $patron_np->set_password( { password => $password, skip_validation => 1 } );
429     my $userid_np = $patron_np->userid;
430
431     my $patron = $builder->build_object(
432         { class => 'Koha::Patrons', value => { flags => 0 } } );
433     $patron->set_password( { password => $password, skip_validation => 1 } );
434     my $userid = $patron->userid;
435     $builder->build(
436         {
437             source => 'UserPermission',
438             value  => {
439                 borrowernumber => $patron->borrowernumber,
440                 module_bit     => 6,
441                 code           => 'modify_holds_priority',
442             },
443         }
444     );
445
446     # Disable logging
447     t::lib::Mocks::mock_preference( 'HoldsLog',      0 );
448     t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 );
449
450     my $biblio   = $builder->build_sample_biblio;
451     my $patron_1 = $builder->build_object(
452         {
453             class => 'Koha::Patrons',
454             value => { branchcode => $library->branchcode }
455         }
456     );
457     my $patron_2 = $builder->build_object(
458         {
459             class => 'Koha::Patrons',
460             value => { branchcode => $library->branchcode }
461         }
462     );
463     my $patron_3 = $builder->build_object(
464         {
465             class => 'Koha::Patrons',
466             value => { branchcode => $library->branchcode }
467         }
468     );
469
470     my $hold_1 = Koha::Holds->find(
471         AddReserve(
472             $library->branchcode,  $patron_1->borrowernumber,
473             $biblio->biblionumber, undef,
474             1
475         )
476     );
477     my $hold_2 = Koha::Holds->find(
478         AddReserve(
479             $library->branchcode,  $patron_2->borrowernumber,
480             $biblio->biblionumber, undef,
481             2
482         )
483     );
484     my $hold_3 = Koha::Holds->find(
485         AddReserve(
486             $library->branchcode,  $patron_3->borrowernumber,
487             $biblio->biblionumber, undef,
488             3
489         )
490     );
491
492     $t->put_ok( "//$userid_np:$password@/api/v1/holds/"
493           . $hold_3->id
494           . "/priority" => json => 1 )->status_is(403);
495
496     $t->put_ok( "//$userid:$password@/api/v1/holds/"
497           . $hold_3->id
498           . "/priority" => json => 1 )->status_is(200)->json_is(1);
499
500     is( $hold_1->discard_changes->priority, 2, 'Priority adjusted correctly' );
501     is( $hold_2->discard_changes->priority, 3, 'Priority adjusted correctly' );
502     is( $hold_3->discard_changes->priority, 1, 'Priority adjusted correctly' );
503
504     $t->put_ok( "//$userid:$password@/api/v1/holds/"
505           . $hold_3->id
506           . "/priority" => json => 3 )->status_is(200)->json_is(3);
507
508     is( $hold_1->discard_changes->priority, 1, 'Priority adjusted correctly' );
509     is( $hold_2->discard_changes->priority, 2, 'Priority adjusted correctly' );
510     is( $hold_3->discard_changes->priority, 3, 'Priority adjusted correctly' );
511
512     $schema->storage->txn_rollback;
513 };