Package ldaptor :: Package test :: Module test_delta
[hide private]
[frames] | no frames]

Source Code for Module ldaptor.test.test_delta

  1  """ 
  2  Test cases for ldaptor.protocols.ldap.delta 
  3  """ 
  4   
  5  from twisted.trial import unittest 
  6  from ldaptor import testutil 
  7  from ldaptor import delta, entry, attributeset, inmemory 
  8  from ldaptor.protocols.ldap import ldapsyntax, distinguishedname, ldaperrors 
  9   
10 -class TestModifications(unittest.TestCase):
11 - def setUp(self):
12 self.foo = ldapsyntax.LDAPEntry( 13 None, 14 dn='cn=foo,dc=example,dc=com', 15 attributes={ 16 'objectClass': ['person'], 17 'cn': ['foo', 'thud'], 18 'sn': ['bar'], 19 'more': ['junk'], 20 })
21
22 - def testAddOld(self):
23 mod = delta.Add('cn', ['quux']) 24 mod.patch(self.foo) 25 26 self.failIf('stuff' in self.foo) 27 self.failUnlessEqual(self.foo['cn'], ['foo', 'thud', 'quux'])
28
29 - def testAddNew(self):
30 mod = delta.Add('stuff', ['val1', 'val2']) 31 mod.patch(self.foo) 32 33 self.failUnlessEqual(self.foo['stuff'], ['val1', 'val2']) 34 self.failUnlessEqual(self.foo['cn'], ['foo', 'thud'])
35
36 - def testDelete(self):
37 mod = delta.Delete('cn', ['thud']) 38 mod.patch(self.foo) 39 40 self.failIf('stuff' in self.foo) 41 self.failUnlessEqual(self.foo['cn'], ['foo'])
42
43 - def testDeleteAll(self):
44 mod = delta.Delete('more') 45 mod.patch(self.foo) 46 47 self.failIf('stuff' in self.foo) 48 self.failUnlessEqual(self.foo['cn'], ['foo', 'thud'])
49
51 mod = delta.Delete('notexist', []) 52 self.assertRaises(KeyError, 53 mod.patch, 54 self.foo)
55
57 mod = delta.Delete('notexist', ['a']) 58 self.assertRaises(KeyError, 59 mod.patch, 60 self.foo)
61
63 mod = delta.Delete('cn', ['notexist']) 64 self.assertRaises(LookupError, 65 mod.patch, 66 self.foo)
67 68
69 - def testReplace_Add(self):
70 mod = delta.Replace('stuff', ['val1', 'val2']) 71 mod.patch(self.foo) 72 73 self.failUnlessEqual(self.foo['stuff'], ['val1', 'val2']) 74 self.failUnlessEqual(self.foo['sn'], ['bar']) 75 self.failUnlessEqual(self.foo['more'], ['junk'])
76
77 - def testReplace_Modify(self):
78 mod = delta.Replace('sn', ['baz']) 79 mod.patch(self.foo) 80 81 self.failIf('stuff' in self.foo) 82 self.failUnlessEqual(self.foo['sn'], ['baz']) 83 self.failUnlessEqual(self.foo['more'], ['junk'])
84
86 mod = delta.Replace('more', []) 87 mod.patch(self.foo) 88 89 self.failIf('stuff' in self.foo) 90 self.failUnlessEqual(self.foo['sn'], ['bar']) 91 self.failIf('more' in self.foo)
92
94 mod = delta.Replace('nonExisting', []) 95 mod.patch(self.foo) 96 97 self.failIf('stuff' in self.foo) 98 self.failUnlessEqual(self.foo['sn'], ['bar']) 99 self.failUnlessEqual(self.foo['more'], ['junk'])
100
101 -class TestModificationOpLDIF(unittest.TestCase):
102 - def testAdd(self):
103 m=delta.Add('foo', ['bar', 'baz']) 104 self.assertEquals(m.asLDIF(), 105 """\ 106 add: foo 107 foo: bar 108 foo: baz 109 - 110 """)
111
112 - def testDelete(self):
113 m=delta.Delete('foo', ['bar', 'baz']) 114 self.assertEquals(m.asLDIF(), 115 """\ 116 delete: foo 117 foo: bar 118 foo: baz 119 - 120 """)
121
122 - def testDeleteAll(self):
123 m=delta.Delete('foo') 124 self.assertEquals(m.asLDIF(), 125 """\ 126 delete: foo 127 - 128 """)
129
130 - def testReplace(self):
131 m=delta.Replace('foo', ['bar', 'baz']) 132 self.assertEquals(m.asLDIF(), 133 """\ 134 replace: foo 135 foo: bar 136 foo: baz 137 - 138 """)
139
140 - def testReplaceAll(self):
141 m=delta.Replace('thud') 142 self.assertEquals(m.asLDIF(), 143 """\ 144 replace: thud 145 - 146 """)
147 148
149 -class TestAddOpLDIF(unittest.TestCase):
150 - def testSimple(self):
151 op=delta.AddOp(entry.BaseLDAPEntry( 152 dn='dc=example,dc=com', 153 attributes={'foo': ['bar', 'baz'], 154 'quux': ['thud']})) 155 self.assertEquals(op.asLDIF(), 156 """\ 157 dn: dc=example,dc=com 158 changetype: add 159 foo: bar 160 foo: baz 161 quux: thud 162 163 """)
164 165
166 -class TestDeleteOpLDIF(unittest.TestCase):
167 - def testSimple(self):
168 op=delta.DeleteOp('dc=example,dc=com') 169 self.assertEquals(op.asLDIF(), 170 """\ 171 dn: dc=example,dc=com 172 changetype: delete 173 174 """)
175 176 177
178 -class TestOperationLDIF(unittest.TestCase):
179 - def testModify(self):
180 op=delta.ModifyOp('cn=Paula Jensen, ou=Product Development, dc=airius, dc=com', 181 [ 182 delta.Add('postaladdress', 183 ['123 Anystreet $ Sunnyvale, CA $ 94086']), 184 delta.Delete('description'), 185 delta.Replace('telephonenumber', ['+1 408 555 1234', '+1 408 555 5678']), 186 delta.Delete('facsimiletelephonenumber', ['+1 408 555 9876']), 187 ]) 188 self.assertEquals(op.asLDIF(), 189 """\ 190 dn: cn=Paula Jensen,ou=Product Development,dc=airius,dc=com 191 changetype: modify 192 add: postaladdress 193 postaladdress: 123 Anystreet $ Sunnyvale, CA $ 94086 194 - 195 delete: description 196 - 197 replace: telephonenumber 198 telephonenumber: +1 408 555 1234 199 telephonenumber: +1 408 555 5678 200 - 201 delete: facsimiletelephonenumber 202 facsimiletelephonenumber: +1 408 555 9876 203 - 204 205 """)
206
207 -class TestModificationComparison(unittest.TestCase):
208 - def testEquality_Add_True(self):
209 a = delta.Add('k', ['b', 'c', 'd']) 210 b = delta.Add('k', ['b', 'c', 'd']) 211 self.assertEquals(a, b)
212
214 a = delta.Add('k', ['b', 'c', 'd']) 215 b = delta.Delete('k', ['b', 'c', 'd']) 216 self.assertNotEquals(a, b)
217
219 a = delta.Add('k', ['b', 'c', 'd']) 220 b = attributeset.LDAPAttributeSet('k', ['b', 'c', 'd']) 221 self.assertNotEquals(a, b)
222
223 - def testEquality_List_False(self):
224 a = delta.Add('k', ['b', 'c', 'd']) 225 b = ['b', 'c', 'd'] 226 self.assertNotEquals(a, b)
227
228 -class TestOperations(unittest.TestCase):
229 - def setUp(self):
230 self.root = inmemory.ReadOnlyInMemoryLDAPEntry( 231 dn=distinguishedname.DistinguishedName('dc=example,dc=com')) 232 self.meta=self.root.addChild( 233 rdn='ou=metasyntactic', 234 attributes={ 235 'objectClass': ['a', 'b'], 236 'ou': ['metasyntactic'], 237 }) 238 self.foo=self.meta.addChild( 239 rdn='cn=foo', 240 attributes={ 241 'objectClass': ['a', 'b'], 242 'cn': ['foo'], 243 }) 244 self.bar=self.meta.addChild( 245 rdn='cn=bar', 246 attributes={ 247 'objectClass': ['a', 'b'], 248 'cn': ['bar'], 249 }) 250 251 self.empty=self.root.addChild( 252 rdn='ou=empty', 253 attributes={ 254 'objectClass': ['a', 'b'], 255 'ou': ['empty'], 256 }) 257 258 self.oneChild=self.root.addChild( 259 rdn='ou=oneChild', 260 attributes={ 261 'objectClass': ['a', 'b'], 262 'ou': ['oneChild'], 263 }) 264 self.theChild=self.oneChild.addChild( 265 rdn='cn=theChild', 266 attributes={ 267 'objectClass': ['a', 'b'], 268 'cn': ['theChild'], 269 })
270
271 - def testAddOp_DNExists(self):
272 foo2 = entry.BaseLDAPEntry( 273 dn='cn=foo,ou=metasyntactic,dc=example,dc=com', 274 attributes={'foo': ['bar', 'baz'], 275 'quux': ['thud']}) 276 op = delta.AddOp(foo2) 277 d = op.patch(self.root) 278 def eb(fail): 279 fail.trap(ldaperrors.LDAPEntryAlreadyExists)
280 d.addCallbacks(testutil.mustRaise, eb) 281 return d
282
283 - def testDeleteOp_DNNotFound(self):
284 op = delta.DeleteOp('cn=nope,dc=example,dc=com') 285 d = op.patch(self.root) 286 def eb(fail): 287 fail.trap(ldaperrors.LDAPNoSuchObject)
288 d.addCallbacks(testutil.mustRaise, eb) 289 return d 290
291 - def testModifyOp_DNNotFound(self):
292 op = delta.ModifyOp('cn=nope,dc=example,dc=com', 293 [delta.Add('foo', ['bar'])]) 294 d = op.patch(self.root) 295 def eb(fail): 296 fail.trap(ldaperrors.LDAPNoSuchObject)
297 d.addCallbacks(testutil.mustRaise, eb) 298 return d 299