Browse Source

* LDV::LDAP : tested

master
Alex 'AdUser' Z 10 years ago
parent
commit
0ef9b5103c
  1. 14
      lib/LDV/LDAP.pm
  2. 36
      t/LDAP.t

14
lib/LDV/LDAP.pm

@ -59,8 +59,8 @@ sub create {
my $result = $conn->add($dn, attr => [ my $result = $conn->add($dn, attr => [
objectClass => ['inetOrgPerson'], objectClass => ['inetOrgPerson'],
uid => $uid, uid => $uid,
sn => 'just', cn => 'just',
cn => 'created', sn => 'created',
]); ]);
$conn->unbind; $conn->unbind;
return $result->error if ($result->code); return $result->error if ($result->code);
@ -90,9 +90,11 @@ sub get {
my $data = {}; my $data = {};
foreach my $attr ($entry->attributes) { foreach my $attr ($entry->attributes) {
$data->{$attr} = $entry->get_value($attr); $data->{$attr} = $entry->get_value($attr);
utf8::decode($data->{$attr});
} }
delete $data->{userPassword}; delete $data->{userPassword};
delete $data->{objectClass};
return $data; return $data;
} }
@ -109,11 +111,13 @@ sub update {
unless ($data); unless ($data);
my $dn = sprintf "uid=%s,%s", $uid, $self->{userbase}; my $dn = sprintf "uid=%s,%s", $uid, $self->{userbase};
my %upd = ();
while (my ($key, $value) = each(%$attrs)) { while (my ($key, $value) = each(%$attrs)) {
next if ($value and exists $data->{$key}); next unless ($value and exists $data->{$key});
delete $attrs->{$key}; next if ($key eq 'uid'); # rename protection
$upd{$key} = $value;
} }
my $result = $conn->modify($dn, replace => $attrs); my $result = $conn->modify($dn, replace => \%upd);
$conn->unbind; $conn->unbind;
return $result->error if ($result->code); return $result->error if ($result->code);

36
t/LDAP.t

@ -4,21 +4,31 @@ use strict;
use warnings; use warnings;
use utf8; use utf8;
use Test;
use LDV::LDAP; use LDV::LDAP;
use File::Slurp;
use Data::Dumper; use Data::Dumper;
use Test::More tests => 8;
BEGIN { plan test => 2 }; my $hash = read_file('conf/ldv.conf');
my $config = eval "$hash";
SKIP: {
skip "Can't load config", 8 unless (ref($config) eq 'HASH');
my $ldap = LDV::LDAP->new({ my $ldap = LDV::LDAP->new($config->{ldap});
server => '127.0.0.1', isa_ok($ldap, "LDV::LDAP", "LDV::LDAP->new");
binddn => undef, can_ok($ldap, qw(create delete get update));
bindpass => undef,
userbase => undef,
});
print Dumper $ldap->get("ad_user"); is($ldap->get("test20"), undef, "get non-existing user");
#print Dumper $ldap->create("test20"); is($ldap->create("test20"), undef, "creating user");
#print Dumper $ldap->get("test20");
#print Dumper $ldap->delete("test20"); my $attrs = {uid => 'test20', cn => 'just', sn => 'created'};
print Dumper $ldap->get("test20"); is_deeply($ldap->get("test20"), $attrs, "getting data of newly created user");
$attrs = {uid => 'test20', cn => 'Полиграф', sn => 'Шариков'};
is($ldap->update("test20", $attrs), undef, "updating user data");
is_deeply($ldap->get("test20"), $attrs, "getting data of updated user");
is($ldap->delete("test20"), undef, "deleting user");
};
exit 0;

Loading…
Cancel
Save