|
Lines 38-45
use Test::DBIx::Class;
Link Here
|
| 38 |
|
38 |
|
| 39 |
# Mock Variables |
39 |
# Mock Variables |
| 40 |
my $matchpoint = 'userid'; |
40 |
my $matchpoint = 'userid'; |
| 41 |
my %mapping = ( 'userid' => { 'is' => 'uid' }, ); |
41 |
my $autocreate = 0; |
| 42 |
$ENV{'uid'} = "test1234"; |
42 |
my %mapping = ( |
|
|
43 |
'userid' => { 'is' => 'uid' }, |
| 44 |
'surname' => { 'is' => 'sn' }, |
| 45 |
'dateexpiry' => { 'is' => 'exp' }, |
| 46 |
'categorycode' => { 'is' => 'cat' }, |
| 47 |
'address' => { 'is' => 'add' }, |
| 48 |
'city' => { 'is' => 'city' }, |
| 49 |
); |
| 50 |
$ENV{'uid'} = "test1234"; |
| 51 |
$ENV{'sn'} = undef; |
| 52 |
$ENV{'exp'} = undef; |
| 53 |
$ENV{'cat'} = undef; |
| 54 |
$ENV{'add'} = undef; |
| 55 |
$ENV{'city'} = undef; |
| 43 |
|
56 |
|
| 44 |
# Setup Mocks |
57 |
# Setup Mocks |
| 45 |
## Mock Context |
58 |
## Mock Context |
|
Lines 48-98
my $context = new Test::MockModule('C4::Context');
Link Here
|
| 48 |
### Mock ->config |
61 |
### Mock ->config |
| 49 |
$context->mock( 'config', \&mockedConfig ); |
62 |
$context->mock( 'config', \&mockedConfig ); |
| 50 |
|
63 |
|
| 51 |
sub mockedConfig { |
|
|
| 52 |
my $param = shift; |
| 53 |
|
| 54 |
my %shibboleth = ( |
| 55 |
'matchpoint' => $matchpoint, |
| 56 |
'mapping' => \%mapping |
| 57 |
); |
| 58 |
|
| 59 |
return \%shibboleth; |
| 60 |
} |
| 61 |
|
| 62 |
### Mock ->preference |
64 |
### Mock ->preference |
| 63 |
my $OPACBaseURL = "testopac.com"; |
65 |
my $OPACBaseURL = "testopac.com"; |
| 64 |
$context->mock( 'preference', \&mockedPref ); |
66 |
$context->mock( 'preference', \&mockedPref ); |
| 65 |
|
67 |
|
| 66 |
sub mockedPref { |
|
|
| 67 |
my $param = $_[1]; |
| 68 |
my $return; |
| 69 |
|
| 70 |
if ( $param eq 'OPACBaseURL' ) { |
| 71 |
$return = $OPACBaseURL; |
| 72 |
} |
| 73 |
|
| 74 |
return $return; |
| 75 |
} |
| 76 |
|
| 77 |
## Mock Database |
68 |
## Mock Database |
| 78 |
my $database = new Test::MockModule('Koha::Database'); |
69 |
my $database = new Test::MockModule('Koha::Database'); |
| 79 |
|
70 |
|
| 80 |
### Mock ->schema |
71 |
### Mock ->schema |
| 81 |
$database->mock( 'schema', \&mockedSchema ); |
72 |
$database->mock( 'schema', \&mockedSchema ); |
| 82 |
|
73 |
|
| 83 |
sub mockedSchema { |
|
|
| 84 |
return Schema(); |
| 85 |
} |
| 86 |
|
| 87 |
## Convenience method to reset config |
| 88 |
sub reset_config { |
| 89 |
$matchpoint = 'userid'; |
| 90 |
%mapping = ( 'userid' => { 'is' => 'uid' }, ); |
| 91 |
$ENV{'uid'} = "test1234"; |
| 92 |
|
| 93 |
return 1; |
| 94 |
} |
| 95 |
|
| 96 |
# Tests |
74 |
# Tests |
| 97 |
############################################################## |
75 |
############################################################## |
| 98 |
|
76 |
|
|
Lines 175-181
subtest "get_login_shib tests" => sub {
Link Here
|
| 175 |
|
153 |
|
| 176 |
## checkpw_shib |
154 |
## checkpw_shib |
| 177 |
subtest "checkpw_shib tests" => sub { |
155 |
subtest "checkpw_shib tests" => sub { |
| 178 |
plan tests => 13; |
156 |
plan tests => 18; |
| 179 |
|
157 |
|
| 180 |
my $shib_login; |
158 |
my $shib_login; |
| 181 |
my ( $retval, $retcard, $retuserid ); |
159 |
my ( $retval, $retcard, $retuserid ); |
|
Lines 186-191
subtest "checkpw_shib tests" => sub {
Link Here
|
| 186 |
[qw/cardnumber userid surname address city/], |
164 |
[qw/cardnumber userid surname address city/], |
| 187 |
[qw/testcardnumber test1234 renvoize myaddress johnston/], |
165 |
[qw/testcardnumber test1234 renvoize myaddress johnston/], |
| 188 |
], |
166 |
], |
|
|
167 |
'Category' => [ [qw/categorycode default_privacy/], [qw/S never/], ] |
| 189 |
], |
168 |
], |
| 190 |
'Installed some custom fixtures via the Populate fixture class'; |
169 |
'Installed some custom fixtures via the Populate fixture class'; |
| 191 |
|
170 |
|
|
Lines 195-201
subtest "checkpw_shib tests" => sub {
Link Here
|
| 195 |
# good user |
174 |
# good user |
| 196 |
$shib_login = "test1234"; |
175 |
$shib_login = "test1234"; |
| 197 |
warnings_are { |
176 |
warnings_are { |
| 198 |
( $retval, $retcard, $retuserid ) = checkpw_shib( $shib_login ); |
177 |
( $retval, $retcard, $retuserid ) = checkpw_shib($shib_login); |
| 199 |
} |
178 |
} |
| 200 |
[], "good user with no debug"; |
179 |
[], "good user with no debug"; |
| 201 |
is( $retval, "1", "user authenticated" ); |
180 |
is( $retval, "1", "user authenticated" ); |
|
Lines 205-226
subtest "checkpw_shib tests" => sub {
Link Here
|
| 205 |
# bad user |
184 |
# bad user |
| 206 |
$shib_login = 'martin'; |
185 |
$shib_login = 'martin'; |
| 207 |
warnings_are { |
186 |
warnings_are { |
| 208 |
( $retval, $retcard, $retuserid ) = checkpw_shib( $shib_login ); |
187 |
( $retval, $retcard, $retuserid ) = checkpw_shib($shib_login); |
| 209 |
} |
188 |
} |
| 210 |
[], "bad user with no debug"; |
189 |
[], "bad user with no debug"; |
| 211 |
is( $retval, "0", "user not authenticated" ); |
190 |
is( $retval, "0", "user not authenticated" ); |
| 212 |
|
191 |
|
|
|
192 |
# autocreate user |
| 193 |
$autocreate = 1; |
| 194 |
$shib_login = 'test4321'; |
| 195 |
$ENV{'uid'} = 'test4321'; |
| 196 |
$ENV{'sn'} = "pika"; |
| 197 |
$ENV{'exp'} = "2017"; |
| 198 |
$ENV{'cat'} = "S"; |
| 199 |
$ENV{'add'} = 'Address'; |
| 200 |
$ENV{'city'} = 'City'; |
| 201 |
warnings_are { |
| 202 |
( $retval, $retcard, $retuserid ) = checkpw_shib($shib_login); |
| 203 |
} |
| 204 |
[], "new user added with no debug"; |
| 205 |
is( $retval, "1", "user authenticated" ); |
| 206 |
is( $retuserid, "test4321", "expected userid returned" ); |
| 207 |
ok my $new_user = ResultSet('Borrower') |
| 208 |
->search( { 'userid' => 'test4321' }, { rows => 1 } ), "new user found"; |
| 209 |
is_fields [qw/surname dateexpiry address city/], $new_user->next, |
| 210 |
[qw/pika 2017 Address City/], |
| 211 |
'Found $new_users surname'; |
| 212 |
$autocreate = 0; |
| 213 |
|
| 213 |
# debug on |
214 |
# debug on |
| 214 |
$C4::Auth_with_shibboleth::debug = '1'; |
215 |
$C4::Auth_with_shibboleth::debug = '1'; |
| 215 |
|
216 |
|
| 216 |
# good user |
217 |
# good user |
| 217 |
$shib_login = "test1234"; |
218 |
$shib_login = "test1234"; |
| 218 |
warnings_exist { |
219 |
warnings_exist { |
| 219 |
( $retval, $retcard, $retuserid ) = checkpw_shib( $shib_login ); |
220 |
( $retval, $retcard, $retuserid ) = checkpw_shib($shib_login); |
| 220 |
} |
221 |
} |
| 221 |
[ qr/checkpw_shib/, qr/koha borrower field to match: userid/, |
222 |
[ |
| 222 |
qr/shibboleth attribute to match: uid/, |
223 |
qr/checkpw_shib/, |
| 223 |
qr/User Shibboleth-authenticated as:/ ], |
224 |
qr/koha borrower field to match: userid/, |
|
|
225 |
qr/shibboleth attribute to match: uid/, |
| 226 |
qr/User Shibboleth-authenticated as:/ |
| 227 |
], |
| 224 |
"good user with debug enabled"; |
228 |
"good user with debug enabled"; |
| 225 |
is( $retval, "1", "user authenticated" ); |
229 |
is( $retval, "1", "user authenticated" ); |
| 226 |
is( $retcard, "testcardnumber", "expected cardnumber returned" ); |
230 |
is( $retcard, "testcardnumber", "expected cardnumber returned" ); |
|
Lines 229-235
subtest "checkpw_shib tests" => sub {
Link Here
|
| 229 |
# bad user |
233 |
# bad user |
| 230 |
$shib_login = "martin"; |
234 |
$shib_login = "martin"; |
| 231 |
warnings_exist { |
235 |
warnings_exist { |
| 232 |
( $retval, $retcard, $retuserid ) = checkpw_shib( $shib_login ); |
236 |
( $retval, $retcard, $retuserid ) = checkpw_shib($shib_login); |
| 233 |
} |
237 |
} |
| 234 |
[ |
238 |
[ |
| 235 |
qr/checkpw_shib/, |
239 |
qr/checkpw_shib/, |
|
Lines 251-258
is( C4::Auth_with_shibboleth::_get_uri(),
Link Here
|
| 251 |
$OPACBaseURL = "http://testopac.com"; |
255 |
$OPACBaseURL = "http://testopac.com"; |
| 252 |
my $result; |
256 |
my $result; |
| 253 |
warning_like { $result = C4::Auth_with_shibboleth::_get_uri() } |
257 |
warning_like { $result = C4::Auth_with_shibboleth::_get_uri() } |
| 254 |
[ qr/Shibboleth requires OPACBaseURL to use the https protocol!/ ], |
258 |
[qr/Shibboleth requires OPACBaseURL to use the https protocol!/], |
| 255 |
"improper protocol - received expected warning"; |
259 |
"improper protocol - received expected warning"; |
| 256 |
is( $result, "https://testopac.com", "https opac uri returned" ); |
260 |
is( $result, "https://testopac.com", "https opac uri returned" ); |
| 257 |
|
261 |
|
| 258 |
$OPACBaseURL = "https://testopac.com"; |
262 |
$OPACBaseURL = "https://testopac.com"; |
|
Lines 261-269
is( C4::Auth_with_shibboleth::_get_uri(),
Link Here
|
| 261 |
|
265 |
|
| 262 |
$OPACBaseURL = undef; |
266 |
$OPACBaseURL = undef; |
| 263 |
warning_like { $result = C4::Auth_with_shibboleth::_get_uri() } |
267 |
warning_like { $result = C4::Auth_with_shibboleth::_get_uri() } |
| 264 |
[ qr/OPACBaseURL not set!/ ], |
268 |
[qr/OPACBaseURL not set!/], |
| 265 |
"undefined OPACBaseURL - received expected warning"; |
269 |
"undefined OPACBaseURL - received expected warning"; |
| 266 |
is( $result, "https://", "https opac uri returned" ); |
270 |
is( $result, "https://", "https opac uri returned" ); |
| 267 |
|
271 |
|
| 268 |
## _get_shib_config |
272 |
## _get_shib_config |
| 269 |
# Internal helper function, covered in tests above |
273 |
# Internal helper function, covered in tests above |
| 270 |
- |
274 |
|
|
|
275 |
sub mockedConfig { |
| 276 |
my $param = shift; |
| 277 |
|
| 278 |
my %shibboleth = ( |
| 279 |
'autocreate' => $autocreate, |
| 280 |
'matchpoint' => $matchpoint, |
| 281 |
'mapping' => \%mapping |
| 282 |
); |
| 283 |
|
| 284 |
return \%shibboleth; |
| 285 |
} |
| 286 |
|
| 287 |
sub mockedPref { |
| 288 |
my $param = $_[1]; |
| 289 |
my $return; |
| 290 |
|
| 291 |
if ( $param eq 'OPACBaseURL' ) { |
| 292 |
$return = $OPACBaseURL; |
| 293 |
} |
| 294 |
|
| 295 |
return $return; |
| 296 |
} |
| 297 |
|
| 298 |
sub mockedSchema { |
| 299 |
return Schema(); |
| 300 |
} |
| 301 |
|
| 302 |
## Convenience method to reset config |
| 303 |
sub reset_config { |
| 304 |
$matchpoint = 'userid'; |
| 305 |
$autocreate = 0; |
| 306 |
%mapping = ( |
| 307 |
'userid' => { 'is' => 'uid' }, |
| 308 |
'surname' => { 'is' => 'sn' }, |
| 309 |
'dateexpiry' => { 'is' => 'exp' }, |
| 310 |
'categorycode' => { 'is' => 'cat' }, |
| 311 |
'address' => { 'is' => 'add' }, |
| 312 |
'city' => { 'is' => 'city' }, |
| 313 |
); |
| 314 |
$ENV{'uid'} = "test1234"; |
| 315 |
$ENV{'sn'} = undef; |
| 316 |
$ENV{'exp'} = undef; |
| 317 |
$ENV{'cat'} = undef; |
| 318 |
$ENV{'add'} = undef; |
| 319 |
$ENV{'city'} = undef; |
| 320 |
|
| 321 |
return 1; |
| 322 |
} |
| 323 |
|
| 324 |
1; |