def testComplex(self): a = entry.BaseLDAPEntry( dn="cn=Paula Jensen,ou=Product Development,dc=airius,dc=com", attributes={ "description": ["Something"], "telephonenumber": ["+123 456"], "facsimiletelephonenumber": ["+1 408 555 9876"], }, ) b = entry.BaseLDAPEntry( dn="cn=Paula Jensen,ou=Product Development,dc=airius,dc=com", attributes={ "postalAddress": ["123 Anystreet $ Sunnyvale, CA $ 94086"], "telephonenumber": ["+1 408 555 1234", "+1 408 555 5678"], }, ) result = a.diff(b) self.assertEqual( result, delta.ModifyOp( "cn=Paula Jensen,ou=Product Development,dc=airius,dc=com", [ delta.Add( "postalAddress", ["123 Anystreet $ Sunnyvale, CA $ 94086"] ), delta.Delete("description", ["Something"]), delta.Delete("facsimiletelephonenumber", ["+1 408 555 9876"]), delta.Add( "telephonenumber", ["+1 408 555 1234", "+1 408 555 5678"] ), delta.Delete("telephonenumber", ["+123 456"]), ], ), )
def testComplex(self): a = entry.BaseLDAPEntry( dn='cn=Paula Jensen,ou=Product Development,dc=airius,dc=com', attributes={ 'description': ['Something'], 'telephonenumber': ['+123 456'], 'facsimiletelephonenumber': ['+1 408 555 9876'], }) b = entry.BaseLDAPEntry( dn='cn=Paula Jensen,ou=Product Development,dc=airius,dc=com', attributes={ 'postalAddress': ['123 Anystreet $ Sunnyvale, CA $ 94086'], 'telephonenumber': ['+1 408 555 1234', '+1 408 555 5678'], }) result = a.diff(b) self.assertEqual( result, delta.ModifyOp( 'cn=Paula Jensen,ou=Product Development,dc=airius,dc=com', [ delta.Add('postalAddress', ['123 Anystreet $ Sunnyvale, CA $ 94086']), delta.Delete('description', ['Something']), delta.Delete('facsimiletelephonenumber', ['+1 408 555 9876']), delta.Add('telephonenumber', ['+1 408 555 1234', '+1 408 555 5678']), delta.Delete('telephonenumber', ['+123 456']), ]))
def testModify(self): op=delta.ModifyOp('cn=Paula Jensen, ou=Product Development, dc=airius, dc=com', [ delta.Add('postaladdress', ['123 Anystreet $ Sunnyvale, CA $ 94086']), delta.Delete('description'), delta.Replace('telephonenumber', ['+1 408 555 1234', '+1 408 555 5678']), delta.Delete('facsimiletelephonenumber', ['+1 408 555 9876']), ]) self.assertEquals(op.asLDIF(), """\ dn: cn=Paula Jensen,ou=Product Development,dc=airius,dc=com changetype: modify add: postaladdress postaladdress: 123 Anystreet $ Sunnyvale, CA $ 94086 - delete: description - replace: telephonenumber telephonenumber: +1 408 555 1234 telephonenumber: +1 408 555 5678 - delete: facsimiletelephonenumber facsimiletelephonenumber: +1 408 555 9876 - """)
def testInequalityDiffertnOperations(self): """ Modify operations for same DN but different operations are not equal. """ first = delta.ModifyOp("cn=john,dc=example,dc=com", [delta.Delete("description")]) second = delta.ModifyOp("cn=doe,dc=example,dc=com", [delta.Delete("homeDirectory")]) self.assertNotEqual(first, second)
def testInequalityDiffertnDN(self): """ Modify operations for different DN are not equal. """ first = delta.ModifyOp("cn=john,dc=example,dc=com", [delta.Delete("description")]) second = delta.ModifyOp("cn=doe,dc=example,dc=com", [delta.Delete("description")]) self.assertNotEqual(first, second)
def testHashInequality(self): """ Different modify operations have different hash values. """ first = delta.ModifyOp("cn=john,dc=example,dc=com", [delta.Delete("description")]) second = delta.ModifyOp("cn=john,dc=example,dc=com", [delta.Delete("homeDirectory")]) self.assertNotEqual(first.asLDIF(), second.asLDIF()) self.assertNotEqual(hash(first), hash(second))
def testDeleteAll(self): m=delta.Delete('foo') self.assertEqual(m.asLDIF(), b"""\ delete: foo - """)
def testDelete_Some_OneType(self): a = entry.BaseLDAPEntry( dn="dc=foo", attributes={ "foo": ["bar"], "baz": ["quux", "thud"], }, ) b = entry.BaseLDAPEntry( dn="dc=foo", attributes={ "foo": ["bar"], "baz": ["thud"], }, ) result = a.diff(b) self.assertEqual( result, delta.ModifyOp( "dc=foo", [ delta.Delete("baz", ["quux"]), ], ), )
def testInequalityNotModifyOP(self): """ Modify operations are not equal with other object types. """ sut = delta.ModifyOp("cn=john,dc=example,dc=com", [delta.Delete("description")]) self.assertNotEqual("cn=john,dc=example,dc=com", sut)
def testDelete(self): m = delta.Delete('foo', ['bar', 'baz']) self.assertEqual(m.asLDIF(), b"""\ delete: foo foo: bar foo: baz - """)
def testModification_complex(self): proto = LDIFDeltaDriver() proto.dataReceived(b"""\ version: 1 dn: cn=foo,dc=example,dc=com changetype: modify delete: foo foo: bar - delete: garply - add: thud thud: quux thud: baz - replace: waldo - add: foo foo: baz - replace: thud thud: xyzzy - add: silly - """) proto.connectionLost() self.assertEqual( proto.listOfCompleted, [ delta.ModifyOp( dn=b"cn=foo,dc=example,dc=com", modifications=[ delta.Delete(b"foo", [b"bar"]), delta.Delete(b"garply"), delta.Add(b"thud", [b"quux", b"baz"]), delta.Replace(b"waldo"), delta.Add(b"foo", [b"baz"]), delta.Replace(b"thud", [b"xyzzy"]), delta.Add(b"silly"), ], ), ], )
def testHashEquality(self): """ Modify operations can be hashed and equal objects have the same hash. """ first = delta.ModifyOp("cn=john,dc=example,dc=com", [delta.Delete("description")]) second = delta.ModifyOp("cn=john,dc=example,dc=com", [delta.Delete("description")]) self.assertEqual(first, second) self.assertEqual( first.asLDIF(), second.asLDIF(), "LDIF equality is a precondition for valid hash values", ) self.assertEqual(hash(first), hash(second))
def diff(self, other): """ Compute differences between this and another LDAP entry. @param other: An LDAPEntry to compare to. @return: None if equal, otherwise a ModifyOp that would make this entry look like other. """ assert self.dn == other.dn if self == other: return None r = [] myKeys = set(key for key in self) otherKeys = set(key for key in other) addedKeys = list(otherKeys - myKeys) addedKeys.sort(key=to_bytes) # for reproducability only for added in addedKeys: r.append(delta.Add(added, other[added])) deletedKeys = list(myKeys - otherKeys) deletedKeys.sort(key=to_bytes) # for reproducability only for deleted in deletedKeys: r.append(delta.Delete(deleted, self[deleted])) sharedKeys = list(myKeys & otherKeys) sharedKeys.sort(key=to_bytes) # for reproducability only for shared in sharedKeys: addedValues = list(other[shared] - self[shared]) if addedValues: addedValues.sort(key=to_bytes) # for reproducability only r.append(delta.Add(shared, addedValues)) deletedValues = list(self[shared] - other[shared]) if deletedValues: deletedValues.sort(key=to_bytes) # for reproducability only r.append(delta.Delete(shared, deletedValues)) return delta.ModifyOp(dn=self.dn, modifications=r)
def testRepr(self): """ Getting string representation """ sut = delta.ModifyOp('cn=john,dc=example,dc=com', [delta.Delete('description')]) self.assertEqual( repr(sut), "ModifyOp(dn='cn=john,dc=example,dc=com', " "modifications=[Delete('description', [])])")
def testDelete(self): m = delta.Delete("foo", ["bar", "baz"]) self.assertEqual( m.asLDIF(), b"""\ delete: foo foo: bar foo: baz - """, )
def testInequalityDifferentModifications(self): """ Modify operations with different modifications are not equal """ first = delta.ModifyOp("cn=john,dc=example,dc=com", [delta.Add("description")]) second = delta.ModifyOp("cn=john,dc=example,dc=com", [delta.Delete("description")]) self.assertNotEqual(first, second)
def testAsLDIF(self): """ It will return a LDIF representation of the contained operations. """ sut = delta.ModifyOp( 'cn=Paula Jensen, ou=Dev Ops, dc=airius, dc=com', [ delta.Add( 'postaladdress', ['123 Anystreet $ Sunnyvale, CA $ 94086'], ), delta.Delete('description'), delta.Replace( 'telephonenumber', ['+1 408 555 1234', '+1 408 555 5678'], ), delta.Delete( 'facsimiletelephonenumber', ['+1 408 555 9876']), ] ) result = sut.asLDIF() self.assertEqual(b"""dn: cn=Paula Jensen,ou=Dev Ops,dc=airius,dc=com changetype: modify add: postaladdress postaladdress: 123 Anystreet $ Sunnyvale, CA $ 94086 - delete: description - replace: telephonenumber telephonenumber: +1 408 555 1234 telephonenumber: +1 408 555 5678 - delete: facsimiletelephonenumber facsimiletelephonenumber: +1 408 555 9876 - """, result, )
def testAsLDIF(self): """ It will return a LDIF representation of the contained operations. """ sut = delta.ModifyOp( "cn=Paula Jensen, ou=Dev Ops, dc=airius, dc=com", [ delta.Add( "postaladdress", ["123 Anystreet $ Sunnyvale, CA $ 94086"], ), delta.Delete("description"), delta.Replace( "telephonenumber", ["+1 408 555 1234", "+1 408 555 5678"], ), delta.Delete("facsimiletelephonenumber", ["+1 408 555 9876"]), ], ) result = sut.asLDIF() self.assertEqual( b"""dn: cn=Paula Jensen,ou=Dev Ops,dc=airius,dc=com changetype: modify add: postaladdress postaladdress: 123 Anystreet $ Sunnyvale, CA $ 94086 - delete: description - replace: telephonenumber telephonenumber: +1 408 555 1234 telephonenumber: +1 408 555 5678 - delete: facsimiletelephonenumber facsimiletelephonenumber: +1 408 555 9876 - """, result, )
def testModification_complex(self): proto = LDIFDeltaDriver() proto.dataReceived(b"""\ version: 1 dn: cn=foo,dc=example,dc=com changetype: modify delete: foo foo: bar - delete: garply - add: thud thud: quux thud: baz - replace: waldo - add: foo foo: baz - replace: thud thud: xyzzy - add: silly - """) proto.connectionLost() self.assertEqual(proto.listOfCompleted, [ delta.ModifyOp(dn=b'cn=foo,dc=example,dc=com', modifications=[ delta.Delete(b'foo', [b'bar']), delta.Delete(b'garply'), delta.Add(b'thud', [b'quux', b'baz']), delta.Replace(b'waldo'), delta.Add(b'foo', [b'baz']), delta.Replace(b'thud', [b'xyzzy']), delta.Add(b'silly'), ]), ])
def testDelete_All_OneType(self): a = entry.BaseLDAPEntry(dn='dc=foo', attributes={ 'foo': ['bar'], 'baz': ['quux', 'thud'], }) b = entry.BaseLDAPEntry(dn='dc=foo', attributes={ 'foo': ['bar'], }) result = a.diff(b) self.assertEqual( result, delta.ModifyOp('dc=foo', [ delta.Delete('baz', ['quux', 'thud']), ]))
def __delitem__(self, key): self._checkState() self._canRemoveAll(key) super(LDAPEntryWithClient, self).__delitem__(key) self.journal(delta.Delete(key))
def clear(self): self.ldapObject._canRemoveAll(self.key) super(JournaledLDAPAttributeSet, self).clear() self.ldapObject.journal(delta.Delete(self.key))
def remove(self, value): if value not in self: raise LookupError(value) self.ldapObject._canRemove(self.key, value) self.ldapObject.journal(delta.Delete(self.key, [value])) super(JournaledLDAPAttributeSet, self).remove(value)
def testDelete_FailOnNonExistingAttributeValue(self): mod = delta.Delete('cn', ['notexist']) self.assertRaises(LookupError, mod.patch, self.foo)
def testEquality_AddVsDelete_False(self): a = delta.Add('k', ['b', 'c', 'd']) b = delta.Delete('k', ['b', 'c', 'd']) self.assertNotEquals(a, b)
def testDelete_FailOnNonExistingAttributeType_OneValue(self): mod = delta.Delete('notexist', ['a']) self.assertRaises(KeyError, mod.patch, self.foo)
def testDeleteAll(self): mod = delta.Delete('more') mod.patch(self.foo) self.failIf('stuff' in self.foo) self.failUnlessEqual(self.foo['cn'], ['foo', 'thud'])
def __delitem__(self, key): self._checkState() self._canRemoveAll(key) super().__delitem__(key) self.journal(delta.Delete(key))
def clear(self): self.ldapObject._canRemoveAll(self.key) super().clear() self.ldapObject.journal(delta.Delete(self.key))
def testEquality_AddVsDelete_False(self): a = delta.Add("k", ["b", "c", "d"]) b = delta.Delete("k", ["b", "c", "d"]) self.assertNotEquals(a, b)