Bug 12026: [Rebase Follow-up] - Fix Test
[koha.git] / t / Auth_with_shibboleth.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 $| = 1;
21 use Module::Load::Conditional qw/check_install/;
22 use Test::More;
23 use Test::MockModule;
24 use Test::Warn;
25
26 use CGI;
27 use C4::Context;
28
29 BEGIN {
30     if ( check_install( module => 'Test::DBIx::Class' ) ) {
31         plan tests => 11;
32     } else {
33         plan skip_all => "Need Test::DBIx::Class"
34     }
35 }
36
37 use Test::DBIx::Class { schema_class => 'Koha::Schema', connect_info => ['dbi:SQLite:dbname=:memory:','',''] };
38
39 # Mock Variables
40 my $matchpoint = 'userid';
41 my %mapping = ( 'userid' => { 'is' => 'uid' }, );
42 $ENV{'uid'} = "test1234";
43
44 # Setup Mocks
45 ## Mock Context
46 my $context = new Test::MockModule('C4::Context');
47
48 ### Mock ->config
49 $context->mock( 'config', \&mockedConfig );
50
51 sub mockedConfig {
52     my $param = shift;
53
54     my %shibboleth = (
55         'matchpoint' => $matchpoint,
56         'mapping'    => \%mapping
57     );
58
59     return \%shibboleth;
60 }
61
62 ### Mock ->preference
63 my $OPACBaseURL = "testopac.com";
64 $context->mock( 'preference', \&mockedPref );
65
66 sub mockedPref {
67     my $param = $_[1];
68     my $return;
69
70     if ( $param eq 'OPACBaseURL' ) {
71         $return = $OPACBaseURL;
72     }
73
74     return $return;
75 }
76
77 ## Mock Database
78 my $database = new Test::MockModule('Koha::Database');
79
80 ### Mock ->schema
81 $database->mock( 'schema', \&mockedSchema );
82
83 sub mockedSchema {
84     return Schema();
85 }
86
87 ## Convenience method to reset config
88 sub reset_config {
89     $matchpoint = 'userid';
90     %mapping    = ( 'userid' => { 'is' => 'uid' }, );
91     $ENV{'uid'} = "test1234";
92
93     return 1;
94 }
95
96 # Tests
97 ##############################################################
98
99 # Can module load
100 use C4::Auth_with_shibboleth;
101 require_ok('C4::Auth_with_shibboleth');
102 $C4::Auth_with_shibboleth::debug = '0';
103
104 # Subroutine tests
105 ## shib_ok
106 subtest "shib_ok tests" => sub {
107     plan tests => 5;
108     my $result;
109
110     # correct config, no debug
111     is( shib_ok(), '1', "good config" );
112
113     # bad config, no debug
114     $matchpoint = undef;
115     warnings_are { $result = shib_ok() }
116     [ { carped => 'shibboleth matchpoint not defined' }, ],
117       "undefined matchpoint = fatal config, warning given";
118     is( $result, '0', "bad config" );
119
120     $matchpoint = 'email';
121     warnings_are { $result = shib_ok() }
122     [ { carped => 'shibboleth matchpoint not mapped' }, ],
123       "unmapped matchpoint = fatal config, warning given";
124     is( $result, '0', "bad config" );
125
126     # add test for undefined shibboleth block
127
128     reset_config();
129 };
130
131 ## logout_shib
132 #my $query = CGI->new();
133 #is(logout_shib($query),"https://".$opac."/Shibboleth.sso/Logout?return="."https://".$opac,"logout_shib");
134
135 ## login_shib_url
136 my $query_string = 'language=en-GB';
137 $ENV{QUERY_STRING} = $query_string;
138 $ENV{SCRIPT_NAME}  = '/cgi-bin/koha/opac-user.pl';
139 my $query = CGI->new($query_string);
140 is(
141     login_shib_url($query),
142     'https://testopac.com'
143       . '/Shibboleth.sso/Login?target='
144       . 'https://testopac.com/cgi-bin/koha/opac-user.pl' . '%3F'
145       . $query_string,
146     "login shib url"
147 );
148
149 ## get_login_shib
150 subtest "get_login_shib tests" => sub {
151     plan tests => 4;
152     my $login;
153
154     # good config
155     ## debug off
156     $C4::Auth_with_shibboleth::debug = '0';
157     warnings_are { $login = get_login_shib() }[],
158       "good config with debug off, no warnings received";
159     is( $login, "test1234",
160         "good config with debug off, attribute value returned" );
161
162     ## debug on
163     $C4::Auth_with_shibboleth::debug = '1';
164     warnings_are { $login = get_login_shib() }[
165         "koha borrower field to match: userid",
166         "shibboleth attribute to match: uid",
167         "uid value: test1234"
168     ],
169       "good config with debug enabled, correct warnings received";
170     is( $login, "test1234",
171         "good config with debug enabled, attribute value returned" );
172
173 # bad config - with shib_ok implemented, we should never reach this sub with a bad config
174 };
175
176 ## checkpw_shib
177 subtest "checkpw_shib tests" => sub {
178     plan tests => 13;
179
180     my $shib_login;
181     my ( $retval, $retcard, $retuserid );
182
183     # Setup Mock Database Data
184     fixtures_ok [
185         'Borrower' => [
186             [qw/cardnumber userid surname address city/],
187             [qw/testcardnumber test1234 renvoize myaddress johnston/],
188         ],
189       ],
190       'Installed some custom fixtures via the Populate fixture class';
191
192     # debug off
193     $C4::Auth_with_shibboleth::debug = '0';
194
195     # good user
196     $shib_login = "test1234";
197     warnings_are {
198         ( $retval, $retcard, $retuserid ) = checkpw_shib( $shib_login );
199     }
200     [], "good user with no debug";
201     is( $retval,    "1",              "user authenticated" );
202     is( $retcard,   "testcardnumber", "expected cardnumber returned" );
203     is( $retuserid, "test1234",       "expected userid returned" );
204
205     # bad user
206     $shib_login = 'martin';
207     warnings_are {
208         ( $retval, $retcard, $retuserid ) = checkpw_shib( $shib_login );
209     }
210     [], "bad user with no debug";
211     is( $retval, "0", "user not authenticated" );
212
213     # debug on
214     $C4::Auth_with_shibboleth::debug = '1';
215
216     # good user
217     $shib_login = "test1234";
218     warnings_exist {
219         ( $retval, $retcard, $retuserid ) = checkpw_shib( $shib_login );
220     }
221     [ qr/checkpw_shib/, qr/koha borrower field to match: userid/,
222       qr/shibboleth attribute to match: uid/,
223       qr/User Shibboleth-authenticated as:/ ],
224       "good user with debug enabled";
225     is( $retval,    "1",              "user authenticated" );
226     is( $retcard,   "testcardnumber", "expected cardnumber returned" );
227     is( $retuserid, "test1234",       "expected userid returned" );
228
229     # bad user
230     $shib_login = "martin";
231     warnings_exist {
232         ( $retval, $retcard, $retuserid ) = checkpw_shib( $shib_login );
233     }
234     [
235         qr/checkpw_shib/,
236         qr/koha borrower field to match: userid/,
237         qr/shibboleth attribute to match: uid/,
238         qr/User Shibboleth-authenticated as:/,
239         qr/not a valid Koha user/
240     ],
241       "bad user with debug enabled";
242     is( $retval, "0", "user not authenticated" );
243
244 };
245
246 ## _get_uri
247 $OPACBaseURL = "testopac.com";
248 is( C4::Auth_with_shibboleth::_get_uri(),
249     "https://testopac.com", "https opac uri returned" );
250
251 $OPACBaseURL = "http://testopac.com";
252 my $result;
253 warning_like { $result = C4::Auth_with_shibboleth::_get_uri() }
254              [ qr/Shibboleth requires OPACBaseURL to use the https protocol!/ ],
255              "improper protocol - received expected warning";
256 is( $result, "https://testopac.com", "https opac uri returned" );
257
258 $OPACBaseURL = "https://testopac.com";
259 is( C4::Auth_with_shibboleth::_get_uri(),
260     "https://testopac.com", "https opac uri returned" );
261
262 $OPACBaseURL = undef;
263 warning_like { $result = C4::Auth_with_shibboleth::_get_uri() }
264              [ qr/OPACBaseURL not set!/ ],
265              "undefined OPACBaseURL - received expected warning";
266 is( $result, "https://", "https opac uri returned" );
267
268 ## _get_shib_config
269 # Internal helper function, covered in tests above