Bug 24065: Fail shib login if multiple users matched
[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 => 17;
32     }
33     else {
34         plan skip_all => "Need Test::DBIx::Class";
35     }
36 }
37
38 use Test::DBIx::Class {
39     schema_class => 'Koha::Schema',
40     connect_info => [ 'dbi:SQLite:dbname=:memory:', '', '' ]
41 };
42
43 # Mock Variables
44 my $matchpoint = 'userid';
45 my $autocreate = 0;
46 my $sync = 0;
47 my %mapping    = (
48     'userid'       => { 'is' => 'uid' },
49     'surname'      => { 'is' => 'sn' },
50     'dateexpiry'   => { 'is' => 'exp' },
51     'categorycode' => { 'is' => 'cat' },
52     'address'      => { 'is' => 'add' },
53     'city'         => { 'is' => 'city' },
54 );
55 $ENV{'uid'}  = "test1234";
56 $ENV{'sn'}   = undef;
57 $ENV{'exp'}  = undef;
58 $ENV{'cat'}  = undef;
59 $ENV{'add'}  = undef;
60 $ENV{'city'} = undef;
61
62 # Setup Mocks
63 ## Mock Context
64 my $context = new Test::MockModule('C4::Context');
65
66 ### Mock ->config
67 $context->mock( 'config', \&mockedConfig );
68
69 ### Mock ->preference
70 my $OPACBaseURL = "testopac.com";
71 my $staffClientBaseURL = "teststaff.com";
72 $context->mock( 'preference', \&mockedPref );
73
74 ### Mock ->tz
75 $context->mock( 'timezone', sub { return 'local'; } );
76
77 ### Mock ->interface
78 my $interface = 'opac';
79 $context->mock( 'interface', \&mockedInterface );
80
81 ## Mock Database
82 my $database = new Test::MockModule('Koha::Database');
83
84 ### Mock ->schema
85 $database->mock( 'schema', \&mockedSchema );
86
87 # Tests
88 ##############################################################
89
90 # Can module load
91 use C4::Auth_with_shibboleth;
92 require_ok('C4::Auth_with_shibboleth');
93 $C4::Auth_with_shibboleth::debug = '0';
94
95 # Subroutine tests
96 ## shib_ok
97 subtest "shib_ok tests" => sub {
98     plan tests => 5;
99     my $result;
100
101     # correct config, no debug
102     is( shib_ok(), '1', "good config" );
103
104     # bad config, no debug
105     $matchpoint = undef;
106     warnings_are { $result = shib_ok() }
107     [ { carped => 'shibboleth matchpoint not defined' }, ],
108       "undefined matchpoint = fatal config, warning given";
109     is( $result, '0', "bad config" );
110
111     $matchpoint = 'email';
112     warnings_are { $result = shib_ok() }
113     [ { carped => 'shibboleth matchpoint not mapped' }, ],
114       "unmapped matchpoint = fatal config, warning given";
115     is( $result, '0', "bad config" );
116
117     # add test for undefined shibboleth block
118
119     reset_config();
120 };
121
122 ## logout_shib
123 #my $query = CGI->new();
124 #is(logout_shib($query),"https://".$opac."/Shibboleth.sso/Logout?return="."https://".$opac,"logout_shib");
125
126 ## login_shib_url
127 my $query_string = 'language=en-GB';
128 $ENV{QUERY_STRING} = $query_string;
129 $ENV{SCRIPT_NAME}  = '/cgi-bin/koha/opac-user.pl';
130 my $query = CGI->new($query_string);
131 is(
132     login_shib_url($query),
133     'https://testopac.com'
134       . '/Shibboleth.sso/Login?target='
135       . 'https://testopac.com/cgi-bin/koha/opac-user.pl?'
136       . $query_string,
137     "login shib url"
138 );
139
140 ## get_login_shib
141 subtest "get_login_shib tests" => sub {
142     plan tests => 4;
143     my $login;
144
145     # good config
146     ## debug off
147     $C4::Auth_with_shibboleth::debug = '0';
148     warnings_are { $login = get_login_shib() }[],
149       "good config with debug off, no warnings received";
150     is( $login, "test1234",
151         "good config with debug off, attribute value returned" );
152
153     ## debug on
154     $C4::Auth_with_shibboleth::debug = '1';
155     warnings_are { $login = get_login_shib() }[
156         "koha borrower field to match: userid",
157         "shibboleth attribute to match: uid",
158         "uid value: test1234"
159     ],
160       "good config with debug enabled, correct warnings received";
161     is( $login, "test1234",
162         "good config with debug enabled, attribute value returned" );
163
164 # bad config - with shib_ok implemented, we should never reach this sub with a bad config
165 };
166
167 ## checkpw_shib
168 subtest "checkpw_shib tests" => sub {
169     plan tests => 24;
170
171     my $shib_login;
172     my ( $retval, $retcard, $retuserid );
173
174     # Setup Mock Database Data
175     fixtures_ok [
176         'Borrower' => [
177             [qw/cardnumber userid surname address city email/],
178             [qw/testcardnumber test1234 renvoize myaddress johnston  /],
179             [qw/testcardnumber1 test12345 clamp1 myaddress quechee kid@clamp.io/],
180             [qw/testcardnumber2 test123456 clamp2 myaddress quechee kid@clamp.io/],
181         ],
182         'Category' => [ [qw/categorycode default_privacy/], [qw/S never/], ]
183       ],
184       'Installed some custom fixtures via the Populate fixture class';
185
186     # debug off
187     $C4::Auth_with_shibboleth::debug = '0';
188
189     # good user
190     $shib_login = "test1234";
191     warnings_are {
192         ( $retval, $retcard, $retuserid ) = checkpw_shib($shib_login);
193     }
194     [], "good user with no debug";
195     is( $retval,    "1",              "user authenticated" );
196     is( $retcard,   "testcardnumber", "expected cardnumber returned" );
197     is( $retuserid, "test1234",       "expected userid returned" );
198
199     # bad user
200     $shib_login = 'martin';
201     warnings_are {
202         ( $retval, $retcard, $retuserid ) = checkpw_shib($shib_login);
203     }
204     [], "bad user with no debug";
205     is( $retval, "0", "user not authenticated" );
206
207     # duplicated matchpoint
208     $matchpoint = 'email';
209     $mapping{'email'} = { is => 'email' };
210     $shib_login = 'kid@clamp.io';
211     warnings_are {
212         ( $retval, $retcard, $retuserid ) = checkpw_shib($shib_login);
213     }
214     [], "bad user with no debug";
215     is( $retval, "0", "user not authenticated if duplicated matchpoint" );
216     $C4::Auth_with_shibboleth::debug = '1';
217     warnings_are {
218         ( $retval, $retcard, $retuserid ) = checkpw_shib($shib_login);
219     }
220     [
221         q/checkpw_shib/,
222         q/koha borrower field to match: email/,
223         q/shibboleth attribute to match: email/,
224         q/User Shibboleth-authenticated as: kid@clamp.io/,
225         q/There are several users with email of kid@clamp.io, matchpoints must be unique/
226     ], "duplicated matchpoint warned with debug";
227     $C4::Auth_with_shibboleth::debug = '0';
228     reset_config();
229
230     # autocreate user
231     $autocreate  = 1;
232     $shib_login  = 'test4321';
233     $ENV{'uid'}  = 'test4321';
234     $ENV{'sn'}   = "pika";
235     $ENV{'exp'}  = "2017";
236     $ENV{'cat'}  = "S";
237     $ENV{'add'}  = 'Address';
238     $ENV{'city'} = 'City';
239     warnings_are {
240         ( $retval, $retcard, $retuserid ) = checkpw_shib($shib_login);
241     }
242     [], "new user added with no debug";
243     is( $retval,    "1",        "user authenticated" );
244     is( $retuserid, "test4321", "expected userid returned" );
245     ok my $new_user = ResultSet('Borrower')
246       ->search( { 'userid' => 'test4321' }, { rows => 1 } ), "new user found";
247     is_fields [qw/surname dateexpiry address city/], $new_user->next,
248       [qw/pika 2017 Address City/],
249       'Found $new_users surname';
250     $autocreate = 0;
251
252     # sync user
253     $sync = 1;
254     $ENV{'city'} = 'AnotherCity';
255     warnings_are {
256         ( $retval, $retcard, $retuserid ) = checkpw_shib($shib_login);
257     }
258     [], "good user with sync";
259
260     ok my $sync_user = ResultSet('Borrower')
261       ->search( { 'userid' => 'test4321' }, { rows => 1 } ), "sync user found";
262
263     is_fields [qw/surname dateexpiry address city/], $sync_user->next,
264       [qw/pika 2017 Address AnotherCity/],
265       'Found $sync_user synced city';
266     $sync = 0;
267
268     # debug on
269     $C4::Auth_with_shibboleth::debug = '1';
270
271     # good user
272     $shib_login = "test1234";
273     warnings_exist {
274         ( $retval, $retcard, $retuserid ) = checkpw_shib($shib_login);
275     }
276     [
277         qr/checkpw_shib/,
278         qr/koha borrower field to match: userid/,
279         qr/shibboleth attribute to match: uid/,
280         qr/User Shibboleth-authenticated as:/
281     ],
282       "good user with debug enabled";
283     is( $retval,    "1",              "user authenticated" );
284     is( $retcard,   "testcardnumber", "expected cardnumber returned" );
285     is( $retuserid, "test1234",       "expected userid returned" );
286
287     # bad user
288     $shib_login = "martin";
289     warnings_exist {
290         ( $retval, $retcard, $retuserid ) = checkpw_shib($shib_login);
291     }
292     [
293         qr/checkpw_shib/,
294         qr/koha borrower field to match: userid/,
295         qr/shibboleth attribute to match: uid/,
296         qr/User Shibboleth-authenticated as:/,
297         qr/not a valid Koha user/
298     ],
299       "bad user with debug enabled";
300     is( $retval, "0", "user not authenticated" );
301
302 };
303
304 ## _get_uri - opac
305 $OPACBaseURL = "testopac.com";
306 is( C4::Auth_with_shibboleth::_get_uri(),
307     "https://testopac.com", "https opac uri returned" );
308
309 $OPACBaseURL = "http://testopac.com";
310 my $result;
311 warnings_are { $result = C4::Auth_with_shibboleth::_get_uri() }[
312     "shibboleth interface: $interface",
313 "Shibboleth requires OPACBaseURL/staffClientBaseURL to use the https protocol!"
314 ],
315   "improper protocol - received expected warning";
316 is( $result, "https://testopac.com", "https opac uri returned" );
317
318 $OPACBaseURL = "https://testopac.com";
319 is( C4::Auth_with_shibboleth::_get_uri(),
320     "https://testopac.com", "https opac uri returned" );
321
322 $OPACBaseURL = undef;
323 warnings_are { $result = C4::Auth_with_shibboleth::_get_uri() }
324 [ "shibboleth interface: $interface", "OPACBaseURL not set!" ],
325   "undefined OPACBaseURL - received expected warning";
326 is( $result, "https://", "https $interface uri returned" );
327
328 ## _get_uri - intranet
329 $interface = 'intranet';
330 $staffClientBaseURL = "teststaff.com";
331 is( C4::Auth_with_shibboleth::_get_uri(),
332     "https://teststaff.com", "https $interface uri returned" );
333
334 $staffClientBaseURL = "http://teststaff.com";
335 warnings_are { $result = C4::Auth_with_shibboleth::_get_uri() }[
336     "shibboleth interface: $interface",
337 "Shibboleth requires OPACBaseURL/staffClientBaseURL to use the https protocol!"
338 ],
339   "improper protocol - received expected warning";
340 is( $result, "https://teststaff.com", "https $interface uri returned" );
341
342 $staffClientBaseURL = "https://teststaff.com";
343 is( C4::Auth_with_shibboleth::_get_uri(),
344     "https://teststaff.com", "https $interface uri returned" );
345
346 $staffClientBaseURL = undef;
347 warnings_are { $result = C4::Auth_with_shibboleth::_get_uri() }
348 [ "shibboleth interface: $interface", "staffClientBaseURL not set!" ],
349   "undefined staffClientBaseURL - received expected warning";
350 is( $result, "https://", "https $interface uri returned" );
351
352 ## _get_shib_config
353 # Internal helper function, covered in tests above
354
355 sub mockedConfig {
356     my $param = shift;
357
358     my %shibboleth = (
359         'autocreate' => $autocreate,
360         'sync'       => $sync,
361         'matchpoint' => $matchpoint,
362         'mapping'    => \%mapping
363     );
364
365     return \%shibboleth;
366 }
367
368 sub mockedPref {
369     my $param = $_[1];
370     my $return;
371
372     if ( $param eq 'OPACBaseURL' ) {
373         $return = $OPACBaseURL;
374     }
375
376     if ( $param eq 'staffClientBaseURL' ) {
377         $return = $staffClientBaseURL;
378     }
379
380     return $return;
381 }
382
383 sub mockedInterface {
384     return $interface;
385 }
386
387 sub mockedSchema {
388     return Schema();
389 }
390
391 ## Convenience method to reset config
392 sub reset_config {
393     $matchpoint = 'userid';
394     $autocreate = 0;
395     $sync = 0;
396     %mapping    = (
397         'userid'       => { 'is' => 'uid' },
398         'surname'      => { 'is' => 'sn' },
399         'dateexpiry'   => { 'is' => 'exp' },
400         'categorycode' => { 'is' => 'cat' },
401         'address'      => { 'is' => 'add' },
402         'city'         => { 'is' => 'city' },
403     );
404     $ENV{'uid'}  = "test1234";
405     $ENV{'sn'}   = undef;
406     $ENV{'exp'}  = undef;
407     $ENV{'cat'}  = undef;
408     $ENV{'add'}  = undef;
409     $ENV{'city'} = undef;
410
411     return 1;
412 }
413