Skip to content

Commit

Permalink
Merge pull request #912 from ikedas/issue-893 by ikedas
Browse files Browse the repository at this point in the history
Data sources: ldap_2level: Returns at most one result not according to select2 parameter
  • Loading branch information
ikedas authored Mar 23, 2020
2 parents e4169a8 + 6526e85 commit 9fbae92
Show file tree
Hide file tree
Showing 5 changed files with 181 additions and 2 deletions.
2 changes: 2 additions & 0 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ SUBDIRS = src default doc po www
check_SCRIPTS = \
t/01_Conf.t \
t/Config_XML.t \
t/DataSource_LDAP2.t \
t/DatabaseManager.t \
t/Database_LDAP.t \
t/Language.t \
Expand All @@ -49,6 +50,7 @@ check_SCRIPTS = \
t/parse_templates.t \
t/pod-syntax.t
check_DATA = \
src/lib/Sympa/Test/MockLDAP.pm \
t/data/list_data/test/config \
t/data/sympa.conf \
t/data/sympa.sqlite \
Expand Down
1 change: 1 addition & 0 deletions cpanfile
Original file line number Diff line number Diff line change
Expand Up @@ -317,4 +317,5 @@ on 'develop' => sub {
requires 'Test::PerlTidy', '== 20130104';
requires 'Perl::Tidy', '== 20180220';
requires 'Code::TidyAll';
requires 'Test::Net::LDAP', '>= 0.06';
};
2 changes: 0 additions & 2 deletions src/lib/Sympa/DataSource/LDAP.pm
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,6 @@ sub _load_next {

last if $ldap_select eq 'first';
}

last if @retrieved;
}

return [@retrieved];
Expand Down
67 changes: 67 additions & 0 deletions src/lib/Sympa/Test/MockLDAP.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# -*- indent-tabs-mode: nil; -*-
# vim:ft=perl:et:sw=4

package Sympa::Test::MockLDAP;

use strict;
use warnings;
use Test::Net::LDAP::Util qw(ldap_mockify);

use Sympa::DatabaseDriver::LDAP;

sub build {
my @entries = @_;

no warnings qw(redefine);
*Sympa::DatabaseDriver::LDAP::_connect = sub {
my $ldap;
ldap_mockify {
$ldap = Net::LDAP->new;

foreach my $entry (@entries) {
$ldap->add(@$entry);
}
};
$ldap;
};
}

1;
__END__
=encoding UTF-8
=head1 NAME
Sympa::Test::MockLDAP - Mocking LDAP directory
=head1 DESCRIPTION
L<Sympa::Test::MockLDAP> mocks LDAP directory on memory so that it will be
used in unit tests.
=head2 Functions
=over
=item build ( entries... )
Builds mocked directory on memory.
I<entries...> is a list of arrayref to arguments fed to add().
=back
=head1 SEE ALSO
L<Sympa::DatabaseDriver::LDAP>,
L<Sympa::DataSource::LDAP>,
L<Sympa::DataSource::LDAP2>.
=head1 HISTORY
L<Sympa::Test::MockLDAP> appeared on Sympa 6.2.55b.
=cut
111 changes: 111 additions & 0 deletions t/DataSource_LDAP2.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
# -*- indent-tabs-mode: nil; -*-
# vim:ft=perl:et:sw=4

use strict;
use warnings;
use Data::Dumper;
use English qw(-no_match_vars);
use Test::More;
BEGIN { eval 'use Sympa::Test::MockLDAP'; }

unless (eval 'Test::Net::LDAP::Util->can("ldap_mockify")') {
plan skip_all => 'Test::Net::LDAP required';
}

$Data::Dumper::Terse = 1;
$Data::Dumper::Indent = 0;

use_ok('Sympa::DataSource::LDAP2');

my $fake_list = bless {
name => 'list1',
domain => 'mail.example.org',
} => 'Sympa::List';

Sympa::Test::MockLDAP::build(
[ 'CN=student1,OU=ELEVES,OU=PERSONNES,DC=info,DC=example,DC=qc,DC=ca',
attrs => [
cn => 'student1',
businessCategory => '706',
departmentNumber => '023',
],
],
[ '[email protected],OU=PARENTS,OU=PERSONNES,DC=info,DC=example,DC=qc,DC=ca',
attrs => [
cn => '[email protected]',
mail => '[email protected]',
kids => ['student2', 'student1'],
],
],
[ '[email protected],OU=PARENTS,OU=PERSONNES,DC=info,DC=example,DC=qc,DC=ca',
attrs => [
cn => '[email protected]',
mail => ['[email protected]', '[email protected]'],
kids => ['student2', 'student1'],
],
],
);

my $ds;
my @res;

$ds = Sympa::DataSource->new(
'LDAP2', 'member',
context => $fake_list,
name => 'parent023706',
suffix1 => 'OU=ELEVES,OU=PERSONNES,DC=info,DC=example,DC=qc,DC=ca',
filter1 => '(&(departmentNumber=023)(businessCategory=706))',
scope1 => 'sub',
select1 => 'all',
attrs1 => 'cn',
timeout1 => '60',
suffix2 => 'OU=PARENTS,OU=PERSONNES,dc=info,dc=example,dc=qc,dc=ca',
filter2 => '(kids=[attrs1])',
scope2 => 'sub',
select2 => 'all',
attrs2 => 'mail',
timeout2 => '60',
);
isa_ok $ds, 'Sympa::DataSource::LDAP2';
ok $ds->open, 'open()';

@res = ();
while (my $ent = $ds->next) {
push @res, $ent;
}
is_deeply [sort { $a->[0] cmp $b->[0] } @res],
[
['[email protected]', undef],
['[email protected]', undef],
['[email protected]', undef]
],
'LDAP 2-level data source with select=all';
diag Dumper([@res]);

$ds = Sympa::DataSource->new(
'LDAP2', 'member',
context => $fake_list,
name => 'parent023706',
suffix1 => 'OU=ELEVES,OU=PERSONNES,DC=info,DC=example,DC=qc,DC=ca',
filter1 => '(&(departmentNumber=023)(businessCategory=706))',
scope1 => 'sub',
select1 => 'all',
attrs1 => 'cn',
timeout1 => '60',
suffix2 => 'OU=PARENTS,OU=PERSONNES,dc=info,dc=example,dc=qc,dc=ca',
filter2 => '(kids=[attrs1])',
scope2 => 'sub',
select2 => 'first',
attrs2 => 'mail',
timeout2 => '60',
);
$ds->open or die;

@res = ();
while (my $ent = $ds->next) {
push @res, $ent;
}
is scalar(@res), 2, 'LDAP 2-level data source with select=first';
diag Dumper([@res]);

done_testing();

0 comments on commit 9fbae92

Please sign in to comment.