Bug 13895: (follow-up) Fix POST response
[koha-equinox.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 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.
9 #
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.
13 #
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.
17
18 use Modern::Perl;
19
20 use Test::More tests => 53;
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;
30
31 use Koha::Database;
32 use Koha::DateUtils;
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 #FIXME - should be removed
45 $dbh->do('DELETE FROM issuingrules');
46
47 my $librarian = $builder->build_object({
48     class => 'Koha::Patrons',
49     value => { flags => 2 }
50 });
51 my $password = 'thePassword123';
52 $librarian->set_password({ password => $password, skip_validation => 1 });
53 my $userid = $librarian->userid;
54
55 my $patron = $builder->build_object({
56     class => 'Koha::Patrons',
57     value => { flags => 0 }
58 });
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;
63
64 my $branchcode = $builder->build({ source => 'Branch' })->{ branchcode };
65 my $module = new Test::MockModule('C4::Context');
66 $module->mock('userenv', sub { { branch => $branchcode } });
67
68 $t->get_ok( "//$userid:$password@/api/v1/checkouts?patron_id=$patron_id" )
69   ->status_is(200)
70   ->json_is([]);
71
72 my $notexisting_patron_id = $patron_id + 1;
73 $t->get_ok( "//$userid:$password@/api/v1/checkouts?patron_id=$notexisting_patron_id" )
74   ->status_is(200)
75   ->json_is([]);
76
77 my $item1 = $builder->build_sample_item;
78 my $item2 = $builder->build_sample_item;
79 my $item3 = $builder->build_sample_item;
80
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 );
88
89 $t->get_ok( "//$userid:$password@/api/v1/checkouts?patron_id=$patron_id" )
90   ->status_is(200)
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 }) )
97   ->json_hasnt('/2');
98
99
100 $t->get_ok( "//$unauth_userid:$unauth_password@/api/v1/checkouts/" . $issue3->issue_id )
101   ->status_is(403)
102   ->json_is({ error => "Authorization failure. Missing required permission(s).",
103               required_permissions => { circulate => "circulate_remaining_permissions" }
104             });
105
106 $t->get_ok( "//$userid:$password@/api/v1/checkouts?patron_id=$patron_id")
107   ->status_is(200)
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 }) )
114   ->json_hasnt('/2');
115
116 $t->get_ok( "//$userid:$password@/api/v1/checkouts/" . $issue1->issue_id)
117   ->status_is(200)
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 }) )
121   ->json_hasnt('/1');
122
123 $t->get_ok( "//$userid:$password@/api/v1/checkouts/" . $issue1->issue_id)
124   ->status_is(200)
125   ->json_is('/due_date' => output_pref({ dateformat => "rfc3339", dt => $date_due1 }) );
126
127 $t->get_ok( "//$userid:$password@/api/v1/checkouts/" . $issue2->issue_id)
128   ->status_is(200)
129   ->json_is('/due_date' => output_pref( { dateformat => "rfc3339", dt => $date_due2 }) );
130
131
132 $dbh->do('DELETE FROM issuingrules');
133 $dbh->do(q{
134     INSERT INTO issuingrules (categorycode, branchcode, itemtype, renewalperiod, renewalsallowed)
135     VALUES (?, ?, ?, ?, ?)
136 }, {}, '*', '*', '*', 7, 1);
137
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" )
140   ->status_is(201)
141   ->json_is('/due_date' => output_pref( { dateformat => "rfc3339", dt => $expected_datedue }) )
142   ->header_is(Location => "/api/v1/checkouts/" . $issue1->issue_id . "/renewal");
143
144 $t->post_ok( "//$unauth_userid:$unauth_password@/api/v1/checkouts/" . $issue3->issue_id . "/renewal" )
145   ->status_is(403)
146   ->json_is({ error => "Authorization failure. Missing required permission(s).",
147               required_permissions => { circulate => "circulate_remaining_permissions" }
148             });
149
150 $t->post_ok( "//$userid:$password@/api/v1/checkouts/" . $issue2->issue_id . "/renewal" )
151   ->status_is(201)
152   ->json_is('/due_date' => output_pref({ dateformat => "rfc3339", dt => $expected_datedue}) )
153   ->header_is(Location => "/api/v1/checkouts/" . $issue2->issue_id . "/renewal");
154
155
156 $t->post_ok( "//$userid:$password@/api/v1/checkouts/" . $issue1->issue_id . "/renewal" )
157   ->status_is(403)
158   ->json_is({ error => 'Renewal not authorized (too_many)' });
159