forked from zulip/fakeldap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtests.py
115 lines (98 loc) · 4.04 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
from nose.tools import *
from fakeldap import MockLDAP
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_simple_bind_s_operation(self):
"""Try to bind a user."""
# Make a valid bind
eq_(
(97,[]),
self.mock_ldap.simple_bind_s("cn=admin,dc=30loops,dc=net", "ldaptest")
)
# Supply the wrong password
assert_raises(
ldap.INVALID_CREDENTIALS,
self.mock_ldap.simple_bind_s,
who="cn=admin,dc=30loops,dc=net", cred="wrong"
)
def test_add_s_operation(self):
"""Test the addition of records to the mock ldap object."""
record = [
('uid', 'crito'),
('userPassword', 'secret'),
]
eq_((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"}
}
eq_(directory, self.mock_ldap.directory)
record = [
('uid', 'bas'),
('userPassword', 'secret'),
]
eq_((105,[],2,[]), self.mock_ldap.add_s(
"uid=bas,ou=people,dc=30loops,dc=net", record
))
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" },
"cn=admin,ou=users,dc=30loops,dc=net": {
"userPassword": "ldaptest"
},
"cn=john,ou=users,dc=30loops,dc=net": {
"userPassword": "ldaptest",
"mail": "[email protected]"
},
"cn=jack,ou=users,dc=30loops,dc=net": {
# test [value, ] format here
"userPassword": ["ldaptest", ],
"mail": ["[email protected]", ]
},
"cn=john2,ou=users,dc=30loops,dc=net": {
"userPassword": "ldaptest",
"mail": "[email protected]" # same mail as john
}
}
self.mock_ldap = MockLDAP(directory)
result = self.mock_ldap.search_s("dc=30loops,dc=net", ldap.SCOPE_ONELEVEL,
"([email protected])")
# 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,
"([email protected])")
self.assertEqual(
result,
[('cn=jack,ou=users,dc=30loops,dc=net',
{'userPassword': ['ldaptest'], 'mail': ['[email protected]']})]
)
result = self.mock_ldap.search_s("ou=users,dc=30loops,dc=net", ldap.SCOPE_ONELEVEL,
"([email protected])")
self.assertEqual(len(result), 2)
self.assertIn(
('cn=john,ou=users,dc=30loops,dc=net', {'userPassword': 'ldaptest', 'mail': '[email protected]'}),
result
)
self.assertIn(
('cn=john2,ou=users,dc=30loops,dc=net', {'userPassword': 'ldaptest', 'mail': '[email protected]'}),
result
)
result = self.mock_ldap.search_s("dc=30loops,dc=net", ldap.SCOPE_ONELEVEL,
"([email protected])")
self.assertEqual(result, [])