From 762fa03432926551b3272ad8900fabb14a1f78de Mon Sep 17 00:00:00 2001 From: Martin Renvoize Date: Fri, 30 Sep 2022 11:44:39 +0100 Subject: [PATCH] Bug 30646: Unit tests for Auth_with_ldap This patch adds a unit test to the Auth_with_ldap.t to confirm welcome notices are send when replace + welcome are enabled in the configuration. We also add a snippet to hide 'Subroutine redefined' warnings in this test as it makes the test output rather challenging to read and the way the test is written these warning are expected. Signed-off-by: Tomas Cohen Arazi --- t/db_dependent/Auth_with_ldap.t | 59 +++++++++++++++++++++++++++++++-- 1 file changed, 57 insertions(+), 2 deletions(-) diff --git a/t/db_dependent/Auth_with_ldap.t b/t/db_dependent/Auth_with_ldap.t index df385e1569..7afba77014 100755 --- a/t/db_dependent/Auth_with_ldap.t +++ b/t/db_dependent/Auth_with_ldap.t @@ -28,6 +28,15 @@ use C4::Context; use Koha::Patrons; +# Hide all the subrouteine redefined warnings when running this test.. +# We reload the ldap module lots in the test and each reload triggers the +# 'Subroutine X redefined at' warning.. disable that to make the test output +# readable. +$SIG{__WARN__} = sub { + my $warning = shift; + warn $warning unless $warning =~ /Subroutine .* redefined at/; +}; + # Start transaction my $schema = Koha::Database->new->schema; $schema->storage->txn_begin(); @@ -37,6 +46,7 @@ my $builder = t::lib::TestBuilder->new(); # Variables controlling LDAP server config my $update = 0; my $replicate = 0; +my $welcome = 0; my $auth_by_bind = 1; my $anonymous_bind = 1; my $user = 'cn=Manager,dc=metavore,dc=com'; @@ -144,7 +154,7 @@ subtest 'checkpw_ldap tests' => sub { subtest 'auth_by_bind = 1 tests' => sub { - plan tests => 11; + plan tests => 14; $auth_by_bind = 1; @@ -221,7 +231,51 @@ subtest 'checkpw_ldap tests' => sub { 'checkpw_ldap returns 0 if user lookup returns 0' ); - $desired_bind_result = 'error'; + # test replicate functionality and welcome notice + $desired_authentication_result = 'success'; + $anonymous_bind = 1; + $desired_admin_bind_result = 'success'; + $desired_search_result = 'success'; + $desired_count_result = 1; + $desired_bind_result = 'success'; + $replicate = 1; + $welcome = 1; + reload_ldap_module(); + + $auth->mock( + 'ldap_entry_2_hash', + sub { + return ( + userid => 'hola', + branchcode => $branchcode, + categorycode => $categorycode, + email => 'me@myemail.com', + ); + } + ); + + C4::Auth_with_ldap::checkpw_ldap( 'hola', password => 'hey' ); + my $patrons = Koha::Patrons->search( { userid => 'hola' } ); + is( $patrons->count, 1, 'New patron added with "replicate"' ); + + $patron = $patrons->next; + my $queued_notices = Koha::Notice::Messages->search( + { borrowernumber => $patron->borrowernumber } ); + is( $queued_notices->count, 1, + "One notice queued when `welcome` is set" ); + + my $THE_notice = $queued_notices->next; + is( $THE_notice->status, 'failed', "The notice was sent immediately" ); + + # clean up + $patron->delete; + $replicate = 0; + $welcome = 0; + $auth->unmock('ldap_entry_2_hash'); + # end replicate testing + + $desired_count_result = 0; + $desired_bind_result = 'error'; reload_ldap_module(); warning_like { @@ -376,6 +430,7 @@ sub mockedC4Config { pass => $pass, principal_name => '%s@my_domain.com', replicate => $replicate, + welcome => $welcome, update => $update, user => $user, ); -- 2.39.2