View | Details | Raw Unified | Return to bug 33538
Collapse All | Expand All

(-)a/t/db_dependent/Koha/Auth/Client.t (-1 / +145 lines)
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
};
(-)a/t/db_dependent/api/v1/identity_provider_mappings.t (-4 / +7 lines)
Lines 161-167 subtest 'get() tests' => sub { Link Here
161
161
162
subtest 'add() tests' => sub {
162
subtest 'add() tests' => sub {
163
163
164
    plan tests => 12;
164
    plan tests => 14;
165
165
166
    $schema->storage->txn_begin;
166
    $schema->storage->txn_begin;
167
167
Lines 175-182 subtest 'add() tests' => sub { Link Here
175
    my $pid      = $provider->identity_provider_id;
175
    my $pid      = $provider->identity_provider_id;
176
176
177
    my $new_mapping = {
177
    my $new_mapping = {
178
        koha_field     => 'email',
178
        koha_field       => 'email',
179
        provider_field => 'mail',
179
        provider_field   => 'mail',
180
        sync_on_creation => 0,
181
        sync_on_update   => 0,
180
    };
182
    };
181
183
182
    # Unauthorized
184
    # Unauthorized
Lines 196-201 subtest 'add() tests' => sub { Link Here
196
        ->status_is(201)
198
        ->status_is(201)
197
        ->json_is( '/koha_field',           'email' )
199
        ->json_is( '/koha_field',           'email' )
198
        ->json_is( '/identity_provider_id', $pid )
200
        ->json_is( '/identity_provider_id', $pid )
201
        ->json_is( '/sync_on_creation',     0 )
202
        ->json_is( '/sync_on_update',       0 )
199
        ->json_has('/mapping_id')
203
        ->json_has('/mapping_id')
200
        ->header_like( 'Location', qr|/api/v1/auth/identity_providers/$pid/mappings/\d+| );
204
        ->header_like( 'Location', qr|/api/v1/auth/identity_providers/$pid/mappings/\d+| );
201
205
202
- 

Return to bug 33538