Bug 13903: (QA followup) change routes to /holds
[koha.git] / t / db_dependent / api / v1 / holds.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 => 30;
21 use Test::Mojo;
22
23 use DateTime;
24
25 use C4::Context;
26 use C4::Biblio;
27 use C4::Items;
28 use C4::Reserves;
29
30 use Koha::Database;
31 use Koha::Patron;
32
33 my $dbh = C4::Context->dbh;
34 $dbh->{AutoCommit} = 0;
35 $dbh->{RaiseError} = 1;
36
37 my $t = Test::Mojo->new('Koha::REST::V1');
38
39 my $categorycode = Koha::Database->new()->schema()->resultset('Category')->first()->categorycode();
40 my $branchcode = Koha::Database->new()->schema()->resultset('Branch')->first()->branchcode();
41
42 my $borrower = Koha::Patron->new;
43 $borrower->categorycode( $categorycode );
44 $borrower->branchcode( $branchcode );
45 $borrower->surname("Test Surname");
46 $borrower->store;
47 my $borrowernumber = $borrower->borrowernumber;
48
49 my $borrower2 = Koha::Patron->new;
50 $borrower2->categorycode( $categorycode );
51 $borrower2->branchcode( $branchcode );
52 $borrower2->surname("Test Surname 2");
53 $borrower2->store;
54 my $borrowernumber2 = $borrower2->borrowernumber;
55
56 my $biblionumber = create_biblio('RESTful Web APIs');
57 my $itemnumber = create_item($biblionumber, 'TEST000001');
58
59 my $reserve_id = C4::Reserves::AddReserve($branchcode, $borrowernumber,
60     $biblionumber, undef, 1, undef, undef, undef, '', $itemnumber);
61
62 # Add another reserve to be able to change first reserve's rank
63 C4::Reserves::AddReserve($branchcode, $borrowernumber2,
64     $biblionumber, undef, 2, undef, undef, undef, '', $itemnumber);
65
66 my $suspend_until = DateTime->now->add(days => 10)->ymd;
67 my $put_data = {
68     priority => 2,
69     suspend_until => $suspend_until,
70 };
71 $t->put_ok("/api/v1/holds/$reserve_id" => json => $put_data)
72   ->status_is(200)
73   ->json_is('/reserve_id', $reserve_id)
74   ->json_is('/suspend_until', $suspend_until . ' 00:00:00')
75   ->json_is('/priority', 2);
76
77 $t->delete_ok("/api/v1/holds/$reserve_id")
78   ->status_is(200);
79
80 $t->put_ok("/api/v1/holds/$reserve_id" => json => $put_data)
81   ->status_is(404)
82   ->json_has('/error');
83
84 $t->delete_ok("/api/v1/holds/$reserve_id")
85   ->status_is(404)
86   ->json_has('/error');
87
88
89 $t->get_ok("/api/v1/holds?borrowernumber=$borrowernumber")
90   ->status_is(200)
91   ->json_is([]);
92
93 my $inexisting_borrowernumber = $borrowernumber2 + 1;
94 $t->get_ok("/api/v1/holds?borrowernumber=$inexisting_borrowernumber")
95   ->status_is(404)
96   ->json_has('/error');
97
98 $dbh->do('DELETE FROM issuingrules');
99 $dbh->do(q{
100     INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed)
101     VALUES (?, ?, ?, ?)
102 }, {}, '*', '*', '*', 1);
103
104 my $expirationdate = DateTime->now->add(days => 10)->ymd;
105 my $post_data = {
106     borrowernumber => int($borrowernumber),
107     biblionumber => int($biblionumber),
108     itemnumber => int($itemnumber),
109     branchcode => $branchcode,
110     expirationdate => $expirationdate,
111 };
112 $t->post_ok("/api/v1/holds" => json => $post_data)
113   ->status_is(201)
114   ->json_has('/reserve_id');
115
116 $reserve_id = $t->tx->res->json->{reserve_id};
117
118 $t->get_ok("/api/v1/holds?borrowernumber=$borrowernumber")
119   ->status_is(200)
120   ->json_is('/0/reserve_id', $reserve_id)
121   ->json_is('/0/expirationdate', $expirationdate)
122   ->json_is('/0/branchcode', $branchcode);
123
124 $t->post_ok("/api/v1/holds" => json => $post_data)
125   ->status_is(403)
126   ->json_like('/error', qr/tooManyReserves/);
127
128
129 $dbh->rollback;
130
131 sub create_biblio {
132     my ($title) = @_;
133
134     my $record = new MARC::Record;
135     $record->append_fields(
136         new MARC::Field('200', ' ', ' ', a => $title),
137     );
138
139     my ($biblionumber) = C4::Biblio::AddBiblio($record, '');
140
141     return $biblionumber;
142 }
143
144 sub create_item {
145     my ($biblionumber, $barcode) = @_;
146
147     my $item = {
148         barcode => $barcode,
149     };
150
151     my $itemnumber = C4::Items::AddItem($item, $biblionumber);
152
153     return $itemnumber;
154 }