Lines 17-23
Link Here
|
17 |
|
17 |
|
18 |
use Modern::Perl; |
18 |
use Modern::Perl; |
19 |
|
19 |
|
20 |
use Test::More tests => 1; |
20 |
use Test::More tests => 6; |
21 |
use Test::Mojo; |
21 |
use Test::Mojo; |
22 |
|
22 |
|
23 |
use t::lib::TestBuilder; |
23 |
use t::lib::TestBuilder; |
Lines 85-87
subtest 'list_av_from_category() tests' => sub {
Link Here
|
85 |
|
85 |
|
86 |
$schema->storage->txn_rollback; |
86 |
$schema->storage->txn_rollback; |
87 |
}; |
87 |
}; |
88 |
- |
88 |
|
|
|
89 |
subtest 'list() tests' => sub { |
90 |
|
91 |
plan tests => 20; |
92 |
|
93 |
$schema->storage->txn_begin; |
94 |
|
95 |
Koha::AuthorisedValues->search->delete; |
96 |
|
97 |
my $librarian = $builder->build_object( |
98 |
{ |
99 |
class => 'Koha::Patrons', |
100 |
value => { flags => 2 ** 2 } # catalogue flag = 2 |
101 |
} |
102 |
); |
103 |
my $password = 'thePassword123'; |
104 |
$librarian->set_password( { password => $password, skip_validation => 1 } ); |
105 |
my $userid = $librarian->userid; |
106 |
|
107 |
my $patron = $builder->build_object( |
108 |
{ |
109 |
class => 'Koha::Patrons', |
110 |
value => { flags => 0 } |
111 |
} |
112 |
); |
113 |
|
114 |
$patron->set_password( { password => $password, skip_validation => 1 } ); |
115 |
my $unauth_userid = $patron->userid; |
116 |
|
117 |
## Authorized user tests |
118 |
# No AVs, so empty array should be returned |
119 |
$t->get_ok("//$userid:$password@/api/v1/authorised_values") |
120 |
->status_is(200) |
121 |
->json_is( [] ); |
122 |
|
123 |
my $av = $builder->build_object({ class => 'Koha::AuthorisedValues' }); |
124 |
|
125 |
# One av created, should get returned |
126 |
$t->get_ok("//$userid:$password@/api/v1/authorised_values") |
127 |
->status_is(200) |
128 |
->json_is( [$av->to_api] ); |
129 |
|
130 |
my $another_av = $builder->build_object( |
131 |
{ class => 'Koha::AuthorisedValues', value => { lib => $av->lib } } ); |
132 |
my $av_with_another_lib = $builder->build_object({ class => 'Koha::AuthorisedValues' }); |
133 |
|
134 |
# Two avs created, they should both be returned |
135 |
$t->get_ok("//$userid:$password@/api/v1/authorised_values") |
136 |
->status_is(200) |
137 |
->json_is([$av->to_api, |
138 |
$another_av->to_api, |
139 |
$av_with_another_lib->to_api |
140 |
] ); |
141 |
|
142 |
# Filtering works, two avs sharing lib |
143 |
$t->get_ok("//$userid:$password@/api/v1/authorised_values?description=" . $av->lib ) |
144 |
->status_is(200) |
145 |
->json_is([ $av->to_api, |
146 |
$another_av->to_api |
147 |
]); |
148 |
|
149 |
$t->get_ok("//$userid:$password@/api/v1/authorised_values?value=" . $av->authorised_value ) |
150 |
->status_is(200) |
151 |
->json_is( [$av->to_api] ); |
152 |
|
153 |
# Warn on unsupported query parameter |
154 |
$t->get_ok("//$userid:$password@/api/v1/authorised_values?av_blah=blah" ) |
155 |
->status_is(400) |
156 |
->json_is( [{ path => '/query/av_blah', message => 'Malformed query string'}] ); |
157 |
|
158 |
# Unauthorized access |
159 |
$t->get_ok("//$unauth_userid:$password@/api/v1/authorised_values") |
160 |
->status_is(403); |
161 |
|
162 |
$schema->storage->txn_rollback; |
163 |
}; |
164 |
|
165 |
subtest 'get() tests' => sub { |
166 |
|
167 |
plan tests => 8; |
168 |
|
169 |
$schema->storage->txn_begin; |
170 |
|
171 |
my $av = $builder->build_object({ class => 'Koha::AuthorisedValues' }); |
172 |
my $librarian = $builder->build_object( |
173 |
{ |
174 |
class => 'Koha::Patrons', |
175 |
value => { flags => 2**2 } # catalogue flag = 2 |
176 |
} |
177 |
); |
178 |
my $password = 'thePassword123'; |
179 |
$librarian->set_password( { password => $password, skip_validation => 1 } ); |
180 |
my $userid = $librarian->userid; |
181 |
|
182 |
my $patron = $builder->build_object( |
183 |
{ |
184 |
class => 'Koha::Patrons', |
185 |
value => { flags => 0 } |
186 |
} |
187 |
); |
188 |
|
189 |
$patron->set_password( { password => $password, skip_validation => 1 } ); |
190 |
my $unauth_userid = $patron->userid; |
191 |
|
192 |
$t->get_ok( "//$userid:$password@/api/v1/authorised_values/" . $av->id ) |
193 |
->status_is(200) |
194 |
->json_is($av->to_api); |
195 |
|
196 |
$t->get_ok( "//$unauth_userid:$password@/api/v1/authorised_values/" . $av->id ) |
197 |
->status_is(403); |
198 |
|
199 |
my $av_to_delete = $builder->build_object({ class => 'Koha::AuthorisedValues' }); |
200 |
my $non_existent_id = $av_to_delete->id; |
201 |
$av_to_delete->delete; |
202 |
|
203 |
$t->get_ok( "//$userid:$password@/api/v1/authorised_values/$non_existent_id" ) |
204 |
->status_is(404) |
205 |
->json_is( '/error' => 'Authorised value not found' ); |
206 |
|
207 |
$schema->storage->txn_rollback; |
208 |
}; |
209 |
|
210 |
subtest 'add() tests' => sub { |
211 |
|
212 |
plan tests => 18; |
213 |
|
214 |
$schema->storage->txn_begin; |
215 |
|
216 |
my $librarian = $builder->build_object( |
217 |
{ |
218 |
class => 'Koha::Patrons', |
219 |
value => { flags => 2**3 } # parameters flag = 2 |
220 |
} |
221 |
); |
222 |
my $password = 'thePassword123'; |
223 |
$librarian->set_password( { password => $password, skip_validation => 1 } ); |
224 |
my $userid = $librarian->userid; |
225 |
|
226 |
my $patron = $builder->build_object( |
227 |
{ |
228 |
class => 'Koha::Patrons', |
229 |
value => { flags => 0 } |
230 |
} |
231 |
); |
232 |
|
233 |
$patron->set_password( { password => $password, skip_validation => 1 } ); |
234 |
my $unauth_userid = $patron->userid; |
235 |
|
236 |
my $av_cat = $builder->build_object({ class => 'Koha::AuthorisedValueCategories' }); |
237 |
my $av = { |
238 |
category => $av_cat->category_name, |
239 |
value => "a value", |
240 |
description => "a lib", |
241 |
opac_description => "opac lib" |
242 |
}; |
243 |
|
244 |
# Unauthorized attempt to write |
245 |
$t->post_ok("//$unauth_userid:$password@/api/v1/authorised_values" => json => $av) |
246 |
->status_is(403); |
247 |
|
248 |
# Authorized attempt to write invalid data |
249 |
my $av_with_invalid_field = { |
250 |
blah => "Av Blah", |
251 |
category => $av_cat->category_name, |
252 |
value => "a value", |
253 |
description => "a lib", |
254 |
opac_description => "opac lib" |
255 |
}; |
256 |
|
257 |
$t->post_ok( "//$userid:$password@/api/v1/authorised_values" => json => $av_with_invalid_field ) |
258 |
->status_is(400) |
259 |
->json_is( |
260 |
"/errors" => [ |
261 |
{ |
262 |
message => "Properties not allowed: blah.", |
263 |
path => "/body" |
264 |
} |
265 |
] |
266 |
); |
267 |
|
268 |
# Authorized attempt to write |
269 |
my $av_id = |
270 |
$t->post_ok( "//$userid:$password@/api/v1/authorised_values" => json => $av ) |
271 |
->status_is( 201, 'SWAGGER3.2.1' ) |
272 |
->header_like( |
273 |
Location => qr|^\/api\/v1\/authorised_values/\d*|, |
274 |
'SWAGGER3.4.1' |
275 |
) |
276 |
->json_is( '/category' => $av->{category} ) |
277 |
->json_is( '/value' => $av->{value} ) |
278 |
->json_is( '/description' => $av->{description} ) |
279 |
->json_is( '/opac_description' => $av->{opac_description} ) |
280 |
->tx->res->json->{authorised_value_id}; |
281 |
|
282 |
# Authorized attempt to create with null id |
283 |
$av->{authorised_value_id} = undef; |
284 |
$t->post_ok( "//$userid:$password@/api/v1/authorised_values" => json => $av ) |
285 |
->status_is(400) |
286 |
->json_has('/errors'); |
287 |
|
288 |
# Authorized attempt to create with existing id |
289 |
$av->{authorised_value_id} = $av_id; |
290 |
$t->post_ok( "//$userid:$password@/api/v1/authorised_values" => json => $av ) |
291 |
->status_is(400) |
292 |
->json_is( |
293 |
"/errors" => [ |
294 |
{ |
295 |
message => "Read-only.", |
296 |
path => "/body/authorised_value_id" |
297 |
} |
298 |
] |
299 |
); |
300 |
|
301 |
$schema->storage->txn_rollback; |
302 |
}; |
303 |
|
304 |
subtest 'update() tests' => sub { |
305 |
|
306 |
plan tests => 15; |
307 |
|
308 |
$schema->storage->txn_begin; |
309 |
|
310 |
my $librarian = $builder->build_object( |
311 |
{ |
312 |
class => 'Koha::Patrons', |
313 |
value => { flags => 2**3 } # parameters flag = 2 |
314 |
} |
315 |
); |
316 |
my $password = 'thePassword123'; |
317 |
$librarian->set_password( { password => $password, skip_validation => 1 } ); |
318 |
my $userid = $librarian->userid; |
319 |
|
320 |
my $patron = $builder->build_object( |
321 |
{ |
322 |
class => 'Koha::Patrons', |
323 |
value => { flags => 0 } |
324 |
} |
325 |
); |
326 |
|
327 |
$patron->set_password( { password => $password, skip_validation => 1 } ); |
328 |
my $unauth_userid = $patron->userid; |
329 |
|
330 |
my $av_id = $builder->build_object({ class => 'Koha::AuthorisedValues' } )->id; |
331 |
|
332 |
# Unauthorized attempt to update |
333 |
$t->put_ok( "//$unauth_userid:$password@/api/v1/authorised_values/$av_id" => json => { name => 'New unauthorized name change' } ) |
334 |
->status_is(403); |
335 |
|
336 |
my $av_cat = $builder->build_object({ class => 'Koha::AuthorisedValueCategories' }); |
337 |
|
338 |
# Attempt partial update on a PUT |
339 |
my $av_with_missing_field = { |
340 |
category => $av_cat->category_name, |
341 |
value => "a value", |
342 |
}; |
343 |
|
344 |
$t->put_ok( "//$userid:$password@/api/v1/authorised_values/$av_id" => json => $av_with_missing_field ) |
345 |
->status_is(400) |
346 |
->json_is( "/errors" => |
347 |
[ { message => "Missing property.", path => "/body/description" } ] |
348 |
); |
349 |
|
350 |
# Full object update on PUT |
351 |
my $av_with_updated_field = { |
352 |
category => $av_cat->category_name, |
353 |
value => "a value", |
354 |
description => "a lib", |
355 |
}; |
356 |
|
357 |
$t->put_ok( "//$userid:$password@/api/v1/authorised_values/$av_id" => json => $av_with_updated_field ) |
358 |
->status_is(200) |
359 |
->json_is( '/description' => 'a lib' ); |
360 |
|
361 |
# Authorized attempt to write invalid data |
362 |
my $av_with_invalid_field = { |
363 |
blah => "Av Blah", |
364 |
category => $av_cat->category_name, |
365 |
value => "a value", |
366 |
description => "a lib", |
367 |
opac_description => "opac lib" |
368 |
}; |
369 |
|
370 |
$t->put_ok( "//$userid:$password@/api/v1/authorised_values/$av_id" => json => $av_with_invalid_field ) |
371 |
->status_is(400) |
372 |
->json_is( |
373 |
"/errors" => [ |
374 |
{ |
375 |
message => "Properties not allowed: blah.", |
376 |
path => "/body" |
377 |
} |
378 |
] |
379 |
); |
380 |
|
381 |
my $av_to_delete = $builder->build_object({ class => 'Koha::AuthorisedValues' }); |
382 |
my $non_existent_id = $av_to_delete->id; |
383 |
$av_to_delete->delete; |
384 |
|
385 |
$t->put_ok( "//$userid:$password@/api/v1/authorised_values/$non_existent_id" => json => $av_with_updated_field ) |
386 |
->status_is(404); |
387 |
|
388 |
# Wrong method (POST) |
389 |
$av_with_updated_field->{authorised_value_id} = 2; |
390 |
|
391 |
$t->post_ok( "//$userid:$password@/api/v1/authorised_values/$av_id" => json => $av_with_updated_field ) |
392 |
->status_is(404); |
393 |
|
394 |
$schema->storage->txn_rollback; |
395 |
}; |
396 |
|
397 |
subtest 'delete() tests' => sub { |
398 |
|
399 |
plan tests => 7; |
400 |
|
401 |
$schema->storage->txn_begin; |
402 |
|
403 |
my $librarian = $builder->build_object( |
404 |
{ |
405 |
class => 'Koha::Patrons', |
406 |
value => { flags => 2**3 } # parameters flag = 2 |
407 |
} |
408 |
); |
409 |
my $password = 'thePassword123'; |
410 |
$librarian->set_password( { password => $password, skip_validation => 1 } ); |
411 |
my $userid = $librarian->userid; |
412 |
|
413 |
my $patron = $builder->build_object( |
414 |
{ |
415 |
class => 'Koha::Patrons', |
416 |
value => { flags => 0 } |
417 |
} |
418 |
); |
419 |
|
420 |
$patron->set_password( { password => $password, skip_validation => 1 } ); |
421 |
my $unauth_userid = $patron->userid; |
422 |
|
423 |
my $av_id = $builder->build_object({ class => 'Koha::AuthorisedValues' })->id; |
424 |
|
425 |
# Unauthorized attempt to delete |
426 |
$t->delete_ok( "//$unauth_userid:$password@/api/v1/authorised_values/$av_id" ) |
427 |
->status_is(403); |
428 |
|
429 |
$t->delete_ok("//$userid:$password@/api/v1/authorised_values/$av_id") |
430 |
->status_is(204, 'SWAGGER3.2.4') |
431 |
->content_is('', 'SWAGGER3.3.4'); |
432 |
|
433 |
$t->delete_ok("//$userid:$password@/api/v1/authorised_values/$av_id") |
434 |
->status_is(404); |
435 |
|
436 |
$schema->storage->txn_rollback; |
437 |
}; |