| 1 |
import unittest |
| 2 |
import sys |
| 3 |
|
| 4 |
import pytest |
| 5 |
from conftest import get_config, network_delay |
| 6 |
|
| 7 |
from bonsai import LDAPClient |
| 8 |
from bonsai import LDAPEntry |
| 9 |
import bonsai.errors |
| 10 |
|
| 11 |
|
| 12 |
def dummy(timeout=None): |
| 13 |
def dummy_f(f): |
| 14 |
return f |
| 15 |
|
| 16 |
return dummy_f |
| 17 |
|
| 18 |
|
| 19 |
if sys.platform == "win32" and sys.version_info.minor >= 8: |
| 20 |
|
| 21 |
import asyncio |
| 22 |
|
| 23 |
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy()) |
| 24 |
|
| 25 |
try: |
| 26 |
from tornado import gen |
| 27 |
from tornado.testing import gen_test |
| 28 |
from tornado.testing import AsyncTestCase |
| 29 |
from bonsai.tornado import TornadoLDAPConnection |
| 30 |
|
| 31 |
TestCaseClass = AsyncTestCase |
| 32 |
MOD_INSTALLED = True |
| 33 |
except ImportError: |
| 34 |
TestCaseClass = unittest.TestCase |
| 35 |
gen_test = dummy |
| 36 |
MOD_INSTALLED = False |
| 37 |
|
| 38 |
|
| 39 |
@pytest.mark.skipif(not MOD_INSTALLED, reason="Tornado is not installed.") |
| 40 |
class TornadoLDAPConnectionTest(TestCaseClass): |
| 41 |
"""Test TornadoLDAPConnection object.""" |
| 42 |
|
| 43 |
def setUp(self): |
| 44 |
"""Set LDAP URL and open connection.""" |
| 45 |
super().setUp() |
| 46 |
self.cfg = get_config() |
| 47 |
self.url = "ldap://%s:%s/%s?%s?%s" % ( |
| 48 |
self.cfg["SERVER"]["hostip"], |
| 49 |
self.cfg["SERVER"]["port"], |
| 50 |
self.cfg["SERVER"]["basedn"], |
| 51 |
self.cfg["SERVER"]["search_attr"], |
| 52 |
self.cfg["SERVER"]["search_scope"], |
| 53 |
) |
| 54 |
self.basedn = self.cfg["SERVER"]["basedn"] |
| 55 |
self.ipaddr = self.cfg["SERVER"]["hostip"] |
| 56 |
self.client = LDAPClient(self.url) |
| 57 |
self.client.set_credentials( |
| 58 |
"SIMPLE", |
| 59 |
user=self.cfg["SIMPLEAUTH"]["user"], |
| 60 |
password=self.cfg["SIMPLEAUTH"]["password"], |
| 61 |
) |
| 62 |
self.client.set_async_connection_class(TornadoLDAPConnection) |
| 63 |
self.io_loop = self.get_new_ioloop() |
| 64 |
|
| 65 |
@gen_test(timeout=20.0) |
| 66 |
def test_connection(self): |
| 67 |
"""Test opening a connection.""" |
| 68 |
conn = yield self.client.connect(True, ioloop=self.io_loop) |
| 69 |
assert conn is not None |
| 70 |
assert not conn.closed |
| 71 |
conn.close() |
| 72 |
|
| 73 |
@gen_test(timeout=20.0) |
| 74 |
def test_search(self): |
| 75 |
"""Test search.""" |
| 76 |
with (yield self.client.connect(True, ioloop=self.io_loop)) as conn: |
| 77 |
res = yield conn.search() |
| 78 |
assert res is not None |
| 79 |
|
| 80 |
@gen_test(timeout=20.0) |
| 81 |
def test_add_and_delete(self): |
| 82 |
"""Test addding and deleting an LDAP entry.""" |
| 83 |
with (yield self.client.connect(True, ioloop=self.io_loop)) as conn: |
| 84 |
entry = LDAPEntry("cn=async_test,%s" % self.basedn) |
| 85 |
entry["objectclass"] = [ |
| 86 |
"top", |
| 87 |
"inetOrgPerson", |
| 88 |
"person", |
| 89 |
"organizationalPerson", |
| 90 |
] |
| 91 |
entry["sn"] = "async_test" |
| 92 |
try: |
| 93 |
yield conn.add(entry) |
| 94 |
except bonsai.errors.AlreadyExists: |
| 95 |
yield conn.delete(entry.dn) |
| 96 |
yield conn.add(entry) |
| 97 |
except: |
| 98 |
self.fail("Unexpected error.") |
| 99 |
res = yield conn.search() |
| 100 |
assert entry in res |
| 101 |
yield entry.delete() |
| 102 |
res = yield conn.search() |
| 103 |
assert entry not in res |
| 104 |
|
| 105 |
@gen_test(timeout=20.0) |
| 106 |
def test_recursive_delete(self): |
| 107 |
"""Test removing a subtree recursively.""" |
| 108 |
org1 = bonsai.LDAPEntry("ou=testusers,%s" % self.basedn) |
| 109 |
org1.update({"objectclass": ["organizationalUnit", "top"], "ou": "testusers"}) |
| 110 |
org2 = bonsai.LDAPEntry("ou=tops,ou=testusers,%s" % self.basedn) |
| 111 |
org2.update({"objectclass": ["organizationalUnit", "top"], "ou": "tops"}) |
| 112 |
entry = bonsai.LDAPEntry("cn=tester,ou=tops,ou=testusers,%s" % self.basedn) |
| 113 |
entry.update( |
| 114 |
{"objectclass": ["top", "inetorgperson"], "cn": "tester", "sn": "example"} |
| 115 |
) |
| 116 |
try: |
| 117 |
with ( |
| 118 |
yield self.client.connect(True, timeout=10.0, ioloop=self.io_loop) |
| 119 |
) as conn: |
| 120 |
yield conn.add(org1) |
| 121 |
yield conn.add(org2) |
| 122 |
yield conn.add(entry) |
| 123 |
with pytest.raises(bonsai.errors.NotAllowedOnNonleaf): |
| 124 |
yield conn.delete(org1.dn) |
| 125 |
yield conn.delete(org1.dn, recursive=True) |
| 126 |
res = yield conn.search(org1.dn, 2) |
| 127 |
assert res == [] |
| 128 |
except bonsai.LDAPError as err: |
| 129 |
self.fail("Recursive delete is failed: %s" % err) |
| 130 |
|
| 131 |
@gen_test(timeout=20.0) |
| 132 |
def test_modify_and_rename(self): |
| 133 |
"""Test modifying and renaming an LDAP entry.""" |
| 134 |
with (yield self.client.connect(True, ioloop=self.io_loop)) as conn: |
| 135 |
entry = LDAPEntry("cn=async_test,%s" % self.basedn) |
| 136 |
entry["objectclass"] = [ |
| 137 |
"top", |
| 138 |
"inetOrgPerson", |
| 139 |
"person", |
| 140 |
"organizationalPerson", |
| 141 |
] |
| 142 |
entry["sn"] = "async_test" |
| 143 |
oldname = "cn=async_test,%s" % self.basedn |
| 144 |
newname = "cn=async_test2,%s" % self.basedn |
| 145 |
res = yield conn.search(newname, 0) |
| 146 |
if res: |
| 147 |
yield res[0].delete() |
| 148 |
try: |
| 149 |
yield conn.add(entry) |
| 150 |
except bonsai.errors.AlreadyExists: |
| 151 |
yield conn.delete(entry.dn) |
| 152 |
yield conn.add(entry) |
| 153 |
except: |
| 154 |
self.fail("Unexpected error.") |
| 155 |
entry["sn"] = "async_test2" |
| 156 |
yield entry.modify() |
| 157 |
yield entry.rename(newname) |
| 158 |
res = yield conn.search(entry.dn, 0, attrlist=["sn"]) |
| 159 |
assert entry["sn"] == res[0]["sn"] |
| 160 |
res = yield conn.search(oldname, 0) |
| 161 |
assert res == [] |
| 162 |
yield conn.delete(entry.dn) |
| 163 |
|
| 164 |
@gen_test(timeout=20.0) |
| 165 |
def test_obj_err(self): |
| 166 |
"""Test object class violation error.""" |
| 167 |
entry = LDAPEntry("cn=async_test,%s" % self.basedn) |
| 168 |
entry["cn"] = ["async_test"] |
| 169 |
with (yield self.client.connect(True, ioloop=self.io_loop)) as conn: |
| 170 |
with pytest.raises(bonsai.errors.ObjectClassViolation): |
| 171 |
yield conn.add(entry) |
| 172 |
|
| 173 |
@gen_test(timeout=20.0) |
| 174 |
def test_whoami(self): |
| 175 |
"""Test whoami.""" |
| 176 |
with (yield self.client.connect(True, ioloop=self.io_loop)) as conn: |
| 177 |
obj = yield conn.whoami() |
| 178 |
expected_res = [ |
| 179 |
"dn:%s" % self.cfg["SIMPLEAUTH"]["user"], |
| 180 |
self.cfg["SIMPLEAUTH"]["adusername"], |
| 181 |
] |
| 182 |
assert obj in expected_res |
| 183 |
|
| 184 |
@gen_test(timeout=12.0) |
| 185 |
def test_connection_timeout(self): |
| 186 |
"""Test connection timeout.""" |
| 187 |
with network_delay(7.0): |
| 188 |
with pytest.raises(gen.TimeoutError): |
| 189 |
yield self.client.connect(True, ioloop=self.io_loop, timeout=8.0) |
| 190 |
|
| 191 |
@gen_test(timeout=18.0) |
| 192 |
def test_search_timeout(self): |
| 193 |
"""Test search timeout.""" |
| 194 |
with (yield self.client.connect(True, ioloop=self.io_loop)) as conn: |
| 195 |
with network_delay(7.0): |
| 196 |
with pytest.raises(gen.TimeoutError): |
| 197 |
yield conn.search(timeout=3.0) |
| 198 |
|
| 199 |
@gen_test(timeout=20.0) |
| 200 |
def test_paged_search(self): |
| 201 |
"""Test paged results control.""" |
| 202 |
self.client.auto_page_acquire = False |
| 203 |
search_dn = "ou=nerdherd,%s" % self.basedn |
| 204 |
with (yield self.client.connect(True, ioloop=self.io_loop)) as conn: |
| 205 |
res = yield conn.paged_search(search_dn, 1, page_size=2) |
| 206 |
for ent in res: |
| 207 |
assert isinstance(ent, bonsai.LDAPEntry) |
| 208 |
page = 1 |
| 209 |
while True: |
| 210 |
if len(res) > 2: |
| 211 |
pytest.fail("The size of the page is greater than expected.") |
| 212 |
msgid = res.acquire_next_page() |
| 213 |
if msgid is None: |
| 214 |
break |
| 215 |
res = yield conn.get_result(msgid) |
| 216 |
page += 1 |
| 217 |
assert page == 3 |
| 218 |
|
| 219 |
@gen_test(timeout=20.0) |
| 220 |
def test_paged_search_with_auto_acq(self): |
| 221 |
"""Test paged search with auto page acquiring.""" |
| 222 |
search_dn = "ou=nerdherd,%s" % self.basedn |
| 223 |
with (yield self.client.connect(True, ioloop=self.io_loop)) as conn: |
| 224 |
res_iter = yield conn.paged_search(search_dn, 1, page_size=3) |
| 225 |
res_iter = type(res_iter).__aiter__(res_iter) |
| 226 |
cnt = 0 |
| 227 |
while True: |
| 228 |
try: |
| 229 |
res = yield type(res_iter).__anext__(res_iter) |
| 230 |
assert isinstance(res, LDAPEntry) |
| 231 |
cnt += 1 |
| 232 |
except StopAsyncIteration: |
| 233 |
break |
| 234 |
assert cnt == 6 |