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