|
Line 0
Link Here
|
| 0 |
- |
1 |
#!/usr/bin/perl |
|
|
2 |
# |
| 3 |
# This Koha test module is a stub! |
| 4 |
# Add more tests here!!! |
| 5 |
|
| 6 |
use Modern::Perl; |
| 7 |
|
| 8 |
use Test::More tests => 6; |
| 9 |
use Test::MockModule; |
| 10 |
use Test::Warn; |
| 11 |
use DBD::Mock; |
| 12 |
|
| 13 |
use CGI; |
| 14 |
use C4::Context; |
| 15 |
|
| 16 |
# Mock Variables |
| 17 |
my $matchpoint = 'userid'; |
| 18 |
my %mapping = ( 'userid' => { 'is' => 'uid' }, ); |
| 19 |
$ENV{'uid'} = "test1234"; |
| 20 |
|
| 21 |
#my %shibboleth = ( |
| 22 |
# 'matchpoint' => $matchpoint, |
| 23 |
# 'mapping' => \%mapping |
| 24 |
#); |
| 25 |
|
| 26 |
# Setup Mocks |
| 27 |
## Mock Context |
| 28 |
my $context = new Test::MockModule('C4::Context'); |
| 29 |
|
| 30 |
### Mock ->dbh |
| 31 |
$context->mock( |
| 32 |
'_new_dbh', |
| 33 |
sub { |
| 34 |
my $dbh = DBI->connect( 'DBI:Mock:', '', '' ) |
| 35 |
|| die "Cannot create handle: $DBI::errstr\n"; |
| 36 |
return $dbh; |
| 37 |
} |
| 38 |
); |
| 39 |
|
| 40 |
### Mock ->config |
| 41 |
$context->mock( 'config', \&mockedConfig ); |
| 42 |
|
| 43 |
sub mockedConfig { |
| 44 |
my $param = shift; |
| 45 |
|
| 46 |
my %shibboleth = ( |
| 47 |
'matchpoint' => $matchpoint, |
| 48 |
'mapping' => \%mapping |
| 49 |
); |
| 50 |
|
| 51 |
return \%shibboleth; |
| 52 |
} |
| 53 |
|
| 54 |
### Mock ->preference |
| 55 |
$context->mock( 'preference', \&mockedPref ); |
| 56 |
|
| 57 |
sub mockedPref { |
| 58 |
my $param = $_[1]; |
| 59 |
my $return; |
| 60 |
|
| 61 |
if ( $param eq 'OPACBaseURL' ) { |
| 62 |
$return = "testopac.com"; |
| 63 |
} |
| 64 |
|
| 65 |
return $return; |
| 66 |
} |
| 67 |
|
| 68 |
# Convenience methods |
| 69 |
## Reset Context |
| 70 |
sub reset_config { |
| 71 |
$matchpoint = 'userid'; |
| 72 |
%mapping = ( 'userid' => { 'is' => 'uid' }, ); |
| 73 |
$ENV{'uid'} = "test1234"; |
| 74 |
|
| 75 |
return 1; |
| 76 |
} |
| 77 |
|
| 78 |
# Tests |
| 79 |
my $dbh = C4::Context->dbh(); |
| 80 |
|
| 81 |
# Can module load |
| 82 |
use_ok('C4::Auth_with_shibboleth'); |
| 83 |
$C4::Auth_with_shibboleth::debug = '0'; |
| 84 |
|
| 85 |
# Subroutine tests |
| 86 |
## shib_ok |
| 87 |
subtest "shib_ok tests" => sub { |
| 88 |
plan tests => 5; |
| 89 |
my $result; |
| 90 |
|
| 91 |
# correct config, no debug |
| 92 |
is( shib_ok(), '1', "good config" ); |
| 93 |
|
| 94 |
# bad config, no debug |
| 95 |
$matchpoint = undef; |
| 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" ); |
| 100 |
|
| 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" ); |
| 106 |
|
| 107 |
# add test for undefined shibboleth block |
| 108 |
|
| 109 |
reset_config(); |
| 110 |
}; |
| 111 |
|
| 112 |
## logout_shib |
| 113 |
#my $query = CGI->new(); |
| 114 |
#is(logout_shib($query),"https://".$opac."/Shibboleth.sso/Logout?return="."https://".$opac,"logout_shib"); |
| 115 |
|
| 116 |
## login_shib_url |
| 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); |
| 121 |
is( |
| 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' |
| 126 |
. $query_string, |
| 127 |
"login shib url" |
| 128 |
); |
| 129 |
|
| 130 |
## get_login_shib |
| 131 |
subtest "get_login_shib tests" => sub { |
| 132 |
plan tests => 4; |
| 133 |
my $login; |
| 134 |
|
| 135 |
# good config |
| 136 |
## debug off |
| 137 |
$C4::Auth_with_shibboleth::debug = '0'; |
| 138 |
warnings_are { $login = get_login_shib() }[], |
| 139 |
"good config with debug off, no warnings recieved"; |
| 140 |
is( $login, "test1234", |
| 141 |
"good config with debug off, attribute value returned" ); |
| 142 |
|
| 143 |
## debug on |
| 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" |
| 149 |
], |
| 150 |
"good config with debug enabled, correct warnings recieved"; |
| 151 |
is( $login, "test1234", |
| 152 |
"good config with debug enabled, attribute value returned" ); |
| 153 |
|
| 154 |
# bad config - with shib_ok implimented, we should never reach this sub with a bad config |
| 155 |
}; |
| 156 |
|
| 157 |
## checkpw_shib |
| 158 |
subtest "checkpw_shib tests" => sub { |
| 159 |
plan tests => 12; |
| 160 |
|
| 161 |
my $shib_login = 'test1234'; |
| 162 |
my @borrower_results = |
| 163 |
( [ 'cardnumber', 'userid' ], [ 'testcardnumber', 'test1234' ], ); |
| 164 |
$dbh->{mock_add_resultset} = \@borrower_results; |
| 165 |
|
| 166 |
my ( $retval, $retcard, $retuserid ); |
| 167 |
|
| 168 |
# debug off |
| 169 |
$C4::Auth_with_shibboleth::debug = '0'; |
| 170 |
|
| 171 |
# good user |
| 172 |
warnings_are { |
| 173 |
( $retval, $retcard, $retuserid ) = checkpw_shib( $dbh, $shib_login ); |
| 174 |
} |
| 175 |
[], "good user with no debug"; |
| 176 |
is( $retval, "1", "user authenticated" ); |
| 177 |
is( $retcard, "testcardnumber", "expected cardnumber returned" ); |
| 178 |
is( $retuserid, "test1234", "expected userid returned" ); |
| 179 |
|
| 180 |
# bad user |
| 181 |
warnings_are { |
| 182 |
( $retval, $retcard, $retuserid ) = checkpw_shib( $dbh, $shib_login ); |
| 183 |
} |
| 184 |
[], "bad user with no debug"; |
| 185 |
is( $retval, "0", "user not authenticated" ); |
| 186 |
|
| 187 |
# reset db mock |
| 188 |
$dbh->{mock_add_resultset} = \@borrower_results; |
| 189 |
|
| 190 |
# debug on |
| 191 |
$C4::Auth_with_shibboleth::debug = '1'; |
| 192 |
|
| 193 |
# good user |
| 194 |
warnings_exist { |
| 195 |
( $retval, $retcard, $retuserid ) = checkpw_shib( $dbh, $shib_login ); |
| 196 |
} |
| 197 |
[ qr/checkpw_shib/, qr/User Shibboleth-authenticated as:/ ], |
| 198 |
"good user with debug enabled"; |
| 199 |
is( $retval, "1", "user authenticated" ); |
| 200 |
is( $retcard, "testcardnumber", "expected cardnumber returned" ); |
| 201 |
is( $retuserid, "test1234", "expected userid returned" ); |
| 202 |
|
| 203 |
# bad user |
| 204 |
warnings_exist { |
| 205 |
( $retval, $retcard, $retuserid ) = checkpw_shib( $dbh, $shib_login ); |
| 206 |
} |
| 207 |
[ |
| 208 |
qr/checkpw_shib/, |
| 209 |
qr/User Shibboleth-authenticated as:/, |
| 210 |
qr/not a valid Koha user/ |
| 211 |
], |
| 212 |
"bad user with debug enabled"; |
| 213 |
is( $retval, "0", "user not authenticated" ); |
| 214 |
|
| 215 |
}; |
| 216 |
|
| 217 |
## _get_uri |
| 218 |
is( C4::Auth_with_shibboleth::_get_uri(), |
| 219 |
"https://testopac.com", "https opac uri returned" ); |
| 220 |
|
| 221 |
## _get_shib_config |