| 1 | import sys |
| 2 | import bonsai.errors |
| 3 | from bonsai import get_vendor_info |
| 4 | from bonsai import LDAPClient |
| 5 | from bonsai import LDAPEntry |
| 6 | |
| 7 | import pytest |
| 8 | from conftest import get_config, network_delay |
| 9 | |
| 10 | try: |
| 11 | from gevent import socket |
| 12 | from bonsai.gevent import GeventLDAPConnection |
| 13 | except ImportError: |
| 14 | pass |
| 15 | gevent = pytest.importorskip("gevent") |
| 16 | |
| 17 | |
| 18 | @pytest.fixture(scope="module") |
| 19 | def gclient(): |
| 20 | """ Get an LDAPClient with GeventLDAPConnection async class. """ |
| 21 | cfg = get_config() |
| 22 | url = "ldap://%s:%s/%s?%s?%s" % ( |
| 23 | cfg["SERVER"]["hostip"], |
| 24 | cfg["SERVER"]["port"], |
| 25 | cfg["SERVER"]["basedn"], |
| 26 | cfg["SERVER"]["search_attr"], |
| 27 | cfg["SERVER"]["search_scope"], |
| 28 | ) |
| 29 | cli = LDAPClient(url) |
| 30 | cli.set_credentials( |
| 31 | "SIMPLE", user=cfg["SIMPLEAUTH"]["user"], password=cfg["SIMPLEAUTH"]["password"] |
| 32 | ) |
| 33 | cli.set_async_connection_class(GeventLDAPConnection) |
| 34 | return cli |
| 35 | |
| 36 | |
| 37 | @pytest.fixture |
| 38 | def turn_async_conn(): |
| 39 | bonsai.set_connect_async(True) |
| 40 | yield None |
| 41 | bonsai.set_connect_async(False) |
| 42 | |
| 43 | |
| 44 | def test_connection(gclient): |
| 45 | """ Test opening a connection. """ |
| 46 | conn = gclient.connect(True) |
| 47 | assert conn is not None |
| 48 | assert not conn.closed |
| 49 | |
| 50 | |
| 51 | def test_search(gclient): |
| 52 | """ Test search. """ |
| 53 | with gclient.connect(True) as conn: |
| 54 | res = conn.search() |
| 55 | assert res is not None |
| 56 | |
| 57 | |
| 58 | def test_add_and_delete(gclient, basedn): |
| 59 | """ Test adding and deleting an LDAP entry. """ |
| 60 | with gclient.connect(True) as conn: |
| 61 | entry = LDAPEntry("cn=async_test,%s" % basedn) |
| 62 | entry["objectclass"] = [ |
| 63 | "top", |
| 64 | "inetOrgPerson", |
| 65 | "person", |
| 66 | "organizationalPerson", |
| 67 | ] |
| 68 | entry["sn"] = "async_test" |
| 69 | try: |
| 70 | conn.add(entry) |
| 71 | except bonsai.errors.AlreadyExists: |
| 72 | conn.delete(entry.dn) |
| 73 | conn.add(entry) |
| 74 | except: |
| 75 | pytest.fail("Unexpected error.") |
| 76 | res = conn.search() |
| 77 | assert entry in res |
| 78 | entry.delete() |
| 79 | res = conn.search() |
| 80 | assert entry not in res |
| 81 | |
| 82 | |
| 83 | def test_recursive_delete(gclient, basedn): |
| 84 | """ Test removing a subtree recursively. """ |
| 85 | org1 = bonsai.LDAPEntry("ou=testusers,%s" % basedn) |
| 86 | org1.update({"objectclass": ["organizationalUnit", "top"], "ou": "testusers"}) |
| 87 | org2 = bonsai.LDAPEntry("ou=tops,ou=testusers,%s" % basedn) |
| 88 | org2.update({"objectclass": ["organizationalUnit", "top"], "ou": "tops"}) |
| 89 | entry = bonsai.LDAPEntry("cn=tester,ou=tops,ou=testusers,%s" % basedn) |
| 90 | entry.update( |
| 91 | {"objectclass": ["top", "inetorgperson"], "cn": "tester", "sn": "example"} |
| 92 | ) |
| 93 | try: |
| 94 | with gclient.connect(True) as conn: |
| 95 | conn.add(org1) |
| 96 | conn.add(org2) |
| 97 | conn.add(entry) |
| 98 | with pytest.raises(bonsai.errors.NotAllowedOnNonleaf): |
| 99 | conn.delete(org1.dn) |
| 100 | conn.delete(org1.dn, recursive=True) |
| 101 | res = conn.search(org1.dn, 2) |
| 102 | assert res == [] |
| 103 | except bonsai.LDAPError as err: |
| 104 | pytest.fail("Recursive delete is failed: %s" % err) |
| 105 | |
| 106 | |
| 107 | def test_modify_and_rename(gclient, basedn): |
| 108 | """ Test modifying and renaming LDAP entry. """ |
| 109 | with gclient.connect(True) as conn: |
| 110 | entry = LDAPEntry("cn=async_test,%s" % basedn) |
| 111 | entry["objectclass"] = [ |
| 112 | "top", |
| 113 | "inetOrgPerson", |
| 114 | "person", |
| 115 | "organizationalPerson", |
| 116 | ] |
| 117 | entry["sn"] = "async_test" |
| 118 | oldname = "cn=async_test,%s" % basedn |
| 119 | newname = "cn=async_test2,%s" % basedn |
| 120 | res = conn.search(newname, 0) |
| 121 | if res: |
| 122 | res[0].delete() |
| 123 | try: |
| 124 | conn.add(entry) |
| 125 | except bonsai.errors.AlreadyExists: |
| 126 | conn.delete(entry.dn) |
| 127 | conn.add(entry) |
| 128 | except: |
| 129 | pytest.fail("Unexpected error.") |
| 130 | entry["sn"] = "async_test2" |
| 131 | entry.modify() |
| 132 | entry.rename(newname) |
| 133 | res = conn.search(entry.dn, 0, attrlist=["sn"]) |
| 134 | assert entry["sn"] == res[0]["sn"] |
| 135 | res = conn.search(oldname, 0) |
| 136 | assert res == [] |
| 137 | conn.delete(entry.dn) |
| 138 | |
| 139 | |
| 140 | def test_obj_err(gclient, basedn): |
| 141 | """ Test object class violation error. """ |
| 142 | entry = LDAPEntry("cn=async_test,%s" % basedn) |
| 143 | entry["cn"] = ["async_test"] |
| 144 | with pytest.raises(bonsai.errors.ObjectClassViolation): |
| 145 | with gclient.connect(True) as conn: |
| 146 | conn.add(entry) |
| 147 | |
| 148 | |
| 149 | def test_whoami(gclient): |
| 150 | """ Test whoami. """ |
| 151 | with gclient.connect(True) as conn: |
| 152 | cfg = get_config() |
| 153 | obj = conn.whoami() |
| 154 | expected_res = [ |
| 155 | "dn:%s" % cfg["SIMPLEAUTH"]["user"], |
| 156 | cfg["SIMPLEAUTH"]["adusername"], |
| 157 | ] |
| 158 | assert obj in expected_res |
| 159 | |
| 160 | |
| 161 | @pytest.mark.skipif( |
| 162 | get_vendor_info()[1] < 20445 or sys.platform != "linux", |
| 163 | reason="No async timeout support", |
| 164 | ) |
| 165 | @pytest.mark.timeout(18) |
| 166 | def test_connection_timeout(gclient, turn_async_conn): |
| 167 | """ Test connection timeout. """ |
| 168 | turn_async_conn |
| 169 | with network_delay(6.0): |
| 170 | with pytest.raises(socket.timeout): |
| 171 | gclient.connect(True, timeout=5.0) |