Bug 21190: Add tests for authentication success and failure
authorMarcel de Rooy <m.de.rooy@rijksmuseum.nl>
Mon, 28 Oct 2019 10:06:11 +0000 (10:06 +0000)
committerMartin Renvoize <martin.renvoize@ptfs-europe.com>
Tue, 14 Apr 2020 15:13:39 +0000 (16:13 +0100)
Test plan:
Run t/db_dependent/Log.t

Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>
Signed-off-by: Jon Knight <J.P.Knight@lboro.ac.uk>
Signed-off-by: Michal Denar <black23@gmail.com>
Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>

t/db_dependent/Log.t

index ca8fa9e..8376b61 100644 (file)
@@ -19,6 +19,7 @@ use Test::More tests => 5;
 
 use C4::Context;
 use C4::Log;
+use C4::Auth qw/checkpw/;
 use Koha::Database;
 use Koha::DateUtils;
 
@@ -162,7 +163,7 @@ subtest 'GetLogs() respects interface filters' => sub {
 };
 
 subtest 'GDPR logging' => sub {
-    plan tests => 1;
+    plan tests => 6;
 
     my $builder = t::lib::TestBuilder->new;
     my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
@@ -170,8 +171,26 @@ subtest 'GDPR logging' => sub {
     t::lib::Mocks::mock_userenv({ patron => $patron });
     logaction( 'AUTH', 'FAILURE', $patron->id, '', 'opac' );
     my $logs = GetLogs( undef, undef, $patron->id, ['AUTH'], ['FAILURE'], $patron->id );
-    is( @$logs, 1, 'We should find one auth failure for this patron' );
-
+    is( @$logs, 1, 'We should find one auth failure' );
+
+    t::lib::Mocks::mock_preference('AuthFailureLog', 1);
+    my $strong_password = 'N0tStr0ngAnyM0reN0w:)';
+    $patron->set_password({ password => $strong_password });
+    my @ret = checkpw( $dbh, $patron->userid, 'WrongPassword', undef, undef, 1);
+    is( $ret[0], 0, 'Authentication failed' );
+    # Look for auth failure but NOT on patron id, pass userid in info parameter
+    $logs = GetLogs( undef, undef, 0, ['AUTH'], ['FAILURE'], undef, $patron->userid );
+    is( @$logs, 1, 'We should find one auth failure with this userid' );
+    t::lib::Mocks::mock_preference('AuthFailureLog', 0);
+    @ret = checkpw( $dbh, $patron->userid, 'WrongPassword', undef, undef, 1);
+    $logs = GetLogs( undef, undef, 0, ['AUTH'], ['FAILURE'], undef, $patron->userid );
+    is( @$logs, 1, 'Still only one failure with this userid' );
+    t::lib::Mocks::mock_preference('AuthSuccessLog', 1);
+    @ret = checkpw( $dbh, $patron->userid, $strong_password, undef, undef, 1);
+    is( $ret[0], 1, 'Authentication succeeded' );
+    # Now we can look for patron id
+    $logs = GetLogs( undef, undef, $patron->id, ['AUTH'], ['SUCCESS'], $patron->id );
+    is( @$logs, 1, 'We expect only one auth success line for this patron' );
 };
 
 $schema->storage->txn_rollback;