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
{ schema_class
=> 'Koha::Schema', connect_info
=> ['dbi:SQLite:dbname=:memory:','',''] };
40 my $matchpoint = 'userid';
41 my %mapping = ( 'userid' => { 'is' => 'uid' }, );
42 $ENV{'uid'} = "test1234";
46 my $context = new Test
::MockModule
('C4::Context');
49 $context->mock( 'config', \
&mockedConfig
);
55 'matchpoint' => $matchpoint,
56 'mapping' => \
%mapping
63 my $OPACBaseURL = "testopac.com";
64 $context->mock( 'preference', \
&mockedPref
);
70 if ( $param eq 'OPACBaseURL' ) {
71 $return = $OPACBaseURL;
78 my $database = new Test
::MockModule
('Koha::Database');
81 $database->mock( 'schema', \
&mockedSchema
);
87 ## Convenience method to reset config
89 $matchpoint = 'userid';
90 %mapping = ( 'userid' => { 'is' => 'uid' }, );
91 $ENV{'uid'} = "test1234";
97 ##############################################################
100 use_ok
('C4::Auth_with_shibboleth');
101 $C4::Auth_with_shibboleth
::debug
= '0';
105 subtest
"shib_ok tests" => sub {
109 # correct config, no debug
110 is
( shib_ok
(), '1', "good config" );
112 # bad config, no debug
114 warnings_are
{ $result = shib_ok
() }
115 [ { carped
=> 'shibboleth matchpoint not defined' }, ],
116 "undefined matchpoint = fatal config, warning given";
117 is
( $result, '0', "bad config" );
119 $matchpoint = 'email';
120 warnings_are
{ $result = shib_ok
() }
121 [ { carped
=> 'shibboleth matchpoint not mapped' }, ],
122 "unmapped matchpoint = fatal config, warning given";
123 is
( $result, '0', "bad config" );
125 # add test for undefined shibboleth block
131 #my $query = CGI->new();
132 #is(logout_shib($query),"https://".$opac."/Shibboleth.sso/Logout?return="."https://".$opac,"logout_shib");
135 my $query_string = 'language=en-GB';
136 $ENV{QUERY_STRING
} = $query_string;
137 $ENV{SCRIPT_NAME
} = '/cgi-bin/koha/opac-user.pl';
138 my $query = CGI
->new($query_string);
140 login_shib_url
($query),
141 'https://testopac.com'
142 . '/Shibboleth.sso/Login?target='
143 . 'https://testopac.com/cgi-bin/koha/opac-user.pl' . '%3F'
149 subtest
"get_login_shib tests" => sub {
155 $C4::Auth_with_shibboleth
::debug
= '0';
156 warnings_are
{ $login = get_login_shib
() }[],
157 "good config with debug off, no warnings received";
158 is
( $login, "test1234",
159 "good config with debug off, attribute value returned" );
162 $C4::Auth_with_shibboleth
::debug
= '1';
163 warnings_are
{ $login = get_login_shib
() }[
164 "koha borrower field to match: userid",
165 "shibboleth attribute to match: uid",
166 "uid value: test1234"
168 "good config with debug enabled, correct warnings received";
169 is
( $login, "test1234",
170 "good config with debug enabled, attribute value returned" );
172 # bad config - with shib_ok implemented, we should never reach this sub with a bad config
176 subtest
"checkpw_shib tests" => sub {
180 my ( $retval, $retcard, $retuserid );
182 # Setup Mock Database Data
185 [qw
/cardnumber userid surname address city/],
186 [qw
/testcardnumber test1234 renvoize myaddress johnston/],
189 'Installed some custom fixtures via the Populate fixture class';
192 $C4::Auth_with_shibboleth
::debug
= '0';
195 $shib_login = "test1234";
197 ( $retval, $retcard, $retuserid ) = checkpw_shib
( $shib_login );
199 [], "good user with no debug";
200 is
( $retval, "1", "user authenticated" );
201 is
( $retcard, "testcardnumber", "expected cardnumber returned" );
202 is
( $retuserid, "test1234", "expected userid returned" );
205 $shib_login = 'martin';
207 ( $retval, $retcard, $retuserid ) = checkpw_shib
( $shib_login );
209 [], "bad user with no debug";
210 is
( $retval, "0", "user not authenticated" );
213 $C4::Auth_with_shibboleth
::debug
= '1';
216 $shib_login = "test1234";
218 ( $retval, $retcard, $retuserid ) = checkpw_shib
( $shib_login );
220 [ qr/checkpw_shib/, qr/koha borrower field to match: userid/,
221 qr/shibboleth attribute to match: uid/,
222 qr/User Shibboleth-authenticated as:/ ],
223 "good user with debug enabled";
224 is
( $retval, "1", "user authenticated" );
225 is
( $retcard, "testcardnumber", "expected cardnumber returned" );
226 is
( $retuserid, "test1234", "expected userid returned" );
229 $shib_login = "martin";
231 ( $retval, $retcard, $retuserid ) = checkpw_shib
( $shib_login );
235 qr/koha borrower field to match: userid/,
236 qr/shibboleth attribute to match: uid/,
237 qr/User Shibboleth-authenticated as:/,
238 qr/not a valid Koha user/
240 "bad user with debug enabled";
241 is
( $retval, "0", "user not authenticated" );
246 $OPACBaseURL = "testopac.com";
247 is
( C4
::Auth_with_shibboleth
::_get_uri
(),
248 "https://testopac.com", "https opac uri returned" );
250 $OPACBaseURL = "http://testopac.com";
252 warning_like
{ $result = C4
::Auth_with_shibboleth
::_get_uri
() }
253 [ qr/Shibboleth requires OPACBaseURL to use the https protocol!/ ],
254 "improper protocol - received expected warning";
255 is
( $result, "https://testopac.com", "https opac uri returned" );
257 $OPACBaseURL = "https://testopac.com";
258 is
( C4
::Auth_with_shibboleth
::_get_uri
(),
259 "https://testopac.com", "https opac uri returned" );
261 $OPACBaseURL = undef;
262 warning_like
{ $result = C4
::Auth_with_shibboleth
::_get_uri
() }
263 [ qr/OPACBaseURL not set!/ ],
264 "undefined OPACBaseURL - received expected warning";
265 is
( $result, "https://", "https opac uri returned" );
268 # Internal helper function, covered in tests above