From b0142dcc9f66738fe2830b42a3b93e205c238858 Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Tue, 16 Mar 2021 16:46:14 -0300 Subject: [PATCH] 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 Signed-off-by: Katrin Fischer --- t/db_dependent/Auth_with_cas.t | 61 ++++++++++++++++++++++++++++++++++++++---- 1 file changed, 56 insertions(+), 5 deletions(-) diff --git a/t/db_dependent/Auth_with_cas.t b/t/db_dependent/Auth_with_cas.t index 2f1075542e..3038228e6a 100755 --- a/t/db_dependent/Auth_with_cas.t +++ b/t/db_dependent/Auth_with_cas.t @@ -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' ); +}; -- 2.11.0