3 # This file is part of Koha.
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
20 use Test::More tests => 4;
23 use t::lib::TestBuilder;
30 my $dbh = C4::Context->dbh;
33 $dbh->{AutoCommit} = 0;
34 $dbh->{RaiseError} = 1;
36 my $builder = t::lib::TestBuilder->new();
38 # Variables controlling LDAP server config
42 my $anonymous_bind = 1;
44 # Variables controlling LDAP behaviour
45 my $desired_authentication_result = 'success';
46 my $desired_connection_result = 'error';
47 my $desired_bind_result = 'error';
48 my $desired_compare_result = 'error';
49 my $desired_search_result = 'error';
50 my $desired_count_result = 1;
51 my $non_anonymous_bind_result = 'error';
54 # Mock the context module
55 my $context = Test::MockModule->new('C4::Context');
56 $context->mock( 'config', \&mockedC4Config );
58 # Mock the Net::LDAP module
59 my $net_ldap = Test::MockModule->new('Net::LDAP');
64 if ( $desired_connection_result eq 'error' ) {
66 # We were asked to fail the LDAP conexion
70 # Return a mocked Net::LDAP object (Test::MockObject)
71 return mock_net_ldap();
76 my $categorycode = $builder->build( { source => 'Category' } )->{categorycode};
77 my $branchcode = $builder->build( { source => 'Branch' } )->{branchcode};
78 my $attr_type = $builder->build(
80 source => 'BorrowerAttributeType',
82 category_code => $categorycode
87 my $borrower = $builder->build(
92 branchcode => $branchcode,
93 categorycode => $categorycode
100 source => 'BorrowerAttribute',
102 borrowernumber => $borrower->{borrowernumber},
103 code => $attr_type->{code},
109 # C4::Auth_with_ldap needs several stuff set first ^^^
110 use_ok('C4::Auth_with_ldap');
112 'C4::Auth_with_ldap', qw/
117 subtest 'checkpw_ldap tests' => sub {
121 ## Connection fail tests
122 $desired_connection_result = 'error';
125 C4::Auth_with_ldap::checkpw_ldap( $dbh, 'hola', password => 'hey' );
127 'LDAP connexion failed',
128 'checkpw_ldap prints correct warning if LDAP conexion fails';
129 is( $ret, 0, 'checkpw_ldap returns 0 if LDAP conexion fails' );
131 ## Connection success tests
132 $desired_connection_result = 'success';
134 subtest 'auth_by_bind = 1 tests' => sub {
140 $desired_authentication_result = 'success';
142 $desired_bind_result = 'error';
143 $desired_search_result = 'error';
144 reload_ldap_module();
147 $ret = C4::Auth_with_ldap::checkpw_ldap( $dbh, 'hola',
150 qr/Anonymous LDAP bind failed: LDAP error #1: error_name/,
151 'checkpw_ldap prints correct warning if LDAP anonymous bind fails';
152 is( $ret, 0, 'checkpw_ldap returns 0 if LDAP anonymous bind fails' );
154 $desired_authentication_result = 'success';
156 $desired_bind_result = 'success';
157 $desired_search_result = 'success';
158 $desired_count_result = 1;
159 $non_anonymous_bind_result = 'success';
161 reload_ldap_module();
163 my $auth = Test::MockModule->new('C4::Auth_with_ldap');
167 return $borrower->{cardnumber};
171 C4::Auth_with_ldap::checkpw_ldap( $dbh, 'hola', password => 'hey' );
174 C4::Members::Attributes::GetBorrowerAttributes(
175 $borrower->{borrowernumber}
178 'Extended attributes are not deleted'
180 $auth->unmock('update_local');
183 $desired_count_result = 0; # user auth problem
184 Koha::Patrons->find( $borrower->{borrowernumber} )->delete;
185 reload_ldap_module();
187 C4::Auth_with_ldap::checkpw_ldap( $dbh, 'hola', password => 'hey' ),
189 'checkpw_ldap returns 0 if user lookup returns 0'
192 $non_anonymous_bind_result = 'error';
193 reload_ldap_module();
196 $ret = C4::Auth_with_ldap::checkpw_ldap( $dbh, 'hola',
199 qr/LDAP bind failed as kohauser hola: LDAP error #1: error_name/,
200 'checkpw_ldap prints correct warning if LDAP bind fails';
202 'checkpw_ldap returns -1 LDAP bind fails for user (Bug 8148)' );
204 # regression tests for bug 12831
205 $desired_authentication_result = 'error';
207 $desired_bind_result = 'error';
208 $desired_search_result = 'success';
209 $desired_count_result = 0; # user auth problem
210 $non_anonymous_bind_result = 'error';
211 reload_ldap_module();
214 $ret = C4::Auth_with_ldap::checkpw_ldap( $dbh, 'hola',
217 qr/LDAP bind failed as kohauser hola: LDAP error #1: error_name/,
218 'checkpw_ldap prints correct warning if LDAP bind fails';
220 'checkpw_ldap returns 0 LDAP bind fails for user (Bug 12831)' );
224 subtest 'auth_by_bind = 0 tests' => sub {
232 $desired_bind_result = 'error';
233 $non_anonymous_bind_result = 'error';
234 reload_ldap_module();
237 $ret = C4::Auth_with_ldap::checkpw_ldap( $dbh, 'hola',
240 qr/LDAP bind failed as ldapuser cn=Manager,dc=metavore,dc=com: LDAP error #1: error_name/,
241 'checkpw_ldap prints correct warning if LDAP bind fails';
242 is( $ret, 0, 'checkpw_ldap returns 0 if bind fails' );
245 $desired_bind_result = 'success';
246 $non_anonymous_bind_result = 'success';
247 $desired_compare_result = 'error';
248 reload_ldap_module();
251 $ret = C4::Auth_with_ldap::checkpw_ldap( $dbh, 'hola',
254 qr/LDAP Auth rejected : invalid password for user 'hola'. LDAP error #1: error_name/,
255 'checkpw_ldap prints correct warning if LDAP bind fails';
256 is( $ret, -1, 'checkpw_ldap returns -1 if bind fails (Bug 8148)' );
260 $desired_bind_result = 'success';
261 $non_anonymous_bind_result = 'error';
262 $desired_compare_result = 'dont care';
263 reload_ldap_module();
266 $ret = C4::Auth_with_ldap::checkpw_ldap( $dbh, 'hola',
269 qr/LDAP bind failed as ldapuser cn=Manager,dc=metavore,dc=com: LDAP error #1: error_name/,
270 'checkpw_ldap prints correct warning if LDAP bind fails';
271 is( $ret, 0, 'checkpw_ldap returns 0 if bind fails' );
274 $desired_bind_result = 'success';
275 $non_anonymous_bind_result = 'success';
276 $desired_compare_result = 'error';
277 reload_ldap_module();
280 $ret = C4::Auth_with_ldap::checkpw_ldap( $dbh, 'hola',
283 qr/LDAP Auth rejected : invalid password for user 'hola'. LDAP error #1: error_name/,
284 'checkpw_ldap prints correct warning if LDAP bind fails';
285 is( $ret, -1, 'checkpw_ldap returns -1 if bind fails (Bug 8148)' );
290 subtest 'search_method tests' => sub {
294 my $ldap = mock_net_ldap();
297 is( C4::Auth_with_ldap::search_method( $ldap, undef ),
298 undef, 'search_method returns undef on undefined userid' );
299 is( C4::Auth_with_ldap::search_method( undef, 'undef' ),
300 undef, 'search_method returns undef on undefined ldap object' );
302 # search ->code and !->code
303 $desired_search_result = 'error';
304 reload_ldap_module();
306 eval { $ret = C4::Auth_with_ldap::search_method( $ldap, 'undef' ); };
309 qr/LDAP search failed to return object : 1/,
310 'search_method prints correct warning when db->search returns error code'
313 $desired_search_result = 'success';
314 $desired_count_result = 2;
315 reload_ldap_module();
316 warning_like { $ret = C4::Auth_with_ldap::search_method( $ldap, '123' ) }
317 qr/^LDAP Auth rejected \: \(uid\=123\) gets 2 hits/,
318 'search_method prints correct warning when hits count is not 1';
319 is( $ret, 0, 'search_method returns 0 when hits count is not 1' );
323 # Function that mocks the call to C4::Context->config(param)
328 if ( $param eq 'useshibboleth' ) {
331 if ( $param eq 'ldapserver' ) {
333 firstname => { is => 'givenname' },
334 surname => { is => 'sn' },
335 address => { is => 'postaladdress' },
336 city => { is => 'l' },
337 zipcode => { is => 'postalcode' },
338 branchcode => { is => 'branch' },
339 userid => { is => 'uid' },
340 password => { is => 'userpassword' },
341 email => { is => 'mail' },
342 categorycode => { is => 'employeetype' },
343 phone => { is => 'telephonenumber' },
347 anonymous_bind => $anonymous_bind,
348 auth_by_bind => $auth_by_bind,
349 base => 'dc=metavore,dc=com',
350 hostname => 'localhost',
351 mapping => \%ldap_mapping,
353 principal_name => '%s@my_domain.com',
354 replicate => $replicate,
356 user => 'cn=Manager,dc=metavore,dc=com',
358 return \%ldap_config;
360 if ( $param =~ /(intranetdir|opachtdocs|intrahtdocs)/x ) {
363 if ( ref $class eq 'HASH' ) {
364 return $class->{$param};
369 # Function that mocks the call to Net::LDAP
372 my $mocked_ldap = Test::MockObject->new();
383 # Args passed => non-anonymous bind
384 if ( $non_anonymous_bind_result eq 'error' ) {
385 return mock_net_ldap_message( 1, 1, 'error_name',
389 return mock_net_ldap_message( 0, 0, q{}, q{} );
393 $mocked_message = mock_net_ldap_message(
394 ( $desired_bind_result eq 'error' ) ? 1 : 0, # code
395 ( $desired_bind_result eq 'error' ) ? 1 : 0, # error
396 ( $desired_bind_result eq 'error' )
399 ( $desired_bind_result eq 'error' )
405 return $mocked_message;
415 if ( $desired_compare_result eq 'error' ) {
417 mock_net_ldap_message( 1, 1, 'error_name', 'error_text' );
420 # we expect return code 6 for success
421 $mocked_message = mock_net_ldap_message( 6, 0, q{}, q{} );
424 return $mocked_message;
432 return mock_net_ldap_search(
434 count => ($desired_count_result)
435 ? $desired_count_result
437 code => ( $desired_search_result eq 'error' )
440 error => ( $desired_search_result eq 'error' ) ? 1
442 error_text => ( $desired_search_result eq 'error' )
445 error_name => ( $desired_search_result eq 'error' )
448 shift_entry => mock_net_ldap_entry( 'sampledn', 1 )
458 sub mock_net_ldap_search {
459 my ($parameters) = @_;
461 my $count = $parameters->{count};
462 my $code = $parameters->{code};
463 my $error = $parameters->{error};
464 my $error_text = $parameters->{error_text};
465 my $error_name = $parameters->{error_name};
466 my $shift_entry = $parameters->{shift_entry};
468 my $mocked_search = Test::MockObject->new();
469 $mocked_search->mock( 'count', sub { return $count; } );
470 $mocked_search->mock( 'code', sub { return $code; } );
471 $mocked_search->mock( 'error', sub { return $error; } );
472 $mocked_search->mock( 'error_name', sub { return $error_name; } );
473 $mocked_search->mock( 'error_text', sub { return $error_text; } );
474 $mocked_search->mock( 'shift_entry', sub { return $shift_entry; } );
476 return $mocked_search;
479 sub mock_net_ldap_message {
480 my ( $code, $error, $error_name, $error_text ) = @_;
482 my $mocked_message = Test::MockObject->new();
483 $mocked_message->mock( 'code', sub { $code } );
484 $mocked_message->mock( 'error', sub { $error } );
485 $mocked_message->mock( 'error_name', sub { $error_name } );
486 $mocked_message->mock( 'error_text', sub { $error_text } );
488 return $mocked_message;
491 sub mock_net_ldap_entry {
492 my ( $dn, $exists ) = @_;
494 my $mocked_entry = Test::MockObject->new();
495 $mocked_entry->mock( 'dn', sub { return $dn; } );
496 $mocked_entry->mock( 'exists', sub { return $exists } );
498 return $mocked_entry;
501 # TODO: Once we remove the global variables in C4::Auth_with_ldap
502 # we shouldn't need this...
504 sub reload_ldap_module {
505 delete $INC{'C4/Auth_with_ldap.pm'};
506 require C4::Auth_with_ldap;
507 C4::Auth_with_ldap->import;