|
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 <https://www.gnu.org/licenses>. |
| 17 |
|
| 18 |
use Modern::Perl; |
| 19 |
|
| 20 |
use Test::NoWarnings; |
| 21 |
use Test::More tests => 6; |
| 22 |
use Test::Mojo; |
| 23 |
|
| 24 |
use t::lib::TestBuilder; |
| 25 |
use t::lib::Mocks; |
| 26 |
|
| 27 |
use Koha::ShibbolethFieldMappings; |
| 28 |
use Koha::Database; |
| 29 |
|
| 30 |
my $schema = Koha::Database->new->schema; |
| 31 |
my $builder = t::lib::TestBuilder->new; |
| 32 |
|
| 33 |
my $t = Test::Mojo->new('Koha::REST::V1'); |
| 34 |
t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 ); |
| 35 |
|
| 36 |
subtest 'list() tests' => sub { |
| 37 |
|
| 38 |
plan tests => 20; |
| 39 |
|
| 40 |
$schema->storage->txn_begin; |
| 41 |
|
| 42 |
Koha::ShibbolethFieldMappings->search->delete; |
| 43 |
|
| 44 |
my $librarian = $builder->build_object( |
| 45 |
{ |
| 46 |
class => 'Koha::Patrons', |
| 47 |
value => { flags => 2**3 } |
| 48 |
} |
| 49 |
); |
| 50 |
my $password = 'thePassword123'; |
| 51 |
$librarian->set_password( { password => $password, skip_validation => 1 } ); |
| 52 |
my $userid = $librarian->userid; |
| 53 |
|
| 54 |
my $patron = $builder->build_object( |
| 55 |
{ |
| 56 |
class => 'Koha::Patrons', |
| 57 |
value => { flags => 0 } |
| 58 |
} |
| 59 |
); |
| 60 |
|
| 61 |
$patron->set_password( { password => $password, skip_validation => 1 } ); |
| 62 |
my $unauth_userid = $patron->userid; |
| 63 |
|
| 64 |
$t->get_ok("//$userid:$password@/api/v1/shibboleth/mappings")->status_is(200)->json_is( [] ); |
| 65 |
|
| 66 |
my $mapping = $builder->build_object( { class => 'Koha::ShibbolethFieldMappings' } ); |
| 67 |
|
| 68 |
$t->get_ok("//$userid:$password@/api/v1/shibboleth/mappings")->status_is(200)->json_is( [ $mapping->to_api ] ); |
| 69 |
|
| 70 |
my $another_mapping = $builder->build_object( |
| 71 |
{ |
| 72 |
class => 'Koha::ShibbolethFieldMappings', |
| 73 |
value => { idp_field => 'eppn', koha_field => 'userid' } |
| 74 |
} |
| 75 |
); |
| 76 |
|
| 77 |
$t->get_ok("//$userid:$password@/api/v1/shibboleth/mappings")->status_is(200)->json_is( |
| 78 |
[ |
| 79 |
$mapping->to_api, |
| 80 |
$another_mapping->to_api, |
| 81 |
] |
| 82 |
); |
| 83 |
|
| 84 |
$mapping->delete; |
| 85 |
$another_mapping->delete; |
| 86 |
$t->get_ok(qq~//$userid:$password@/api/v1/shibboleth/mappings?q=[{"me.koha_field":{"like":"%user%"}}]~) |
| 87 |
->status_is(200)->json_is( [] ); |
| 88 |
|
| 89 |
my $mapping_to_search = $builder->build_object( |
| 90 |
{ |
| 91 |
class => 'Koha::ShibbolethFieldMappings', |
| 92 |
value => { |
| 93 |
koha_field => 'userid', |
| 94 |
} |
| 95 |
} |
| 96 |
); |
| 97 |
|
| 98 |
$t->get_ok(qq~//$userid:$password@/api/v1/shibboleth/mappings?q=[{"me.koha_field":{"like":"%user%"}}]~) |
| 99 |
->status_is(200)->json_is( [ $mapping_to_search->to_api ] ); |
| 100 |
|
| 101 |
$t->get_ok("//$userid:$password@/api/v1/shibboleth/mappings?blah=blah")->status_is(400) |
| 102 |
->json_is( [ { path => '/query/blah', message => 'Malformed query string' } ] ); |
| 103 |
|
| 104 |
$t->get_ok("//$unauth_userid:$password@/api/v1/shibboleth/mappings")->status_is(403); |
| 105 |
|
| 106 |
$schema->storage->txn_rollback; |
| 107 |
}; |
| 108 |
|
| 109 |
subtest 'get() tests' => sub { |
| 110 |
|
| 111 |
plan tests => 8; |
| 112 |
|
| 113 |
$schema->storage->txn_begin; |
| 114 |
|
| 115 |
my $mapping = $builder->build_object( { class => 'Koha::ShibbolethFieldMappings' } ); |
| 116 |
my $librarian = $builder->build_object( |
| 117 |
{ |
| 118 |
class => 'Koha::Patrons', |
| 119 |
value => { flags => 2**3 } |
| 120 |
} |
| 121 |
); |
| 122 |
my $password = 'thePassword123'; |
| 123 |
$librarian->set_password( { password => $password, skip_validation => 1 } ); |
| 124 |
my $userid = $librarian->userid; |
| 125 |
|
| 126 |
my $patron = $builder->build_object( |
| 127 |
{ |
| 128 |
class => 'Koha::Patrons', |
| 129 |
value => { flags => 0 } |
| 130 |
} |
| 131 |
); |
| 132 |
|
| 133 |
$patron->set_password( { password => $password, skip_validation => 1 } ); |
| 134 |
my $unauth_userid = $patron->userid; |
| 135 |
|
| 136 |
$t->get_ok( "//$userid:$password@/api/v1/shibboleth/mappings/" . $mapping->mapping_id )->status_is(200) |
| 137 |
->json_is( $mapping->to_api ); |
| 138 |
|
| 139 |
$t->get_ok( "//$unauth_userid:$password@/api/v1/shibboleth/mappings/" . $mapping->mapping_id )->status_is(403); |
| 140 |
|
| 141 |
my $mapping_to_delete = $builder->build_object( { class => 'Koha::ShibbolethFieldMappings' } ); |
| 142 |
my $non_existent_id = $mapping_to_delete->mapping_id; |
| 143 |
$mapping_to_delete->delete; |
| 144 |
|
| 145 |
$t->get_ok("//$userid:$password@/api/v1/shibboleth/mappings/$non_existent_id")->status_is(404) |
| 146 |
->json_is( '/error' => 'Mapping not found' ); |
| 147 |
|
| 148 |
$schema->storage->txn_rollback; |
| 149 |
}; |
| 150 |
|
| 151 |
subtest 'add() tests' => sub { |
| 152 |
|
| 153 |
plan tests => 19; |
| 154 |
|
| 155 |
$schema->storage->txn_begin; |
| 156 |
|
| 157 |
my $librarian = $builder->build_object( |
| 158 |
{ |
| 159 |
class => 'Koha::Patrons', |
| 160 |
value => { flags => 2**3 } |
| 161 |
} |
| 162 |
); |
| 163 |
my $password = 'thePassword123'; |
| 164 |
$librarian->set_password( { password => $password, skip_validation => 1 } ); |
| 165 |
my $userid = $librarian->userid; |
| 166 |
|
| 167 |
my $patron = $builder->build_object( |
| 168 |
{ |
| 169 |
class => 'Koha::Patrons', |
| 170 |
value => { flags => 0 } |
| 171 |
} |
| 172 |
); |
| 173 |
|
| 174 |
$patron->set_password( { password => $password, skip_validation => 1 } ); |
| 175 |
my $unauth_userid = $patron->userid; |
| 176 |
|
| 177 |
my $mapping = { |
| 178 |
idp_field => "mail", |
| 179 |
koha_field => "email", |
| 180 |
is_matchpoint => 0, |
| 181 |
}; |
| 182 |
|
| 183 |
$t->post_ok( "//$unauth_userid:$password@/api/v1/shibboleth/mappings" => json => $mapping )->status_is(403); |
| 184 |
|
| 185 |
my $mapping_with_invalid_field = { |
| 186 |
blah => "Mapping Blah", |
| 187 |
idp_field => "mail", |
| 188 |
koha_field => "email", |
| 189 |
is_matchpoint => 0, |
| 190 |
}; |
| 191 |
|
| 192 |
$t->post_ok( "//$userid:$password@/api/v1/shibboleth/mappings" => json => $mapping_with_invalid_field ) |
| 193 |
->status_is(400)->json_is( |
| 194 |
"/errors" => [ |
| 195 |
{ |
| 196 |
message => "Properties not allowed: blah.", |
| 197 |
path => "/body" |
| 198 |
} |
| 199 |
] |
| 200 |
); |
| 201 |
|
| 202 |
my $mapping_id = |
| 203 |
$t->post_ok( "//$userid:$password@/api/v1/shibboleth/mappings" => json => $mapping ) |
| 204 |
->status_is( 201, 'REST3.2.1' )->header_like( |
| 205 |
Location => qr|^/api/v1/shibboleth/mappings/\d*|, |
| 206 |
'REST3.4.1' |
| 207 |
)->json_is( '/idp_field' => $mapping->{idp_field} )->json_is( '/koha_field' => $mapping->{koha_field} ) |
| 208 |
->tx->res->json->{mapping_id}; |
| 209 |
|
| 210 |
$mapping->{mapping_id} = undef; |
| 211 |
$t->post_ok( "//$userid:$password@/api/v1/shibboleth/mappings" => json => $mapping )->status_is(400) |
| 212 |
->json_has('/errors'); |
| 213 |
|
| 214 |
$mapping->{mapping_id} = $mapping_id; |
| 215 |
|
| 216 |
$t->post_ok( "//$userid:$password@/api/v1/shibboleth/mappings" => json => $mapping )->status_is(400)->json_is( |
| 217 |
"/errors" => [ |
| 218 |
{ |
| 219 |
message => "Read-only.", |
| 220 |
path => "/body/mapping_id" |
| 221 |
} |
| 222 |
] |
| 223 |
); |
| 224 |
|
| 225 |
my $mapping_without_idp_or_default = { |
| 226 |
koha_field => "phonepro", |
| 227 |
is_matchpoint => 0, |
| 228 |
}; |
| 229 |
|
| 230 |
$t->post_ok( "//$userid:$password@/api/v1/shibboleth/mappings" => json => $mapping_without_idp_or_default ) |
| 231 |
->status_is(400)->json_is( "/error" => "Either idp_field or default_content must be provided" ); |
| 232 |
|
| 233 |
$schema->storage->txn_rollback; |
| 234 |
}; |
| 235 |
|
| 236 |
subtest 'update() tests' => sub { |
| 237 |
|
| 238 |
plan tests => 15; |
| 239 |
|
| 240 |
$schema->storage->txn_begin; |
| 241 |
|
| 242 |
my $librarian = $builder->build_object( |
| 243 |
{ |
| 244 |
class => 'Koha::Patrons', |
| 245 |
value => { flags => 2**3 } |
| 246 |
} |
| 247 |
); |
| 248 |
my $password = 'thePassword123'; |
| 249 |
$librarian->set_password( { password => $password, skip_validation => 1 } ); |
| 250 |
my $userid = $librarian->userid; |
| 251 |
|
| 252 |
my $patron = $builder->build_object( |
| 253 |
{ |
| 254 |
class => 'Koha::Patrons', |
| 255 |
value => { flags => 0 } |
| 256 |
} |
| 257 |
); |
| 258 |
|
| 259 |
$patron->set_password( { password => $password, skip_validation => 1 } ); |
| 260 |
my $unauth_userid = $patron->userid; |
| 261 |
|
| 262 |
my $mapping_id = $builder->build_object( { class => 'Koha::ShibbolethFieldMappings' } )->mapping_id; |
| 263 |
|
| 264 |
$t->put_ok( "//$unauth_userid:$password@/api/v1/shibboleth/mappings/$mapping_id" => json => |
| 265 |
{ idp_field => 'New unauthorized name change' } )->status_is(403); |
| 266 |
|
| 267 |
my $mapping_without_idp_or_default = { |
| 268 |
koha_field => 'surname', |
| 269 |
idp_field => undef, |
| 270 |
default_content => undef, |
| 271 |
is_matchpoint => 0, |
| 272 |
}; |
| 273 |
|
| 274 |
$t->put_ok( |
| 275 |
"//$userid:$password@/api/v1/shibboleth/mappings/$mapping_id" => json => $mapping_without_idp_or_default ) |
| 276 |
->status_is(400)->json_is( "/error" => "Either idp_field or default_content must be provided" ); |
| 277 |
|
| 278 |
my $mapping_with_updated_field = { |
| 279 |
idp_field => "sn", |
| 280 |
koha_field => "surname", |
| 281 |
is_matchpoint => 0, |
| 282 |
}; |
| 283 |
|
| 284 |
$t->put_ok( "//$userid:$password@/api/v1/shibboleth/mappings/$mapping_id" => json => $mapping_with_updated_field ) |
| 285 |
->status_is(200)->json_is( '/idp_field' => 'sn' ); |
| 286 |
|
| 287 |
my $mapping_with_invalid_field = { |
| 288 |
blah => "Mapping Blah", |
| 289 |
idp_field => "sn", |
| 290 |
koha_field => "surname", |
| 291 |
is_matchpoint => 0, |
| 292 |
}; |
| 293 |
|
| 294 |
$t->put_ok( "//$userid:$password@/api/v1/shibboleth/mappings/$mapping_id" => json => $mapping_with_invalid_field ) |
| 295 |
->status_is(400)->json_is( |
| 296 |
"/errors" => [ |
| 297 |
{ |
| 298 |
message => "Properties not allowed: blah.", |
| 299 |
path => "/body" |
| 300 |
} |
| 301 |
] |
| 302 |
); |
| 303 |
|
| 304 |
my $mapping_to_delete = $builder->build_object( { class => 'Koha::ShibbolethFieldMappings' } ); |
| 305 |
my $non_existent_id = $mapping_to_delete->mapping_id; |
| 306 |
$mapping_to_delete->delete; |
| 307 |
|
| 308 |
$t->put_ok( |
| 309 |
"//$userid:$password@/api/v1/shibboleth/mappings/$non_existent_id" => json => $mapping_with_updated_field ) |
| 310 |
->status_is(404); |
| 311 |
|
| 312 |
$mapping_with_updated_field->{mapping_id} = 2; |
| 313 |
|
| 314 |
$t->post_ok( "//$userid:$password@/api/v1/shibboleth/mappings/$mapping_id" => json => $mapping_with_updated_field ) |
| 315 |
->status_is(404); |
| 316 |
|
| 317 |
$schema->storage->txn_rollback; |
| 318 |
}; |
| 319 |
|
| 320 |
subtest 'delete() tests' => sub { |
| 321 |
|
| 322 |
plan tests => 7; |
| 323 |
|
| 324 |
$schema->storage->txn_begin; |
| 325 |
|
| 326 |
my $librarian = $builder->build_object( |
| 327 |
{ |
| 328 |
class => 'Koha::Patrons', |
| 329 |
value => { flags => 2**3 } |
| 330 |
} |
| 331 |
); |
| 332 |
my $password = 'thePassword123'; |
| 333 |
$librarian->set_password( { password => $password, skip_validation => 1 } ); |
| 334 |
my $userid = $librarian->userid; |
| 335 |
|
| 336 |
my $patron = $builder->build_object( |
| 337 |
{ |
| 338 |
class => 'Koha::Patrons', |
| 339 |
value => { flags => 0 } |
| 340 |
} |
| 341 |
); |
| 342 |
|
| 343 |
$patron->set_password( { password => $password, skip_validation => 1 } ); |
| 344 |
my $unauth_userid = $patron->userid; |
| 345 |
|
| 346 |
my $mapping_id = $builder->build_object( { class => 'Koha::ShibbolethFieldMappings' } )->mapping_id; |
| 347 |
|
| 348 |
$t->delete_ok("//$unauth_userid:$password@/api/v1/shibboleth/mappings/$mapping_id")->status_is(403); |
| 349 |
|
| 350 |
$t->delete_ok("//$userid:$password@/api/v1/shibboleth/mappings/$mapping_id")->status_is( 204, 'REST3.2.4' ) |
| 351 |
->content_is( '', 'REST3.3.4' ); |
| 352 |
|
| 353 |
$t->delete_ok("//$userid:$password@/api/v1/shibboleth/mappings/$mapping_id")->status_is(404); |
| 354 |
|
| 355 |
$schema->storage->txn_rollback; |
| 356 |
}; |