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;
24 use t::lib::TestBuilder;
34 my $database = Koha::Database->new();
35 my $schema = $database->schema();
36 $schema->storage->txn_begin();
38 my $builder = t::lib::TestBuilder->new();
40 # Variables controlling LDAP server config
44 my $anonymous_bind = 1;
46 # Variables controlling LDAP behaviour
47 my $desired_authentication_result = 'success';
48 my $desired_connection_result = 'error';
49 my $desired_admin_bind_result = 'error';
50 my $desired_compare_result = 'error';
51 my $desired_search_result = 'error';
52 my $desired_count_result = 1;
53 my $desired_bind_result = 'error';
56 # Mock the context module
57 my $context = Test::MockModule->new('C4::Context');
58 $context->mock( 'config', \&mockedC4Config );
60 # Mock the Net::LDAP module
61 my $net_ldap = Test::MockModule->new('Net::LDAP');
66 if ( $desired_connection_result eq 'error' ) {
68 # We were asked to fail the LDAP conexion
72 # Return a mocked Net::LDAP object (Test::MockObject)
73 return mock_net_ldap();
78 my $categorycode = $builder->build( { source => 'Category' } )->{categorycode};
79 my $branchcode = $builder->build( { source => 'Branch' } )->{branchcode};
80 my $attr_type = $builder->build(
82 source => 'BorrowerAttributeType',
84 category_code => $categorycode
88 my $attr_type2 = $builder->build(
90 source => 'BorrowerAttributeType',
92 category_code => $categorycode
97 my $borrower = $builder->build(
102 branchcode => $branchcode,
103 categorycode => $categorycode
110 source => 'BorrowerAttribute',
112 borrowernumber => $borrower->{borrowernumber},
113 code => $attr_type->{code},
119 # C4::Auth_with_ldap needs several stuff set first ^^^
120 use_ok('C4::Auth_with_ldap');
122 'C4::Auth_with_ldap', qw/
127 subtest 'checkpw_ldap tests' => sub {
131 my $dbh = C4::Context->dbh;
132 ## Connection fail tests
133 $desired_connection_result = 'error';
136 C4::Auth_with_ldap::checkpw_ldap( $dbh, 'hola', password => 'hey' );
138 'LDAP connexion failed',
139 'checkpw_ldap prints correct warning if LDAP conexion fails';
140 is( $ret, 0, 'checkpw_ldap returns 0 if LDAP conexion fails' );
142 ## Connection success tests
143 $desired_connection_result = 'success';
145 subtest 'auth_by_bind = 1 tests' => sub {
151 $desired_authentication_result = 'success';
153 $desired_admin_bind_result = 'error';
154 $desired_search_result = 'error';
155 reload_ldap_module();
158 $ret = C4::Auth_with_ldap::checkpw_ldap( $dbh, 'hola',
161 qr/Anonymous LDAP bind failed: LDAP error #1: error_name/,
162 'checkpw_ldap prints correct warning if LDAP anonymous bind fails';
163 is( $ret, 0, 'checkpw_ldap returns 0 if LDAP anonymous bind fails' );
165 $desired_authentication_result = 'success';
167 $desired_admin_bind_result = 'success';
168 $desired_search_result = 'success';
169 $desired_count_result = 1;
170 $desired_bind_result = 'success';
172 reload_ldap_module();
174 t::lib::Mocks::mock_preference( 'ExtendedPatronAttributes', 1 );
175 my $auth = Test::MockModule->new('C4::Auth_with_ldap');
179 return $borrower->{cardnumber};
186 $attr_type2->{code}, 'BAR'
191 C4::Auth_with_ldap::checkpw_ldap( $dbh, 'hola', password => 'hey' );
194 C4::Members::Attributes::GetBorrowerAttributes(
195 $borrower->{borrowernumber}
198 'Extended attributes are not deleted'
201 is( C4::Members::Attributes::GetBorrowerAttributeValue($borrower->{borrowernumber}, $attr_type2->{code}), 'BAR', 'Mapped attribute is BAR' );
202 $auth->unmock('update_local');
203 $auth->unmock('ldap_entry_2_hash');
206 $desired_count_result = 0; # user auth problem
207 Koha::Patrons->find( $borrower->{borrowernumber} )->delete;
208 reload_ldap_module();
210 C4::Auth_with_ldap::checkpw_ldap( $dbh, 'hola', password => 'hey' ),
212 'checkpw_ldap returns 0 if user lookup returns 0'
215 $desired_bind_result = 'error';
216 reload_ldap_module();
219 $ret = C4::Auth_with_ldap::checkpw_ldap( $dbh, 'hola',
222 qr/LDAP bind failed as kohauser hola: LDAP error #1: error_name/,
223 'checkpw_ldap prints correct warning if LDAP bind fails';
225 'checkpw_ldap returns -1 LDAP bind fails for user (Bug 8148)' );
227 # regression tests for bug 12831
228 $desired_authentication_result = 'error';
230 $desired_admin_bind_result = 'error';
231 $desired_search_result = 'success';
232 $desired_count_result = 0; # user auth problem
233 $desired_bind_result = 'error';
234 reload_ldap_module();
237 $ret = C4::Auth_with_ldap::checkpw_ldap( $dbh, 'hola',
240 qr/LDAP bind failed as kohauser hola: LDAP error #1: error_name/,
241 'checkpw_ldap prints correct warning if LDAP bind fails';
243 'checkpw_ldap returns 0 LDAP bind fails for user (Bug 12831)' );
247 subtest 'auth_by_bind = 0 tests' => sub {
255 $desired_admin_bind_result = 'error';
256 $desired_bind_result = 'error';
257 reload_ldap_module();
260 $ret = C4::Auth_with_ldap::checkpw_ldap( $dbh, 'hola',
263 qr/LDAP bind failed as ldapuser cn=Manager,dc=metavore,dc=com: LDAP error #1: error_name/,
264 'checkpw_ldap prints correct warning if LDAP bind fails';
265 is( $ret, 0, 'checkpw_ldap returns 0 if bind fails' );
268 $desired_admin_bind_result = 'success';
269 $desired_bind_result = 'error';
270 reload_ldap_module();
273 $ret = C4::Auth_with_ldap::checkpw_ldap( $dbh, 'hola',
276 qr/LDAP Auth rejected : invalid password for user 'hola'. LDAP error #1: error_name/,
277 'checkpw_ldap prints correct warning if LDAP bind fails';
278 is( $ret, -1, 'checkpw_ldap returns -1 if bind fails (Bug 8148)' );
282 $desired_admin_bind_result = 'error';
283 $desired_bind_result = 'error';
284 reload_ldap_module();
287 $ret = C4::Auth_with_ldap::checkpw_ldap( $dbh, 'hola',
290 qr/LDAP bind failed as ldapuser cn=Manager,dc=metavore,dc=com: LDAP error #1: error_name/,
291 'checkpw_ldap prints correct warning if LDAP bind fails';
292 is( $ret, 0, 'checkpw_ldap returns 0 if bind fails' );
295 $desired_admin_bind_result = 'success';
296 $desired_bind_result = 'error';
297 reload_ldap_module();
300 $ret = C4::Auth_with_ldap::checkpw_ldap( $dbh, 'hola',
303 qr/LDAP Auth rejected : invalid password for user 'hola'. LDAP error #1: error_name/,
304 'checkpw_ldap prints correct warning if LDAP bind fails';
305 is( $ret, -1, 'checkpw_ldap returns -1 if bind fails (Bug 8148)' );
310 subtest 'search_method tests' => sub {
314 my $ldap = mock_net_ldap();
317 is( C4::Auth_with_ldap::search_method( $ldap, undef ),
318 undef, 'search_method returns undef on undefined userid' );
319 is( C4::Auth_with_ldap::search_method( undef, 'undef' ),
320 undef, 'search_method returns undef on undefined ldap object' );
322 # search ->code and !->code
323 $desired_search_result = 'error';
324 reload_ldap_module();
326 eval { $ret = C4::Auth_with_ldap::search_method( $ldap, 'undef' ); };
329 qr/LDAP search failed to return object : 1/,
330 'search_method prints correct warning when db->search returns error code'
333 $desired_search_result = 'success';
334 $desired_count_result = 2;
335 reload_ldap_module();
336 warning_like { $ret = C4::Auth_with_ldap::search_method( $ldap, '123' ) }
337 qr/^LDAP Auth rejected \: \(uid\=123\) gets 2 hits/,
338 'search_method prints correct warning when hits count is not 1';
339 is( $ret, 0, 'search_method returns 0 when hits count is not 1' );
343 # Function that mocks the call to C4::Context->config(param)
348 if ( $param eq 'useshibboleth' ) {
351 if ( $param eq 'ldapserver' ) {
353 firstname => { is => 'givenname' },
354 surname => { is => 'sn' },
355 address => { is => 'postaladdress' },
356 city => { is => 'l' },
357 zipcode => { is => 'postalcode' },
358 branchcode => { is => 'branch' },
359 userid => { is => 'uid' },
360 password => { is => 'userpassword' },
361 email => { is => 'mail' },
362 categorycode => { is => 'employeetype' },
363 phone => { is => 'telephonenumber' },
367 anonymous_bind => $anonymous_bind,
368 auth_by_bind => $auth_by_bind,
369 base => 'dc=metavore,dc=com',
370 hostname => 'localhost',
371 mapping => \%ldap_mapping,
373 principal_name => '%s@my_domain.com',
374 replicate => $replicate,
376 user => 'cn=Manager,dc=metavore,dc=com',
378 return \%ldap_config;
380 if ( $param =~ /(intranetdir|opachtdocs|intrahtdocs)/x ) {
383 if ( ref $class eq 'HASH' ) {
384 return $class->{$param};
389 # Function that mocks the call to Net::LDAP
392 my $mocked_ldap = Test::MockObject->new();
394 $mocked_ldap->mock( 'bind', sub {
395 if (is_admin_bind(@_)) {
396 return mock_net_ldap_message(
397 ($desired_admin_bind_result eq 'error' ) ? 1 : 0, # code
398 ($desired_admin_bind_result eq 'error' ) ? 1 : 0, # error
399 ($desired_admin_bind_result eq 'error' ) ? 'error_name' : 0, # error_name
400 ($desired_admin_bind_result eq 'error' ) ? 'error_text' : 0 # error_text
404 if ( $desired_bind_result eq 'error' ) {
405 return mock_net_ldap_message(1,1,'error_name','error_text');
407 return mock_net_ldap_message(0,0,'','');
415 return mock_net_ldap_search(
417 count => ($desired_count_result)
418 ? $desired_count_result
420 code => ( $desired_search_result eq 'error' )
423 error => ( $desired_search_result eq 'error' ) ? 1
425 error_text => ( $desired_search_result eq 'error' )
428 error_name => ( $desired_search_result eq 'error' )
431 shift_entry => mock_net_ldap_entry( 'sampledn', 1 )
441 sub mock_net_ldap_search {
442 my ($parameters) = @_;
444 my $count = $parameters->{count};
445 my $code = $parameters->{code};
446 my $error = $parameters->{error};
447 my $error_text = $parameters->{error_text};
448 my $error_name = $parameters->{error_name};
449 my $shift_entry = $parameters->{shift_entry};
451 my $mocked_search = Test::MockObject->new();
452 $mocked_search->mock( 'count', sub { return $count; } );
453 $mocked_search->mock( 'code', sub { return $code; } );
454 $mocked_search->mock( 'error', sub { return $error; } );
455 $mocked_search->mock( 'error_name', sub { return $error_name; } );
456 $mocked_search->mock( 'error_text', sub { return $error_text; } );
457 $mocked_search->mock( 'shift_entry', sub { return $shift_entry; } );
459 return $mocked_search;
462 sub mock_net_ldap_message {
463 my ( $code, $error, $error_name, $error_text ) = @_;
465 my $mocked_message = Test::MockObject->new();
466 $mocked_message->mock( 'code', sub { $code } );
467 $mocked_message->mock( 'error', sub { $error } );
468 $mocked_message->mock( 'error_name', sub { $error_name } );
469 $mocked_message->mock( 'error_text', sub { $error_text } );
471 return $mocked_message;
474 sub mock_net_ldap_entry {
475 my ( $dn, $exists ) = @_;
477 my $mocked_entry = Test::MockObject->new();
478 $mocked_entry->mock( 'dn', sub { return $dn; } );
479 $mocked_entry->mock( 'exists', sub { return $exists } );
481 return $mocked_entry;
484 # TODO: Once we remove the global variables in C4::Auth_with_ldap
485 # we shouldn't need this...
487 sub reload_ldap_module {
488 delete $INC{'C4/Auth_with_ldap.pm'};
489 require C4::Auth_with_ldap;
490 C4::Auth_with_ldap->import;
497 if ($#args <= 1 || $args[1] eq 'cn=Manager,dc=metavore,dc=com') {
504 $schema->storage->txn_rollback;