Bug 23517: Add a spec for PUT /holds/{hold_id}/priority
[koha.git] / t / db_dependent / api / v1 / checkouts.t
blob89e16fdb50486b8bc49d3ee7570c7dd0c67e8094
1 #!/usr/bin/env perl
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
8 # version.
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.
18 use Modern::Perl;
20 use Test::More tests => 79;
21 use Test::MockModule;
22 use Test::Mojo;
23 use t::lib::Mocks;
24 use t::lib::TestBuilder;
26 use DateTime;
28 use C4::Context;
29 use C4::Circulation;
31 use Koha::Database;
32 use Koha::DateUtils;
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 }
47 });
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 }
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;
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" )
66 ->status_is(200)
67 ->json_is([]);
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([]);
74 my $item1 = $builder->build_sample_item;
75 my $item2 = $builder->build_sample_item;
76 my $item3 = $builder->build_sample_item;
78 my $date_due = DateTime->now->add(weeks => 2);
79 my $issue1 = C4::Circulation::AddIssue($patron->unblessed, $item1->barcode, $date_due);
80 my $date_due1 = Koha::DateUtils::dt_from_string( $issue1->date_due );
81 my $issue2 = C4::Circulation::AddIssue($patron->unblessed, $item2->barcode, $date_due);
82 my $date_due2 = Koha::DateUtils::dt_from_string( $issue2->date_due );
83 my $issue3 = C4::Circulation::AddIssue($librarian->unblessed, $item3->barcode, $date_due);
84 my $date_due3 = Koha::DateUtils::dt_from_string( $issue3->date_due );
86 $t->get_ok( "//$userid:$password@/api/v1/checkouts?patron_id=$patron_id" )
87 ->status_is(200)
88 ->json_is('/0/patron_id' => $patron_id)
89 ->json_is('/0/item_id' => $item1->itemnumber)
90 ->json_is('/0/due_date' => output_pref({ dateformat => "rfc3339", dt => $date_due1 }) )
91 ->json_is('/1/patron_id' => $patron_id)
92 ->json_is('/1/item_id' => $item2->itemnumber)
93 ->json_is('/1/due_date' => output_pref({ dateformat => "rfc3339", dt => $date_due2 }) )
94 ->json_hasnt('/2');
97 $t->get_ok( "//$unauth_userid:$unauth_password@/api/v1/checkouts/" . $issue3->issue_id )
98 ->status_is(403)
99 ->json_is({ error => "Authorization failure. Missing required permission(s).",
100 required_permissions => { circulate => "circulate_remaining_permissions" }
103 $t->get_ok( "//$userid:$password@/api/v1/checkouts?patron_id=$patron_id")
104 ->status_is(200)
105 ->json_is('/0/patron_id' => $patron_id)
106 ->json_is('/0/item_id' => $item1->itemnumber)
107 ->json_is('/0/due_date' => output_pref({ dateformat => "rfc3339", dt => $date_due1 }) )
108 ->json_is('/1/patron_id' => $patron_id)
109 ->json_is('/1/item_id' => $item2->itemnumber)
110 ->json_is('/1/due_date' => output_pref({ dateformat => "rfc3339", dt => $date_due2 }) )
111 ->json_hasnt('/2');
113 $t->get_ok( "//$userid:$password@/api/v1/checkouts?patron_id=$patron_id&_per_page=1&_page=1")
114 ->status_is(200)
115 ->header_is('X-Total-Count', '2')
116 ->header_like('Link', qr|rel="next"|)
117 ->header_like('Link', qr|rel="first"|)
118 ->header_like('Link', qr|rel="last"|)
119 ->json_is('/0/patron_id' => $patron_id)
120 ->json_is('/0/item_id' => $item1->itemnumber)
121 ->json_is('/0/due_date' => output_pref({ dateformat => "rfc3339", dt => $date_due1 }) )
122 ->json_hasnt('/1');
124 $t->get_ok( "//$userid:$password@/api/v1/checkouts?patron_id=$patron_id&_per_page=1&_page=2")
125 ->status_is(200)
126 ->header_is('X-Total-Count', '2')
127 ->header_like('Link', qr|rel="prev"|)
128 ->header_like('Link', qr|rel="first"|)
129 ->header_like('Link', qr|rel="last"|)
130 ->json_is('/0/patron_id' => $patron_id)
131 ->json_is('/0/item_id' => $item2->itemnumber)
132 ->json_is('/0/due_date' => output_pref({ dateformat => "rfc3339", dt => $date_due2 }) )
133 ->json_hasnt('/1');
135 $t->get_ok( "//$userid:$password@/api/v1/checkouts/" . $issue1->issue_id)
136 ->status_is(200)
137 ->json_is('/patron_id' => $patron_id)
138 ->json_is('/item_id' => $item1->itemnumber)
139 ->json_is('/due_date' => output_pref({ dateformat => "rfc3339", dt => $date_due1 }) )
140 ->json_hasnt('/1');
142 $t->get_ok( "//$userid:$password@/api/v1/checkouts/" . $issue1->issue_id)
143 ->status_is(200)
144 ->json_is('/due_date' => output_pref({ dateformat => "rfc3339", dt => $date_due1 }) );
146 $t->get_ok( "//$userid:$password@/api/v1/checkouts/" . $issue2->issue_id)
147 ->status_is(200)
148 ->json_is('/due_date' => output_pref( { dateformat => "rfc3339", dt => $date_due2 }) );
151 $dbh->do('DELETE FROM issuingrules');
152 $dbh->do(q{
153 INSERT INTO issuingrules (categorycode, branchcode, itemtype, renewalperiod, renewalsallowed)
154 VALUES (?, ?, ?, ?, ?)
155 }, {}, '*', '*', '*', 7, 1);
157 my $expected_datedue = DateTime->now
158 ->set_time_zone('local')
159 ->add(days => 14)
160 ->set(hour => 23, minute => 59, second => 0);
161 $t->post_ok ( "//$userid:$password@/api/v1/checkouts/" . $issue1->issue_id . "/renewal" )
162 ->status_is(201)
163 ->json_is('/due_date' => output_pref( { dateformat => "rfc3339", dt => $expected_datedue }) )
164 ->header_is(Location => "/api/v1/checkouts/" . $issue1->issue_id . "/renewal");
166 $t->post_ok( "//$unauth_userid:$unauth_password@/api/v1/checkouts/" . $issue3->issue_id . "/renewal" )
167 ->status_is(403)
168 ->json_is({ error => "Authorization failure. Missing required permission(s).",
169 required_permissions => { circulate => "circulate_remaining_permissions" }
172 $t->get_ok( "//$userid:$password@/api/v1/checkouts/" . $issue2->issue_id . "/allows_renewal")
173 ->status_is(200)
174 ->json_is({
175 allows_renewal => Mojo::JSON->true,
176 max_renewals => 1,
177 current_renewals => 0,
178 error => undef
181 $t->post_ok( "//$userid:$password@/api/v1/checkouts/" . $issue2->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/" . $issue2->issue_id . "/renewal");
187 $t->post_ok( "//$userid:$password@/api/v1/checkouts/" . $issue1->issue_id . "/renewal" )
188 ->status_is(403)
189 ->json_is({ error => 'Renewal not authorized (too_many)' });
191 $t->get_ok( "//$userid:$password@/api/v1/checkouts/" . $issue2->issue_id . "/allows_renewal")
192 ->status_is(200)
193 ->json_is({
194 allows_renewal => Mojo::JSON->false,
195 max_renewals => 1,
196 current_renewals => 1,
197 error => 'too_many'