|
Lines 24-36
use Test::MockModule;
Link Here
|
| 24 |
use Test::Warn; |
24 |
use Test::Warn; |
| 25 |
use File::Temp qw(tempdir); |
25 |
use File::Temp qw(tempdir); |
| 26 |
|
26 |
|
|
|
27 |
use t::lib::Mocks::Logger; |
| 28 |
|
| 27 |
use utf8; |
29 |
use utf8; |
| 28 |
use CGI qw(-utf8 ); |
30 |
use CGI qw(-utf8 ); |
| 29 |
use C4::Context; |
31 |
use C4::Context; |
| 30 |
|
32 |
|
| 31 |
BEGIN { |
33 |
BEGIN { |
| 32 |
if ( check_install( module => 'Test::DBIx::Class' ) ) { |
34 |
if ( check_install( module => 'Test::DBIx::Class' ) ) { |
| 33 |
plan tests => 17; |
35 |
plan tests => 18; |
| 34 |
} |
36 |
} |
| 35 |
else { |
37 |
else { |
| 36 |
plan skip_all => "Need Test::DBIx::Class"; |
38 |
plan skip_all => "Need Test::DBIx::Class"; |
|
Lines 89-98
$database->mock( 'schema', \&mockedSchema );
Link Here
|
| 89 |
# Tests |
91 |
# Tests |
| 90 |
############################################################## |
92 |
############################################################## |
| 91 |
|
93 |
|
|
|
94 |
my $logger = t::lib::Mocks::Logger->new(); |
| 95 |
|
| 92 |
# Can module load |
96 |
# Can module load |
| 93 |
use C4::Auth_with_shibboleth; |
97 |
use C4::Auth_with_shibboleth; |
| 94 |
require_ok('C4::Auth_with_shibboleth'); |
98 |
require_ok('C4::Auth_with_shibboleth'); |
| 95 |
$C4::Auth_with_shibboleth::debug = '0'; |
|
|
| 96 |
|
99 |
|
| 97 |
# Subroutine tests |
100 |
# Subroutine tests |
| 98 |
## shib_ok |
101 |
## shib_ok |
|
Lines 117-123
subtest "shib_ok tests" => sub {
Link Here
|
| 117 |
is( $result, '0', "bad config" ); |
120 |
is( $result, '0', "bad config" ); |
| 118 |
|
121 |
|
| 119 |
# add test for undefined shibboleth block |
122 |
# add test for undefined shibboleth block |
| 120 |
|
123 |
$logger->clear; |
| 121 |
reset_config(); |
124 |
reset_config(); |
| 122 |
}; |
125 |
}; |
| 123 |
|
126 |
|
|
Lines 172-205
subtest "login_shib_url tests" => sub {
Link Here
|
| 172 |
|
175 |
|
| 173 |
## get_login_shib |
176 |
## get_login_shib |
| 174 |
subtest "get_login_shib tests" => sub { |
177 |
subtest "get_login_shib tests" => sub { |
| 175 |
plan tests => 4; |
178 |
|
|
|
179 |
plan tests => 3; |
| 180 |
|
| 176 |
my $login; |
181 |
my $login; |
| 177 |
|
182 |
|
| 178 |
# good config |
183 |
$login = get_login_shib(); |
| 179 |
## debug off |
184 |
|
| 180 |
$C4::Auth_with_shibboleth::debug = '0'; |
185 |
$logger->debug_is("koha borrower field to match: userid", "borrower match field debug info") |
| 181 |
warnings_are { $login = get_login_shib() }[], |
186 |
->debug_is("shibboleth attribute to match: uid", "shib match attribute debug info") |
| 182 |
"good config with debug off, no warnings received"; |
187 |
->clear(); |
| 183 |
is( $login, "test1234", |
188 |
|
| 184 |
"good config with debug off, attribute value returned" ); |
189 |
is( $login, "test1234", "good config, attribute value returned" ); |
| 185 |
|
|
|
| 186 |
## debug on |
| 187 |
$C4::Auth_with_shibboleth::debug = '1'; |
| 188 |
warnings_are { $login = get_login_shib() }[ |
| 189 |
"koha borrower field to match: userid", |
| 190 |
"shibboleth attribute to match: uid", |
| 191 |
"uid value: test1234" |
| 192 |
], |
| 193 |
"good config with debug enabled, correct warnings received"; |
| 194 |
is( $login, "test1234", |
| 195 |
"good config with debug enabled, attribute value returned" ); |
| 196 |
|
| 197 |
# bad config - with shib_ok implemented, we should never reach this sub with a bad config |
| 198 |
}; |
190 |
}; |
| 199 |
|
191 |
|
| 200 |
## checkpw_shib |
192 |
## checkpw_shib |
| 201 |
subtest "checkpw_shib tests" => sub { |
193 |
subtest "checkpw_shib tests" => sub { |
| 202 |
plan tests => 24; |
194 |
|
|
|
195 |
plan tests => 33; |
| 203 |
|
196 |
|
| 204 |
my $shib_login; |
197 |
my $shib_login; |
| 205 |
my ( $retval, $retcard, $retuserid ); |
198 |
my ( $retval, $retcard, $retuserid ); |
|
Lines 216-263
subtest "checkpw_shib tests" => sub {
Link Here
|
| 216 |
], |
209 |
], |
| 217 |
'Installed some custom fixtures via the Populate fixture class'; |
210 |
'Installed some custom fixtures via the Populate fixture class'; |
| 218 |
|
211 |
|
| 219 |
# debug off |
|
|
| 220 |
$C4::Auth_with_shibboleth::debug = '0'; |
| 221 |
|
| 222 |
# good user |
212 |
# good user |
| 223 |
$shib_login = "test1234"; |
213 |
$shib_login = "test1234"; |
| 224 |
warnings_are { |
214 |
( $retval, $retcard, $retuserid ) = checkpw_shib($shib_login); |
| 225 |
( $retval, $retcard, $retuserid ) = checkpw_shib($shib_login); |
215 |
|
| 226 |
} |
216 |
is( $logger->count(), 2, "Two debugging entries"); |
| 227 |
[], "good user with no debug"; |
|
|
| 228 |
is( $retval, "1", "user authenticated" ); |
217 |
is( $retval, "1", "user authenticated" ); |
| 229 |
is( $retcard, "testcardnumber", "expected cardnumber returned" ); |
218 |
is( $retcard, "testcardnumber", "expected cardnumber returned" ); |
| 230 |
is( $retuserid, "test1234", "expected userid returned" ); |
219 |
is( $retuserid, "test1234", "expected userid returned" ); |
|
|
220 |
$logger->debug_is("koha borrower field to match: userid", "borrower match field debug info") |
| 221 |
->debug_is("shibboleth attribute to match: uid", "shib match attribute debug info") |
| 222 |
->clear(); |
| 231 |
|
223 |
|
| 232 |
# bad user |
224 |
# bad user |
| 233 |
$shib_login = 'martin'; |
225 |
$shib_login = 'martin'; |
| 234 |
warnings_are { |
226 |
( $retval, $retcard, $retuserid ) = checkpw_shib($shib_login); |
| 235 |
( $retval, $retcard, $retuserid ) = checkpw_shib($shib_login); |
|
|
| 236 |
} |
| 237 |
[], "bad user with no debug"; |
| 238 |
is( $retval, "0", "user not authenticated" ); |
227 |
is( $retval, "0", "user not authenticated" ); |
|
|
228 |
$logger->debug_is("koha borrower field to match: userid", "borrower match field debug info") |
| 229 |
->debug_is("shibboleth attribute to match: uid", "shib match attribute debug info") |
| 230 |
->clear(); |
| 239 |
|
231 |
|
| 240 |
# duplicated matchpoint |
232 |
# duplicated matchpoint |
| 241 |
$matchpoint = 'email'; |
233 |
$matchpoint = 'email'; |
| 242 |
$mapping{'email'} = { is => 'email' }; |
234 |
$mapping{'email'} = { is => 'email' }; |
| 243 |
$shib_login = 'kid@clamp.io'; |
235 |
$shib_login = 'kid@clamp.io'; |
| 244 |
warnings_are { |
236 |
( $retval, $retcard, $retuserid ) = checkpw_shib($shib_login); |
| 245 |
( $retval, $retcard, $retuserid ) = checkpw_shib($shib_login); |
|
|
| 246 |
} |
| 247 |
[], "bad user with no debug"; |
| 248 |
is( $retval, "0", "user not authenticated if duplicated matchpoint" ); |
237 |
is( $retval, "0", "user not authenticated if duplicated matchpoint" ); |
| 249 |
$C4::Auth_with_shibboleth::debug = '1'; |
238 |
$logger->debug_is("koha borrower field to match: email", "borrower match field debug info") |
| 250 |
warnings_are { |
239 |
->debug_is("shibboleth attribute to match: email", "shib match attribute debug info") |
| 251 |
( $retval, $retcard, $retuserid ) = checkpw_shib($shib_login); |
240 |
->clear(); |
| 252 |
} |
241 |
|
| 253 |
[ |
242 |
( $retval, $retcard, $retuserid ) = checkpw_shib($shib_login); |
| 254 |
q/checkpw_shib/, |
243 |
$logger->debug_is("koha borrower field to match: email", "borrower match field debug info") |
| 255 |
q/koha borrower field to match: email/, |
244 |
->debug_is("shibboleth attribute to match: email", "shib match attribute debug info") |
| 256 |
q/shibboleth attribute to match: email/, |
245 |
->warn_is('There are several users with email of kid@clamp.io, matchpoints must be unique', "duplicated matchpoint warned with debug") |
| 257 |
q/User Shibboleth-authenticated as: kid@clamp.io/, |
246 |
->clear(); |
| 258 |
q/There are several users with email of kid@clamp.io, matchpoints must be unique/ |
247 |
|
| 259 |
], "duplicated matchpoint warned with debug"; |
|
|
| 260 |
$C4::Auth_with_shibboleth::debug = '0'; |
| 261 |
reset_config(); |
248 |
reset_config(); |
| 262 |
|
249 |
|
| 263 |
# autocreate user |
250 |
# autocreate user |
|
Lines 269-280
subtest "checkpw_shib tests" => sub {
Link Here
|
| 269 |
$ENV{'cat'} = "S"; |
256 |
$ENV{'cat'} = "S"; |
| 270 |
$ENV{'add'} = 'Address'; |
257 |
$ENV{'add'} = 'Address'; |
| 271 |
$ENV{'city'} = 'City'; |
258 |
$ENV{'city'} = 'City'; |
| 272 |
warnings_are { |
259 |
|
| 273 |
( $retval, $retcard, $retuserid ) = checkpw_shib($shib_login); |
260 |
( $retval, $retcard, $retuserid ) = checkpw_shib($shib_login); |
| 274 |
} |
|
|
| 275 |
[], "new user added with no debug"; |
| 276 |
is( $retval, "1", "user authenticated" ); |
261 |
is( $retval, "1", "user authenticated" ); |
| 277 |
is( $retuserid, "test4321", "expected userid returned" ); |
262 |
is( $retuserid, "test4321", "expected userid returned" ); |
|
|
263 |
$logger->debug_is("koha borrower field to match: userid", "borrower match field debug info") |
| 264 |
->debug_is("shibboleth attribute to match: uid", "shib match attribute debug info") |
| 265 |
->clear(); |
| 266 |
|
| 278 |
ok my $new_user = ResultSet('Borrower') |
267 |
ok my $new_user = ResultSet('Borrower') |
| 279 |
->search( { 'userid' => 'test4321' }, { rows => 1 } ), "new user found"; |
268 |
->search( { 'userid' => 'test4321' }, { rows => 1 } ), "new user found"; |
| 280 |
is_fields [qw/surname dateexpiry address city/], $new_user->next, |
269 |
is_fields [qw/surname dateexpiry address city/], $new_user->next, |
|
Lines 285-294
subtest "checkpw_shib tests" => sub {
Link Here
|
| 285 |
# sync user |
274 |
# sync user |
| 286 |
$sync = 1; |
275 |
$sync = 1; |
| 287 |
$ENV{'city'} = 'AnotherCity'; |
276 |
$ENV{'city'} = 'AnotherCity'; |
| 288 |
warnings_are { |
277 |
( $retval, $retcard, $retuserid ) = checkpw_shib($shib_login); |
| 289 |
( $retval, $retcard, $retuserid ) = checkpw_shib($shib_login); |
278 |
$logger->debug_is("koha borrower field to match: userid", "borrower match field debug info") |
| 290 |
} |
279 |
->debug_is("shibboleth attribute to match: uid", "shib match attribute debug info") |
| 291 |
[], "good user with sync"; |
280 |
->clear(); |
| 292 |
|
281 |
|
| 293 |
ok my $sync_user = ResultSet('Borrower') |
282 |
ok my $sync_user = ResultSet('Borrower') |
| 294 |
->search( { 'userid' => 'test4321' }, { rows => 1 } ), "sync user found"; |
283 |
->search( { 'userid' => 'test4321' }, { rows => 1 } ), "sync user found"; |
|
Lines 298-337
subtest "checkpw_shib tests" => sub {
Link Here
|
| 298 |
'Found $sync_user synced city'; |
287 |
'Found $sync_user synced city'; |
| 299 |
$sync = 0; |
288 |
$sync = 0; |
| 300 |
|
289 |
|
| 301 |
# debug on |
|
|
| 302 |
$C4::Auth_with_shibboleth::debug = '1'; |
| 303 |
|
| 304 |
# good user |
290 |
# good user |
| 305 |
$shib_login = "test1234"; |
291 |
$shib_login = "test1234"; |
| 306 |
warnings_exist { |
292 |
( $retval, $retcard, $retuserid ) = checkpw_shib($shib_login); |
| 307 |
( $retval, $retcard, $retuserid ) = checkpw_shib($shib_login); |
|
|
| 308 |
} |
| 309 |
[ |
| 310 |
qr/checkpw_shib/, |
| 311 |
qr/koha borrower field to match: userid/, |
| 312 |
qr/shibboleth attribute to match: uid/, |
| 313 |
qr/User Shibboleth-authenticated as:/ |
| 314 |
], |
| 315 |
"good user with debug enabled"; |
| 316 |
is( $retval, "1", "user authenticated" ); |
293 |
is( $retval, "1", "user authenticated" ); |
| 317 |
is( $retcard, "testcardnumber", "expected cardnumber returned" ); |
294 |
is( $retcard, "testcardnumber", "expected cardnumber returned" ); |
| 318 |
is( $retuserid, "test1234", "expected userid returned" ); |
295 |
is( $retuserid, "test1234", "expected userid returned" ); |
|
|
296 |
$logger->debug_is("koha borrower field to match: userid", "borrower match field debug info") |
| 297 |
->debug_is("shibboleth attribute to match: uid", "shib match attribute debug info") |
| 298 |
->clear(); |
| 319 |
|
299 |
|
| 320 |
# bad user |
300 |
# bad user |
| 321 |
$shib_login = "martin"; |
301 |
$shib_login = "martin"; |
| 322 |
warnings_exist { |
302 |
( $retval, $retcard, $retuserid ) = checkpw_shib($shib_login); |
| 323 |
( $retval, $retcard, $retuserid ) = checkpw_shib($shib_login); |
|
|
| 324 |
} |
| 325 |
[ |
| 326 |
qr/checkpw_shib/, |
| 327 |
qr/koha borrower field to match: userid/, |
| 328 |
qr/shibboleth attribute to match: uid/, |
| 329 |
qr/User Shibboleth-authenticated as:/, |
| 330 |
qr/not a valid Koha user/ |
| 331 |
], |
| 332 |
"bad user with debug enabled"; |
| 333 |
is( $retval, "0", "user not authenticated" ); |
303 |
is( $retval, "0", "user not authenticated" ); |
| 334 |
|
304 |
$logger->info_is("There are several users with userid of martin, matchpoints must be unique", "Duplicated matchpoint warned to info"); |
| 335 |
}; |
305 |
}; |
| 336 |
|
306 |
|
| 337 |
## _get_uri - opac |
307 |
## _get_uri - opac |
|
Lines 339-386
$OPACBaseURL = "testopac.com";
Link Here
|
| 339 |
is( C4::Auth_with_shibboleth::_get_uri(), |
309 |
is( C4::Auth_with_shibboleth::_get_uri(), |
| 340 |
"https://testopac.com", "https opac uri returned" ); |
310 |
"https://testopac.com", "https opac uri returned" ); |
| 341 |
|
311 |
|
|
|
312 |
$logger->clear; |
| 313 |
|
| 342 |
$OPACBaseURL = "http://testopac.com"; |
314 |
$OPACBaseURL = "http://testopac.com"; |
| 343 |
my $result; |
315 |
my $result = C4::Auth_with_shibboleth::_get_uri(); |
| 344 |
warnings_are { $result = C4::Auth_with_shibboleth::_get_uri() }[ |
|
|
| 345 |
"shibboleth interface: $interface", |
| 346 |
"Shibboleth requires OPACBaseURL/staffClientBaseURL to use the https protocol!" |
| 347 |
], |
| 348 |
"improper protocol - received expected warning"; |
| 349 |
is( $result, "https://testopac.com", "https opac uri returned" ); |
316 |
is( $result, "https://testopac.com", "https opac uri returned" ); |
|
|
317 |
$logger->warn_is("Shibboleth requires OPACBaseURL/staffClientBaseURL to use the https protocol!", "Improper protocol logged to warn") |
| 318 |
->clear(); |
| 350 |
|
319 |
|
| 351 |
$OPACBaseURL = "https://testopac.com"; |
320 |
$OPACBaseURL = "https://testopac.com"; |
| 352 |
is( C4::Auth_with_shibboleth::_get_uri(), |
321 |
is( C4::Auth_with_shibboleth::_get_uri(), |
| 353 |
"https://testopac.com", "https opac uri returned" ); |
322 |
"https://testopac.com", "https opac uri returned" ); |
| 354 |
|
323 |
|
|
|
324 |
$logger->clear(); |
| 325 |
|
| 355 |
$OPACBaseURL = undef; |
326 |
$OPACBaseURL = undef; |
| 356 |
warnings_are { $result = C4::Auth_with_shibboleth::_get_uri() } |
327 |
$result = C4::Auth_with_shibboleth::_get_uri(); |
| 357 |
[ "shibboleth interface: $interface", "OPACBaseURL not set!" ], |
|
|
| 358 |
"undefined OPACBaseURL - received expected warning"; |
| 359 |
is( $result, "https://", "https $interface uri returned" ); |
328 |
is( $result, "https://", "https $interface uri returned" ); |
| 360 |
|
329 |
|
|
|
330 |
$logger->warn_is("Syspref staffClientBaseURL or OPACBaseURL not set!", "undefined OPACBaseURL - received expected warning") |
| 331 |
->clear(); |
| 332 |
|
| 361 |
## _get_uri - intranet |
333 |
## _get_uri - intranet |
| 362 |
$interface = 'intranet'; |
334 |
$interface = 'intranet'; |
| 363 |
$staffClientBaseURL = "teststaff.com"; |
335 |
$staffClientBaseURL = "teststaff.com"; |
| 364 |
is( C4::Auth_with_shibboleth::_get_uri(), |
336 |
is( C4::Auth_with_shibboleth::_get_uri(), |
| 365 |
"https://teststaff.com", "https $interface uri returned" ); |
337 |
"https://teststaff.com", "https $interface uri returned" ); |
| 366 |
|
338 |
|
|
|
339 |
|
| 340 |
$logger->clear; |
| 341 |
|
| 367 |
$staffClientBaseURL = "http://teststaff.com"; |
342 |
$staffClientBaseURL = "http://teststaff.com"; |
| 368 |
warnings_are { $result = C4::Auth_with_shibboleth::_get_uri() }[ |
343 |
$result = C4::Auth_with_shibboleth::_get_uri(); |
| 369 |
"shibboleth interface: $interface", |
|
|
| 370 |
"Shibboleth requires OPACBaseURL/staffClientBaseURL to use the https protocol!" |
| 371 |
], |
| 372 |
"improper protocol - received expected warning"; |
| 373 |
is( $result, "https://teststaff.com", "https $interface uri returned" ); |
344 |
is( $result, "https://teststaff.com", "https $interface uri returned" ); |
|
|
345 |
$logger->warn_is("Shibboleth requires OPACBaseURL/staffClientBaseURL to use the https protocol!") |
| 346 |
->clear; |
| 374 |
|
347 |
|
| 375 |
$staffClientBaseURL = "https://teststaff.com"; |
348 |
$staffClientBaseURL = "https://teststaff.com"; |
| 376 |
is( C4::Auth_with_shibboleth::_get_uri(), |
349 |
is( C4::Auth_with_shibboleth::_get_uri(), |
| 377 |
"https://teststaff.com", "https $interface uri returned" ); |
350 |
"https://teststaff.com", "https $interface uri returned" ); |
|
|
351 |
is( $logger->count(), 0, 'No logging' ); |
| 378 |
|
352 |
|
| 379 |
$staffClientBaseURL = undef; |
353 |
$staffClientBaseURL = undef; |
| 380 |
warnings_are { $result = C4::Auth_with_shibboleth::_get_uri() } |
354 |
$result = C4::Auth_with_shibboleth::_get_uri(); |
| 381 |
[ "shibboleth interface: $interface", "staffClientBaseURL not set!" ], |
|
|
| 382 |
"undefined staffClientBaseURL - received expected warning"; |
| 383 |
is( $result, "https://", "https $interface uri returned" ); |
355 |
is( $result, "https://", "https $interface uri returned" ); |
|
|
356 |
$logger->warn_is("Syspref staffClientBaseURL or OPACBaseURL not set!", "undefined staffClientBaseURL - received expected warning") |
| 357 |
->clear; |
| 384 |
|
358 |
|
| 385 |
## _get_shib_config |
359 |
## _get_shib_config |
| 386 |
# Internal helper function, covered in tests above |
360 |
# Internal helper function, covered in tests above |
| 387 |
- |
|
|