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';
47 'userid' => { 'is' => 'uid' },
48 'surname' => { 'is' => 'sn' },
49 'dateexpiry' => { 'is' => 'exp' },
50 'categorycode' => { 'is' => 'cat' },
51 'address' => { 'is' => 'add' },
52 'city' => { 'is' => 'city' },
54 $ENV{'uid'} = "test1234";
63 my $context = new Test
::MockModule
('C4::Context');
66 $context->mock( 'config', \
&mockedConfig
);
69 my $OPACBaseURL = "testopac.com";
70 my $staffClientBaseURL = "teststaff.com";
71 $context->mock( 'preference', \
&mockedPref
);
74 $context->mock( 'timezone', sub { return 'local'; } );
77 my $interface = 'opac';
78 $context->mock( 'interface', \
&mockedInterface
);
81 my $database = new Test
::MockModule
('Koha::Database');
84 $database->mock( 'schema', \
&mockedSchema
);
87 ##############################################################
90 use C4
::Auth_with_shibboleth
;
91 require_ok
('C4::Auth_with_shibboleth');
92 $C4::Auth_with_shibboleth
::debug
= '0';
96 subtest
"shib_ok tests" => sub {
100 # correct config, no debug
101 is
( shib_ok
(), '1', "good config" );
103 # bad config, no debug
105 warnings_are
{ $result = shib_ok
() }
106 [ { carped
=> 'shibboleth matchpoint not defined' }, ],
107 "undefined matchpoint = fatal config, warning given";
108 is
( $result, '0', "bad config" );
110 $matchpoint = 'email';
111 warnings_are
{ $result = shib_ok
() }
112 [ { carped
=> 'shibboleth matchpoint not mapped' }, ],
113 "unmapped matchpoint = fatal config, warning given";
114 is
( $result, '0', "bad config" );
116 # add test for undefined shibboleth block
122 #my $query = CGI->new();
123 #is(logout_shib($query),"https://".$opac."/Shibboleth.sso/Logout?return="."https://".$opac,"logout_shib");
126 my $query_string = 'language=en-GB';
127 $ENV{QUERY_STRING
} = $query_string;
128 $ENV{SCRIPT_NAME
} = '/cgi-bin/koha/opac-user.pl';
129 my $query = CGI
->new($query_string);
131 login_shib_url
($query),
132 'https://testopac.com'
133 . '/Shibboleth.sso/Login?target='
134 . 'https://testopac.com/cgi-bin/koha/opac-user.pl' . '%3F'
140 subtest
"get_login_shib tests" => sub {
146 $C4::Auth_with_shibboleth
::debug
= '0';
147 warnings_are
{ $login = get_login_shib
() }[],
148 "good config with debug off, no warnings received";
149 is
( $login, "test1234",
150 "good config with debug off, attribute value returned" );
153 $C4::Auth_with_shibboleth
::debug
= '1';
154 warnings_are
{ $login = get_login_shib
() }[
155 "koha borrower field to match: userid",
156 "shibboleth attribute to match: uid",
157 "uid value: test1234"
159 "good config with debug enabled, correct warnings received";
160 is
( $login, "test1234",
161 "good config with debug enabled, attribute value returned" );
163 # bad config - with shib_ok implemented, we should never reach this sub with a bad config
167 subtest
"checkpw_shib tests" => sub {
171 my ( $retval, $retcard, $retuserid );
173 # Setup Mock Database Data
176 [qw
/cardnumber userid surname address city/],
177 [qw
/testcardnumber test1234 renvoize myaddress johnston/],
179 'Category' => [ [qw
/categorycode default_privacy/], [qw
/S never/], ]
181 'Installed some custom fixtures via the Populate fixture class';
184 $C4::Auth_with_shibboleth
::debug
= '0';
187 $shib_login = "test1234";
189 ( $retval, $retcard, $retuserid ) = checkpw_shib
($shib_login);
191 [], "good user with no debug";
192 is
( $retval, "1", "user authenticated" );
193 is
( $retcard, "testcardnumber", "expected cardnumber returned" );
194 is
( $retuserid, "test1234", "expected userid returned" );
197 $shib_login = 'martin';
199 ( $retval, $retcard, $retuserid ) = checkpw_shib
($shib_login);
201 [], "bad user with no debug";
202 is
( $retval, "0", "user not authenticated" );
206 $shib_login = 'test4321';
207 $ENV{'uid'} = 'test4321';
209 $ENV{'exp'} = "2017";
211 $ENV{'add'} = 'Address';
212 $ENV{'city'} = 'City';
214 ( $retval, $retcard, $retuserid ) = checkpw_shib
($shib_login);
216 [], "new user added with no debug";
217 is
( $retval, "1", "user authenticated" );
218 is
( $retuserid, "test4321", "expected userid returned" );
219 ok
my $new_user = ResultSet
('Borrower')
220 ->search( { 'userid' => 'test4321' }, { rows
=> 1 } ), "new user found";
221 is_fields
[qw
/surname dateexpiry address city/], $new_user->next,
222 [qw
/pika 2017 Address City/],
223 'Found $new_users surname';
227 $C4::Auth_with_shibboleth
::debug
= '1';
230 $shib_login = "test1234";
232 ( $retval, $retcard, $retuserid ) = checkpw_shib
($shib_login);
236 qr/koha borrower field to match: userid/,
237 qr/shibboleth attribute to match: uid/,
238 qr/User Shibboleth-authenticated as:/
240 "good user with debug enabled";
241 is
( $retval, "1", "user authenticated" );
242 is
( $retcard, "testcardnumber", "expected cardnumber returned" );
243 is
( $retuserid, "test1234", "expected userid returned" );
246 $shib_login = "martin";
248 ( $retval, $retcard, $retuserid ) = checkpw_shib
($shib_login);
252 qr/koha borrower field to match: userid/,
253 qr/shibboleth attribute to match: uid/,
254 qr/User Shibboleth-authenticated as:/,
255 qr/not a valid Koha user/
257 "bad user with debug enabled";
258 is
( $retval, "0", "user not authenticated" );
263 $OPACBaseURL = "testopac.com";
264 is
( C4
::Auth_with_shibboleth
::_get_uri
(),
265 "https://testopac.com", "https opac uri returned" );
267 $OPACBaseURL = "http://testopac.com";
269 warnings_are
{ $result = C4
::Auth_with_shibboleth
::_get_uri
() }[
270 "shibboleth interface: $interface",
271 "Shibboleth requires OPACBaseURL/staffClientBaseURL to use the https protocol!"
273 "improper protocol - received expected warning";
274 is
( $result, "https://testopac.com", "https opac uri returned" );
276 $OPACBaseURL = "https://testopac.com";
277 is
( C4
::Auth_with_shibboleth
::_get_uri
(),
278 "https://testopac.com", "https opac uri returned" );
280 $OPACBaseURL = undef;
281 warnings_are
{ $result = C4
::Auth_with_shibboleth
::_get_uri
() }
282 [ "shibboleth interface: $interface", "OPACBaseURL not set!" ],
283 "undefined OPACBaseURL - received expected warning";
284 is
( $result, "https://", "https $interface uri returned" );
286 ## _get_uri - intranet
287 $interface = 'intranet';
288 $staffClientBaseURL = "teststaff.com";
289 is
( C4
::Auth_with_shibboleth
::_get_uri
(),
290 "https://teststaff.com", "https $interface uri returned" );
292 $staffClientBaseURL = "http://teststaff.com";
293 warnings_are
{ $result = C4
::Auth_with_shibboleth
::_get_uri
() }[
294 "shibboleth interface: $interface",
295 "Shibboleth requires OPACBaseURL/staffClientBaseURL to use the https protocol!"
297 "improper protocol - received expected warning";
298 is
( $result, "https://teststaff.com", "https $interface uri returned" );
300 $staffClientBaseURL = "https://teststaff.com";
301 is
( C4
::Auth_with_shibboleth
::_get_uri
(),
302 "https://teststaff.com", "https $interface uri returned" );
304 $staffClientBaseURL = undef;
305 warnings_are
{ $result = C4
::Auth_with_shibboleth
::_get_uri
() }
306 [ "shibboleth interface: $interface", "staffClientBaseURL not set!" ],
307 "undefined staffClientBaseURL - received expected warning";
308 is
( $result, "https://", "https $interface uri returned" );
311 # Internal helper function, covered in tests above
317 'autocreate' => $autocreate,
318 'matchpoint' => $matchpoint,
319 'mapping' => \
%mapping
329 if ( $param eq 'OPACBaseURL' ) {
330 $return = $OPACBaseURL;
333 if ( $param eq 'staffClientBaseURL' ) {
334 $return = $staffClientBaseURL;
340 sub mockedInterface
{
348 ## Convenience method to reset config
350 $matchpoint = 'userid';
353 'userid' => { 'is' => 'uid' },
354 'surname' => { 'is' => 'sn' },
355 'dateexpiry' => { 'is' => 'exp' },
356 'categorycode' => { 'is' => 'cat' },
357 'address' => { 'is' => 'add' },
358 'city' => { 'is' => 'city' },
360 $ENV{'uid'} = "test1234";
365 $ENV{'city'} = undef;