Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_search_with_managedsait_ctrl(ipaddr):
""" Test searching with manageDsaIT control. """
refdn = LDAPDN("o=admin-ref,ou=nerdherd,dc=bonsai,dc=test")
cli = LDAPClient("ldap://%s" % ipaddr)
with cli.connect() as conn:
res = conn.search(refdn, LDAPSearchScope.BASE, attrlist=["ref"])[0]
assert str(res.dn) == "cn=admin,dc=bonsai,dc=test"
cli.set_managedsait(True)
with cli.connect() as conn:
res = conn.search(refdn, LDAPSearchScope.BASE, attrlist=["ref"])[0]
assert refdn == res.dn
assert "ldap://bonsai.test/cn=admin,dc=bonsai,dc=test" == res["ref"][0]
def test_clear():
""" Test LDAPEntry's clear method. """
entry = LDAPEntry("cn=test")
entry["sn"] = ["test1", "test2"]
entry["gn"] = ["test3"]
entry.clear()
assert entry == {"dn": LDAPDN("cn=test")}
assert entry.dn == "cn=test"
def test_special_char():
""" Test parsing special characters in DN string. """
spec = LDAPDN(r"cn=special\, name,dc=test,dc=local")
assert str(spec) == r"cn=special\, name,dc=test,dc=local"
def test_set_bind_properties():
""" Test setting LDAPURL bind properties. """
url = LDAPURL()
with pytest.raises(InvalidDN):
url.basedn = "test"
url.basedn = LDAPDN("cn=test")
assert str(url.basedn) == "cn=test"
def test_search_ldapdn(conn, basedn):
""" Test searching with LDAPDN object. """
ldap_dn = LDAPDN(basedn)
obj = conn.search(ldap_dn, 1)
assert obj is not None
def test_rename_error(client, basedn, test_entry):
""" Test LDAPEntry's rename error handling. """
dname = bonsai.LDAPDN("cn=test,%s" % basedn)
entry = LDAPEntry(dname)
with pytest.raises(ValueError):
entry.rename("cn=test2")
with client.connect() as conn:
entry = test_entry(conn, dname)
with pytest.raises(TypeError):
entry.rename(0)
with pytest.raises(TypeError):
entry.rename(0, delete_old_rdn=0)
try:
newdn = bonsai.LDAPDN("cn=test2,ou=invalid,%s" % basedn)
entry.rename(newdn)
except bonsai.LDAPError:
assert entry.dn == dname
with pytest.raises(bonsai.errors.ClosedConnection):
entry.rename("cn=test2")
def test_equal():
""" Test equality check. """
entry1 = LDAPEntry("cn=test")
entry2 = LDAPEntry("cn=test")
entry3 = LDAPEntry("cn=test1")
assert entry1 == entry2
assert not (entry1 == entry3)
assert entry1 == {"dn": LDAPDN("cn=test")}
assert not (entry1 == 2)
def test_modify_referrals(client):
""" Test modifying an LDAP referral with ManageDdsIT control. """
refdn = bonsai.LDAPDN("o=invalid-ref,ou=nerdherd,dc=bonsai,dc=test")
newref = "ldap://invalid.host/cn=nobody"
cli = LDAPClient(client.url)
cli.set_credentials(client.mechanism, **client.credentials)
cli.managedsait = True
with cli.connect() as conn:
entry = LDAPEntry(refdn, conn)
entry.change_attribute("ref", LDAPModOp.ADD, newref)
entry.modify()
res = conn.search(refdn, 0, attrlist=["ref"])[0]
assert len(res["ref"]) == 3
assert newref in res["ref"]
entry.change_attribute("ref", LDAPModOp.DELETE, newref)
entry.modify()
def test_invaliddn():
""" Test InvalidDN exception. """
with pytest.raises(errors.InvalidDN):
_ = LDAPDN("cn=test,dc=one+two")
def test_equal(dnobj):
""" Test __eq__ method of LDAPDN object. """
assert dnobj == LDAPDN(VALID_STRDN)
assert dnobj == LDAPDN(VALID_STRDN.title())
assert dnobj == VALID_STRDN.upper()