|
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 under the |
| 6 |
# terms of the GNU General Public License as published by the Free Software |
| 7 |
# Foundation; either version 3 of the License, or (at your option) any later |
| 8 |
# version. |
| 9 |
# |
| 10 |
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY |
| 11 |
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR |
| 12 |
# A PARTICULAR PURPOSE. See the GNU General Public License for more details. |
| 13 |
# |
| 14 |
# You should have received a copy of the GNU General Public License along |
| 15 |
# with Koha; if not, write to the Free Software Foundation, Inc., |
| 16 |
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 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::Patron::Messages; |
| 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::Patron::Messages->search->delete; |
| 42 |
|
| 43 |
my $librarian = $builder->build_object( |
| 44 |
{ |
| 45 |
class => 'Koha::Patrons', |
| 46 |
value => { flags => 4 ** 2 } # borrowers flag = 4 |
| 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 messages, so empty array should be returned |
| 65 |
$t->get_ok("//$userid:$password@/api/v1/messages") |
| 66 |
->status_is(200) |
| 67 |
->json_is( [] ); |
| 68 |
|
| 69 |
my $message = $builder->build_object({ class => 'Koha::Patron::Messages' }); |
| 70 |
|
| 71 |
# One message created, should get returned |
| 72 |
$t->get_ok("//$userid:$password@/api/v1/messages") |
| 73 |
->status_is(200) |
| 74 |
->json_is( [Koha::REST::V1::Messages::_to_api( $message->TO_JSON )] ); |
| 75 |
|
| 76 |
my $another_message = $builder->build_object( |
| 77 |
{ class => 'Koha::Patron::Messages', value => { message_type => $message->message_type } } ); |
| 78 |
my $message_with_another_message_type = $builder->build_object({ class => 'Koha::Patron::Messages' }); |
| 79 |
|
| 80 |
# Two messages created, they should both be returned |
| 81 |
$t->get_ok("//$userid:$password@/api/v1/messages") |
| 82 |
->status_is(200) |
| 83 |
->json_is([Koha::REST::V1::Messages::_to_api($message->TO_JSON), |
| 84 |
Koha::REST::V1::Messages::_to_api($another_message->TO_JSON), |
| 85 |
Koha::REST::V1::Messages::_to_api($message_with_another_message_type->TO_JSON) |
| 86 |
] ); |
| 87 |
|
| 88 |
# Filtering works, two messages sharing message_type |
| 89 |
$t->get_ok("//$userid:$password@/api/v1/messages?message_type=" . $message->message_type ) |
| 90 |
->status_is(200) |
| 91 |
->json_is([ Koha::REST::V1::Messages::_to_api($message->TO_JSON), |
| 92 |
Koha::REST::V1::Messages::_to_api($another_message->TO_JSON) |
| 93 |
]); |
| 94 |
|
| 95 |
$t->get_ok("//$userid:$password@/api/v1/messages?message=" . $message->message ) |
| 96 |
->status_is(200) |
| 97 |
->json_is( [Koha::REST::V1::Messages::_to_api($message->TO_JSON)] ); |
| 98 |
|
| 99 |
# Warn on unsupported query parameter |
| 100 |
$t->get_ok("//$userid:$password@/api/v1/messages?message_blah=blah" ) |
| 101 |
->status_is(400) |
| 102 |
->json_is( [{ path => '/query/message_blah', message => 'Malformed query string'}] ); |
| 103 |
|
| 104 |
# Unauthorized access |
| 105 |
$t->get_ok("//$unauth_userid:$password@/api/v1/messages") |
| 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 $message = $builder->build_object({ class => 'Koha::Patron::Messages' }); |
| 118 |
my $librarian = $builder->build_object( |
| 119 |
{ |
| 120 |
class => 'Koha::Patrons', |
| 121 |
value => { flags => 4**2 } # borrowers flag = 4 |
| 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/messages/" . $message->message_id ) |
| 139 |
->status_is(200) |
| 140 |
->json_is(Koha::REST::V1::Messages::_to_api($message->TO_JSON)); |
| 141 |
|
| 142 |
$t->get_ok( "//$unauth_userid:$password@/api/v1/messages/" . $message->message_id ) |
| 143 |
->status_is(403); |
| 144 |
|
| 145 |
my $message_to_delete = $builder->build_object({ class => 'Koha::Patron::Messages' }); |
| 146 |
my $non_existent_id = $message_to_delete->id; |
| 147 |
$message_to_delete->delete; |
| 148 |
|
| 149 |
$t->get_ok( "//$userid:$password@/api/v1/messages/$non_existent_id" ) |
| 150 |
->status_is(404) |
| 151 |
->json_is( '/error' => 'Message not found' ); |
| 152 |
|
| 153 |
$schema->storage->txn_rollback; |
| 154 |
}; |
| 155 |
|
| 156 |
subtest 'add() tests' => sub { |
| 157 |
|
| 158 |
plan tests => 27; |
| 159 |
|
| 160 |
$schema->storage->txn_begin; |
| 161 |
|
| 162 |
my $librarian = $builder->build_object( |
| 163 |
{ |
| 164 |
class => 'Koha::Patrons', |
| 165 |
value => { flags => 4**2 } # borrowers flag = 4 |
| 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 $message = { |
| 183 |
patron_id => $patron->borrowernumber, |
| 184 |
library_id => $patron->branchcode, |
| 185 |
message_type => "B", |
| 186 |
message => "Old Fox jumped over Cheeseboy" |
| 187 |
}; |
| 188 |
|
| 189 |
# Unauthorized attempt to write |
| 190 |
$t->post_ok("//$unauth_userid:$password@/api/v1/messages" => json => $message) |
| 191 |
->status_is(403); |
| 192 |
|
| 193 |
# Authorized attempt to write invalid data |
| 194 |
my $message_with_invalid_field = { |
| 195 |
blah => "message Blah", |
| 196 |
patron_id => $patron->borrowernumber, |
| 197 |
library_id => $patron->branchcode, |
| 198 |
message_type => "B", |
| 199 |
message => "Old Fox jumped over Cheeseboy", |
| 200 |
manager_id => $librarian->borrowernumber |
| 201 |
}; |
| 202 |
|
| 203 |
$t->post_ok( "//$userid:$password@/api/v1/messages" => json => $message_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 $message_id = |
| 216 |
$t->post_ok( "//$userid:$password@/api/v1/messages" => json => $message ) |
| 217 |
->status_is( 201, 'SWAGGER3.2.1' ) |
| 218 |
->header_like( |
| 219 |
Location => qr|^\/api\/v1\/messages/\d*|, |
| 220 |
'SWAGGER3.4.1' |
| 221 |
) |
| 222 |
->json_is( '/patron_id' => $message->{patron_id} ) |
| 223 |
->json_is( '/library_id' => $message->{library_id} ) |
| 224 |
->json_is( '/message_type' => $message->{message_type} ) |
| 225 |
->json_is( '/message' => $message->{message} ) |
| 226 |
->json_is( '/manager_id' => $librarian->borrowernumber ) |
| 227 |
->tx->res->json->{message_id}; |
| 228 |
|
| 229 |
# Authorized attempt to write with manager_id defined |
| 230 |
$message->{manager_id} = $message->{patron_id}; |
| 231 |
$message_id = |
| 232 |
$t->post_ok( "//$userid:$password@/api/v1/messages" => json => $message ) |
| 233 |
->status_is( 201, 'SWAGGER3.2.1' ) |
| 234 |
->header_like( |
| 235 |
Location => qr|^\/api\/v1\/messages/\d*|, |
| 236 |
'SWAGGER3.4.1' |
| 237 |
) |
| 238 |
->json_is( '/patron_id' => $message->{patron_id} ) |
| 239 |
->json_is( '/library_id' => $message->{library_id} ) |
| 240 |
->json_is( '/message_type' => $message->{message_type} ) |
| 241 |
->json_is( '/message' => $message->{message} ) |
| 242 |
->json_is( '/manager_id' => $message->{patron_id} ) |
| 243 |
->tx->res->json->{message_id}; |
| 244 |
|
| 245 |
# Authorized attempt to create with null id |
| 246 |
$message->{message_id} = undef; |
| 247 |
$t->post_ok( "//$userid:$password@/api/v1/messages" => json => $message ) |
| 248 |
->status_is(400) |
| 249 |
->json_has('/errors'); |
| 250 |
|
| 251 |
# Authorized attempt to create with existing id |
| 252 |
$message->{message_id} = $message_id; |
| 253 |
$t->post_ok( "//$userid:$password@/api/v1/messages" => json => $message ) |
| 254 |
->status_is(400) |
| 255 |
->json_is( |
| 256 |
"/errors" => [ |
| 257 |
{ |
| 258 |
message => "Read-only.", |
| 259 |
path => "/body/message_id" |
| 260 |
} |
| 261 |
] |
| 262 |
); |
| 263 |
|
| 264 |
$schema->storage->txn_rollback; |
| 265 |
}; |
| 266 |
|
| 267 |
subtest 'update() tests' => sub { |
| 268 |
|
| 269 |
plan tests => 15; |
| 270 |
|
| 271 |
$schema->storage->txn_begin; |
| 272 |
|
| 273 |
my $librarian = $builder->build_object( |
| 274 |
{ |
| 275 |
class => 'Koha::Patrons', |
| 276 |
value => { flags => 4**2 } # borrowers flag = 4 |
| 277 |
} |
| 278 |
); |
| 279 |
my $password = 'thePassword123'; |
| 280 |
$librarian->set_password( { password => $password, skip_validation => 1 } ); |
| 281 |
my $userid = $librarian->userid; |
| 282 |
|
| 283 |
my $patron = $builder->build_object( |
| 284 |
{ |
| 285 |
class => 'Koha::Patrons', |
| 286 |
value => { flags => 0 } |
| 287 |
} |
| 288 |
); |
| 289 |
|
| 290 |
$patron->set_password( { password => $password, skip_validation => 1 } ); |
| 291 |
my $unauth_userid = $patron->userid; |
| 292 |
|
| 293 |
my $message_id = $builder->build_object({ class => 'Koha::Patron::Messages' } )->id; |
| 294 |
|
| 295 |
# Unauthorized attempt to update |
| 296 |
$t->put_ok( "//$unauth_userid:$password@/api/v1/messages/$message_id" => json => { name => 'New unauthorized name change' } ) |
| 297 |
->status_is(403); |
| 298 |
|
| 299 |
# Attempt partial update on a PUT |
| 300 |
my $message_with_missing_field = { |
| 301 |
patron_id => $patron->borrowernumber, |
| 302 |
library_id => $patron->branchcode, |
| 303 |
message => "Old Fox jumped over Cheeseboy", |
| 304 |
manager_id => $librarian->borrowernumber |
| 305 |
}; |
| 306 |
|
| 307 |
$t->put_ok( "//$userid:$password@/api/v1/messages/$message_id" => json => $message_with_missing_field ) |
| 308 |
->status_is(400) |
| 309 |
->json_is( "/errors" => |
| 310 |
[ { message => "Missing property.", path => "/body/message_type" } ] |
| 311 |
); |
| 312 |
|
| 313 |
# Full object update on PUT |
| 314 |
my $message_with_updated_field = { |
| 315 |
patron_id => $patron->borrowernumber, |
| 316 |
library_id => $patron->branchcode, |
| 317 |
message_type => "B", |
| 318 |
message => "Old Fox jumped over Cheeseboy", |
| 319 |
manager_id => $librarian->borrowernumber |
| 320 |
}; |
| 321 |
|
| 322 |
$t->put_ok( "//$userid:$password@/api/v1/messages/$message_id" => json => $message_with_updated_field ) |
| 323 |
->status_is(200) |
| 324 |
->json_is( '/message' => 'Old Fox jumped over Cheeseboy' ); |
| 325 |
|
| 326 |
# Authorized attempt to write invalid data |
| 327 |
my $message_with_invalid_field = { |
| 328 |
blah => "message Blah", |
| 329 |
patron_id => $patron->borrowernumber, |
| 330 |
library_id => $patron->branchcode, |
| 331 |
message_type => "B", |
| 332 |
message => "Old Fox jumped over Cheeseboy", |
| 333 |
manager_id => $librarian->borrowernumber |
| 334 |
}; |
| 335 |
|
| 336 |
$t->put_ok( "//$userid:$password@/api/v1/messages/$message_id" => json => $message_with_invalid_field ) |
| 337 |
->status_is(400) |
| 338 |
->json_is( |
| 339 |
"/errors" => [ |
| 340 |
{ |
| 341 |
message => "Properties not allowed: blah.", |
| 342 |
path => "/body" |
| 343 |
} |
| 344 |
] |
| 345 |
); |
| 346 |
|
| 347 |
my $message_to_delete = $builder->build_object({ class => 'Koha::Patron::Messages' }); |
| 348 |
my $non_existent_id = $message_to_delete->id; |
| 349 |
$message_to_delete->delete; |
| 350 |
|
| 351 |
$t->put_ok( "//$userid:$password@/api/v1/messages/$non_existent_id" => json => $message_with_updated_field ) |
| 352 |
->status_is(404); |
| 353 |
|
| 354 |
# Wrong method (POST) |
| 355 |
$message_with_updated_field->{message_id} = 2; |
| 356 |
|
| 357 |
$t->post_ok( "//$userid:$password@/api/v1/messages/$message_id" => json => $message_with_updated_field ) |
| 358 |
->status_is(404); |
| 359 |
|
| 360 |
$schema->storage->txn_rollback; |
| 361 |
}; |
| 362 |
|
| 363 |
subtest 'delete() tests' => sub { |
| 364 |
|
| 365 |
plan tests => 7; |
| 366 |
|
| 367 |
$schema->storage->txn_begin; |
| 368 |
|
| 369 |
my $librarian = $builder->build_object( |
| 370 |
{ |
| 371 |
class => 'Koha::Patrons', |
| 372 |
value => { flags => 4**2 } # borrowers flag = 4 |
| 373 |
} |
| 374 |
); |
| 375 |
my $password = 'thePassword123'; |
| 376 |
$librarian->set_password( { password => $password, skip_validation => 1 } ); |
| 377 |
my $userid = $librarian->userid; |
| 378 |
|
| 379 |
my $patron = $builder->build_object( |
| 380 |
{ |
| 381 |
class => 'Koha::Patrons', |
| 382 |
value => { flags => 0 } |
| 383 |
} |
| 384 |
); |
| 385 |
|
| 386 |
$patron->set_password( { password => $password, skip_validation => 1 } ); |
| 387 |
my $unauth_userid = $patron->userid; |
| 388 |
|
| 389 |
my $message_id = $builder->build_object({ class => 'Koha::Patron::Messages' })->id; |
| 390 |
|
| 391 |
# Unauthorized attempt to delete |
| 392 |
$t->delete_ok( "//$unauth_userid:$password@/api/v1/messages/$message_id" ) |
| 393 |
->status_is(403); |
| 394 |
|
| 395 |
$t->delete_ok("//$userid:$password@/api/v1/messages/$message_id") |
| 396 |
->status_is(200) |
| 397 |
->content_is('""'); |
| 398 |
|
| 399 |
$t->delete_ok("//$userid:$password@/api/v1/messages/$message_id") |
| 400 |
->status_is(404); |
| 401 |
|
| 402 |
$schema->storage->txn_rollback; |
| 403 |
}; |