|
Lines 19-25
Link Here
|
| 19 |
|
19 |
|
| 20 |
use Modern::Perl; |
20 |
use Modern::Perl; |
| 21 |
|
21 |
|
| 22 |
use Test::More tests => 5; |
22 |
use Test::More tests => 6; |
| 23 |
|
23 |
|
| 24 |
use Test::MockModule; |
24 |
use Test::MockModule; |
| 25 |
use Test::MockObject; |
25 |
use Test::MockObject; |
|
Lines 284-286
subtest '_traverse_hash() tests' => sub {
Link Here
|
| 284 |
); |
284 |
); |
| 285 |
is( $third_result, 'second element', 'get the value of the second element of an array within a hash structure' ); |
285 |
is( $third_result, 'second element', 'get the value of the second element of an array within a hash structure' ); |
| 286 |
}; |
286 |
}; |
|
|
287 |
|
| 288 |
subtest 'Sync flags tests' => sub { |
| 289 |
plan tests => 6; |
| 290 |
|
| 291 |
$schema->storage->txn_begin; |
| 292 |
|
| 293 |
my $client = Koha::Auth::Client::OAuth->new; |
| 294 |
my $provider = $builder->build_object( { class => 'Koha::Auth::Identity::Providers' } ); |
| 295 |
my $hostname_obj = |
| 296 |
$builder->build_object( { class => 'Koha::Auth::Hostnames', value => { hostname => 'test.library.com' } } ); |
| 297 |
$builder->build_object( |
| 298 |
{ |
| 299 |
class => 'Koha::Auth::Identity::Provider::Hostnames', |
| 300 |
value => { |
| 301 |
identity_provider_id => $provider->id, |
| 302 |
hostname_id => $hostname_obj->id, |
| 303 |
matchpoint => 'email', |
| 304 |
} |
| 305 |
} |
| 306 |
); |
| 307 |
my $domain = $builder->build_object( |
| 308 |
{ |
| 309 |
class => 'Koha::Auth::Identity::Provider::Domains', |
| 310 |
value => { |
| 311 |
identity_provider_id => $provider->id, domain => '*test.com', update_on_auth => 1, allow_opac => 1, |
| 312 |
allow_staff => 0 |
| 313 |
} |
| 314 |
} |
| 315 |
); |
| 316 |
|
| 317 |
# Mapping for matchpoint |
| 318 |
$builder->build_object( |
| 319 |
{ |
| 320 |
class => 'Koha::Auth::Identity::Provider::Mappings', |
| 321 |
value => { |
| 322 |
identity_provider_id => $provider->id, |
| 323 |
koha_field => 'email', |
| 324 |
provider_field => 'email', |
| 325 |
sync_on_update => 1, |
| 326 |
sync_on_creation => 1, |
| 327 |
} |
| 328 |
} |
| 329 |
); |
| 330 |
|
| 331 |
# Mapping with sync_on_update = 0 |
| 332 |
$builder->build_object( |
| 333 |
{ |
| 334 |
class => 'Koha::Auth::Identity::Provider::Mappings', |
| 335 |
value => { |
| 336 |
identity_provider_id => $provider->id, |
| 337 |
koha_field => 'firstname', |
| 338 |
provider_field => 'given_name', |
| 339 |
sync_on_update => 0, |
| 340 |
sync_on_creation => 1, |
| 341 |
} |
| 342 |
} |
| 343 |
); |
| 344 |
|
| 345 |
# Mapping with sync_on_update = 1 |
| 346 |
$builder->build_object( |
| 347 |
{ |
| 348 |
class => 'Koha::Auth::Identity::Provider::Mappings', |
| 349 |
value => { |
| 350 |
identity_provider_id => $provider->id, |
| 351 |
koha_field => 'surname', |
| 352 |
provider_field => 'family_name', |
| 353 |
sync_on_update => 1, |
| 354 |
sync_on_creation => 1, |
| 355 |
} |
| 356 |
} |
| 357 |
); |
| 358 |
|
| 359 |
# Mapping with default_content and sync_on_update = 1 (should NOT sync default on update) |
| 360 |
$builder->build_object( |
| 361 |
{ |
| 362 |
class => 'Koha::Auth::Identity::Provider::Mappings', |
| 363 |
value => { |
| 364 |
identity_provider_id => $provider->id, |
| 365 |
koha_field => 'othernames', |
| 366 |
provider_field => 'missing_field', |
| 367 |
default_content => 'default value', |
| 368 |
sync_on_update => 1, |
| 369 |
sync_on_creation => 1, |
| 370 |
} |
| 371 |
} |
| 372 |
); |
| 373 |
|
| 374 |
my $patron = $builder->build_object( |
| 375 |
{ |
| 376 |
class => 'Koha::Patrons', |
| 377 |
value => { |
| 378 |
email => 'patron@test.com', |
| 379 |
firstname => 'Original Firstname', |
| 380 |
surname => 'Original Surname', |
| 381 |
othernames => 'Original Othernames', |
| 382 |
} |
| 383 |
} |
| 384 |
); |
| 385 |
|
| 386 |
my $id_token = 'header.' . encode_base64url( |
| 387 |
encode_json( |
| 388 |
{ |
| 389 |
email => 'patron@test.com', |
| 390 |
given_name => 'New Firstname', |
| 391 |
family_name => 'New Surname', |
| 392 |
} |
| 393 |
) |
| 394 |
) . '.footer'; |
| 395 |
|
| 396 |
my $data = { id_token => $id_token }; |
| 397 |
|
| 398 |
$client->get_user( |
| 399 |
{ provider => $provider->code, data => $data, interface => 'opac', hostname => 'test.library.com' } ); |
| 400 |
|
| 401 |
$patron->discard_changes; |
| 402 |
is( $patron->firstname, 'Original Firstname', 'firstname NOT synced (sync_on_update=0)' ); |
| 403 |
is( $patron->surname, 'New Surname', 'surname synced (sync_on_update=1)' ); |
| 404 |
is( $patron->othernames, 'Original Othernames', 'othernames NOT synced with default value on update' ); |
| 405 |
|
| 406 |
# Now test creation (using auth.register helper logic) |
| 407 |
# We can't easily call auth.register here because it's a Mojo helper, |
| 408 |
# but we can test the logic directly or via the REST API if we wanted. |
| 409 |
# For now, let's just test that the flags are respected in a similar loop. |
| 410 |
|
| 411 |
my $mappings = $provider->mappings->as_auth_mapping; |
| 412 |
my $mapped_data = { |
| 413 |
firstname => 'New Firstname', |
| 414 |
surname => 'New Surname', |
| 415 |
}; |
| 416 |
|
| 417 |
my %borrower_data; |
| 418 |
for my $key ( keys %$mappings ) { |
| 419 |
next unless $mappings->{$key}->{sync_on_creation}; |
| 420 |
my $value = $mapped_data->{$key}; |
| 421 |
$value //= $mappings->{$key}->{content}; |
| 422 |
$borrower_data{$key} = $value if defined $value; |
| 423 |
} |
| 424 |
|
| 425 |
is( $borrower_data{firstname}, 'New Firstname', 'firstname included on creation' ); |
| 426 |
is( $borrower_data{surname}, 'New Surname', 'surname included on creation' ); |
| 427 |
is( $borrower_data{othernames}, 'default value', 'othernames included with default on creation' ); |
| 428 |
|
| 429 |
$schema->storage->txn_rollback; |
| 430 |
}; |