Bug 21336: Do not increase login_attempts after locking
[koha-equinox.git] / t / db_dependent / Auth.t
index c08060e..490e4a7 100644 (file)
@@ -32,12 +32,13 @@ my $dbh     = C4::Context->dbh;
 # FIXME: SessionStorage defaults to mysql, but it seems to break transaction
 # handling
 t::lib::Mocks::mock_preference( 'SessionStorage', 'tmp' );
+t::lib::Mocks::mock_preference( 'GDPR_Policy', '' ); # Disabled
 
 $schema->storage->txn_begin;
 
 subtest 'checkauth() tests' => sub {
 
-    plan tests => 2;
+    plan tests => 3;
 
     my $patron = $builder->build({ source => 'Borrower', value => { flags => undef } })->{userid};
 
@@ -63,32 +64,79 @@ subtest 'checkauth() tests' => sub {
         });
     ( $userid, $cookie, $sessionID, $flags ) = C4::Auth::checkauth( $cgi, $authnotrequired );
     is ( $userid, undef, 'If DB user is used, it should not be logged in' );
+
+    my $is_allowed = C4::Auth::haspermission( $db_user_id, { can_do => 'everything' } );
+
+    # FIXME This belongs to t/db_dependent/Auth/haspermission.t but we do not want to c/p the pervious mock statements
+    ok( !$is_allowed, 'DB user should not have any permissions');
+
     C4::Context->_new_userenv; # For next tests
 
 };
 
-my $hash1 = hash_password('password');
-my $hash2 = hash_password('password');
+subtest 'track_login_daily tests' => sub {
 
-{ # tests no_set_userenv parameter
-    my $patron = $builder->build( { source => 'Borrower' } );
-    Koha::Patrons->find( $patron->{borrowernumber} )->update_password( $patron->{userid}, $hash1 );
-    my $library = $builder->build(
-        {
-            source => 'Branch',
-        }
-    );
+    plan tests => 5;
+
+    my $patron = $builder->build_object({ class => 'Koha::Patrons' });
+    my $userid = $patron->userid;
+
+    $patron->lastseen( undef );
+    $patron->store();
+
+    my $cache     = Koha::Caches->get_instance();
+    my $cache_key = "track_login_" . $patron->userid;
+    $cache->clear_from_cache($cache_key);
+
+    t::lib::Mocks::mock_preference( 'TrackLastPatronActivity', '1' );
 
-    ok( checkpw( $dbh, $patron->{userid}, 'password', undef, undef, 1 ), 'checkpw returns true' );
+    is( $patron->lastseen, undef, 'Patron should have not last seen when newly created' );
+
+    C4::Auth::track_login_daily( $userid );
+    $patron->_result()->discard_changes();
+    isnt( $patron->lastseen, undef, 'Patron should have last seen set when TrackLastPatronActivity = 1' );
+
+    sleep(1); # We need to wait a tiny bit to make sure the timestamp will be different
+    my $last_seen = $patron->lastseen;
+    C4::Auth::track_login_daily( $userid );
+    $patron->_result()->discard_changes();
+    is( $patron->lastseen, $last_seen, 'Patron last seen should still be unchanged' );
+
+    $cache->clear_from_cache($cache_key);
+    C4::Auth::track_login_daily( $userid );
+    $patron->_result()->discard_changes();
+    isnt( $patron->lastseen, $last_seen, 'Patron last seen should be changed if we cleared the cache' );
+
+    t::lib::Mocks::mock_preference( 'TrackLastPatronActivity', '0' );
+    $patron->lastseen( undef )->store;
+    $cache->clear_from_cache($cache_key);
+    C4::Auth::track_login_daily( $userid );
+    $patron->_result()->discard_changes();
+    is( $patron->lastseen, undef, 'Patron should still have last seen unchanged when TrackLastPatronActivity = 0' );
+
+};
+
+subtest 'no_set_userenv parameter tests' => sub {
+
+    plan tests => 7;
+
+    my $library = $builder->build_object( { class => 'Koha::Libraries' } );
+    my $patron  = $builder->build_object( { class => 'Koha::Patrons' } );
+    my $password = 'password';
+
+    t::lib::Mocks::mock_preference( 'RequireStrongPassword', 0 );
+    $patron->set_password({ password => $password });
+
+    ok( checkpw( $dbh, $patron->userid, $password, undef, undef, 1 ), 'checkpw returns true' );
     is( C4::Context->userenv, undef, 'Userenv should be undef as required' );
     C4::Context->_new_userenv('DUMMY SESSION');
-    C4::Context->set_userenv(0,0,0,'firstname','surname', $library->{branchcode}, 'Library 1', 0, '', '');
-    is( C4::Context->userenv->{branch}, $library->{branchcode}, 'Userenv gives correct branch' );
-    ok( checkpw( $dbh, $patron->{userid}, 'password', undef, undef, 1 ), 'checkpw returns true' );
-    is( C4::Context->userenv->{branch}, $library->{branchcode}, 'Userenv branch is preserved if no_set_userenv is true' );
-    ok( checkpw( $dbh, $patron->{userid}, 'password', undef, undef, 0 ), 'checkpw still returns true' );
-    isnt( C4::Context->userenv->{branch}, $library->{branchcode}, 'Userenv branch is overwritten if no_set_userenv is false' );
-}
+    C4::Context->set_userenv(0,0,0,'firstname','surname', $library->branchcode, 'Library 1', 0, '', '');
+    is( C4::Context->userenv->{branch}, $library->branchcode, 'Userenv gives correct branch' );
+    ok( checkpw( $dbh, $patron->userid, $password, undef, undef, 1 ), 'checkpw returns true' );
+    is( C4::Context->userenv->{branch}, $library->branchcode, 'Userenv branch is preserved if no_set_userenv is true' );
+    ok( checkpw( $dbh, $patron->userid, $password, undef, undef, 0 ), 'checkpw still returns true' );
+    isnt( C4::Context->userenv->{branch}, $library->branchcode, 'Userenv branch is overwritten if no_set_userenv is false' );
+};
 
 # get_template_and_user tests
 
