3 # This file is part of Koha.
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
20 use Test::More tests => 5;
31 use_ok('C4::Auth_with_cas');
32 can_ok('C4::Auth_with_cas', qw/
41 my $schema = Koha::Database->new->schema;
42 $schema->storage->txn_begin;
44 C4::Context->disable_syspref_cache();
45 t::lib::Mocks::mock_preference('OPACBaseURL','http://localhost');
46 t::lib::Mocks::mock_preference('staffClientBaseURL','http://localhost:8080');
48 my $opac_base_url = C4::Context->preference('OpacBaseURL');
49 my $staff_base_url = C4::Context->preference('staffClientBaseURL');
50 my $query_string = 'ticket=foo&bar=baz';
52 $ENV{QUERY_STRING} = $query_string;
53 $ENV{SCRIPT_NAME} = '/cgi-bin/koha/opac-user.pl';
55 my $cgi = CGI->new($query_string);
56 $cgi->delete('ticket');
58 # _url_with_get_params tests
59 is(C4::Auth_with_cas::_url_with_get_params($cgi, 'opac'),
60 "$opac_base_url/cgi-bin/koha/opac-user.pl?bar=baz",
61 "_url_with_get_params should return URL without deleted parameters (Bug 12398)");
63 $ENV{SCRIPT_NAME} = '/cgi-bin/koha/circ/circulation-home.pl';
65 is(C4::Auth_with_cas::_url_with_get_params($cgi, 'intranet'),
66 "$staff_base_url/cgi-bin/koha/circ/circulation-home.pl?bar=baz",
67 "Intranet URL should be returned when using intranet login (Bug 13507)");
69 subtest 'logout_cas() tests' => sub {
73 my $cas_url = "https://mycasserver.url";
75 my $auth_with_cas_mock = Test::MockModule->new('C4::Auth_with_cas');
76 $auth_with_cas_mock->mock( '_get_cas_and_service', sub
78 my $cas = Test::MockObject->new();
79 $cas->mock( 'logout_url', sub {
80 return "$cas_url/logout/?url=https://mykoha.url";
82 return ( $cas, "$cas_url?logout.x=1" );
87 my $expected_logout_url;
89 # Yeah, this gets funky
90 my $cgi_mock = Test::MockModule->new('CGI');
91 $cgi_mock->mock( 'redirect', sub {
92 my ( $self, $url ) = @_;
96 # Test CAS 2.0 behavior
98 $expected_logout_url = "$cas_url/logout/?url=https://mykoha.url";
100 my $redirect_output = '';
102 open(STDOUT, ">", \$redirect_output) or die "Error opening STDOUT";
104 t::lib::Mocks::mock_preference( 'casServerVersion', $cas_version );
105 C4::Auth_with_cas::logout_cas( CGI->new, 'anything' );
106 is( $redirect_output, $expected_logout_url, "The generated URL is correct (v$cas_version\.0)" );
107 unlike( $redirect_output, qr/logout\.x\=1/, 'logout.x=1 gets removed' );
109 # Test CAS 3.0 behavior
110 $redirect_output = '';
112 open(STDOUT, ">", \$redirect_output) or die "Error opening STDOUT";
115 $expected_logout_url = "$cas_url/logout/?service=https://mykoha.url";
117 t::lib::Mocks::mock_preference( 'casServerVersion', $cas_version );
118 C4::Auth_with_cas::logout_cas( CGI->new, 'anything' );
119 is( $redirect_output, $expected_logout_url, "The generated URL is correct (v$cas_version\.0)" );
120 unlike( $redirect_output, qr/logout\.x\=1/, 'logout.x=1 gets removed' );