Bug 30642: Make renewal_type an enum in spec and add test
[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
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 => 99;
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 qw( AddIssue AddReturn );
30
31 use Koha::Database;
32 use Koha::DateUtils qw( dt_from_string output_pref );
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
63 $t->get_ok( "//$userid:$password@/api/v1/checkouts?patron_id=$patron_id" )
64   ->status_is(200)
65   ->json_is([]);
66
67 my $notexisting_patron_id = $patron_id + 1;
68 $t->get_ok( "//$userid:$password@/api/v1/checkouts?patron_id=$notexisting_patron_id" )
69   ->status_is(200)
70   ->json_is([]);
71
72 Koha::CirculationRules->set_rules(
73     {
74         categorycode => undef,
75         itemtype     => undef,
76         branchcode   => undef,
77         rules        => {
78             renewalperiod => 7,
79             renewalsallowed => 1,
80             issuelength => 5,
81         }
82     }
83 );
84
85 my $item1 = $builder->build_sample_item;
86 my $item2 = $builder->build_sample_item;
87 my $item3 = $builder->build_sample_item;
88 my $item4 = $builder->build_sample_item;
89
90 my $date_due = DateTime->now->add(weeks => 2);
91 my $issue1 = C4::Circulation::AddIssue($patron->unblessed, $item1->barcode, $date_due);
92 my $date_due1 = Koha::DateUtils::dt_from_string( $issue1->date_due );
93 my $issue2 = C4::Circulation::AddIssue($patron->unblessed, $item2->barcode, $date_due);
94 my $date_due2 = Koha::DateUtils::dt_from_string( $issue2->date_due );
95 my $issue3 = C4::Circulation::AddIssue($librarian->unblessed, $item3->barcode, $date_due);
96 my $date_due3 = Koha::DateUtils::dt_from_string( $issue3->date_due );
97 my $issue4 = C4::Circulation::AddIssue($patron->unblessed, $item4->barcode);
98 C4::Circulation::AddReturn($item4->barcode, $branchcode);
99
100 $t->get_ok( "//$userid:$password@/api/v1/checkouts?patron_id=$patron_id" )
101   ->status_is(200)
102   ->json_is('/0/patron_id' => $patron_id)
103   ->json_is('/0/item_id' => $item1->itemnumber)
104   ->json_is('/0/due_date' => output_pref({ dateformat => "rfc3339", dt => $date_due1 }) )
105   ->json_is('/1/patron_id' => $patron_id)
106   ->json_is('/1/item_id' => $item2->itemnumber)
107   ->json_is('/1/due_date' => output_pref({ dateformat => "rfc3339", dt => $date_due2 }) )
108   ->json_hasnt('/2');
109
110 # Test checked_in parameter, zero means, the response is same as without it
111 $t->get_ok( "//$userid:$password@/api/v1/checkouts?patron_id=$patron_id&checked_in=0" )
112   ->status_is(200)
113   ->json_is('/0/patron_id' => $patron_id)
114   ->json_is('/0/item_id' => $item1->itemnumber)
115   ->json_is('/0/due_date' => output_pref({ dateformat => "rfc3339", dt => $date_due1 }) )
116   ->json_is('/1/patron_id' => $patron_id)
117   ->json_is('/1/item_id' => $item2->itemnumber)
118   ->json_is('/1/due_date' => output_pref({ dateformat => "rfc3339", dt => $date_due2 }) )
119   ->json_hasnt('/2');
120
121 # Test checked_in parameter, one measn, the checked in checkout is in the response too
122 $t->get_ok( "//$userid:$password@/api/v1/checkouts?patron_id=$patron_id&checked_in=1" )
123   ->status_is(200)
124   ->json_is('/0/patron_id' => $patron_id)
125   ->json_is('/0/item_id' => $item4->itemnumber)
126   ->json_hasnt('/1');
127
128 $t->get_ok( "//$unauth_userid:$unauth_password@/api/v1/checkouts/" . $issue3->issue_id )
129   ->status_is(403)
130   ->json_is({ error => "Authorization failure. Missing required permission(s).",
131               required_permissions => { circulate => "circulate_remaining_permissions" }
132             });
133
134 $t->get_ok( "//$userid:$password@/api/v1/checkouts?patron_id=$patron_id")
135   ->status_is(200)
136   ->json_is('/0/patron_id' => $patron_id)
137   ->json_is('/0/item_id' => $item1->itemnumber)
138   ->json_is('/0/due_date' => output_pref({ dateformat => "rfc3339", dt => $date_due1 }) )
139   ->json_is('/1/patron_id' => $patron_id)
140   ->json_is('/1/item_id' => $item2->itemnumber)
141   ->json_is('/1/due_date' => output_pref({ dateformat => "rfc3339", dt => $date_due2 }) )
142   ->json_hasnt('/2');
143
144 $t->get_ok( "//$userid:$password@/api/v1/checkouts?patron_id=$patron_id&_per_page=1&_page=1")
145   ->status_is(200)
146   ->header_is('X-Total-Count', '2')
147   ->header_like('Link', qr|rel="next"|)
148   ->header_like('Link', qr|rel="first"|)
149   ->header_like('Link', qr|rel="last"|)
150   ->json_is('/0/patron_id' => $patron_id)
151   ->json_is('/0/item_id' => $item1->itemnumber)
152   ->json_is('/0/due_date' => output_pref({ dateformat => "rfc3339", dt => $date_due1 }) )
153   ->json_hasnt('/1');
154
155 $t->get_ok( "//$userid:$password@/api/v1/checkouts?patron_id=$patron_id&_per_page=1&_page=2")
156   ->status_is(200)
157   ->header_is('X-Total-Count', '2')
158   ->header_like('Link', qr|rel="prev"|)
159   ->header_like('Link', qr|rel="first"|)
160   ->header_like('Link', qr|rel="last"|)
161   ->json_is('/0/patron_id' => $patron_id)
162   ->json_is('/0/item_id' => $item2->itemnumber)
163   ->json_is('/0/due_date' => output_pref({ dateformat => "rfc3339", dt => $date_due2 }) )
164   ->json_hasnt('/1');
165
166 $t->get_ok( "//$userid:$password@/api/v1/checkouts/" . $issue1->issue_id)
167   ->status_is(200)
168   ->json_is('/patron_id' => $patron_id)
169   ->json_is('/item_id' => $item1->itemnumber)
170   ->json_is('/due_date' => output_pref({ dateformat => "rfc3339", dt => $date_due1 }) )
171   ->json_hasnt('/1');
172
173 $t->get_ok( "//$userid:$password@/api/v1/checkouts/" . $issue1->issue_id)
174   ->status_is(200)
175   ->json_is('/due_date' => output_pref({ dateformat => "rfc3339", dt => $date_due1 }) );
176
177 $t->get_ok( "//$userid:$password@/api/v1/checkouts/" . $issue2->issue_id)
178   ->status_is(200)
179   ->json_is('/due_date' => output_pref( { dateformat => "rfc3339", dt => $date_due2 }) );
180
181 my $expected_datedue = $date_due
182     ->set_time_zone('local')
183     ->add(days => 7)
184     ->set(hour => 23, minute => 59, second => 0);
185
186 $t->post_ok ( "//$userid:$password@/api/v1/checkouts/" . $issue1->issue_id . "/renewal" )
187   ->status_is(201)
188   ->json_is('/due_date' => output_pref( { dateformat => "rfc3339", dt => $expected_datedue }) )
189   ->header_is(Location => "/api/v1/checkouts/" . $issue1->issue_id . "/renewal");
190
191 my $renewal = $issue1->renewals->last;
192 is( $renewal->renewal_type, 'Manual', 'Manual renewal recorded' );
193
194 $t->get_ok ( "//$userid:$password@/api/v1/checkouts/" . $issue1->issue_id . "/renewals" )
195   ->status_is(200)
196   ->json_is('/0/checkout_id' => $issue1->issue_id)
197   ->json_is('/0/interface'   => 'api')
198   ->json_is('/0/renewer_id'  => $librarian->borrowernumber );
199
200 $t->post_ok( "//$unauth_userid:$unauth_password@/api/v1/checkouts/" . $issue3->issue_id . "/renewal" )
201   ->status_is(403)
202   ->json_is({ error => "Authorization failure. Missing required permission(s).",
203               required_permissions => { circulate => "circulate_remaining_permissions" }
204             });
205
206 $t->get_ok( "//$userid:$password@/api/v1/checkouts/" . $issue2->issue_id . "/allows_renewal")
207   ->status_is(200)
208   ->json_is({
209         allows_renewal   => Mojo::JSON->true,
210         max_renewals     => 1,
211         unseen_renewals  => 0,
212         current_renewals => 0,
213         error            => undef
214     });
215
216 $t->post_ok( "//$userid:$password@/api/v1/checkouts/" . $issue2->issue_id . "/renewal" )
217   ->status_is(201)
218   ->json_is('/due_date' => output_pref({ dateformat => "rfc3339", dt => $expected_datedue}) )
219   ->header_is(Location => "/api/v1/checkouts/" . $issue2->issue_id . "/renewal");
220
221
222 $t->post_ok( "//$userid:$password@/api/v1/checkouts/" . $issue1->issue_id . "/renewal" )
223   ->status_is(403)
224   ->json_is({ error => 'Renewal not authorized (too_many)' });
225
226 $t->get_ok( "//$userid:$password@/api/v1/checkouts/" . $issue2->issue_id . "/allows_renewal")
227   ->status_is(200)
228   ->json_is({
229         allows_renewal   => Mojo::JSON->false,
230         max_renewals     => 1,
231         unseen_renewals  => 0,
232         current_renewals => 1,
233         error            => 'too_many'
234     });