Bug 36503: Move and update unit test
[koha.git] / t / db_dependent / Koha / Plugins / Auth / Client.t
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 ) =
92         $client->get_user( { provider => $provider->code, data => $data, interface => 'opac' } );
93     is( $mapped_data->{cardnumber}, '12345', 'Plugin manipulated mapped_data successfully' );
94     isnt( $resolved_patron->borrowernumber, $patron->borrowernumber, 'Plugin changed the resolved patron' );
95     isnt( $resolved_domain->domain,         $domain->domain,         'Plugin changed the resolved domain' );
96
97     Koha::Plugins->RemovePlugins;
98     $schema->storage->txn_rollback;
99 };