|
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 Mojo::JSON qw(encode_json); |
| 24 |
|
| 25 |
use t::lib::TestBuilder; |
| 26 |
use t::lib::Mocks; |
| 27 |
|
| 28 |
use Koha::StockRotationRotas; |
| 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::StockRotationRotas->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 |
## Authorized user tests |
| 66 |
# No rotas, so empty array should be returned |
| 67 |
$t->get_ok("//$userid:$password@/api/v1/rotas")->status_is(200) |
| 68 |
->json_is( [] ); |
| 69 |
|
| 70 |
my $active_rota = $builder->build_object( |
| 71 |
{ class => 'Koha::StockRotationRotas', value => { active => 1 } } ); |
| 72 |
|
| 73 |
# One rota created, should get returned |
| 74 |
$t->get_ok("//$userid:$password@/api/v1/rotas")->status_is(200) |
| 75 |
->json_is( [ $active_rota->to_api ] ); |
| 76 |
|
| 77 |
my $another_active_rota = $builder->build_object( |
| 78 |
{ class => 'Koha::StockRotationRotas', value => { active => 1 } } ); |
| 79 |
my $inactive_rota = |
| 80 |
$builder->build_object( { class => 'Koha::StockRotationRotas' } ); |
| 81 |
|
| 82 |
# Three rotas created, all should both be returned |
| 83 |
$t->get_ok("//$userid:$password@/api/v1/rotas")->status_is(200)->json_is( |
| 84 |
[ |
| 85 |
$active_rota->to_api, $another_active_rota->to_api, |
| 86 |
$inactive_rota->to_api |
| 87 |
] |
| 88 |
); |
| 89 |
|
| 90 |
# Filtering works, two rotas are active |
| 91 |
my $api_filter = encode_json( { 'me.active' => Mojo::JSON->true } ); |
| 92 |
$t->get_ok("//$userid:$password@/api/v1/rotas?q=$api_filter") |
| 93 |
->status_is(200) |
| 94 |
->json_is( [ $active_rota->to_api, $another_active_rota->to_api ] ); |
| 95 |
|
| 96 |
$api_filter = encode_json( { 'me.title' => $active_rota->title } ); |
| 97 |
$t->get_ok("//$userid:$password@/api/v1/rotas?q=$api_filter") |
| 98 |
->status_is(200)->json_is( [ $active_rota->to_api ] ); |
| 99 |
|
| 100 |
# Warn on unsupported query parameter |
| 101 |
$t->get_ok( |
| 102 |
"//$userid:$password@/api/v1/rotas?title=" . $active_rota->title ) |
| 103 |
->status_is(400) |
| 104 |
->json_is( |
| 105 |
[ { path => '/query/title', message => 'Malformed query string' } ] ); |
| 106 |
|
| 107 |
# Unauthorized access |
| 108 |
$t->get_ok("//$unauth_userid:$password@/api/v1/rotas")->status_is(403); |
| 109 |
|
| 110 |
$schema->storage->txn_rollback; |
| 111 |
}; |
| 112 |
|
| 113 |
subtest 'get() tests' => sub { |
| 114 |
|
| 115 |
plan tests => 8; |
| 116 |
|
| 117 |
$schema->storage->txn_begin; |
| 118 |
|
| 119 |
my $rota = |
| 120 |
$builder->build_object( { class => 'Koha::StockRotationRotas' } ); |
| 121 |
my $librarian = $builder->build_object( |
| 122 |
{ |
| 123 |
class => 'Koha::Patrons', |
| 124 |
value => { flags => 2**2 } # catalogue flag = 2 |
| 125 |
} |
| 126 |
); |
| 127 |
my $password = 'thePassword123'; |
| 128 |
$librarian->set_password( { password => $password, skip_validation => 1 } ); |
| 129 |
my $userid = $librarian->userid; |
| 130 |
|
| 131 |
my $patron = $builder->build_object( |
| 132 |
{ |
| 133 |
class => 'Koha::Patrons', |
| 134 |
value => { flags => 0 } |
| 135 |
} |
| 136 |
); |
| 137 |
|
| 138 |
$patron->set_password( { password => $password, skip_validation => 1 } ); |
| 139 |
my $unauth_userid = $patron->userid; |
| 140 |
|
| 141 |
$t->get_ok( "//$userid:$password@/api/v1/rotas/" . $rota->rota_id ) |
| 142 |
->status_is(200)->json_is( $rota->to_api ); |
| 143 |
|
| 144 |
$t->get_ok( "//$unauth_userid:$password@/api/v1/rotas/" . $rota->rota_id ) |
| 145 |
->status_is(403); |
| 146 |
|
| 147 |
my $rota_to_delete = |
| 148 |
$builder->build_object( { class => 'Koha::StockRotationRotas' } ); |
| 149 |
my $non_existent_id = $rota_to_delete->id; |
| 150 |
$rota_to_delete->delete; |
| 151 |
|
| 152 |
$t->get_ok("//$userid:$password@/api/v1/rotas/$non_existent_id") |
| 153 |
->status_is(404)->json_is( '/error' => 'Rota not found' ); |
| 154 |
|
| 155 |
$schema->storage->txn_rollback; |
| 156 |
}; |
| 157 |
|
| 158 |
subtest 'add() tests' => sub { |
| 159 |
|
| 160 |
plan tests => 18; |
| 161 |
|
| 162 |
$schema->storage->txn_begin; |
| 163 |
|
| 164 |
my $librarian = $builder->build_object( |
| 165 |
{ |
| 166 |
class => 'Koha::Patrons', |
| 167 |
value => { flags => 2**24 } # stockrotation flag = 24 |
| 168 |
} |
| 169 |
); |
| 170 |
my $password = 'thePassword123'; |
| 171 |
$librarian->set_password( { password => $password, skip_validation => 1 } ); |
| 172 |
my $userid = $librarian->userid; |
| 173 |
|
| 174 |
my $patron = $builder->build_object( |
| 175 |
{ |
| 176 |
class => 'Koha::Patrons', |
| 177 |
value => { flags => 0 } |
| 178 |
} |
| 179 |
); |
| 180 |
|
| 181 |
$patron->set_password( { password => $password, skip_validation => 1 } ); |
| 182 |
my $unauth_userid = $patron->userid; |
| 183 |
|
| 184 |
my $rota = { |
| 185 |
title => "Rota Title", |
| 186 |
description => "Rota Description", |
| 187 |
active => Mojo::JSON->true, |
| 188 |
cyclical => Mojo::JSON->true |
| 189 |
}; |
| 190 |
|
| 191 |
# Unauthorized attempt to write |
| 192 |
$t->post_ok( "//$unauth_userid:$password@/api/v1/rotas" => json => $rota ) |
| 193 |
->status_is(403); |
| 194 |
|
| 195 |
# Authorized attempt to write invalid data |
| 196 |
my $rota_with_invalid_field = { |
| 197 |
blah => "Rota Blah", |
| 198 |
description => "Rota Description", |
| 199 |
active => Mojo::JSON->true, |
| 200 |
cyclical => Mojo::JSON->true |
| 201 |
}; |
| 202 |
|
| 203 |
$t->post_ok( "//$userid:$password@/api/v1/rotas" => json => |
| 204 |
$rota_with_invalid_field )->status_is(400)->json_is( |
| 205 |
"/errors" => [ |
| 206 |
{ |
| 207 |
message => "Properties not allowed: blah.", |
| 208 |
path => "/body" |
| 209 |
} |
| 210 |
] |
| 211 |
); |
| 212 |
|
| 213 |
# Authorized attempt to write |
| 214 |
my $rota_id = |
| 215 |
$t->post_ok( "//$userid:$password@/api/v1/rotas" => json => $rota ) |
| 216 |
->status_is( 201, 'SWAGGER3.2.1' )->header_like( |
| 217 |
Location => qr|^\/api\/v1\/rotas/\d*|, |
| 218 |
'SWAGGER3.4.1' |
| 219 |
)->json_is( '/title' => $rota->{title} ) |
| 220 |
->json_is( '/description' => $rota->{description} ) |
| 221 |
->json_is( '/active' => $rota->{active} ) |
| 222 |
->json_is( '/cyclical' => $rota->{cyclical} )->tx->res->json->{rota_id}; |
| 223 |
|
| 224 |
# Authorized attempt to create with null id |
| 225 |
$rota->{rota_id} = undef; |
| 226 |
$t->post_ok( "//$userid:$password@/api/v1/rotas" => json => $rota ) |
| 227 |
->status_is(400)->json_has('/errors'); |
| 228 |
|
| 229 |
# Authorized attempt to create with existing id |
| 230 |
$rota->{rota_id} = $rota_id; |
| 231 |
$t->post_ok( "//$userid:$password@/api/v1/rotas" => json => $rota ) |
| 232 |
->status_is(400)->json_is( |
| 233 |
"/errors" => [ |
| 234 |
{ |
| 235 |
message => "Read-only.", |
| 236 |
path => "/body/rota_id" |
| 237 |
} |
| 238 |
] |
| 239 |
); |
| 240 |
|
| 241 |
$schema->storage->txn_rollback; |
| 242 |
}; |
| 243 |
|
| 244 |
subtest 'update() tests' => sub { |
| 245 |
|
| 246 |
plan tests => 15; |
| 247 |
|
| 248 |
$schema->storage->txn_begin; |
| 249 |
|
| 250 |
my $librarian = $builder->build_object( |
| 251 |
{ |
| 252 |
class => 'Koha::Patrons', |
| 253 |
value => { flags => 2**24 } # stockrotation flag = 24 |
| 254 |
} |
| 255 |
); |
| 256 |
my $password = 'thePassword123'; |
| 257 |
$librarian->set_password( { password => $password, skip_validation => 1 } ); |
| 258 |
my $userid = $librarian->userid; |
| 259 |
|
| 260 |
my $patron = $builder->build_object( |
| 261 |
{ |
| 262 |
class => 'Koha::Patrons', |
| 263 |
value => { flags => 0 } |
| 264 |
} |
| 265 |
); |
| 266 |
|
| 267 |
$patron->set_password( { password => $password, skip_validation => 1 } ); |
| 268 |
my $unauth_userid = $patron->userid; |
| 269 |
|
| 270 |
my $rota_id = |
| 271 |
$builder->build_object( { class => 'Koha::StockRotationRotas' } )->id; |
| 272 |
|
| 273 |
# Unauthorized attempt to update |
| 274 |
$t->put_ok( "//$unauth_userid:$password@/api/v1/rotas/$rota_id" => json => |
| 275 |
{ title => 'New unauthorized title change' } )->status_is(403); |
| 276 |
|
| 277 |
# Attempt partial update on a PUT |
| 278 |
my $rota_with_missing_field = { |
| 279 |
title => 'New title', |
| 280 |
description => 'New description', |
| 281 |
cyclical => Mojo::JSON->false |
| 282 |
}; |
| 283 |
|
| 284 |
$t->put_ok( "//$userid:$password@/api/v1/rotas/$rota_id" => json => |
| 285 |
$rota_with_missing_field )->status_is(400) |
| 286 |
->json_is( "/errors" => |
| 287 |
[ { message => "Missing property.", path => "/body/active" } ] ); |
| 288 |
|
| 289 |
# Full object update on PUT |
| 290 |
my $rota_with_updated_field = { |
| 291 |
title => "London", |
| 292 |
description => "Rota Description", |
| 293 |
active => Mojo::JSON->true, |
| 294 |
cyclical => Mojo::JSON->false |
| 295 |
}; |
| 296 |
|
| 297 |
$t->put_ok( "//$userid:$password@/api/v1/rotas/$rota_id" => json => |
| 298 |
$rota_with_updated_field )->status_is(200) |
| 299 |
->json_is( '/title' => 'London' ); |
| 300 |
|
| 301 |
# Authorized attempt to write invalid data |
| 302 |
my $rota_with_invalid_field = { |
| 303 |
blah => "Rota Blah", |
| 304 |
description => "Rota Description", |
| 305 |
active => Mojo::JSON->true, |
| 306 |
cyclical => Mojo::JSON->false |
| 307 |
}; |
| 308 |
|
| 309 |
$t->put_ok( "//$userid:$password@/api/v1/rotas/$rota_id" => json => |
| 310 |
$rota_with_invalid_field )->status_is(400)->json_is( |
| 311 |
"/errors" => [ |
| 312 |
{ |
| 313 |
message => "Properties not allowed: blah.", |
| 314 |
path => "/body" |
| 315 |
} |
| 316 |
] |
| 317 |
); |
| 318 |
|
| 319 |
my $rota_to_delete = |
| 320 |
$builder->build_object( { class => 'Koha::StockRotationRotas' } ); |
| 321 |
my $non_existent_id = $rota_to_delete->id; |
| 322 |
$rota_to_delete->delete; |
| 323 |
|
| 324 |
$t->put_ok( "//$userid:$password@/api/v1/rotas/$non_existent_id" => json => |
| 325 |
$rota_with_updated_field )->status_is(404); |
| 326 |
|
| 327 |
# Wrong method (POST) |
| 328 |
$rota_with_updated_field->{rota_id} = 2; |
| 329 |
|
| 330 |
$t->post_ok( "//$userid:$password@/api/v1/rotas/$rota_id" => json => |
| 331 |
$rota_with_updated_field )->status_is(404); |
| 332 |
|
| 333 |
$schema->storage->txn_rollback; |
| 334 |
}; |
| 335 |
|
| 336 |
subtest 'delete() tests' => sub { |
| 337 |
|
| 338 |
plan tests => 7; |
| 339 |
|
| 340 |
$schema->storage->txn_begin; |
| 341 |
|
| 342 |
my $librarian = $builder->build_object( |
| 343 |
{ |
| 344 |
class => 'Koha::Patrons', |
| 345 |
value => { flags => 2**24 } # stockrotation flag = 24 |
| 346 |
} |
| 347 |
); |
| 348 |
my $password = 'thePassword123'; |
| 349 |
$librarian->set_password( { password => $password, skip_validation => 1 } ); |
| 350 |
my $userid = $librarian->userid; |
| 351 |
|
| 352 |
my $patron = $builder->build_object( |
| 353 |
{ |
| 354 |
class => 'Koha::Patrons', |
| 355 |
value => { flags => 0 } |
| 356 |
} |
| 357 |
); |
| 358 |
|
| 359 |
$patron->set_password( { password => $password, skip_validation => 1 } ); |
| 360 |
my $unauth_userid = $patron->userid; |
| 361 |
|
| 362 |
my $rota_id = |
| 363 |
$builder->build_object( { class => 'Koha::StockRotationRotas' } )->id; |
| 364 |
|
| 365 |
# Unauthorized attempt to delete |
| 366 |
$t->delete_ok("//$unauth_userid:$password@/api/v1/rotas/$rota_id") |
| 367 |
->status_is(403); |
| 368 |
|
| 369 |
$t->delete_ok("//$userid:$password@/api/v1/rotas/$rota_id") |
| 370 |
->status_is( 204, 'SWAGGER3.2.4' )->content_is( '', 'SWAGGER3.3.4' ); |
| 371 |
|
| 372 |
$t->delete_ok("//$userid:$password@/api/v1/rotas/$rota_id")->status_is(404); |
| 373 |
|
| 374 |
$schema->storage->txn_rollback; |
| 375 |
}; |