def testKnownValues(self): for s, l in self.knownValues: fromString = dn.DistinguishedName(s) listOfRDNs = [] for av in l: listOfAttributeTypesAndValues = [] for a,v in av: listOfAttributeTypesAndValues.append(dn.LDAPAttributeTypeAndValue(attributeType=a, value=v)) r=dn.RelativeDistinguishedName(listOfAttributeTypesAndValues) listOfRDNs.append(r) fromList = dn.DistinguishedName(listOfRDNs) self.assertEquals(fromString, fromList) fromStringToString = str(fromString) fromListToString = str(fromList) assert fromStringToString == fromListToString canon = fromStringToString # DNs equal their string representation. Note this does # not mean they equal all the possible string # representations -- just the canonical one. self.assertEquals(fromString, canon) self.assertEquals(fromList, canon) self.assertEquals(canon, fromString) self.assertEquals(canon, fromList) # DNs can be used interchangeably with their canonical # string representation as hash keys. self.assertEquals(hash(fromString), hash(canon)) self.assertEquals(hash(fromList), hash(canon)) self.assertEquals(hash(canon), hash(fromString)) self.assertEquals(hash(canon), hash(fromList))
def handle_LDAPModifyDNRequest(self, request, controls, reply): self.checkControls(controls) dn = distinguishedname.DistinguishedName(request.entry) newrdn = distinguishedname.RelativeDistinguishedName(request.newrdn) deleteoldrdn = bool(request.deleteoldrdn) if not deleteoldrdn: #TODO support this raise ldaperrors.LDAPUnwillingToPerform( "Cannot handle preserving old RDN yet.") newSuperior = request.newSuperior if newSuperior is None: newSuperior = dn.up() else: newSuperior = distinguishedname.DistinguishedName(newSuperior) newdn = distinguishedname.DistinguishedName(listOfRDNs=(newrdn, ) + newSuperior.split()) #TODO make this more atomic root = interfaces.IConnectedLDAPEntry(self.factory) d = root.lookup(dn) def _gotEntry(entry): d = entry.move(newdn) return d d.addCallback(_gotEntry) def _report(entry): return pureldap.LDAPModifyDNResponse(resultCode=0) d.addCallback(_report) return d
def _deleteChild(self, rdn): if not isinstance(rdn, distinguishedname.RelativeDistinguishedName): rdn = distinguishedname.RelativeDistinguishedName(stringValue=rdn) for c in self._children: if c.dn.split()[0] == rdn: self._children.remove(c) return c raise ldaperrors.LDAPNoSuchObject, rdn
def testOpenLDAPEqualsEscape(self): """Slapd wants = to be escaped in RDN attributeValues.""" got = dn.DistinguishedName(listOfRDNs=[ dn.RelativeDistinguishedName( attributeTypesAndValues=[ dn.LDAPAttributeTypeAndValue(attributeType='cn', value=r'test'), dn.LDAPAttributeTypeAndValue(attributeType='owner', value=r'uid=foo,ou=depart' +r'ment,dc=example,dc=com'), ]), dn.RelativeDistinguishedName('dc=example'), dn.RelativeDistinguishedName('dc=com'), ]) got = str(got) self.assertEquals(got, r'cn=test+owner=uid\=foo\,ou\=depar' +r'tment\,dc\=example\,dc\=com,dc=ex' +r'ample,dc=com')
def addChild(self, rdn, attributes): """TODO ugly API. Returns the created entry.""" rdn = distinguishedname.RelativeDistinguishedName(rdn) for c in self._children: if c.dn.split()[0] == rdn: raise ldaperrors.LDAPEntryAlreadyExists, c.dn dn = distinguishedname.DistinguishedName(listOfRDNs=(rdn, ) + self.dn.split()) e = ReadOnlyInMemoryLDAPEntry(dn, attributes) e._parent = self self._children.append(e) return e
def addChild(self, rdn, attributes): self._checkState() rdn = distinguishedname.RelativeDistinguishedName(rdn) dn = distinguishedname.DistinguishedName(listOfRDNs=(rdn, ) + self.dn.split()) ldapAttrs = [] for attrType, values in attributes.items(): ldapAttrType = pureldap.LDAPAttributeDescription(attrType) l = [] for value in values: l.append(pureldap.LDAPAttributeValue(value)) ldapValues = pureber.BERSet(l) ldapAttrs.append((ldapAttrType, ldapValues)) op = pureldap.LDAPAddRequest(entry=str(dn), attributes=ldapAttrs) d = self.client.send(op) d.addCallback(self._cbAddDone, dn) return d
def testRDN(self): proto=dn.RelativeDistinguishedName('dc=example') rdn=dn.RelativeDistinguishedName(proto) self.assertEquals(str(rdn), 'dc=example')
def testString(self): rdn=dn.RelativeDistinguishedName('dc=example') self.assertEquals(str(rdn), 'dc=example')