Bug 18936: Convert issuingrules fields to circulation_rules
[koha-equinox.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 $dbh->do('DELETE FROM circulation_rules');
118 Koha::CirculationRules->set_rules(
119     {
120         categorycode => '*',
121         branchcode   => '*',
122         itemtype     => '*',
123         rules        => {
124             reservesallowed => 1
125         }
126     }
127 );
128
129 my $reserve_id = C4::Reserves::AddReserve($branchcode, $patron_1->borrowernumber,
130     $biblio_1->biblionumber, undef, 1, undef, undef, undef, '', $item_1->itemnumber);
131
132 # Add another reserve to be able to change first reserve's rank
133 my $reserve_id2 = C4::Reserves::AddReserve($branchcode, $patron_2->borrowernumber,
134     $biblio_1->biblionumber, undef, 2, undef, undef, undef, '', $item_1->itemnumber);
135
136 my $suspended_until = DateTime->now->add(days => 10)->truncate( to => 'day' );
137 my $expiration_date = DateTime->now->add(days => 10)->truncate( to => 'day' );
138
139 my $post_data = {
140     patron_id => int($patron_1->borrowernumber),
141     biblio_id => int($biblio_1->biblionumber),
142     item_id => int($item_1->itemnumber),
143     pickup_library_id => $branchcode,
144     expiration_date => output_pref({ dt => $expiration_date, dateformat => 'rfc3339', dateonly => 1 }),
145     priority => 2,
146 };
147 my $put_data = {
148     priority => 2,
149     suspended_until => output_pref({ dt => $suspended_until, dateformat => 'rfc3339' }),
150 };
151
152 subtest "Test endpoints without authentication" => sub {
153     plan tests => 8;
154     $t->get_ok('/api/v1/holds')
155       ->status_is(401);
156     $t->post_ok('/api/v1/holds')
157       ->status_is(401);
158     $t->put_ok('/api/v1/holds/0')
159       ->status_is(401);
160     $t->delete_ok('/api/v1/holds/0')
161       ->status_is(401);
162 };
163
164 subtest "Test endpoints without permission" => sub {
165
166     plan tests => 10;
167
168     $t->get_ok( "//$nopermission_userid:$password@/api/v1/holds?patron_id=" . $patron_1->borrowernumber ) # no permission
169       ->status_is(403);
170
171     $t->get_ok( "//$userid_3:$password@/api/v1/holds?patron_id=" . $patron_1->borrowernumber )    # no permission
172       ->status_is(403);
173
174     $t->post_ok( "//$nopermission_userid:$password@/api/v1/holds" => json => $post_data )
175       ->status_is(403);
176
177     $t->put_ok( "//$nopermission_userid:$password@/api/v1/holds/0" => json => $put_data )
178       ->status_is(403);
179
180     $t->delete_ok( "//$nopermission_userid:$password@/api/v1/holds/0" )
181       ->status_is(403);
182 };
183
184 subtest "Test endpoints with permission" => sub {
185
186     plan tests => 44;
187
188     $t->get_ok( "//$userid_1:$password@/api/v1/holds" )
189       ->status_is(200)
190       ->json_has('/0')
191       ->json_has('/1')
192       ->json_hasnt('/2');
193
194     $t->get_ok( "//$userid_1:$password@/api/v1/holds?priority=2" )
195       ->status_is(200)
196       ->json_is('/0/patron_id', $patron_2->borrowernumber)
197       ->json_hasnt('/1');
198
199     $t->put_ok( "//$userid_1:$password@/api/v1/holds/$reserve_id" => json => $put_data )
200       ->status_is(200)
201       ->json_is( '/hold_id', $reserve_id )
202       ->json_is( '/suspended_until', output_pref({ dt => $suspended_until, dateformat => 'rfc3339' }) )
203       ->json_is( '/priority', 2 );
204
205     $t->delete_ok( "//$userid_3:$password@/api/v1/holds/$reserve_id" )
206       ->status_is(200);
207
208     $t->put_ok( "//$userid_3:$password@/api/v1/holds/$reserve_id" => json => $put_data )
209       ->status_is(404)
210       ->json_has('/error');
211
212     $t->delete_ok( "//$userid_3:$password@/api/v1/holds/$reserve_id" )
213       ->status_is(404)
214       ->json_has('/error');
215
216     $t->get_ok( "//$userid_2:$password@/api/v1/holds?patron_id=" . $patron_1->borrowernumber )
217       ->status_is(200)
218       ->json_is([]);
219
220     my $inexisting_borrowernumber = $patron_2->borrowernumber * 2;
221     $t->get_ok( "//$userid_1:$password@/api/v1/holds?patron_id=$inexisting_borrowernumber")
222       ->status_is(200)
223       ->json_is([]);
224
225     $t->delete_ok( "//$userid_3:$password@/api/v1/holds/$reserve_id2" )
226       ->status_is(200);
227
228     $t->post_ok( "//$userid_3:$password@/api/v1/holds" => json => $post_data )
229       ->status_is(201)
230       ->json_has('/hold_id');
231     # Get id from response
232     $reserve_id = $t->tx->res->json->{hold_id};
233
234     $t->get_ok( "//$userid_1:$password@/api/v1/holds?patron_id=" . $patron_1->borrowernumber )
235       ->status_is(200)
236       ->json_is('/0/hold_id', $reserve_id)
237       ->json_is('/0/expiration_date', output_pref({ dt => $expiration_date, dateformat => 'rfc3339', dateonly => 1 }))
238       ->json_is('/0/pickup_library_id', $branchcode);
239
240     $t->post_ok( "//$userid_3:$password@/api/v1/holds" => json => $post_data )
241       ->status_is(403)
242       ->json_like('/error', qr/itemAlreadyOnHold/);
243
244     $post_data->{biblionumber} = int($biblio_2->biblionumber);
245     $post_data->{itemnumber}   = int($item_2->itemnumber);
246
247     $t->post_ok( "//$userid_3:$password@/api/v1/holds" => json => $post_data )
248       ->status_is(403)
249       ->json_like('/error', qr/itemAlreadyOnHold/);
250 };
251
252 subtest 'Reserves with itemtype' => sub {
253     plan tests => 9;
254
255     my $post_data = {
256         patron_id => int($patron_1->borrowernumber),
257         biblio_id => int($biblio_1->biblionumber),
258         pickup_library_id => $branchcode,
259         item_type => $itemtype,
260     };
261
262     $t->delete_ok( "//$userid_3:$password@/api/v1/holds/$reserve_id" )
263       ->status_is(200);
264
265     $t->post_ok( "//$userid_3:$password@/api/v1/holds" => json => $post_data )
266       ->status_is(201)
267       ->json_has('/hold_id');
268
269     $reserve_id = $t->tx->res->json->{hold_id};
270
271     $t->get_ok( "//$userid_1:$password@/api/v1/holds?patron_id=" . $patron_1->borrowernumber )
272       ->status_is(200)
273       ->json_is('/0/hold_id', $reserve_id)
274       ->json_is('/0/item_type', $itemtype);
275 };
276
277
278 subtest 'test AllowHoldDateInFuture' => sub {
279
280     plan tests => 6;
281
282     $dbh->do('DELETE FROM reserves');
283
284     my $future_hold_date = DateTime->now->add(days => 10)->truncate( to => 'day' );
285
286     my $post_data = {
287         patron_id => int($patron_1->borrowernumber),
288         biblio_id => int($biblio_1->biblionumber),
289         item_id => int($item_1->itemnumber),
290         pickup_library_id => $branchcode,
291         expiration_date => output_pref({ dt => $expiration_date, dateformat => 'rfc3339', dateonly => 1 }),
292         hold_date => output_pref({ dt => $future_hold_date, dateformat => 'rfc3339', dateonly => 1 }),
293         priority => 2,
294     };
295
296     t::lib::Mocks::mock_preference( 'AllowHoldDateInFuture', 0 );
297
298     $t->post_ok( "//$userid_3:$password@/api/v1/holds" => json => $post_data )
299       ->status_is(400)
300       ->json_has('/error');
301
302     t::lib::Mocks::mock_preference( 'AllowHoldDateInFuture', 1 );
303
304     $t->post_ok( "//$userid_3:$password@/api/v1/holds" => json => $post_data )
305       ->status_is(201)
306       ->json_is('/hold_date', output_pref({ dt => $future_hold_date, dateformat => 'rfc3339', dateonly => 1 }));
307 };
308
309 subtest 'test AllowHoldPolicyOverride' => sub {
310
311     plan tests => 5;
312
313     $dbh->do('DELETE FROM reserves');
314
315     Koha::CirculationRules->set_rules(
316         {
317             categorycode => undef,
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 };