Line 0
Link Here
|
0 |
- |
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 |
6 |
# under the terms of the GNU General Public License as published by |
7 |
# the Free Software Foundation; either version 3 of the License, or |
8 |
# (at your option) any later version. |
9 |
# |
10 |
# Koha is distributed in the hope that it will be useful, but |
11 |
# WITHOUT ANY WARRANTY; without even the implied warranty of |
12 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 |
# GNU General Public License for more details. |
14 |
# |
15 |
# You should have received a copy of the GNU General Public License |
16 |
# along with Koha; if not, see <http://www.gnu.org/licenses>. |
17 |
|
18 |
use Modern::Perl; |
19 |
|
20 |
use Test::More tests => 5; |
21 |
use Test::Mojo; |
22 |
use Test::Warn; |
23 |
|
24 |
use t::lib::TestBuilder; |
25 |
use t::lib::Mocks; |
26 |
|
27 |
use List::Util qw(min); |
28 |
|
29 |
use Koha::Libraries; |
30 |
use Koha::Database; |
31 |
|
32 |
my $schema = Koha::Database->new->schema; |
33 |
my $builder = t::lib::TestBuilder->new; |
34 |
|
35 |
t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 ); |
36 |
t::lib::Mocks::mock_preference( 'EnableVolumes', 1 ); |
37 |
|
38 |
my $t = Test::Mojo->new('Koha::REST::V1'); |
39 |
|
40 |
subtest 'volumes list() tests' => sub { |
41 |
plan tests => 9; |
42 |
|
43 |
$schema->storage->txn_begin; |
44 |
|
45 |
my $patron = $builder->build_object({ |
46 |
class => 'Koha::Patrons', |
47 |
value => { flags => 4 } |
48 |
}); |
49 |
my $password = 'thePassword123'; |
50 |
$patron->set_password({ password => $password, skip_validation => 1 }); |
51 |
my $userid = $patron->userid; |
52 |
|
53 |
my $biblio = $builder->build_sample_biblio(); |
54 |
my $biblio_id = $biblio->id; |
55 |
|
56 |
$t->get_ok( "//$userid:$password@/api/v1/biblios/$biblio_id/volumes" ) |
57 |
->status_is( 200, 'SWAGGER3.2.2' ); |
58 |
my $response_count = scalar @{ $t->tx->res->json }; |
59 |
is( $response_count, 0, 'Results count is 2'); |
60 |
|
61 |
my $volume_1 = Koha::Biblio::Volume->new( { biblionumber => $biblio->id, display_order => 1, description => "Vol 1" } )->store(); |
62 |
|
63 |
$t->get_ok( "//$userid:$password@/api/v1/biblios/$biblio_id/volumes" ) |
64 |
->status_is( 200, 'SWAGGER3.2.2' ); |
65 |
$response_count = scalar @{ $t->tx->res->json }; |
66 |
is( $response_count, 1, 'Results count is 2'); |
67 |
|
68 |
my $volume_2 = Koha::Biblio::Volume->new( { biblionumber => $biblio->id, display_order => 2, description => "Vol 2" } )->store(); |
69 |
|
70 |
$t->get_ok( "//$userid:$password@/api/v1/biblios/$biblio_id/volumes" ) |
71 |
->status_is( 200, 'SWAGGER3.2.2' ); |
72 |
|
73 |
$response_count = scalar @{ $t->tx->res->json }; |
74 |
is( $response_count, 2, 'Results count is 2'); |
75 |
|
76 |
$schema->storage->txn_rollback; |
77 |
}; |
78 |
|
79 |
subtest 'volumes add() tests' => sub { |
80 |
|
81 |
plan tests => 6; |
82 |
|
83 |
$schema->storage->txn_begin; |
84 |
|
85 |
my $authorized_patron = $builder->build_object({ |
86 |
class => 'Koha::Patrons', |
87 |
value => { flags => 1 } |
88 |
}); |
89 |
my $password = 'thePassword123'; |
90 |
$authorized_patron->set_password({ password => $password, skip_validation => 1 }); |
91 |
my $auth_userid = $authorized_patron->userid; |
92 |
|
93 |
my $unauthorized_patron = $builder->build_object({ |
94 |
class => 'Koha::Patrons', |
95 |
value => { flags => 0 } |
96 |
}); |
97 |
$unauthorized_patron->set_password({ password => $password, skip_validation => 1 }); |
98 |
my $unauth_userid = $unauthorized_patron->userid; |
99 |
|
100 |
my $biblio = $builder->build_sample_biblio(); |
101 |
my $biblio_id = $biblio->id; |
102 |
my $volume = { description => 'Vol 1', display_order => 1 }; |
103 |
|
104 |
# Unauthorized attempt |
105 |
$t->post_ok( "//$unauth_userid:$password@/api/v1/biblios/$biblio_id/volumes" => json => $volume ) |
106 |
->status_is(403); |
107 |
|
108 |
# Authorized attempt |
109 |
$t->post_ok( "//$auth_userid:$password@/api/v1/biblios/$biblio_id/volumes" => json => $volume ) |
110 |
->status_is( 201, 'SWAGGER3.2.1' ); |
111 |
|
112 |
# Invalid biblio id |
113 |
$t->post_ok( "//$auth_userid:$password@/api/v1/biblios/XXX/volumes" => json => $volume ) |
114 |
->status_is( 409, 'SWAGGER3.2.1' ); |
115 |
|
116 |
$schema->storage->txn_rollback; |
117 |
}; |
118 |
|
119 |
subtest 'volumes update() tests' => sub { |
120 |
plan tests => 9; |
121 |
|
122 |
$schema->storage->txn_begin; |
123 |
|
124 |
my $authorized_patron = $builder->build_object({ |
125 |
class => 'Koha::Patrons', |
126 |
value => { flags => 1 } |
127 |
}); |
128 |
my $password = 'thePassword123'; |
129 |
$authorized_patron->set_password({ password => $password, skip_validation => 1 }); |
130 |
my $auth_userid = $authorized_patron->userid; |
131 |
|
132 |
my $unauthorized_patron = $builder->build_object({ |
133 |
class => 'Koha::Patrons', |
134 |
value => { flags => 0 } |
135 |
}); |
136 |
$unauthorized_patron->set_password({ password => $password, skip_validation => 1 }); |
137 |
my $unauth_userid = $unauthorized_patron->userid; |
138 |
|
139 |
my $biblio = $builder->build_sample_biblio(); |
140 |
my $biblio_id = $biblio->id; |
141 |
my $volume = Koha::Biblio::Volume->new( { biblionumber => $biblio->id, display_order => 1, description => "Vol 1" } )->store(); |
142 |
my $volume_id = $volume->id; |
143 |
|
144 |
# Unauthorized attempt |
145 |
$t->put_ok( "//$unauth_userid:$password@/api/v1/biblios/$biblio_id/volumes/$volume_id" |
146 |
=> json => { description => 'New unauthorized desc change' } ) |
147 |
->status_is(403); |
148 |
|
149 |
# Authorized attempt |
150 |
$t->put_ok( "//$auth_userid:$password@/api/v1/biblios/$biblio_id/volumes/$volume_id" => json => { description => "Vol A" } ) |
151 |
->status_is(200, 'SWAGGER3.2.1') |
152 |
->json_has( '/description' => "Vol A", 'SWAGGER3.3.3' ); |
153 |
|
154 |
# Invalid biblio id |
155 |
$t->put_ok( "//$auth_userid:$password@/api/v1/biblios/XXX/volumes/$volume_id" => json => { description => "Vol A" } ) |
156 |
->status_is(404); |
157 |
|
158 |
# Invalid volume id |
159 |
$t->put_ok( "//$auth_userid:$password@/api/v1/biblios/$biblio_id/volumes/XXX" => json => { description => "Vol A" } ) |
160 |
->status_is(404); |
161 |
|
162 |
$schema->storage->txn_rollback; |
163 |
}; |
164 |
|
165 |
subtest 'volumes delete() tests' => sub { |
166 |
plan tests => 9; |
167 |
|
168 |
$schema->storage->txn_begin; |
169 |
|
170 |
my $authorized_patron = $builder->build_object({ |
171 |
class => 'Koha::Patrons', |
172 |
value => { flags => 1 } |
173 |
}); |
174 |
my $password = 'thePassword123'; |
175 |
$authorized_patron->set_password({ password => $password, skip_validation => 1 }); |
176 |
my $auth_userid = $authorized_patron->userid; |
177 |
|
178 |
my $unauthorized_patron = $builder->build_object({ |
179 |
class => 'Koha::Patrons', |
180 |
value => { flags => 0 } |
181 |
}); |
182 |
$unauthorized_patron->set_password({ password => $password, skip_validation => 1 }); |
183 |
my $unauth_userid = $unauthorized_patron->userid; |
184 |
|
185 |
my $biblio = $builder->build_sample_biblio(); |
186 |
my $biblio_id = $biblio->id; |
187 |
my $volume = Koha::Biblio::Volume->new( { biblionumber => $biblio->id, display_order => 1, description => "Vol 1" } )->store(); |
188 |
my $volume_id = $volume->id; |
189 |
|
190 |
$t->delete_ok( "//$auth_userid:$password@/api/v1/biblios/$biblio_id/volumes/$volume_id" ) |
191 |
->status_is(204, 'SWAGGER3.2.4') |
192 |
->content_is('', 'SWAGGER3.3.4'); |
193 |
|
194 |
# Unauthorized attempt to delete |
195 |
$t->delete_ok( "//$unauth_userid:$password@/api/v1/biblios/$biblio_id/volumes/$volume_id" ) |
196 |
->status_is(403); |
197 |
|
198 |
$t->delete_ok( "//$auth_userid:$password@/api/v1/biblios/XXX/volumes/$volume_id" ) |
199 |
->status_is(404); |
200 |
|
201 |
$t->delete_ok( "//$auth_userid:$password@/api/v1/biblios/$biblio_id/volumes/XXX" ) |
202 |
->status_is(404); |
203 |
|
204 |
$schema->storage->txn_rollback; |
205 |
}; |
206 |
|
207 |
subtest 'volume items add() + delete() tests' => sub { |
208 |
plan tests => 11; |
209 |
|
210 |
$schema->storage->txn_begin; |
211 |
|
212 |
my $patron = $builder->build_object({ |
213 |
class => 'Koha::Patrons', |
214 |
value => { flags => 4 } |
215 |
}); |
216 |
my $password = 'thePassword123'; |
217 |
$patron->set_password({ password => $password, skip_validation => 1 }); |
218 |
my $userid = $patron->userid; |
219 |
|
220 |
my $biblio = $builder->build_sample_biblio(); |
221 |
my $biblio_id = $biblio->id; |
222 |
|
223 |
my $volume = Koha::Biblio::Volume->new( { biblionumber => $biblio->id, display_order => 1, description => "Vol 1" } )->store(); |
224 |
my $volume_id = $volume->id; |
225 |
|
226 |
my @items = $volume->items; |
227 |
is( scalar(@items), 0, 'Volume has no items'); |
228 |
|
229 |
my $item_1 = $builder->build_sample_item({ biblionumber => $biblio->biblionumber }); |
230 |
my $item_1_id = $item_1->id; |
231 |
|
232 |
$t->post_ok( "//$userid:$password@/api/v1/biblios/{biblio_id}/volumes/$volume_id/items" => json => { item_id => $item_1->id } ) |
233 |
->status_is( 201, 'SWAGGER3.2.1' ); |
234 |
|
235 |
@items = $volume->items; |
236 |
is( scalar(@items), 1, 'Volume now has one item'); |
237 |
|
238 |
my $item_2 = $builder->build_sample_item({ biblionumber => $biblio->biblionumber }); |
239 |
my $item_2_id = $item_2->id; |
240 |
|
241 |
$t->post_ok( "//$userid:$password@/api/v1/biblios/{biblio_id}/volumes/$volume_id/items" => json => { item_id => $item_2->id } ) |
242 |
->status_is( 201, 'SWAGGER3.2.1' ); |
243 |
|
244 |
@items = $volume->items; |
245 |
is( scalar(@items), 2, 'Volume now has two items'); |
246 |
|
247 |
warn "A VOLUME ID: $volume_id"; |
248 |
warn "A ITEM ID: $item_1_id"; |
249 |
$t->delete_ok( "//$userid:$password@/api/v1/biblios/$biblio_id/volumes/$volume_id/items/$item_1_id" ) |
250 |
->status_is(204, 'SWAGGER3.2.4') |
251 |
->content_is('', 'SWAGGER3.3.4'); |
252 |
|
253 |
@items = $volume->items; |
254 |
is( scalar(@items), 1, 'Volume now has one item'); |
255 |
|
256 |
$schema->storage->txn_rollback; |
257 |
}; |