|
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 |
use Test::Warn; |
| 23 |
|
| 24 |
use t::lib::TestBuilder; |
| 25 |
use t::lib::Mocks; |
| 26 |
|
| 27 |
use C4::Auth; |
| 28 |
use Koha::Acquisition::Booksellers; |
| 29 |
use Koha::Database; |
| 30 |
|
| 31 |
my $schema = Koha::Database->new->schema; |
| 32 |
my $builder = t::lib::TestBuilder->new; |
| 33 |
|
| 34 |
# FIXME: sessionStorage defaults to mysql, but it seems to break transaction handling |
| 35 |
# this affects the other REST api tests |
| 36 |
t::lib::Mocks::mock_preference( 'SessionStorage', 'tmp' ); |
| 37 |
|
| 38 |
my $remote_address = '127.0.0.1'; |
| 39 |
my $t = Test::Mojo->new('Koha::REST::V1'); |
| 40 |
|
| 41 |
subtest 'list() and delete() tests | authorized user' => sub { |
| 42 |
|
| 43 |
plan tests => 35; |
| 44 |
|
| 45 |
$schema->storage->txn_begin; |
| 46 |
|
| 47 |
$schema->resultset('Aqbasket')->search->delete; |
| 48 |
Koha::Acquisition::Booksellers->search->delete; |
| 49 |
my ( $borrowernumber, $session_id ) |
| 50 |
= create_user_and_session( { authorized => 1 } ); |
| 51 |
|
| 52 |
## Authorized user tests |
| 53 |
# No vendors, so empty array should be returned |
| 54 |
my $tx = $t->ua->build_tx( GET => '/api/v1/acquisitions/vendors' ); |
| 55 |
$tx->req->cookies( { name => 'CGISESSID', value => $session_id } ); |
| 56 |
$tx->req->env( { REMOTE_ADDR => $remote_address } ); |
| 57 |
$t->request_ok($tx) |
| 58 |
->status_is(200) |
| 59 |
->json_is( [] ); |
| 60 |
|
| 61 |
my $vendor_name = 'Ruben libros'; |
| 62 |
my $vendor = $builder->build_object({ class => 'Koha::Acquisition::Booksellers', value => { name => $vendor_name } }); |
| 63 |
|
| 64 |
# One vendor created, should get returned |
| 65 |
$tx = $t->ua->build_tx( GET => '/api/v1/acquisitions/vendors' ); |
| 66 |
$tx->req->cookies( { name => 'CGISESSID', value => $session_id } ); |
| 67 |
$tx->req->env( { REMOTE_ADDR => $remote_address } ); |
| 68 |
$t->request_ok($tx) |
| 69 |
->status_is(200) |
| 70 |
->json_like( '/0/name' => qr/$vendor_name/ ); |
| 71 |
|
| 72 |
my $other_vendor_name = 'Amerindia'; |
| 73 |
my $other_vendor |
| 74 |
= $builder->build_object({ class => 'Koha::Acquisition::Booksellers', value => { name => $other_vendor_name } }); |
| 75 |
|
| 76 |
$tx = $t->ua->build_tx( GET => '/api/v1/acquisitions/vendors' ); |
| 77 |
$tx->req->cookies( { name => 'CGISESSID', value => $session_id } ); |
| 78 |
$tx->req->env( { REMOTE_ADDR => $remote_address } ); |
| 79 |
$t->request_ok($tx) |
| 80 |
->status_is(200) |
| 81 |
->json_like( '/0/name' => qr/Ruben/ ) |
| 82 |
->json_like( '/1/name' => qr/Amerindia/ ); |
| 83 |
|
| 84 |
$tx = $t->ua->build_tx( GET => '/api/v1/acquisitions/vendors?name=' . $vendor_name ); |
| 85 |
$tx->req->cookies( { name => 'CGISESSID', value => $session_id } ); |
| 86 |
$tx->req->env( { REMOTE_ADDR => $remote_address } ); |
| 87 |
$t->request_ok($tx) |
| 88 |
->status_is(200) |
| 89 |
->json_like( '/0/name' => qr/Ruben/ ); |
| 90 |
|
| 91 |
$tx = $t->ua->build_tx( GET => '/api/v1/acquisitions/vendors?name=' . $other_vendor_name ); |
| 92 |
$tx->req->cookies( { name => 'CGISESSID', value => $session_id } ); |
| 93 |
$tx->req->env( { REMOTE_ADDR => $remote_address } ); |
| 94 |
$t->request_ok($tx) |
| 95 |
->status_is(200) |
| 96 |
->json_like( '/0/name' => qr/Amerindia/ ); |
| 97 |
|
| 98 |
$tx = $t->ua->build_tx( GET => '/api/v1/acquisitions/vendors?accountnumber=' . $vendor->accountnumber ); |
| 99 |
$tx->req->cookies( { name => 'CGISESSID', value => $session_id } ); |
| 100 |
$tx->req->env( { REMOTE_ADDR => $remote_address } ); |
| 101 |
$t->request_ok($tx) |
| 102 |
->status_is(200) |
| 103 |
->json_like( '/0/name' => qr/Ruben/ ); |
| 104 |
|
| 105 |
$tx = $t->ua->build_tx( GET => '/api/v1/acquisitions/vendors?accountnumber=' . $other_vendor->accountnumber ); |
| 106 |
$tx->req->cookies( { name => 'CGISESSID', value => $session_id } ); |
| 107 |
$tx->req->env( { REMOTE_ADDR => $remote_address } ); |
| 108 |
$t->request_ok($tx) |
| 109 |
->status_is(200) |
| 110 |
->json_like( '/0/name' => qr/Amerindia/ ); |
| 111 |
|
| 112 |
$tx = $t->ua->build_tx( DELETE => '/api/v1/acquisitions/vendors/' . $vendor->id ); |
| 113 |
$tx->req->cookies( { name => 'CGISESSID', value => $session_id } ); |
| 114 |
$tx->req->env( { REMOTE_ADDR => $remote_address } ); |
| 115 |
$t->request_ok($tx) |
| 116 |
->status_is(200) |
| 117 |
->content_is(q{""}); |
| 118 |
|
| 119 |
$tx = $t->ua->build_tx( GET => '/api/v1/acquisitions/vendors' ); |
| 120 |
$tx->req->cookies( { name => 'CGISESSID', value => $session_id } ); |
| 121 |
$tx->req->env( { REMOTE_ADDR => $remote_address } ); |
| 122 |
$t->request_ok($tx) |
| 123 |
->status_is(200) |
| 124 |
->json_like( '/0/name' => qr/$other_vendor_name/ ) |
| 125 |
->json_hasnt( '/1', 'Only one vendor' ); |
| 126 |
|
| 127 |
$tx = $t->ua->build_tx( DELETE => '/api/v1/acquisitions/vendors/' . $other_vendor->id ); |
| 128 |
$tx->req->cookies( { name => 'CGISESSID', value => $session_id } ); |
| 129 |
$tx->req->env( { REMOTE_ADDR => $remote_address } ); |
| 130 |
$t->request_ok($tx) |
| 131 |
->status_is(200) |
| 132 |
->content_is(q{""}); |
| 133 |
|
| 134 |
$tx = $t->ua->build_tx( GET => '/api/v1/acquisitions/vendors' ); |
| 135 |
$tx->req->cookies( { name => 'CGISESSID', value => $session_id } ); |
| 136 |
$tx->req->env( { REMOTE_ADDR => $remote_address } ); |
| 137 |
$t->request_ok($tx) |
| 138 |
->status_is(200) |
| 139 |
->json_is( [] ); |
| 140 |
|
| 141 |
$schema->storage->txn_rollback; |
| 142 |
}; |
| 143 |
|
| 144 |
subtest 'get() test' => sub { |
| 145 |
|
| 146 |
plan tests => 9; |
| 147 |
|
| 148 |
$schema->storage->txn_begin; |
| 149 |
|
| 150 |
my $vendor = $builder->build_object({ class => 'Koha::Acquisition::Booksellers' }); |
| 151 |
my ( $borrowernumber, $session_id ) |
| 152 |
= create_user_and_session( { authorized => 1 } ); |
| 153 |
|
| 154 |
my $tx = $t->ua->build_tx( GET => "/api/v1/acquisitions/vendors/" . $vendor->id ); |
| 155 |
$tx->req->cookies( { name => 'CGISESSID', value => $session_id } ); |
| 156 |
$tx->req->env( { REMOTE_ADDR => $remote_address } ); |
| 157 |
$t->request_ok($tx) |
| 158 |
->status_is(200) |
| 159 |
->json_is( Koha::REST::V1::Acquisitions::Vendors::_to_api( $vendor->TO_JSON ) ); |
| 160 |
|
| 161 |
my $non_existent_id = $vendor->id + 1; |
| 162 |
$tx = $t->ua->build_tx( GET => "/api/v1/acquisitions/vendors/" . $non_existent_id ); |
| 163 |
$tx->req->cookies( { name => 'CGISESSID', value => $session_id } ); |
| 164 |
$tx->req->env( { REMOTE_ADDR => $remote_address } ); |
| 165 |
$t->request_ok($tx) |
| 166 |
->status_is(404) |
| 167 |
->json_is( '/error' => 'Vendor not found' ); |
| 168 |
|
| 169 |
my ( $unauthorized_borrowernumber, $unauthorized_session_id ) |
| 170 |
= create_user_and_session( { authorized => 0 } ); |
| 171 |
$tx = $t->ua->build_tx( GET => "/api/v1/acquisitions/vendors/" . $vendor->id ); |
| 172 |
$tx->req->cookies( { name => 'CGISESSID', value => $unauthorized_session_id } ); |
| 173 |
$tx->req->env( { REMOTE_ADDR => $remote_address } ); |
| 174 |
$t->request_ok($tx) |
| 175 |
->status_is(403) |
| 176 |
->json_is( '/error', 'Authorization failure. Missing required permission(s).' ); |
| 177 |
|
| 178 |
$schema->storage->txn_rollback; |
| 179 |
}; |
| 180 |
|
| 181 |
subtest 'add() tests' => sub { |
| 182 |
|
| 183 |
plan tests => 15; |
| 184 |
|
| 185 |
$schema->storage->txn_begin; |
| 186 |
|
| 187 |
my ( $unauthorized_borrowernumber, $unauthorized_session_id ) |
| 188 |
= create_user_and_session( { authorized => 0 } ); |
| 189 |
my ( $authorized_borrowernumber, $authorized_session_id ) |
| 190 |
= create_user_and_session( { authorized => 1 } ); |
| 191 |
my $vendor = { name => 'Ruben libros' }; |
| 192 |
|
| 193 |
# Unauthorized attempt to write |
| 194 |
my $tx = $t->ua->build_tx( POST => "/api/v1/acquisitions/vendors" => json => $vendor ); |
| 195 |
$tx->req->cookies( { name => 'CGISESSID', value => $unauthorized_session_id } ); |
| 196 |
$tx->req->env( { REMOTE_ADDR => $remote_address } ); |
| 197 |
$t->request_ok($tx) |
| 198 |
->status_is(403); |
| 199 |
|
| 200 |
# Authorized attempt to write invalid data |
| 201 |
my $vendor_with_invalid_field = { |
| 202 |
name => 'Amerindia', |
| 203 |
address5 => 'An address' |
| 204 |
}; |
| 205 |
|
| 206 |
$tx = $t->ua->build_tx( |
| 207 |
POST => "/api/v1/acquisitions/vendors" => json => $vendor_with_invalid_field ); |
| 208 |
$tx->req->cookies( { name => 'CGISESSID', value => $authorized_session_id } ); |
| 209 |
$tx->req->env( { REMOTE_ADDR => $remote_address } ); |
| 210 |
$t->request_ok($tx) |
| 211 |
->status_is(400) |
| 212 |
->json_is( |
| 213 |
"/errors" => [ |
| 214 |
{ message => "Properties not allowed: address5.", |
| 215 |
path => "/body" |
| 216 |
} |
| 217 |
] |
| 218 |
); |
| 219 |
|
| 220 |
# Authorized attempt to write |
| 221 |
$tx = $t->ua->build_tx( POST => "/api/v1/acquisitions/vendors" => json => $vendor ); |
| 222 |
$tx->req->cookies( { name => 'CGISESSID', value => $authorized_session_id } ); |
| 223 |
$tx->req->env( { REMOTE_ADDR => $remote_address } ); |
| 224 |
my $vendor_id = $t->request_ok($tx) |
| 225 |
->status_is(200) |
| 226 |
->json_is( '/name' => $vendor->{name} ) |
| 227 |
->json_is( '/address1' => $vendor->{address1} )->tx->res->json('/id') |
| 228 |
; # read the response vendor id for later use |
| 229 |
|
| 230 |
# Authorized attempt to create with null id |
| 231 |
$vendor->{id} = undef; |
| 232 |
$tx = $t->ua->build_tx( POST => "/api/v1/acquisitions/vendors" => json => $vendor ); |
| 233 |
$tx->req->cookies( { name => 'CGISESSID', value => $authorized_session_id } ); |
| 234 |
$tx->req->env( { REMOTE_ADDR => $remote_address } ); |
| 235 |
$t->request_ok($tx) |
| 236 |
->status_is(400) |
| 237 |
->json_has('/errors'); |
| 238 |
|
| 239 |
# Authorized attempt to create with existing id |
| 240 |
$vendor->{id} = $vendor_id; |
| 241 |
$tx = $t->ua->build_tx( POST => "/api/v1/acquisitions/vendors" => json => $vendor ); |
| 242 |
$tx->req->cookies( { name => 'CGISESSID', value => $authorized_session_id } ); |
| 243 |
$tx->req->env( { REMOTE_ADDR => $remote_address } ); |
| 244 |
$t->request_ok($tx) |
| 245 |
->status_is(400) |
| 246 |
->json_is( |
| 247 |
"/errors" => [ |
| 248 |
{ message => "Read-only.", |
| 249 |
path => "/body/id" |
| 250 |
} |
| 251 |
] |
| 252 |
); |
| 253 |
|
| 254 |
$schema->storage->txn_rollback; |
| 255 |
}; |
| 256 |
|
| 257 |
subtest 'update() tests' => sub { |
| 258 |
|
| 259 |
plan tests => 15; |
| 260 |
|
| 261 |
$schema->storage->txn_begin; |
| 262 |
|
| 263 |
my ( $unauthorized_borrowernumber, $unauthorized_session_id ) |
| 264 |
= create_user_and_session( { authorized => 0 } ); |
| 265 |
my ( $authorized_borrowernumber, $authorized_session_id ) |
| 266 |
= create_user_and_session( { authorized => 1 } ); |
| 267 |
|
| 268 |
my $vendor_id = $builder->build( { source => 'Aqbookseller' } )->{id}; |
| 269 |
|
| 270 |
# Unauthorized attempt to update |
| 271 |
my $tx = $t->ua->build_tx( PUT => "/api/v1/acquisitions/vendors/$vendor_id" => json => |
| 272 |
{ city_name => 'New unauthorized name change' } ); |
| 273 |
$tx->req->cookies( { name => 'CGISESSID', value => $unauthorized_session_id } ); |
| 274 |
$tx->req->env( { REMOTE_ADDR => $remote_address } ); |
| 275 |
$t->request_ok($tx) |
| 276 |
->status_is(403); |
| 277 |
|
| 278 |
# Attempt partial update on a PUT |
| 279 |
my $vendor_without_mandatory_field = { address1 => 'New address' }; |
| 280 |
|
| 281 |
$tx = $t->ua->build_tx( PUT => "/api/v1/acquisitions/vendors/$vendor_id" => json => |
| 282 |
$vendor_without_mandatory_field ); |
| 283 |
$tx->req->cookies( { name => 'CGISESSID', value => $authorized_session_id } ); |
| 284 |
$tx->req->env( { REMOTE_ADDR => $remote_address } ); |
| 285 |
$t->request_ok($tx) |
| 286 |
->status_is(400) |
| 287 |
->json_is( "/errors" => [ { message => "Missing property.", path => "/body/name" } ] ); |
| 288 |
|
| 289 |
# Full object update on PUT |
| 290 |
my $vendor_with_updated_field = { name => "London books", }; |
| 291 |
|
| 292 |
$tx = $t->ua->build_tx( |
| 293 |
PUT => "/api/v1/acquisitions/vendors/$vendor_id" => json => $vendor_with_updated_field ); |
| 294 |
$tx->req->cookies( { name => 'CGISESSID', value => $authorized_session_id } ); |
| 295 |
$tx->req->env( { REMOTE_ADDR => $remote_address } ); |
| 296 |
$t->request_ok($tx) |
| 297 |
->status_is(200) |
| 298 |
->json_is( '/name' => 'London books' ); |
| 299 |
|
| 300 |
# Authorized attempt to write invalid data |
| 301 |
my $vendor_with_invalid_field = { |
| 302 |
blah => "Blah", |
| 303 |
address1 => "Address 1", |
| 304 |
address2 => "Address 2", |
| 305 |
address3 => "Address 3" |
| 306 |
}; |
| 307 |
|
| 308 |
$tx = $t->ua->build_tx( |
| 309 |
PUT => "/api/v1/acquisitions/vendors/$vendor_id" => json => $vendor_with_invalid_field ); |
| 310 |
$tx->req->cookies( { name => 'CGISESSID', value => $authorized_session_id } ); |
| 311 |
$tx->req->env( { REMOTE_ADDR => $remote_address } ); |
| 312 |
$t->request_ok($tx) |
| 313 |
->status_is(400) |
| 314 |
->json_is( |
| 315 |
"/errors" => [ |
| 316 |
{ message => "Properties not allowed: blah.", |
| 317 |
path => "/body" |
| 318 |
} |
| 319 |
] |
| 320 |
); |
| 321 |
|
| 322 |
my $non_existent_id = $vendor_id + 1; |
| 323 |
$tx = $t->ua->build_tx( PUT => "/api/v1/acquisitions/vendors/$non_existent_id" => json => |
| 324 |
$vendor_with_updated_field ); |
| 325 |
$tx->req->cookies( { name => 'CGISESSID', value => $authorized_session_id } ); |
| 326 |
$tx->req->env( { REMOTE_ADDR => $remote_address } ); |
| 327 |
$t->request_ok($tx)->status_is(404); |
| 328 |
|
| 329 |
$schema->storage->txn_rollback; |
| 330 |
|
| 331 |
# Wrong method (POST) |
| 332 |
$vendor_with_updated_field->{id} = 2; |
| 333 |
|
| 334 |
$tx = $t->ua->build_tx( |
| 335 |
POST => "/api/v1/acquisitions/vendors/$vendor_id" => json => $vendor_with_updated_field ); |
| 336 |
$tx->req->cookies( { name => 'CGISESSID', value => $authorized_session_id } ); |
| 337 |
$tx->req->env( { REMOTE_ADDR => $remote_address } ); |
| 338 |
$t->request_ok($tx) |
| 339 |
->status_is(404); |
| 340 |
}; |
| 341 |
|
| 342 |
subtest 'delete() tests' => sub { |
| 343 |
|
| 344 |
plan tests => 7; |
| 345 |
|
| 346 |
$schema->storage->txn_begin; |
| 347 |
|
| 348 |
my ( $unauthorized_borrowernumber, $unauthorized_session_id ) |
| 349 |
= create_user_and_session( { authorized => 0 } ); |
| 350 |
my ( $authorized_borrowernumber, $authorized_session_id ) |
| 351 |
= create_user_and_session( { authorized => 1 } ); |
| 352 |
|
| 353 |
my $vendor_id = $builder->build( { source => 'Aqbookseller' } )->{id}; |
| 354 |
|
| 355 |
# Unauthorized attempt to update |
| 356 |
my $tx = $t->ua->build_tx( DELETE => "/api/v1/acquisitions/vendors/$vendor_id" ); |
| 357 |
$tx->req->cookies( { name => 'CGISESSID', value => $unauthorized_session_id } ); |
| 358 |
$tx->req->env( { REMOTE_ADDR => $remote_address } ); |
| 359 |
$t->request_ok($tx) |
| 360 |
->status_is(403); |
| 361 |
|
| 362 |
$tx = $t->ua->build_tx( DELETE => "/api/v1/acquisitions/vendors/$vendor_id" ); |
| 363 |
$tx->req->cookies( { name => 'CGISESSID', value => $authorized_session_id } ); |
| 364 |
$tx->req->env( { REMOTE_ADDR => $remote_address } ); |
| 365 |
$t->request_ok($tx) |
| 366 |
->status_is(200) |
| 367 |
->content_is(q{""}); |
| 368 |
|
| 369 |
$tx = $t->ua->build_tx( DELETE => "/api/v1/acquisitions/vendors/$vendor_id" ); |
| 370 |
$tx->req->cookies( { name => 'CGISESSID', value => $authorized_session_id } ); |
| 371 |
$tx->req->env( { REMOTE_ADDR => $remote_address } ); |
| 372 |
$t->request_ok($tx) |
| 373 |
->status_is(404); |
| 374 |
|
| 375 |
$schema->storage->txn_rollback; |
| 376 |
}; |
| 377 |
|
| 378 |
sub create_user_and_session { |
| 379 |
|
| 380 |
my $args = shift; |
| 381 |
my $flags = ( $args->{authorized} ) ? 2052 : 0; |
| 382 |
|
| 383 |
# my $flags = ( $args->{authorized} ) ? $args->{authorized} : 0; |
| 384 |
my $dbh = C4::Context->dbh; |
| 385 |
|
| 386 |
my $user = $builder->build( |
| 387 |
{ source => 'Borrower', |
| 388 |
value => { flags => $flags } |
| 389 |
} |
| 390 |
); |
| 391 |
|
| 392 |
# Create a session for the authorized user |
| 393 |
my $session = C4::Auth::get_session(''); |
| 394 |
$session->param( 'number', $user->{borrowernumber} ); |
| 395 |
$session->param( 'id', $user->{userid} ); |
| 396 |
$session->param( 'ip', '127.0.0.1' ); |
| 397 |
$session->param( 'lasttime', time() ); |
| 398 |
$session->flush; |
| 399 |
|
| 400 |
if ( $args->{authorized} ) { |
| 401 |
$dbh->do( |
| 402 |
q{ |
| 403 |
INSERT INTO user_permissions (borrowernumber,module_bit,code) |
| 404 |
VALUES (?,11,'vendors_manage')}, |
| 405 |
undef, $user->{borrowernumber} |
| 406 |
); |
| 407 |
} |
| 408 |
|
| 409 |
return ( $user->{borrowernumber}, $session->id ); |
| 410 |
} |
| 411 |
|
| 412 |
1; |