|
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::AuthorisedValues; |
| 27 |
use Koha::Database; |
| 28 |
|
| 29 |
my $schema = Koha::Database->new->schema; |
| 30 |
my $builder = t::lib::TestBuilder->new; |
| 31 |
|
| 32 |
my $t = Test::Mojo->new('Koha::REST::V1'); |
| 33 |
t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 ); |
| 34 |
|
| 35 |
subtest 'list() tests' => sub { |
| 36 |
|
| 37 |
plan tests => 20; |
| 38 |
|
| 39 |
$schema->storage->txn_begin; |
| 40 |
|
| 41 |
Koha::AuthorisedValues->search->delete; |
| 42 |
|
| 43 |
my $librarian = $builder->build_object( |
| 44 |
{ |
| 45 |
class => 'Koha::Patrons', |
| 46 |
value => { flags => 2 ** 2 } # catalogue flag = 2 |
| 47 |
} |
| 48 |
); |
| 49 |
my $password = 'thePassword123'; |
| 50 |
$librarian->set_password( { password => $password, skip_validation => 1 } ); |
| 51 |
my $userid = $librarian->userid; |
| 52 |
|
| 53 |
my $patron = $builder->build_object( |
| 54 |
{ |
| 55 |
class => 'Koha::Patrons', |
| 56 |
value => { flags => 0 } |
| 57 |
} |
| 58 |
); |
| 59 |
|
| 60 |
$patron->set_password( { password => $password, skip_validation => 1 } ); |
| 61 |
my $unauth_userid = $patron->userid; |
| 62 |
|
| 63 |
## Authorized user tests |
| 64 |
# No AVs, so empty array should be returned |
| 65 |
$t->get_ok("//$userid:$password@/api/v1/authorised_values") |
| 66 |
->status_is(200) |
| 67 |
->json_is( [] ); |
| 68 |
|
| 69 |
my $av = $builder->build_object({ class => 'Koha::AuthorisedValues' }); |
| 70 |
|
| 71 |
# One av created, should get returned |
| 72 |
$t->get_ok("//$userid:$password@/api/v1/authorised_values") |
| 73 |
->status_is(200) |
| 74 |
->json_is( [$av->to_api] ); |
| 75 |
|
| 76 |
my $another_av = $builder->build_object( |
| 77 |
{ class => 'Koha::AuthorisedValues', value => { lib => $av->lib } } ); |
| 78 |
my $av_with_another_lib = $builder->build_object({ class => 'Koha::AuthorisedValues' }); |
| 79 |
|
| 80 |
# Two avs created, they should both be returned |
| 81 |
$t->get_ok("//$userid:$password@/api/v1/authorised_values") |
| 82 |
->status_is(200) |
| 83 |
->json_is([$av->to_api, |
| 84 |
$another_av->to_api, |
| 85 |
$av_with_another_lib->to_api |
| 86 |
] ); |
| 87 |
|
| 88 |
# Filtering works, two avs sharing lib |
| 89 |
$t->get_ok("//$userid:$password@/api/v1/authorised_values?description=" . $av->lib ) |
| 90 |
->status_is(200) |
| 91 |
->json_is([ $av->to_api, |
| 92 |
$another_av->to_api |
| 93 |
]); |
| 94 |
|
| 95 |
$t->get_ok("//$userid:$password@/api/v1/authorised_values?value=" . $av->authorised_value ) |
| 96 |
->status_is(200) |
| 97 |
->json_is( [$av->to_api] ); |
| 98 |
|
| 99 |
# Warn on unsupported query parameter |
| 100 |
$t->get_ok("//$userid:$password@/api/v1/authorised_values?av_blah=blah" ) |
| 101 |
->status_is(400) |
| 102 |
->json_is( [{ path => '/query/av_blah', message => 'Malformed query string'}] ); |
| 103 |
|
| 104 |
# Unauthorized access |
| 105 |
$t->get_ok("//$unauth_userid:$password@/api/v1/authorised_values") |
| 106 |
->status_is(403); |
| 107 |
|
| 108 |
$schema->storage->txn_rollback; |
| 109 |
}; |
| 110 |
|
| 111 |
subtest 'get() tests' => sub { |
| 112 |
|
| 113 |
plan tests => 8; |
| 114 |
|
| 115 |
$schema->storage->txn_begin; |
| 116 |
|
| 117 |
my $av = $builder->build_object({ class => 'Koha::AuthorisedValues' }); |
| 118 |
my $librarian = $builder->build_object( |
| 119 |
{ |
| 120 |
class => 'Koha::Patrons', |
| 121 |
value => { flags => 2**2 } # catalogue flag = 2 |
| 122 |
} |
| 123 |
); |
| 124 |
my $password = 'thePassword123'; |
| 125 |
$librarian->set_password( { password => $password, skip_validation => 1 } ); |
| 126 |
my $userid = $librarian->userid; |
| 127 |
|
| 128 |
my $patron = $builder->build_object( |
| 129 |
{ |
| 130 |
class => 'Koha::Patrons', |
| 131 |
value => { flags => 0 } |
| 132 |
} |
| 133 |
); |
| 134 |
|
| 135 |
$patron->set_password( { password => $password, skip_validation => 1 } ); |
| 136 |
my $unauth_userid = $patron->userid; |
| 137 |
|
| 138 |
$t->get_ok( "//$userid:$password@/api/v1/authorised_values/" . $av->id ) |
| 139 |
->status_is(200) |
| 140 |
->json_is($av->to_api); |
| 141 |
|
| 142 |
$t->get_ok( "//$unauth_userid:$password@/api/v1/authorised_values/" . $av->id ) |
| 143 |
->status_is(403); |
| 144 |
|
| 145 |
my $av_to_delete = $builder->build_object({ class => 'Koha::AuthorisedValues' }); |
| 146 |
my $non_existent_id = $av_to_delete->id; |
| 147 |
$av_to_delete->delete; |
| 148 |
|
| 149 |
$t->get_ok( "//$userid:$password@/api/v1/authorised_values/$non_existent_id" ) |
| 150 |
->status_is(404) |
| 151 |
->json_is( '/error' => 'Authorised value not found' ); |
| 152 |
|
| 153 |
$schema->storage->txn_rollback; |
| 154 |
}; |
| 155 |
|
| 156 |
subtest 'add() tests' => sub { |
| 157 |
|
| 158 |
plan tests => 18; |
| 159 |
|
| 160 |
$schema->storage->txn_begin; |
| 161 |
|
| 162 |
my $librarian = $builder->build_object( |
| 163 |
{ |
| 164 |
class => 'Koha::Patrons', |
| 165 |
value => { flags => 2**3 } # parameters flag = 2 |
| 166 |
} |
| 167 |
); |
| 168 |
my $password = 'thePassword123'; |
| 169 |
$librarian->set_password( { password => $password, skip_validation => 1 } ); |
| 170 |
my $userid = $librarian->userid; |
| 171 |
|
| 172 |
my $patron = $builder->build_object( |
| 173 |
{ |
| 174 |
class => 'Koha::Patrons', |
| 175 |
value => { flags => 0 } |
| 176 |
} |
| 177 |
); |
| 178 |
|
| 179 |
$patron->set_password( { password => $password, skip_validation => 1 } ); |
| 180 |
my $unauth_userid = $patron->userid; |
| 181 |
|
| 182 |
my $av_cat = $builder->build_object({ class => 'Koha::AuthorisedValueCategories' }); |
| 183 |
my $av = { |
| 184 |
category => $av_cat->category_name, |
| 185 |
value => "a value", |
| 186 |
description => "a lib", |
| 187 |
opac_description => "opac lib" |
| 188 |
}; |
| 189 |
|
| 190 |
# Unauthorized attempt to write |
| 191 |
$t->post_ok("//$unauth_userid:$password@/api/v1/authorised_values" => json => $av) |
| 192 |
->status_is(403); |
| 193 |
|
| 194 |
# Authorized attempt to write invalid data |
| 195 |
my $av_with_invalid_field = { |
| 196 |
blah => "Av Blah", |
| 197 |
category => $av_cat->category_name, |
| 198 |
value => "a value", |
| 199 |
description => "a lib", |
| 200 |
opac_description => "opac lib" |
| 201 |
}; |
| 202 |
|
| 203 |
$t->post_ok( "//$userid:$password@/api/v1/authorised_values" => json => $av_with_invalid_field ) |
| 204 |
->status_is(400) |
| 205 |
->json_is( |
| 206 |
"/errors" => [ |
| 207 |
{ |
| 208 |
message => "Properties not allowed: blah.", |
| 209 |
path => "/body" |
| 210 |
} |
| 211 |
] |
| 212 |
); |
| 213 |
|
| 214 |
# Authorized attempt to write |
| 215 |
my $av_id = |
| 216 |
$t->post_ok( "//$userid:$password@/api/v1/authorised_values" => json => $av ) |
| 217 |
->status_is( 201, 'SWAGGER3.2.1' ) |
| 218 |
->header_like( |
| 219 |
Location => qr|^\/api\/v1\/authorised_values/\d*|, |
| 220 |
'SWAGGER3.4.1' |
| 221 |
) |
| 222 |
->json_is( '/category' => $av->{category} ) |
| 223 |
->json_is( '/value' => $av->{value} ) |
| 224 |
->json_is( '/description' => $av->{description} ) |
| 225 |
->json_is( '/opac_description' => $av->{opac_description} ) |
| 226 |
->tx->res->json->{authorised_value_id}; |
| 227 |
|
| 228 |
# Authorized attempt to create with null id |
| 229 |
$av->{authorised_value_id} = undef; |
| 230 |
$t->post_ok( "//$userid:$password@/api/v1/authorised_values" => json => $av ) |
| 231 |
->status_is(400) |
| 232 |
->json_has('/errors'); |
| 233 |
|
| 234 |
# Authorized attempt to create with existing id |
| 235 |
$av->{authorised_value_id} = $av_id; |
| 236 |
$t->post_ok( "//$userid:$password@/api/v1/authorised_values" => json => $av ) |
| 237 |
->status_is(400) |
| 238 |
->json_is( |
| 239 |
"/errors" => [ |
| 240 |
{ |
| 241 |
message => "Read-only.", |
| 242 |
path => "/body/authorised_value_id" |
| 243 |
} |
| 244 |
] |
| 245 |
); |
| 246 |
|
| 247 |
$schema->storage->txn_rollback; |
| 248 |
}; |
| 249 |
|
| 250 |
subtest 'update() tests' => sub { |
| 251 |
|
| 252 |
plan tests => 15; |
| 253 |
|
| 254 |
$schema->storage->txn_begin; |
| 255 |
|
| 256 |
my $librarian = $builder->build_object( |
| 257 |
{ |
| 258 |
class => 'Koha::Patrons', |
| 259 |
value => { flags => 2**3 } # parameters flag = 2 |
| 260 |
} |
| 261 |
); |
| 262 |
my $password = 'thePassword123'; |
| 263 |
$librarian->set_password( { password => $password, skip_validation => 1 } ); |
| 264 |
my $userid = $librarian->userid; |
| 265 |
|
| 266 |
my $patron = $builder->build_object( |
| 267 |
{ |
| 268 |
class => 'Koha::Patrons', |
| 269 |
value => { flags => 0 } |
| 270 |
} |
| 271 |
); |
| 272 |
|
| 273 |
$patron->set_password( { password => $password, skip_validation => 1 } ); |
| 274 |
my $unauth_userid = $patron->userid; |
| 275 |
|
| 276 |
my $av_id = $builder->build_object({ class => 'Koha::AuthorisedValues' } )->id; |
| 277 |
|
| 278 |
# Unauthorized attempt to update |
| 279 |
$t->put_ok( "//$unauth_userid:$password@/api/v1/authorised_values/$av_id" => json => { name => 'New unauthorized name change' } ) |
| 280 |
->status_is(403); |
| 281 |
|
| 282 |
my $av_cat = $builder->build_object({ class => 'Koha::AuthorisedValueCategories' }); |
| 283 |
|
| 284 |
# Attempt partial update on a PUT |
| 285 |
my $av_with_missing_field = { |
| 286 |
category => $av_cat->category_name, |
| 287 |
value => "a value", |
| 288 |
}; |
| 289 |
|
| 290 |
$t->put_ok( "//$userid:$password@/api/v1/authorised_values/$av_id" => json => $av_with_missing_field ) |
| 291 |
->status_is(400) |
| 292 |
->json_is( "/errors" => |
| 293 |
[ { message => "Missing property.", path => "/body/description" } ] |
| 294 |
); |
| 295 |
|
| 296 |
# Full object update on PUT |
| 297 |
my $av_with_updated_field = { |
| 298 |
category => $av_cat->category_name, |
| 299 |
value => "a value", |
| 300 |
description => "a lib", |
| 301 |
}; |
| 302 |
|
| 303 |
$t->put_ok( "//$userid:$password@/api/v1/authorised_values/$av_id" => json => $av_with_updated_field ) |
| 304 |
->status_is(200) |
| 305 |
->json_is( '/description' => 'a lib' ); |
| 306 |
|
| 307 |
# Authorized attempt to write invalid data |
| 308 |
my $av_with_invalid_field = { |
| 309 |
blah => "Av Blah", |
| 310 |
category => $av_cat->category_name, |
| 311 |
value => "a value", |
| 312 |
description => "a lib", |
| 313 |
opac_description => "opac lib" |
| 314 |
}; |
| 315 |
|
| 316 |
$t->put_ok( "//$userid:$password@/api/v1/authorised_values/$av_id" => json => $av_with_invalid_field ) |
| 317 |
->status_is(400) |
| 318 |
->json_is( |
| 319 |
"/errors" => [ |
| 320 |
{ |
| 321 |
message => "Properties not allowed: blah.", |
| 322 |
path => "/body" |
| 323 |
} |
| 324 |
] |
| 325 |
); |
| 326 |
|
| 327 |
my $av_to_delete = $builder->build_object({ class => 'Koha::AuthorisedValues' }); |
| 328 |
my $non_existent_id = $av_to_delete->id; |
| 329 |
$av_to_delete->delete; |
| 330 |
|
| 331 |
$t->put_ok( "//$userid:$password@/api/v1/authorised_values/$non_existent_id" => json => $av_with_updated_field ) |
| 332 |
->status_is(404); |
| 333 |
|
| 334 |
# Wrong method (POST) |
| 335 |
$av_with_updated_field->{authorised_value_id} = 2; |
| 336 |
|
| 337 |
$t->post_ok( "//$userid:$password@/api/v1/authorised_values/$av_id" => json => $av_with_updated_field ) |
| 338 |
->status_is(404); |
| 339 |
|
| 340 |
$schema->storage->txn_rollback; |
| 341 |
}; |
| 342 |
|
| 343 |
subtest 'delete() tests' => sub { |
| 344 |
|
| 345 |
plan tests => 7; |
| 346 |
|
| 347 |
$schema->storage->txn_begin; |
| 348 |
|
| 349 |
my $librarian = $builder->build_object( |
| 350 |
{ |
| 351 |
class => 'Koha::Patrons', |
| 352 |
value => { flags => 2**3 } # parameters flag = 2 |
| 353 |
} |
| 354 |
); |
| 355 |
my $password = 'thePassword123'; |
| 356 |
$librarian->set_password( { password => $password, skip_validation => 1 } ); |
| 357 |
my $userid = $librarian->userid; |
| 358 |
|
| 359 |
my $patron = $builder->build_object( |
| 360 |
{ |
| 361 |
class => 'Koha::Patrons', |
| 362 |
value => { flags => 0 } |
| 363 |
} |
| 364 |
); |
| 365 |
|
| 366 |
$patron->set_password( { password => $password, skip_validation => 1 } ); |
| 367 |
my $unauth_userid = $patron->userid; |
| 368 |
|
| 369 |
my $av_id = $builder->build_object({ class => 'Koha::AuthorisedValues' })->id; |
| 370 |
|
| 371 |
# Unauthorized attempt to delete |
| 372 |
$t->delete_ok( "//$unauth_userid:$password@/api/v1/authorised_values/$av_id" ) |
| 373 |
->status_is(403); |
| 374 |
|
| 375 |
$t->delete_ok("//$userid:$password@/api/v1/authorised_values/$av_id") |
| 376 |
->status_is(204, 'SWAGGER3.2.4') |
| 377 |
->content_is('', 'SWAGGER3.3.4'); |
| 378 |
|
| 379 |
$t->delete_ok("//$userid:$password@/api/v1/authorised_values/$av_id") |
| 380 |
->status_is(404); |
| 381 |
|
| 382 |
$schema->storage->txn_rollback; |
| 383 |
}; |