-
Notifications
You must be signed in to change notification settings - Fork 12
/
tests.py
executable file
·273 lines (223 loc) · 9.9 KB
/
tests.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import sys
import unittest
import ldapom
import test_server
if sys.version_info[0] >= 3: # Python 3
unicode = str
class LDAPServerMixin(object):
"""Mixin to set up an LDAPConnection connected to a testing LDAP server.
"""
def setUp(self):
self.ldap_server = test_server.LDAPServer()
self.ldap_server.start()
self.ldap_connection = ldapom.LDAPConnection(
uri=self.ldap_server.ldapi_url(),
base='dc=example,dc=com',
bind_dn='cn=admin,dc=example,dc=com',
bind_password='admin')
def tearDown(self):
self.ldap_server.stop()
class LDAPomTest(LDAPServerMixin, unittest.TestCase):
def test_init_with_credentials(self):
# Test normal instantiation with valid credentials
ldapom.LDAPConnection(
uri=self.ldap_server.ldapi_url(),
base="dc=example,dc=com",
bind_dn="cn=Noël,dc=example,dc=com",
bind_password="noel")
# Test with invalid credentials
with self.assertRaises(ldapom.LDAPInvalidCredentialsError):
ldapom.LDAPConnection(
uri=self.ldap_server.ldapi_url(),
base="dc=example,dc=com",
bind_dn="cn=Noël,dc=example,dc=com",
bind_password="invalid")
def test_can_bind(self):
self.assertTrue(self.ldap_connection.can_bind(
bind_dn="cn=Noël,dc=example,dc=com",
bind_password="noel"))
self.assertFalse(self.ldap_connection.can_bind(
bind_dn="cn=Noël,dc=example,dc=com",
bind_password="invalid"))
def test_entry_can_bind(self):
entry = self.ldap_connection.get_entry("cn=Noël,dc=example,dc=com")
self.assertTrue(entry.can_bind("noel"))
self.assertFalse(entry.can_bind("invalid"))
def test_exists(self):
self.assertTrue(self.ldap_connection.get_entry(
"cn=jack,dc=example,dc=com").exists())
self.assertFalse(self.ldap_connection.get_entry(
"cn=nobody,dc=example,dc=com").exists())
self.assertFalse(self.ldap_connection.get_entry(
"cn=umlautä,dc=example,dc=com").exists())
def test_create_entry(self):
entry = self.ldap_connection.get_entry(
"cn=sören.pequeño,dc=example,dc=com")
entry.objectClass = ["person", "top"]
entry.sn = "Sören Pequeño"
entry.cn = "sören.pequeño"
entry.save()
def test_create_entry_with_empty_attribute(self):
entry = self.ldap_connection.get_entry(
"cn=sören.pequeño,dc=example,dc=com")
entry.objectClass = ["person", "top"]
entry.sn = "Sören Pequeño"
entry.cn = "sören.pequeño"
entry.givenName = []
entry.save()
# Verify that the new entry arrived at the server
entry = self.ldap_connection.get_entry(
"cn=sören.pequeño,dc=example,dc=com")
self.assertEqual(entry.sn, {"Sören Pequeño"})
self.assertEqual(entry.cn, {"sören.pequeño"})
def test_delete_entry(self):
entry = self.ldap_connection.get_entry(
"cn=jack,dc=example,dc=com")
self.assertTrue(entry.exists())
entry.delete()
self.assertFalse(entry.exists())
def test_create_attribute(self):
entry = self.ldap_connection.get_entry(
"cn=jack,dc=example,dc=com")
entry.description = "Test user"
entry.save()
entry = self.ldap_connection.get_entry(
"cn=jack,dc=example,dc=com")
entry.fetch()
self.assertEqual(entry.description, {"Test user"})
def test_create_invalid_attribute_name(self):
entry = self.ldap_connection.get_entry(
"cn=jack,dc=example,dc=com")
with self.assertRaises(ldapom.LDAPAttributeNameNotFoundError):
entry.invalidAttribute = 'invalid'
def test_delete_attribute(self):
entry = self.ldap_connection.get_entry(
"cn=jack,dc=example,dc=com")
self.assertEqual(entry.loginShell, "/bin/bash")
del entry.loginShell
entry.save()
entry = self.ldap_connection.get_entry(
"cn=jack,dc=example,dc=com")
self.assertRaises(AttributeError, getattr, entry, "loginShell")
def test_modify_single_value_attribute(self):
entry = self.ldap_connection.get_entry(
"cn=jack,dc=example,dc=com")
self.assertEqual(entry.loginShell, "/bin/bash")
entry.loginShell = "/bin/zsh"
entry.save()
entry = self.ldap_connection.get_entry(
"cn=jack,dc=example,dc=com")
self.assertEqual(entry.loginShell, "/bin/zsh")
entry.loginShell = None
self.assertEqual(entry.loginShell, None)
entry.save()
entry.fetch()
self.assertRaises(AttributeError, getattr, entry, "loginShell")
def test_modify_multi_value_attribute(self):
entry = self.ldap_connection.get_entry(
"cn=jack,dc=example,dc=com")
self.assertEqual(entry.loginShell, "/bin/bash")
entry.sn = "Jaqueline"
entry.save()
entry = self.ldap_connection.get_entry(
"cn=jack,dc=example,dc=com")
self.assertEqual(entry.sn, {"Jaqueline"})
entry.sn = {"Jaqueline", "Jacky"}
entry.save()
entry = self.ldap_connection.get_entry(
"cn=jack,dc=example,dc=com")
self.assertEqual(entry.sn, {"Jaqueline", "Jacky"})
entry = self.ldap_connection.get_entry(
"cn=jack,dc=example,dc=com")
entry.sn.add("Jacko")
entry.save()
entry = self.ldap_connection.get_entry(
"cn=jack,dc=example,dc=com")
self.assertEqual(entry.sn, {"Jaqueline", "Jacky", "Jacko"})
def test_object_class_getter(self):
entry = self.ldap_connection.get_entry(
"cn=daniel,dc=example,dc=com")
self.assertTrue(entry.is_person)
self.assertFalse(entry.is_monkey)
def test_dn_computed_properties(self):
entry = self.ldap_connection.get_entry(
"cn=daniel,dc=example,dc=com")
self.assertEqual(entry.dn, "cn=daniel,dc=example,dc=com")
self.assertEqual(entry.parent_dn, "dc=example,dc=com")
self.assertEqual(entry.rdn, "cn=daniel")
def test_search(self):
result = self.ldap_connection.search("cn=*n*")
self.assertEqual(set(["Noël", "daniel"]),
set([next(iter(r.cn)) for r in result]))
def test_search_empty_result(self):
result = self.ldap_connection.search("cn=nobody")
self.assertRaises(StopIteration, next, result)
def test_rename(self):
entry = self.ldap_connection.get_entry(
"cn=daniel,dc=example,dc=com")
entry.rename("cn=dieter,dc=example,dc=com")
self.assertTrue(entry.exists())
entry = self.ldap_connection.get_entry(
"cn=daniel,dc=example,dc=com")
self.assertFalse(entry.exists())
entry = self.ldap_connection.get_entry(
"cn=dieter,dc=example,dc=com")
self.assertTrue(entry.exists())
self.assertEqual(entry.cn, {"dieter"})
def test_attribute_string_representation(self):
cn_attr = self.ldap_connection.get_attribute_type("cn")("cn")
cn_attr.values = {"günther", }
self.assertEqual(unicode(cn_attr), "cn: günther")
cn_attr.values = {"günther", "gunther"}
self.assertTrue(
unicode(cn_attr) == "cn: günther, gunther" or
unicode(cn_attr) == "cn: gunther, günther")
self.assertEqual(cn_attr.__repr__(), str("<LDAPAttribute cn>"))
def test_entry_save_without_fetch(self):
entry = self.ldap_connection.get_entry(
"cn=daniel,dc=example,dc=com")
self.assertRaises(ldapom.LDAPomError, entry.save)
def test_fetch_nonexistant_entry(self):
entry = self.ldap_connection.get_entry(
"cn=doesnotexist,dc=example,dc=com")
self.assertRaises(ldapom.LDAPNoSuchObjectError, entry.fetch)
def test_set_password(self):
self.assertTrue(self.ldap_connection.can_bind(
bind_dn="cn=Noël,dc=example,dc=com",
bind_password="noel"))
self.ldap_connection.get_entry("cn=Noël,dc=example,dc=com").set_password("new")
self.assertTrue(self.ldap_connection.can_bind(
bind_dn="cn=Noël,dc=example,dc=com",
bind_password="new"))
def test_entry_empty_multi_value_attribute(self):
entry = self.ldap_connection.get_entry("cn=daniel,dc=example,dc=com")
del entry.description
self.assertEqual(set(), entry.description)
# Ensure that saving still works
entry.description.add('superman')
entry.save()
entry = self.ldap_connection.get_entry("cn=daniel,dc=example,dc=com")
self.assertEqual({'superman'}, entry.description)
def test_entry_nonexistant_single_value_attribute(self):
entry = self.ldap_connection.get_entry("cn=daniel,dc=example,dc=com")
del entry.loginShell
with self.assertRaises(AttributeError):
entry.loginShell
def test_retrieve_operational_attributes(self):
# Test for https://github.com/HaDiNet/ldapom/issues/29
entry = self.ldap_connection.get_entry("cn=daniel,dc=example,dc=com",
retrieve_operational_attributes=True)
entry.fetch()
entry.entryCSN
def test_set_binary_attribute(self):
entry = self.ldap_connection.get_entry("cn=daniel,dc=example,dc=com")
# binary_value has a NULL byte in the middle!
fake_jpeg = b'\xff\xd8\xff\xe0\x00\x10\xff'
entry.jpegPhoto = fake_jpeg
entry.save()
entry.fetch()
self.assertEqual(entry.jpegPhoto, {fake_jpeg})
if __name__ == '__main__':
unittest.main()