3 # This Koha test module is a stub!
4 # Add more tests here!!!
12 use List::MoreUtils qw/all any none/;
13 use Test::More tests => 16;
16 use t::lib::TestBuilder;
20 use Koha::AuthUtils qw/hash_password/;
23 use Koha::Auth::TwoFactorAuth;
26 use_ok('C4::Auth', qw( checkauth haspermission track_login_daily checkpw get_template_and_user checkpw_hash ));
29 my $schema = Koha::Database->schema;
30 my $builder = t::lib::TestBuilder->new;
32 # FIXME: SessionStorage defaults to mysql, but it seems to break transaction
34 t::lib::Mocks::mock_preference( 'SessionStorage', 'tmp' );
35 t::lib::Mocks::mock_preference( 'GDPR_Policy', '' ); # Disabled
37 # To silence useless warnings
38 $ENV{REMOTE_ADDR} = '127.0.0.1';
40 $schema->storage->txn_begin;
42 subtest 'checkauth() tests' => sub {
46 my $patron = $builder->build_object({ class => 'Koha::Patrons', value => { flags => undef } });
48 # Mock a CGI object with real userid param
49 my $cgi = Test::MockObject->new();
54 if ( $var eq 'userid' ) { return $patron->userid; }
57 $cgi->mock( 'cookie', sub { return; } );
58 $cgi->mock( 'request_method', sub { return 'POST' } );
60 my $authnotrequired = 1;
61 my ( $userid, $cookie, $sessionID, $flags ) = C4::Auth::checkauth( $cgi, $authnotrequired );
63 is( $userid, undef, 'checkauth() returns undef for userid if no logged in user (Bug 18275)' );
65 my $db_user_id = C4::Context->config('user');
66 my $db_user_pass = C4::Context->config('pass');
67 $cgi = Test::MockObject->new();
68 $cgi->mock( 'cookie', sub { return; } );
69 $cgi->mock( 'param', sub {
70 my ( $self, $param ) = @_;
71 if ( $param eq 'userid' ) { return $db_user_id; }
72 elsif ( $param eq 'password' ) { return $db_user_pass; }
75 $cgi->mock( 'request_method', sub { return 'POST' } );
76 ( $userid, $cookie, $sessionID, $flags ) = C4::Auth::checkauth( $cgi, $authnotrequired );
77 is ( $userid, undef, 'If DB user is used, it should not be logged in' );
79 my $is_allowed = C4::Auth::haspermission( $db_user_id, { can_do => 'everything' } );
81 # FIXME This belongs to t/db_dependent/Auth/haspermission.t but we do not want to c/p the pervious mock statements
82 ok( !$is_allowed, 'DB user should not have any permissions');
84 subtest 'Prevent authentication when sending credential via GET' => sub {
88 my $patron = $builder->build_object(
89 { class => 'Koha::Patrons', value => { flags => 1 } } );
90 my $password = 'password';
91 t::lib::Mocks::mock_preference( 'RequireStrongPassword', 0 );
92 $patron->set_password( { password => $password } );
93 $cgi = Test::MockObject->new();
94 $cgi->mock( 'cookie', sub { return; } );
98 my ( $self, $param ) = @_;
99 if ( $param eq 'userid' ) { return $patron->userid; }
100 elsif ( $param eq 'password' ) { return $password; }
105 $cgi->mock( 'request_method', sub { return 'POST' } );
106 ( $userid, $cookie, $sessionID, $flags ) = C4::Auth::checkauth( $cgi, 'authrequired' );
107 is( $userid, $patron->userid, 'If librarian user is used and password with POST, they should be logged in' );
109 $cgi->mock( 'request_method', sub { return 'GET' } );
110 ( $userid, $cookie, $sessionID, $flags ) = C4::Auth::checkauth( $cgi, 'authrequired' );
111 is( $userid, undef, 'If librarian user is used and password with GET, they should not be logged in' );
114 subtest 'Two-factor authentication' => sub {
116 my $patron = $builder->build_object(
117 { class => 'Koha::Patrons', value => { flags => 1 } } );
118 my $password = 'password';
119 $patron->set_password( { password => $password } );
120 $cgi = Test::MockObject->new();
123 our ( $logout, $sessionID, $verified );
127 my ( $self, $param ) = @_;
128 if ( $param eq 'userid' ) { return $patron->userid; }
129 elsif ( $param eq 'password' ) { return $password; }
130 elsif ( $param eq 'otp_token' ) { return $otp_token; }
131 elsif ( $param eq 'logout.x' ) { return $logout; }
135 $cgi->mock( 'request_method', sub { return 'POST' } );
136 $cgi->mock( 'cookie', sub { return $sessionID } );
138 my $two_factor_auth = Test::MockModule->new( 'Koha::Auth::TwoFactorAuth' );
139 $two_factor_auth->mock( 'verify', sub {$verified} );
141 my ( $userid, $cookie, $flags );
142 ( $userid, $cookie, $sessionID, $flags ) = C4::Auth::checkauth( $cgi, 'authrequired', undef, 'intranet' );
148 C4::Auth::checkauth( $cgi, 'authrequired', undef, 'intranet' );
152 t::lib::Mocks::mock_preference( 'TwoFactorAuthentication', 0 );
153 $patron->auth_method('password')->store;
154 ( $userid, $cookie, $sessionID, $flags ) = C4::Auth::checkauth( $cgi, 'authrequired', undef, 'intranet' );
155 is( $userid, $patron->userid, 'Succesful login' );
156 is( C4::Auth::get_session($sessionID)->param('waiting-for-2FA'), undef, 'Second auth not required' );
159 $patron->auth_method('two-factor')->store;
160 ( $userid, $cookie, $sessionID, $flags ) = C4::Auth::checkauth( $cgi, 'authrequired', undef, 'intranet' );
161 is( $userid, $patron->userid, 'Succesful login' );
162 is( C4::Auth::get_session($sessionID)->param('waiting-for-2FA'), undef, 'Second auth not required' );
165 t::lib::Mocks::mock_preference( 'TwoFactorAuthentication', 1 );
166 t::lib::Mocks::mock_config('encryption_key', '1234tH1s=t&st');
167 $patron->auth_method('password')->store;
168 ( $userid, $cookie, $sessionID, $flags ) = C4::Auth::checkauth( $cgi, 'authrequired', undef, 'intranet' );
169 is( $userid, $patron->userid, 'Succesful login' );
170 is( C4::Auth::get_session($sessionID)->param('waiting-for-2FA'), undef, 'Second auth not required' );
173 $patron->encode_secret('one_secret');
174 $patron->auth_method('two-factor');
176 ( $userid, $cookie, $sessionID, $flags ) = C4::Auth::checkauth( $cgi, 'authrequired', undef, 'intranet' );
177 is( $userid, $patron->userid, 'Succesful login' );
178 my $session = C4::Auth::get_session($sessionID);
179 is( C4::Auth::get_session($sessionID)->param('waiting-for-2FA'), 1, 'Second auth required' );
182 $otp_token = "wrong";
184 $patron->auth_method('two-factor')->store;
185 ( $userid, $cookie, $sessionID, $flags ) = C4::Auth::checkauth( $cgi, 'authrequired', undef, 'intranet' );
186 is( $userid, $patron->userid, 'Succesful login' );
187 is( C4::Auth::get_session($sessionID)->param('waiting-for-2FA'), 1, 'Second auth still required after wrong OTP token' );
191 ( $userid, $cookie, $sessionID, $flags ) = C4::Auth::checkauth( $cgi, 'authrequired', undef, 'intranet' );
192 is( $userid, $patron->userid, 'Succesful login' );
193 is( C4::Auth::get_session($sessionID)->param('waiting-for-2FA'), 0, 'Second auth no longer required if OTP token has been verified' );
196 ( $userid, $cookie, $sessionID, $flags ) = C4::Auth::checkauth( $cgi, 'authrequired', undef, 'opac' );
197 is( $userid, $patron->userid, 'Succesful login at the OPAC' );
198 is( C4::Auth::get_session($sessionID)->param('waiting-for-2FA'), undef, 'No second auth required at the OPAC' );
200 t::lib::Mocks::mock_preference( 'TwoFactorAuthentication', 0 );
203 C4::Context->_new_userenv; # For next tests
207 subtest 'track_login_daily tests' => sub {
211 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
212 my $userid = $patron->userid;
214 $patron->lastseen( undef );
217 my $cache = Koha::Caches->get_instance();
218 my $cache_key = "track_login_" . $patron->userid;
219 $cache->clear_from_cache($cache_key);
221 t::lib::Mocks::mock_preference( 'TrackLastPatronActivity', '1' );
223 is( $patron->lastseen, undef, 'Patron should have not last seen when newly created' );
225 C4::Auth::track_login_daily( $userid );
226 $patron->_result()->discard_changes();
227 isnt( $patron->lastseen, undef, 'Patron should have last seen set when TrackLastPatronActivity = 1' );
229 sleep(1); # We need to wait a tiny bit to make sure the timestamp will be different
230 my $last_seen = $patron->lastseen;
231 C4::Auth::track_login_daily( $userid );
232 $patron->_result()->discard_changes();
233 is( $patron->lastseen, $last_seen, 'Patron last seen should still be unchanged' );
235 $cache->clear_from_cache($cache_key);
236 C4::Auth::track_login_daily( $userid );
237 $patron->_result()->discard_changes();
238 isnt( $patron->lastseen, $last_seen, 'Patron last seen should be changed if we cleared the cache' );
240 t::lib::Mocks::mock_preference( 'TrackLastPatronActivity', '0' );
241 $patron->lastseen( undef )->store;
242 $cache->clear_from_cache($cache_key);
243 C4::Auth::track_login_daily( $userid );
244 $patron->_result()->discard_changes();
245 is( $patron->lastseen, undef, 'Patron should still have last seen unchanged when TrackLastPatronActivity = 0' );
249 subtest 'no_set_userenv parameter tests' => sub {
253 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
254 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
255 my $password = 'password';
257 t::lib::Mocks::mock_preference( 'RequireStrongPassword', 0 );
258 $patron->set_password({ password => $password });
260 ok( checkpw( $patron->userid, $password, undef, undef, 1 ), 'checkpw returns true' );
261 is( C4::Context->userenv, undef, 'Userenv should be undef as required' );
262 C4::Context->_new_userenv('DUMMY SESSION');
263 C4::Context->set_userenv(0,0,0,'firstname','surname', $library->branchcode, 'Library 1', 0, '', '');
264 is( C4::Context->userenv->{branch}, $library->branchcode, 'Userenv gives correct branch' );
265 ok( checkpw( $patron->userid, $password, undef, undef, 1 ), 'checkpw returns true' );
266 is( C4::Context->userenv->{branch}, $library->branchcode, 'Userenv branch is preserved if no_set_userenv is true' );
267 ok( checkpw( $patron->userid, $password, undef, undef, 0 ), 'checkpw still returns true' );
268 isnt( C4::Context->userenv->{branch}, $library->branchcode, 'Userenv branch is overwritten if no_set_userenv is false' );
271 subtest 'checkpw lockout tests' => sub {
275 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
276 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
277 my $password = 'password';
278 t::lib::Mocks::mock_preference( 'RequireStrongPassword', 0 );
279 t::lib::Mocks::mock_preference( 'FailedLoginAttempts', 1 );
280 $patron->set_password({ password => $password });
282 my ( $checkpw, undef, undef ) = checkpw( $patron->cardnumber, $password, undef, undef, 1 );
283 ok( $checkpw, 'checkpw returns true with right password when logging in via cardnumber' );
284 ( $checkpw, undef, undef ) = checkpw( $patron->userid, "wrong_password", undef, undef, 1 );
285 is( $checkpw, 0, 'checkpw returns false when given wrong password' );
286 $patron = $patron->get_from_storage;
287 is( $patron->account_locked, 1, "Account is locked from failed login");
288 ( $checkpw, undef, undef ) = checkpw( $patron->userid, $password, undef, undef, 1 );
289 is( $checkpw, undef, 'checkpw returns undef with right password when account locked' );
290 ( $checkpw, undef, undef ) = checkpw( $patron->cardnumber, $password, undef, undef, 1 );
291 is( $checkpw, undef, 'checkpw returns undefwith right password when logging in via cardnumber if account locked' );
295 # get_template_and_user tests
297 subtest 'get_template_and_user' => sub { # Tests for the language URL parameter
299 sub MockedCheckauth {
300 my ($query,$authnotrequired,$flagsrequired,$type) = @_;
302 my $userid = 'cobain';
304 # we don't need to bother about permissions for this test
306 superlibrarian => 1, acquisition => 0,
308 catalogue => 1, circulate => 0,
309 coursereserves => 0, editauthorities => 0,
311 parameters => 0, permissions => 0,
312 plugins => 0, reports => 0,
313 reserveforothers => 0, serials => 0,
314 staffaccess => 0, tools => 0,
318 my $session_cookie = $query->cookie(
319 -name => 'CGISESSID',
324 return ( $userid, [ $session_cookie ], $sessionID, $flags );
327 # Mock checkauth, build the scenario
328 my $auth = Test::MockModule->new( 'C4::Auth' );
329 $auth->mock( 'checkauth', \&MockedCheckauth );
331 # Make sure 'EnableOpacSearchHistory' is set
332 t::lib::Mocks::mock_preference('EnableOpacSearchHistory',1);
333 # Enable es-ES for the OPAC and staff interfaces
334 t::lib::Mocks::mock_preference('OPACLanguages','en,es-ES');
335 t::lib::Mocks::mock_preference('language','en,es-ES');
337 # we need a session cookie
338 $ENV{"SERVER_PORT"} = 80;
339 $ENV{"HTTP_COOKIE"} = 'CGISESSID=nirvana';
341 my $query = CGI->new;
342 $query->param('language','es-ES');
344 my ( $template, $loggedinuser, $cookies ) = get_template_and_user(
346 template_name => "about.tt",
349 authnotrequired => 1,
350 flagsrequired => { catalogue => 1 },
355 ok ( ( all { ref($_) eq 'CGI::Cookie' } @$cookies ),
356 'BZ9735: the cookies array is flat' );
358 # new query, with non-existent language (we only have en and es-ES)
359 $query->param('language','tomas');
361 ( $template, $loggedinuser, $cookies ) = get_template_and_user(
363 template_name => "about.tt",
366 authnotrequired => 1,
367 flagsrequired => { catalogue => 1 },
372 ok( ( none { $_->name eq 'KohaOpacLanguage' and $_->value eq 'tomas' } @$cookies ),
373 'BZ9735: invalid language, it is not set');
375 ok( ( any { $_->name eq 'KohaOpacLanguage' and $_->value eq 'en' } @$cookies ),
376 'BZ9735: invalid language, then default to en');
378 for my $template_name (
380 ../../../../../../../../../../../../../../../etc/passwd
381 test/../../../../../../../../../../../../../../etc/passwd
383 test/does_not_finished_by_tt_t
387 ( $template, $loggedinuser, $cookies ) = get_template_and_user(
389 template_name => $template_name,
392 authnotrequired => 1,
393 flagsrequired => { catalogue => 1 },
397 like ( $@, qr(bad template path), "The file $template_name should not be accessible" );
399 ( $template, $loggedinuser, $cookies ) = get_template_and_user(
401 template_name => 'errors/errorpage.tt',
404 authnotrequired => 1,
405 flagsrequired => { catalogue => 1 },
408 my $file_exists = ( -f $template->{filename} ) ? 1 : 0;
409 is ( $file_exists, 1, 'The file errors/errorpage.tt should be accessible (contains integers)' );
411 # Regression test for env opac search limit override
412 $ENV{"OPAC_SEARCH_LIMIT"} = "branch:CPL";
413 $ENV{"OPAC_LIMIT_OVERRIDE"} = 1;
415 ( $template, $loggedinuser, $cookies) = get_template_and_user(
417 template_name => 'opac-main.tt',
420 authnotrequired => 1,
423 is($template->{VARS}->{'opac_name'}, "CPL", "Opac name was set correctly");
424 is($template->{VARS}->{'opac_search_limit'}, "branch:CPL", "Search limit was set correctly");
426 $ENV{"OPAC_SEARCH_LIMIT"} = "branch:multibranch-19";
428 ( $template, $loggedinuser, $cookies) = get_template_and_user(
430 template_name => 'opac-main.tt',
433 authnotrequired => 1,
436 is($template->{VARS}->{'opac_name'}, "multibranch-19", "Opac name was set correctly");
437 is($template->{VARS}->{'opac_search_limit'}, "branch:multibranch-19", "Search limit was set correctly");
439 delete $ENV{"HTTP_COOKIE"};
442 # Check that there is always an OPACBaseURL set.
443 my $input = CGI->new();
444 my ( $template1, $borrowernumber, $cookie );
445 ( $template1, $borrowernumber, $cookie ) = get_template_and_user(
447 template_name => "opac-detail.tt",
450 authnotrequired => 1,
454 ok( ( any { 'OPACBaseURL' eq $_ } keys %{$template1->{VARS}} ),
455 'OPACBaseURL is in OPAC template' );
458 ( $template2, $borrowernumber, $cookie ) = get_template_and_user(
460 template_name => "catalogue/detail.tt",
463 authnotrequired => 1,
467 ok( ( any { 'OPACBaseURL' eq $_ } keys %{$template2->{VARS}} ),
468 'OPACBaseURL is in Staff template' );
470 my $hash1 = hash_password('password');
471 my $hash2 = hash_password('password');
473 ok(C4::Auth::checkpw_hash('password', $hash1), 'password validates with first hash');
474 ok(C4::Auth::checkpw_hash('password', $hash2), 'password validates with second hash');
476 subtest 'Check value of login_attempts in checkpw' => sub {
479 t::lib::Mocks::mock_preference('FailedLoginAttempts', 3);
481 # Only interested here in regular login
485 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
486 $patron->login_attempts(2);
487 $patron->password('123')->store; # yes, deliberately not hashed
489 is( $patron->account_locked, 0, 'Patron not locked' );
490 my @test = checkpw( $patron->userid, '123', undef, 'opac', 1 );
491 # Note: 123 will not be hashed to 123 !
492 is( $test[0], 0, 'checkpw should have failed' );
493 $patron->discard_changes; # refresh
494 is( $patron->login_attempts, 3, 'Login attempts increased' );
495 is( $patron->account_locked, 1, 'Check locked status' );
497 # And another try to go over the limit: different return value!
498 @test = checkpw( $patron->userid, '123', undef, 'opac', 1 );
499 is( @test, 0, 'checkpw failed again and returns nothing now' );
500 $patron->discard_changes; # refresh
501 is( $patron->login_attempts, 3, 'Login attempts not increased anymore' );
503 # Administrative lockout cannot be undone?
504 # Pass the right password now (or: add a nice mock).
505 my $auth = Test::MockModule->new( 'C4::Auth' );
506 $auth->mock( 'checkpw_hash', sub { return 1; } ); # not for production :)
507 $patron->login_attempts(0)->store;
508 @test = checkpw( $patron->userid, '123', undef, 'opac', 1 );
509 is( $test[0], 1, 'Build confidence in the mock' );
510 $patron->login_attempts(-1)->store;
511 is( $patron->account_locked, 1, 'Check administrative lockout' );
512 @test = checkpw( $patron->userid, '123', undef, 'opac', 1 );
513 is( @test, 0, 'checkpw gave red' );
514 $patron->discard_changes; # refresh
515 is( $patron->login_attempts, -1, 'Still locked out' );
516 t::lib::Mocks::mock_preference('FailedLoginAttempts', ''); # disable
517 is( $patron->account_locked, 1, 'Check administrative lockout without pref' );
520 subtest 'Check value of login_attempts in checkpw' => sub {
523 t::lib::Mocks::mock_preference('FailedLoginAttempts', 3);
524 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
525 $patron->set_password({ password => '123', skip_validation => 1 });
527 my @test = checkpw( $patron->userid, '123', undef, 'opac', 1 );
528 is( $test[0], 1, 'Patron authenticated correctly' );
530 $patron->password_expiration_date('2020-01-01')->store;
531 @test = checkpw( $patron->userid, '123', undef, 'opac', 1 );
532 is( $test[0], -2, 'Patron returned as expired correctly' );
536 subtest '_timeout_syspref' => sub {
540 t::lib::Mocks::mock_preference('timeout', "100");
541 is( C4::Auth::_timeout_syspref, 100, );
543 t::lib::Mocks::mock_preference('timeout', "2d");
544 is( C4::Auth::_timeout_syspref, 2*86400, );
546 t::lib::Mocks::mock_preference('timeout', "2D");
547 is( C4::Auth::_timeout_syspref, 2*86400, );
549 t::lib::Mocks::mock_preference('timeout', "10h");
550 is( C4::Auth::_timeout_syspref, 10*3600, );
552 t::lib::Mocks::mock_preference('timeout', "10x");
554 { is( C4::Auth::_timeout_syspref, 600, ); }
555 "The value of the system preference 'timeout' is not correct, defaulting to 600",
556 'Bad values throw a warning and fallback to 600';
559 subtest 'check_cookie_auth' => sub {
562 t::lib::Mocks::mock_preference('timeout', "1d"); # back to default
564 my $patron = $builder->build_object({ class => 'Koha::Patrons', value => { flags => 1 } });
566 # Mock a CGI object with real userid param
567 my $cgi = Test::MockObject->new();
572 if ( $var eq 'userid' ) { return $patron->userid; }
575 $cgi->mock('multi_param', sub {return q{}} );
576 $cgi->mock( 'cookie', sub { return; } );
577 $cgi->mock( 'request_method', sub { return 'POST' } );
579 $ENV{REMOTE_ADDR} = '127.0.0.1';
581 # Setting authnotrequired=1 or we wont' hit the return but the end of the sub that prints headers
582 my ( $userid, $cookie, $sessionID, $flags ) = C4::Auth::checkauth( $cgi, 1 );
584 my ($auth_status, $session) = C4::Auth::check_cookie_auth($sessionID);
585 isnt( $auth_status, 'ok', 'check_cookie_auth should not return ok if the user has not been authenticated before if no permissions needed' );
586 is( $auth_status, 'anon', 'check_cookie_auth should return anon if the user has not been authenticated before and no permissions needed' );
588 ( $userid, $cookie, $sessionID, $flags ) = C4::Auth::checkauth( $cgi, 1 );
590 ($auth_status, $session) = C4::Auth::check_cookie_auth($sessionID, {catalogue => 1});
591 isnt( $auth_status, 'ok', 'check_cookie_auth should not return ok if the user has not been authenticated before and permissions needed' );
592 is( $auth_status, 'anon', 'check_cookie_auth should return anon if the user has not been authenticated before and permissions needed' );
594 #FIXME We should have a test to cover 'failed' status when a user has logged in, but doesn't have permission
597 subtest 'checkauth & check_cookie_auth' => sub {
600 # flags = 4 => { catalogue => 1 }
601 my $patron = $builder->build_object({ class => 'Koha::Patrons', value => { flags => 4 } });
602 my $password = 'password';
603 t::lib::Mocks::mock_preference( 'RequireStrongPassword', 0 );
604 $patron->set_password( { password => $password } );
606 my $cgi_mock = Test::MockModule->new('CGI');
607 $cgi_mock->mock( 'request_method', sub { return 'POST' } );
611 my $auth = Test::MockModule->new( 'C4::Auth' );
612 # Tests will fail if we hit safe_exit
613 $auth->mock( 'safe_exit', sub { return } );
615 my ( $userid, $cookie, $sessionID, $flags );
617 # checkauth will redirect and safe_exit if not authenticated and not authorized
620 open STDOUT, '>', \$stdout;
621 C4::Auth::checkauth($cgi, 0, {catalogue => 1});
622 like( $stdout, qr{<title>\s*Log in to your account} );
623 $sessionID = ( $stdout =~ m{Set-Cookie: CGISESSID=((\d|\w)+);} ) ? $1 : undef;
628 my $first_sessionID = $sessionID;
630 $ENV{"HTTP_COOKIE"} = "CGISESSID=$sessionID";
631 # Not authenticated yet, checkauth didn't return the session
635 open STDOUT, '>', \$stdout;
636 ( $userid, $cookie, $sessionID, $flags ) = C4::Auth::checkauth($cgi, 0, {catalogue => 1} );
639 is( $sessionID, undef);
642 # Sending undefined fails obviously
643 my ( $auth_status, $session ) = C4::Auth::check_cookie_auth($sessionID, {catalogue => 1} );
644 is( $auth_status, 'failed' );
645 is( $session, undef );
647 # Simulating the login form submission
648 $cgi->param('userid', $patron->userid);
649 $cgi->param('password', $password);
652 ( $userid, $cookie, $sessionID, $flags ) = C4::Auth::checkauth($cgi, 0, {catalogue => 1});
653 is( $sessionID, $first_sessionID );
654 is( $userid, $patron->userid );
656 ( $auth_status, $session ) = C4::Auth::check_cookie_auth($sessionID, {catalogue => 1});
657 is( $auth_status, 'ok' );
658 is( $session->id, $first_sessionID );
661 $cgi->param('logout.x', 1);
662 $cgi->delete( 'userid', 'password' );
666 open STDOUT, '>', \$stdout;
667 ( $userid, $cookie, $sessionID, $flags ) = C4::Auth::checkauth($cgi, 0, {catalogue => 1});
670 is( $sessionID, undef );
671 is( $ENV{"HTTP_COOKIE"}, "CGISESSID=$first_sessionID", 'HTTP_COOKIE not unset' );
672 ( $auth_status, $session) = C4::Auth::check_cookie_auth( $first_sessionID, {catalogue => 1} );
673 is( $auth_status, "expired");
674 is( $session, undef );
677 # Trying to access without sessionID
679 ( $auth_status, $session) = C4::Auth::check_cookie_auth(undef, {catalogue => 1});
680 is( $auth_status, 'failed' );
681 is( $session, undef );
683 # This will fail on permissions
684 undef $ENV{"HTTP_COOKIE"};
688 open STDOUT, '>', \$stdout;
689 ( $userid, $cookie, $sessionID, $flags ) = C4::Auth::checkauth($cgi, 0, {catalogue => 1} );
692 is( $userid, undef );
693 is( $sessionID, undef );
699 $cgi->param('userid', $patron->userid);
700 $cgi->param('password', $password);
701 ( $userid, $cookie, $sessionID, $flags ) = C4::Auth::checkauth($cgi, 0, {catalogue => 1});
702 is( $userid, $patron->userid );
703 $first_sessionID = $sessionID;
705 # Patron does not have the borrowers permission
706 # $ENV{"HTTP_COOKIE"} = "CGISESSID=$sessionID"; # not needed, we use $cgi here
710 open STDOUT, '>', \$stdout;
711 ( $userid, $cookie, $sessionID, $flags ) = C4::Auth::checkauth($cgi, 0, {borrowers => 1} );
714 is( $userid, undef );
715 is( $sessionID, undef );
717 # When calling check_cookie_auth, the session will be deleted
718 ( $auth_status, $session) = C4::Auth::check_cookie_auth( $first_sessionID, { borrowers => 1 } );
719 is( $auth_status, "failed" );
720 is( $session, undef );
721 ( $auth_status, $session) = C4::Auth::check_cookie_auth( $first_sessionID, { borrowers => 1 } );
722 is( $auth_status, 'expired', 'Session no longer exists' );
724 # NOTE: It is not what the UI is doing.
725 # From the UI we are allowed to hit an unauthorized page then reuse the session to hit back authorized area.
726 # It is because check_cookie_auth is ALWAYS called from checkauth WITHOUT $flagsrequired
727 # It then return "ok", when the previous called got "failed"
729 # Try reusing the deleted session: since it does not exist, we should get a new one now when passing correct permissions
730 $cgi->cookie( -name => 'CGISESSID', value => $first_sessionID );
731 ( $userid, $cookie, $sessionID, $flags ) = C4::Auth::checkauth($cgi, 0, {catalogue => 1});
732 is( $userid, $patron->userid );
733 isnt( $sessionID, undef, 'Check if we have a sessionID' );
734 isnt( $sessionID, $first_sessionID, 'New value expected' );
735 ( $auth_status, $session) = C4::Auth::check_cookie_auth( $sessionID, {catalogue => 1} );
736 is( $auth_status, "ok" );
737 is( $session->id, $sessionID, 'Same session' );
738 # Two additional tests on userenv
739 is( $C4::Context::context->{activeuser}, $session->id, 'Check if environment has been setup for session' );
740 is( C4::Context->userenv->{id}, $userid, 'Check userid in userenv' );
744 subtest 'Userenv clearing in check_cookie_auth' => sub {
745 # Note: We did already test userenv for a logged-in user in previous subtest
748 t::lib::Mocks::mock_preference( 'timeout', 600 );
751 # Create a new anonymous session by passing a fake session ID
752 $cgi->cookie( -name => 'CGISESSID', -value => 'fake_sessionID' );
753 my ($userid, $cookie, $sessionID, $flags ) = C4::Auth::checkauth($cgi, 1);
754 my ( $auth_status, $session) = C4::Auth::check_cookie_auth( $sessionID );
755 is( $auth_status, 'anon', 'Should be anonymous' );
756 is( $C4::Context::context->{activeuser}, $session->id, 'Check activeuser' );
757 is( defined C4::Context->userenv, 1, 'There should be a userenv' );
758 is( C4::Context->userenv->{id}, q{}, 'userid should be empty string' );
760 # Make the session expire now, check_cookie_auth will delete it
761 $session->param('lasttime', time() - 1200 );
763 ( $auth_status, $session) = C4::Auth::check_cookie_auth( $sessionID );
764 is( $auth_status, 'expired', 'Should be expired' );
765 is( C4::Context->userenv, undef, 'Environment should be cleared too' );
767 # Show that we clear the userenv again: set up env and check deleted session
768 C4::Context->_new_userenv( $sessionID );
769 C4::Context->set_userenv; # empty
770 is( defined C4::Context->userenv, 1, 'There should be an empty userenv again' );
771 ( $auth_status, $session) = C4::Auth::check_cookie_auth( $sessionID );
772 is( $auth_status, 'expired', 'Should be expired already' );
773 is( C4::Context->userenv, undef, 'Environment should be cleared again' );
776 $schema->storage->txn_rollback;