3 # This file is part of Koha.
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
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.
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.
20 use Test::More tests => 93;
24 use t::lib::TestBuilder;
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 };
62 my $module = new Test::MockModule('C4::Context');
63 $module->mock('userenv', sub { { branch => $branchcode } });
65 $t->get_ok( "//$userid:$password@/api/v1/checkouts?patron_id=$patron_id" )
69 my $notexisting_patron_id = $patron_id + 1;
70 $t->get_ok( "//$userid:$password@/api/v1/checkouts?patron_id=$notexisting_patron_id" )
74 $dbh->do('DELETE FROM issuingrules');
76 INSERT INTO issuingrules (categorycode, branchcode, itemtype, renewalperiod, renewalsallowed, issuelength)
77 VALUES (?, ?, ?, ?, ?, ?)
78 }, {}, '*', '*', '*', 7, 1, 5);
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;
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);
95 $t->get_ok( "//$userid:$password@/api/v1/checkouts?patron_id=$patron_id" )
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 }) )
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" )
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 }) )
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" )
119 ->json_is('/0/patron_id' => $patron_id)
120 ->json_is('/0/item_id' => $item4->itemnumber)
123 $t->get_ok( "//$unauth_userid:$unauth_password@/api/v1/checkouts/" . $issue3->issue_id )
125 ->json_is({ error => "Authorization failure. Missing required permission(s).",
126 required_permissions => { circulate => "circulate_remaining_permissions" }
129 $t->get_ok( "//$userid:$password@/api/v1/checkouts?patron_id=$patron_id")
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 }) )
139 $t->get_ok( "//$userid:$password@/api/v1/checkouts?patron_id=$patron_id&_per_page=1&_page=1")
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 }) )
150 $t->get_ok( "//$userid:$password@/api/v1/checkouts?patron_id=$patron_id&_per_page=1&_page=2")
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 }) )
161 $t->get_ok( "//$userid:$password@/api/v1/checkouts/" . $issue1->issue_id)
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 }) )
168 $t->get_ok( "//$userid:$password@/api/v1/checkouts/" . $issue1->issue_id)
170 ->json_is('/due_date' => output_pref({ dateformat => "rfc3339", dt => $date_due1 }) );
172 $t->get_ok( "//$userid:$password@/api/v1/checkouts/" . $issue2->issue_id)
174 ->json_is('/due_date' => output_pref( { dateformat => "rfc3339", dt => $date_due2 }) );
177 my $expected_datedue = $date_due
178 ->set_time_zone('local')
180 ->set(hour => 23, minute => 59, second => 0);
181 $t->post_ok ( "//$userid:$password@/api/v1/checkouts/" . $issue1->issue_id . "/renewal" )
183 ->json_is('/due_date' => output_pref( { dateformat => "rfc3339", dt => $expected_datedue }) )
184 ->header_is(Location => "/api/v1/checkouts/" . $issue1->issue_id . "/renewal");
186 $t->post_ok( "//$unauth_userid:$unauth_password@/api/v1/checkouts/" . $issue3->issue_id . "/renewal" )
188 ->json_is({ error => "Authorization failure. Missing required permission(s).",
189 required_permissions => { circulate => "circulate_remaining_permissions" }
192 $t->get_ok( "//$userid:$password@/api/v1/checkouts/" . $issue2->issue_id . "/allows_renewal")
195 allows_renewal => Mojo::JSON->true,
197 current_renewals => 0,
201 $t->post_ok( "//$userid:$password@/api/v1/checkouts/" . $issue2->issue_id . "/renewal" )
203 ->json_is('/due_date' => output_pref({ dateformat => "rfc3339", dt => $expected_datedue}) )
204 ->header_is(Location => "/api/v1/checkouts/" . $issue2->issue_id . "/renewal");
207 $t->post_ok( "//$userid:$password@/api/v1/checkouts/" . $issue1->issue_id . "/renewal" )
209 ->json_is({ error => 'Renewal not authorized (too_many)' });
211 $t->get_ok( "//$userid:$password@/api/v1/checkouts/" . $issue2->issue_id . "/allows_renewal")
214 allows_renewal => Mojo::JSON->false,
216 current_renewals => 1,