Bug 20854: Add more CAS tests

This patch aims to add full test coverage for 'logout_cas' the way we do
it nowadays (in a subtest, mocking things, etc). It is provided as a
separate patch so we can test the follow-up tidy and see no behavior
change.

To test:
1. Run:
   $ kshell
  k$ prove t/db_dependent/Auth_with_cas.t
=> SUCCESS: Tests pass!
2. Apply this patch
3. Repeat 1
=> SUCCESS: Tests pass!
4. Apply the refactoring follow-up
5. Repeat 3
=> SUCCESS: Tests still pass!
6. Sign off :-D

Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>

Signed-off-by: Katrin Fischer <katrin.fischer.83@web.de>

Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
(cherry picked from commit c71dda04c6)
Signed-off-by: Fridolin Somers <fridolin.somers@biblibre.com>
This commit is contained in:
Tomas Cohen Arazi 2021-03-16 16:46:14 -03:00 committed by Fridolin Somers
parent 26e7262963
commit 225517a0f3
1 changed files with 56 additions and 5 deletions

View File

@ -18,6 +18,9 @@
use Modern::Perl;
use Test::More tests => 5;
use Test::MockModule;
use Test::MockObject;
use CGI;
use t::lib::Mocks;
@ -63,8 +66,56 @@ is(C4::Auth_with_cas::_url_with_get_params($cgi, 'intranet'),
"$staff_base_url/cgi-bin/koha/circ/circulation-home.pl?bar=baz",
"Intranet URL should be returned when using intranet login (Bug 13507)");
# logout parameter
t::lib::Mocks::mock_preference('casServerVersion','3');
is(C4::Auth_with_cas::_fix_logout_url('https://mycasserver.url/logout/?url=https://mykoha.url'),
'https://mycasserver.url/logout/?service=https://mykoha.url',
'service parameter should be used on logout when Cas server is 3.0 or superior (Bug 20854)');
subtest 'logout_cas() tests' => sub {
plan tests => 4;
my $cas_url = "https://mycasserver.url";
my $auth_with_cas_mock = Test::MockModule->new('C4::Auth_with_cas');
$auth_with_cas_mock->mock( '_get_cas_and_service', sub
{
my $cas = Test::MockObject->new();
$cas->mock( 'logout_url', sub {
return "$cas_url/logout/?url=https://mykoha.url";
});
return ( $cas, "$cas_url?logout.x=1" );
}
);
my $cas_version;
my $expected_logout_url;
# Yeah, this gets funky
my $cgi_mock = Test::MockModule->new('CGI');
$cgi_mock->mock( 'redirect', sub {
my ( $self, $url ) = @_;
return $url;
});
# Test CAS 2.0 behavior
$cas_version = 2;
$expected_logout_url = "$cas_url/logout/?url=https://mykoha.url";
my $redirect_output = '';
close(STDOUT);
open(STDOUT, ">", \$redirect_output) or die "Error opening STDOUT";
t::lib::Mocks::mock_preference( 'casServerVersion', $cas_version );
C4::Auth_with_cas::logout_cas( CGI->new, 'anything' );
is( $redirect_output, $expected_logout_url, "The generated URL is correct (v$cas_version\.0)" );
unlike( $redirect_output, qr/logout\.x\=1/, 'logout.x=1 gets removed' );
# Test CAS 3.0 behavior
$redirect_output = '';
close(STDOUT);
open(STDOUT, ">", \$redirect_output) or die "Error opening STDOUT";
$cas_version = 3;
$expected_logout_url = "$cas_url/logout/?service=https://mykoha.url";
t::lib::Mocks::mock_preference( 'casServerVersion', $cas_version );
C4::Auth_with_cas::logout_cas( CGI->new, 'anything' );
is( $redirect_output, $expected_logout_url, "The generated URL is correct (v$cas_version\.0)" );
unlike( $redirect_output, qr/logout\.x\=1/, 'logout.x=1 gets removed' );
};