+
+subtest 'Check value of login_attempts in checkpw' => sub {
+ plan tests => 11;
+
+ 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( $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( $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' );
+
+ # Administrative lockout cannot be undone?
+ # Pass the right password now (or: add a nice mock).
+ my $auth = Test::MockModule->new( 'C4::Auth' );
+ $auth->mock( 'checkpw_hash', sub { return 1; } ); # not for production :)
+ $patron->login_attempts(0)->store;
+ @test = checkpw( $patron->userid, '123', undef, 'opac', 1 );
+ is( $test[0], 1, 'Build confidence in the mock' );
+ $patron->login_attempts(-1)->store;
+ is( $patron->account_locked, 1, 'Check administrative lockout' );
+ @test = checkpw( $patron->userid, '123', undef, 'opac', 1 );
+ is( @test, 0, 'checkpw gave red' );
+ $patron->discard_changes; # refresh
+ is( $patron->login_attempts, -1, 'Still locked out' );
+ t::lib::Mocks::mock_preference('FailedLoginAttempts', ''); # disable
+ is( $patron->account_locked, 1, 'Check administrative lockout without pref' );
+};
+
+subtest 'Check value of login_attempts in checkpw' => sub {
+ plan tests => 2;
+
+ t::lib::Mocks::mock_preference('FailedLoginAttempts', 3);
+ my $patron = $builder->build_object({ class => 'Koha::Patrons' });
+ $patron->set_password({ password => '123', skip_validation => 1 });
+
+ my @test = checkpw( $patron->userid, '123', undef, 'opac', 1 );
+ is( $test[0], 1, 'Patron authenticated correctly' );
+
+ $patron->password_expiration_date('2020-01-01')->store;
+ @test = checkpw( $patron->userid, '123', undef, 'opac', 1 );
+ is( $test[0], -2, 'Patron returned as expired correctly' );
+
+};
+
+subtest '_timeout_syspref' => sub {
+
+ plan tests => 6;
+
+ t::lib::Mocks::mock_preference('timeout', "100");
+ is( C4::Auth::_timeout_syspref, 100, );
+
+ t::lib::Mocks::mock_preference('timeout', "2d");
+ is( C4::Auth::_timeout_syspref, 2*86400, );
+
+ t::lib::Mocks::mock_preference('timeout', "2D");
+ is( C4::Auth::_timeout_syspref, 2*86400, );
+
+ t::lib::Mocks::mock_preference('timeout', "10h");
+ is( C4::Auth::_timeout_syspref, 10*3600, );
+
+ t::lib::Mocks::mock_preference('timeout', "10x");
+ warning_is
+ { is( C4::Auth::_timeout_syspref, 600, ); }
+ "The value of the system preference 'timeout' is not correct, defaulting to 600",
+ 'Bad values throw a warning and fallback to 600';
+};
+
+subtest 'check_cookie_auth' => sub {
+ plan tests => 4;
+
+ t::lib::Mocks::mock_preference('timeout', "1d"); # back to default
+
+ my $patron = $builder->build_object({ class => 'Koha::Patrons', value => { flags => 1 } });
+
+ # Mock a CGI object with real userid param
+ my $cgi = Test::MockObject->new();
+ $cgi->mock(
+ 'param',
+ sub {
+ my $var = shift;
+ if ( $var eq 'userid' ) { return $patron->userid; }
+ }
+ );
+ $cgi->mock('multi_param', sub {return q{}} );
+ $cgi->mock( 'cookie', sub { return; } );
+ $cgi->mock( 'request_method', sub { return 'POST' } );
+
+ $ENV{REMOTE_ADDR} = '127.0.0.1';
+
+ # Setting authnotrequired=1 or we wont' hit the return but the end of the sub that prints headers
+ my ( $userid, $cookie, $sessionID, $flags ) = C4::Auth::checkauth( $cgi, 1 );
+
+ my ($auth_status, $session) = C4::Auth::check_cookie_auth($sessionID);
+ isnt( $auth_status, 'ok', 'check_cookie_auth should not return ok if the user has not been authenticated before if no permissions needed' );
+ is( $auth_status, 'anon', 'check_cookie_auth should return anon if the user has not been authenticated before and no permissions needed' );
+
+ ( $userid, $cookie, $sessionID, $flags ) = C4::Auth::checkauth( $cgi, 1 );
+
+ ($auth_status, $session) = C4::Auth::check_cookie_auth($sessionID, {catalogue => 1});
+ isnt( $auth_status, 'ok', 'check_cookie_auth should not return ok if the user has not been authenticated before and permissions needed' );
+ is( $auth_status, 'anon', 'check_cookie_auth should return anon if the user has not been authenticated before and permissions needed' );
+
+ #FIXME We should have a test to cover 'failed' status when a user has logged in, but doesn't have permission
+};
+
+subtest 'checkauth & check_cookie_auth' => sub {
+ plan tests => 31;
+
+ # flags = 4 => { catalogue => 1 }
+ my $patron = $builder->build_object({ class => 'Koha::Patrons', value => { flags => 4 } });
+ my $password = 'password';
+ t::lib::Mocks::mock_preference( 'RequireStrongPassword', 0 );
+ $patron->set_password( { password => $password } );
+
+ my $cgi_mock = Test::MockModule->new('CGI');
+ $cgi_mock->mock( 'request_method', sub { return 'POST' } );
+
+ my $cgi = CGI->new;
+
+ my $auth = Test::MockModule->new( 'C4::Auth' );
+ # Tests will fail if we hit safe_exit
+ $auth->mock( 'safe_exit', sub { return } );
+
+ my ( $userid, $cookie, $sessionID, $flags );
+ {
+ # checkauth will redirect and safe_exit if not authenticated and not authorized
+ local *STDOUT;
+ my $stdout;
+ open STDOUT, '>', \$stdout;
+ C4::Auth::checkauth($cgi, 0, {catalogue => 1});
+ like( $stdout, qr{<title>\s*Log in to your account} );
+ $sessionID = ( $stdout =~ m{Set-Cookie: CGISESSID=((\d|\w)+);} ) ? $1 : undef;
+ ok($sessionID);
+ close STDOUT;
+ };
+
+ my $first_sessionID = $sessionID;
+
+ $ENV{"HTTP_COOKIE"} = "CGISESSID=$sessionID";
+ # Not authenticated yet, checkauth didn't return the session
+ {
+ local *STDOUT;
+ my $stdout;
+ open STDOUT, '>', \$stdout;
+ ( $userid, $cookie, $sessionID, $flags ) = C4::Auth::checkauth($cgi, 0, {catalogue => 1} );
+ close STDOUT;
+ }
+ is( $sessionID, undef);
+ is( $userid, undef);
+
+ # Sending undefined fails obviously
+ my ( $auth_status, $session ) = C4::Auth::check_cookie_auth($sessionID, {catalogue => 1} );
+ is( $auth_status, 'failed' );
+ is( $session, undef );
+
+ # Simulating the login form submission
+ $cgi->param('userid', $patron->userid);
+ $cgi->param('password', $password);
+
+ # Logged in!
+ ( $userid, $cookie, $sessionID, $flags ) = C4::Auth::checkauth($cgi, 0, {catalogue => 1});
+ is( $sessionID, $first_sessionID );
+ is( $userid, $patron->userid );
+
+ ( $auth_status, $session ) = C4::Auth::check_cookie_auth($sessionID, {catalogue => 1});
+ is( $auth_status, 'ok' );
+ is( $session->id, $first_sessionID );
+
+ # Logging out!
+ $cgi->param('logout.x', 1);
+ $cgi->delete( 'userid', 'password' );
+ {
+ local *STDOUT;
+ my $stdout;
+ open STDOUT, '>', \$stdout;
+ ( $userid, $cookie, $sessionID, $flags ) = C4::Auth::checkauth($cgi, 0, {catalogue => 1});
+ close STDOUT;
+ }
+ is( $sessionID, undef );
+ is( $ENV{"HTTP_COOKIE"}, "CGISESSID=$first_sessionID", 'HTTP_COOKIE not unset' );
+ ( $auth_status, $session) = C4::Auth::check_cookie_auth( $first_sessionID, {catalogue => 1} );
+ is( $auth_status, "expired");
+ is( $session, undef );
+
+ {
+ # Trying to access without sessionID
+ $cgi = CGI->new;
+ ( $auth_status, $session) = C4::Auth::check_cookie_auth(undef, {catalogue => 1});
+ is( $auth_status, 'failed' );
+ is( $session, undef );
+
+ # This will fail on permissions
+ undef $ENV{"HTTP_COOKIE"};
+ {
+ local *STDOUT;
+ my $stdout;
+ open STDOUT, '>', \$stdout;
+ ( $userid, $cookie, $sessionID, $flags ) = C4::Auth::checkauth($cgi, 0, {catalogue => 1} );
+ close STDOUT;
+ }
+ is( $userid, undef );
+ is( $sessionID, undef );
+ }
+
+ {
+ # First logging in
+ $cgi = CGI->new;
+ $cgi->param('userid', $patron->userid);
+ $cgi->param('password', $password);
+ ( $userid, $cookie, $sessionID, $flags ) = C4::Auth::checkauth($cgi, 0, {catalogue => 1});
+ is( $userid, $patron->userid );
+ $first_sessionID = $sessionID;
+
+ # Patron does not have the borrowers permission
+ # $ENV{"HTTP_COOKIE"} = "CGISESSID=$sessionID"; # not needed, we use $cgi here
+ {
+ local *STDOUT;
+ my $stdout;
+ open STDOUT, '>', \$stdout;
+ ( $userid, $cookie, $sessionID, $flags ) = C4::Auth::checkauth($cgi, 0, {borrowers => 1} );
+ close STDOUT;
+ }
+ is( $userid, undef );
+ is( $sessionID, undef );
+
+ # When calling check_cookie_auth, the session will be deleted
+ ( $auth_status, $session) = C4::Auth::check_cookie_auth( $first_sessionID, { borrowers => 1 } );
+ is( $auth_status, "failed" );
+ is( $session, undef );
+ ( $auth_status, $session) = C4::Auth::check_cookie_auth( $first_sessionID, { borrowers => 1 } );
+ is( $auth_status, 'expired', 'Session no longer exists' );
+
+ # NOTE: It is not what the UI is doing.
+ # From the UI we are allowed to hit an unauthorized page then reuse the session to hit back authorized area.
+ # It is because check_cookie_auth is ALWAYS called from checkauth WITHOUT $flagsrequired
+ # It then return "ok", when the previous called got "failed"
+
+ # Try reusing the deleted session: since it does not exist, we should get a new one now when passing correct permissions
+ $cgi->cookie( -name => 'CGISESSID', value => $first_sessionID );
+ ( $userid, $cookie, $sessionID, $flags ) = C4::Auth::checkauth($cgi, 0, {catalogue => 1});
+ is( $userid, $patron->userid );
+ isnt( $sessionID, undef, 'Check if we have a sessionID' );
+ isnt( $sessionID, $first_sessionID, 'New value expected' );
+ ( $auth_status, $session) = C4::Auth::check_cookie_auth( $sessionID, {catalogue => 1} );
+ is( $auth_status, "ok" );
+ is( $session->id, $sessionID, 'Same session' );
+ # Two additional tests on userenv
+ is( $C4::Context::context->{activeuser}, $session->id, 'Check if environment has been setup for session' );
+ is( C4::Context->userenv->{id}, $userid, 'Check userid in userenv' );
+ }
+};
+
+subtest 'Userenv clearing in check_cookie_auth' => sub {
+ # Note: We did already test userenv for a logged-in user in previous subtest
+ plan tests => 9;
+
+ t::lib::Mocks::mock_preference( 'timeout', 600 );
+ my $cgi = CGI->new;
+
+ # Create a new anonymous session by passing a fake session ID
+ $cgi->cookie( -name => 'CGISESSID', -value => 'fake_sessionID' );
+ my ($userid, $cookie, $sessionID, $flags ) = C4::Auth::checkauth($cgi, 1);
+ my ( $auth_status, $session) = C4::Auth::check_cookie_auth( $sessionID );
+ is( $auth_status, 'anon', 'Should be anonymous' );
+ is( $C4::Context::context->{activeuser}, $session->id, 'Check activeuser' );
+ is( defined C4::Context->userenv, 1, 'There should be a userenv' );
+ is( C4::Context->userenv->{id}, q{}, 'userid should be empty string' );
+
+ # Make the session expire now, check_cookie_auth will delete it
+ $session->param('lasttime', time() - 1200 );
+ $session->flush;
+ ( $auth_status, $session) = C4::Auth::check_cookie_auth( $sessionID );
+ is( $auth_status, 'expired', 'Should be expired' );
+ is( C4::Context->userenv, undef, 'Environment should be cleared too' );
+
+ # Show that we clear the userenv again: set up env and check deleted session
+ C4::Context->_new_userenv( $sessionID );
+ C4::Context->set_userenv; # empty
+ is( defined C4::Context->userenv, 1, 'There should be an empty userenv again' );
+ ( $auth_status, $session) = C4::Auth::check_cookie_auth( $sessionID );
+ is( $auth_status, 'expired', 'Should be expired already' );
+ is( C4::Context->userenv, undef, 'Environment should be cleared again' );
+};
+
+subtest 'create_basic_session tests' => sub {
+ plan tests => 13;
+
+ my $patron = $builder->build_object({ class => 'Koha::Patrons' });
+
+ my $session = C4::Auth::create_basic_session({ patron => $patron, interface => 'opac' });
+
+ isnt($session->id, undef, 'A new sessionID was created');
+ is( $session->param('number'), $patron->borrowernumber, 'Session parameter number matches' );
+ is( $session->param('id'), $patron->userid, 'Session parameter id matches' );
+ is( $session->param('cardnumber'), $patron->cardnumber, 'Session parameter cardnumber matches' );
+ is( $session->param('firstname'), $patron->firstname, 'Session parameter firstname matches' );
+ is( $session->param('surname'), $patron->surname, 'Session parameter surname matches' );
+ is( $session->param('branch'), $patron->branchcode, 'Session parameter branch matches' );
+ is( $session->param('branchname'), $patron->library->branchname, 'Session parameter branchname matches' );
+ is( $session->param('flags'), $patron->flags, 'Session parameter flags matches' );
+ is( $session->param('emailaddress'), $patron->email, 'Session parameter emailaddress matches' );
+ is( $session->param('ip'), $session->remote_addr(), 'Session parameter ip matches' );
+ is( $session->param('interface'), 'opac', 'Session parameter interface matches' );
+
+ $session = C4::Auth::create_basic_session({ patron => $patron, interface => 'staff' });
+ is( $session->param('interface'), 'intranet', 'Staff interface gets converted to intranet' );
+};
+
+$schema->storage->txn_rollback;