3 # This file is part of Koha.
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.
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.
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>.
20 use Test::More tests => 99;
24 use t::lib::TestBuilder;
29 use C4::Circulation qw( AddIssue AddReturn );
32 use Koha::DateUtils qw( dt_from_string output_pref );
34 my $schema = Koha::Database->schema;
35 my $builder = t::lib::TestBuilder->new;
37 t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 );
38 my $t = Test::Mojo->new('Koha::REST::V1');
40 $schema->storage->txn_begin;
42 my $dbh = C4::Context->dbh;
44 my $librarian = $builder->build_object({
45 class => 'Koha::Patrons',
46 value => { flags => 2 }
48 my $password = 'thePassword123';
49 $librarian->set_password({ password => $password, skip_validation => 1 });
50 my $userid = $librarian->userid;
52 my $patron = $builder->build_object({
53 class => 'Koha::Patrons',
54 value => { flags => 0 }
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;
61 my $branchcode = $builder->build({ source => 'Branch' })->{ branchcode };
63 $t->get_ok( "//$userid:$password@/api/v1/checkouts?patron_id=$patron_id" )
67 my $notexisting_patron_id = $patron_id + 1;
68 $t->get_ok( "//$userid:$password@/api/v1/checkouts?patron_id=$notexisting_patron_id" )
72 Koha::CirculationRules->set_rules(
74 categorycode => undef,
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;
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);
100 $t->get_ok( "//$userid:$password@/api/v1/checkouts?patron_id=$patron_id" )
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 }) )
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" )
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 }) )
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" )
124 ->json_is('/0/patron_id' => $patron_id)
125 ->json_is('/0/item_id' => $item4->itemnumber)
128 $t->get_ok( "//$unauth_userid:$unauth_password@/api/v1/checkouts/" . $issue3->issue_id )
130 ->json_is({ error => "Authorization failure. Missing required permission(s).",
131 required_permissions => { circulate => "circulate_remaining_permissions" }
134 $t->get_ok( "//$userid:$password@/api/v1/checkouts?patron_id=$patron_id")
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 }) )
144 $t->get_ok( "//$userid:$password@/api/v1/checkouts?patron_id=$patron_id&_per_page=1&_page=1")
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 }) )
155 $t->get_ok( "//$userid:$password@/api/v1/checkouts?patron_id=$patron_id&_per_page=1&_page=2")
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 }) )
166 $t->get_ok( "//$userid:$password@/api/v1/checkouts/" . $issue1->issue_id)
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 }) )
173 $t->get_ok( "//$userid:$password@/api/v1/checkouts/" . $issue1->issue_id)
175 ->json_is('/due_date' => output_pref({ dateformat => "rfc3339", dt => $date_due1 }) );
177 $t->get_ok( "//$userid:$password@/api/v1/checkouts/" . $issue2->issue_id)
179 ->json_is('/due_date' => output_pref( { dateformat => "rfc3339", dt => $date_due2 }) );
181 my $expected_datedue = $date_due
182 ->set_time_zone('local')
184 ->set(hour => 23, minute => 59, second => 0);
186 $t->post_ok ( "//$userid:$password@/api/v1/checkouts/" . $issue1->issue_id . "/renewal" )
188 ->json_is('/due_date' => output_pref( { dateformat => "rfc3339", dt => $expected_datedue }) )
189 ->header_is(Location => "/api/v1/checkouts/" . $issue1->issue_id . "/renewal");
191 my $renewal = $issue1->renewals->last;
192 is( $renewal->renewal_type, 'Manual', 'Manual renewal recorded' );
194 $t->get_ok ( "//$userid:$password@/api/v1/checkouts/" . $issue1->issue_id . "/renewals" )
196 ->json_is('/0/checkout_id' => $issue1->issue_id)
197 ->json_is('/0/interface' => 'api')
198 ->json_is('/0/renewer_id' => $librarian->borrowernumber );
200 $t->post_ok( "//$unauth_userid:$unauth_password@/api/v1/checkouts/" . $issue3->issue_id . "/renewal" )
202 ->json_is({ error => "Authorization failure. Missing required permission(s).",
203 required_permissions => { circulate => "circulate_remaining_permissions" }
206 $t->get_ok( "//$userid:$password@/api/v1/checkouts/" . $issue2->issue_id . "/allows_renewal")
209 allows_renewal => Mojo::JSON->true,
211 unseen_renewals => 0,
212 current_renewals => 0,
216 $t->post_ok( "//$userid:$password@/api/v1/checkouts/" . $issue2->issue_id . "/renewal" )
218 ->json_is('/due_date' => output_pref({ dateformat => "rfc3339", dt => $expected_datedue}) )
219 ->header_is(Location => "/api/v1/checkouts/" . $issue2->issue_id . "/renewal");
222 $t->post_ok( "//$userid:$password@/api/v1/checkouts/" . $issue1->issue_id . "/renewal" )
224 ->json_is({ error => 'Renewal not authorized (too_many)' });
226 $t->get_ok( "//$userid:$password@/api/v1/checkouts/" . $issue2->issue_id . "/allows_renewal")
229 allows_renewal => Mojo::JSON->false,
231 unseen_renewals => 0,
232 current_renewals => 1,