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;
31 # Hide all the subrouteine redefined warnings when running this test..
32 # We reload the ldap module lots in the test and each reload triggers the
33 # 'Subroutine X redefined at' warning.. disable that to make the test output
35 $SIG{__WARN__} = sub {
37 warn $warning unless $warning =~ /Subroutine .* redefined at/;
41 my $schema = Koha::Database->new->schema;
42 $schema->storage->txn_begin();
44 my $builder = t::lib::TestBuilder->new();
46 # Variables controlling LDAP server config
51 my $anonymous_bind = 1;
52 my $user = 'cn=Manager,dc=metavore,dc=com';
53 my $pass = 'metavore';
56 # Variables controlling LDAP behaviour
57 my $desired_authentication_result = 'success';
58 my $desired_connection_result = 'error';
59 my $desired_admin_bind_result = 'error';
60 my $desired_search_result = 'error';
61 my $desired_count_result = 1;
62 my $desired_bind_result = 'error';
63 my $remaining_entry = 1;
66 # Mock the context module
67 my $context = Test::MockModule->new('C4::Context');
68 $context->mock( 'config', \&mockedC4Config );
70 # Mock the Net::LDAP module
71 my $net_ldap = Test::MockModule->new('Net::LDAP');
76 if ( $desired_connection_result eq 'error' ) {
78 # We were asked to fail the LDAP conexion
82 # Return a mocked Net::LDAP object (Test::MockObject)
83 return mock_net_ldap();
88 my $categorycode = $builder->build( { source => 'Category' } )->{categorycode};
89 my $branchcode = $builder->build( { source => 'Branch' } )->{branchcode};
90 my $attr_type = $builder->build(
92 source => 'BorrowerAttributeType',
94 category_code => $categorycode
98 my $attr_type2 = $builder->build(
100 source => 'BorrowerAttributeType',
102 category_code => $categorycode
107 my $borrower = $builder->build(
109 source => 'Borrower',
112 branchcode => $branchcode,
113 categorycode => $categorycode
120 source => 'BorrowerAttribute',
122 borrowernumber => $borrower->{borrowernumber},
123 code => $attr_type->{code},
129 my $patron = Koha::Patrons->find($borrower->{borrowernumber});
131 # C4::Auth_with_ldap needs several stuff set first ^^^
132 use_ok('C4::Auth_with_ldap', qw( checkpw_ldap ));
134 'C4::Auth_with_ldap', qw/
139 subtest 'checkpw_ldap tests' => sub {
143 ## Connection fail tests
144 $desired_connection_result = 'error';
147 C4::Auth_with_ldap::checkpw_ldap( 'hola', password => 'hey' );
149 'LDAP connexion failed',
150 'checkpw_ldap prints correct warning if LDAP conexion fails';
151 is( $ret, 0, 'checkpw_ldap returns 0 if LDAP conexion fails' );
153 ## Connection success tests
154 $desired_connection_result = 'success';
156 subtest 'auth_by_bind = 1 tests' => sub {
162 $desired_authentication_result = 'success';
164 $desired_admin_bind_result = 'error';
165 $desired_search_result = 'error';
166 reload_ldap_module();
169 $ret = C4::Auth_with_ldap::checkpw_ldap( 'hola',
172 qr/Anonymous LDAP bind failed: LDAP error #1: error_name/,
173 'checkpw_ldap prints correct warning if LDAP anonymous bind fails';
174 is( $ret, 0, 'checkpw_ldap returns 0 if LDAP anonymous bind fails' );
179 reload_ldap_module();
182 $ret = C4::Auth_with_ldap::checkpw_ldap( 'hola',
185 qr/LDAP bind failed as kohauser hola: LDAP error #1: error_name/,
186 'checkpw_ldap prints correct warning if LDAP bind_by_auth fails';
187 is( $ret, 0, 'checkpw_ldap returns 0 if LDAP bind_by_auth fails' );
189 $desired_authentication_result = 'success';
191 $desired_admin_bind_result = 'success';
192 $desired_search_result = 'success';
193 $desired_count_result = 1;
194 $desired_bind_result = 'success';
196 reload_ldap_module();
198 t::lib::Mocks::mock_preference( 'ExtendedPatronAttributes', 1 );
199 my $auth = Test::MockModule->new('C4::Auth_with_ldap');
203 return $borrower->{cardnumber};
210 $attr_type2->{code}, 'BAR'
215 C4::Auth_with_ldap::checkpw_ldap( 'hola', password => 'hey' );
217 Koha::Patrons->find($borrower->{borrowernumber})->extended_attributes->count,
218 'Extended attributes are not deleted'
221 is( $patron->get_extended_attribute( $attr_type2->{code} )->attribute, 'BAR', 'Mapped attribute is BAR' );
222 $auth->unmock('update_local');
223 $auth->unmock('ldap_entry_2_hash');
226 $desired_count_result = 0; # user auth problem
228 reload_ldap_module();
230 C4::Auth_with_ldap::checkpw_ldap( 'hola', password => 'hey' ),
232 'checkpw_ldap returns 0 if user lookup returns 0'
235 # test replicate functionality and welcome notice
236 $desired_authentication_result = 'success';
238 $desired_admin_bind_result = 'success';
239 $desired_search_result = 'success';
240 $desired_count_result = 1;
241 $desired_bind_result = 'success';
244 reload_ldap_module();
251 branchcode => $branchcode,
252 categorycode => $categorycode,
253 email => 'me@myemail.com',
258 C4::Auth_with_ldap::checkpw_ldap( 'hola', password => 'hey' );
259 my $patrons = Koha::Patrons->search( { userid => 'hola' } );
260 is( $patrons->count, 1, 'New patron added with "replicate"' );
262 $patron = $patrons->next;
263 my $queued_notices = Koha::Notice::Messages->search(
264 { borrowernumber => $patron->borrowernumber } );
265 is( $queued_notices->count, 1,
266 "One notice queued when `welcome` is set" );
268 my $THE_notice = $queued_notices->next;
269 is( $THE_notice->status, 'failed', "The notice was sent immediately" );
275 $auth->unmock('ldap_entry_2_hash');
276 # end replicate testing
278 $desired_count_result = 0;
279 $desired_bind_result = 'error';
280 reload_ldap_module();
283 $ret = C4::Auth_with_ldap::checkpw_ldap( 'hola',
286 qr/LDAP bind failed as kohauser hola: LDAP error #1: error_name/,
287 'checkpw_ldap prints correct warning if LDAP bind fails';
289 'checkpw_ldap returns -1 LDAP bind fails for user (Bug 8148)' );
291 # regression tests for bug 12831
292 $desired_authentication_result = 'error';
294 $desired_admin_bind_result = 'error';
295 $desired_search_result = 'success';
296 $desired_count_result = 0; # user auth problem
297 $desired_bind_result = 'error';
298 reload_ldap_module();
301 $ret = C4::Auth_with_ldap::checkpw_ldap( 'hola',
304 qr/LDAP bind failed as kohauser hola: LDAP error #1: error_name/,
305 'checkpw_ldap prints correct warning if LDAP bind fails';
307 'checkpw_ldap returns 0 LDAP bind fails for user (Bug 12831)' );
311 subtest 'auth_by_bind = 0 tests' => sub {
319 $user = 'cn=Manager,dc=metavore,dc=com';
321 $desired_admin_bind_result = 'error';
322 $desired_bind_result = 'error';
323 reload_ldap_module();
326 $ret = C4::Auth_with_ldap::checkpw_ldap( 'hola',
329 qr/LDAP bind failed as ldapuser cn=Manager,dc=metavore,dc=com: LDAP error #1: error_name/,
330 'checkpw_ldap prints correct warning if LDAP bind fails';
331 is( $ret, 0, 'checkpw_ldap returns 0 if bind fails' );
334 $desired_admin_bind_result = 'success';
335 $desired_bind_result = 'error';
336 $desired_search_result = 'success';
337 $desired_count_result = 1;
338 reload_ldap_module();
341 $ret = C4::Auth_with_ldap::checkpw_ldap( 'hola',
344 qr/LDAP Auth rejected : invalid password for user 'hola'./,
345 'checkpw_ldap prints correct warning if LDAP bind fails';
346 is( $ret, -1, 'checkpw_ldap returns -1 if bind fails (Bug 8148)' );
350 $desired_admin_bind_result = 'error';
351 $desired_bind_result = 'error';
352 reload_ldap_module();
355 $ret = C4::Auth_with_ldap::checkpw_ldap( 'hola',
358 qr/LDAP bind failed as ldapuser cn=Manager,dc=metavore,dc=com: LDAP error #1: error_name/,
359 'checkpw_ldap prints correct warning if LDAP bind fails';
360 is( $ret, 0, 'checkpw_ldap returns 0 if bind fails' );
363 $desired_admin_bind_result = 'success';
364 $desired_bind_result = 'error';
365 reload_ldap_module();
368 $ret = C4::Auth_with_ldap::checkpw_ldap( 'hola',
371 qr/LDAP Auth rejected : invalid password for user 'hola'./,
372 'checkpw_ldap prints correct warning if LDAP bind fails';
373 is( $ret, -1, 'checkpw_ldap returns -1 if bind fails (Bug 8148)' );
377 subtest "'update' config tets" => sub {
381 $schema->storage->txn_begin;
383 my $cardnumber = '123456789';
384 my $userid = '987654321';
386 # test safety cleanup
387 Koha::Patrons->search( [ { cardnumber => $cardnumber }, { userid => $userid } ] )->delete;
389 my $patron = $builder->build_object(
391 class => 'Koha::Patrons',
392 value => { cardnumber => $cardnumber, userid => $userid },
397 t::lib::Mocks::mock_preference( 'ExtendedPatronAttributes', 0 );
405 $desired_count_result = 1;
406 $desired_authentication_result = 'success';
407 $desired_admin_bind_result = 'success';
408 $desired_search_result = 'success';
409 $desired_bind_result = 'success';
412 branch => [ $patron->branchcode ],
413 employeetype => [ $patron->categorycode ],
414 givenname => [ $patron->firstname ],
415 mail => [ $patron->email ],
416 postaladdress => [ $patron->address ],
417 sn => [ $patron->surname ],
418 uid => [ $patron->userid ],
419 userpassword => ['some password'],
422 reload_ldap_module();
424 my ( $ret_val, $ret_cardnumber, $ret_userid ) =
425 C4::Auth_with_ldap::checkpw_ldap( $patron->userid, password => 'hey' );
427 ok( $ret_val, 'Authentication success returns 1' );
428 is( $ret_cardnumber, $cardnumber, "The correct 'cardnumber' is returned" );
429 is( $ret_userid, $userid, "The correct 'userid' is returned" );
431 $patron->discard_changes;
432 is( $patron->cardnumber, $cardnumber, 'The cardnumber is not mistakenly changed to userid when not mapped' );
434 $schema->storage->txn_rollback;
438 subtest 'search_method tests' => sub {
442 my $ldap = mock_net_ldap();
445 is( C4::Auth_with_ldap::search_method( $ldap, undef ),
446 undef, 'search_method returns undef on undefined userid' );
447 is( C4::Auth_with_ldap::search_method( undef, 'undef' ),
448 undef, 'search_method returns undef on undefined ldap object' );
450 # search ->code and !->code
451 $desired_search_result = 'error';
452 reload_ldap_module();
454 eval { $ret = C4::Auth_with_ldap::search_method( $ldap, 'undef' ); };
457 qr/LDAP search failed to return object : 1/,
458 'search_method prints correct warning when db->search returns error code'
462 # Function that mocks the call to C4::Context->config(param)
467 if ( $param eq 'useshibboleth' ) {
470 if ( $param eq 'ldapserver' ) {
472 firstname => { is => 'givenname' },
473 surname => { is => 'sn' },
474 address => { is => 'postaladdress' },
475 city => { is => 'l' },
476 zipcode => { is => 'postalcode' },
477 branchcode => { is => 'branch' },
478 userid => { is => 'uid' },
479 password => { is => 'userpassword' },
480 email => { is => 'mail' },
481 categorycode => { is => 'employeetype' },
482 phone => { is => 'telephonenumber' },
486 anonymous_bind => $anonymous_bind,
487 auth_by_bind => $auth_by_bind,
488 base => 'dc=metavore,dc=com',
489 hostname => 'localhost',
490 mapping => \%ldap_mapping,
492 principal_name => '%s@my_domain.com',
493 replicate => $replicate,
498 return \%ldap_config;
500 if ( $param =~ /(intranetdir|opachtdocs|intrahtdocs)/x ) {
503 if ( ref $class eq 'HASH' ) {
504 return $class->{$param};
507 return C4::Context::_common_config($param, 'config');
510 # Function that mocks the call to Net::LDAP
513 my $mocked_ldap = Test::MockObject->new();
515 $mocked_ldap->mock( 'bind', sub {
516 if (is_admin_bind(@_)) {
517 return mock_net_ldap_message(
518 ($desired_admin_bind_result eq 'error' ) ? 1 : 0, # code
519 ($desired_admin_bind_result eq 'error' ) ? 1 : 0, # error
520 ($desired_admin_bind_result eq 'error' ) ? 'error_name' : 0, # error_name
521 ($desired_admin_bind_result eq 'error' ) ? 'error_text' : 0 # error_text
525 if ( $desired_bind_result eq 'error' ) {
526 return mock_net_ldap_message(1,1,'error_name','error_text');
528 return mock_net_ldap_message(0,0,'','');
536 $remaining_entry = 1;
538 return mock_net_ldap_search(
540 count => ($desired_count_result)
541 ? $desired_count_result
543 code => ( $desired_search_result eq 'error' )
546 error => ( $desired_search_result eq 'error' ) ? 1
548 error_text => ( $desired_search_result eq 'error' )
551 error_name => ( $desired_search_result eq 'error' )
554 shift_entry => mock_net_ldap_entry( 'sampledn', 1 )
564 sub mock_net_ldap_search {
565 my ($parameters) = @_;
567 my $count = $parameters->{count};
568 my $code = $parameters->{code};
569 my $error = $parameters->{error};
570 my $error_text = $parameters->{error_text};
571 my $error_name = $parameters->{error_name};
572 my $shift_entry = $parameters->{shift_entry};
574 my $mocked_search = Test::MockObject->new();
575 $mocked_search->mock( 'count', sub { return $count; } );
576 $mocked_search->mock( 'code', sub { return $code; } );
577 $mocked_search->mock( 'error', sub { return $error; } );
578 $mocked_search->mock( 'error_name', sub { return $error_name; } );
579 $mocked_search->mock( 'error_text', sub { return $error_text; } );
580 $mocked_search->mock( 'shift_entry', sub {
581 if ($remaining_entry) {
588 return $mocked_search;
591 sub mock_net_ldap_message {
592 my ( $code, $error, $error_name, $error_text ) = @_;
594 my $mocked_message = Test::MockObject->new();
595 $mocked_message->mock( 'code', sub { $code } );
596 $mocked_message->mock( 'error', sub { $error } );
597 $mocked_message->mock( 'error_name', sub { $error_name } );
598 $mocked_message->mock( 'error_text', sub { $error_text } );
600 return $mocked_message;
603 sub mock_net_ldap_entry {
604 my ( $dn, $exists ) = @_;
606 my $mocked_entry = Test::MockObject->new( { attrs => $attrs } );
607 $mocked_entry->mock( 'dn', sub { return $dn; } );
608 $mocked_entry->mock( 'exists', sub { return $exists } );
610 return $mocked_entry;
613 # TODO: Once we remove the global variables in C4::Auth_with_ldap
614 # we shouldn't need this...
616 sub reload_ldap_module {
617 delete $INC{'C4/Auth_with_ldap.pm'};
618 require C4::Auth_with_ldap;
619 C4::Auth_with_ldap->import;
626 if ($#args <= 1 || $args[1] eq 'cn=Manager,dc=metavore,dc=com') {
633 $schema->storage->txn_rollback();