|
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 JSON qw(encode_json); |
| 27 |
|
| 28 |
use Koha::Bookings; |
| 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::Bookings->search->delete; |
| 44 |
|
| 45 |
my $librarian = $builder->build_object( |
| 46 |
{ |
| 47 |
class => 'Koha::Patrons', |
| 48 |
value => { flags => 2**2 } # catalogue flag = 2 |
| 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 |
# Unauthorized access |
| 66 |
$t->get_ok("//$unauth_userid:$password@/api/v1/bookings")->status_is(403); |
| 67 |
|
| 68 |
## Authorized user tests |
| 69 |
# No bookings, so empty array should be returned |
| 70 |
$t->get_ok("//$userid:$password@/api/v1/bookings")->status_is(200)->json_is( [] ); |
| 71 |
|
| 72 |
# One booking |
| 73 |
my $start_0 = DateTime->now->subtract( days => 2 )->truncate( to => 'day' ); |
| 74 |
my $end_0 = DateTime->now->add( days => 4 )->truncate( to => 'day' ); |
| 75 |
my $booking_0 = $builder->build_object( |
| 76 |
{ |
| 77 |
class => 'Koha::Bookings', |
| 78 |
value => { |
| 79 |
start_date => $start_0, |
| 80 |
end_date => $end_0 |
| 81 |
} |
| 82 |
} |
| 83 |
); |
| 84 |
|
| 85 |
$t->get_ok("//$userid:$password@/api/v1/bookings")->status_is(200)->json_is( [ $booking_0->to_api ] ); |
| 86 |
|
| 87 |
# More bookings |
| 88 |
my $start_1 = DateTime->now->add( days => 1 )->truncate( to => 'day' ); |
| 89 |
my $end_1 = DateTime->now->add( days => 6 )->truncate( to => 'day' ); |
| 90 |
my $booking_1 = $builder->build_object( |
| 91 |
{ |
| 92 |
class => 'Koha::Bookings', |
| 93 |
value => { |
| 94 |
start_date => $start_1, |
| 95 |
end_date => $end_1 |
| 96 |
} |
| 97 |
} |
| 98 |
); |
| 99 |
|
| 100 |
my $start_2 = DateTime->now->add( days => 4 )->truncate( to => 'day' ); |
| 101 |
my $end_2 = DateTime->now->add( days => 8 )->truncate( to => 'day' ); |
| 102 |
my $booking_2 = $builder->build_object( |
| 103 |
{ |
| 104 |
class => 'Koha::Bookings', |
| 105 |
value => { |
| 106 |
start_date => $start_2, |
| 107 |
end_date => $end_2 |
| 108 |
} |
| 109 |
} |
| 110 |
); |
| 111 |
|
| 112 |
# No filtering |
| 113 |
$t->get_ok("//$userid:$password@/api/v1/bookings")->status_is(200)->json_is( |
| 114 |
'' => [ |
| 115 |
$booking_0->to_api, |
| 116 |
$booking_1->to_api, |
| 117 |
$booking_2->to_api |
| 118 |
], |
| 119 |
'unfiltered returns all bookings' |
| 120 |
); |
| 121 |
|
| 122 |
# Filtering works, two bookings after today |
| 123 |
my $api_filter = encode_json( { 'me.start_date' => { '>=' => DateTime->now->rfc3339 } } ); |
| 124 |
$t->get_ok("//$userid:$password@/api/v1/bookings?q=$api_filter")->status_is(200)->json_is( |
| 125 |
'' => [ |
| 126 |
$booking_1->to_api, |
| 127 |
$booking_2->to_api |
| 128 |
], |
| 129 |
'filtered returns two future bookings' |
| 130 |
); |
| 131 |
|
| 132 |
$api_filter = encode_json( { 'me.start_date' => { '<=' => DateTime->now->rfc3339 } } ); |
| 133 |
$t->get_ok("//$userid:$password@/api/v1/bookings?q=$api_filter")->status_is(200) |
| 134 |
->json_is( '' => [ $booking_0->to_api ], 'filtering to before today also works' ); |
| 135 |
|
| 136 |
# Warn on unsupported query parameter |
| 137 |
$t->get_ok("//$userid:$password@/api/v1/bookings?booking_blah=blah")->status_is(400) |
| 138 |
->json_is( [ { path => '/query/booking_blah', message => 'Malformed query string' } ] ); |
| 139 |
|
| 140 |
$schema->storage->txn_rollback; |
| 141 |
}; |
| 142 |
|
| 143 |
subtest 'get() tests' => sub { |
| 144 |
|
| 145 |
plan tests => 8; |
| 146 |
|
| 147 |
$schema->storage->txn_begin; |
| 148 |
|
| 149 |
my $booking = $builder->build_object( { class => 'Koha::Bookings' } ); |
| 150 |
my $librarian = $builder->build_object( |
| 151 |
{ |
| 152 |
class => 'Koha::Patrons', |
| 153 |
value => { flags => 2**2 } # catalogue flag = 2 |
| 154 |
} |
| 155 |
); |
| 156 |
my $password = 'thePassword123'; |
| 157 |
$librarian->set_password( { password => $password, skip_validation => 1 } ); |
| 158 |
my $userid = $librarian->userid; |
| 159 |
|
| 160 |
my $patron = $builder->build_object( |
| 161 |
{ |
| 162 |
class => 'Koha::Patrons', |
| 163 |
value => { flags => 0 } |
| 164 |
} |
| 165 |
); |
| 166 |
|
| 167 |
$patron->set_password( { password => $password, skip_validation => 1 } ); |
| 168 |
my $unauth_userid = $patron->userid; |
| 169 |
|
| 170 |
# Unauthorized access |
| 171 |
$t->get_ok( "//$unauth_userid:$password@/api/v1/bookings/" . $booking->booking_id )->status_is(403); |
| 172 |
|
| 173 |
# Authorized user tests |
| 174 |
$t->get_ok( "//$userid:$password@/api/v1/bookings/" . $booking->booking_id )->status_is(200) |
| 175 |
->json_is( $booking->to_api ); |
| 176 |
|
| 177 |
my $booking_to_delete = $builder->build_object( { class => 'Koha::Bookings' } ); |
| 178 |
my $non_existent_id = $booking_to_delete->id; |
| 179 |
$booking_to_delete->delete; |
| 180 |
|
| 181 |
$t->get_ok("//$userid:$password@/api/v1/bookings/$non_existent_id")->status_is(404) |
| 182 |
->json_is( '/error' => 'Booking not found' ); |
| 183 |
|
| 184 |
$schema->storage->txn_rollback; |
| 185 |
}; |
| 186 |
|
| 187 |
subtest 'add() tests' => sub { |
| 188 |
|
| 189 |
plan tests => 15; |
| 190 |
|
| 191 |
$schema->storage->txn_begin; |
| 192 |
|
| 193 |
my $librarian = $builder->build_object( |
| 194 |
{ |
| 195 |
class => 'Koha::Patrons', |
| 196 |
value => { flags => 2**1 } # circulate flag = 1 |
| 197 |
} |
| 198 |
); |
| 199 |
my $password = 'thePassword123'; |
| 200 |
$librarian->set_password( { password => $password, skip_validation => 1 } ); |
| 201 |
my $userid = $librarian->userid; |
| 202 |
|
| 203 |
my $patron = $builder->build_object( |
| 204 |
{ |
| 205 |
class => 'Koha::Patrons', |
| 206 |
value => { flags => 0 } |
| 207 |
} |
| 208 |
); |
| 209 |
|
| 210 |
$patron->set_password( { password => $password, skip_validation => 1 } ); |
| 211 |
my $unauth_userid = $patron->userid; |
| 212 |
|
| 213 |
my $biblio = $builder->build_sample_biblio; |
| 214 |
my $item = $builder->build_sample_item( { bookable => 1, biblionumber => $biblio->id } ); |
| 215 |
my $booking = { |
| 216 |
biblio_id => $biblio->id, |
| 217 |
item_id => undef, |
| 218 |
patron_id => $patron->id, |
| 219 |
start_date => DateTime->now->add( days => 2 )->rfc3339, |
| 220 |
end_date => DateTime->now->add( days => 6 )->rfc3339, |
| 221 |
}; |
| 222 |
|
| 223 |
# Unauthorized attempt to write |
| 224 |
$t->post_ok( "//$unauth_userid:$password@/api/v1/bookings" => json => $booking )->status_is(403); |
| 225 |
|
| 226 |
# Authorized attempt to write invalid data |
| 227 |
my $booking_with_invalid_field = { %{$booking}, blah => 'some stuff' }; |
| 228 |
|
| 229 |
$t->post_ok( "//$userid:$password@/api/v1/bookings" => json => $booking_with_invalid_field )->status_is(400) |
| 230 |
->json_is( |
| 231 |
"/errors" => [ |
| 232 |
{ |
| 233 |
message => "Properties not allowed: blah.", |
| 234 |
path => "/body" |
| 235 |
} |
| 236 |
] |
| 237 |
); |
| 238 |
|
| 239 |
# Authorized attempt to write |
| 240 |
my $booking_id = |
| 241 |
$t->post_ok( "//$userid:$password@/api/v1/bookings" => json => $booking )->status_is( 201, 'SWAGGER3.2.1' ) |
| 242 |
->header_like( Location => qr|^\/api\/v1\/bookings/\d*|, 'SWAGGER3.4.1' ) |
| 243 |
->json_is( '/biblio_id' => $biblio->id )->tx->res->json->{booking_id}; |
| 244 |
|
| 245 |
# Authorized attempt to create with null id |
| 246 |
$booking->{booking_id} = undef; |
| 247 |
$t->post_ok( "//$userid:$password@/api/v1/bookings" => json => $booking )->status_is(400)->json_has('/errors'); |
| 248 |
|
| 249 |
# Authorized attempt to create with existing id |
| 250 |
$booking->{booking_id} = $booking_id; |
| 251 |
$t->post_ok( "//$userid:$password@/api/v1/bookings" => json => $booking )->status_is(400)->json_is( |
| 252 |
"/errors" => [ |
| 253 |
{ |
| 254 |
message => "Read-only.", |
| 255 |
path => "/body/booking_id" |
| 256 |
} |
| 257 |
] |
| 258 |
); |
| 259 |
|
| 260 |
# TODO: Test bookings clashes |
| 261 |
# TODO: Test item auto-assignment |
| 262 |
|
| 263 |
$schema->storage->txn_rollback; |
| 264 |
}; |
| 265 |
|
| 266 |
subtest 'update() tests' => sub { |
| 267 |
|
| 268 |
plan tests => 15; |
| 269 |
|
| 270 |
$schema->storage->txn_begin; |
| 271 |
|
| 272 |
my $librarian = $builder->build_object( |
| 273 |
{ |
| 274 |
class => 'Koha::Patrons', |
| 275 |
value => { flags => 2**1 } # circulate flag = 1 |
| 276 |
} |
| 277 |
); |
| 278 |
my $password = 'thePassword123'; |
| 279 |
$librarian->set_password( { password => $password, skip_validation => 1 } ); |
| 280 |
my $userid = $librarian->userid; |
| 281 |
|
| 282 |
my $patron = $builder->build_object( |
| 283 |
{ |
| 284 |
class => 'Koha::Patrons', |
| 285 |
value => { flags => 0 } |
| 286 |
} |
| 287 |
); |
| 288 |
|
| 289 |
$patron->set_password( { password => $password, skip_validation => 1 } ); |
| 290 |
my $unauth_userid = $patron->userid; |
| 291 |
|
| 292 |
my $booking_id = $builder->build_object( { class => 'Koha::Bookings' } )->id; |
| 293 |
|
| 294 |
# Unauthorized attempt to update |
| 295 |
$t->put_ok( |
| 296 |
"//$unauth_userid:$password@/api/v1/bookings/$booking_id" => json => { name => 'New unauthorized name change' } |
| 297 |
)->status_is(403); |
| 298 |
|
| 299 |
my $biblio = $builder->build_sample_biblio; |
| 300 |
my $item = $builder->build_sample_item( { bookable => 1, biblionumber => $biblio->id } ); |
| 301 |
|
| 302 |
# Attempt partial update on a PUT |
| 303 |
my $booking_with_missing_field = { |
| 304 |
item_id => undef, |
| 305 |
patron_id => $patron->id, |
| 306 |
start_date => DateTime->now->add( days => 2 )->rfc3339, |
| 307 |
end_date => DateTime->now->add( days => 6 )->rfc3339, |
| 308 |
}; |
| 309 |
|
| 310 |
$t->put_ok( "//$userid:$password@/api/v1/bookings/$booking_id" => json => $booking_with_missing_field ) |
| 311 |
->status_is(400)->json_is( "/errors" => [ { message => "Missing property.", path => "/body/biblio_id" } ] ); |
| 312 |
|
| 313 |
# Full object update on PUT |
| 314 |
my $booking_with_updated_field = { |
| 315 |
biblio_id => $biblio->id, |
| 316 |
item_id => undef, |
| 317 |
patron_id => $patron->id, |
| 318 |
start_date => DateTime->now->add( days => 2 )->rfc3339, |
| 319 |
end_date => DateTime->now->add( days => 6 )->rfc3339, |
| 320 |
}; |
| 321 |
|
| 322 |
$t->put_ok( "//$userid:$password@/api/v1/bookings/$booking_id" => json => $booking_with_updated_field ) |
| 323 |
->status_is(200)->json_is( '/biblio_id' => $biblio->id ); |
| 324 |
|
| 325 |
# Authorized attempt to write invalid data |
| 326 |
my $booking_with_invalid_field = { |
| 327 |
blah => "Booking Blah", |
| 328 |
biblio_id => $biblio->id, |
| 329 |
item_id => undef, |
| 330 |
patron_id => $patron->id, |
| 331 |
start_date => DateTime->now->add( days => 2 )->rfc3339, |
| 332 |
end_date => DateTime->now->add( days => 6 )->rfc3339, |
| 333 |
}; |
| 334 |
|
| 335 |
$t->put_ok( "//$userid:$password@/api/v1/bookings/$booking_id" => json => $booking_with_invalid_field ) |
| 336 |
->status_is(400)->json_is( |
| 337 |
"/errors" => [ |
| 338 |
{ |
| 339 |
message => "Properties not allowed: blah.", |
| 340 |
path => "/body" |
| 341 |
} |
| 342 |
] |
| 343 |
); |
| 344 |
|
| 345 |
my $booking_to_delete = $builder->build_object( { class => 'Koha::Bookings' } ); |
| 346 |
my $non_existent_id = $booking_to_delete->id; |
| 347 |
$booking_to_delete->delete; |
| 348 |
|
| 349 |
$t->put_ok( "//$userid:$password@/api/v1/bookings/$non_existent_id" => json => $booking_with_updated_field ) |
| 350 |
->status_is(404); |
| 351 |
|
| 352 |
# TODO: Test bookings clashes |
| 353 |
# TODO: Test item auto-assignment |
| 354 |
|
| 355 |
# Wrong method (POST) |
| 356 |
$booking_with_updated_field->{booking_id} = 2; |
| 357 |
|
| 358 |
$t->post_ok( "//$userid:$password@/api/v1/bookings/$booking_id" => json => $booking_with_updated_field ) |
| 359 |
->status_is(404); |
| 360 |
|
| 361 |
$schema->storage->txn_rollback; |
| 362 |
}; |
| 363 |
|
| 364 |
subtest 'delete() tests' => sub { |
| 365 |
|
| 366 |
plan tests => 7; |
| 367 |
|
| 368 |
$schema->storage->txn_begin; |
| 369 |
|
| 370 |
my $librarian = $builder->build_object( |
| 371 |
{ |
| 372 |
class => 'Koha::Patrons', |
| 373 |
value => { flags => 2**1 } # circulate flag = 1 |
| 374 |
} |
| 375 |
); |
| 376 |
my $password = 'thePassword123'; |
| 377 |
$librarian->set_password( { password => $password, skip_validation => 1 } ); |
| 378 |
my $userid = $librarian->userid; |
| 379 |
|
| 380 |
my $patron = $builder->build_object( |
| 381 |
{ |
| 382 |
class => 'Koha::Patrons', |
| 383 |
value => { flags => 0 } |
| 384 |
} |
| 385 |
); |
| 386 |
|
| 387 |
$patron->set_password( { password => $password, skip_validation => 1 } ); |
| 388 |
my $unauth_userid = $patron->userid; |
| 389 |
|
| 390 |
my $booking_id = $builder->build_object( { class => 'Koha::Bookings' } )->id; |
| 391 |
|
| 392 |
# Unauthorized attempt to delete |
| 393 |
$t->delete_ok("//$unauth_userid:$password@/api/v1/bookings/$booking_id")->status_is(403); |
| 394 |
|
| 395 |
$t->delete_ok("//$userid:$password@/api/v1/bookings/$booking_id")->status_is( 204, 'SWAGGER3.2.4' ) |
| 396 |
->content_is( '', 'SWAGGER3.3.4' ); |
| 397 |
|
| 398 |
$t->delete_ok("//$userid:$password@/api/v1/bookings/$booking_id")->status_is(404); |
| 399 |
|
| 400 |
$schema->storage->txn_rollback; |
| 401 |
}; |