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 => 5; |
9 |
use Test::MockModule; |
10 |
use Test::Warn; |
11 |
use Test::Carp; |
12 |
use Test::Exception; |
13 |
use DBD::Mock; |
14 |
use CGI; |
15 |
use C4::Context; |
16 |
use Data::Printer; |
17 |
|
18 |
# Setup mock db |
19 |
my $context = new Test::MockModule('C4::Context'); |
20 |
$context->mock( |
21 |
'_new_dbh', |
22 |
sub { |
23 |
my $dbh = DBI->connect( 'DBI:Mock:', '', '' ) |
24 |
|| die "Cannot create handle: $DBI::errstr\n"; |
25 |
return $dbh; |
26 |
} |
27 |
); |
28 |
|
29 |
# Setup mock config |
30 |
$context->mock( 'config', \&mockedConfig ); |
31 |
|
32 |
# Setup mock preference |
33 |
$context->mock( 'preference', \&mockedPref ); |
34 |
|
35 |
my $matchpoint = 'userid'; |
36 |
my %mapping = ( |
37 |
'userid' => { 'is' => 'uid' }, |
38 |
); |
39 |
$ENV{'uid'} = "test1234"; |
40 |
|
41 |
#my %shibboleth = ( |
42 |
# 'matchpoint' => $matchpoint, |
43 |
# 'mapping' => \%mapping |
44 |
#); |
45 |
|
46 |
sub mockedConfig { |
47 |
my $param = shift; |
48 |
|
49 |
my %shibboleth = ( |
50 |
'matchpoint' => $matchpoint, |
51 |
'mapping' => \%mapping |
52 |
); |
53 |
|
54 |
return \%shibboleth; |
55 |
} |
56 |
|
57 |
sub reset_config { |
58 |
$matchpoint = 'userid'; |
59 |
%mapping = ( |
60 |
'userid' => { 'is' => 'uid' }, |
61 |
); |
62 |
$ENV{'uid'} = "test1234"; |
63 |
|
64 |
return 1; |
65 |
} |
66 |
|
67 |
sub mockedPref { |
68 |
my $param = $_[1]; |
69 |
my $return; |
70 |
|
71 |
if ( $param eq 'OPACBaseURL' ) { |
72 |
$return = "testopac.com"; |
73 |
} |
74 |
|
75 |
return $return; |
76 |
} |
77 |
|
78 |
my $dbh = C4::Context->dbh(); |
79 |
|
80 |
# Commence Testing |
81 |
# Test module can load |
82 |
use_ok('C4::Auth_with_shibboleth'); |
83 |
$C4::Auth_with_shibboleth::debug = '0'; |
84 |
|
85 |
# Subroutine tests |
86 |
|
87 |
## shib_ok |
88 |
subtest "shib_ok tests" => sub { |
89 |
plan tests => 5; |
90 |
my $result; |
91 |
|
92 |
# correct config, no debug |
93 |
is( shib_ok(), '1', "good config"); |
94 |
|
95 |
# bad config, no debug |
96 |
$matchpoint = undef; |
97 |
warnings_are { $result = shib_ok() } [{ carped => 'shibboleth matchpoint not defined'},], "undefined matchpoint = fatal config, warning given"; |
98 |
is( $result, '0', "bad config"); |
99 |
|
100 |
$matchpoint = 'email'; |
101 |
warnings_are { $result = shib_ok() } [{ carped => 'shibboleth matchpoint not mapped'},], "unmapped matchpoint = fatal config, warning given"; |
102 |
is( $result, '0', "bad config"); |
103 |
|
104 |
# add test for undefined shibboleth block |
105 |
|
106 |
reset_config(); |
107 |
}; |
108 |
|
109 |
## logout_shib |
110 |
#my $query = CGI->new(); |
111 |
#is(logout_shib($query),"https://".$opac."/Shibboleth.sso/Logout?return="."https://".$opac,"logout_shib"); |
112 |
|
113 |
## login_shib_url |
114 |
#is(login_shib_url($query),,); |
115 |
|
116 |
## get_login_shib |
117 |
subtest "get_login_shib tests" => sub { |
118 |
plan tests => 4; |
119 |
my $login; |
120 |
|
121 |
# good config |
122 |
## debug off |
123 |
$C4::Auth_with_shibboleth::debug = '0'; |
124 |
warnings_are { $login = get_login_shib() }[], |
125 |
"good config with debug off, no warnings recieved"; |
126 |
is( $login, "test1234", "good config with debug off, attribute value returned" ); |
127 |
|
128 |
## debug on |
129 |
$C4::Auth_with_shibboleth::debug = '1'; |
130 |
warnings_are { $login = get_login_shib() }[ |
131 |
"koha borrower field to match: userid", |
132 |
"shibboleth attribute to match: uid", |
133 |
"uid value: test1234" |
134 |
], |
135 |
"good config with debug enabled, correct warnings recieved"; |
136 |
is( $login, "test1234", "good config with debug enabled, attribute value returned" ); |
137 |
|
138 |
# bad config - with shib_ok implimented, we should never reach this sub with a bad config |
139 |
}; |
140 |
|
141 |
## checkpw_shib |
142 |
subtest "checkpw_shib tests" => sub { |
143 |
plan tests => 12; |
144 |
|
145 |
my $shib_login = 'test1234'; |
146 |
my @borrower_results = ( |
147 |
[ 'cardnumber', 'userid' ], |
148 |
[ 'testcardnumber', 'test1234' ], |
149 |
); |
150 |
$dbh->{mock_add_resultset} = \@borrower_results; |
151 |
|
152 |
my ( $retval, $retcard, $retuserid ); |
153 |
|
154 |
# debug off |
155 |
$C4::Auth_with_shibboleth::debug = '0'; |
156 |
# good user |
157 |
warnings_are { ($retval, $retcard, $retuserid) = checkpw_shib($dbh, $shib_login) } [],"good user with no debug"; |
158 |
is( $retval, "1", "user authenticated" ); |
159 |
is( $retcard, "testcardnumber", "expected cardnumber returned" ); |
160 |
is( $retuserid, "test1234", "expected userid returned"); |
161 |
|
162 |
# bad user |
163 |
warnings_are { ($retval, $retcard, $retuserid) = checkpw_shib($dbh, $shib_login) } [],"bad user with no debug"; |
164 |
is( $retval, "0", "user not authenticated" ); |
165 |
|
166 |
# reset db mock |
167 |
$dbh->{mock_add_resultset} = \@borrower_results; |
168 |
|
169 |
# debug on |
170 |
$C4::Auth_with_shibboleth::debug = '1'; |
171 |
|
172 |
# good user |
173 |
warnings_exist { ($retval, $retcard, $retuserid) = checkpw_shib($dbh, $shib_login) } [qr/checkpw_shib/,qr/User Shibboleth-authenticated as:/],"good user with debug enabled"; |
174 |
is( $retval, "1", "user authenticated" ); |
175 |
is( $retcard, "testcardnumber", "expected cardnumber returned" ); |
176 |
is( $retuserid, "test1234", "expected userid returned"); |
177 |
|
178 |
# bad user |
179 |
warnings_exist { ($retval, $retcard, $retuserid) = checkpw_shib($dbh, $shib_login) } [qr/checkpw_shib/,qr/User Shibboleth-authenticated as:/,qr/not a valid Koha user/],"bad user with debug enabled"; |
180 |
is( $retval, "0", "user not authenticated" ); |
181 |
|
182 |
}; |
183 |
|
184 |
## _get_uri |
185 |
subtest "_get_uri tests" => sub { |
186 |
plan tests => 1; |
187 |
|
188 |
is( C4::Auth_with_shibboleth::_get_uri(), "https://testopac.com", "https opac uri returned" ); |
189 |
|
190 |
}; |
191 |
|
192 |
## _get_shib_config |