From bdb5e2bdbd7a7ad101d0479e5fdd776a1ad7cb1b Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Thu, 25 Jan 2024 09:36:01 +0100 Subject: [PATCH] Bug 35890: Add tests for AutoLocation MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Matt Blenkinsop Signed-off-by: Kyle M Hall (cherry picked from commit 85717a99c7ba20d3bef8e9ba15df6d0a86f368c6) Signed-off-by: Fridolin Somers (cherry picked from commit 6e4711d925ae705ecba6dcf78e030dc149c80017) Signed-off-by: Frédéric Demians --- t/db_dependent/Auth.t | 59 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/t/db_dependent/Auth.t b/t/db_dependent/Auth.t index 5cc625e655..24344790c1 100755 --- a/t/db_dependent/Auth.t +++ b/t/db_dependent/Auth.t @@ -1306,3 +1306,62 @@ subtest 'checkpw() return values tests' => sub { $schema->storage->txn_rollback; }; }; + +subtest 'AutoLocation' => sub { + + plan tests => 6; + + $schema->storage->txn_begin; + + t::lib::Mocks::mock_preference( 'AutoLocation', 0 ); + + my $patron = $builder->build_object( { class => 'Koha::Patrons', value => { flags => 1 } } ); + 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'); + + # Simulating the login form submission + $cgi->param( 'userid', $patron->userid ); + $cgi->param( 'password', $password ); + + $ENV{REMOTE_ADDR} = '127.0.0.1'; + my ( $userid, $cookie, $sessionID, $flags ) = C4::Auth::checkauth( $cgi, 0, { catalogue => 1 }, 'intranet' ); + is( $userid, $patron->userid ); + + my $template; + t::lib::Mocks::mock_preference( 'AutoLocation', 1 ); + + # AutoLocation: "Require staff to log in from a computer in the IP address range specified by their library (if any)" + $patron->library->branchip('')->store; # There is none, allow access from anywhere + ( $userid, $cookie, $sessionID, $flags, $template ) = + C4::Auth::checkauth( $cgi, 0, { catalogue => 1 }, 'intranet' ); + is( $userid, $patron->userid ); + is( $template, undef ); + + $patron->library->branchip('1.2.3.4')->store; + ( $userid, $cookie, $sessionID, $flags, $template ) = + C4::Auth::checkauth( $cgi, 0, { catalogue => 1 }, 'intranet', undef, undef, { do_not_print => 1 } ); + is( $template->{VARS}->{wrongip}, 1 ); + + $patron->library->branchip('127.0.0.1')->store; + ( $userid, $cookie, $sessionID, $flags, $template ) = + C4::Auth::checkauth( $cgi, 0, { catalogue => 1 }, 'intranet' ); + is( $userid, $patron->userid ); + is( $template, undef ); + + $schema->storage->txn_rollback; + +}; + +sub set_weak_password { + my ($patron) = @_; + my $password = 'password'; + t::lib::Mocks::mock_preference( 'RequireStrongPassword', 0 ); + $patron->set_password( { password => $password } ); + return $password; +} -- 2.39.5