-
Notifications
You must be signed in to change notification settings - Fork 24
/
tests.py
221 lines (184 loc) · 9.21 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
from fakeldap import MockLDAP, _tupelize
import ldap
import unittest
directory = {
"cn=admin,dc=30loops,dc=net": {
"userPassword": "ldaptest"
}
}
class TestLdapOperations(unittest.TestCase):
def setUp(self):
self.mock_ldap = MockLDAP(directory)
def tearDown(self):
self.mock_ldap.reset()
def test_tupelize(self):
# one level
self.assertEqual('string', _tupelize('string'))
self.assertEqual((1,2,3), _tupelize([1,2,3]))
self.assertEqual( (('a', 1), ('b', 2), ('c', 3),), _tupelize( {'a': 1, 'b': 2, 'c': 3, } ))
# recursively and complex
self.assertEqual( ('string', (1,2,3,), (('a', 1), ('b', 2), ('c', 3),)), _tupelize( ['string', [1,2,3], {'a': 1, 'b': 2, 'c': 3, }, ] ))
def test_simple_bind_s_operation(self):
"""Try to bind a user."""
# Make a valid bind
self.assertEqual(
(97,[]),
self.mock_ldap.simple_bind_s("cn=admin,dc=30loops,dc=net", "ldaptest")
)
# Supply the wrong password
with self.assertRaises(ldap.INVALID_CREDENTIALS):
self.mock_ldap.simple_bind_s("cn=admin,dc=30loops,dc=net", "wrong")
def test_add_s_operation(self):
"""Test the addition of records to the mock ldap object."""
record = [
('uid', 'crito'),
('userPassword', 'secret'),
]
self.assertEqual((105,[],1,[]), self.mock_ldap.add_s(
"uid=crito,ou=people,dc=30loops,dc=net", record
))
directory = {
"cn=admin,dc=30loops,dc=net": {"userPassword": "ldaptest"},
"uid=crito,ou=people,dc=30loops,dc=net": {
"uid": "crito", "userPassword": "secret"}
}
self.assertEqual(directory, self.mock_ldap.directory)
record = [
('uid', 'bas'),
('userPassword', 'secret'),
]
self.assertEqual((105,[],2,[]), self.mock_ldap.add_s(
"uid=bas,ou=people,dc=30loops,dc=net", record
))
def test_modify_s_operation(self):
"""Test the modification of records in the mock ldap object."""
directory = {
"ou=groups,dc=30loops,dc=net": { "ou": ("groups",) },
"cn=users,ou=groups,dc=30loops,dc=net": {
"cn": ("users",),
"memberUid": ( "john", "jack", "john2", "sam", "jim", "ben", ),
},
}
self.mock_ldap = MockLDAP(directory)
modlist_MOD_ADD_non_present_attrdesc = [ (self.mock_ldap.MOD_ADD, 'description', 'Group of all users', ), ]
self.assertEqual((103,[]), self.mock_ldap.modify_s("cn=users,ou=groups,dc=30loops,dc=net", modlist_MOD_ADD_non_present_attrdesc))
self.assertEqual(self.mock_ldap.directory["cn=users,ou=groups,dc=30loops,dc=net"],
{
"cn": ("users",),
"description": ('Group of all users',),
"memberUid": ("john", "jack", "john2", "sam", "jim", "ben", ),
}
)
modlist_MOD_ADD_already_present_attrdesc = [ (self.mock_ldap.MOD_ADD, 'description', 'but not all users on the entire internet', ), ]
self.assertEqual((103,[]), self.mock_ldap.modify_s("cn=users,ou=groups,dc=30loops,dc=net", modlist_MOD_ADD_already_present_attrdesc))
self.assertEqual(self.mock_ldap.directory["cn=users,ou=groups,dc=30loops,dc=net"],
{
"cn": ("users",),
"description": ('Group of all users', 'but not all users on the entire internet',),
"memberUid": ("john", "jack", "john2", "sam", "jim", "ben", ),
}
)
modlist_MOD_DELETE_entire_attrdesc = [ (self.mock_ldap.MOD_DELETE, 'description', None, ), ]
self.assertEqual((103,[]), self.mock_ldap.modify_s("cn=users,ou=groups,dc=30loops,dc=net", modlist_MOD_DELETE_entire_attrdesc))
self.assertEqual(self.mock_ldap.directory["cn=users,ou=groups,dc=30loops,dc=net"],
{
"cn": ("users",),
"memberUid": ("john", "jack", "john2", "sam", "jim", "ben", ),
}
)
modlist_MOD_DELETE_one_value = [ (self.mock_ldap.MOD_DELETE, 'memberUid', 'jack', ), ]
self.assertEqual((103,[]), self.mock_ldap.modify_s("cn=users,ou=groups,dc=30loops,dc=net", modlist_MOD_DELETE_one_value))
self.assertEqual(self.mock_ldap.directory["cn=users,ou=groups,dc=30loops,dc=net"],
{
"cn": ("users",),
"memberUid": ("john", "john2", "sam", "jim", "ben", ),
}
)
modlist_MOD_DELETE_several_values = [ (self.mock_ldap.MOD_DELETE, 'memberUid', ['john', 'sam', 'ben'], ), ]
self.assertEqual((103,[]), self.mock_ldap.modify_s("cn=users,ou=groups,dc=30loops,dc=net", modlist_MOD_DELETE_several_values))
self.assertEqual(self.mock_ldap.directory["cn=users,ou=groups,dc=30loops,dc=net"],
{
"cn": ("users",),
"memberUid": ("john2", "jim", ),
}
)
modlist_MOD_REPLACE_all_different = [ (self.mock_ldap.MOD_REPLACE, 'memberUid', ['wilhelm', 'bernd', 'karl'], ), ]
self.assertEqual((103,[]), self.mock_ldap.modify_s("cn=users,ou=groups,dc=30loops,dc=net", modlist_MOD_REPLACE_all_different))
self.assertEqual(self.mock_ldap.directory["cn=users,ou=groups,dc=30loops,dc=net"],
{
"cn": ("users",),
"memberUid": ("wilhelm", "bernd", "karl", ),
}
)
def test_search_s_base(self):
result = self.mock_ldap.search_s("cn=admin,dc=30loops,dc=net", ldap.SCOPE_BASE)
self.assertEqual(result, [('cn=admin,dc=30loops,dc=net', {'userPassword': 'ldaptest'})])
def test_search_s_onelevel(self):
directory = {
"ou=users,dc=30loops,dc=net": { "ou": "users" },
"ou=groups,dc=30loops,dc=net": { "ou": "groups" },
"cn=admin,ou=users,dc=30loops,dc=net": {
"userPassword": "ldaptest"
},
"cn=john,ou=users,dc=30loops,dc=net": {
"userPassword": "ldaptest",
"mail": "john@example.com"
},
"cn=jack,ou=users,dc=30loops,dc=net": {
# test [value, ] format here
"userPassword": ["ldaptest", ],
"mail": ["jack@example.com", ]
},
"cn=john2,ou=users,dc=30loops,dc=net": {
"userPassword": "ldaptest",
"mail": "john@example.com" # same mail as john
},
"cn=group1,ou=groups,dc=30loops,dc=net": {
"objectClass": "groupOfUniqueNames",
"uniqueMember": "cn=john2,ou=users,dc=30loops,dc=net"
}
}
self.mock_ldap = MockLDAP(directory)
result = self.mock_ldap.search_s("dc=30loops,dc=net", ldap.SCOPE_ONELEVEL,
"(mail=jack@example.com)")
# The search is one-level, so the above should return no results:
self.assertEqual(result, [])
result = self.mock_ldap.search_s("ou=users,dc=30loops,dc=net", ldap.SCOPE_ONELEVEL,
"(mail=jack@example.com)")
self.assertEqual(
result,
[('cn=jack,ou=users,dc=30loops,dc=net',
{'userPassword': ['ldaptest'], 'mail': ['jack@example.com']})]
)
result = self.mock_ldap.search_s("ou=users,dc=30loops,dc=net", ldap.SCOPE_ONELEVEL,
"(mail=john@example.com)")
self.assertEqual(len(result), 2)
self.assertIn(
('cn=john,ou=users,dc=30loops,dc=net', {'userPassword': 'ldaptest', 'mail': 'john@example.com'}),
result
)
self.assertIn(
('cn=john2,ou=users,dc=30loops,dc=net', {'userPassword': 'ldaptest', 'mail': 'john@example.com'}),
result
)
result = self.mock_ldap.search_s("dc=30loops,dc=net", ldap.SCOPE_ONELEVEL,
"(mail=nonexistant@example.com)")
self.assertEqual(result, [])
# search for both johns using anded conditions:
result = self.mock_ldap.search_s("ou=users,dc=30loops,dc=net", ldap.SCOPE_ONELEVEL,
"(&(mail=john@example.com)(userPassword=ldaptest))")
self.assertIn(
('cn=john,ou=users,dc=30loops,dc=net',{'mail': 'john@example.com', 'userPassword': 'ldaptest'}),
result
)
self.assertIn(
('cn=john2,ou=users,dc=30loops,dc=net',{'mail': 'john@example.com', 'userPassword': 'ldaptest'}),
result
)
# search for both johns using anded conditions:
result = self.mock_ldap.search_s("ou=groups,dc=30loops,dc=net", ldap.SCOPE_ONELEVEL,
"(&(objectClass=groupOfUniqueNames)(uniqueMember=cn=john2,ou=users,dc=30loops,dc=net))")
self.assertIn(
('cn=group1,ou=groups,dc=30loops,dc=net',{'objectClass': 'groupOfUniqueNames', 'uniqueMember': 'cn=john2,ou=users,dc=30loops,dc=net'}),
result
)