From 44cb4dcc60ac2bf33e8f454f45d74ec96664cdf2 Mon Sep 17 00:00:00 2001 From: Martin Renvoize Date: Fri, 3 May 2024 12:34:33 +0100 Subject: [PATCH] Bug 36503: Move and update unit test Signed-off-by: Martin Renvoize --- t/db_dependent/Koha/Auth/Client.t | 33 -------- t/db_dependent/Koha/Plugins/Auth/Client.t | 98 +++++++++++++++++++++++ t/lib/plugins/Koha/Plugin/Test.pm | 30 +++++++ 3 files changed, 128 insertions(+), 33 deletions(-) create mode 100644 t/db_dependent/Koha/Plugins/Auth/Client.t diff --git a/t/db_dependent/Koha/Auth/Client.t b/t/db_dependent/Koha/Auth/Client.t index bf545360f76..861371484d7 100755 --- a/t/db_dependent/Koha/Auth/Client.t +++ b/t/db_dependent/Koha/Auth/Client.t @@ -78,40 +78,7 @@ subtest 'get_user() tests' => sub { is( $mapped_data->{surname}, undef, 'No surname mapped' ); is( $domain->identity_provider_domain_id, $resolved_domain->identity_provider_domain_id, 'Is the same domain' ); - # Test the plugin hook "auth_client_get_user". - t::lib::Mocks::mock_config( 'enable_plugins', 1 ); - my $get_plugins = sub { - my $methods = [ - { - auth_client_get_user => sub { - $_[1]->{mapped_data}->{firstname} = 'test name modified 1'; - return; - } - }, - ]; - my @plugins; - foreach my $method ( @{$methods} ) { - my $plugin = Test::MockObject->new; - foreach my $name ( keys %{$method} ) { - $plugin->mock( $name, $method->{$name} ); - } - push @plugins, $plugin; - } - return @plugins; - }; - my $plugins_module = Test::MockModule->new('Koha::Plugins'); - $plugins_module->mock( 'GetPlugins', $get_plugins ); - $plugins_module->mock( 'get_enabled_plugins', $get_plugins ); - ( $resolved_patron, $mapped_data, $resolved_domain ) = - $client->get_user( { provider => $provider->code, data => $data, interface => 'opac' } ); - is( - $mapped_data->{firstname}, - 'test name modified 1', - 'Data modified correctly by plugins' - ); - $schema->storage->txn_rollback; - }; subtest 'get_valid_domain_config() tests' => sub { diff --git a/t/db_dependent/Koha/Plugins/Auth/Client.t b/t/db_dependent/Koha/Plugins/Auth/Client.t new file mode 100644 index 00000000000..c702169e638 --- /dev/null +++ b/t/db_dependent/Koha/Plugins/Auth/Client.t @@ -0,0 +1,98 @@ +#!/usr/bin/perl + +# This file is part of Koha. +# +# Koha is free software; you can redistribute it and/or modify it under the +# terms of the GNU General Public License as published by the Free Software +# Foundation; either version 3 of the License, or (at your option) any later +# version. +# +# Koha is distributed in the hope that it will be useful, but WITHOUT ANY +# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR +# A PARTICULAR PURPOSE. See the GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with Koha; if not, see . + +use Modern::Perl; + +use Test::More tests => 6; +use Test::Exception; + +use File::Basename; +use JSON qw(encode_json); +use MIME::Base64 qw{ encode_base64url }; + +use t::lib::Mocks; +use t::lib::TestBuilder; + +BEGIN { + # Mock pluginsdir before loading Plugins module + my $path = dirname(__FILE__) . '/../../../../lib/plugins'; + t::lib::Mocks::mock_config( 'pluginsdir', $path ); + + use_ok('Koha::Plugins'); + use_ok('Koha::Plugins::Handler'); + use_ok('Koha::Plugin::Test'); + use_ok('Koha::Auth::Client'); + use_ok('Koha::Auth::Client::OAuth'); +} + +my $schema = Koha::Database->new->schema; +my $builder = t::lib::TestBuilder->new; + +t::lib::Mocks::mock_config( 'enable_plugins', 1 ); + +subtest 'auth_client_get_user hook tests' => sub { + + plan tests => 3; + + $schema->storage->txn_begin; + + my $plugins = Koha::Plugins->new; + $plugins->InstallPlugins; + + # Test Plugin manipulates mapped_data + my $plugin = Koha::Plugin::Test->new->enable; + + my $client = Koha::Auth::Client::OAuth->new; + my $provider = + $builder->build_object( { class => 'Koha::Auth::Identity::Providers', value => { matchpoint => 'email' } } ); + my $domain = $builder->build_object( + { + class => 'Koha::Auth::Identity::Provider::Domains', + value => { + identity_provider_id => $provider->id, domain => '', update_on_auth => 0, allow_opac => 1, + allow_staff => 0 + } + } + ); + my $patron = $builder->build_object( { class => 'Koha::Patrons', value => { email => 'patron@test.com' } } ); + my $mapping = { + email => 'electronic_mail', + firstname => 'given_name', + surname => 'family_name', + cardnumber => 'cardnumber', + }; + $provider->set_mapping($mapping)->store; + + my $id_token = 'header.' . encode_base64url( + encode_json( + { + electronic_mail => 'patron@test.com', + given_name => 'test name', + cardnumber => 'kit:12345', + } + ) + ) . '.footer'; + + my $data = { id_token => $id_token }; + + my ( $resolved_patron, $mapped_data, $resolved_domain ) = $client->get_user( { provider => $provider->code, data => $data, interface => 'opac' } ); + is( $mapped_data->{cardnumber}, '12345', 'Plugin manipulated mapped_data successfully' ); + isnt( $resolved_patron->borrowernumber, $patron->borrowernumber, 'Plugin changed the resolved patron' ); + isnt( $resolved_domain->domain, $domain->domain, 'Plugin changed the resolved domain'); + + Koha::Plugins->RemovePlugins; + $schema->storage->txn_rollback; +}; diff --git a/t/lib/plugins/Koha/Plugin/Test.pm b/t/lib/plugins/Koha/Plugin/Test.pm index 8bb0554af88..255fc90a122 100644 --- a/t/lib/plugins/Koha/Plugin/Test.pm +++ b/t/lib/plugins/Koha/Plugin/Test.pm @@ -9,6 +9,8 @@ use Koha::Plugins::Tab; use MARC::Field; use Mojo::JSON qw( decode_json ); +use t::lib::TestBuilder; + ## Required for all plugins use base qw(Koha::Plugins::Base); @@ -419,6 +421,34 @@ sub transform_prepared_letter { Koha::Exception->throw("transform_prepared_letter called with letter content $params->{letter}->{content}"); } +sub auth_client_get_user { + my ( $self, $params ) = @_; + + my $builder = t::lib::TestBuilder->new; + + my $new_patron = $builder->build_object( { class => 'Koha::Patrons' } ); + $params->{patron} = $new_patron; + + my $new_domain = $builder->build_object( + { + class => 'Koha::Auth::Identity::Provider::Domains', + value => { + domain => 'changed', update_on_auth => 0, allow_opac => 1, + allow_staff => 0 + } + } + ); + $params->{domain} = $new_domain; + + if ( defined $params->{mapped_data}->{'cardnumber'} ) { + + # Split data (e. g. kit.edu:123456789) and set the card number. + my ( $card_domain, $cardnumber ) = split( /\:/, $params->{mapped_data}->{'cardnumber'} ); + $params->{mapped_data}->{'cardnumber'} = $cardnumber; + } + return; +} + sub _private_sub { return ""; } -- 2.45.0