Bug 36503: Move and update unit test

Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
Signed-off-by: Katrin Fischer <katrin.fischer@bsz-bw.de>
This commit is contained in:
Martin Renvoize 2024-05-03 12:34:33 +01:00 committed by Katrin Fischer
parent 4417811d9a
commit 3c07553b36
No known key found for this signature in database
GPG Key ID: 0EF6E2C03357A834
3 changed files with 129 additions and 33 deletions

View File

@ -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 {

View File

@ -0,0 +1,99 @@
#!/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 <http://www.gnu.org/licenses>.
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;
};

View File

@ -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 "";
}