|
Line 0
Link Here
|
|
|
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 <https://www.gnu.org/licenses>. |
| 17 |
|
| 18 |
use Modern::Perl; |
| 19 |
|
| 20 |
use Test::NoWarnings; |
| 21 |
use Test::More tests => 9; |
| 22 |
use Test::Mojo; |
| 23 |
|
| 24 |
use t::lib::TestBuilder; |
| 25 |
use t::lib::Mocks; |
| 26 |
|
| 27 |
use JSON; |
| 28 |
|
| 29 |
use Koha::Auth::Hostname; |
| 30 |
use Koha::Auth::Hostnames; |
| 31 |
use Koha::Auth::Identity::Provider::Hostnames; |
| 32 |
use Koha::Auth::Identity::Providers; |
| 33 |
use Koha::Database; |
| 34 |
|
| 35 |
my $schema = Koha::Database->new->schema; |
| 36 |
my $builder = t::lib::TestBuilder->new; |
| 37 |
|
| 38 |
t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 ); |
| 39 |
|
| 40 |
my $t = Test::Mojo->new('Koha::REST::V1'); |
| 41 |
|
| 42 |
my $password = 'thePassword123'; |
| 43 |
|
| 44 |
sub authorized_patron { |
| 45 |
my $patron = $builder->build_object( |
| 46 |
{ |
| 47 |
class => 'Koha::Patrons', |
| 48 |
value => { flags => 0 } |
| 49 |
} |
| 50 |
); |
| 51 |
$builder->build( |
| 52 |
{ |
| 53 |
source => 'UserPermission', |
| 54 |
value => { |
| 55 |
borrowernumber => $patron->borrowernumber, |
| 56 |
module_bit => 3, |
| 57 |
code => 'manage_identity_providers', |
| 58 |
}, |
| 59 |
} |
| 60 |
); |
| 61 |
$patron->set_password( { password => $password, skip_validation => 1 } ); |
| 62 |
return $patron; |
| 63 |
} |
| 64 |
|
| 65 |
sub unauthorized_patron { |
| 66 |
my $patron = $builder->build_object( |
| 67 |
{ |
| 68 |
class => 'Koha::Patrons', |
| 69 |
value => { flags => 0 } |
| 70 |
} |
| 71 |
); |
| 72 |
$patron->set_password( { password => $password, skip_validation => 1 } ); |
| 73 |
return $patron; |
| 74 |
} |
| 75 |
|
| 76 |
subtest 'list() tests for /auth/hostnames' => sub { |
| 77 |
|
| 78 |
plan tests => 8; |
| 79 |
|
| 80 |
$schema->storage->txn_begin; |
| 81 |
|
| 82 |
Koha::Auth::Hostnames->search->delete; |
| 83 |
|
| 84 |
my $auth_patron = authorized_patron(); |
| 85 |
my $unauth_patron = unauthorized_patron(); |
| 86 |
|
| 87 |
my $auth_userid = $auth_patron->userid; |
| 88 |
my $unauth_userid = $unauth_patron->userid; |
| 89 |
|
| 90 |
# Unauthorized |
| 91 |
$t->get_ok("//$unauth_userid:$password@/api/v1/auth/hostnames")->status_is(403); |
| 92 |
|
| 93 |
# Empty list |
| 94 |
$t->get_ok("//$auth_userid:$password@/api/v1/auth/hostnames")->status_is(200)->json_is( [] ); |
| 95 |
|
| 96 |
# With a hostname |
| 97 |
$builder->build_object( |
| 98 |
{ |
| 99 |
class => 'Koha::Auth::Hostnames', |
| 100 |
value => { hostname => 'library.example.com' } |
| 101 |
} |
| 102 |
); |
| 103 |
|
| 104 |
$t->get_ok("//$auth_userid:$password@/api/v1/auth/hostnames")->status_is(200)->json_has("/0/hostname_id"); |
| 105 |
|
| 106 |
$schema->storage->txn_rollback; |
| 107 |
}; |
| 108 |
|
| 109 |
subtest 'get() tests for /auth/hostnames/{hostname_id}' => sub { |
| 110 |
|
| 111 |
plan tests => 8; |
| 112 |
|
| 113 |
$schema->storage->txn_begin; |
| 114 |
|
| 115 |
my $auth_patron = authorized_patron(); |
| 116 |
my $unauth_patron = unauthorized_patron(); |
| 117 |
|
| 118 |
my $auth_userid = $auth_patron->userid; |
| 119 |
my $unauth_userid = $unauth_patron->userid; |
| 120 |
|
| 121 |
my $hostname = $builder->build_object( |
| 122 |
{ |
| 123 |
class => 'Koha::Auth::Hostnames', |
| 124 |
value => { hostname => 'lib.example.org' } |
| 125 |
} |
| 126 |
); |
| 127 |
my $hid = $hostname->hostname_id; |
| 128 |
|
| 129 |
# Unauthorized |
| 130 |
$t->get_ok("//$unauth_userid:$password@/api/v1/auth/hostnames/$hid")->status_is(403); |
| 131 |
|
| 132 |
# Not found |
| 133 |
$t->get_ok("//$auth_userid:$password@/api/v1/auth/hostnames/999999")->status_is(404); |
| 134 |
|
| 135 |
# Found |
| 136 |
$t->get_ok("//$auth_userid:$password@/api/v1/auth/hostnames/$hid") |
| 137 |
->status_is(200) |
| 138 |
->json_is( '/hostname_id', $hid ) |
| 139 |
->json_is( '/hostname', 'lib.example.org' ); |
| 140 |
|
| 141 |
$schema->storage->txn_rollback; |
| 142 |
}; |
| 143 |
|
| 144 |
subtest 'list() tests for /auth/identity_providers/{id}/hostnames' => sub { |
| 145 |
|
| 146 |
plan tests => 10; |
| 147 |
|
| 148 |
$schema->storage->txn_begin; |
| 149 |
|
| 150 |
my $auth_patron = authorized_patron(); |
| 151 |
my $unauth_patron = unauthorized_patron(); |
| 152 |
|
| 153 |
my $auth_userid = $auth_patron->userid; |
| 154 |
my $unauth_userid = $unauth_patron->userid; |
| 155 |
|
| 156 |
my $provider = $builder->build_object( { class => 'Koha::Auth::Identity::Providers' } ); |
| 157 |
my $pid = $provider->identity_provider_id; |
| 158 |
|
| 159 |
# Unauthorized |
| 160 |
$t->get_ok("//$unauth_userid:$password@/api/v1/auth/identity_providers/$pid/hostnames")->status_is(403); |
| 161 |
|
| 162 |
# Non-existent provider |
| 163 |
$t->get_ok("//$auth_userid:$password@/api/v1/auth/identity_providers/999999/hostnames")->status_is(404); |
| 164 |
|
| 165 |
# Empty list |
| 166 |
$t->get_ok("//$auth_userid:$password@/api/v1/auth/identity_providers/$pid/hostnames") |
| 167 |
->status_is(200) |
| 168 |
->json_is( [] ); |
| 169 |
|
| 170 |
# With a hostname association |
| 171 |
my $hostname = |
| 172 |
$builder->build_object( { class => 'Koha::Auth::Hostnames', value => { hostname => 'provider.example.com' } } ); |
| 173 |
$builder->build_object( |
| 174 |
{ |
| 175 |
class => 'Koha::Auth::Identity::Provider::Hostnames', |
| 176 |
value => { |
| 177 |
identity_provider_id => $pid, |
| 178 |
hostname_id => $hostname->hostname_id, |
| 179 |
is_enabled => 1, |
| 180 |
force_sso => 0, |
| 181 |
} |
| 182 |
} |
| 183 |
); |
| 184 |
|
| 185 |
$t->get_ok("//$auth_userid:$password@/api/v1/auth/identity_providers/$pid/hostnames") |
| 186 |
->status_is(200) |
| 187 |
->json_has("/0/identity_provider_hostname_id"); |
| 188 |
|
| 189 |
$schema->storage->txn_rollback; |
| 190 |
}; |
| 191 |
|
| 192 |
subtest 'get() tests for /auth/identity_providers/{id}/hostnames/{hid}' => sub { |
| 193 |
|
| 194 |
plan tests => 10; |
| 195 |
|
| 196 |
$schema->storage->txn_begin; |
| 197 |
|
| 198 |
my $auth_patron = authorized_patron(); |
| 199 |
my $unauth_patron = unauthorized_patron(); |
| 200 |
|
| 201 |
my $auth_userid = $auth_patron->userid; |
| 202 |
my $unauth_userid = $unauth_patron->userid; |
| 203 |
|
| 204 |
my $provider = $builder->build_object( { class => 'Koha::Auth::Identity::Providers' } ); |
| 205 |
my $pid = $provider->identity_provider_id; |
| 206 |
|
| 207 |
my $hostname = |
| 208 |
$builder->build_object( { class => 'Koha::Auth::Hostnames', value => { hostname => 'get.example.com' } } ); |
| 209 |
|
| 210 |
my $ph = $builder->build_object( |
| 211 |
{ |
| 212 |
class => 'Koha::Auth::Identity::Provider::Hostnames', |
| 213 |
value => { |
| 214 |
identity_provider_id => $pid, |
| 215 |
hostname_id => $hostname->hostname_id, |
| 216 |
is_enabled => 1, |
| 217 |
force_sso => 0, |
| 218 |
} |
| 219 |
} |
| 220 |
); |
| 221 |
my $phid = $ph->identity_provider_hostname_id; |
| 222 |
|
| 223 |
# Unauthorized |
| 224 |
$t->get_ok("//$unauth_userid:$password@/api/v1/auth/identity_providers/$pid/hostnames/$phid")->status_is(403); |
| 225 |
|
| 226 |
# Non-existent provider |
| 227 |
$t->get_ok("//$auth_userid:$password@/api/v1/auth/identity_providers/999999/hostnames/$phid")->status_is(404); |
| 228 |
|
| 229 |
# Non-existent hostname association |
| 230 |
$t->get_ok("//$auth_userid:$password@/api/v1/auth/identity_providers/$pid/hostnames/999999")->status_is(404); |
| 231 |
|
| 232 |
# Found |
| 233 |
$t->get_ok("//$auth_userid:$password@/api/v1/auth/identity_providers/$pid/hostnames/$phid") |
| 234 |
->status_is(200) |
| 235 |
->json_is( '/identity_provider_hostname_id', $phid ) |
| 236 |
->json_is( '/hostname', 'get.example.com' ); |
| 237 |
|
| 238 |
$schema->storage->txn_rollback; |
| 239 |
}; |
| 240 |
|
| 241 |
subtest 'add() tests - hostname string' => sub { |
| 242 |
|
| 243 |
plan tests => 12; |
| 244 |
|
| 245 |
$schema->storage->txn_begin; |
| 246 |
|
| 247 |
my $auth_patron = authorized_patron(); |
| 248 |
my $unauth_patron = unauthorized_patron(); |
| 249 |
|
| 250 |
my $auth_userid = $auth_patron->userid; |
| 251 |
my $unauth_userid = $unauth_patron->userid; |
| 252 |
|
| 253 |
my $provider = $builder->build_object( { class => 'Koha::Auth::Identity::Providers' } ); |
| 254 |
my $pid = $provider->identity_provider_id; |
| 255 |
|
| 256 |
my $new_hostname = { |
| 257 |
hostname => 'add.example.com', |
| 258 |
is_enabled => JSON::true, |
| 259 |
force_sso => JSON::false, |
| 260 |
}; |
| 261 |
|
| 262 |
# Unauthorized |
| 263 |
$t->post_ok( "//$unauth_userid:$password@/api/v1/auth/identity_providers/$pid/hostnames" => json => $new_hostname ) |
| 264 |
->status_is(403); |
| 265 |
|
| 266 |
# Non-existent provider |
| 267 |
$t->post_ok( "//$auth_userid:$password@/api/v1/auth/identity_providers/999999/hostnames" => json => $new_hostname ) |
| 268 |
->status_is(404); |
| 269 |
|
| 270 |
# Valid creation using hostname string (find-or-create) |
| 271 |
$t->post_ok( "//$auth_userid:$password@/api/v1/auth/identity_providers/$pid/hostnames" => json => $new_hostname ) |
| 272 |
->status_is(201) |
| 273 |
->json_is( '/hostname', 'add.example.com' ) |
| 274 |
->json_is( '/identity_provider_id', $pid ) |
| 275 |
->json_has('/identity_provider_hostname_id') |
| 276 |
->header_like( 'Location', qr|/api/v1/auth/identity_providers/$pid/hostnames/\d+| ); |
| 277 |
|
| 278 |
# Duplicate should return 409 (suppress the expected Koha::Object DuplicateID warn) |
| 279 |
{ |
| 280 |
local $SIG{__WARN__} = sub { }; |
| 281 |
$t->post_ok( |
| 282 |
"//$auth_userid:$password@/api/v1/auth/identity_providers/$pid/hostnames" => json => $new_hostname ) |
| 283 |
->status_is(409); |
| 284 |
} |
| 285 |
|
| 286 |
$schema->storage->txn_rollback; |
| 287 |
}; |
| 288 |
|
| 289 |
subtest 'add() tests - hostname_id' => sub { |
| 290 |
|
| 291 |
plan tests => 6; |
| 292 |
|
| 293 |
$schema->storage->txn_begin; |
| 294 |
|
| 295 |
my $auth_patron = authorized_patron(); |
| 296 |
my $auth_userid = $auth_patron->userid; |
| 297 |
|
| 298 |
my $provider = $builder->build_object( { class => 'Koha::Auth::Identity::Providers' } ); |
| 299 |
my $pid = $provider->identity_provider_id; |
| 300 |
|
| 301 |
my $hostname = |
| 302 |
$builder->build_object( { class => 'Koha::Auth::Hostnames', value => { hostname => 'byid.example.com' } } ); |
| 303 |
|
| 304 |
my $new_hostname = { |
| 305 |
hostname_id => $hostname->hostname_id, |
| 306 |
is_enabled => JSON::true, |
| 307 |
force_sso => JSON::false, |
| 308 |
}; |
| 309 |
|
| 310 |
# Valid creation using hostname_id |
| 311 |
$t->post_ok( "//$auth_userid:$password@/api/v1/auth/identity_providers/$pid/hostnames" => json => $new_hostname ) |
| 312 |
->status_is(201) |
| 313 |
->json_is( '/hostname', 'byid.example.com' ) |
| 314 |
->json_is( '/hostname_id', $hostname->hostname_id ) |
| 315 |
->json_is( '/identity_provider_id', $pid ) |
| 316 |
->json_has('/identity_provider_hostname_id'); |
| 317 |
|
| 318 |
$schema->storage->txn_rollback; |
| 319 |
}; |
| 320 |
|
| 321 |
subtest 'update() tests' => sub { |
| 322 |
|
| 323 |
plan tests => 9; |
| 324 |
|
| 325 |
$schema->storage->txn_begin; |
| 326 |
|
| 327 |
my $auth_patron = authorized_patron(); |
| 328 |
my $unauth_patron = unauthorized_patron(); |
| 329 |
|
| 330 |
my $auth_userid = $auth_patron->userid; |
| 331 |
my $unauth_userid = $unauth_patron->userid; |
| 332 |
|
| 333 |
my $provider = $builder->build_object( { class => 'Koha::Auth::Identity::Providers' } ); |
| 334 |
my $pid = $provider->identity_provider_id; |
| 335 |
|
| 336 |
my $hostname = |
| 337 |
$builder->build_object( { class => 'Koha::Auth::Hostnames', value => { hostname => 'update.example.com' } } ); |
| 338 |
|
| 339 |
my $ph = $builder->build_object( |
| 340 |
{ |
| 341 |
class => 'Koha::Auth::Identity::Provider::Hostnames', |
| 342 |
value => { |
| 343 |
identity_provider_id => $pid, |
| 344 |
hostname_id => $hostname->hostname_id, |
| 345 |
is_enabled => 1, |
| 346 |
force_sso => 0, |
| 347 |
} |
| 348 |
} |
| 349 |
); |
| 350 |
my $phid = $ph->identity_provider_hostname_id; |
| 351 |
|
| 352 |
my $update = { |
| 353 |
hostname_id => $hostname->hostname_id, |
| 354 |
is_enabled => JSON::false, |
| 355 |
force_sso => JSON::true, |
| 356 |
}; |
| 357 |
|
| 358 |
# Unauthorized |
| 359 |
$t->put_ok( "//$unauth_userid:$password@/api/v1/auth/identity_providers/$pid/hostnames/$phid" => json => $update ) |
| 360 |
->status_is(403); |
| 361 |
|
| 362 |
# Not found |
| 363 |
$t->put_ok( "//$auth_userid:$password@/api/v1/auth/identity_providers/$pid/hostnames/999999" => json => $update ) |
| 364 |
->status_is(404); |
| 365 |
|
| 366 |
# Valid update |
| 367 |
$t->put_ok( "//$auth_userid:$password@/api/v1/auth/identity_providers/$pid/hostnames/$phid" => json => $update ) |
| 368 |
->status_is(200) |
| 369 |
->json_is( '/identity_provider_hostname_id', $phid ) |
| 370 |
->json_is( '/is_enabled', JSON::false ) |
| 371 |
->json_is( '/force_sso', JSON::true ); |
| 372 |
|
| 373 |
$schema->storage->txn_rollback; |
| 374 |
}; |
| 375 |
|
| 376 |
subtest 'delete() tests' => sub { |
| 377 |
|
| 378 |
plan tests => 6; |
| 379 |
|
| 380 |
$schema->storage->txn_begin; |
| 381 |
|
| 382 |
my $auth_patron = authorized_patron(); |
| 383 |
my $unauth_patron = unauthorized_patron(); |
| 384 |
|
| 385 |
my $auth_userid = $auth_patron->userid; |
| 386 |
my $unauth_userid = $unauth_patron->userid; |
| 387 |
|
| 388 |
my $provider = $builder->build_object( { class => 'Koha::Auth::Identity::Providers' } ); |
| 389 |
my $pid = $provider->identity_provider_id; |
| 390 |
|
| 391 |
my $hostname = |
| 392 |
$builder->build_object( { class => 'Koha::Auth::Hostnames', value => { hostname => 'delete.example.com' } } ); |
| 393 |
|
| 394 |
my $ph = $builder->build_object( |
| 395 |
{ |
| 396 |
class => 'Koha::Auth::Identity::Provider::Hostnames', |
| 397 |
value => { |
| 398 |
identity_provider_id => $pid, |
| 399 |
hostname_id => $hostname->hostname_id, |
| 400 |
is_enabled => 1, |
| 401 |
force_sso => 0, |
| 402 |
} |
| 403 |
} |
| 404 |
); |
| 405 |
my $phid = $ph->identity_provider_hostname_id; |
| 406 |
|
| 407 |
# Unauthorized |
| 408 |
$t->delete_ok("//$unauth_userid:$password@/api/v1/auth/identity_providers/$pid/hostnames/$phid")->status_is(403); |
| 409 |
|
| 410 |
# Not found |
| 411 |
$t->delete_ok("//$auth_userid:$password@/api/v1/auth/identity_providers/$pid/hostnames/999999")->status_is(404); |
| 412 |
|
| 413 |
# Valid deletion |
| 414 |
$t->delete_ok("//$auth_userid:$password@/api/v1/auth/identity_providers/$pid/hostnames/$phid")->status_is(204); |
| 415 |
|
| 416 |
$schema->storage->txn_rollback; |
| 417 |
}; |