|
Lines 225-252
$t->get_ok( "//$userid:$password@/api/v1/checkouts/" . $issue2->issue_id . "/all
Link Here
|
| 225 |
error => 'too_many' |
225 |
error => 'too_many' |
| 226 |
}); |
226 |
}); |
| 227 |
|
227 |
|
| 228 |
my $new_date_due = Koha::DateUtils::dt_from_string( $issue2->date_due ); |
|
|
| 229 |
$new_date_due->add(days => 2); |
| 230 |
$new_date_due = output_pref({ dateformat => "rfc3339", dt => $new_date_due }); |
| 231 |
|
| 232 |
$t->put_ok( "//$userid:$password@/api/v1/checkouts/" . $issue2->issue_id => json => { due_date => $new_date_due }) |
| 233 |
->status_is(200, 'Due date updated successfully') |
| 234 |
->json_is('/due_date' => $new_date_due); |
| 235 |
|
| 236 |
# Test for date handling for client differs from server locale |
| 237 |
$new_date_due = $date_due->clone->set_time_zone( 'Australia/Sydney' ); |
| 238 |
$new_date_due->add(days => 2); |
| 239 |
my $offset_rfc3339 = $new_date_due . get_offset_string($new_date_due->offset); |
| 240 |
my $offset_servertime = output_pref({ dateformat => "rfc3339", dt => $new_date_due }); |
| 241 |
$t->put_ok( "//$userid:$password@/api/v1/checkouts/" . $issue2->issue_id => json => { due_date => $offset_rfc3339}) |
| 242 |
->status_is(200, 'Due date updated successfully') |
| 243 |
->json_is('/due_date' => $offset_servertime); |
| 244 |
|
| 245 |
$t->put_ok( "//$userid:$password@/api/v1/checkouts/" . $issue2->issue_id => json => { item_id => 3 }) |
| 246 |
->status_is(400, 'readOnly properties not updateable'); |
| 247 |
|
| 248 |
$schema->storage->txn_rollback; |
228 |
$schema->storage->txn_rollback; |
| 249 |
|
229 |
|
|
|
230 |
subtest 'update() tests' => sub { |
| 231 |
|
| 232 |
plan tests => 2; |
| 233 |
|
| 234 |
$schema->storage->txn_begin; |
| 235 |
|
| 236 |
my $librarian = $builder->build_object( |
| 237 |
{ |
| 238 |
class => 'Koha::Patrons', |
| 239 |
value => { flags => 2 } |
| 240 |
} |
| 241 |
); |
| 242 |
my $password = 'thePassword123'; |
| 243 |
$librarian->set_password( { password => $password, skip_validation => 1 } ); |
| 244 |
my $userid = $librarian->userid; |
| 245 |
|
| 246 |
my $patron = $builder->build_object( |
| 247 |
{ |
| 248 |
class => 'Koha::Patrons', |
| 249 |
value => { flags => 0 } |
| 250 |
} |
| 251 |
); |
| 252 |
|
| 253 |
$patron->set_password( { password => $password, skip_validation => 1 } ); |
| 254 |
my $unauth_userid = $patron->userid; |
| 255 |
|
| 256 |
my $issue = $builder->build_object({ class => 'Koha::Checkouts' } ); |
| 257 |
my $issue_id = $issue->id; |
| 258 |
|
| 259 |
# Unauthorized attempt to update |
| 260 |
$t->put_ok( |
| 261 |
"//$unauth_userid:$password@/api/v1/checkouts/$issue_id" => json => |
| 262 |
{ borrowernumber => $issue->borrowernumber } )->status_is(403); |
| 263 |
|
| 264 |
# FIXME: The tests for partial update are inspired by the same for /cities.. |
| 265 |
# but they do not account for the readOnly nature of fields defined on this route. |
| 266 |
|
| 267 |
# Attempt partial update on a PUT |
| 268 |
my $checkout_with_missing_field = { |
| 269 |
borrowernumber => $issue->borrowernumber, |
| 270 |
itemnumber => $issue->itemnumber, |
| 271 |
library_id => $issue->branchcode, |
| 272 |
checkin_date => undef, |
| 273 |
last_renewed_date => undef, |
| 274 |
checkout_date => undef, |
| 275 |
note_date => undef, |
| 276 |
note_seen => undef |
| 277 |
}; |
| 278 |
|
| 279 |
$t->put_ok( "//$userid:$password@/api/v1/checkouts/$issue_id" => json => $checkout_with_missing_field ) |
| 280 |
->status_is(400) |
| 281 |
->json_is( "/errors" => |
| 282 |
[ { message => "Missing property.", path => "/body/due_date" } ] |
| 283 |
); |
| 284 |
|
| 285 |
# Full object update on PUT |
| 286 |
my $checkout_with_updated_field = { |
| 287 |
borrowernumber => $issue->borrowernumber, |
| 288 |
itemnumber => $issue->itemnumber, |
| 289 |
library_id => $issue->branchcode, |
| 290 |
due_date => $issue->date_due, |
| 291 |
checkin_date => $issue->returndate, |
| 292 |
last_renewed_date => $issue->lastreneweddate, |
| 293 |
checkout_date => $issue->issuedate, |
| 294 |
note_date => $issue->notedate, |
| 295 |
note_seen => $issue->noteseen |
| 296 |
}; |
| 297 |
|
| 298 |
$t->put_ok( "//$userid:$password@/api/v1/checkouts/$issue_id" => json => $checkout_with_updated_field ) |
| 299 |
->status_is(200) |
| 300 |
->json_is( '/name' => 'London' ); |
| 301 |
|
| 302 |
# Authorized attempt to write invalid data |
| 303 |
my $checkout_with_invalid_field = { |
| 304 |
blah => "Checkout Blah", |
| 305 |
state => "Checkout State", |
| 306 |
postal_code => "Checkout Zipcode", |
| 307 |
country => "Checkout Country" |
| 308 |
}; |
| 309 |
|
| 310 |
$t->put_ok( "//$userid:$password@/api/v1/checkouts/$issue_id" => json => $checkout_with_invalid_field ) |
| 311 |
->status_is(400) |
| 312 |
->json_is( |
| 313 |
"/errors" => [ |
| 314 |
{ |
| 315 |
message => "Properties not allowed: blah.", |
| 316 |
path => "/body" |
| 317 |
} |
| 318 |
] |
| 319 |
); |
| 320 |
|
| 321 |
my $checkout_to_delete = $builder->build_object({ class => 'Koha::Checkouts' }); |
| 322 |
my $non_existent_id = $checkout_to_delete->id; |
| 323 |
$checkout_to_delete->delete; |
| 324 |
|
| 325 |
$t->put_ok( "//$userid:$password@/api/v1/checkouts/$non_existent_id" => json => $checkout_with_updated_field ) |
| 326 |
->status_is(404); |
| 327 |
|
| 328 |
my $new_date_due = Koha::DateUtils::dt_from_string( $issue->date_due ); |
| 329 |
$new_date_due->add( days => 2 ); |
| 330 |
$new_date_due = |
| 331 |
output_pref( { dateformat => "rfc3339", dt => $new_date_due } ); |
| 332 |
|
| 333 |
$t->put_ok( "//$userid:$password@/api/v1/checkouts/" |
| 334 |
. $issue->issue_id => json => { due_date => $new_date_due } ) |
| 335 |
->status_is( 200, 'Due date updated successfully' ) |
| 336 |
->json_is( '/due_date' => $new_date_due ); |
| 337 |
|
| 338 |
# Test for date handling for client differs from server locale |
| 339 |
$new_date_due = $date_due->clone->set_time_zone('Australia/Sydney'); |
| 340 |
$new_date_due->add( days => 2 ); |
| 341 |
my $offset_rfc3339 = |
| 342 |
$new_date_due . get_offset_string( $new_date_due->offset ); |
| 343 |
my $offset_servertime = |
| 344 |
output_pref( { dateformat => "rfc3339", dt => $new_date_due } ); |
| 345 |
$t->put_ok( "//$userid:$password@/api/v1/checkouts/" |
| 346 |
. $issue->issue_id => json => { due_date => $offset_rfc3339 } ) |
| 347 |
->status_is( 200, 'Due date updated successfully' ) |
| 348 |
->json_is( '/due_date' => $offset_servertime ); |
| 349 |
|
| 350 |
$t->put_ok( "//$userid:$password@/api/v1/checkouts/" |
| 351 |
. $issue->issue_id => json => { item_id => 3 } ) |
| 352 |
->status_is( 400, 'readOnly properties not updateable' ); |
| 353 |
|
| 354 |
$schema->storage->txn_rollback; |
| 355 |
}; |
| 356 |
|
| 250 |
# Borrowed from 2020 release of DateTime |
357 |
# Borrowed from 2020 release of DateTime |
| 251 |
sub get_offset_string { |
358 |
sub get_offset_string { |
| 252 |
my $offset = shift; |
359 |
my $offset = shift; |
| 253 |
- |
|
|