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 => 53;
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 #FIXME - should be removed
45 $dbh->do('DELETE FROM issuingrules');
47 my $librarian = $builder->build_object({
48 class => 'Koha::Patrons',
49 value => { flags => 2 }
51 my $password = 'thePassword123';
52 $librarian->set_password({ password => $password, skip_validation => 1 });
53 my $userid = $librarian->userid;
55 my $patron = $builder->build_object({
56 class => 'Koha::Patrons',
57 value => { flags => 0 }
59 my $unauth_password = 'thePassword000';
60 $patron->set_password({ password => $unauth_password, skip_validattion => 1 });
61 my $unauth_userid = $patron->userid;
62 my $patron_id = $patron->borrowernumber;
64 my $branchcode = $builder->build({ source => 'Branch' })->{ branchcode };
65 my $module = new Test::MockModule('C4::Context');
66 $module->mock('userenv', sub { { branch => $branchcode } });
68 $t->get_ok( "//$userid:$password@/api/v1/checkouts?patron_id=$patron_id" )
72 my $notexisting_patron_id = $patron_id + 1;
73 $t->get_ok( "//$userid:$password@/api/v1/checkouts?patron_id=$notexisting_patron_id" )
77 my $item1 = $builder->build_sample_item;
78 my $item2 = $builder->build_sample_item;
79 my $item3 = $builder->build_sample_item;
81 my $date_due = DateTime->now->add(weeks => 2);
82 my $issue1 = C4::Circulation::AddIssue($patron->unblessed, $item1->barcode, $date_due);
83 my $date_due1 = Koha::DateUtils::dt_from_string( $issue1->date_due );
84 my $issue2 = C4::Circulation::AddIssue($patron->unblessed, $item2->barcode, $date_due);
85 my $date_due2 = Koha::DateUtils::dt_from_string( $issue2->date_due );
86 my $issue3 = C4::Circulation::AddIssue($librarian->unblessed, $item3->barcode, $date_due);
87 my $date_due3 = Koha::DateUtils::dt_from_string( $issue3->date_due );
89 $t->get_ok( "//$userid:$password@/api/v1/checkouts?patron_id=$patron_id" )
91 ->json_is('/0/patron_id' => $patron_id)
92 ->json_is('/0/item_id' => $item1->itemnumber)
93 ->json_is('/0/due_date' => output_pref({ dateformat => "rfc3339", dt => $date_due1 }) )
94 ->json_is('/1/patron_id' => $patron_id)
95 ->json_is('/1/item_id' => $item2->itemnumber)
96 ->json_is('/1/due_date' => output_pref({ dateformat => "rfc3339", dt => $date_due2 }) )
100 $t->get_ok( "//$unauth_userid:$unauth_password@/api/v1/checkouts/" . $issue3->issue_id )
102 ->json_is({ error => "Authorization failure. Missing required permission(s).",
103 required_permissions => { circulate => "circulate_remaining_permissions" }
106 $t->get_ok( "//$userid:$password@/api/v1/checkouts?patron_id=$patron_id")
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 $t->get_ok( "//$userid:$password@/api/v1/checkouts/" . $issue1->issue_id)
118 ->json_is('/patron_id' => $patron_id)
119 ->json_is('/item_id' => $item1->itemnumber)
120 ->json_is('/due_date' => output_pref({ dateformat => "rfc3339", dt => $date_due1 }) )
123 $t->get_ok( "//$userid:$password@/api/v1/checkouts/" . $issue1->issue_id)
125 ->json_is('/due_date' => output_pref({ dateformat => "rfc3339", dt => $date_due1 }) );
127 $t->get_ok( "//$userid:$password@/api/v1/checkouts/" . $issue2->issue_id)
129 ->json_is('/due_date' => output_pref( { dateformat => "rfc3339", dt => $date_due2 }) );
132 $dbh->do('DELETE FROM issuingrules');
134 INSERT INTO issuingrules (categorycode, branchcode, itemtype, renewalperiod, renewalsallowed)
135 VALUES (?, ?, ?, ?, ?)
136 }, {}, '*', '*', '*', 7, 1);
138 my $expected_datedue = DateTime->now->add(days => 14)->set(hour => 23, minute => 59, second => 0);
139 $t->post_ok ( "//$userid:$password@/api/v1/checkouts/" . $issue1->issue_id . "/renewal" )
141 ->json_is('/due_date' => output_pref( { dateformat => "rfc3339", dt => $expected_datedue }) )
142 ->header_is(Location => "/api/v1/checkouts/" . $issue1->issue_id . "/renewal");
144 $t->post_ok( "//$unauth_userid:$unauth_password@/api/v1/checkouts/" . $issue3->issue_id . "/renewal" )
146 ->json_is({ error => "Authorization failure. Missing required permission(s).",
147 required_permissions => { circulate => "circulate_remaining_permissions" }
150 $t->post_ok( "//$userid:$password@/api/v1/checkouts/" . $issue2->issue_id . "/renewal" )
152 ->json_is('/due_date' => output_pref({ dateformat => "rfc3339", dt => $expected_datedue}) )
153 ->header_is(Location => "/api/v1/checkouts/" . $issue2->issue_id . "/renewal");
156 $t->post_ok( "//$userid:$password@/api/v1/checkouts/" . $issue1->issue_id . "/renewal" )
158 ->json_is({ error => 'Renewal not authorized (too_many)' });