|
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::NoWarnings; |
| 21 |
use Test::More tests => 6; |
| 22 |
use Test::Mojo; |
| 23 |
use JSON; |
| 24 |
|
| 25 |
use t::lib::TestBuilder; |
| 26 |
use t::lib::Mocks; |
| 27 |
|
| 28 |
use Koha::DisplayItems; |
| 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 => 20; |
| 40 |
|
| 41 |
$schema->storage->txn_begin; |
| 42 |
|
| 43 |
Koha::DisplayItems->search->delete; |
| 44 |
|
| 45 |
my $librarian = $builder->build_object( |
| 46 |
{ |
| 47 |
class => 'Koha::Patrons', |
| 48 |
value => { flags => 1 } # displays permission |
| 49 |
} |
| 50 |
); |
| 51 |
my $password = 'thePassword123'; |
| 52 |
$librarian->set_password( { password => $password, skip_validation => 1 } ); |
| 53 |
my $userid = $librarian->userid; |
| 54 |
|
| 55 |
my $patron = $builder->build_object( |
| 56 |
{ |
| 57 |
class => 'Koha::Patrons', |
| 58 |
value => { flags => 0 } |
| 59 |
} |
| 60 |
); |
| 61 |
|
| 62 |
$patron->set_password( { password => $password, skip_validation => 1 } ); |
| 63 |
my $unauth_userid = $patron->userid; |
| 64 |
|
| 65 |
## Authorized user tests |
| 66 |
# No displayitems, so empty array should be returned |
| 67 |
$t->get_ok("//$userid:$password@/api/v1/display/items")->status_is(200)->json_is( [] ); |
| 68 |
|
| 69 |
# Create test data |
| 70 |
my $display = $builder->build_object( { class => 'Koha::Displays' } ); |
| 71 |
my $biblio = $builder->build_sample_biblio(); |
| 72 |
my $item = $builder->build_sample_item( { biblionumber => $biblio->biblionumber } ); |
| 73 |
|
| 74 |
my $displayitem = $builder->build_object( |
| 75 |
{ |
| 76 |
class => 'Koha::DisplayItems', |
| 77 |
value => { |
| 78 |
display_id => $display->display_id, |
| 79 |
itemnumber => $item->itemnumber, |
| 80 |
biblionumber => $biblio->biblionumber |
| 81 |
} |
| 82 |
} |
| 83 |
); |
| 84 |
|
| 85 |
# One displayitem created, should get returned |
| 86 |
$t->get_ok("//$userid:$password@/api/v1/display/items")->status_is(200)->json_is( [ $displayitem->to_api ] ); |
| 87 |
|
| 88 |
# Create another displayitem with same display |
| 89 |
my $item2 = $builder->build_sample_item( { biblionumber => $biblio->biblionumber } ); |
| 90 |
my $another_displayitem = $builder->build_object( |
| 91 |
{ |
| 92 |
class => 'Koha::DisplayItems', |
| 93 |
value => { |
| 94 |
display_id => $display->display_id, |
| 95 |
itemnumber => $item2->itemnumber, |
| 96 |
biblionumber => $biblio->biblionumber |
| 97 |
} |
| 98 |
} |
| 99 |
); |
| 100 |
|
| 101 |
# Create displayitem with different display |
| 102 |
my $display2 = $builder->build_object( { class => 'Koha::Displays' } ); |
| 103 |
my $item3 = $builder->build_sample_item( { biblionumber => $biblio->biblionumber } ); |
| 104 |
my $displayitem_different_display = $builder->build_object( |
| 105 |
{ |
| 106 |
class => 'Koha::DisplayItems', |
| 107 |
value => { |
| 108 |
display_id => $display2->display_id, |
| 109 |
itemnumber => $item3->itemnumber, |
| 110 |
biblionumber => $biblio->biblionumber |
| 111 |
} |
| 112 |
} |
| 113 |
); |
| 114 |
|
| 115 |
# Three displayitems created, they should all be returned |
| 116 |
$t->get_ok("//$userid:$password@/api/v1/display/items")->status_is(200)->json_is( |
| 117 |
[ |
| 118 |
$displayitem->to_api, |
| 119 |
$another_displayitem->to_api, |
| 120 |
$displayitem_different_display->to_api |
| 121 |
] |
| 122 |
); |
| 123 |
|
| 124 |
# Filtering works, two displayitems sharing display_id |
| 125 |
$t->get_ok( "//$userid:$password@/api/v1/display/items?display_id=" . $display->display_id )->status_is(200) |
| 126 |
->json_is( |
| 127 |
[ |
| 128 |
$displayitem->to_api, |
| 129 |
$another_displayitem->to_api |
| 130 |
] |
| 131 |
); |
| 132 |
|
| 133 |
$t->get_ok( "//$userid:$password@/api/v1/display/items?itemnumber=" . $item->itemnumber )->status_is(200) |
| 134 |
->json_is( [ $displayitem->to_api ] ); |
| 135 |
|
| 136 |
# Warn on unsupported query parameter |
| 137 |
$t->get_ok("//$userid:$password@/api/v1/display/items?displayitem_blah=blah")->status_is(400) |
| 138 |
->json_is( [ { path => '/query/displayitem_blah', message => 'Malformed query string' } ] ); |
| 139 |
|
| 140 |
# Unauthorized access |
| 141 |
$t->get_ok("//$unauth_userid:$password@/api/v1/display/items")->status_is(403); |
| 142 |
|
| 143 |
$schema->storage->txn_rollback; |
| 144 |
}; |
| 145 |
|
| 146 |
subtest 'get() tests' => sub { |
| 147 |
|
| 148 |
plan tests => 11; |
| 149 |
|
| 150 |
$schema->storage->txn_begin; |
| 151 |
|
| 152 |
# Create test data |
| 153 |
my $display = $builder->build_object( { class => 'Koha::Displays' } ); |
| 154 |
my $biblio = $builder->build_sample_biblio(); |
| 155 |
my $item = $builder->build_sample_item( { biblionumber => $biblio->biblionumber } ); |
| 156 |
|
| 157 |
my $displayitem = $builder->build_object( |
| 158 |
{ |
| 159 |
class => 'Koha::DisplayItems', |
| 160 |
value => { |
| 161 |
display_id => $display->display_id, |
| 162 |
itemnumber => $item->itemnumber, |
| 163 |
biblionumber => $biblio->biblionumber |
| 164 |
} |
| 165 |
} |
| 166 |
); |
| 167 |
|
| 168 |
my $librarian = $builder->build_object( |
| 169 |
{ |
| 170 |
class => 'Koha::Patrons', |
| 171 |
value => { flags => 1 } # displays permission |
| 172 |
} |
| 173 |
); |
| 174 |
my $password = 'thePassword123'; |
| 175 |
$librarian->set_password( { password => $password, skip_validation => 1 } ); |
| 176 |
my $userid = $librarian->userid; |
| 177 |
|
| 178 |
my $patron = $builder->build_object( |
| 179 |
{ |
| 180 |
class => 'Koha::Patrons', |
| 181 |
value => { flags => 0 } |
| 182 |
} |
| 183 |
); |
| 184 |
|
| 185 |
$patron->set_password( { password => $password, skip_validation => 1 } ); |
| 186 |
my $unauth_userid = $patron->userid; |
| 187 |
|
| 188 |
$t->get_ok( |
| 189 |
"//$userid:$password@/api/v1/display/items/" . $displayitem->display_id . "/" . $displayitem->itemnumber ) |
| 190 |
->status_is(200)->json_is( $displayitem->to_api ); |
| 191 |
|
| 192 |
$t->get_ok( "//$unauth_userid:$password@/api/v1/display/items/" |
| 193 |
. $displayitem->display_id . "/" |
| 194 |
. $displayitem->itemnumber )->status_is(403); |
| 195 |
|
| 196 |
# Create displayitem to delete |
| 197 |
my $item_to_delete = $builder->build_sample_item( { biblionumber => $biblio->biblionumber } ); |
| 198 |
my $displayitem_to_delete = $builder->build_object( |
| 199 |
{ |
| 200 |
class => 'Koha::DisplayItems', |
| 201 |
value => { |
| 202 |
display_id => $display->display_id, |
| 203 |
itemnumber => $item_to_delete->itemnumber, |
| 204 |
biblionumber => $biblio->biblionumber |
| 205 |
} |
| 206 |
} |
| 207 |
); |
| 208 |
my $non_existent_display_id = $displayitem_to_delete->display_id; |
| 209 |
my $non_existent_item_id = $displayitem_to_delete->itemnumber; |
| 210 |
$displayitem_to_delete->delete; |
| 211 |
|
| 212 |
$t->get_ok("//$userid:$password@/api/v1/display/items/$non_existent_display_id/$non_existent_item_id") |
| 213 |
->status_is(404)->json_is( '/error' => 'Display item not found' ); |
| 214 |
|
| 215 |
# Test with completely non-existent IDs |
| 216 |
$t->get_ok("//$userid:$password@/api/v1/display/items/99999/99999")->status_is(404) |
| 217 |
->json_is( '/error' => 'Display item not found' ); |
| 218 |
|
| 219 |
$schema->storage->txn_rollback; |
| 220 |
}; |
| 221 |
|
| 222 |
subtest 'add() tests' => sub { |
| 223 |
|
| 224 |
plan tests => 17; |
| 225 |
|
| 226 |
$schema->storage->txn_begin; |
| 227 |
|
| 228 |
my $librarian = $builder->build_object( |
| 229 |
{ |
| 230 |
class => 'Koha::Patrons', |
| 231 |
value => { flags => 1 } # displays permission |
| 232 |
} |
| 233 |
); |
| 234 |
my $password = 'thePassword123'; |
| 235 |
$librarian->set_password( { password => $password, skip_validation => 1 } ); |
| 236 |
my $userid = $librarian->userid; |
| 237 |
|
| 238 |
my $patron = $builder->build_object( |
| 239 |
{ |
| 240 |
class => 'Koha::Patrons', |
| 241 |
value => { flags => 0 } |
| 242 |
} |
| 243 |
); |
| 244 |
|
| 245 |
$patron->set_password( { password => $password, skip_validation => 1 } ); |
| 246 |
my $unauth_userid = $patron->userid; |
| 247 |
|
| 248 |
# Create test data |
| 249 |
my $display = $builder->build_object( { class => 'Koha::Displays' } ); |
| 250 |
my $biblio = $builder->build_sample_biblio(); |
| 251 |
my $item = $builder->build_sample_item( { biblionumber => $biblio->biblionumber } ); |
| 252 |
|
| 253 |
my $displayitem = { |
| 254 |
display_id => $display->display_id, |
| 255 |
itemnumber => $item->itemnumber, |
| 256 |
biblionumber => $biblio->biblionumber, |
| 257 |
date_added => "2024-01-15", |
| 258 |
date_remove => "2024-02-15" |
| 259 |
}; |
| 260 |
|
| 261 |
# Unauthorized attempt to write |
| 262 |
$t->post_ok( "//$unauth_userid:$password@/api/v1/display/items" => json => $displayitem )->status_is(403); |
| 263 |
|
| 264 |
# Authorized attempt to write invalid data |
| 265 |
my $displayitem_with_invalid_field = { |
| 266 |
blah => "DisplayItem Blah", |
| 267 |
display_id => $display->display_id, |
| 268 |
itemnumber => $item->itemnumber, |
| 269 |
biblionumber => $biblio->biblionumber |
| 270 |
}; |
| 271 |
|
| 272 |
$t->post_ok( "//$userid:$password@/api/v1/display/items" => json => $displayitem_with_invalid_field ) |
| 273 |
->status_is(400)->json_is( |
| 274 |
"/errors" => [ |
| 275 |
{ |
| 276 |
message => "Properties not allowed: blah.", |
| 277 |
path => "/body" |
| 278 |
} |
| 279 |
] |
| 280 |
); |
| 281 |
|
| 282 |
# Authorized attempt to write |
| 283 |
$t->post_ok( "//$userid:$password@/api/v1/display/items" => json => $displayitem )->status_is( 201, 'REST3.2.1' ) |
| 284 |
->header_like( |
| 285 |
Location => qr|^\/api\/v1\/display/items/\d+/\d+|, |
| 286 |
'REST3.4.1' |
| 287 |
)->json_is( '/display_id' => $displayitem->{display_id} ) |
| 288 |
->json_is( '/itemnumber' => $displayitem->{itemnumber} ) |
| 289 |
->json_is( '/biblionumber' => $displayitem->{biblionumber} ) |
| 290 |
->json_is( '/date_added' => $displayitem->{date_added} ); |
| 291 |
|
| 292 |
# Test missing required field (display_id is required) |
| 293 |
my $displayitem_missing_field = { |
| 294 |
itemnumber => $item->itemnumber, |
| 295 |
biblionumber => $biblio->biblionumber |
| 296 |
|
| 297 |
# Missing display_id (required field) |
| 298 |
}; |
| 299 |
|
| 300 |
$t->post_ok( "//$userid:$password@/api/v1/display/items" => json => $displayitem_missing_field )->status_is(400) |
| 301 |
->json_has('/errors'); |
| 302 |
|
| 303 |
# Test successful creation with different item |
| 304 |
my $item2 = $builder->build_sample_item( { biblionumber => $biblio->biblionumber } ); |
| 305 |
my $displayitem2 = { |
| 306 |
display_id => $display->display_id, |
| 307 |
itemnumber => $item2->itemnumber, |
| 308 |
biblionumber => $biblio->biblionumber, |
| 309 |
date_added => "2024-01-16", |
| 310 |
date_remove => "2024-02-16" |
| 311 |
}; |
| 312 |
$t->post_ok( "//$userid:$password@/api/v1/display/items" => json => $displayitem2 )->status_is(201); |
| 313 |
|
| 314 |
$schema->storage->txn_rollback; |
| 315 |
}; |
| 316 |
|
| 317 |
subtest 'update() tests' => sub { |
| 318 |
|
| 319 |
plan tests => 15; |
| 320 |
|
| 321 |
$schema->storage->txn_begin; |
| 322 |
|
| 323 |
my $librarian = $builder->build_object( |
| 324 |
{ |
| 325 |
class => 'Koha::Patrons', |
| 326 |
value => { flags => 1 } # displays permission |
| 327 |
} |
| 328 |
); |
| 329 |
my $password = 'thePassword123'; |
| 330 |
$librarian->set_password( { password => $password, skip_validation => 1 } ); |
| 331 |
my $userid = $librarian->userid; |
| 332 |
|
| 333 |
my $patron = $builder->build_object( |
| 334 |
{ |
| 335 |
class => 'Koha::Patrons', |
| 336 |
value => { flags => 0 } |
| 337 |
} |
| 338 |
); |
| 339 |
|
| 340 |
$patron->set_password( { password => $password, skip_validation => 1 } ); |
| 341 |
my $unauth_userid = $patron->userid; |
| 342 |
|
| 343 |
# Create test data |
| 344 |
my $display = $builder->build_object( { class => 'Koha::Displays' } ); |
| 345 |
my $biblio = $builder->build_sample_biblio(); |
| 346 |
my $item = $builder->build_sample_item( { biblionumber => $biblio->biblionumber } ); |
| 347 |
|
| 348 |
my $displayitem = $builder->build_object( |
| 349 |
{ |
| 350 |
class => 'Koha::DisplayItems', |
| 351 |
value => { |
| 352 |
display_id => $display->display_id, |
| 353 |
itemnumber => $item->itemnumber, |
| 354 |
biblionumber => $biblio->biblionumber |
| 355 |
} |
| 356 |
} |
| 357 |
); |
| 358 |
|
| 359 |
# Unauthorized attempt to update |
| 360 |
$t->put_ok( "//$unauth_userid:$password@/api/v1/display/items/" |
| 361 |
. $displayitem->display_id . "/" |
| 362 |
. $displayitem->itemnumber => json => { date_added => '2024-01-20' } )->status_is(403); |
| 363 |
|
| 364 |
# Attempt partial update on a PUT (missing required field) |
| 365 |
my $displayitem_with_missing_field = { |
| 366 |
biblionumber => $biblio->biblionumber |
| 367 |
|
| 368 |
# Missing display_id and itemnumber (required fields) |
| 369 |
}; |
| 370 |
|
| 371 |
$t->put_ok( "//$userid:$password@/api/v1/display/items/" |
| 372 |
. $displayitem->display_id . "/" |
| 373 |
. $displayitem->itemnumber => json => $displayitem_with_missing_field )->status_is(400) |
| 374 |
->json_has("/errors"); |
| 375 |
|
| 376 |
# Full object update on PUT |
| 377 |
my $displayitem_with_updated_field = { |
| 378 |
display_id => $display->display_id, |
| 379 |
itemnumber => $item->itemnumber, |
| 380 |
biblionumber => $biblio->biblionumber, |
| 381 |
date_added => "2024-01-20", |
| 382 |
date_remove => "2024-03-20" |
| 383 |
}; |
| 384 |
|
| 385 |
$t->put_ok( "//$userid:$password@/api/v1/display/items/" |
| 386 |
. $displayitem->display_id . "/" |
| 387 |
. $displayitem->itemnumber => json => $displayitem_with_updated_field )->status_is(200) |
| 388 |
->json_is( '/date_added' => '2024-01-20' ); |
| 389 |
|
| 390 |
# Authorized attempt to write invalid data |
| 391 |
my $displayitem_with_invalid_field = { |
| 392 |
blah => "DisplayItem Blah", |
| 393 |
display_id => $display->display_id, |
| 394 |
itemnumber => $item->itemnumber, |
| 395 |
biblionumber => $biblio->biblionumber |
| 396 |
}; |
| 397 |
|
| 398 |
$t->put_ok( "//$userid:$password@/api/v1/display/items/" |
| 399 |
. $displayitem->display_id . "/" |
| 400 |
. $displayitem->itemnumber => json => $displayitem_with_invalid_field )->status_is(400)->json_is( |
| 401 |
"/errors" => [ |
| 402 |
{ |
| 403 |
message => "Properties not allowed: blah.", |
| 404 |
path => "/body" |
| 405 |
} |
| 406 |
] |
| 407 |
); |
| 408 |
|
| 409 |
# Test with non-existent displayitem |
| 410 |
$t->put_ok( "//$userid:$password@/api/v1/display/items/99999/99999" => json => $displayitem_with_updated_field ) |
| 411 |
->status_is(404); |
| 412 |
|
| 413 |
# Wrong method (POST) |
| 414 |
$t->post_ok( "//$userid:$password@/api/v1/display/items/" |
| 415 |
. $displayitem->display_id . "/" |
| 416 |
. $displayitem->itemnumber => json => $displayitem_with_updated_field )->status_is(404); |
| 417 |
|
| 418 |
$schema->storage->txn_rollback; |
| 419 |
}; |
| 420 |
|
| 421 |
subtest 'delete() tests' => sub { |
| 422 |
|
| 423 |
plan tests => 7; |
| 424 |
|
| 425 |
$schema->storage->txn_begin; |
| 426 |
|
| 427 |
my $librarian = $builder->build_object( |
| 428 |
{ |
| 429 |
class => 'Koha::Patrons', |
| 430 |
value => { flags => 1 } # displays permission |
| 431 |
} |
| 432 |
); |
| 433 |
my $password = 'thePassword123'; |
| 434 |
$librarian->set_password( { password => $password, skip_validation => 1 } ); |
| 435 |
my $userid = $librarian->userid; |
| 436 |
|
| 437 |
my $patron = $builder->build_object( |
| 438 |
{ |
| 439 |
class => 'Koha::Patrons', |
| 440 |
value => { flags => 0 } |
| 441 |
} |
| 442 |
); |
| 443 |
|
| 444 |
$patron->set_password( { password => $password, skip_validation => 1 } ); |
| 445 |
my $unauth_userid = $patron->userid; |
| 446 |
|
| 447 |
# Create test data |
| 448 |
my $display = $builder->build_object( { class => 'Koha::Displays' } ); |
| 449 |
my $biblio = $builder->build_sample_biblio(); |
| 450 |
my $item = $builder->build_sample_item( { biblionumber => $biblio->biblionumber } ); |
| 451 |
|
| 452 |
my $displayitem = $builder->build_object( |
| 453 |
{ |
| 454 |
class => 'Koha::DisplayItems', |
| 455 |
value => { |
| 456 |
display_id => $display->display_id, |
| 457 |
itemnumber => $item->itemnumber, |
| 458 |
biblionumber => $biblio->biblionumber |
| 459 |
} |
| 460 |
} |
| 461 |
); |
| 462 |
|
| 463 |
# Unauthorized attempt to delete |
| 464 |
$t->delete_ok( "//$unauth_userid:$password@/api/v1/display/items/" |
| 465 |
. $displayitem->display_id . "/" |
| 466 |
. $displayitem->itemnumber )->status_is(403); |
| 467 |
|
| 468 |
$t->delete_ok( |
| 469 |
"//$userid:$password@/api/v1/display/items/" . $displayitem->display_id . "/" . $displayitem->itemnumber ) |
| 470 |
->status_is( 204, 'REST3.2.4' )->content_is( '', 'REST3.3.4' ); |
| 471 |
|
| 472 |
$t->delete_ok( |
| 473 |
"//$userid:$password@/api/v1/display/items/" . $displayitem->display_id . "/" . $displayitem->itemnumber ) |
| 474 |
->status_is(404); |
| 475 |
|
| 476 |
$schema->storage->txn_rollback; |
| 477 |
}; |