| 1 | import os.path |
| 2 | import sys |
| 3 | |
| 4 | import pytest |
| 5 | |
| 6 | from bonsai import LDAPClient |
| 7 | from bonsai import LDAPEntry |
| 8 | from bonsai import LDAPModOp |
| 9 | from bonsai import LDAPDN |
| 10 | import bonsai.errors |
| 11 | from bonsai.errors import AuthenticationError, InvalidDN |
| 12 | from bonsai.active_directory import UserAccountControl |
| 13 | |
| 14 | |
| 15 | @pytest.fixture |
| 16 | def test_entry(): |
| 17 | """Create a test LDAP entry.""" |
| 18 | gconn = None |
| 19 | entry = None |
| 20 | |
| 21 | def _create_entry(conn, dname): |
| 22 | nonlocal gconn |
| 23 | nonlocal entry |
| 24 | entry = LDAPEntry(dname) |
| 25 | entry["objectclass"] = [ |
| 26 | "top", |
| 27 | "inetOrgPerson", |
| 28 | "person", |
| 29 | "organizationalPerson", |
| 30 | ] |
| 31 | entry["sn"] = "test" |
| 32 | gconn = conn |
| 33 | try: |
| 34 | conn.add(entry) |
| 35 | except bonsai.AlreadyExists: |
| 36 | conn.delete(entry.dn) |
| 37 | conn.add(entry) |
| 38 | return entry |
| 39 | |
| 40 | yield _create_entry |
| 41 | |
| 42 | if gconn.closed: |
| 43 | gconn = gconn.open() |
| 44 | gconn.delete(entry.dn) |
| 45 | gconn.close() |
| 46 | |
| 47 | |
| 48 | @pytest.fixture |
| 49 | def test_ad_user(): |
| 50 | """Create a test AD user.""" |
| 51 | gconn = None |
| 52 | entry = None |
| 53 | |
| 54 | def _create_entry(conn, dname, password): |
| 55 | nonlocal gconn |
| 56 | nonlocal entry |
| 57 | entry = LDAPEntry(dname) |
| 58 | entry["objectclass"] = [ |
| 59 | "top", |
| 60 | "inetOrgPerson", |
| 61 | "person", |
| 62 | "organizationalPerson", |
| 63 | "user", |
| 64 | ] |
| 65 | entry["sn"] = "ad_user" |
| 66 | entry["cn"] = "ad_user" |
| 67 | entry["givenName"] = "AD" |
| 68 | entry["userPrincipalName"] = "ad_user" |
| 69 | entry["displayName"] = "ad_user" |
| 70 | entry["sAMAccountName"] = "ad_user" |
| 71 | entry["userPassword"] = password |
| 72 | entry["mail"] = "[email protected]" |
| 73 | gconn = conn |
| 74 | try: |
| 75 | conn.add(entry) |
| 76 | except bonsai.AlreadyExists: |
| 77 | conn.delete(entry.dn) |
| 78 | conn.add(entry) |
| 79 | return entry |
| 80 | |
| 81 | yield _create_entry |
| 82 | |
| 83 | if gconn.closed: |
| 84 | gconn = gconn.open() |
| 85 | gconn.delete(entry.dn) |
| 86 | gconn.close() |
| 87 | |
| 88 | |
| 89 | def test_set_get(): |
| 90 | """Test LDAPEntry's SetItem, GetItem and get methods.""" |
| 91 | entry = LDAPEntry("cn=test") |
| 92 | entry["sn"] = "Test" |
| 93 | assert entry == {"dn": LDAPDN("cn=test"), "sn": ["Test"]} |
| 94 | entry["givenname"] = "Test" |
| 95 | assert entry.get("None") is None |
| 96 | assert entry.get("GivenName") == entry["givenNAME"] |
| 97 | del entry["sn"] |
| 98 | with pytest.raises(KeyError): |
| 99 | _ = entry["sn"] |
| 100 | |
| 101 | |
| 102 | def test_append_extend(): |
| 103 | """Test append and extend methods of LDAPEntry's attribute.""" |
| 104 | entry = LDAPEntry("cn=test") |
| 105 | entry["givenName"] = "test" |
| 106 | entry["givenname"].append("test2") |
| 107 | assert entry["givenname"] == ["test", "test2"] |
| 108 | assert entry["givenname"][0] == "test" |
| 109 | with pytest.raises(ValueError): |
| 110 | entry["GivenName"].extend(["teSt", "test3"]) |
| 111 | |
| 112 | |
| 113 | def test_pop(): |
| 114 | """Test LDAPEntry's pop method.""" |
| 115 | entry = LDAPEntry("cn=test") |
| 116 | entry["test"] = "test" |
| 117 | with pytest.raises(TypeError): |
| 118 | _ = entry.pop() |
| 119 | with pytest.raises(TypeError): |
| 120 | _ = entry.pop("t", 2, 3) |
| 121 | with pytest.raises(KeyError): |
| 122 | _ = entry.pop("t") |
| 123 | assert entry.pop("test") == ["test"] |
| 124 | assert entry.pop("test", None) is None |
| 125 | |
| 126 | |
| 127 | def test_popitem(): |
| 128 | """Test LDAPEntry's popitem method.""" |
| 129 | entry = LDAPEntry("cn=test") |
| 130 | entry["test"] = "test" |
| 131 | entry["test2"] = "test" |
| 132 | item = entry.popitem() |
| 133 | assert len(item) == 2 |
| 134 | assert item[0] not in entry |
| 135 | entry[item[0]] = item[1] |
| 136 | assert entry[item[0]] == item[1] |
| 137 | |
| 138 | |
| 139 | def test_popitem_empty(): |
| 140 | """Test LDAPEntry's popitem raises KeyError if it is empty.""" |
| 141 | entry = LDAPEntry("cn=test") |
| 142 | with pytest.raises(KeyError): |
| 143 | _ = entry.popitem() |
| 144 | |
| 145 | |
| 146 | def test_clear(): |
| 147 | """Test LDAPEntry's clear method.""" |
| 148 | entry = LDAPEntry("cn=test") |
| 149 | entry["sn"] = ["test1", "test2"] |
| 150 | entry["gn"] = ["test3"] |
| 151 | entry.clear() |
| 152 | assert entry == {"dn": LDAPDN("cn=test")} |
| 153 | assert entry.dn == "cn=test" |
| 154 | |
| 155 | |
| 156 | def test_items(): |
| 157 | """Test LDAPEntry's items method.""" |
| 158 | entry = LDAPEntry("cn=test") |
| 159 | entry["cn"] = "test" |
| 160 | entry["sn"] = "Test" |
| 161 | assert len(entry.items()) == 3 |
| 162 | assert ("dn", entry.dn) in entry.items() |
| 163 | assert ("cn", entry["cn"]) in entry.items() |
| 164 | assert ("sn", entry["sn"]) in entry.items() |
| 165 | assert len(list(entry.items(exclude_dn=True))) == 2 |
| 166 | assert ("dn", entry.dn) not in entry.items(exclude_dn=True) |
| 167 | assert ("cn", entry["cn"]) in entry.items(exclude_dn=True) |
| 168 | assert ("sn", entry["sn"]) in entry.items(exclude_dn=True) |
| 169 | |
| 170 | |
| 171 | def test_keys(): |
| 172 | """Test LDAPEntry's keys method.""" |
| 173 | entry = LDAPEntry("cn=test") |
| 174 | entry["cn"] = "test" |
| 175 | entry["sn"] = "Test" |
| 176 | assert set(entry.keys()) == set(["dn", "cn", "sn"]) |
| 177 | assert set(entry.keys(exclude_dn=True)) == set(["cn", "sn"]) |
| 178 | |
| 179 | |
| 180 | def test_values(): |
| 181 | """Test LDAPEntry's values method.""" |
| 182 | entry = LDAPEntry("cn=test") |
| 183 | entry["cn"] = "test" |
| 184 | entry["sn"] = "Test" |
| 185 | assert len(entry.values()) == 3 |
| 186 | assert entry.dn in entry.values() |
| 187 | assert entry["cn"] in entry.values() |
| 188 | assert entry["sn"] in entry.values() |
| 189 | assert len(list(entry.values(exclude_dn=True))) == 2 |
| 190 | assert entry.dn not in entry.values(exclude_dn=True) |
| 191 | assert entry["cn"] in entry.values(exclude_dn=True) |
| 192 | assert entry["sn"] in entry.values(exclude_dn=True) |
| 193 | |
| 194 | |
| 195 | def test_update(): |
| 196 | """Test updating LDAPEntry object.""" |
| 197 | entry = LDAPEntry("cn=test") |
| 198 | entry.update({"GivenName": "test2", "mail": "test@mail"}) |
| 199 | entry.update([("sn", "test")]) |
| 200 | with pytest.raises(ValueError): |
| 201 | entry.update([("sn", "test", 1)]) |
| 202 | entry.update(uidnumber=1, gidnumber=1) |
| 203 | assert entry["mail"] == ["test@mail"] |
| 204 | assert entry["givenname"] == ["test2"] |
| 205 | assert entry["sn"][0] == "test" |
| 206 | assert entry["uidnumber"] == [1] |
| 207 | assert entry["gidnumber"] == [1] |
| 208 | |
| 209 | |
| 210 | def test_equal(): |
| 211 | """Test equality check.""" |
| 212 | entry1 = LDAPEntry("cn=test") |
| 213 | entry2 = LDAPEntry("cn=test") |
| 214 | entry3 = LDAPEntry("cn=test1") |
| 215 | assert entry1 == entry2 |
| 216 | assert not (entry1 == entry3) |
| 217 | assert entry1 == {"dn": LDAPDN("cn=test")} |
| 218 | assert not (entry1 == 2) |
| 219 | |
| 220 | |
| 221 | def test_special_char(client, basedn): |
| 222 | """Test adding entry with special character in its DN.""" |
| 223 | with client.connect() as conn: |
| 224 | entry = LDAPEntry(r"cn=test\, *\+withspec,%s" % basedn) |
| 225 | entry["objectclass"] = ["top", "inetOrgPerson"] |
| 226 | entry["sn"] = "Test,*special" |
| 227 | conn.add(entry) |
| 228 | result = conn.search(basedn, 1) |
| 229 | entry.delete() |
| 230 | assert entry.dn in [res.dn for res in result] |
| 231 | |
| 232 | |
| 233 | def test_dn_with_space(client, basedn): |
| 234 | """Test adding entry with DN that has a space in it.""" |
| 235 | with client.connect() as conn: |
| 236 | entry = LDAPEntry("cn=test, %s" % basedn) |
| 237 | entry["objectclass"] = ["top", "inetOrgPerson"] |
| 238 | entry["sn"] = "Test with space" |
| 239 | conn.add(entry) |
| 240 | result = conn.search(basedn, 1) |
| 241 | entry.delete() |
| 242 | assert " " in str(entry.dn) |
| 243 | assert entry.dn.rdns[0][0][1] in [res.dn.rdns[0][0][1] for res in result] |
| 244 | |
| 245 | |
| 246 | def test_unicode(client, basedn): |
| 247 | """Test adding entry with special character in its DN.""" |
| 248 | with client.connect() as conn: |
| 249 | dname = "cn=test_µčབྷñ,%s" % basedn |
| 250 | entry = LDAPEntry(dname) |
| 251 | entry["objectclass"] = ["top", "inetOrgPerson"] |
| 252 | entry["sn"] = "unicode_µčབྷñ" |
| 253 | conn.add(entry) |
| 254 | result = conn.search(dname, 0) |
| 255 | entry.delete() |
| 256 | assert dname in [res.dn for res in result] |
| 257 | |
| 258 | |
| 259 | def test_binary(client, basedn): |
| 260 | """Test adding binary data.""" |
| 261 | curdir = os.path.abspath(os.path.dirname(__file__)) |
| 262 | conn = client.connect() |
| 263 | dname = "cn=binary,%s" % basedn |
| 264 | entry = LDAPEntry(dname) |
| 265 | entry["objectclass"] = ["top", "inetOrgPerson"] |
| 266 | entry["sn"] = "binary_test" |
| 267 | with open("%s/testenv/test.jpeg" % curdir, "rb") as image: |
| 268 | entry["jpegPhoto"] = image.read() |
| 269 | conn.add(entry) |
| 270 | result = conn.search(dname, 0) |
| 271 | entry.delete() |
| 272 | conn.close() |
| 273 | assert "jpegPhoto" in result[0].keys() |
| 274 | assert result[0]["jpegphoto"][0] == entry["jpegphoto"][0] |
| 275 | |
| 276 | |
| 277 | def test_connection(client, basedn): |
| 278 | """Test set and get connection object from LDAPEntry.""" |
| 279 | entry = LDAPEntry("cn=test,%s" % basedn) |
| 280 | with pytest.raises(ValueError): |
| 281 | _ = entry.connection |
| 282 | conn = client.connect() |
| 283 | entry.connection = conn |
| 284 | assert entry.connection == conn |
| 285 | with pytest.raises(TypeError): |
| 286 | entry.connection = "string" |
| 287 | with pytest.raises(TypeError): |
| 288 | del entry.connection |
| 289 | |
| 290 | |
| 291 | def test_rename(client, basedn, test_entry): |
| 292 | """Test LDAPEntry's rename LDAP operation.""" |
| 293 | with client.connect() as conn: |
| 294 | entry = test_entry(conn, "cn=test,%s" % basedn) |
| 295 | entry.rename("cn=test2,%s" % basedn) |
| 296 | assert str(entry.dn) == "cn=test2,%s" % basedn |
| 297 | obj = conn.search("cn=test,%s" % basedn, 0) |
| 298 | assert obj == [] |
| 299 | obj = conn.search("cn=test2,%s" % basedn, 0)[0] |
| 300 | assert entry.dn == obj.dn |
| 301 | |
| 302 | |
| 303 | @pytest.mark.skipif( |
| 304 | sys.platform.startswith("win"), reason="Cannot rename entry with old RDN on Windows" |
| 305 | ) |
| 306 | def test_rename_with_old_rdn(client, basedn, test_entry): |
| 307 | """Test LDAPEntry's rename LDAP operation.""" |
| 308 | with client.connect() as conn: |
| 309 | entry = test_entry(conn, "cn=test,%s" % basedn) |
| 310 | entry.rename("uid=test2,%s" % basedn, delete_old_rdn=False) |
| 311 | assert str(entry.dn) == "uid=test2,%s" % basedn |
| 312 | obj = conn.search("cn=test,%s" % basedn, 0) |
| 313 | assert obj == [] |
| 314 | obj = conn.search("uid=test2,%s" % basedn, 0)[0] |
| 315 | assert entry.dn == obj.dn |
| 316 | assert "test2" in obj["uid"] |
| 317 | assert "test" in obj["cn"] |
| 318 | |
| 319 | |
| 320 | def test_rename_error(client, basedn, test_entry): |
| 321 | """Test LDAPEntry's rename error handling.""" |
| 322 | dname = bonsai.LDAPDN("cn=test,%s" % basedn) |
| 323 | entry = LDAPEntry(dname) |
| 324 | with pytest.raises(ValueError): |
| 325 | entry.rename("cn=test2") |
| 326 | with client.connect() as conn: |
| 327 | entry = test_entry(conn, dname) |
| 328 | with pytest.raises(TypeError): |
| 329 | entry.rename(0) |
| 330 | with pytest.raises(TypeError): |
| 331 | entry.rename(0, delete_old_rdn=0) |
| 332 | try: |
| 333 | newdn = bonsai.LDAPDN("cn=test2,ou=invalid,%s" % basedn) |
| 334 | entry.rename(newdn) |
| 335 | except bonsai.LDAPError: |
| 336 | assert entry.dn == dname |
| 337 | with pytest.raises(bonsai.errors.ClosedConnection): |
| 338 | entry.rename("cn=test2") |
| 339 | |
| 340 | |
| 341 | def test_sync_operations(client, basedn): |
| 342 | """ |
| 343 | Test LDAPEntry's add, modify and delete synchronous operations. |
| 344 | """ |
| 345 | entry = LDAPEntry("cn=test,%s" % basedn) |
| 346 | with pytest.raises(ValueError): |
| 347 | entry.modify() |
| 348 | with client.connect() as conn: |
| 349 | entry["sn"] = "test" |
| 350 | with pytest.raises(bonsai.ObjectClassViolation): |
| 351 | conn.add(entry) |
| 352 | entry["objectclass"] = [ |
| 353 | "top", |
| 354 | "inetOrgPerson", |
| 355 | "person", |
| 356 | "organizationalPerson", |
| 357 | ] |
| 358 | try: |
| 359 | conn.add(entry) |
| 360 | except bonsai.AlreadyExists: |
| 361 | conn.delete(entry.dn) |
| 362 | conn.add(entry) |
| 363 | except: |
| 364 | pytest.fail("Adding LDAPEntry to the server is failed.") |
| 365 | entry["sn"] = "Test_modify" |
| 366 | try: |
| 367 | entry.modify() |
| 368 | except: |
| 369 | pytest.fail("Modify failed.") |
| 370 | obj = conn.search("cn=test,%s" % basedn, 0)[0] |
| 371 | assert entry["sn"] == obj["sn"] |
| 372 | try: |
| 373 | entry.delete() |
| 374 | except: |
| 375 | pytest.fail("Delete failed.") |
| 376 | with pytest.raises(bonsai.errors.ClosedConnection): |
| 377 | entry.modify() |
| 378 | |
| 379 | |
| 380 | def test_dn_attr(basedn): |
| 381 | """Test LDAPEntry's DN attribute.""" |
| 382 | entry = LDAPEntry("cn=test,%s" % basedn) |
| 383 | entry.dn = "cn=test" |
| 384 | assert str(entry.dn) == "cn=test" |
| 385 | with pytest.raises(TypeError): |
| 386 | del entry.dn |
| 387 | with pytest.raises(TypeError): |
| 388 | entry["dn"] = 5 |
| 389 | |
| 390 | |
| 391 | def test_wrong_params(): |
| 392 | """Test passing wrong params to LDAPEntry.""" |
| 393 | with pytest.raises(TypeError): |
| 394 | _ = LDAPEntry("", 1) |
| 395 | with pytest.raises(InvalidDN): |
| 396 | _ = LDAPEntry("5", 1) |
| 397 | |
| 398 | |
| 399 | @pytest.mark.skipif( |
| 400 | sys.platform.startswith("win"), reason="Cannot use password policy on Windows" |
| 401 | ) |
| 402 | def test_password_modify(client): |
| 403 | """ |
| 404 | Test modifying password with simple modify operation and |
| 405 | password policy. |
| 406 | """ |
| 407 | cli = LDAPClient(client.url) |
| 408 | user_dn = "cn=jeff,ou=nerdherd,dc=bonsai,dc=test" |
| 409 | cli.set_password_policy(True) |
| 410 | cli.set_credentials("SIMPLE", user_dn, "p@ssword") |
| 411 | conn, _ = cli.connect() |
| 412 | entry = conn.search(user_dn, 0)[0] |
| 413 | try: |
| 414 | entry["userPassword"] = "newpassword" |
| 415 | entry.modify() |
| 416 | except Exception as exc: |
| 417 | assert isinstance(exc, bonsai.errors.PasswordModNotAllowed) |
| 418 | user_dn = "cn=skip,ou=nerdherd,dc=bonsai,dc=test" |
| 419 | cli.set_credentials("SIMPLE", user_dn, "p@ssword") |
| 420 | conn, _ = cli.connect() |
| 421 | entry = conn.search(user_dn, 0)[0] |
| 422 | try: |
| 423 | entry["userPassword"] = "short" |
| 424 | entry.modify() |
| 425 | except Exception as exc: |
| 426 | assert isinstance(exc, bonsai.errors.PasswordTooShort) |
| 427 | try: |
| 428 | entry["userPassword"] = "p@ssword" |
| 429 | entry.modify() |
| 430 | except Exception as exc: |
| 431 | assert isinstance(exc, bonsai.errors.PasswordInHistory) |
| 432 | |
| 433 | |
| 434 | def test_extended_dn_attr(): |
| 435 | """Test that extended dn attribute is read only.""" |
| 436 | entry = LDAPEntry("cn=test") |
| 437 | with pytest.raises(AttributeError): |
| 438 | entry.extended_dn = "cn=test2" |
| 439 | |
| 440 | |
| 441 | def test_change_attribute(client): |
| 442 | """Test change_attribute method.""" |
| 443 | user_dn = "cn=sam,ou=nerdherd,dc=bonsai,dc=test" |
| 444 | if sys.platform == "win32": |
| 445 | multiattr = "otherTelephone" |
| 446 | else: |
| 447 | multiattr = "mail" |
| 448 | with client.connect() as conn: |
| 449 | entry = LDAPEntry(user_dn, conn) |
| 450 | entry.change_attribute("mail", LDAPModOp.ADD, "[email protected]") |
| 451 | assert entry["mail"].status == 1 |
| 452 | entry.modify() |
| 453 | assert conn.search(user_dn, 0)[0]["mail"][0] == "[email protected]" |
| 454 | entry.change_attribute("mail", 1, "[email protected]") |
| 455 | assert entry["mail"].status == 1 |
| 456 | entry.modify() |
| 457 | with pytest.raises(KeyError): |
| 458 | _ = conn.search(user_dn, 0)[0]["mail"] |
| 459 | entry.change_attribute( |
| 460 | multiattr, LDAPModOp.REPLACE, "[email protected]", "[email protected]" |
| 461 | ) |
| 462 | assert entry[multiattr].status == 2 |
| 463 | entry.modify() |
| 464 | res = conn.search(user_dn, 0)[0][multiattr] |
| 465 | assert "[email protected]" in res |
| 466 | assert "[email protected]" in res |
| 467 | entry.change_attribute(multiattr, 1, "[email protected]") |
| 468 | entry.change_attribute(multiattr, 0, "[email protected]") |
| 469 | entry.modify() |
| 470 | res = conn.search(user_dn, 0)[0][multiattr] |
| 471 | assert "[email protected]" in res |
| 472 | assert "[email protected]" in res |
| 473 | entry.change_attribute(multiattr, 1) |
| 474 | entry.modify() |
| 475 | assert multiattr not in conn.search(user_dn, 0)[0].keys() |
| 476 | |
| 477 | |
| 478 | def test_change_attribute_error(client): |
| 479 | """Test change_attribute method's error handling.""" |
| 480 | user_dn = "cn=sam,ou=nerdherd,dc=bonsai,dc=test" |
| 481 | with client.connect() as conn: |
| 482 | entry = LDAPEntry(user_dn, conn) |
| 483 | with pytest.raises(ValueError): |
| 484 | entry.change_attribute("mail", 4, "t") |
| 485 | entry.change_attribute("sn", 0, "Lembeck") |
| 486 | with pytest.raises(bonsai.TypeOrValueExists): |
| 487 | entry.modify() |
| 488 | entry.clear_attribute_changes("sn") |
| 489 | entry.change_attribute("description", LDAPModOp.DELETE, "sam") |
| 490 | with pytest.raises(bonsai.NoSuchAttribute): |
| 491 | entry.modify() |
| 492 | |
| 493 | |
| 494 | def test_clear_attribute_changes(): |
| 495 | """Test clear_attribute_changes method.""" |
| 496 | user_dn = "cn=sam,ou=nerdherd,dc=bonsai,dc=test" |
| 497 | entry = LDAPEntry(user_dn) |
| 498 | entry.change_attribute("uidNumber", 0, 4) |
| 499 | assert entry["uidNumber"].added == [4] |
| 500 | entry.change_attribute("uidNumber", 1, 4) |
| 501 | assert entry["uidNumber"].deleted == [4] |
| 502 | entry.clear_attribute_changes("uidNumber") |
| 503 | assert entry["uidNumber"].status == 0 |
| 504 | assert entry["uidNumber"].added == [] |
| 505 | assert entry["uidNumber"].deleted == [] |
| 506 | |
| 507 | |
| 508 | @pytest.mark.skipif( |
| 509 | sys.platform.startswith("win"), reason="Cannot use ManageDsaIT on Windows" |
| 510 | ) |
| 511 | def test_modify_referrals(client): |
| 512 | """Test modifying an LDAP referral with ManageDdsIT control.""" |
| 513 | refdn = bonsai.LDAPDN("o=invalid-ref,ou=nerdherd-refs,dc=bonsai,dc=test") |
| 514 | newref = "ldap://invalid.host/cn=nobody" |
| 515 | cli = LDAPClient(client.url) |
| 516 | cli.set_credentials(client.mechanism, **client.credentials) |
| 517 | cli.managedsait = True |
| 518 | with cli.connect() as conn: |
| 519 | entry = LDAPEntry(refdn, conn) |
| 520 | entry.change_attribute("ref", LDAPModOp.ADD, newref) |
| 521 | entry.modify() |
| 522 | res = conn.search(refdn, 0, attrlist=["ref"])[0] |
| 523 | assert len(res["ref"]) == 3 |
| 524 | assert newref in res["ref"] |
| 525 | entry.change_attribute("ref", LDAPModOp.DELETE, newref) |
| 526 | entry.modify() |
| 527 | |
| 528 | |
| 529 | @pytest.mark.skipif( |
| 530 | not sys.platform.startswith("win"), |
| 531 | reason="Makes sense when the remote server is an Active Directory", |
| 532 | ) |
| 533 | def test_create_ad_user(cfg, basedn, test_ad_user): |
| 534 | """Test creating and enabling an AD user .""" |
| 535 | url = "ldap://%s:%s/%s??%s" % ( |
| 536 | cfg["SERVER"]["hostname"], |
| 537 | cfg["SERVER"]["port"], |
| 538 | cfg["SERVER"]["basedn"], |
| 539 | cfg["SERVER"]["search_scope"], |
| 540 | ) |
| 541 | cli = LDAPClient(url, tls=True) |
| 542 | cli.set_ca_cert("./tests/testenv/certs/cacert.pem") |
| 543 | cli.set_credentials( |
| 544 | "SIMPLE", user=cfg["SIMPLEAUTH"]["user"], password=cfg["SIMPLEAUTH"]["password"] |
| 545 | ) |
| 546 | ad_user_dn = "cn=ad_user,%s" % basedn |
| 547 | password = "C0mpleX_P4ssW0rd" |
| 548 | with cli.connect() as conn: |
| 549 | entry = test_ad_user(conn, ad_user_dn, password) |
| 550 | res = conn.search(entry.dn, 0) |
| 551 | assert res |
| 552 | uac = UserAccountControl(res[0]["userAccountControl"][0]) |
| 553 | assert uac.value != 66048 |
| 554 | assert uac.properties["accountdisable"] |
| 555 | user_cli = LDAPClient(url, tls=True) |
| 556 | user_cli.set_credentials("SIMPLE", ad_user_dn, password) |
| 557 | with pytest.raises(AuthenticationError): |
| 558 | _ = user_cli.connect() |
| 559 | entry.change_attribute("userAccountControl", LDAPModOp.REPLACE, 66048) |
| 560 | entry.modify() |
| 561 | with user_cli.connect() as uconn: |
| 562 | res = uconn.search(entry.dn, 0) |
| 563 | assert not UserAccountControl(res[0]["userAccountControl"][0]).properties[ |
| 564 | "accountdisable" |
| 565 | ] |
| 566 | assert uconn.whoami() == "u:BONSAI\\ad_user" |