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