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>.
21 use Module::Load::Conditional qw/check_install/;
30 if ( check_install( module => 'Test::DBIx::Class' ) ) {
34 plan skip_all => "Need Test::DBIx::Class";
38 use Test::DBIx::Class {
39 schema_class => 'Koha::Schema',
40 connect_info => [ 'dbi:SQLite:dbname=:memory:', '', '' ]
44 my $matchpoint = 'userid';
48 'userid' => { 'is' => 'uid' },
49 'surname' => { 'is' => 'sn' },
50 'dateexpiry' => { 'is' => 'exp' },
51 'categorycode' => { 'is' => 'cat' },
52 'address' => { 'is' => 'add' },
53 'city' => { 'is' => 'city' },
55 $ENV{'uid'} = "test1234";
64 my $context = new Test::MockModule('C4::Context');
67 $context->mock( 'config', \&mockedConfig );
70 my $OPACBaseURL = "testopac.com";
71 my $staffClientBaseURL = "teststaff.com";
72 $context->mock( 'preference', \&mockedPref );
75 $context->mock( 'timezone', sub { return 'local'; } );
78 my $interface = 'opac';
79 $context->mock( 'interface', \&mockedInterface );
82 my $database = new Test::MockModule('Koha::Database');
85 $database->mock( 'schema', \&mockedSchema );
88 ##############################################################
91 use C4::Auth_with_shibboleth;
92 require_ok('C4::Auth_with_shibboleth');
93 $C4::Auth_with_shibboleth::debug = '0';
97 subtest "shib_ok tests" => sub {
101 # correct config, no debug
102 is( shib_ok(), '1', "good config" );
104 # bad config, no debug
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" );
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" );
117 # add test for undefined shibboleth block
123 #my $query = CGI->new();
124 #is(logout_shib($query),"https://".$opac."/Shibboleth.sso/Logout?return="."https://".$opac,"logout_shib");
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);
132 login_shib_url($query),
133 'https://testopac.com'
134 . '/Shibboleth.sso/Login?target='
135 . 'https://testopac.com/cgi-bin/koha/opac-user.pl?'
141 subtest "get_login_shib tests" => sub {
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" );
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"
160 "good config with debug enabled, correct warnings received";
161 is( $login, "test1234",
162 "good config with debug enabled, attribute value returned" );
164 # bad config - with shib_ok implemented, we should never reach this sub with a bad config
168 subtest "checkpw_shib tests" => sub {
172 my ( $retval, $retcard, $retuserid );
174 # Setup Mock Database Data
177 [qw/cardnumber userid surname address city/],
178 [qw/testcardnumber test1234 renvoize myaddress johnston/],
180 'Category' => [ [qw/categorycode default_privacy/], [qw/S never/], ]
182 'Installed some custom fixtures via the Populate fixture class';
185 $C4::Auth_with_shibboleth::debug = '0';
188 $shib_login = "test1234";
190 ( $retval, $retcard, $retuserid ) = checkpw_shib($shib_login);
192 [], "good user with no debug";
193 is( $retval, "1", "user authenticated" );
194 is( $retcard, "testcardnumber", "expected cardnumber returned" );
195 is( $retuserid, "test1234", "expected userid returned" );
198 $shib_login = 'martin';
200 ( $retval, $retcard, $retuserid ) = checkpw_shib($shib_login);
202 [], "bad user with no debug";
203 is( $retval, "0", "user not authenticated" );
207 $shib_login = 'test4321';
208 $ENV{'uid'} = 'test4321';
210 $ENV{'exp'} = "2017";
212 $ENV{'add'} = 'Address';
213 $ENV{'city'} = 'City';
215 ( $retval, $retcard, $retuserid ) = checkpw_shib($shib_login);
217 [], "new user added with no debug";
218 is( $retval, "1", "user authenticated" );
219 is( $retuserid, "test4321", "expected userid returned" );
220 ok my $new_user = ResultSet('Borrower')
221 ->search( { 'userid' => 'test4321' }, { rows => 1 } ), "new user found";
222 is_fields [qw/surname dateexpiry address city/], $new_user->next,
223 [qw/pika 2017 Address City/],
224 'Found $new_users surname';
229 $ENV{'city'} = 'AnotherCity';
231 ( $retval, $retcard, $retuserid ) = checkpw_shib($shib_login);
233 [], "good user with sync";
235 ok my $sync_user = ResultSet('Borrower')
236 ->search( { 'userid' => 'test4321' }, { rows => 1 } ), "sync user found";
238 is_fields [qw/surname dateexpiry address city/], $sync_user->next,
239 [qw/pika 2017 Address AnotherCity/],
240 'Found $sync_user synced city';
244 $C4::Auth_with_shibboleth::debug = '1';
247 $shib_login = "test1234";
249 ( $retval, $retcard, $retuserid ) = checkpw_shib($shib_login);
253 qr/koha borrower field to match: userid/,
254 qr/shibboleth attribute to match: uid/,
255 qr/User Shibboleth-authenticated as:/
257 "good user with debug enabled";
258 is( $retval, "1", "user authenticated" );
259 is( $retcard, "testcardnumber", "expected cardnumber returned" );
260 is( $retuserid, "test1234", "expected userid returned" );
263 $shib_login = "martin";
265 ( $retval, $retcard, $retuserid ) = checkpw_shib($shib_login);
269 qr/koha borrower field to match: userid/,
270 qr/shibboleth attribute to match: uid/,
271 qr/User Shibboleth-authenticated as:/,
272 qr/not a valid Koha user/
274 "bad user with debug enabled";
275 is( $retval, "0", "user not authenticated" );
280 $OPACBaseURL = "testopac.com";
281 is( C4::Auth_with_shibboleth::_get_uri(),
282 "https://testopac.com", "https opac uri returned" );
284 $OPACBaseURL = "http://testopac.com";
286 warnings_are { $result = C4::Auth_with_shibboleth::_get_uri() }[
287 "shibboleth interface: $interface",
288 "Shibboleth requires OPACBaseURL/staffClientBaseURL to use the https protocol!"
290 "improper protocol - received expected warning";
291 is( $result, "https://testopac.com", "https opac uri returned" );
293 $OPACBaseURL = "https://testopac.com";
294 is( C4::Auth_with_shibboleth::_get_uri(),
295 "https://testopac.com", "https opac uri returned" );
297 $OPACBaseURL = undef;
298 warnings_are { $result = C4::Auth_with_shibboleth::_get_uri() }
299 [ "shibboleth interface: $interface", "OPACBaseURL not set!" ],
300 "undefined OPACBaseURL - received expected warning";
301 is( $result, "https://", "https $interface uri returned" );
303 ## _get_uri - intranet
304 $interface = 'intranet';
305 $staffClientBaseURL = "teststaff.com";
306 is( C4::Auth_with_shibboleth::_get_uri(),
307 "https://teststaff.com", "https $interface uri returned" );
309 $staffClientBaseURL = "http://teststaff.com";
310 warnings_are { $result = C4::Auth_with_shibboleth::_get_uri() }[
311 "shibboleth interface: $interface",
312 "Shibboleth requires OPACBaseURL/staffClientBaseURL to use the https protocol!"
314 "improper protocol - received expected warning";
315 is( $result, "https://teststaff.com", "https $interface uri returned" );
317 $staffClientBaseURL = "https://teststaff.com";
318 is( C4::Auth_with_shibboleth::_get_uri(),
319 "https://teststaff.com", "https $interface uri returned" );
321 $staffClientBaseURL = undef;
322 warnings_are { $result = C4::Auth_with_shibboleth::_get_uri() }
323 [ "shibboleth interface: $interface", "staffClientBaseURL not set!" ],
324 "undefined staffClientBaseURL - received expected warning";
325 is( $result, "https://", "https $interface uri returned" );
328 # Internal helper function, covered in tests above
334 'autocreate' => $autocreate,
336 'matchpoint' => $matchpoint,
337 'mapping' => \%mapping
347 if ( $param eq 'OPACBaseURL' ) {
348 $return = $OPACBaseURL;
351 if ( $param eq 'staffClientBaseURL' ) {
352 $return = $staffClientBaseURL;
358 sub mockedInterface {
366 ## Convenience method to reset config
368 $matchpoint = 'userid';
372 'userid' => { 'is' => 'uid' },
373 'surname' => { 'is' => 'sn' },
374 'dateexpiry' => { 'is' => 'exp' },
375 'categorycode' => { 'is' => 'cat' },
376 'address' => { 'is' => 'add' },
377 'city' => { 'is' => 'city' },
379 $ENV{'uid'} = "test1234";
384 $ENV{'city'} = undef;