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;
44 my $user = 'cn=Manager,dc=metavore,dc=com';
45 my $pass = 'metavore';
47 # Variables controlling LDAP behaviour
48 my $desired_authentication_result = 'success';
49 my $desired_connection_result = 'error';
50 my $desired_admin_bind_result = 'error';
51 my $desired_search_result = 'error';
52 my $desired_count_result = 1;
53 my $desired_bind_result = 'error';
54 my $remaining_entry = 1;
57 # Mock the context module
58 my $context = Test
::MockModule
->new('C4::Context');
59 $context->mock( 'config', \
&mockedC4Config
);
61 # Mock the Net::LDAP module
62 my $net_ldap = Test
::MockModule
->new('Net::LDAP');
67 if ( $desired_connection_result eq 'error' ) {
69 # We were asked to fail the LDAP conexion
73 # Return a mocked Net::LDAP object (Test::MockObject)
74 return mock_net_ldap
();
79 my $categorycode = $builder->build( { source
=> 'Category' } )->{categorycode
};
80 my $branchcode = $builder->build( { source
=> 'Branch' } )->{branchcode
};
81 my $attr_type = $builder->build(
83 source
=> 'BorrowerAttributeType',
85 category_code
=> $categorycode
89 my $attr_type2 = $builder->build(
91 source
=> 'BorrowerAttributeType',
93 category_code
=> $categorycode
98 my $borrower = $builder->build(
100 source
=> 'Borrower',
103 branchcode
=> $branchcode,
104 categorycode
=> $categorycode
111 source
=> 'BorrowerAttribute',
113 borrowernumber
=> $borrower->{borrowernumber
},
114 code
=> $attr_type->{code
},
120 my $patron = Koha
::Patrons
->find($borrower->{borrowernumber
});
122 # C4::Auth_with_ldap needs several stuff set first ^^^
123 use_ok
('C4::Auth_with_ldap');
125 'C4::Auth_with_ldap', qw
/
130 subtest
'checkpw_ldap tests' => sub {
134 my $dbh = C4
::Context
->dbh;
135 ## Connection fail tests
136 $desired_connection_result = 'error';
139 C4
::Auth_with_ldap
::checkpw_ldap
( $dbh, 'hola', password
=> 'hey' );
141 'LDAP connexion failed',
142 'checkpw_ldap prints correct warning if LDAP conexion fails';
143 is
( $ret, 0, 'checkpw_ldap returns 0 if LDAP conexion fails' );
145 ## Connection success tests
146 $desired_connection_result = 'success';
148 subtest
'auth_by_bind = 1 tests' => sub {
154 $desired_authentication_result = 'success';
156 $desired_admin_bind_result = 'error';
157 $desired_search_result = 'error';
158 reload_ldap_module
();
161 $ret = C4
::Auth_with_ldap
::checkpw_ldap
( $dbh, 'hola',
164 qr/Anonymous LDAP bind failed: LDAP error #1: error_name/,
165 'checkpw_ldap prints correct warning if LDAP anonymous bind fails';
166 is
( $ret, 0, 'checkpw_ldap returns 0 if LDAP anonymous bind fails' );
171 reload_ldap_module
();
174 $ret = C4
::Auth_with_ldap
::checkpw_ldap
( $dbh, 'hola',
177 qr/LDAP bind failed as kohauser hola: LDAP error #1: error_name/,
178 'checkpw_ldap prints correct warning if LDAP bind_by_auth fails';
179 is
( $ret, 0, 'checkpw_ldap returns 0 if LDAP bind_by_auth fails' );
181 $desired_authentication_result = 'success';
183 $desired_admin_bind_result = 'success';
184 $desired_search_result = 'success';
185 $desired_count_result = 1;
186 $desired_bind_result = 'success';
188 reload_ldap_module
();
190 t
::lib
::Mocks
::mock_preference
( 'ExtendedPatronAttributes', 1 );
191 my $auth = Test
::MockModule
->new('C4::Auth_with_ldap');
195 return $borrower->{cardnumber
};
202 $attr_type2->{code
}, 'BAR'
207 C4
::Auth_with_ldap
::checkpw_ldap
( $dbh, 'hola', password
=> 'hey' );
209 Koha
::Patrons
->find($borrower->{borrowernumber
})->extended_attributes->count,
210 'Extended attributes are not deleted'
213 is
( $patron->get_extended_attribute( $attr_type2->{code
} )->attribute, 'BAR', 'Mapped attribute is BAR' );
214 $auth->unmock('update_local');
215 $auth->unmock('ldap_entry_2_hash');
218 $desired_count_result = 0; # user auth problem
220 reload_ldap_module
();
222 C4
::Auth_with_ldap
::checkpw_ldap
( $dbh, 'hola', password
=> 'hey' ),
224 'checkpw_ldap returns 0 if user lookup returns 0'
227 $desired_bind_result = 'error';
228 reload_ldap_module
();
231 $ret = C4
::Auth_with_ldap
::checkpw_ldap
( $dbh, 'hola',
234 qr/LDAP bind failed as kohauser hola: LDAP error #1: error_name/,
235 'checkpw_ldap prints correct warning if LDAP bind fails';
237 'checkpw_ldap returns -1 LDAP bind fails for user (Bug 8148)' );
239 # regression tests for bug 12831
240 $desired_authentication_result = 'error';
242 $desired_admin_bind_result = 'error';
243 $desired_search_result = 'success';
244 $desired_count_result = 0; # user auth problem
245 $desired_bind_result = 'error';
246 reload_ldap_module
();
249 $ret = C4
::Auth_with_ldap
::checkpw_ldap
( $dbh, 'hola',
252 qr/LDAP bind failed as kohauser hola: LDAP error #1: error_name/,
253 'checkpw_ldap prints correct warning if LDAP bind fails';
255 'checkpw_ldap returns 0 LDAP bind fails for user (Bug 12831)' );
259 subtest
'auth_by_bind = 0 tests' => sub {
267 $user = 'cn=Manager,dc=metavore,dc=com';
269 $desired_admin_bind_result = 'error';
270 $desired_bind_result = 'error';
271 reload_ldap_module
();
274 $ret = C4
::Auth_with_ldap
::checkpw_ldap
( $dbh, 'hola',
277 qr/LDAP bind failed as ldapuser cn=Manager,dc=metavore,dc=com: LDAP error #1: error_name/,
278 'checkpw_ldap prints correct warning if LDAP bind fails';
279 is
( $ret, 0, 'checkpw_ldap returns 0 if bind fails' );
282 $desired_admin_bind_result = 'success';
283 $desired_bind_result = 'error';
284 $desired_search_result = 'success';
285 $desired_count_result = 1;
286 reload_ldap_module
();
289 $ret = C4
::Auth_with_ldap
::checkpw_ldap
( $dbh, 'hola',
292 qr/LDAP Auth rejected : invalid password for user 'hola'./,
293 'checkpw_ldap prints correct warning if LDAP bind fails';
294 is
( $ret, -1, 'checkpw_ldap returns -1 if bind fails (Bug 8148)' );
298 $desired_admin_bind_result = 'error';
299 $desired_bind_result = 'error';
300 reload_ldap_module
();
303 $ret = C4
::Auth_with_ldap
::checkpw_ldap
( $dbh, 'hola',
306 qr/LDAP bind failed as ldapuser cn=Manager,dc=metavore,dc=com: LDAP error #1: error_name/,
307 'checkpw_ldap prints correct warning if LDAP bind fails';
308 is
( $ret, 0, 'checkpw_ldap returns 0 if bind fails' );
311 $desired_admin_bind_result = 'success';
312 $desired_bind_result = 'error';
313 reload_ldap_module
();
316 $ret = C4
::Auth_with_ldap
::checkpw_ldap
( $dbh, 'hola',
319 qr/LDAP Auth rejected : invalid password for user 'hola'./,
320 'checkpw_ldap prints correct warning if LDAP bind fails';
321 is
( $ret, -1, 'checkpw_ldap returns -1 if bind fails (Bug 8148)' );
326 subtest
'search_method tests' => sub {
330 my $ldap = mock_net_ldap
();
333 is
( C4
::Auth_with_ldap
::search_method
( $ldap, undef ),
334 undef, 'search_method returns undef on undefined userid' );
335 is
( C4
::Auth_with_ldap
::search_method
( undef, 'undef' ),
336 undef, 'search_method returns undef on undefined ldap object' );
338 # search ->code and !->code
339 $desired_search_result = 'error';
340 reload_ldap_module
();
342 eval { $ret = C4
::Auth_with_ldap
::search_method
( $ldap, 'undef' ); };
345 qr/LDAP search failed to return object : 1/,
346 'search_method prints correct warning when db->search returns error code'
350 # Function that mocks the call to C4::Context->config(param)
355 if ( $param eq 'useshibboleth' ) {
358 if ( $param eq 'ldapserver' ) {
360 firstname
=> { is
=> 'givenname' },
361 surname
=> { is
=> 'sn' },
362 address
=> { is
=> 'postaladdress' },
363 city
=> { is
=> 'l' },
364 zipcode
=> { is
=> 'postalcode' },
365 branchcode
=> { is
=> 'branch' },
366 userid
=> { is
=> 'uid' },
367 password
=> { is
=> 'userpassword' },
368 email
=> { is
=> 'mail' },
369 categorycode
=> { is
=> 'employeetype' },
370 phone
=> { is
=> 'telephonenumber' },
374 anonymous_bind
=> $anonymous_bind,
375 auth_by_bind
=> $auth_by_bind,
376 base
=> 'dc=metavore,dc=com',
377 hostname
=> 'localhost',
378 mapping
=> \
%ldap_mapping,
380 principal_name
=> '%s@my_domain.com',
381 replicate
=> $replicate,
385 return \
%ldap_config;
387 if ( $param =~ /(intranetdir|opachtdocs|intrahtdocs)/x ) {
390 if ( ref $class eq 'HASH' ) {
391 return $class->{$param};
394 return C4
::Context
::_common_config
($param, 'config');
397 # Function that mocks the call to Net::LDAP
400 my $mocked_ldap = Test
::MockObject
->new();
402 $mocked_ldap->mock( 'bind', sub {
403 if (is_admin_bind
(@_)) {
404 return mock_net_ldap_message
(
405 ($desired_admin_bind_result eq 'error' ) ?
1 : 0, # code
406 ($desired_admin_bind_result eq 'error' ) ?
1 : 0, # error
407 ($desired_admin_bind_result eq 'error' ) ?
'error_name' : 0, # error_name
408 ($desired_admin_bind_result eq 'error' ) ?
'error_text' : 0 # error_text
412 if ( $desired_bind_result eq 'error' ) {
413 return mock_net_ldap_message
(1,1,'error_name','error_text');
415 return mock_net_ldap_message
(0,0,'','');
423 $remaining_entry = 1;
425 return mock_net_ldap_search
(
427 count
=> ($desired_count_result)
428 ?
$desired_count_result
430 code
=> ( $desired_search_result eq 'error' )
433 error
=> ( $desired_search_result eq 'error' ) ?
1
435 error_text
=> ( $desired_search_result eq 'error' )
438 error_name
=> ( $desired_search_result eq 'error' )
441 shift_entry
=> mock_net_ldap_entry
( 'sampledn', 1 )
451 sub mock_net_ldap_search
{
452 my ($parameters) = @_;
454 my $count = $parameters->{count
};
455 my $code = $parameters->{code
};
456 my $error = $parameters->{error
};
457 my $error_text = $parameters->{error_text
};
458 my $error_name = $parameters->{error_name
};
459 my $shift_entry = $parameters->{shift_entry
};
461 my $mocked_search = Test
::MockObject
->new();
462 $mocked_search->mock( 'count', sub { return $count; } );
463 $mocked_search->mock( 'code', sub { return $code; } );
464 $mocked_search->mock( 'error', sub { return $error; } );
465 $mocked_search->mock( 'error_name', sub { return $error_name; } );
466 $mocked_search->mock( 'error_text', sub { return $error_text; } );
467 $mocked_search->mock( 'shift_entry', sub {
468 if ($remaining_entry) {
475 return $mocked_search;
478 sub mock_net_ldap_message
{
479 my ( $code, $error, $error_name, $error_text ) = @_;
481 my $mocked_message = Test
::MockObject
->new();
482 $mocked_message->mock( 'code', sub { $code } );
483 $mocked_message->mock( 'error', sub { $error } );
484 $mocked_message->mock( 'error_name', sub { $error_name } );
485 $mocked_message->mock( 'error_text', sub { $error_text } );
487 return $mocked_message;
490 sub mock_net_ldap_entry
{
491 my ( $dn, $exists ) = @_;
493 my $mocked_entry = Test
::MockObject
->new();
494 $mocked_entry->mock( 'dn', sub { return $dn; } );
495 $mocked_entry->mock( 'exists', sub { return $exists } );
497 return $mocked_entry;
500 # TODO: Once we remove the global variables in C4::Auth_with_ldap
501 # we shouldn't need this...
503 sub reload_ldap_module
{
504 delete $INC{'C4/Auth_with_ldap.pm'};
505 require C4
::Auth_with_ldap
;
506 C4
::Auth_with_ldap
->import;
513 if ($#args <= 1 || $args[1] eq 'cn=Manager,dc=metavore,dc=com') {
520 $schema->storage->txn_rollback();