Unverified Commit 9fbae923 authored by IKEDA Soji's avatar IKEDA Soji Committed by GitHub
Browse files

Merge pull request #912 from ikedas/issue-893 by ikedas

Data sources: ldap_2level: Returns at most one result not according to select2 parameter
parents e4169a8f 6526e85d
......@@ -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 \
......@@ -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 \
......
......@@ -317,4 +317,5 @@ on 'develop' => sub {
requires 'Test::PerlTidy', '== 20130104';
requires 'Perl::Tidy', '== 20180220';
requires 'Code::TidyAll';
requires 'Test::Net::LDAP', '>= 0.06';
};
......@@ -149,8 +149,6 @@ sub _load_next {
last if $ldap_select eq 'first';
}
last if @retrieved;
}
return [@retrieved];
......
# -*- 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
# -*- 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',
],
],
[ 'CN=random@hotmail.com,OU=PARENTS,OU=PERSONNES,DC=info,DC=example,DC=qc,DC=ca',
attrs => [
cn => 'random@hotmail.com',
mail => 'random@hotmail.com',
kids => ['student2', 'student1'],
],
],
[ 'CN=random2@hotmail.com,OU=PARENTS,OU=PERSONNES,DC=info,DC=example,DC=qc,DC=ca',
attrs => [
cn => 'random2@hotmail.com',
mail => ['random1@hotmail.com', 'random2@hotmail.com'],
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],
[
['random1@hotmail.com', undef],
['random2@hotmail.com', undef],
['random@hotmail.com', 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();
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment