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 (+96 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 => 1;
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
94
    Koha::Plugins->RemovePlugins;
95
    $schema->storage->txn_rollback;
96
};
(-)a/t/lib/plugins/Koha/Plugin/Test.pm (-1 / +13 lines)
Lines 419-424 sub transform_prepared_letter { Link Here
419
    Koha::Exception->throw("transform_prepared_letter called with letter content $params->{letter}->{content}");
419
    Koha::Exception->throw("transform_prepared_letter called with letter content $params->{letter}->{content}");
420
}
420
}
421
421
422
sub auth_client_get_user {
423
    my ( $self, $params ) = @_;
424
425
    my $mapped_data = $params->{'mapped_data'};
426
427
    if ( defined $mapped_data->{'cardnumber'} ) {
428
429
        # Split data (e. g. kit.edu:123456789) and set the card number.
430
        my ( $card_domain, $cardnumber ) = split( /\:/, $mapped_data->{'cardnumber'} );
431
        $mapped_data->{'cardnumber'} = $cardnumber;
432
    }
433
}
434
422
sub _private_sub {
435
sub _private_sub {
423
    return "";
436
    return "";
424
}
437
}
425
- 

Return to bug 36503