From d2f365751969474a6c845395cec0f7ad5480b8e8 Mon Sep 17 00:00:00 2001 From: Marcel de Rooy Date: Wed, 26 Jan 2022 12:27:56 +0000 Subject: [PATCH] Bug 29915: Changes to Auth.t Content-Type: text/plain; charset=utf-8 Test plan: Run Auth.t Signed-off-by: Marcel de Rooy --- t/db_dependent/Auth.t | 43 ++++++++++++++++++++++--------------------- 1 file changed, 22 insertions(+), 21 deletions(-) diff --git a/t/db_dependent/Auth.t b/t/db_dependent/Auth.t index 2e6290281e..10e4d0d71b 100755 --- a/t/db_dependent/Auth.t +++ b/t/db_dependent/Auth.t @@ -483,7 +483,7 @@ subtest 'check_cookie_auth' => sub { }; subtest 'checkauth & check_cookie_auth' => sub { - plan tests => 27; + plan tests => 29; # flags = 4 => { catalogue => 1 } my $patron = $builder->build_object({ class => 'Koha::Patrons', value => { flags => 4 } }); @@ -517,12 +517,12 @@ subtest 'checkauth & check_cookie_auth' => sub { $ENV{"HTTP_COOKIE"} = "CGISESSID=$sessionID"; # Not authenticated yet, checkauth didn't return the session - ( $userid, $cookie, $sessionID, $flags ) = C4::Auth::checkauth($cgi, 0, {catalogue => 1}); + ( $userid, $cookie, $sessionID, $flags ) = C4::Auth::checkauth($cgi, 0, {catalogue => 1}, undef, undef, undef, { skip_auth_template => 1 } ); is( $sessionID, undef); is( $userid, undef); - # Same here, check_cookie_auth does not return the session - my ( $auth_status, $session ) = C4::Auth::check_cookie_auth($sessionID, {catalogue => 1}); + # Sending undefined fails obviously + my ( $auth_status, $session ) = C4::Auth::check_cookie_auth($sessionID, {catalogue => 1} ); is( $auth_status, 'failed' ); is( $session, undef ); @@ -544,54 +544,55 @@ subtest 'checkauth & check_cookie_auth' => sub { ( $userid, $cookie, $sessionID, $flags ) = C4::Auth::checkauth($cgi, 0, {catalogue => 1}); is( $sessionID, undef ); is( $ENV{"HTTP_COOKIE"}, "CGISESSID=$first_sessionID", 'HTTP_COOKIE not unset' ); - ( $auth_status, $session) = C4::Auth::check_cookie_auth($cgi, 0, {catalogue => 1}); + ( $auth_status, $session) = C4::Auth::check_cookie_auth( $first_sessionID, {catalogue => 1} ); is( $auth_status, "expired"); is( $session, undef ); { # Trying to access without sessionID - undef $ENV{"HTTP_COOKIE"}; $cgi = CGI->new; - ( $auth_status, $session) = C4::Auth::check_cookie_auth(undef, 0, {catalogue => 1}); + ( $auth_status, $session) = C4::Auth::check_cookie_auth( undef, {catalogue => 1}); is( $auth_status, 'failed' ); is( $session, undef ); - ( $userid, $cookie, $sessionID, $flags ) = C4::Auth::checkauth($cgi, 0, {catalogue => 1}); + # This will fail on permissions + undef $ENV{"HTTP_COOKIE"}; + ( $userid, $cookie, $sessionID, $flags ) = C4::Auth::checkauth($cgi, 0, {catalogue => 1}, undef, undef, undef, { skip_auth_template => 1 } ); is( $userid, undef ); is( $sessionID, undef ); - } { - # Hit unauthorized page then reuse the cookie - undef $ENV{"HTTP_COOKIE"}; + # First logging in $cgi = CGI->new; - # Logging in $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; - $ENV{"HTTP_COOKIE"} = "CGISESSID=$sessionID"; - # Patron does not have the borrowers permission - ( $userid, $cookie, $sessionID, $flags ) = C4::Auth::checkauth($cgi, 0, {borrowers => 1}); + # $ENV{"HTTP_COOKIE"} = "CGISESSID=$sessionID"; # not needed, we use $cgi here + ( $userid, $cookie, $sessionID, $flags ) = C4::Auth::checkauth($cgi, 0, {borrowers => 1}, undef, undef, undef, { skip_auth_template => 1 } ); is( $userid, undef ); is( $sessionID, undef ); - ( $auth_status, $session) = C4::Auth::check_cookie_auth($cgi, 0, {borrowers => 1}); + # 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' ); - # Reuse the cookie + # 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 ); - is( $sessionID, $first_sessionID ); - - ( $auth_status, $session) = C4::Auth::check_cookie_auth($cgi, 0, {catalogue => 1}); + 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, $first_sessionID ); + is( $session->id, $sessionID, 'Same session' ); } }; -- 2.20.1