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::Illbatch; |
27 |
use Koha::Illbatches; |
28 |
use Koha::Illrequests; |
29 |
use Koha::Database; |
30 |
|
31 |
my $schema = Koha::Database->new->schema; |
32 |
my $builder = t::lib::TestBuilder->new; |
33 |
|
34 |
my $t = Test::Mojo->new('Koha::REST::V1'); |
35 |
t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 ); |
36 |
|
37 |
subtest 'list() tests' => sub { |
38 |
|
39 |
plan tests => 19; |
40 |
|
41 |
$schema->storage->txn_begin; |
42 |
|
43 |
Koha::Illbatches->search->delete; |
44 |
|
45 |
my $librarian = $builder->build_object( |
46 |
{ |
47 |
class => 'Koha::Patrons', |
48 |
value => { |
49 |
flags => 2 ** 22 # 22 => ill |
50 |
} |
51 |
} |
52 |
); |
53 |
|
54 |
my $branch = $builder->build_object( |
55 |
{ |
56 |
class => 'Koha::Libraries' |
57 |
} |
58 |
); |
59 |
|
60 |
my $password = 'sheev_is_da_boss!'; |
61 |
$librarian->set_password( { password => $password, skip_validation => 1 } ); |
62 |
my $userid = $librarian->userid; |
63 |
|
64 |
## Authorized user tests |
65 |
# No batches, so empty array should be returned |
66 |
$t->get_ok("//$userid:$password@/api/v1/illbatches") |
67 |
->status_is(200) |
68 |
->json_is( [] ); |
69 |
|
70 |
my $batch = $builder->build_object({ |
71 |
class => 'Koha::Illbatches', |
72 |
value => { |
73 |
name => "PapaPalpatine", |
74 |
backend => "Mock", |
75 |
borrowernumber => $librarian->borrowernumber, |
76 |
branchcode => $branch->branchcode |
77 |
} |
78 |
}); |
79 |
|
80 |
my $illrq = $builder->build({ |
81 |
source => 'Illrequest', |
82 |
value => { |
83 |
borrowernumber => $librarian->borrowernumber, |
84 |
batch_id => $batch->id |
85 |
} |
86 |
}); |
87 |
|
88 |
# One batch created, should get returned |
89 |
$t->get_ok("//$userid:$password@/api/v1/illbatches") |
90 |
->status_is(200) |
91 |
->json_has( '/0/id', 'Batch ID' ) |
92 |
->json_has( '/0/name', 'Batch name' ) |
93 |
->json_has( '/0/backend', 'Backend name' ) |
94 |
->json_has( '/0/borrowernumber', 'Borrowernumber' ) |
95 |
->json_has( '/0/branchcode', 'Branchcode' ) |
96 |
->json_has( '/0/patron', 'patron embedded' ) |
97 |
->json_has( '/0/branch', 'branch embedded' ) |
98 |
->json_has( '/0/requests_count', 'request count' ); |
99 |
|
100 |
# Try to create a second batch with the same name, this should fail |
101 |
my $another_batch = $builder->build_object({ class => 'Koha::Illbatches', value => { |
102 |
name => $batch->name |
103 |
} }); |
104 |
# Create a second batch with a different name |
105 |
my $batch_with_another_name = $builder->build_object({ class => 'Koha::Illbatches' }); |
106 |
|
107 |
# Two batches created, they should both be returned |
108 |
$t->get_ok("//$userid:$password@/api/v1/illbatches") |
109 |
->status_is(200) |
110 |
->json_has('/0', 'has first batch') |
111 |
->json_has('/1', 'has second batch'); |
112 |
|
113 |
my $patron = $builder->build_object( |
114 |
{ |
115 |
class => 'Koha::Patrons', |
116 |
value => { |
117 |
cardnumber => 999, |
118 |
flags => 0 |
119 |
} |
120 |
} |
121 |
); |
122 |
|
123 |
$patron->set_password( { password => $password, skip_validation => 1 } ); |
124 |
my $unauth_userid = $patron->userid; |
125 |
|
126 |
# Unauthorized access |
127 |
$t->get_ok("//$unauth_userid:$password@/api/v1/illbatches") |
128 |
->status_is(403); |
129 |
|
130 |
$schema->storage->txn_rollback; |
131 |
}; |
132 |
|
133 |
subtest 'get() tests' => sub { |
134 |
|
135 |
plan tests => 15; |
136 |
|
137 |
$schema->storage->txn_begin; |
138 |
|
139 |
my $librarian = $builder->build_object( |
140 |
{ |
141 |
class => 'Koha::Patrons', |
142 |
value => { flags => 2**22 } # 22 => ill |
143 |
} |
144 |
); |
145 |
my $password = 'Rebelz4DaWin'; |
146 |
$librarian->set_password( { password => $password, skip_validation => 1 } ); |
147 |
my $userid = $librarian->userid; |
148 |
|
149 |
my $patron = $builder->build_object( |
150 |
{ |
151 |
class => 'Koha::Patrons', |
152 |
value => { flags => 0 } |
153 |
} |
154 |
); |
155 |
|
156 |
my $branch = $builder->build_object( |
157 |
{ |
158 |
class => 'Koha::Libraries' |
159 |
} |
160 |
); |
161 |
|
162 |
my $batch = $builder->build_object({ |
163 |
class => 'Koha::Illbatches', |
164 |
value => { |
165 |
name => "LeiaOrgana", |
166 |
backend => "Mock", |
167 |
borrowernumber => $librarian->borrowernumber, |
168 |
branchcode => $branch->branchcode |
169 |
} |
170 |
}); |
171 |
|
172 |
|
173 |
$patron->set_password( { password => $password, skip_validation => 1 } ); |
174 |
my $unauth_userid = $patron->userid; |
175 |
|
176 |
$t->get_ok( "//$userid:$password@/api/v1/illbatches/" . $batch->id ) |
177 |
->status_is(200) |
178 |
->json_has( '/id', 'Batch ID' ) |
179 |
->json_has( '/name', 'Batch name' ) |
180 |
->json_has( '/backend', 'Backend name' ) |
181 |
->json_has( '/borrowernumber', 'Borrowernumber' ) |
182 |
->json_has( '/branchcode', 'Branchcode' ) |
183 |
->json_has( '/patron', 'patron embedded' ) |
184 |
->json_has( '/branch', 'branch embedded' ) |
185 |
->json_has( '/requests_count', 'request count' ); |
186 |
|
187 |
$t->get_ok( "//$unauth_userid:$password@/api/v1/illbatches/" . $batch->id ) |
188 |
->status_is(403); |
189 |
|
190 |
my $batch_to_delete = $builder->build_object({ class => 'Koha::Illbatches' }); |
191 |
my $non_existent_id = $batch_to_delete->id; |
192 |
$batch_to_delete->delete; |
193 |
|
194 |
$t->get_ok( "//$userid:$password@/api/v1/illbatches/$non_existent_id" ) |
195 |
->status_is(404) |
196 |
->json_is( '/error' => 'ILL batch not found' ); |
197 |
|
198 |
$schema->storage->txn_rollback; |
199 |
}; |
200 |
|
201 |
subtest 'add() tests' => sub { |
202 |
|
203 |
plan tests =>16; |
204 |
|
205 |
$schema->storage->txn_begin; |
206 |
|
207 |
my $librarian = $builder->build_object( |
208 |
{ |
209 |
class => 'Koha::Patrons', |
210 |
value => { flags => 2**22 } # 22 => ill |
211 |
} |
212 |
); |
213 |
my $password = 'v4d3rRox'; |
214 |
$librarian->set_password( { password => $password, skip_validation => 1 } ); |
215 |
my $userid = $librarian->userid; |
216 |
|
217 |
my $patron = $builder->build_object( |
218 |
{ |
219 |
class => 'Koha::Patrons', |
220 |
value => { flags => 0 } |
221 |
} |
222 |
); |
223 |
|
224 |
$patron->set_password( { password => $password, skip_validation => 1 } ); |
225 |
my $unauth_userid = $patron->userid; |
226 |
|
227 |
my $branch = $builder->build_object( |
228 |
{ |
229 |
class => 'Koha::Libraries' |
230 |
} |
231 |
); |
232 |
|
233 |
my $batch_metadata = { |
234 |
name => "Anakin's requests", |
235 |
backend => "Mock", |
236 |
borrowernumber => $librarian->borrowernumber, |
237 |
branchcode => $branch->branchcode |
238 |
}; |
239 |
|
240 |
# Unauthorized attempt to write |
241 |
$t->post_ok("//$unauth_userid:$password@/api/v1/illbatches" => json => $batch_metadata) |
242 |
->status_is(403); |
243 |
|
244 |
# Authorized attempt to write invalid data |
245 |
my $batch_with_invalid_field = { |
246 |
%{$batch_metadata}, |
247 |
doh => 1 |
248 |
}; |
249 |
|
250 |
$t->post_ok( "//$userid:$password@/api/v1/illbatches" => json => $batch_with_invalid_field ) |
251 |
->status_is(400) |
252 |
->json_is( |
253 |
"/errors" => [ |
254 |
{ |
255 |
message => "Properties not allowed: doh.", |
256 |
path => "/body" |
257 |
} |
258 |
] |
259 |
); |
260 |
|
261 |
# Authorized attempt to write |
262 |
my $batch_id = |
263 |
$t->post_ok( "//$userid:$password@/api/v1/illbatches" => json => $batch_metadata ) |
264 |
->status_is( 201 ) |
265 |
->json_is( '/name' => $batch_metadata->{name} ) |
266 |
->json_is( '/backend' => $batch_metadata->{backend} ) |
267 |
->json_is( '/borrowernumber' => $batch_metadata->{borrowernumber} ) |
268 |
->json_is( '/branchcode' => $batch_metadata->{branchcode} ) |
269 |
->json_has( '/patron' ) |
270 |
->json_has( '/branch' ); |
271 |
|
272 |
# Authorized attempt to create with null id |
273 |
$batch_metadata->{id} = undef; |
274 |
$t->post_ok( "//$userid:$password@/api/v1/illbatches" => json => $batch_metadata ) |
275 |
->status_is(400) |
276 |
->json_has('/errors'); |
277 |
|
278 |
$schema->storage->txn_rollback; |
279 |
}; |
280 |
|
281 |
subtest 'update() tests' => sub { |
282 |
|
283 |
plan tests => 15; |
284 |
|
285 |
$schema->storage->txn_begin; |
286 |
|
287 |
my $librarian = $builder->build_object( |
288 |
{ |
289 |
class => 'Koha::Patrons', |
290 |
value => { flags => 2**22 } # 22 => ill |
291 |
} |
292 |
); |
293 |
my $password = 'aw3s0m3y0d41z'; |
294 |
$librarian->set_password( { password => $password, skip_validation => 1 } ); |
295 |
my $userid = $librarian->userid; |
296 |
|
297 |
my $patron = $builder->build_object( |
298 |
{ |
299 |
class => 'Koha::Patrons', |
300 |
value => { flags => 0 } |
301 |
} |
302 |
); |
303 |
|
304 |
$patron->set_password( { password => $password, skip_validation => 1 } ); |
305 |
my $unauth_userid = $patron->userid; |
306 |
|
307 |
my $branch = $builder->build_object( |
308 |
{ |
309 |
class => 'Koha::Libraries' |
310 |
} |
311 |
); |
312 |
|
313 |
my $batch_id = $builder->build_object({ class => 'Koha::Illbatches' } )->id; |
314 |
|
315 |
# Unauthorized attempt to update |
316 |
$t->put_ok( "//$unauth_userid:$password@/api/v1/illbatches/$batch_id" => json => { name => 'These are not the droids you are looking for' } ) |
317 |
->status_is(403); |
318 |
|
319 |
# Attempt partial update on a PUT |
320 |
my $batch_with_missing_field = { |
321 |
backend => "Mock", |
322 |
borrowernumber => $librarian->borrowernumber, |
323 |
branchcode => $branch->branchcode |
324 |
}; |
325 |
|
326 |
$t->put_ok( "//$userid:$password@/api/v1/illbatches/$batch_id" => json => $batch_with_missing_field ) |
327 |
->status_is(400) |
328 |
->json_is( "/errors" => |
329 |
[ { message => "Missing property.", path => "/body/name" } ] |
330 |
); |
331 |
|
332 |
# Full object update on PUT |
333 |
my $batch_with_updated_field = { |
334 |
name => "Master Ploo Koon", |
335 |
backend => "Mock", |
336 |
borrowernumber => $librarian->borrowernumber, |
337 |
branchcode => $branch->branchcode |
338 |
}; |
339 |
|
340 |
$t->put_ok( "//$userid:$password@/api/v1/illbatches/$batch_id" => json => $batch_with_updated_field ) |
341 |
->status_is(200) |
342 |
->json_is( '/name' => 'Master Ploo Koon' ); |
343 |
|
344 |
# Authorized attempt to write invalid data |
345 |
my $batch_with_invalid_field = { |
346 |
doh => 1, |
347 |
name => "Master Mace Windu", |
348 |
backend => "Mock" |
349 |
}; |
350 |
|
351 |
$t->put_ok( "//$userid:$password@/api/v1/illbatches/$batch_id" => json => $batch_with_invalid_field ) |
352 |
->status_is(400) |
353 |
->json_is( |
354 |
"/errors" => [ |
355 |
{ |
356 |
message => "Properties not allowed: doh.", |
357 |
path => "/body" |
358 |
} |
359 |
] |
360 |
); |
361 |
|
362 |
my $batch_to_delete = $builder->build_object({ class => 'Koha::Cities' }); |
363 |
my $non_existent_id = $batch_to_delete->id; |
364 |
$batch_to_delete->delete; |
365 |
|
366 |
$t->put_ok( "//$userid:$password@/api/v1/illbatches/$non_existent_id" => json => $batch_with_updated_field ) |
367 |
->status_is(404); |
368 |
|
369 |
# Wrong method (POST) |
370 |
$batch_with_updated_field->{id} = 2; |
371 |
|
372 |
$t->post_ok( "//$userid:$password@/api/v1/illbatches/$batch_id" => json => $batch_with_updated_field ) |
373 |
->status_is(404); |
374 |
|
375 |
$schema->storage->txn_rollback; |
376 |
}; |
377 |
|
378 |
subtest 'delete() tests' => sub { |
379 |
|
380 |
plan tests => 6; |
381 |
|
382 |
$schema->storage->txn_begin; |
383 |
|
384 |
my $librarian = $builder->build_object( |
385 |
{ |
386 |
class => 'Koha::Patrons', |
387 |
value => { flags => 2**22 } # 22 => ill |
388 |
} |
389 |
); |
390 |
my $password = 's1th43v3r!'; |
391 |
$librarian->set_password( { password => $password, skip_validation => 1 } ); |
392 |
my $userid = $librarian->userid; |
393 |
|
394 |
my $patron = $builder->build_object( |
395 |
{ |
396 |
class => 'Koha::Patrons', |
397 |
value => { flags => 0 } |
398 |
} |
399 |
); |
400 |
|
401 |
$patron->set_password( { password => $password, skip_validation => 1 } ); |
402 |
my $unauth_userid = $patron->userid; |
403 |
|
404 |
my $batch_id = $builder->build_object({ class => 'Koha::Illbatches' })->id; |
405 |
|
406 |
# Unauthorized attempt to delete |
407 |
$t->delete_ok( "//$unauth_userid:$password@/api/v1/illbatches/$batch_id" ) |
408 |
->status_is(403); |
409 |
|
410 |
$t->delete_ok("//$userid:$password@/api/v1/illbatches/$batch_id") |
411 |
->status_is(204); |
412 |
|
413 |
$t->delete_ok("//$userid:$password@/api/v1/illbatches/$batch_id") |
414 |
->status_is(404); |
415 |
|
416 |
$schema->storage->txn_rollback; |
417 |
}; |