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' ) ) {
33 plan skip_all
=> "Need Test::DBIx::Class"
37 use Test
::DBIx
::Class
;
40 my $matchpoint = 'userid';
43 'userid' => { 'is' => 'uid' },
44 'surname' => { 'is' => 'sn' },
45 'dateexpiry' => { 'is' => 'exp' },
46 'categorycode' => { 'is' => 'cat' },
47 'address' => { 'is' => 'add' },
48 'city' => { 'is' => 'city' },
50 $ENV{'uid'} = "test1234";
59 my $context = new Test
::MockModule
('C4::Context');
62 $context->mock( 'config', \
&mockedConfig
);
65 my $OPACBaseURL = "testopac.com";
66 $context->mock( 'preference', \
&mockedPref
);
69 $context->mock( 'timezone', sub { return 'local'; } );
72 my $database = new Test
::MockModule
('Koha::Database');
75 $database->mock( 'schema', \
&mockedSchema
);
78 ##############################################################
81 use C4
::Auth_with_shibboleth
;
82 require_ok
('C4::Auth_with_shibboleth');
83 $C4::Auth_with_shibboleth
::debug
= '0';
87 subtest
"shib_ok tests" => sub {
91 # correct config, no debug
92 is
( shib_ok
(), '1', "good config" );
94 # bad config, no debug
96 warnings_are
{ $result = shib_ok
() }
97 [ { carped
=> 'shibboleth matchpoint not defined' }, ],
98 "undefined matchpoint = fatal config, warning given";
99 is
( $result, '0', "bad config" );
101 $matchpoint = 'email';
102 warnings_are
{ $result = shib_ok
() }
103 [ { carped
=> 'shibboleth matchpoint not mapped' }, ],
104 "unmapped matchpoint = fatal config, warning given";
105 is
( $result, '0', "bad config" );
107 # add test for undefined shibboleth block
113 #my $query = CGI->new();
114 #is(logout_shib($query),"https://".$opac."/Shibboleth.sso/Logout?return="."https://".$opac,"logout_shib");
117 my $query_string = 'language=en-GB';
118 $ENV{QUERY_STRING
} = $query_string;
119 $ENV{SCRIPT_NAME
} = '/cgi-bin/koha/opac-user.pl';
120 my $query = CGI
->new($query_string);
122 login_shib_url
($query),
123 'https://testopac.com'
124 . '/Shibboleth.sso/Login?target='
125 . 'https://testopac.com/cgi-bin/koha/opac-user.pl' . '%3F'
131 subtest
"get_login_shib tests" => sub {
137 $C4::Auth_with_shibboleth
::debug
= '0';
138 warnings_are
{ $login = get_login_shib
() }[],
139 "good config with debug off, no warnings received";
140 is
( $login, "test1234",
141 "good config with debug off, attribute value returned" );
144 $C4::Auth_with_shibboleth
::debug
= '1';
145 warnings_are
{ $login = get_login_shib
() }[
146 "koha borrower field to match: userid",
147 "shibboleth attribute to match: uid",
148 "uid value: test1234"
150 "good config with debug enabled, correct warnings received";
151 is
( $login, "test1234",
152 "good config with debug enabled, attribute value returned" );
154 # bad config - with shib_ok implemented, we should never reach this sub with a bad config
158 subtest
"checkpw_shib tests" => sub {
162 my ( $retval, $retcard, $retuserid );
164 # Setup Mock Database Data
167 [qw
/cardnumber userid surname address city/],
168 [qw
/testcardnumber test1234 renvoize myaddress johnston/],
170 'Category' => [ [qw
/categorycode default_privacy/], [qw
/S never/], ]
172 'Installed some custom fixtures via the Populate fixture class';
175 $C4::Auth_with_shibboleth
::debug
= '0';
178 $shib_login = "test1234";
180 ( $retval, $retcard, $retuserid ) = checkpw_shib
($shib_login);
182 [], "good user with no debug";
183 is
( $retval, "1", "user authenticated" );
184 is
( $retcard, "testcardnumber", "expected cardnumber returned" );
185 is
( $retuserid, "test1234", "expected userid returned" );
188 $shib_login = 'martin';
190 ( $retval, $retcard, $retuserid ) = checkpw_shib
($shib_login);
192 [], "bad user with no debug";
193 is
( $retval, "0", "user not authenticated" );
197 $shib_login = 'test4321';
198 $ENV{'uid'} = 'test4321';
200 $ENV{'exp'} = "2017";
202 $ENV{'add'} = 'Address';
203 $ENV{'city'} = 'City';
205 ( $retval, $retcard, $retuserid ) = checkpw_shib
($shib_login);
207 [], "new user added with no debug";
208 is
( $retval, "1", "user authenticated" );
209 is
( $retuserid, "test4321", "expected userid returned" );
210 ok
my $new_user = ResultSet
('Borrower')
211 ->search( { 'userid' => 'test4321' }, { rows
=> 1 } ), "new user found";
212 is_fields
[qw
/surname dateexpiry address city/], $new_user->next,
213 [qw
/pika 2017 Address City/],
214 'Found $new_users surname';
218 $C4::Auth_with_shibboleth
::debug
= '1';
221 $shib_login = "test1234";
223 ( $retval, $retcard, $retuserid ) = checkpw_shib
($shib_login);
227 qr/koha borrower field to match: userid/,
228 qr/shibboleth attribute to match: uid/,
229 qr/User Shibboleth-authenticated as:/
231 "good user with debug enabled";
232 is
( $retval, "1", "user authenticated" );
233 is
( $retcard, "testcardnumber", "expected cardnumber returned" );
234 is
( $retuserid, "test1234", "expected userid returned" );
237 $shib_login = "martin";
239 ( $retval, $retcard, $retuserid ) = checkpw_shib
($shib_login);
243 qr/koha borrower field to match: userid/,
244 qr/shibboleth attribute to match: uid/,
245 qr/User Shibboleth-authenticated as:/,
246 qr/not a valid Koha user/
248 "bad user with debug enabled";
249 is
( $retval, "0", "user not authenticated" );
254 $OPACBaseURL = "testopac.com";
255 is
( C4
::Auth_with_shibboleth
::_get_uri
(),
256 "https://testopac.com", "https opac uri returned" );
258 $OPACBaseURL = "http://testopac.com";
260 warning_like
{ $result = C4
::Auth_with_shibboleth
::_get_uri
() }
261 [qr/Shibboleth requires OPACBaseURL to use the https protocol!/],
262 "improper protocol - received expected warning";
263 is
( $result, "https://testopac.com", "https opac uri returned" );
265 $OPACBaseURL = "https://testopac.com";
266 is
( C4
::Auth_with_shibboleth
::_get_uri
(),
267 "https://testopac.com", "https opac uri returned" );
269 $OPACBaseURL = undef;
270 warning_like
{ $result = C4
::Auth_with_shibboleth
::_get_uri
() }
271 [qr/OPACBaseURL not set!/],
272 "undefined OPACBaseURL - received expected warning";
273 is
( $result, "https://", "https opac uri returned" );
276 # Internal helper function, covered in tests above
282 'autocreate' => $autocreate,
283 'matchpoint' => $matchpoint,
284 'mapping' => \
%mapping
294 if ( $param eq 'OPACBaseURL' ) {
295 $return = $OPACBaseURL;
305 ## Convenience method to reset config
307 $matchpoint = 'userid';
310 'userid' => { 'is' => 'uid' },
311 'surname' => { 'is' => 'sn' },
312 'dateexpiry' => { 'is' => 'exp' },
313 'categorycode' => { 'is' => 'cat' },
314 'address' => { 'is' => 'add' },
315 'city' => { 'is' => 'city' },
317 $ENV{'uid'} = "test1234";
322 $ENV{'city'} = undef;