public void Can_Add_Update_ModifyMembersDynamic_Group() { var members = _context.Query <IntegrationUserTest>().Select(u => u.DistinguishedName).ToList(); members.Count.Should().Be.GreaterThan(0); var group = new DirectoryAttributes("CN=TestGroup,CN=Roles,CN=Employees,DC=Northwind,DC=local"); group.Set("objectClass", new[] { "top", "group" }); group.Set("Member", new Collection <string>(members.GetRange(0, members.Count - 1))); var added = _context.AddAndGet(group); var addedMembers = added.GetStrings("member").ToList(); foreach (var member in members.GetRange(0, members.Count - 1)) { addedMembers.Should().Contain(member); } int removedIndex = members.IndexOf(addedMembers[0]); addedMembers.Should().Not.Contain(members.Last()); addedMembers.RemoveAt(0); addedMembers.Add(members.Last()); added.Set("member", addedMembers); var updated = _context.UpdateAndGet(added); updated.GetStrings("member").Should().Not.Contain(members[removedIndex]) .And.Contain(members.Last()); _context.Delete(updated.DistinguishedName); }
private static IDirectoryAttributes AddEntryIfNecessary(string dn, string objectClass, IDirectoryContext context) { IDirectoryAttributes entry = null; try { entry = context.GetByDN(dn); } catch (DirectoryOperationException ex) { if (ex.Message.IndexOf("object does not exist", StringComparison.OrdinalIgnoreCase) == -1) { throw; } } if (entry == null) { entry = new DirectoryAttributes(dn); entry.Set("objectClass", objectClass); Console.WriteLine("Adding {0}", dn); return(context.AddAndGet(entry)); } Console.WriteLine("{0} already exists", dn); return(entry); }
public void DeleteSubTree() { var container = new DirectoryAttributes("CN=DeleteContainer,CN=Employees,DC=Northwind,DC=local"); container.Set("objectClass", new[] { "top", "container" }); _context.Add(container); var attributes = new DirectoryAttributes("CN=IntegrationTest," + container.DistinguishedName); attributes.SetNull("AccountExpires"); attributes.Set("objectclass", "user"); _context.AddAndGet(attributes); _context.Delete(container.DistinguishedName, new TreeDeleteControl()); Executing.This(() => _context.GetByDN(container.DistinguishedName)) .Should().Throw <DirectoryOperationException>().And.Exception.Message .Should().Contain("does not exist"); }
public void Can_Add_Update_Remove_Dynamic() { var attributes = new DirectoryAttributes("CN=IntegrationTest," + IntegrationUserTest.NamingContext); attributes.SetNull("AccountExpires"); attributes.Set("objectclass", "user"); var added = _context.AddAndGet(attributes); added.Should().Not.Be.Null(); added.GetString("cn").Should().Be.EqualTo("IntegrationTest"); added.GetString("accountexpires").Should().Be.Null(); added.GetGuid("objectguid").Should().Have.Value(); added.GetSecurityIdentifier("objectsid").Should().Not.Be.Null(); added.GetSecurityIdentifiers("objectsid").Should().Not.Be.Empty(); added.GetStrings("objectclass").Should().Have.Count.GreaterThan(1); added.Set("accountExpires", "9223372036854775807").SetNull("manager"); added = _context.UpdateAndGet(added); added.GetString("accountExpires").Should().Be.EqualTo("9223372036854775807"); added.GetDateTime("accountExpires", null).Should().Not.Have.Value(); added.GetString("manager").Should().Be.Null(); var renamed = _context.RenameEntry(added.DistinguishedName, "IntegrationTest2"); var moved = _context.MoveEntry(renamed, IntegrationUserTest.NamingContext2); _context.Delete(moved); Executing.This(() => _context.GetByDN(moved)) .Should().Throw <DirectoryOperationException>().And.Exception.Message .Should().Contain("does not exist"); }
private static void PropulateDirectoryForInheritance() { AddContainerIfNecessary("CN", "InheritanceTest"); using (var context = new DirectoryContext()) { int entryCount = context.Query(InheritanceDirectoryContainer).Count(); if (entryCount > 1) { Console.WriteLine("Inheritince Directory already populated with {0} entries", entryCount); return; } } var users = new DirectoryAttributes[10000]; Parallel.For(0, 10000, i => { var firstName = GetRandomName() + i; var lastName = GetRandomName() + i; var distinguishedName = "CN=" + firstName + " " + lastName + "," + InheritanceDirectoryContainer; var user = new DirectoryAttributes(distinguishedName); var objectClass = i % 5 == 0 ? "user" : (i % 3 == 0 ? "organizationalPerson" : "person"); user.Set("objectClass", objectClass); if (objectClass == "user") { user.Set("givenname", firstName) .Set("sn", lastName) .Set("employeeid", 50 + i) .Set("comment", "This entry was generated by random data.") .Set("telephonenumber", "(123) 555-9857") .Set("title", GetRandomTitle()) .Set("street", "1234 Cool St.") .Set("l", GetRandomCity()) .Set("c", GetRandomCountry()) .Set("PostalCode", "12345"); } else if (objectClass == "person") { user.Set("sn", lastName); } else { user.Set("givenname", firstName) .Set("sn", lastName) .Set("employeeid", 50 + i) .Set("comment", "This entry was generated by random data.") .Set("telephonenumber", "(123) 555-9857") .Set("title", GetRandomTitle()) .Set("street", "1234 Cool St.") .Set("l", GetRandomCity()) .Set("c", GetRandomCountry()) .Set("postalCode", "12345"); } users[i] = user; }); foreach (var user in users.AsParallel()) { using (var context = new DirectoryContext()) { context.Add(user); } Console.WriteLine("{0} added", user.DistinguishedName); } }