Bug 19532: (follow-up) aria-hidden attr on OPAC, and more
[koha.git] / t / db_dependent / Auth_with_cas.t
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
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.
9 #
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.
14 #
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>.
17
18 use Modern::Perl;
19
20 use Test::More tests => 5;
21 use Test::MockModule;
22 use Test::MockObject;
23
24 use CGI;
25
26 use t::lib::Mocks;
27 use C4::Context;
28 use Koha::Database;
29
30 BEGIN {
31     use_ok('C4::Auth_with_cas', qw( check_api_auth_cas checkpw_cas login_cas logout_cas login_cas_url ));
32     can_ok('C4::Auth_with_cas', qw/
33             check_api_auth_cas
34             checkpw_cas
35             login_cas
36             logout_cas
37             login_cas_url
38             /);
39 }
40
41 my $schema = Koha::Database->new->schema;
42 $schema->storage->txn_begin;
43
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');
47
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';
51
52 $ENV{QUERY_STRING} = $query_string;
53 $ENV{SCRIPT_NAME} = '/cgi-bin/koha/opac-user.pl';
54
55 my $cgi = CGI->new($query_string);
56 $cgi->delete('ticket');
57
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)");
62
63 $ENV{SCRIPT_NAME} = '/cgi-bin/koha/circ/circulation-home.pl';
64 # intranet url test
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)");
68
69 subtest 'logout_cas() tests' => sub {
70
71     plan tests => 4;
72
73     my $cas_url = "https://mycasserver.url";
74
75     my $auth_with_cas_mock = Test::MockModule->new('C4::Auth_with_cas');
76     $auth_with_cas_mock->mock( '_get_cas_and_service', sub
77         {
78             my $cas = Test::MockObject->new();
79             $cas->mock( 'logout_url', sub {
80                 return "$cas_url/logout/?url=https://mykoha.url";
81             });
82             return ( $cas, "$cas_url?logout.x=1" );
83         }
84     );
85
86     my $cas_version;
87     my $expected_logout_url;
88
89     # Yeah, this gets funky
90     my $cgi_mock = Test::MockModule->new('CGI');
91     $cgi_mock->mock( 'redirect', sub {
92         my ( $self, $url ) = @_;
93         return $url;
94     });
95
96     # Test CAS 2.0 behavior
97     $cas_version = 2;
98     $expected_logout_url = "$cas_url/logout/?url=https://mykoha.url";
99
100     my $redirect_output = '';
101     close(STDOUT);
102     open(STDOUT, ">", \$redirect_output) or die "Error opening STDOUT";
103
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' );
108
109     # Test CAS 3.0 behavior
110     $redirect_output = '';
111     close(STDOUT);
112     open(STDOUT, ">", \$redirect_output) or die "Error opening STDOUT";
113
114     $cas_version = 3;
115     $expected_logout_url = "$cas_url/logout/?service=https://mykoha.url";
116
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' );
121 };