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 |
|
23 |
use t::lib::TestBuilder; |
24 |
use t::lib::Mocks; |
25 |
|
26 |
use Koha::IllbatchStatus; |
27 |
use Koha::IllbatchStatuses; |
28 |
use Koha::Database; |
29 |
|
30 |
my $schema = Koha::Database->new->schema; |
31 |
my $builder = t::lib::TestBuilder->new; |
32 |
|
33 |
my $t = Test::Mojo->new('Koha::REST::V1'); |
34 |
t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 ); |
35 |
|
36 |
subtest 'list() tests' => sub { |
37 |
|
38 |
plan tests => 9; |
39 |
|
40 |
$schema->storage->txn_begin; |
41 |
|
42 |
Koha::IllbatchStatuses->search->delete; |
43 |
|
44 |
# Create an admin user |
45 |
my $librarian = $builder->build_object( |
46 |
{ |
47 |
class => 'Koha::Patrons', |
48 |
value => { |
49 |
flags => 2 ** 22 # 22 => ill |
50 |
} |
51 |
} |
52 |
); |
53 |
my $password = 'yoda4ever!'; |
54 |
$librarian->set_password( { password => $password, skip_validation => 1 } ); |
55 |
my $userid = $librarian->userid; |
56 |
|
57 |
## Authorized user tests |
58 |
# No statuses, so empty array should be returned |
59 |
$t->get_ok("//$userid:$password@/api/v1/illbatchstatuses") |
60 |
->status_is(200) |
61 |
->json_is( [] ); |
62 |
|
63 |
my $status = $builder->build_object({ |
64 |
class => 'Koha::IllbatchStatuses', |
65 |
value => { |
66 |
name => "Han Solo", |
67 |
code => "SOLO", |
68 |
is_system => 0 |
69 |
} |
70 |
}); |
71 |
|
72 |
# One batch created, should get returned |
73 |
$t->get_ok("//$userid:$password@/api/v1/illbatchstatuses") |
74 |
->status_is(200) |
75 |
->json_has( '/0/id', 'ID' ) |
76 |
->json_has( '/0/name', 'Name' ) |
77 |
->json_has( '/0/code', 'Code' ) |
78 |
->json_has( '/0/is_system', 'is_system' ); |
79 |
|
80 |
$schema->storage->txn_rollback; |
81 |
}; |
82 |
|
83 |
subtest 'get() tests' => sub { |
84 |
|
85 |
plan tests => 11; |
86 |
|
87 |
$schema->storage->txn_begin; |
88 |
|
89 |
my $librarian = $builder->build_object( |
90 |
{ |
91 |
class => 'Koha::Patrons', |
92 |
value => { flags => 2**22 } # 22 => ill |
93 |
} |
94 |
); |
95 |
my $password = 'Rebelz4DaWin'; |
96 |
$librarian->set_password( { password => $password, skip_validation => 1 } ); |
97 |
my $userid = $librarian->userid; |
98 |
|
99 |
my $status = $builder->build_object({ |
100 |
class => 'Koha::IllbatchStatuses', |
101 |
value => { |
102 |
name => "Han Solo", |
103 |
code => "SOLO", |
104 |
is_system => 0 |
105 |
} |
106 |
}); |
107 |
|
108 |
# Unauthorised user |
109 |
my $patron = $builder->build_object( |
110 |
{ |
111 |
class => 'Koha::Patrons', |
112 |
value => { flags => 0 } |
113 |
} |
114 |
); |
115 |
$patron->set_password( { password => $password, skip_validation => 1 } ); |
116 |
my $unauth_userid = $patron->userid; |
117 |
|
118 |
$t->get_ok( "//$userid:$password@/api/v1/illbatchstatuses/" . $status->code ) |
119 |
->status_is(200) |
120 |
->json_has( '/id', 'ID' ) |
121 |
->json_has( '/name', 'Name' ) |
122 |
->json_has( '/code', 'Code' ) |
123 |
->json_has( '/is_system', 'is_system' ); |
124 |
|
125 |
$t->get_ok( "//$unauth_userid:$password@/api/v1/illbatchstatuses/" . $status->id ) |
126 |
->status_is(403); |
127 |
|
128 |
my $status_to_delete = $builder->build_object({ class => 'Koha::IllbatchStatuses' }); |
129 |
my $non_existent_code = $status_to_delete->code; |
130 |
$status_to_delete->delete; |
131 |
|
132 |
$t->get_ok( "//$userid:$password@/api/v1/illbatchstatuses/$non_existent_code" ) |
133 |
->status_is(404) |
134 |
->json_is( '/error' => 'ILL batch status not found' ); |
135 |
|
136 |
$schema->storage->txn_rollback; |
137 |
}; |
138 |
|
139 |
subtest 'add() tests' => sub { |
140 |
|
141 |
plan tests =>14; |
142 |
|
143 |
$schema->storage->txn_begin; |
144 |
|
145 |
my $librarian = $builder->build_object( |
146 |
{ |
147 |
class => 'Koha::Patrons', |
148 |
value => { flags => 2**22 } # 22 => ill |
149 |
} |
150 |
); |
151 |
my $password = '3poRox'; |
152 |
$librarian->set_password( { password => $password, skip_validation => 1 } ); |
153 |
my $userid = $librarian->userid; |
154 |
|
155 |
my $patron = $builder->build_object( |
156 |
{ |
157 |
class => 'Koha::Patrons', |
158 |
value => { flags => 0 } |
159 |
} |
160 |
); |
161 |
$patron->set_password( { password => $password, skip_validation => 1 } ); |
162 |
my $unauth_userid = $patron->userid; |
163 |
|
164 |
my $status_metadata = { |
165 |
name => "In a bacta tank", |
166 |
code => "BACTA", |
167 |
is_system => 0 |
168 |
}; |
169 |
|
170 |
# Unauthorized attempt to write |
171 |
$t->post_ok("//$unauth_userid:$password@/api/v1/illbatchstatuses" => json => $status_metadata) |
172 |
->status_is(403); |
173 |
|
174 |
# Authorized attempt to write invalid data |
175 |
my $status_with_invalid_field = { |
176 |
%{$status_metadata}, |
177 |
doh => 1 |
178 |
}; |
179 |
|
180 |
$t->post_ok( "//$userid:$password@/api/v1/illbatchstatuses" => json => $status_with_invalid_field ) |
181 |
->status_is(400) |
182 |
->json_is( |
183 |
"/errors" => [ |
184 |
{ |
185 |
message => "Properties not allowed: doh.", |
186 |
path => "/body" |
187 |
} |
188 |
] |
189 |
); |
190 |
|
191 |
# Authorized attempt to write |
192 |
my $status_id = |
193 |
$t->post_ok( "//$userid:$password@/api/v1/illbatchstatuses" => json => $status_metadata ) |
194 |
->status_is( 201 ) |
195 |
->json_has( '/id', 'ID' ) |
196 |
->json_has( '/name', 'Name' ) |
197 |
->json_has( '/code', 'Code' ) |
198 |
->json_has( '/is_system', 'is_system' ); |
199 |
|
200 |
# Authorized attempt to create with null id |
201 |
$status_metadata->{id} = undef; |
202 |
$t->post_ok( "//$userid:$password@/api/v1/illbatchstatuses" => json => $status_metadata ) |
203 |
->status_is(400) |
204 |
->json_has('/errors'); |
205 |
|
206 |
$schema->storage->txn_rollback; |
207 |
}; |
208 |
|
209 |
subtest 'update() tests' => sub { |
210 |
|
211 |
plan tests => 13; |
212 |
|
213 |
$schema->storage->txn_begin; |
214 |
|
215 |
my $librarian = $builder->build_object( |
216 |
{ |
217 |
class => 'Koha::Patrons', |
218 |
value => { flags => 2**22 } # 22 => ill |
219 |
} |
220 |
); |
221 |
my $password = 'aw3s0m3y0d41z'; |
222 |
$librarian->set_password( { password => $password, skip_validation => 1 } ); |
223 |
my $userid = $librarian->userid; |
224 |
|
225 |
my $patron = $builder->build_object( |
226 |
{ |
227 |
class => 'Koha::Patrons', |
228 |
value => { flags => 0 } |
229 |
} |
230 |
); |
231 |
$patron->set_password( { password => $password, skip_validation => 1 } ); |
232 |
my $unauth_userid = $patron->userid; |
233 |
|
234 |
my $status_code = $builder->build_object({ class => 'Koha::IllbatchStatuses' } )->code; |
235 |
|
236 |
# Unauthorized attempt to update |
237 |
$t->put_ok( "//$unauth_userid:$password@/api/v1/illbatchstatuses/$status_code" => json => { name => 'These are not the droids you are looking for' } ) |
238 |
->status_is(403); |
239 |
|
240 |
# Attempt partial update on a PUT |
241 |
my $status_with_missing_field = { |
242 |
code => $status_code, |
243 |
is_system => 0 |
244 |
}; |
245 |
|
246 |
$t->put_ok( "//$userid:$password@/api/v1/illbatchstatuses/$status_code" => json => $status_with_missing_field ) |
247 |
->status_is(400) |
248 |
->json_is( "/errors" => |
249 |
[ { message => "Missing property.", path => "/body/name" } ] |
250 |
); |
251 |
|
252 |
# Full object update on PUT |
253 |
my $status_with_updated_field = { |
254 |
name => "Master Ploo Koon", |
255 |
code => $status_code, |
256 |
is_system => 0 |
257 |
}; |
258 |
|
259 |
$t->put_ok( "//$userid:$password@/api/v1/illbatchstatuses/$status_code" => json => $status_with_updated_field ) |
260 |
->status_is(200) |
261 |
->json_is( '/name' => 'Master Ploo Koon' ); |
262 |
|
263 |
# Authorized attempt to write invalid data |
264 |
my $status_with_invalid_field = { |
265 |
doh => 1, |
266 |
name => "Master Mace Windu", |
267 |
code => $status_code |
268 |
}; |
269 |
|
270 |
$t->put_ok( "//$userid:$password@/api/v1/illbatchstatuses/$status_code" => json => $status_with_invalid_field ) |
271 |
->status_is(400) |
272 |
->json_is( |
273 |
"/errors" => [ |
274 |
{ |
275 |
message => "Properties not allowed: doh.", |
276 |
path => "/body" |
277 |
} |
278 |
] |
279 |
); |
280 |
|
281 |
my $status_to_delete = $builder->build_object({ class => 'Koha::IllbatchStatuses' }); |
282 |
my $non_existent_code = $status_to_delete->code; |
283 |
$status_to_delete->delete; |
284 |
|
285 |
$t->put_ok( "//$userid:$password@/api/v1/illbatchstatuses/$non_existent_code" => json => $status_with_updated_field ) |
286 |
->status_is(404); |
287 |
|
288 |
$schema->storage->txn_rollback; |
289 |
}; |
290 |
|
291 |
subtest 'delete() tests' => sub { |
292 |
|
293 |
plan tests => 9; |
294 |
|
295 |
$schema->storage->txn_begin; |
296 |
|
297 |
my $librarian = $builder->build_object( |
298 |
{ |
299 |
class => 'Koha::Patrons', |
300 |
value => { flags => 2**22 } # 22 => ill |
301 |
} |
302 |
); |
303 |
my $password = 's1th43v3r!'; |
304 |
$librarian->set_password( { password => $password, skip_validation => 1 } ); |
305 |
my $userid = $librarian->userid; |
306 |
|
307 |
my $patron = $builder->build_object( |
308 |
{ |
309 |
class => 'Koha::Patrons', |
310 |
value => { flags => 0 } |
311 |
} |
312 |
); |
313 |
|
314 |
$patron->set_password( { password => $password, skip_validation => 1 } ); |
315 |
my $unauth_userid = $patron->userid; |
316 |
|
317 |
my $non_system_status = $builder->build_object({ |
318 |
class => 'Koha::IllbatchStatuses', |
319 |
value => { |
320 |
is_system => 0 |
321 |
} |
322 |
}); |
323 |
|
324 |
my $system_status = $builder->build_object({ |
325 |
class => 'Koha::IllbatchStatuses', |
326 |
value => { |
327 |
is_system => 1 |
328 |
} |
329 |
}); |
330 |
|
331 |
# Unauthorized attempt to delete |
332 |
$t->delete_ok( "//$unauth_userid:$password@/api/v1/illbatchstatuses/" . $non_system_status->code ) |
333 |
->status_is(403); |
334 |
|
335 |
$t->delete_ok("//$userid:$password@/api/v1/illbatchstatuses/" . $non_system_status->code ) |
336 |
->status_is(204); |
337 |
|
338 |
$t->delete_ok("//$userid:$password@/api/v1/illbatchstatuses/" . $non_system_status->code ) |
339 |
->status_is(404); |
340 |
|
341 |
$t->delete_ok("//$userid:$password@/api/v1/illbatchstatuses/" . $system_status->code ) |
342 |
->status_is(400) |
343 |
->json_is( |
344 |
"/errors" => [ |
345 |
{ |
346 |
message => "ILL batch status cannot be deleted" |
347 |
} |
348 |
] |
349 |
); |
350 |
|
351 |
$schema->storage->txn_rollback; |
352 |
}; |