@@ -105,7 +153,7 @@ my $hash2 = hash_password('password');
             borrowers         => 0,
             catalogue         => 1, circulate         => 0,
             coursereserves    => 0, editauthorities   => 0,
-            editcatalogue     => 0, management        => 0,
+            editcatalogue     => 0,
             parameters        => 0, permissions       => 0,
             plugins           => 0, reports           => 0,
             reserveforothers  => 0, serials           => 0,
@@ -205,6 +253,34 @@ my $hash2 = hash_password('password');
     );
     my $file_exists = ( -f $template->{filename} ) ? 1 : 0;
     is ( $file_exists, 1, 'The file errors/errorpage.tt should be accessible (contains integers)' );
+
+    # Regression test for env opac search limit override
+    $ENV{"OPAC_SEARCH_LIMIT"} = "branch:CPL";
+    $ENV{"OPAC_LIMIT_OVERRIDE"} = 1;
+
+    ( $template, $loggedinuser, $cookies) = get_template_and_user(
+        {
+            template_name => 'opac-main.tt',
+            query => $query,
+            type => 'opac',
+            authnotrequired => 1,
+        }
+    );
+    is($template->{VARS}->{'opac_name'}, "CPL", "Opac name was set correctly");
+    is($template->{VARS}->{'opac_search_limit'}, "branch:CPL", "Search limit was set correctly");
+
+    $ENV{"OPAC_SEARCH_LIMIT"} = "branch:multibranch-19";
+
+    ( $template, $loggedinuser, $cookies) = get_template_and_user(
+        {
+            template_name => 'opac-main.tt',
+            query => $query,
+            type => 'opac',
+            authnotrequired => 1,
+        }
+    );
+    is($template->{VARS}->{'opac_name'}, "multibranch-19", "Opac name was set correctly");
+    is($template->{VARS}->{'opac_search_limit'}, "branch:multibranch-19", "Search limit was set correctly");
 }
 
 # Check that there is always an OPACBaseURL set.
@@ -235,5 +311,38 @@ my ( $template2 );
 ok( ( any { 'OPACBaseURL' eq $_ } keys %{$template2->{VARS}} ),
     'OPACBaseURL is in Staff template' );
 
+my $hash1 = hash_password('password');
+my $hash2 = hash_password('password');
+
 ok(C4::Auth::checkpw_hash('password', $hash1), 'password validates with first hash');
 ok(C4::Auth::checkpw_hash('password', $hash2), 'password validates with second hash');
+
+subtest 'Check value of login_attempts in checkpw' => sub {
+    plan tests => 6;
+
+    t::lib::Mocks::mock_preference('FailedLoginAttempts', 3);
+
+    # Only interested here in regular login
+    $C4::Auth::cas  = 0;
+    $C4::Auth::ldap = 0;
+
+    my $patron = $builder->build_object({ class => 'Koha::Patrons' });
+    $patron->login_attempts(2);
+    $patron->password('123')->store; # yes, deliberately not hashed
+
+    is( $patron->account_locked, 0, 'Patron not locked' );
+    my @test = checkpw( $dbh, $patron->userid, '123', undef, 'opac', 1 );
+        # Note: 123 will not be hashed to 123 !
+    is( $test[0], 0, 'checkpw should have failed' );
+    $patron->discard_changes; # refresh
+    is( $patron->login_attempts, 3, 'Login attempts increased' );
+    is( $patron->account_locked, 1, 'Check locked status' );
+
+    # And another try to go over the limit: different return value!
+    @test = checkpw( $dbh, $patron->userid, '123', undef, 'opac', 1 );
+    is( @test, 0, 'checkpw failed again and returns nothing now' );
+    $patron->discard_changes; # refresh
+    is( $patron->login_attempts, 3, 'Login attempts not increased anymore' );
+};
+
+$schema->storage->txn_rollback;