Bug 24543: Fix wrong test in api/v1/checkouts.t
[koha.git] / t / db_dependent / api / v1 / checkouts.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 => 93;
21 use Test::MockModule;
22 use Test::Mojo;
23 use t::lib::Mocks;
24 use t::lib::TestBuilder;
25
26 use DateTime;
27
28 use C4::Context;
29 use C4::Circulation;
30
31 use Koha::Database;
32 use Koha::DateUtils;
33
34 my $schema = Koha::Database->schema;
35 my $builder = t::lib::TestBuilder->new;
36
37 t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 );
38 my $t = Test::Mojo->new('Koha::REST::V1');
39
40 $schema->storage->txn_begin;
41
42 my $dbh = C4::Context->dbh;
43
44 my $librarian = $builder->build_object({
45     class => 'Koha::Patrons',
46     value => { flags => 2 }
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     class => 'Koha::Patrons',
54     value => { flags => 0 }
55 });
56 my $unauth_password = 'thePassword000';
57 $patron->set_password({ password => $unauth_password, skip_validattion => 1 });
58 my $unauth_userid = $patron->userid;
59 my $patron_id = $patron->borrowernumber;
60
61 my $branchcode = $builder->build({ source => 'Branch' })->{ branchcode };
62 my $module = new Test::MockModule('C4::Context');
63 $module->mock('userenv', sub { { branch => $branchcode } });
64
65 $t->get_ok( "//$userid:$password@/api/v1/checkouts?patron_id=$patron_id" )
66   ->status_is(200)
67   ->json_is([]);
68
69 my $notexisting_patron_id = $patron_id + 1;
70 $t->get_ok( "//$userid:$password@/api/v1/checkouts?patron_id=$notexisting_patron_id" )
71   ->status_is(200)
72   ->json_is([]);
73
74 $dbh->do('DELETE FROM issuingrules');
75 $dbh->do(q{
76     INSERT INTO issuingrules (categorycode, branchcode, itemtype, renewalperiod, renewalsallowed, issuelength)
77     VALUES (?, ?, ?, ?, ?, ?)
78 }, {}, '*', '*', '*', 7, 1, 5);
79
80 my $item1 = $builder->build_sample_item;
81 my $item2 = $builder->build_sample_item;
82 my $item3 = $builder->build_sample_item;
83 my $item4 = $builder->build_sample_item;
84
85 my $date_due = DateTime->now->add(weeks => 2);
86 my $issue1 = C4::Circulation::AddIssue($patron->unblessed, $item1->barcode, $date_due);
87 my $date_due1 = Koha::DateUtils::dt_from_string( $issue1->date_due );
88 my $issue2 = C4::Circulation::AddIssue($patron->unblessed, $item2->barcode, $date_due);
89 my $date_due2 = Koha::DateUtils::dt_from_string( $issue2->date_due );
90 my $issue3 = C4::Circulation::AddIssue($librarian->unblessed, $item3->barcode, $date_due);
91 my $date_due3 = Koha::DateUtils::dt_from_string( $issue3->date_due );
92 my $issue4 = C4::Circulation::AddIssue($patron->unblessed, $item4->barcode);
93 C4::Circulation::AddReturn($item4->barcode, $branchcode);
94
95 $t->get_ok( "//$userid:$password@/api/v1/checkouts?patron_id=$patron_id" )
96   ->status_is(200)
97   ->json_is('/0/patron_id' => $patron_id)
98   ->json_is('/0/item_id' => $item1->itemnumber)
99   ->json_is('/0/due_date' => output_pref({ dateformat => "rfc3339", dt => $date_due1 }) )
100   ->json_is('/1/patron_id' => $patron_id)
101   ->json_is('/1/item_id' => $item2->itemnumber)
102   ->json_is('/1/due_date' => output_pref({ dateformat => "rfc3339", dt => $date_due2 }) )
103   ->json_hasnt('/2');
104
105 # Test checked_in parameter, zero means, the response is same as without it
106 $t->get_ok( "//$userid:$password@/api/v1/checkouts?patron_id=$patron_id&checked_in=0" )
107   ->status_is(200)
108   ->json_is('/0/patron_id' => $patron_id)
109   ->json_is('/0/item_id' => $item1->itemnumber)
110   ->json_is('/0/due_date' => output_pref({ dateformat => "rfc3339", dt => $date_due1 }) )
111   ->json_is('/1/patron_id' => $patron_id)
112   ->json_is('/1/item_id' => $item2->itemnumber)
113   ->json_is('/1/due_date' => output_pref({ dateformat => "rfc3339", dt => $date_due2 }) )
114   ->json_hasnt('/2');
115
116 # Test checked_in parameter, one measn, the checked in checkout is in the response too
117 $t->get_ok( "//$userid:$password@/api/v1/checkouts?patron_id=$patron_id&checked_in=1" )
118   ->status_is(200)
119   ->json_is('/0/patron_id' => $patron_id)
120   ->json_is('/0/item_id' => $item4->itemnumber)
121   ->json_hasnt('/1');
122
123 $t->get_ok( "//$unauth_userid:$unauth_password@/api/v1/checkouts/" . $issue3->issue_id )
124   ->status_is(403)
125   ->json_is({ error => "Authorization failure. Missing required permission(s).",
126               required_permissions => { circulate => "circulate_remaining_permissions" }
127             });
128
129 $t->get_ok( "//$userid:$password@/api/v1/checkouts?patron_id=$patron_id")
130   ->status_is(200)
131   ->json_is('/0/patron_id' => $patron_id)
132   ->json_is('/0/item_id' => $item1->itemnumber)
133   ->json_is('/0/due_date' => output_pref({ dateformat => "rfc3339", dt => $date_due1 }) )
134   ->json_is('/1/patron_id' => $patron_id)
135   ->json_is('/1/item_id' => $item2->itemnumber)
136   ->json_is('/1/due_date' => output_pref({ dateformat => "rfc3339", dt => $date_due2 }) )
137   ->json_hasnt('/2');
138
139 $t->get_ok( "//$userid:$password@/api/v1/checkouts?patron_id=$patron_id&_per_page=1&_page=1")
140   ->status_is(200)
141   ->header_is('X-Total-Count', '2')
142   ->header_like('Link', qr|rel="next"|)
143   ->header_like('Link', qr|rel="first"|)
144   ->header_like('Link', qr|rel="last"|)
145   ->json_is('/0/patron_id' => $patron_id)
146   ->json_is('/0/item_id' => $item1->itemnumber)
147   ->json_is('/0/due_date' => output_pref({ dateformat => "rfc3339", dt => $date_due1 }) )
148   ->json_hasnt('/1');
149
150 $t->get_ok( "//$userid:$password@/api/v1/checkouts?patron_id=$patron_id&_per_page=1&_page=2")
151   ->status_is(200)
152   ->header_is('X-Total-Count', '2')
153   ->header_like('Link', qr|rel="prev"|)
154   ->header_like('Link', qr|rel="first"|)
155   ->header_like('Link', qr|rel="last"|)
156   ->json_is('/0/patron_id' => $patron_id)
157   ->json_is('/0/item_id' => $item2->itemnumber)
158   ->json_is('/0/due_date' => output_pref({ dateformat => "rfc3339", dt => $date_due2 }) )
159   ->json_hasnt('/1');
160
161 $t->get_ok( "//$userid:$password@/api/v1/checkouts/" . $issue1->issue_id)
162   ->status_is(200)
163   ->json_is('/patron_id' => $patron_id)
164   ->json_is('/item_id' => $item1->itemnumber)
165   ->json_is('/due_date' => output_pref({ dateformat => "rfc3339", dt => $date_due1 }) )
166   ->json_hasnt('/1');
167
168 $t->get_ok( "//$userid:$password@/api/v1/checkouts/" . $issue1->issue_id)
169   ->status_is(200)
170   ->json_is('/due_date' => output_pref({ dateformat => "rfc3339", dt => $date_due1 }) );
171
172 $t->get_ok( "//$userid:$password@/api/v1/checkouts/" . $issue2->issue_id)
173   ->status_is(200)
174   ->json_is('/due_date' => output_pref( { dateformat => "rfc3339", dt => $date_due2 }) );
175
176
177 my $expected_datedue = $date_due
178     ->set_time_zone('local')
179     ->add(days => 7)
180     ->set(hour => 23, minute => 59, second => 0);
181 $t->post_ok ( "//$userid:$password@/api/v1/checkouts/" . $issue1->issue_id . "/renewal" )
182   ->status_is(201)
183   ->json_is('/due_date' => output_pref( { dateformat => "rfc3339", dt => $expected_datedue }) )
184   ->header_is(Location => "/api/v1/checkouts/" . $issue1->issue_id . "/renewal");
185
186 $t->post_ok( "//$unauth_userid:$unauth_password@/api/v1/checkouts/" . $issue3->issue_id . "/renewal" )
187   ->status_is(403)
188   ->json_is({ error => "Authorization failure. Missing required permission(s).",
189               required_permissions => { circulate => "circulate_remaining_permissions" }
190             });
191
192 $t->get_ok( "//$userid:$password@/api/v1/checkouts/" . $issue2->issue_id . "/allows_renewal")
193   ->status_is(200)
194   ->json_is({
195         allows_renewal   => Mojo::JSON->true,
196         max_renewals     => 1,
197         current_renewals => 0,
198         error            => undef
199     });
200
201 $t->post_ok( "//$userid:$password@/api/v1/checkouts/" . $issue2->issue_id . "/renewal" )
202   ->status_is(201)
203   ->json_is('/due_date' => output_pref({ dateformat => "rfc3339", dt => $expected_datedue}) )
204   ->header_is(Location => "/api/v1/checkouts/" . $issue2->issue_id . "/renewal");
205
206
207 $t->post_ok( "//$userid:$password@/api/v1/checkouts/" . $issue1->issue_id . "/renewal" )
208   ->status_is(403)
209   ->json_is({ error => 'Renewal not authorized (too_many)' });
210
211 $t->get_ok( "//$userid:$password@/api/v1/checkouts/" . $issue2->issue_id . "/allows_renewal")
212   ->status_is(200)
213   ->json_is({
214         allows_renewal   => Mojo::JSON->false,
215         max_renewals     => 1,
216         current_renewals => 1,
217         error            => 'too_many'
218     });