Subject: | [patch] Implement add and delete attribute operations |
The modify action currently unconditionally does a replace on the
attribute. The attached patch extends this to do add/delete/replace as
requested.
Subject: | attributes.patch |
commit 216dcec229af173f83f84f5d2f51147fba97aab2
Author: Christoph Halbartschlager <cha@atikon.com>
Date: Tue Apr 3 14:07:02 2012 +0200
Implement add and delete attribute operations
diff --git a/lib/Net/LDAP/Server/Test.pm b/lib/Net/LDAP/Server/Test.pm
index bd3df0b..91a8efa 100644
--- a/lib/Net/LDAP/Server/Test.pm
+++ b/lib/Net/LDAP/Server/Test.pm
@@ -414,7 +414,18 @@ Only one user-level method is implemented: new().
my $attr = $mod->{modification}->{type};
my $vals = $mod->{modification}->{vals};
my $entry = $Data{$key};
- $entry->replace( $attr => $vals );
+ if ($mod->{operation} == 0) {
+ $entry->add( $attr => $vals );
+ }
+ elsif ($mod->{operation} == 1) {
+ $entry->delete( $attr => $vals );
+ }
+ elsif ($mod->{operation} == 2) {
+ $entry->replace( $attr => $vals );
+ }
+ else {
+ croak "unknown modify operation: $mod->{operation}";
+ }
}
if ( $self->{_flags}->{active_directory} ) {
diff --git a/t/04attributes.t b/t/04attributes.t
new file mode 100644
index 0000000..21962ee
--- /dev/null
+++ b/t/04attributes.t
@@ -0,0 +1,71 @@
+use Test::More tests => 11;
+
+use strict;
+use warnings;
+use Carp;
+
+use Net::LDAP;
+use Net::LDAP::Server::Test;
+use Net::LDAP::Entry;
+
+#
+# these tests pulled nearly verbatim from the Net::LDAP synopsis
+#
+
+my %opts = (port => '10636');
+
+my $host = 'ldap://localhost:' . $opts{port};
+
+ok( my $server = Net::LDAP::Server::Test->new( $opts{port}, auto_schema => 1 ),
+ "spawn new server" );
+
+ok( my $ldap = Net::LDAP->new( $host, %opts, ), "new LDAP connection" );
+
+ok( my $rc = $ldap->bind(), "LDAP bind()" );
+
+$ldap->add(
+ dn => 'ou=test,dc=test,dc=example',
+ attrs => [
+ id => 'test',
+ cn => [qw(cn1 cn2)],
+ ],
+);
+
+sub fetch_entry {
+ my $mesg;
+ ok( $mesg = $ldap->search( # perform a search
+ base => "ou=test,dc=test,dc=example",
+ scope => 'base',
+ filter => "id=test"
+ ),
+ "LDAP search()"
+ );
+
+ $mesg->code && croak $mesg->error;
+ return ($mesg->entries)[0];
+}
+
+# test deleting of attribute cns
+my $entry = fetch_entry;
+$entry->delete(cn => ['cn2']);
+$entry->update($ldap);
+
+$entry = fetch_entry;
+is_deeply([ $entry->get_value('cn') ], ['cn1']);
+
+# test adding of attribute cns
+$entry->add(cn => ['cn2']);
+$entry->update($ldap);
+
+$entry = fetch_entry;
+is_deeply([ $entry->get_value('cn') ], [qw(cn1 cn2)]);
+
+# test replacing of attribute cns
+$entry->replace(cn => [qw(cn3 cn4)]);
+$entry->update($ldap);
+
+$entry = fetch_entry;
+is_deeply([ $entry->get_value('cn') ], [qw(cn3 cn4)]);
+
+ok( $ldap->unbind, "LDAP unbind()" );
+