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

(-)a/t/db_dependent/Koha/Auth/Client.t (-33 lines)
Lines 78-117 subtest 'get_user() tests' => sub { Link Here
78
    is( $mapped_data->{surname},              undef,                                         'No surname mapped' );
78
    is( $mapped_data->{surname},              undef,                                         'No surname mapped' );
79
    is( $domain->identity_provider_domain_id, $resolved_domain->identity_provider_domain_id, 'Is the same domain' );
79
    is( $domain->identity_provider_domain_id, $resolved_domain->identity_provider_domain_id, 'Is the same domain' );
80
80
81
    # Test the plugin hook "auth_client_get_user".
82
    t::lib::Mocks::mock_config( 'enable_plugins', 1 );
83
    my $get_plugins = sub {
84
        my $methods = [
85
            {
86
                auth_client_get_user => sub {
87
                    $_[1]->{mapped_data}->{firstname} = 'test name modified 1';
88
                    return;
89
                }
90
            },
91
        ];
92
        my @plugins;
93
        foreach my $method ( @{$methods} ) {
94
            my $plugin = Test::MockObject->new;
95
            foreach my $name ( keys %{$method} ) {
96
                $plugin->mock( $name, $method->{$name} );
97
            }
98
            push @plugins, $plugin;
99
        }
100
        return @plugins;
101
    };
102
    my $plugins_module = Test::MockModule->new('Koha::Plugins');
103
    $plugins_module->mock( 'GetPlugins',          $get_plugins );
104
    $plugins_module->mock( 'get_enabled_plugins', $get_plugins );
105
    ( $resolved_patron, $mapped_data, $resolved_domain ) =
106
        $client->get_user( { provider => $provider->code, data => $data, interface => 'opac' } );
107
    is(
108
        $mapped_data->{firstname},
109
        'test name modified 1',
110
        'Data modified correctly by plugins'
111
    );
112
113
    $schema->storage->txn_rollback;
81
    $schema->storage->txn_rollback;
114
115
};
82
};
116
83
117
subtest 'get_valid_domain_config() tests' => sub {
84
subtest 'get_valid_domain_config() tests' => sub {
(-)a/t/db_dependent/Koha/Plugins/Auth/Client.t (+98 lines)
Line 0 Link Here
1
#!/usr/bin/perl
2
3
# This file is part of Koha.
4
#
5
# Koha is free software; you can redistribute it and/or modify it under the
6
# terms of the GNU General Public License as published by the Free Software
7
# Foundation; either version 3 of the License, or (at your option) any later
8
# version.
9
#
10
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
11
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12
# A PARTICULAR PURPOSE. See the GNU General Public License for more details.
13
#
14
# You should have received a copy of the GNU General Public License along
15
# with Koha; if not, see <http://www.gnu.org/licenses>.
16
17
use Modern::Perl;
18
19
use Test::More tests => 6;
20
use Test::Exception;
21
22
use File::Basename;
23
use JSON qw(encode_json);
24
use MIME::Base64 qw{ encode_base64url };
25
26
use t::lib::Mocks;
27
use t::lib::TestBuilder;
28
29
BEGIN {
30
    # Mock pluginsdir before loading Plugins module
31
    my $path = dirname(__FILE__) . '/../../../../lib/plugins';
32
    t::lib::Mocks::mock_config( 'pluginsdir', $path );
33
34
    use_ok('Koha::Plugins');
35
    use_ok('Koha::Plugins::Handler');
36
    use_ok('Koha::Plugin::Test');
37
    use_ok('Koha::Auth::Client');
38
    use_ok('Koha::Auth::Client::OAuth');
39
}
40
41
my $schema  = Koha::Database->new->schema;
42
my $builder = t::lib::TestBuilder->new;
43
44
t::lib::Mocks::mock_config( 'enable_plugins', 1 );
45
46
subtest 'auth_client_get_user hook tests' => sub {
47
48
    plan tests => 3;
49
50
    $schema->storage->txn_begin;
51
52
    my $plugins = Koha::Plugins->new;
53
    $plugins->InstallPlugins;
54
55
    # Test Plugin manipulates mapped_data
56
    my $plugin = Koha::Plugin::Test->new->enable;
57
58
    my $client = Koha::Auth::Client::OAuth->new;
59
    my $provider =
60
        $builder->build_object( { class => 'Koha::Auth::Identity::Providers', value => { matchpoint => 'email' } } );
61
    my $domain = $builder->build_object(
62
        {
63
            class => 'Koha::Auth::Identity::Provider::Domains',
64
            value => {
65
                identity_provider_id => $provider->id, domain => '', update_on_auth => 0, allow_opac => 1,
66
                allow_staff          => 0
67
            }
68
        }
69
    );
70
    my $patron  = $builder->build_object( { class => 'Koha::Patrons', value => { email => 'patron@test.com' } } );
71
    my $mapping = {
72
        email     => 'electronic_mail',
73
        firstname => 'given_name',
74
        surname   => 'family_name',
75
        cardnumber => 'cardnumber',
76
    };
77
    $provider->set_mapping($mapping)->store;
78
79
    my $id_token = 'header.' . encode_base64url(
80
        encode_json(
81
            {
82
                electronic_mail => 'patron@test.com',
83
                given_name      => 'test name',
84
                cardnumber      => 'kit:12345',
85
            }
86
        )
87
    ) . '.footer';
88
89
    my $data = { id_token => $id_token };
90
91
    my ( $resolved_patron, $mapped_data, $resolved_domain ) = $client->get_user( { provider => $provider->code, data => $data, interface => 'opac' } );
92
    is( $mapped_data->{cardnumber}, '12345', 'Plugin manipulated mapped_data successfully' );
93
    isnt( $resolved_patron->borrowernumber, $patron->borrowernumber, 'Plugin changed the resolved patron' );
94
    isnt( $resolved_domain->domain, $domain->domain, 'Plugin changed the resolved domain');
95
96
    Koha::Plugins->RemovePlugins;
97
    $schema->storage->txn_rollback;
98
};
(-)a/t/lib/plugins/Koha/Plugin/Test.pm (-1 / +30 lines)
Lines 9-14 use Koha::Plugins::Tab; Link Here
9
use MARC::Field;
9
use MARC::Field;
10
use Mojo::JSON qw( decode_json );
10
use Mojo::JSON qw( decode_json );
11
11
12
use t::lib::TestBuilder;
13
12
## Required for all plugins
14
## Required for all plugins
13
use base qw(Koha::Plugins::Base);
15
use base qw(Koha::Plugins::Base);
14
16
Lines 419-424 sub transform_prepared_letter { Link Here
419
    Koha::Exception->throw("transform_prepared_letter called with letter content $params->{letter}->{content}");
421
    Koha::Exception->throw("transform_prepared_letter called with letter content $params->{letter}->{content}");
420
}
422
}
421
423
424
sub auth_client_get_user {
425
    my ( $self, $params ) = @_;
426
427
    my $builder = t::lib::TestBuilder->new;
428
429
    my $new_patron = $builder->build_object( { class => 'Koha::Patrons' } );
430
    $params->{patron} = $new_patron;
431
432
    my $new_domain = $builder->build_object(
433
        {
434
            class => 'Koha::Auth::Identity::Provider::Domains',
435
            value => {
436
                domain      => 'changed', update_on_auth => 0, allow_opac => 1,
437
                allow_staff => 0
438
            }
439
        }
440
    );
441
    $params->{domain} = $new_domain;
442
443
    if ( defined $params->{mapped_data}->{'cardnumber'} ) {
444
445
        # Split data (e. g. kit.edu:123456789) and set the card number.
446
        my ( $card_domain, $cardnumber ) = split( /\:/, $params->{mapped_data}->{'cardnumber'} );
447
        $params->{mapped_data}->{'cardnumber'} = $cardnumber;
448
    }
449
    return;
450
}
451
422
sub _private_sub {
452
sub _private_sub {
423
    return "";
453
    return "";
424
}
454
}
425
- 

Return to bug 36503