Bug 18330: (follow-up) Adapt holds.t to match the new datetime format
[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 => 4;
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 C4::Reserves;
29
30 use Koha::Database;
31 use Koha::DateUtils;
32 use Koha::Biblios;
33 use Koha::Biblioitems;
34 use Koha::Items;
35 use Koha::Patrons;
36
37 my $schema  = Koha::Database->new->schema;
38 my $builder = t::lib::TestBuilder->new();
39
40 $schema->storage->txn_begin;
41
42 # FIXME: sessionStorage defaults to mysql, but it seems to break transaction handling
43 # this affects the other REST api tests
44 t::lib::Mocks::mock_preference( 'SessionStorage', 'tmp' );
45
46 $ENV{REMOTE_ADDR} = '127.0.0.1';
47 my $t = Test::Mojo->new('Koha::REST::V1');
48 my $tx;
49
50 my $categorycode = $builder->build({ source => 'Category' })->{categorycode};
51 my $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
52
53 # User without any permissions
54 my $nopermission = $builder->build({
55     source => 'Borrower',
56     value => {
57         branchcode   => $branchcode,
58         categorycode => $categorycode,
59         flags        => 0
60     }
61 });
62 my $session_nopermission = C4::Auth::get_session('');
63 $session_nopermission->param('number', $nopermission->{ borrowernumber });
64 $session_nopermission->param('id', $nopermission->{ userid });
65 $session_nopermission->param('ip', '127.0.0.1');
66 $session_nopermission->param('lasttime', time());
67 $session_nopermission->flush;
68
69 my $patron_1 = $builder->build_object(
70     {
71         class => 'Koha::Patrons',
72         value => {
73             categorycode => $categorycode,
74             branchcode   => $branchcode,
75             surname      => 'Test Surname',
76             flags        => 80, #borrowers and reserveforothers flags
77         }
78     }
79 );
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
93 my $patron_3 = $builder->build_object(
94     {
95         class => 'Koha::Patrons',
96         value => {
97             categorycode => $categorycode,
98             branchcode   => $branchcode,
99             surname      => 'Test Surname 3',
100             flags        => 64, # reserveforothers flag
101         }
102     }
103 );
104
105 # Get sessions
106 my $session = C4::Auth::get_session('');
107 $session->param('number', $patron_1->borrowernumber);
108 $session->param('id', $patron_1->userid);
109 $session->param('ip', '127.0.0.1');
110 $session->param('lasttime', time());
111 $session->flush;
112 my $session2 = C4::Auth::get_session('');
113 $session2->param('number', $patron_2->borrowernumber);
114 $session2->param('id', $patron_2->userid);
115 $session2->param('ip', '127.0.0.1');
116 $session2->param('lasttime', time());
117 $session2->flush;
118 my $session3 = C4::Auth::get_session('');
119 $session3->param('number', $patron_3->borrowernumber);
120 $session3->param('id', $patron_3->userid);
121 $session3->param('ip', '127.0.0.1');
122 $session3->param('lasttime', time());
123 $session3->flush;
124
125 my $biblionumber = create_biblio('RESTful Web APIs');
126 my $itemnumber = create_item($biblionumber, 'TEST000001');
127
128 my $biblionumber2 = create_biblio('RESTful Web APIs');
129 my $itemnumber2 = create_item($biblionumber2, 'TEST000002');
130
131 my $dbh = C4::Context->dbh;
132 $dbh->do('DELETE FROM reserves');
133 $dbh->do('DELETE FROM issuingrules');
134     $dbh->do(q{
135         INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed)
136         VALUES (?, ?, ?, ?)
137     }, {}, '*', '*', '*', 1);
138
139 my $reserve_id = C4::Reserves::AddReserve($branchcode, $patron_1->borrowernumber,
140     $biblionumber, undef, 1, undef, undef, undef, '', $itemnumber);
141
142 # Add another reserve to be able to change first reserve's rank
143 my $reserve_id2 = C4::Reserves::AddReserve($branchcode, $patron_2->borrowernumber,
144     $biblionumber, undef, 2, undef, undef, undef, '', $itemnumber);
145
146 my $suspend_until = DateTime->now->add(days => 10)->ymd;
147 my $expirationdate = DateTime->now->add(days => 10)->ymd;
148
149 my $post_data = {
150     borrowernumber => int($patron_1->borrowernumber),
151     biblionumber => int($biblionumber),
152     itemnumber => int($itemnumber),
153     branchcode => $branchcode,
154     expirationdate => $expirationdate,
155 };
156 my $put_data = {
157     priority => 2,
158     suspend_until => $suspend_until,
159 };
160
161 subtest "Test endpoints without authentication" => sub {
162     plan tests => 8;
163     $t->get_ok('/api/v1/holds')
164       ->status_is(401);
165     $t->post_ok('/api/v1/holds')
166       ->status_is(401);
167     $t->put_ok('/api/v1/holds/0')
168       ->status_is(401);
169     $t->delete_ok('/api/v1/holds/0')
170       ->status_is(401);
171 };
172
173
174 subtest "Test endpoints without permission" => sub {
175     plan tests => 10;
176
177     $tx = $t->ua->build_tx(GET => "/api/v1/holds?borrowernumber=" . $patron_1->borrowernumber);
178     $tx->req->cookies({name => 'CGISESSID', value => $session_nopermission->id});
179     $t->request_ok($tx) # no permission
180       ->status_is(403);
181     $tx = $t->ua->build_tx(GET => "/api/v1/holds?borrowernumber=" . $patron_1->borrowernumber);
182     $tx->req->cookies({name => 'CGISESSID', value => $session3->id});
183     $t->request_ok($tx) # reserveforothers permission
184       ->status_is(403);
185     $tx = $t->ua->build_tx(POST => "/api/v1/holds" => json => $post_data );
186     $tx->req->cookies({name => 'CGISESSID', value => $session_nopermission->id});
187     $t->request_ok($tx) # no permission
188       ->status_is(403);
189     $tx = $t->ua->build_tx(PUT => "/api/v1/holds/0" => json => $put_data );
190     $tx->req->cookies({name => 'CGISESSID', value => $session_nopermission->id});
191     $t->request_ok($tx) # no permission
192       ->status_is(403);
193     $tx = $t->ua->build_tx(DELETE => "/api/v1/holds/0");
194     $tx->req->cookies({name => 'CGISESSID', value => $session_nopermission->id});
195     $t->request_ok($tx) # no permission
196       ->status_is(403);
197 };
198 subtest "Test endpoints without permission, but accessing own object" => sub {
199     plan tests => 15;
200
201     my $borrno_tmp = $post_data->{'borrowernumber'};
202     $post_data->{'borrowernumber'} = int $nopermission->{'borrowernumber'};
203     $tx = $t->ua->build_tx(POST => "/api/v1/holds" => json => $post_data);
204     $tx->req->cookies({name => 'CGISESSID', value => $session_nopermission->id});
205     $t->request_ok($tx) # create hold to myself
206       ->status_is(201)
207       ->json_has('/reserve_id');
208
209     $post_data->{'borrowernumber'} = $borrno_tmp;
210     $tx = $t->ua->build_tx(GET => "/api/v1/holds?borrowernumber=".$nopermission-> { borrowernumber });
211     $tx->req->cookies({name => 'CGISESSID', value => $session_nopermission->id});
212     $t->request_ok($tx) # get my own holds
213       ->status_is(200)
214       ->json_is('/0/borrowernumber', $nopermission->{ borrowernumber })
215       ->json_is('/0/biblionumber', $biblionumber)
216       ->json_is('/0/itemnumber', $itemnumber)
217       ->json_is('/0/expirationdate', $expirationdate)
218       ->json_is('/0/branchcode', $branchcode);
219
220     my $reserve_id3 = Koha::Holds->find({ borrowernumber => $nopermission->{borrowernumber} })->reserve_id;
221     $tx = $t->ua->build_tx(PUT => "/api/v1/holds/$reserve_id3" => json => $put_data);
222     $tx->req->cookies({name => 'CGISESSID', value => $session_nopermission->id});
223     $t->request_ok($tx)    # create hold to myself
224       ->status_is(200)->json_is( '/reserve_id', $reserve_id3 )->json_is(
225         '/suspend_until',
226         output_pref(
227             {
228                 dateformat => 'rfc3339',
229                 dt => dt_from_string( $suspend_until . ' 00:00:00', 'sql' )
230             }
231         )
232       )
233       ->json_is( '/priority', 2 );
234 };
235
236 subtest "Test endpoints with permission" => sub {
237     plan tests => 45;
238
239     $tx = $t->ua->build_tx(GET => '/api/v1/holds');
240     $tx->req->cookies({name => 'CGISESSID', value => $session->id});
241     $t->request_ok($tx)
242       ->status_is(200)
243       ->json_has('/0')
244       ->json_has('/1')
245       ->json_has('/2')
246       ->json_hasnt('/3');
247
248     $tx = $t->ua->build_tx(GET => '/api/v1/holds?priority=2');
249     $tx->req->cookies({name => 'CGISESSID', value => $session->id});
250     $t->request_ok($tx)
251       ->status_is(200)
252       ->json_is('/0/borrowernumber', $nopermission->{borrowernumber})
253       ->json_hasnt('/1');
254
255     $tx = $t->ua->build_tx(PUT => "/api/v1/holds/$reserve_id" => json => $put_data);
256     $tx->req->cookies({name => 'CGISESSID', value => $session3->id});
257     $t->request_ok($tx)->status_is(200)->json_is( '/reserve_id', $reserve_id )
258       ->json_is(
259         '/suspend_until',
260         output_pref(
261             {
262                 dateformat => 'rfc3339',
263                 dt => dt_from_string( $suspend_until . ' 00:00:00', 'sql' )
264             }
265         )
266       )
267       ->json_is( '/priority', 2 );
268
269     $tx = $t->ua->build_tx(DELETE => "/api/v1/holds/$reserve_id");
270     $tx->req->cookies({name => 'CGISESSID', value => $session3->id});
271     $t->request_ok($tx)
272       ->status_is(200);
273
274     $tx = $t->ua->build_tx(PUT => "/api/v1/holds/$reserve_id" => json => $put_data);
275     $tx->req->cookies({name => 'CGISESSID', value => $session3->id});
276     $t->request_ok($tx)
277       ->status_is(404)
278       ->json_has('/error');
279
280     $tx = $t->ua->build_tx(DELETE => "/api/v1/holds/$reserve_id");
281     $tx->req->cookies({name => 'CGISESSID', value => $session3->id});
282     $t->request_ok($tx)
283       ->status_is(404)
284       ->json_has('/error');
285
286     $tx = $t->ua->build_tx(GET => "/api/v1/holds?borrowernumber=" . $patron_1->borrowernumber);
287     $tx->req->cookies({name => 'CGISESSID', value => $session2->id}); # get with borrowers flag
288     $t->request_ok($tx)
289       ->status_is(200)
290       ->json_is([]);
291
292     my $inexisting_borrowernumber = $patron_2->borrowernumber * 2;
293     $tx = $t->ua->build_tx(GET => "/api/v1/holds?borrowernumber=$inexisting_borrowernumber");
294     $tx->req->cookies({name => 'CGISESSID', value => $session->id});
295     $t->request_ok($tx)
296       ->status_is(200)
297       ->json_is([]);
298
299     $tx = $t->ua->build_tx(DELETE => "/api/v1/holds/$reserve_id2");
300     $tx->req->cookies({name => 'CGISESSID', value => $session3->id});
301     $t->request_ok($tx)
302       ->status_is(200);
303
304     $tx = $t->ua->build_tx(POST => "/api/v1/holds" => json => $post_data);
305     $tx->req->cookies({name => 'CGISESSID', value => $session3->id});
306     $t->request_ok($tx)
307       ->status_is(201)
308       ->json_has('/reserve_id');
309     $reserve_id = $t->tx->res->json->{reserve_id};
310
311     $tx = $t->ua->build_tx(GET => "/api/v1/holds?borrowernumber=" . $patron_1->borrowernumber);
312     $tx->req->cookies({name => 'CGISESSID', value => $session->id});
313     $t->request_ok($tx)
314       ->status_is(200)
315       ->json_is('/0/reserve_id', $reserve_id)
316       ->json_is('/0/expirationdate', $expirationdate)
317       ->json_is('/0/branchcode', $branchcode);
318
319     $tx = $t->ua->build_tx(POST => "/api/v1/holds" => json => $post_data);
320     $tx->req->cookies({name => 'CGISESSID', value => $session3->id});
321     $t->request_ok($tx)
322       ->status_is(403)
323       ->json_like('/error', qr/itemAlreadyOnHold/);
324
325     $post_data->{biblionumber} = int($biblionumber2);
326     $post_data->{itemnumber} = int($itemnumber2);
327     $tx = $t->ua->build_tx(POST => "/api/v1/holds" => json => $post_data);
328     $tx->req->cookies({name => 'CGISESSID', value => $session3->id});
329     $t->request_ok($tx)
330       ->status_is(403)
331       ->json_like('/error', qr/tooManyReserves/);
332 };
333
334 $schema->storage->txn_rollback;
335
336 sub create_biblio {
337     my ($title) = @_;
338
339     my $biblio = Koha::Biblio->new( { title => $title } )->store;
340     my $biblioitem = Koha::Biblioitem->new({biblionumber => $biblio->biblionumber})->store;
341
342     return $biblio->biblionumber;
343 }
344
345 sub create_item {
346     my ( $biblionumber, $barcode ) = @_;
347
348     Koha::Items->search( { barcode => $barcode } )->delete;
349     my $builder = t::lib::TestBuilder->new;
350     my $item    = $builder->build(
351         {
352             source => 'Item',
353             value  => {
354                 biblionumber     => $biblionumber,
355                 barcode          => $barcode,
356             }
357         }
358     );
359
360     return $item->{itemnumber};
361 }