|
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 |
|
| 21 |
use Test::More tests => 7; |
| 22 |
use Test::Mojo; |
| 23 |
use t::lib::Mocks; |
| 24 |
use t::lib::TestBuilder; |
| 25 |
|
| 26 |
use C4::Auth; |
| 27 |
use C4::Context; |
| 28 |
|
| 29 |
use Koha::Database; |
| 30 |
use Koha::Notice::Messages; |
| 31 |
|
| 32 |
my $schema = Koha::Database->new->schema; |
| 33 |
my $builder = t::lib::TestBuilder->new; |
| 34 |
|
| 35 |
# FIXME: sessionStorage defaults to mysql, but it seems to break transaction handling |
| 36 |
# this affects the other REST api tests |
| 37 |
t::lib::Mocks::mock_preference( 'SessionStorage', 'tmp' ); |
| 38 |
|
| 39 |
my $remote_address = '127.0.0.1'; |
| 40 |
my $t = Test::Mojo->new('Koha::REST::V1'); |
| 41 |
|
| 42 |
subtest 'list() tests' => sub { |
| 43 |
plan tests => 24; |
| 44 |
|
| 45 |
$schema->storage->txn_begin; |
| 46 |
|
| 47 |
Koha::Notice::Messages->delete; |
| 48 |
|
| 49 |
my ($patron, $session) = create_user_and_session(); |
| 50 |
my ($another_patron, undef) = create_user_and_session(); |
| 51 |
my ($librarian, $librarian_session) = create_user_and_session({ |
| 52 |
"messages" => "get_message" |
| 53 |
}); |
| 54 |
|
| 55 |
my $notice = $builder->build({ |
| 56 |
source => 'MessageQueue', |
| 57 |
value => { |
| 58 |
borrowernumber => $patron->borrowernumber, |
| 59 |
time_queued => '2016-01-01 13:00:00' |
| 60 |
} |
| 61 |
}); |
| 62 |
my $another_notice = $builder->build({ |
| 63 |
source => 'MessageQueue', |
| 64 |
value => { |
| 65 |
borrowernumber => $another_patron->borrowernumber, |
| 66 |
time_queued => '2017-01-01 13:00:00' |
| 67 |
} |
| 68 |
}); |
| 69 |
my $message_id = $notice->{'message_id'}; |
| 70 |
my $another_message_id = $another_notice->{'message_id'}; |
| 71 |
|
| 72 |
my $tx = $t->ua->build_tx(GET => "/api/v1/notices?borrowernumber=" |
| 73 |
.$patron->borrowernumber); |
| 74 |
$tx->req->cookies({name => 'CGISESSID', value => $session->id}); |
| 75 |
$tx->req->env({REMOTE_ADDR => '127.0.0.1'}); |
| 76 |
$t->request_ok($tx) |
| 77 |
->status_is(200) |
| 78 |
->json_is('/0/subject' => $notice->{'subject'}); |
| 79 |
|
| 80 |
$tx = $t->ua->build_tx(GET => "/api/v1/notices?borrowernumber=" |
| 81 |
.$another_patron->borrowernumber); |
| 82 |
$tx->req->cookies({name => 'CGISESSID', value => $session->id}); |
| 83 |
$tx->req->env({REMOTE_ADDR => '127.0.0.1'}); |
| 84 |
$t->request_ok($tx) |
| 85 |
->status_is(403); |
| 86 |
|
| 87 |
$tx = $t->ua->build_tx(GET => "/api/v1/notices?borrowernumber=-1"); |
| 88 |
$tx->req->cookies({name => 'CGISESSID', value => $librarian_session->id}); |
| 89 |
$tx->req->env({REMOTE_ADDR => '127.0.0.1'}); |
| 90 |
$t->request_ok($tx) |
| 91 |
->status_is(200) |
| 92 |
->json_is('/0' => undef); |
| 93 |
|
| 94 |
$tx = $t->ua->build_tx(GET => "/api/v1/notices?borrowernumber=" |
| 95 |
.$patron->borrowernumber); |
| 96 |
$tx->req->cookies({name => 'CGISESSID', value => $librarian_session->id}); |
| 97 |
$tx->req->env({REMOTE_ADDR => '127.0.0.1'}); |
| 98 |
$t->request_ok($tx) |
| 99 |
->status_is(200) |
| 100 |
->json_is('/0/subject' => $notice->{'subject'}); |
| 101 |
|
| 102 |
$tx = $t->ua->build_tx(GET => '/api/v1/notices?' |
| 103 |
.'time_queued_start=2016-12-12 12:00:00'); |
| 104 |
$tx->req->cookies({name => 'CGISESSID', value => $librarian_session->id}); |
| 105 |
$tx->req->env({REMOTE_ADDR => '127.0.0.1'}); |
| 106 |
$t->request_ok($tx) |
| 107 |
->status_is(200) |
| 108 |
->json_is('/0/subject' => $another_notice->{'subject'}) |
| 109 |
->json_hasnt('/1'); |
| 110 |
|
| 111 |
$tx = $t->ua->build_tx(GET => '/api/v1/notices?' |
| 112 |
.'time_queued_end=2016-12-12 12:00:00'); |
| 113 |
$tx->req->cookies({name => 'CGISESSID', value => $librarian_session->id}); |
| 114 |
$tx->req->env({REMOTE_ADDR => '127.0.0.1'}); |
| 115 |
$t->request_ok($tx) |
| 116 |
->status_is(200) |
| 117 |
->json_is('/0/subject' => $notice->{'subject'}) |
| 118 |
->json_hasnt('/1'); |
| 119 |
|
| 120 |
$tx = $t->ua->build_tx(GET => '/api/v1/notices?' |
| 121 |
.'time_queued_start=2014-12-12 12:00:00&time_queued_end=2018-01-01 12:00:00'); |
| 122 |
$tx->req->cookies({name => 'CGISESSID', value => $librarian_session->id}); |
| 123 |
$tx->req->env({REMOTE_ADDR => '127.0.0.1'}); |
| 124 |
$t->request_ok($tx) |
| 125 |
->status_is(200) |
| 126 |
->json_is('/0/subject' => $notice->{'subject'}) |
| 127 |
->json_is('/1/subject' => $another_notice->{'subject'}) |
| 128 |
->json_hasnt('/2'); |
| 129 |
|
| 130 |
$schema->storage->txn_rollback; |
| 131 |
}; |
| 132 |
|
| 133 |
subtest 'get() tests' => sub { |
| 134 |
plan tests => 8; |
| 135 |
|
| 136 |
$schema->storage->txn_begin; |
| 137 |
|
| 138 |
my ($patron, $session) = create_user_and_session(); |
| 139 |
my ($another_patron, undef) = create_user_and_session(); |
| 140 |
my ($librarian, $librarian_session) = create_user_and_session({ |
| 141 |
"messages" => "get_message" |
| 142 |
}); |
| 143 |
|
| 144 |
my $notice = $builder->build({ |
| 145 |
source => 'MessageQueue', |
| 146 |
value => { |
| 147 |
borrowernumber => $patron->borrowernumber, |
| 148 |
} |
| 149 |
}); |
| 150 |
my $another_notice = $builder->build({ |
| 151 |
source => 'MessageQueue', |
| 152 |
value => { |
| 153 |
borrowernumber => $another_patron->borrowernumber, |
| 154 |
} |
| 155 |
}); |
| 156 |
my $message_id = $notice->{'message_id'}; |
| 157 |
my $another_message_id = $another_notice->{'message_id'}; |
| 158 |
|
| 159 |
my $tx = $t->ua->build_tx(GET => "/api/v1/notices/$message_id"); |
| 160 |
$tx->req->cookies({name => 'CGISESSID', value => $session->id}); |
| 161 |
$tx->req->env({REMOTE_ADDR => '127.0.0.1'}); |
| 162 |
$t->request_ok($tx) |
| 163 |
->status_is(200) |
| 164 |
->json_is('/subject' => $notice->{'subject'}); |
| 165 |
|
| 166 |
$tx = $t->ua->build_tx(GET => "/api/v1/notices/$another_message_id"); |
| 167 |
$tx->req->cookies({name => 'CGISESSID', value => $session->id}); |
| 168 |
$tx->req->env({REMOTE_ADDR => '127.0.0.1'}); |
| 169 |
$t->request_ok($tx) |
| 170 |
->status_is(403); |
| 171 |
|
| 172 |
$tx = $t->ua->build_tx(GET => "/api/v1/notices/$message_id"); |
| 173 |
$tx->req->cookies({name => 'CGISESSID', value => $librarian_session->id}); |
| 174 |
$tx->req->env({REMOTE_ADDR => '127.0.0.1'}); |
| 175 |
$t->request_ok($tx) |
| 176 |
->status_is(200) |
| 177 |
->json_is('/subject' => $notice->{'subject'}); |
| 178 |
|
| 179 |
$schema->storage->txn_rollback; |
| 180 |
}; |
| 181 |
|
| 182 |
subtest 'post() tests' => sub { |
| 183 |
plan tests => 6; |
| 184 |
|
| 185 |
$schema->storage->txn_begin; |
| 186 |
|
| 187 |
my ($patron, $session) = create_user_and_session(); |
| 188 |
my ($librarian, $librarian_session) = create_user_and_session({ |
| 189 |
"messages" => "create_message" |
| 190 |
}); |
| 191 |
|
| 192 |
my $notice = { |
| 193 |
borrowernumber => 0+$patron->borrowernumber, |
| 194 |
subject => 'Bring milk from the store, please!', |
| 195 |
content => 'Title says it all', |
| 196 |
message_transport_type => 'email', |
| 197 |
status => 'failed', |
| 198 |
}; |
| 199 |
|
| 200 |
my $tx = $t->ua->build_tx(POST => "/api/v1/notices" => json => $notice); |
| 201 |
$tx->req->cookies({name => 'CGISESSID', value => $session->id}); |
| 202 |
$tx->req->env({REMOTE_ADDR => '127.0.0.1'}); |
| 203 |
$t->request_ok($tx) |
| 204 |
->status_is(403); |
| 205 |
|
| 206 |
$tx = $t->ua->build_tx(POST => "/api/v1/notices" => json => $notice); |
| 207 |
$tx->req->cookies({name => 'CGISESSID', value => $librarian_session->id}); |
| 208 |
$tx->req->env({REMOTE_ADDR => '127.0.0.1'}); |
| 209 |
$t->request_ok($tx) |
| 210 |
->status_is(201) |
| 211 |
->json_is('/subject' => $notice->{'subject'}); |
| 212 |
|
| 213 |
my $msg = Koha::Notice::Messages->search({}, { |
| 214 |
order_by => { '-desc' => 'message_id' } |
| 215 |
})->next; |
| 216 |
is($msg->subject, $notice->{subject}, 'The notice was really added!'); |
| 217 |
|
| 218 |
$schema->storage->txn_rollback; |
| 219 |
}; |
| 220 |
|
| 221 |
subtest 'edit() tests' => sub { |
| 222 |
plan tests => 5; |
| 223 |
|
| 224 |
$schema->storage->txn_begin; |
| 225 |
|
| 226 |
my ($patron, $session) = create_user_and_session(); |
| 227 |
my ($librarian, $librarian_session) = create_user_and_session({ |
| 228 |
"messages" => "update_message" |
| 229 |
}); |
| 230 |
|
| 231 |
my $notice = $builder->build({ |
| 232 |
source => 'MessageQueue', |
| 233 |
value => { |
| 234 |
borrowernumber => $patron->borrowernumber, |
| 235 |
} |
| 236 |
}); |
| 237 |
|
| 238 |
my $new_notice = { |
| 239 |
subject => 'Bring milk from the store, please!', |
| 240 |
content => 'Title says it all', |
| 241 |
message_transport_type => 'email', |
| 242 |
status => 'failed', |
| 243 |
}; |
| 244 |
|
| 245 |
my $message_id = $notice->{'message_id'}; |
| 246 |
|
| 247 |
my $tx = $t->ua->build_tx(PUT => "/api/v1/notices/$message_id" => |
| 248 |
json => $new_notice); |
| 249 |
$tx->req->cookies({name => 'CGISESSID', value => $session->id}); |
| 250 |
$tx->req->env({REMOTE_ADDR => '127.0.0.1'}); |
| 251 |
$t->request_ok($tx) |
| 252 |
->status_is(403); |
| 253 |
|
| 254 |
$tx = $t->ua->build_tx(PUT => "/api/v1/notices/$message_id" => |
| 255 |
json => $new_notice); |
| 256 |
$tx->req->cookies({name => 'CGISESSID', value => $librarian_session->id}); |
| 257 |
$tx->req->env({REMOTE_ADDR => '127.0.0.1'}); |
| 258 |
$t->request_ok($tx) |
| 259 |
->status_is(200) |
| 260 |
->json_is('/subject' => $new_notice->{'subject'}); |
| 261 |
|
| 262 |
$schema->storage->txn_rollback; |
| 263 |
}; |
| 264 |
|
| 265 |
subtest 'put() tests' => sub { |
| 266 |
plan tests => 6; |
| 267 |
|
| 268 |
$schema->storage->txn_begin; |
| 269 |
|
| 270 |
my ($patron, $session) = create_user_and_session(); |
| 271 |
my ($librarian, $librarian_session) = create_user_and_session({ |
| 272 |
"messages" => "update_message" |
| 273 |
}); |
| 274 |
|
| 275 |
my $notice = $builder->build({ |
| 276 |
source => 'MessageQueue', |
| 277 |
value => { |
| 278 |
borrowernumber => $patron->borrowernumber, |
| 279 |
} |
| 280 |
}); |
| 281 |
|
| 282 |
my $new_notice = { |
| 283 |
status => 'pending', |
| 284 |
}; |
| 285 |
|
| 286 |
my $message_id = $notice->{'message_id'}; |
| 287 |
|
| 288 |
my $tx = $t->ua->build_tx(PATCH => "/api/v1/notices/$message_id" => |
| 289 |
json => $new_notice); |
| 290 |
$tx->req->cookies({name => 'CGISESSID', value => $session->id}); |
| 291 |
$tx->req->env({REMOTE_ADDR => '127.0.0.1'}); |
| 292 |
$t->request_ok($tx) |
| 293 |
->status_is(403); |
| 294 |
|
| 295 |
$tx = $t->ua->build_tx(PATCH => "/api/v1/notices/$message_id" => |
| 296 |
json => $new_notice); |
| 297 |
$tx->req->cookies({name => 'CGISESSID', value => $librarian_session->id}); |
| 298 |
$tx->req->env({REMOTE_ADDR => '127.0.0.1'}); |
| 299 |
$t->request_ok($tx) |
| 300 |
->status_is(200) |
| 301 |
->json_is('/subject' => $notice->{'subject'}) |
| 302 |
->json_is('/status' => $new_notice->{'status'}); |
| 303 |
|
| 304 |
$schema->storage->txn_rollback; |
| 305 |
}; |
| 306 |
|
| 307 |
subtest 'resend() tests' => sub { |
| 308 |
plan tests => 6; |
| 309 |
|
| 310 |
$schema->storage->txn_begin; |
| 311 |
|
| 312 |
my ($patron, $session) = create_user_and_session(); |
| 313 |
my ($librarian, $librarian_session) = create_user_and_session({ |
| 314 |
"messages" => "resend_message" |
| 315 |
}); |
| 316 |
|
| 317 |
my $notice = $builder->build({ |
| 318 |
source => 'MessageQueue', |
| 319 |
value => { |
| 320 |
borrowernumber => $patron->borrowernumber, |
| 321 |
status => 'failed' |
| 322 |
} |
| 323 |
}); |
| 324 |
|
| 325 |
my $message_id = $notice->{'message_id'}; |
| 326 |
|
| 327 |
my $msg = Koha::Notice::Messages->find($message_id); |
| 328 |
is($msg->status, 'failed', 'The notice is in failed status.'); |
| 329 |
|
| 330 |
my $tx = $t->ua->build_tx(POST => "/api/v1/notices/$message_id/resend"); |
| 331 |
$tx->req->cookies({name => 'CGISESSID', value => $session->id}); |
| 332 |
$tx->req->env({REMOTE_ADDR => '127.0.0.1'}); |
| 333 |
$t->request_ok($tx) |
| 334 |
->status_is(403); |
| 335 |
|
| 336 |
$tx = $t->ua->build_tx(POST => "/api/v1/notices/$message_id/resend"); |
| 337 |
$tx->req->cookies({name => 'CGISESSID', value => $librarian_session->id}); |
| 338 |
$tx->req->env({REMOTE_ADDR => '127.0.0.1'}); |
| 339 |
$t->request_ok($tx) |
| 340 |
->status_is(204); |
| 341 |
|
| 342 |
$msg = Koha::Notice::Messages->find($message_id); |
| 343 |
is($msg->status, 'pending', 'The notice has been set to pending!'); |
| 344 |
|
| 345 |
$schema->storage->txn_rollback; |
| 346 |
}; |
| 347 |
|
| 348 |
subtest 'delete() tests' => sub { |
| 349 |
plan tests => 5; |
| 350 |
|
| 351 |
$schema->storage->txn_begin; |
| 352 |
|
| 353 |
my ($patron, $session) = create_user_and_session(); |
| 354 |
my ($librarian, $librarian_session) = create_user_and_session({ |
| 355 |
"messages" => "delete_message" |
| 356 |
}); |
| 357 |
|
| 358 |
my $notice = $builder->build({ |
| 359 |
source => 'MessageQueue', |
| 360 |
value => { |
| 361 |
borrowernumber => $patron->borrowernumber, |
| 362 |
} |
| 363 |
}); |
| 364 |
|
| 365 |
my $message_id = $notice->{'message_id'}; |
| 366 |
|
| 367 |
my $tx = $t->ua->build_tx(DELETE => "/api/v1/notices/$message_id"); |
| 368 |
$tx->req->cookies({name => 'CGISESSID', value => $session->id}); |
| 369 |
$tx->req->env({REMOTE_ADDR => '127.0.0.1'}); |
| 370 |
$t->request_ok($tx) |
| 371 |
->status_is(403); |
| 372 |
|
| 373 |
$tx = $t->ua->build_tx(DELETE => "/api/v1/notices/$message_id"); |
| 374 |
$tx->req->cookies({name => 'CGISESSID', value => $librarian_session->id}); |
| 375 |
$tx->req->env({REMOTE_ADDR => '127.0.0.1'}); |
| 376 |
$t->request_ok($tx) |
| 377 |
->status_is(204); |
| 378 |
|
| 379 |
my $msg = Koha::Notice::Messages->find($message_id); |
| 380 |
is($msg, undef, 'The notice was really deleted!'); |
| 381 |
|
| 382 |
$schema->storage->txn_rollback; |
| 383 |
}; |
| 384 |
|
| 385 |
sub create_user_and_session { |
| 386 |
my ($flags) = @_; |
| 387 |
|
| 388 |
my $categorycode = $builder->build({ source => 'Category' })->{categorycode}; |
| 389 |
my $branchcode = $builder->build({ source => 'Branch' })->{ branchcode }; |
| 390 |
|
| 391 |
my $borrower = $builder->build({ |
| 392 |
source => 'Borrower', |
| 393 |
value => { |
| 394 |
branchcode => $branchcode, |
| 395 |
categorycode => $categorycode, |
| 396 |
} |
| 397 |
}); |
| 398 |
|
| 399 |
my $session = C4::Auth::get_session(''); |
| 400 |
$session->param('number', $borrower->{ borrowernumber }); |
| 401 |
$session->param('id', $borrower->{ userid }); |
| 402 |
$session->param('ip', '127.0.0.1'); |
| 403 |
$session->param('lasttime', time()); |
| 404 |
$session->flush; |
| 405 |
my $patron = Koha::Patrons->find($borrower->{borrowernumber}); |
| 406 |
if ( $flags ) { |
| 407 |
Koha::Auth::PermissionManager->grantPermissions($patron, $flags); |
| 408 |
} |
| 409 |
|
| 410 |
return ($patron, $session); |
| 411 |
} |