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 C4::Auth_with_shibboleth;
101 require_ok('C4::Auth_with_shibboleth');
102 $C4::Auth_with_shibboleth::debug = '0';
106 subtest "shib_ok tests" => sub {
110 # correct config, no debug
111 is( shib_ok(), '1', "good config" );
113 # bad config, no debug
115 warnings_are { $result = shib_ok() }
116 [ { carped => 'shibboleth matchpoint not defined' }, ],
117 "undefined matchpoint = fatal config, warning given";
118 is( $result, '0', "bad config" );
120 $matchpoint = 'email';
121 warnings_are { $result = shib_ok() }
122 [ { carped => 'shibboleth matchpoint not mapped' }, ],
123 "unmapped matchpoint = fatal config, warning given";
124 is( $result, '0', "bad config" );
126 # add test for undefined shibboleth block
132 #my $query = CGI->new();
133 #is(logout_shib($query),"https://".$opac."/Shibboleth.sso/Logout?return="."https://".$opac,"logout_shib");
136 my $query_string = 'language=en-GB';
137 $ENV{QUERY_STRING} = $query_string;
138 $ENV{SCRIPT_NAME} = '/cgi-bin/koha/opac-user.pl';
139 my $query = CGI->new($query_string);
141 login_shib_url($query),
142 'https://testopac.com'
143 . '/Shibboleth.sso/Login?target='
144 . 'https://testopac.com/cgi-bin/koha/opac-user.pl' . '%3F'
150 subtest "get_login_shib tests" => sub {
156 $C4::Auth_with_shibboleth::debug = '0';
157 warnings_are { $login = get_login_shib() }[],
158 "good config with debug off, no warnings received";
159 is( $login, "test1234",
160 "good config with debug off, attribute value returned" );
163 $C4::Auth_with_shibboleth::debug = '1';
164 warnings_are { $login = get_login_shib() }[
165 "koha borrower field to match: userid",
166 "shibboleth attribute to match: uid",
167 "uid value: test1234"
169 "good config with debug enabled, correct warnings received";
170 is( $login, "test1234",
171 "good config with debug enabled, attribute value returned" );
173 # bad config - with shib_ok implemented, we should never reach this sub with a bad config
177 subtest "checkpw_shib tests" => sub {
181 my ( $retval, $retcard, $retuserid );
183 # Setup Mock Database Data
186 [qw/cardnumber userid surname address city/],
187 [qw/testcardnumber test1234 renvoize myaddress johnston/],
190 'Installed some custom fixtures via the Populate fixture class';
193 $C4::Auth_with_shibboleth::debug = '0';
196 $shib_login = "test1234";
198 ( $retval, $retcard, $retuserid ) = checkpw_shib( $shib_login );
200 [], "good user with no debug";
201 is( $retval, "1", "user authenticated" );
202 is( $retcard, "testcardnumber", "expected cardnumber returned" );
203 is( $retuserid, "test1234", "expected userid returned" );
206 $shib_login = 'martin';
208 ( $retval, $retcard, $retuserid ) = checkpw_shib( $shib_login );
210 [], "bad user with no debug";
211 is( $retval, "0", "user not authenticated" );
214 $C4::Auth_with_shibboleth::debug = '1';
217 $shib_login = "test1234";
219 ( $retval, $retcard, $retuserid ) = checkpw_shib( $shib_login );
221 [ qr/checkpw_shib/, qr/koha borrower field to match: userid/,
222 qr/shibboleth attribute to match: uid/,
223 qr/User Shibboleth-authenticated as:/ ],
224 "good user with debug enabled";
225 is( $retval, "1", "user authenticated" );
226 is( $retcard, "testcardnumber", "expected cardnumber returned" );
227 is( $retuserid, "test1234", "expected userid returned" );
230 $shib_login = "martin";
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:/,
239 qr/not a valid Koha user/
241 "bad user with debug enabled";
242 is( $retval, "0", "user not authenticated" );
247 $OPACBaseURL = "testopac.com";
248 is( C4::Auth_with_shibboleth::_get_uri(),
249 "https://testopac.com", "https opac uri returned" );
251 $OPACBaseURL = "http://testopac.com";
253 warning_like { $result = C4::Auth_with_shibboleth::_get_uri() }
254 [ qr/Shibboleth requires OPACBaseURL to use the https protocol!/ ],
255 "improper protocol - received expected warning";
256 is( $result, "https://testopac.com", "https opac uri returned" );
258 $OPACBaseURL = "https://testopac.com";
259 is( C4::Auth_with_shibboleth::_get_uri(),
260 "https://testopac.com", "https opac uri returned" );
262 $OPACBaseURL = undef;
263 warning_like { $result = C4::Auth_with_shibboleth::_get_uri() }
264 [ qr/OPACBaseURL not set!/ ],
265 "undefined OPACBaseURL - received expected warning";
266 is( $result, "https://", "https opac uri returned" );
269 # Internal helper function, covered in tests above