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 $schema = Koha
::Database
->new->schema;
35 $schema->storage->txn_begin();
37 my $builder = t
::lib
::TestBuilder
->new();
39 # Variables controlling LDAP server config
43 my $anonymous_bind = 1;
45 # Variables controlling LDAP behaviour
46 my $desired_authentication_result = 'success';
47 my $desired_connection_result = 'error';
48 my $desired_admin_bind_result = 'error';
49 my $desired_search_result = 'error';
50 my $desired_count_result = 1;
51 my $desired_bind_result = 'error';
52 my $remaining_entry = 1;
55 # Mock the context module
56 my $context = Test
::MockModule
->new('C4::Context');
57 $context->mock( 'config', \
&mockedC4Config
);
59 # Mock the Net::LDAP module
60 my $net_ldap = Test
::MockModule
->new('Net::LDAP');
65 if ( $desired_connection_result eq 'error' ) {
67 # We were asked to fail the LDAP conexion
71 # Return a mocked Net::LDAP object (Test::MockObject)
72 return mock_net_ldap
();
77 my $categorycode = $builder->build( { source
=> 'Category' } )->{categorycode
};
78 my $branchcode = $builder->build( { source
=> 'Branch' } )->{branchcode
};
79 my $attr_type = $builder->build(
81 source
=> 'BorrowerAttributeType',
83 category_code
=> $categorycode
87 my $attr_type2 = $builder->build(
89 source
=> 'BorrowerAttributeType',
91 category_code
=> $categorycode
96 my $borrower = $builder->build(
101 branchcode
=> $branchcode,
102 categorycode
=> $categorycode
109 source
=> 'BorrowerAttribute',
111 borrowernumber
=> $borrower->{borrowernumber
},
112 code
=> $attr_type->{code
},
118 # C4::Auth_with_ldap needs several stuff set first ^^^
119 use_ok
('C4::Auth_with_ldap');
121 'C4::Auth_with_ldap', qw
/
126 subtest
'checkpw_ldap tests' => sub {
130 my $dbh = C4
::Context
->dbh;
131 ## Connection fail tests
132 $desired_connection_result = 'error';
135 C4
::Auth_with_ldap
::checkpw_ldap
( $dbh, 'hola', password
=> 'hey' );
137 'LDAP connexion failed',
138 'checkpw_ldap prints correct warning if LDAP conexion fails';
139 is
( $ret, 0, 'checkpw_ldap returns 0 if LDAP conexion fails' );
141 ## Connection success tests
142 $desired_connection_result = 'success';
144 subtest
'auth_by_bind = 1 tests' => sub {
150 $desired_authentication_result = 'success';
152 $desired_admin_bind_result = 'error';
153 $desired_search_result = 'error';
154 reload_ldap_module
();
157 $ret = C4
::Auth_with_ldap
::checkpw_ldap
( $dbh, 'hola',
160 qr/Anonymous LDAP bind failed: LDAP error #1: error_name/,
161 'checkpw_ldap prints correct warning if LDAP anonymous bind fails';
162 is
( $ret, 0, 'checkpw_ldap returns 0 if LDAP anonymous bind fails' );
164 $desired_authentication_result = 'success';
166 $desired_admin_bind_result = 'success';
167 $desired_search_result = 'success';
168 $desired_count_result = 1;
169 $desired_bind_result = 'success';
171 reload_ldap_module
();
173 t
::lib
::Mocks
::mock_preference
( 'ExtendedPatronAttributes', 1 );
174 my $auth = Test
::MockModule
->new('C4::Auth_with_ldap');
178 return $borrower->{cardnumber
};
185 $attr_type2->{code
}, 'BAR'
190 C4
::Auth_with_ldap
::checkpw_ldap
( $dbh, 'hola', password
=> 'hey' );
193 C4
::Members
::Attributes
::GetBorrowerAttributes
(
194 $borrower->{borrowernumber
}
197 'Extended attributes are not deleted'
200 is
( C4
::Members
::Attributes
::GetBorrowerAttributeValue
($borrower->{borrowernumber
}, $attr_type2->{code
}), 'BAR', 'Mapped attribute is BAR' );
201 $auth->unmock('update_local');
202 $auth->unmock('ldap_entry_2_hash');
205 $desired_count_result = 0; # user auth problem
206 Koha
::Patrons
->find( $borrower->{borrowernumber
} )->delete;
207 reload_ldap_module
();
209 C4
::Auth_with_ldap
::checkpw_ldap
( $dbh, 'hola', password
=> 'hey' ),
211 'checkpw_ldap returns 0 if user lookup returns 0'
214 $desired_bind_result = 'error';
215 reload_ldap_module
();
218 $ret = C4
::Auth_with_ldap
::checkpw_ldap
( $dbh, 'hola',
221 qr/LDAP bind failed as kohauser hola: LDAP error #1: error_name/,
222 'checkpw_ldap prints correct warning if LDAP bind fails';
224 'checkpw_ldap returns -1 LDAP bind fails for user (Bug 8148)' );
226 # regression tests for bug 12831
227 $desired_authentication_result = 'error';
229 $desired_admin_bind_result = 'error';
230 $desired_search_result = 'success';
231 $desired_count_result = 0; # user auth problem
232 $desired_bind_result = 'error';
233 reload_ldap_module
();
236 $ret = C4
::Auth_with_ldap
::checkpw_ldap
( $dbh, 'hola',
239 qr/LDAP bind failed as kohauser hola: LDAP error #1: error_name/,
240 'checkpw_ldap prints correct warning if LDAP bind fails';
242 'checkpw_ldap returns 0 LDAP bind fails for user (Bug 12831)' );
246 subtest
'auth_by_bind = 0 tests' => sub {
254 $desired_admin_bind_result = 'error';
255 $desired_bind_result = 'error';
256 reload_ldap_module
();
259 $ret = C4
::Auth_with_ldap
::checkpw_ldap
( $dbh, 'hola',
262 qr/LDAP bind failed as ldapuser cn=Manager,dc=metavore,dc=com: LDAP error #1: error_name/,
263 'checkpw_ldap prints correct warning if LDAP bind fails';
264 is
( $ret, 0, 'checkpw_ldap returns 0 if bind fails' );
267 $desired_admin_bind_result = 'success';
268 $desired_bind_result = 'error';
269 $desired_search_result = 'success';
270 $desired_count_result = 1;
271 reload_ldap_module
();
274 $ret = C4
::Auth_with_ldap
::checkpw_ldap
( $dbh, 'hola',
277 qr/LDAP Auth rejected : invalid password for user 'hola'./,
278 'checkpw_ldap prints correct warning if LDAP bind fails';
279 is
( $ret, -1, 'checkpw_ldap returns -1 if bind fails (Bug 8148)' );
283 $desired_admin_bind_result = 'error';
284 $desired_bind_result = 'error';
285 reload_ldap_module
();
288 $ret = C4
::Auth_with_ldap
::checkpw_ldap
( $dbh, 'hola',
291 qr/LDAP bind failed as ldapuser cn=Manager,dc=metavore,dc=com: LDAP error #1: error_name/,
292 'checkpw_ldap prints correct warning if LDAP bind fails';
293 is
( $ret, 0, 'checkpw_ldap returns 0 if bind fails' );
296 $desired_admin_bind_result = 'success';
297 $desired_bind_result = 'error';
298 reload_ldap_module
();
301 $ret = C4
::Auth_with_ldap
::checkpw_ldap
( $dbh, 'hola',
304 qr/LDAP Auth rejected : invalid password for user 'hola'./,
305 'checkpw_ldap prints correct warning if LDAP bind fails';
306 is
( $ret, -1, 'checkpw_ldap returns -1 if bind fails (Bug 8148)' );
311 subtest
'search_method tests' => sub {
315 my $ldap = mock_net_ldap
();
318 is
( C4
::Auth_with_ldap
::search_method
( $ldap, undef ),
319 undef, 'search_method returns undef on undefined userid' );
320 is
( C4
::Auth_with_ldap
::search_method
( undef, 'undef' ),
321 undef, 'search_method returns undef on undefined ldap object' );
323 # search ->code and !->code
324 $desired_search_result = 'error';
325 reload_ldap_module
();
327 eval { $ret = C4
::Auth_with_ldap
::search_method
( $ldap, 'undef' ); };
330 qr/LDAP search failed to return object : 1/,
331 'search_method prints correct warning when db->search returns error code'
335 # Function that mocks the call to C4::Context->config(param)
340 if ( $param eq 'useshibboleth' ) {
343 if ( $param eq 'ldapserver' ) {
345 firstname
=> { is
=> 'givenname' },
346 surname
=> { is
=> 'sn' },
347 address
=> { is
=> 'postaladdress' },
348 city
=> { is
=> 'l' },
349 zipcode
=> { is
=> 'postalcode' },
350 branchcode
=> { is
=> 'branch' },
351 userid
=> { is
=> 'uid' },
352 password
=> { is
=> 'userpassword' },
353 email
=> { is
=> 'mail' },
354 categorycode
=> { is
=> 'employeetype' },
355 phone
=> { is
=> 'telephonenumber' },
359 anonymous_bind
=> $anonymous_bind,
360 auth_by_bind
=> $auth_by_bind,
361 base
=> 'dc=metavore,dc=com',
362 hostname
=> 'localhost',
363 mapping
=> \
%ldap_mapping,
365 principal_name
=> '%s@my_domain.com',
366 replicate
=> $replicate,
368 user
=> 'cn=Manager,dc=metavore,dc=com',
370 return \
%ldap_config;
372 if ( $param =~ /(intranetdir|opachtdocs|intrahtdocs)/x ) {
375 if ( ref $class eq 'HASH' ) {
376 return $class->{$param};
381 # Function that mocks the call to Net::LDAP
384 my $mocked_ldap = Test
::MockObject
->new();
386 $mocked_ldap->mock( 'bind', sub {
387 if (is_admin_bind
(@_)) {
388 return mock_net_ldap_message
(
389 ($desired_admin_bind_result eq 'error' ) ?
1 : 0, # code
390 ($desired_admin_bind_result eq 'error' ) ?
1 : 0, # error
391 ($desired_admin_bind_result eq 'error' ) ?
'error_name' : 0, # error_name
392 ($desired_admin_bind_result eq 'error' ) ?
'error_text' : 0 # error_text
396 if ( $desired_bind_result eq 'error' ) {
397 return mock_net_ldap_message
(1,1,'error_name','error_text');
399 return mock_net_ldap_message
(0,0,'','');
407 $remaining_entry = 1;
409 return mock_net_ldap_search
(
411 count
=> ($desired_count_result)
412 ?
$desired_count_result
414 code
=> ( $desired_search_result eq 'error' )
417 error
=> ( $desired_search_result eq 'error' ) ?
1
419 error_text
=> ( $desired_search_result eq 'error' )
422 error_name
=> ( $desired_search_result eq 'error' )
425 shift_entry
=> mock_net_ldap_entry
( 'sampledn', 1 )
435 sub mock_net_ldap_search
{
436 my ($parameters) = @_;
438 my $count = $parameters->{count
};
439 my $code = $parameters->{code
};
440 my $error = $parameters->{error
};
441 my $error_text = $parameters->{error_text
};
442 my $error_name = $parameters->{error_name
};
443 my $shift_entry = $parameters->{shift_entry
};
445 my $mocked_search = Test
::MockObject
->new();
446 $mocked_search->mock( 'count', sub { return $count; } );
447 $mocked_search->mock( 'code', sub { return $code; } );
448 $mocked_search->mock( 'error', sub { return $error; } );
449 $mocked_search->mock( 'error_name', sub { return $error_name; } );
450 $mocked_search->mock( 'error_text', sub { return $error_text; } );
451 $mocked_search->mock( 'shift_entry', sub {
452 if ($remaining_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();