Lines 26-34
use Test::Warn;
Link Here
|
26 |
use C4::Context; |
26 |
use C4::Context; |
27 |
|
27 |
|
28 |
my $dbh = C4::Context->dbh; |
28 |
my $dbh = C4::Context->dbh; |
|
|
29 |
|
29 |
# Start transaction |
30 |
# Start transaction |
30 |
$dbh->{ AutoCommit } = 0; |
31 |
$dbh->{AutoCommit} = 0; |
31 |
$dbh->{ RaiseError } = 1; |
32 |
$dbh->{RaiseError} = 1; |
32 |
|
33 |
|
33 |
my $builder = t::lib::TestBuilder->new(); |
34 |
my $builder = t::lib::TestBuilder->new(); |
34 |
|
35 |
|
Lines 37-42
my $update = 0;
Link Here
|
37 |
my $replicate = 0; |
38 |
my $replicate = 0; |
38 |
my $auth_by_bind = 1; |
39 |
my $auth_by_bind = 1; |
39 |
my $anonymous_bind = 1; |
40 |
my $anonymous_bind = 1; |
|
|
41 |
|
40 |
# Variables controlling LDAP behaviour |
42 |
# Variables controlling LDAP behaviour |
41 |
my $desired_authentication_result = 'success'; |
43 |
my $desired_authentication_result = 'success'; |
42 |
my $desired_connection_result = 'error'; |
44 |
my $desired_connection_result = 'error'; |
Lines 48-187
my $non_anonymous_bind_result = 'error';
Link Here
|
48 |
my $ret; |
50 |
my $ret; |
49 |
|
51 |
|
50 |
# Mock the context module |
52 |
# Mock the context module |
51 |
my $context = new Test::MockModule( 'C4::Context' ); |
53 |
my $context = Test::MockModule->new('C4::Context'); |
52 |
$context->mock( 'config', \&mockedC4Config ); |
54 |
$context->mock( 'config', \&mockedC4Config ); |
53 |
|
55 |
|
54 |
# Mock the Net::LDAP module |
56 |
# Mock the Net::LDAP module |
55 |
my $ldap = new Test::MockModule( 'Net::LDAP' ); |
57 |
my $net_ldap = Test::MockModule->new('Net::LDAP'); |
56 |
|
58 |
|
57 |
$ldap->mock( 'new', sub { |
59 |
$net_ldap->mock( |
58 |
if ( $desired_connection_result eq 'error' ) { |
60 |
'new', |
59 |
# We were asked to fail the LDAP conexion |
61 |
sub { |
60 |
return; |
62 |
if ( $desired_connection_result eq 'error' ) { |
61 |
} else { |
63 |
|
62 |
# Return a mocked Net::LDAP object (Test::MockObject) |
64 |
# We were asked to fail the LDAP conexion |
63 |
return mock_net_ldap(); |
65 |
return; |
|
|
66 |
} |
67 |
else { |
68 |
# Return a mocked Net::LDAP object (Test::MockObject) |
69 |
return mock_net_ldap(); |
70 |
} |
64 |
} |
71 |
} |
65 |
}); |
72 |
); |
66 |
|
73 |
|
67 |
my $categorycode = $builder->build({ source => 'Category' })->{ categorycode }; |
74 |
my $categorycode = $builder->build( { source => 'Category' } )->{categorycode}; |
68 |
my $branchcode = $builder->build({ source => 'Branch' })->{ branchcode }; |
75 |
my $branchcode = $builder->build( { source => 'Branch' } )->{branchcode}; |
69 |
my $attrType = $builder->build({ |
76 |
my $attr_type = $builder->build( |
70 |
source => 'BorrowerAttributeType', |
77 |
{ |
71 |
value => { |
78 |
source => 'BorrowerAttributeType', |
72 |
category_code => $categorycode |
79 |
value => { |
|
|
80 |
category_code => $categorycode |
81 |
} |
73 |
} |
82 |
} |
74 |
}); |
83 |
); |
75 |
|
84 |
|
76 |
my $borrower = $builder->build({ |
85 |
my $borrower = $builder->build( |
77 |
source => 'Borrower', |
86 |
{ |
78 |
value => { |
87 |
source => 'Borrower', |
79 |
userid => 'hola', |
88 |
value => { |
80 |
branchcode => $branchcode, |
89 |
userid => 'hola', |
81 |
categorycode => $categorycode |
90 |
branchcode => $branchcode, |
|
|
91 |
categorycode => $categorycode |
92 |
} |
82 |
} |
93 |
} |
83 |
}); |
94 |
); |
84 |
|
95 |
|
85 |
$builder->build({ |
96 |
$builder->build( |
86 |
source => 'BorrowerAttribute', |
97 |
{ |
87 |
value => { |
98 |
source => 'BorrowerAttribute', |
88 |
borrowernumber => $borrower->{borrowernumber}, |
99 |
value => { |
89 |
code => $attrType->{code}, |
100 |
borrowernumber => $borrower->{borrowernumber}, |
90 |
attribute => 'FOO' |
101 |
code => $attr_type->{code}, |
|
|
102 |
attribute => 'FOO' |
103 |
} |
91 |
} |
104 |
} |
92 |
}); |
105 |
); |
93 |
|
106 |
|
94 |
# C4::Auth_with_ldap needs several stuff set first ^^^ |
107 |
# C4::Auth_with_ldap needs several stuff set first ^^^ |
95 |
use_ok( 'C4::Auth_with_ldap' ); |
108 |
use_ok('C4::Auth_with_ldap'); |
96 |
can_ok( 'C4::Auth_with_ldap', qw/ |
109 |
can_ok( |
97 |
checkpw_ldap |
110 |
'C4::Auth_with_ldap', qw/ |
98 |
search_method /); |
111 |
checkpw_ldap |
|
|
112 |
search_method / |
113 |
); |
99 |
|
114 |
|
100 |
subtest "checkpw_ldap tests" => sub { |
115 |
subtest 'checkpw_ldap tests' => sub { |
101 |
|
116 |
|
102 |
plan tests => 4; |
117 |
plan tests => 4; |
103 |
|
118 |
|
104 |
## Connection fail tests |
119 |
## Connection fail tests |
105 |
$desired_connection_result = 'error'; |
120 |
$desired_connection_result = 'error'; |
106 |
warning_is { $ret = C4::Auth_with_ldap::checkpw_ldap( $dbh, 'hola', password => 'hey' ) } |
121 |
warning_is { |
107 |
"LDAP connexion failed", |
122 |
$ret = |
108 |
"checkpw_ldap prints correct warning if LDAP conexion fails"; |
123 |
C4::Auth_with_ldap::checkpw_ldap( $dbh, 'hola', password => 'hey' ); |
109 |
is( $ret, 0, "checkpw_ldap returns 0 if LDAP conexion fails"); |
124 |
} |
|
|
125 |
'LDAP connexion failed', |
126 |
'checkpw_ldap prints correct warning if LDAP conexion fails'; |
127 |
is( $ret, 0, 'checkpw_ldap returns 0 if LDAP conexion fails' ); |
110 |
|
128 |
|
111 |
## Connection success tests |
129 |
## Connection success tests |
112 |
$desired_connection_result = 'success'; |
130 |
$desired_connection_result = 'success'; |
113 |
|
131 |
|
114 |
subtest "auth_by_bind = 1 tests" => sub { |
132 |
subtest 'auth_by_bind = 1 tests' => sub { |
115 |
|
133 |
|
116 |
plan tests => 8; |
134 |
plan tests => 8; |
117 |
|
135 |
|
118 |
$auth_by_bind = 1; |
136 |
$auth_by_bind = 1; |
119 |
|
137 |
|
120 |
$desired_authentication_result = 'success'; |
138 |
$desired_authentication_result = 'success'; |
121 |
$anonymous_bind = 1; |
139 |
$anonymous_bind = 1; |
122 |
$desired_bind_result = 'error'; |
140 |
$desired_bind_result = 'error'; |
123 |
$desired_search_result = 'error'; |
141 |
$desired_search_result = 'error'; |
124 |
reload_ldap_module(); |
142 |
reload_ldap_module(); |
125 |
|
143 |
|
126 |
|
144 |
warning_like { |
127 |
warning_like { $ret = C4::Auth_with_ldap::checkpw_ldap( |
145 |
$ret = C4::Auth_with_ldap::checkpw_ldap( $dbh, 'hola', |
128 |
$dbh, 'hola', password => 'hey' ) } |
146 |
password => 'hey' ); |
129 |
qr/Anonymous LDAP bind failed: LDAP error #1: error_name/, |
147 |
} |
130 |
"checkpw_ldap prints correct warning if LDAP anonymous bind fails"; |
148 |
qr/Anonymous LDAP bind failed: LDAP error #1: error_name/, |
131 |
is( $ret, 0, "checkpw_ldap returns 0 if LDAP anonymous bind fails"); |
149 |
'checkpw_ldap prints correct warning if LDAP anonymous bind fails'; |
|
|
150 |
is( $ret, 0, 'checkpw_ldap returns 0 if LDAP anonymous bind fails' ); |
132 |
|
151 |
|
133 |
$desired_authentication_result = 'success'; |
152 |
$desired_authentication_result = 'success'; |
134 |
$anonymous_bind = 1; |
153 |
$anonymous_bind = 1; |
135 |
$desired_bind_result = 'success'; |
154 |
$desired_bind_result = 'success'; |
136 |
$desired_search_result = 'success'; |
155 |
$desired_search_result = 'success'; |
137 |
$desired_count_result = 1; |
156 |
$desired_count_result = 1; |
138 |
$non_anonymous_bind_result = 'success'; |
157 |
$non_anonymous_bind_result = 'success'; |
139 |
$update = 1; |
158 |
$update = 1; |
140 |
reload_ldap_module(); |
159 |
reload_ldap_module(); |
141 |
|
160 |
|
142 |
my $auth = new Test::MockModule('C4::Auth_with_ldap'); |
161 |
my $auth = Test::MockModule->new('C4::Auth_with_ldap'); |
143 |
$auth->mock( 'update_local', sub { |
162 |
$auth->mock( |
|
|
163 |
'update_local', |
164 |
sub { |
144 |
return $borrower->{cardnumber}; |
165 |
return $borrower->{cardnumber}; |
145 |
}); |
166 |
} |
|
|
167 |
); |
146 |
|
168 |
|
147 |
C4::Auth_with_ldap::checkpw_ldap( $dbh, 'hola', password => 'hey'); |
169 |
C4::Auth_with_ldap::checkpw_ldap( $dbh, 'hola', password => 'hey' ); |
148 |
ok(@{ C4::Members::Attributes::GetBorrowerAttributes($borrower->{borrowernumber}) }, 'Extended attributes are not deleted'); |
170 |
ok( |
|
|
171 |
@{ |
172 |
C4::Members::Attributes::GetBorrowerAttributes( |
173 |
$borrower->{borrowernumber} |
174 |
) |
175 |
}, |
176 |
'Extended attributes are not deleted' |
177 |
); |
149 |
$auth->unmock('update_local'); |
178 |
$auth->unmock('update_local'); |
150 |
|
179 |
|
151 |
$update = 0; |
180 |
$update = 0; |
152 |
$desired_count_result = 0; # user auth problem |
181 |
$desired_count_result = 0; # user auth problem |
153 |
C4::Members::DelMember($borrower->{borrowernumber}); |
182 |
C4::Members::DelMember( $borrower->{borrowernumber} ); |
154 |
reload_ldap_module(); |
183 |
reload_ldap_module(); |
155 |
is ( C4::Auth_with_ldap::checkpw_ldap( $dbh, 'hola', password => 'hey' ), |
184 |
is( |
156 |
0, "checkpw_ldap returns 0 if user lookup returns 0"); |
185 |
C4::Auth_with_ldap::checkpw_ldap( $dbh, 'hola', password => 'hey' ), |
|
|
186 |
0, |
187 |
'checkpw_ldap returns 0 if user lookup returns 0' |
188 |
); |
157 |
|
189 |
|
158 |
$non_anonymous_bind_result = 'error'; |
190 |
$non_anonymous_bind_result = 'error'; |
159 |
reload_ldap_module(); |
191 |
reload_ldap_module(); |
160 |
|
192 |
|
161 |
warning_like { $ret = C4::Auth_with_ldap::checkpw_ldap( |
193 |
warning_like { |
162 |
$dbh, 'hola', password => 'hey' ) } |
194 |
$ret = C4::Auth_with_ldap::checkpw_ldap( $dbh, 'hola', |
163 |
qr/LDAP bind failed as kohauser hola: LDAP error #1: error_name/, |
195 |
password => 'hey' ); |
164 |
"checkpw_ldap prints correct warning if LDAP bind fails"; |
196 |
} |
165 |
is ( $ret, -1, "checkpw_ldap returns -1 LDAP bind fails for user (Bug 8148)"); |
197 |
qr/LDAP bind failed as kohauser hola: LDAP error #1: error_name/, |
|
|
198 |
'checkpw_ldap prints correct warning if LDAP bind fails'; |
199 |
is( $ret, -1, |
200 |
'checkpw_ldap returns -1 LDAP bind fails for user (Bug 8148)' ); |
166 |
|
201 |
|
167 |
# regression tests for bug 12831 |
202 |
# regression tests for bug 12831 |
168 |
$desired_authentication_result = 'error'; |
203 |
$desired_authentication_result = 'error'; |
169 |
$anonymous_bind = 0; |
204 |
$anonymous_bind = 0; |
170 |
$desired_bind_result = 'error'; |
205 |
$desired_bind_result = 'error'; |
171 |
$desired_search_result = 'success'; |
206 |
$desired_search_result = 'success'; |
172 |
$desired_count_result = 0; # user auth problem |
207 |
$desired_count_result = 0; # user auth problem |
173 |
$non_anonymous_bind_result = 'error'; |
208 |
$non_anonymous_bind_result = 'error'; |
174 |
reload_ldap_module(); |
209 |
reload_ldap_module(); |
175 |
|
210 |
|
176 |
warning_like { $ret = C4::Auth_with_ldap::checkpw_ldap( |
211 |
warning_like { |
177 |
$dbh, 'hola', password => 'hey' ) } |
212 |
$ret = C4::Auth_with_ldap::checkpw_ldap( $dbh, 'hola', |
178 |
qr/LDAP bind failed as kohauser hola: LDAP error #1: error_name/, |
213 |
password => 'hey' ); |
179 |
"checkpw_ldap prints correct warning if LDAP bind fails"; |
214 |
} |
180 |
is ( $ret, 0, "checkpw_ldap returns 0 LDAP bind fails for user (Bug 12831)"); |
215 |
qr/LDAP bind failed as kohauser hola: LDAP error #1: error_name/, |
|
|
216 |
'checkpw_ldap prints correct warning if LDAP bind fails'; |
217 |
is( $ret, 0, |
218 |
'checkpw_ldap returns 0 LDAP bind fails for user (Bug 12831)' ); |
181 |
|
219 |
|
182 |
}; |
220 |
}; |
183 |
|
221 |
|
184 |
subtest "auth_by_bind = 0 tests" => sub { |
222 |
subtest 'auth_by_bind = 0 tests' => sub { |
185 |
|
223 |
|
186 |
plan tests => 8; |
224 |
plan tests => 8; |
187 |
|
225 |
|
Lines 193-203
subtest "checkpw_ldap tests" => sub {
Link Here
|
193 |
$non_anonymous_bind_result = 'error'; |
231 |
$non_anonymous_bind_result = 'error'; |
194 |
reload_ldap_module(); |
232 |
reload_ldap_module(); |
195 |
|
233 |
|
196 |
warning_like { $ret = C4::Auth_with_ldap::checkpw_ldap( |
234 |
warning_like { |
197 |
$dbh, 'hola', password => 'hey' ) } |
235 |
$ret = C4::Auth_with_ldap::checkpw_ldap( $dbh, 'hola', |
198 |
qr/LDAP bind failed as ldapuser cn=Manager,dc=metavore,dc=com: LDAP error #1: error_name/, |
236 |
password => 'hey' ); |
199 |
"checkpw_ldap prints correct warning if LDAP bind fails"; |
237 |
} |
200 |
is ( $ret, 0, "checkpw_ldap returns 0 if bind fails"); |
238 |
qr/LDAP bind failed as ldapuser cn=Manager,dc=metavore,dc=com: LDAP error #1: error_name/, |
|
|
239 |
'checkpw_ldap prints correct warning if LDAP bind fails'; |
240 |
is( $ret, 0, 'checkpw_ldap returns 0 if bind fails' ); |
201 |
|
241 |
|
202 |
$anonymous_bind = 1; |
242 |
$anonymous_bind = 1; |
203 |
$desired_bind_result = 'success'; |
243 |
$desired_bind_result = 'success'; |
Lines 205-215
subtest "checkpw_ldap tests" => sub {
Link Here
|
205 |
$desired_compare_result = 'error'; |
245 |
$desired_compare_result = 'error'; |
206 |
reload_ldap_module(); |
246 |
reload_ldap_module(); |
207 |
|
247 |
|
208 |
warning_like { $ret = C4::Auth_with_ldap::checkpw_ldap( |
248 |
warning_like { |
209 |
$dbh, 'hola', password => 'hey' ) } |
249 |
$ret = C4::Auth_with_ldap::checkpw_ldap( $dbh, 'hola', |
210 |
qr/LDAP Auth rejected : invalid password for user 'hola'. LDAP error #1: error_name/, |
250 |
password => 'hey' ); |
211 |
"checkpw_ldap prints correct warning if LDAP bind fails"; |
251 |
} |
212 |
is ( $ret, -1, "checkpw_ldap returns -1 if bind fails (Bug 8148)"); |
252 |
qr/LDAP Auth rejected : invalid password for user 'hola'. LDAP error #1: error_name/, |
|
|
253 |
'checkpw_ldap prints correct warning if LDAP bind fails'; |
254 |
is( $ret, -1, 'checkpw_ldap returns -1 if bind fails (Bug 8148)' ); |
213 |
|
255 |
|
214 |
# Non-anonymous bind |
256 |
# Non-anonymous bind |
215 |
$anonymous_bind = 0; |
257 |
$anonymous_bind = 0; |
Lines 218-228
subtest "checkpw_ldap tests" => sub {
Link Here
|
218 |
$desired_compare_result = 'dont care'; |
260 |
$desired_compare_result = 'dont care'; |
219 |
reload_ldap_module(); |
261 |
reload_ldap_module(); |
220 |
|
262 |
|
221 |
warning_like { $ret = C4::Auth_with_ldap::checkpw_ldap( |
263 |
warning_like { |
222 |
$dbh, 'hola', password => 'hey' ) } |
264 |
$ret = C4::Auth_with_ldap::checkpw_ldap( $dbh, 'hola', |
223 |
qr/LDAP bind failed as ldapuser cn=Manager,dc=metavore,dc=com: LDAP error #1: error_name/, |
265 |
password => 'hey' ); |
224 |
"checkpw_ldap prints correct warning if LDAP bind fails"; |
266 |
} |
225 |
is ( $ret, 0, "checkpw_ldap returns 0 if bind fails"); |
267 |
qr/LDAP bind failed as ldapuser cn=Manager,dc=metavore,dc=com: LDAP error #1: error_name/, |
|
|
268 |
'checkpw_ldap prints correct warning if LDAP bind fails'; |
269 |
is( $ret, 0, 'checkpw_ldap returns 0 if bind fails' ); |
226 |
|
270 |
|
227 |
$anonymous_bind = 0; |
271 |
$anonymous_bind = 0; |
228 |
$desired_bind_result = 'success'; |
272 |
$desired_bind_result = 'success'; |
Lines 230-282
subtest "checkpw_ldap tests" => sub {
Link Here
|
230 |
$desired_compare_result = 'error'; |
274 |
$desired_compare_result = 'error'; |
231 |
reload_ldap_module(); |
275 |
reload_ldap_module(); |
232 |
|
276 |
|
233 |
warning_like { $ret = C4::Auth_with_ldap::checkpw_ldap( |
277 |
warning_like { |
234 |
$dbh, 'hola', password => 'hey' ) } |
278 |
$ret = C4::Auth_with_ldap::checkpw_ldap( $dbh, 'hola', |
235 |
qr/LDAP Auth rejected : invalid password for user 'hola'. LDAP error #1: error_name/, |
279 |
password => 'hey' ); |
236 |
"checkpw_ldap prints correct warning if LDAP bind fails"; |
280 |
} |
237 |
is ( $ret, -1, "checkpw_ldap returns -1 if bind fails (Bug 8148)"); |
281 |
qr/LDAP Auth rejected : invalid password for user 'hola'. LDAP error #1: error_name/, |
|
|
282 |
'checkpw_ldap prints correct warning if LDAP bind fails'; |
283 |
is( $ret, -1, 'checkpw_ldap returns -1 if bind fails (Bug 8148)' ); |
238 |
|
284 |
|
239 |
}; |
285 |
}; |
240 |
}; |
286 |
}; |
241 |
|
287 |
|
242 |
subtest "search_method tests" => sub { |
288 |
subtest 'search_method tests' => sub { |
243 |
|
289 |
|
244 |
plan tests => 5; |
290 |
plan tests => 5; |
245 |
|
291 |
|
246 |
my $ldap = mock_net_ldap(); |
292 |
my $ldap = mock_net_ldap(); |
247 |
|
293 |
|
248 |
# Null params tests |
294 |
# Null params tests |
249 |
is( C4::Auth_with_ldap::search_method( $ldap, undef), undef, |
295 |
is( C4::Auth_with_ldap::search_method( $ldap, undef ), |
250 |
"search_method returns undef on undefined userid"); |
296 |
undef, 'search_method returns undef on undefined userid' ); |
251 |
is( C4::Auth_with_ldap::search_method( undef, "undef"), undef, |
297 |
is( C4::Auth_with_ldap::search_method( undef, 'undef' ), |
252 |
"search_method returns undef on undefined ldap object"); |
298 |
undef, 'search_method returns undef on undefined ldap object' ); |
253 |
|
299 |
|
254 |
# search ->code and !->code |
300 |
# search ->code and !->code |
255 |
$desired_search_result = 'error'; |
301 |
$desired_search_result = 'error'; |
256 |
reload_ldap_module(); |
302 |
reload_ldap_module(); |
257 |
eval { $ret = C4::Auth_with_ldap::search_method( $ldap, "undef"); }; |
303 |
my $eval_retval = |
258 |
like( $@, qr/LDAP search failed to return object : 1/, |
304 |
eval { $ret = C4::Auth_with_ldap::search_method( $ldap, 'undef' ); }; |
259 |
"search_method prints correct warning when db->search returns error code"); |
305 |
like( |
|
|
306 |
$@, |
307 |
qr/LDAP search failed to return object : 1/, |
308 |
'search_method prints correct warning when db->search returns error code' |
309 |
); |
260 |
|
310 |
|
261 |
$desired_search_result = 'success'; |
311 |
$desired_search_result = 'success'; |
262 |
$desired_count_result = 2; |
312 |
$desired_count_result = 2; |
263 |
reload_ldap_module(); |
313 |
reload_ldap_module(); |
264 |
warning_like { $ret = C4::Auth_with_ldap::search_method( $ldap, '123') } |
314 |
warning_like { $ret = C4::Auth_with_ldap::search_method( $ldap, '123' ) } |
265 |
qr/^LDAP Auth rejected \: \(uid\=123\) gets 2 hits/, |
315 |
qr/^LDAP Auth rejected \: \(uid\=123\) gets 2 hits/, |
266 |
"search_method prints correct warning when hits count is not 1"; |
316 |
'search_method prints correct warning when hits count is not 1'; |
267 |
is( $ret, 0, "search_method returns 0 when hits count is not 1" ); |
317 |
is( $ret, 0, 'search_method returns 0 when hits count is not 1' ); |
268 |
|
318 |
|
269 |
}; |
319 |
}; |
270 |
|
320 |
|
271 |
# Function that mocks the call to C4::Context->config(param) |
321 |
# Function that mocks the call to C4::Context->config(param) |
272 |
sub mockedC4Config { |
322 |
sub mockedC4Config { |
273 |
my $class = shift; |
323 |
my $class = shift; |
274 |
my $param = shift; |
324 |
my $param = shift; |
275 |
|
325 |
|
276 |
if ($param eq 'useshibboleth') { |
326 |
if ( $param eq 'useshibboleth' ) { |
277 |
return 0; |
327 |
return 0; |
278 |
} |
328 |
} |
279 |
elsif ($param eq 'ldapserver') { |
329 |
if ( $param eq 'ldapserver' ) { |
280 |
my %ldap_mapping = ( |
330 |
my %ldap_mapping = ( |
281 |
firstname => { is => 'givenname' }, |
331 |
firstname => { is => 'givenname' }, |
282 |
surname => { is => 'sn' }, |
332 |
surname => { is => 'sn' }, |
Lines 288-297
sub mockedC4Config {
Link Here
|
288 |
password => { is => 'userpassword' }, |
338 |
password => { is => 'userpassword' }, |
289 |
email => { is => 'mail' }, |
339 |
email => { is => 'mail' }, |
290 |
categorycode => { is => 'employeetype' }, |
340 |
categorycode => { is => 'employeetype' }, |
291 |
phone => { is => 'telephonenumber' } |
341 |
phone => { is => 'telephonenumber' }, |
292 |
); |
342 |
); |
293 |
|
343 |
|
294 |
my %ldap_config = ( |
344 |
my %ldap_config = ( |
295 |
anonymous_bind => $anonymous_bind, |
345 |
anonymous_bind => $anonymous_bind, |
296 |
auth_by_bind => $auth_by_bind, |
346 |
auth_by_bind => $auth_by_bind, |
297 |
base => 'dc=metavore,dc=com', |
347 |
base => 'dc=metavore,dc=com', |
Lines 301-393
sub mockedC4Config {
Link Here
|
301 |
principal_name => '%s@my_domain.com', |
351 |
principal_name => '%s@my_domain.com', |
302 |
replicate => $replicate, |
352 |
replicate => $replicate, |
303 |
update => $update, |
353 |
update => $update, |
304 |
user => 'cn=Manager,dc=metavore,dc=com' |
354 |
user => 'cn=Manager,dc=metavore,dc=com', |
305 |
); |
355 |
); |
306 |
return \%ldap_config; |
356 |
return \%ldap_config; |
307 |
} |
357 |
} |
308 |
elsif ($param =~ /(intranetdir|opachtdocs|intrahtdocs)/ ) { |
358 |
if ( $param =~ /(intranetdir|opachtdocs|intrahtdocs)/x ) { |
309 |
return ''; |
359 |
return q{}; |
310 |
} |
360 |
} |
311 |
elsif (ref $class eq 'HASH') { |
361 |
if ( ref $class eq 'HASH' ) { |
312 |
return $class->{$param}; |
362 |
return $class->{$param}; |
313 |
} |
363 |
} |
314 |
else { |
364 |
return; |
315 |
return; |
365 |
} |
316 |
} |
|
|
317 |
}; |
318 |
|
366 |
|
319 |
# Function that mocks the call to Net::LDAP |
367 |
# Function that mocks the call to Net::LDAP |
320 |
sub mock_net_ldap { |
368 |
sub mock_net_ldap { |
321 |
|
369 |
|
322 |
my $mocked_ldap = Test::MockObject->new(); |
370 |
my $mocked_ldap = Test::MockObject->new(); |
323 |
|
371 |
|
324 |
$mocked_ldap->mock( 'bind', sub { |
372 |
$mocked_ldap->mock( |
|
|
373 |
'bind', |
374 |
sub { |
325 |
|
375 |
|
326 |
my @args = @_; |
376 |
my @args = @_; |
327 |
my $mocked_message; |
377 |
my $mocked_message; |
328 |
|
378 |
|
329 |
if ( $#args > 1 ) { |
379 |
if ( $#args > 1 ) { |
330 |
# Args passed => non-anonymous bind |
380 |
|
331 |
if ( $non_anonymous_bind_result eq 'error' ) { |
381 |
# Args passed => non-anonymous bind |
332 |
return mock_net_ldap_message(1,1,'error_name','error_text'); |
382 |
if ( $non_anonymous_bind_result eq 'error' ) { |
333 |
} else { |
383 |
return mock_net_ldap_message( 1, 1, 'error_name', |
334 |
return mock_net_ldap_message(0,0,'',''); |
384 |
'error_text' ); |
|
|
385 |
} |
386 |
else { |
387 |
return mock_net_ldap_message( 0, 0, q{}, q{} ); |
388 |
} |
335 |
} |
389 |
} |
336 |
} else { |
390 |
else { |
337 |
$mocked_message = mock_net_ldap_message( |
391 |
$mocked_message = mock_net_ldap_message( |
338 |
($desired_bind_result eq 'error' ) ? 1 : 0, # code |
392 |
( $desired_bind_result eq 'error' ) ? 1 : 0, # code |
339 |
($desired_bind_result eq 'error' ) ? 1 : 0, # error |
393 |
( $desired_bind_result eq 'error' ) ? 1 : 0, # error |
340 |
($desired_bind_result eq 'error' ) ? 'error_name' : 0, # error_name |
394 |
( $desired_bind_result eq 'error' ) |
341 |
($desired_bind_result eq 'error' ) ? 'error_text' : 0 # error_text |
395 |
? 'error_name' |
342 |
); |
396 |
: 0, # error_name |
|
|
397 |
( $desired_bind_result eq 'error' ) |
398 |
? 'error_text' |
399 |
: 0 # error_text |
400 |
); |
401 |
} |
402 |
|
403 |
return $mocked_message; |
343 |
} |
404 |
} |
|
|
405 |
); |
344 |
|
406 |
|
345 |
return $mocked_message; |
407 |
$mocked_ldap->mock( |
346 |
}); |
408 |
'compare', |
|
|
409 |
sub { |
347 |
|
410 |
|
348 |
$mocked_ldap->mock( 'compare', sub { |
411 |
my $mocked_message; |
349 |
|
412 |
|
350 |
my $mocked_message; |
413 |
if ( $desired_compare_result eq 'error' ) { |
|
|
414 |
$mocked_message = |
415 |
mock_net_ldap_message( 1, 1, 'error_name', 'error_text' ); |
416 |
} |
417 |
else { |
418 |
# we expect return code 6 for success |
419 |
$mocked_message = mock_net_ldap_message( 6, 0, q{}, q{} ); |
420 |
} |
351 |
|
421 |
|
352 |
if ( $desired_compare_result eq 'error' ) { |
422 |
return $mocked_message; |
353 |
$mocked_message = mock_net_ldap_message(1,1,'error_name','error_text'); |
|
|
354 |
} else { |
355 |
# we expect return code 6 for success |
356 |
$mocked_message = mock_net_ldap_message(6,0,'',''); |
357 |
} |
423 |
} |
|
|
424 |
); |
425 |
|
426 |
$mocked_ldap->mock( |
427 |
'search', |
428 |
sub { |
429 |
|
430 |
return mock_net_ldap_search( |
431 |
{ |
432 |
count => ($desired_count_result) |
433 |
? $desired_count_result |
434 |
: 1, # default to 1 |
435 |
code => ( $desired_search_result eq 'error' ) |
436 |
? 1 |
437 |
: 0, # 0 == success |
438 |
error => ( $desired_search_result eq 'error' ) ? 1 |
439 |
: 0, |
440 |
error_text => ( $desired_search_result eq 'error' ) |
441 |
? 'error_text' |
442 |
: undef, |
443 |
error_name => ( $desired_search_result eq 'error' ) |
444 |
? 'error_name' |
445 |
: undef, |
446 |
shift_entry => mock_net_ldap_entry( 'sampledn', 1 ) |
447 |
} |
448 |
); |
358 |
|
449 |
|
359 |
return $mocked_message; |
450 |
} |
360 |
}); |
451 |
); |
361 |
|
|
|
362 |
$mocked_ldap->mock( 'search', sub { |
363 |
|
364 |
return mock_net_ldap_search( |
365 |
( $desired_count_result ) # count |
366 |
? $desired_count_result |
367 |
: 1, # default to 1 |
368 |
( $desired_search_result eq 'error' ) # code |
369 |
? 1 |
370 |
: 0, # 0 == success |
371 |
( $desired_search_result eq 'error' ) # error |
372 |
? 1 |
373 |
: 0, |
374 |
( $desired_search_result eq 'error' ) # error_text |
375 |
? 'error_text' |
376 |
: undef, |
377 |
( $desired_search_result eq 'error' ) # error_name |
378 |
? 'error_name' |
379 |
: undef, |
380 |
mock_net_ldap_entry( 'sampledn', 1 ) # shift_entry |
381 |
); |
382 |
|
383 |
}); |
384 |
|
452 |
|
385 |
return $mocked_ldap; |
453 |
return $mocked_ldap; |
386 |
} |
454 |
} |
387 |
|
455 |
|
388 |
sub mock_net_ldap_search { |
456 |
sub mock_net_ldap_search { |
389 |
my ( $count, $code, $error, $error_text, |
457 |
my ($parameters) = @_; |
390 |
$error_name, $shift_entry ) = @_; |
458 |
|
|
|
459 |
my $count = $parameters->{count}; |
460 |
my $code = $parameters->{code}; |
461 |
my $error = $parameters->{error}; |
462 |
my $error_text = $parameters->{error_text}; |
463 |
my $error_name = $parameters->{error_name}; |
464 |
my $shift_entry = $parameters->{shift_entry}; |
391 |
|
465 |
|
392 |
my $mocked_search = Test::MockObject->new(); |
466 |
my $mocked_search = Test::MockObject->new(); |
393 |
$mocked_search->mock( 'count', sub { return $count; } ); |
467 |
$mocked_search->mock( 'count', sub { return $count; } ); |
Lines 429-434
sub reload_ldap_module {
Link Here
|
429 |
delete $INC{'C4/Auth_with_ldap.pm'}; |
503 |
delete $INC{'C4/Auth_with_ldap.pm'}; |
430 |
require C4::Auth_with_ldap; |
504 |
require C4::Auth_with_ldap; |
431 |
C4::Auth_with_ldap->import; |
505 |
C4::Auth_with_ldap->import; |
|
|
506 |
return; |
432 |
} |
507 |
} |
433 |
|
508 |
|
434 |
$dbh->rollback; |
509 |
$dbh->rollback; |
435 |
- |
|
|