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_obj_err(self):
""" Test object class violation error. """
entry = LDAPEntry("cn=async_test,%s" % self.basedn)
entry["cn"] = ["async_test"]
with (yield self.client.connect(True, ioloop=self.io_loop)) as conn:
with pytest.raises(bonsai.errors.ObjectClassViolation):
yield conn.add(entry)
entry = LDAPEntry("cn=async_test,%s" % basedn)
entry["objectclass"] = [
"top",
"inetOrgPerson",
"person",
"organizationalPerson",
]
entry["sn"] = "async_test"
oldname = "cn=async_test,%s" % basedn
newname = "cn=async_test2,%s" % basedn
res = await conn.search(newname, 0)
if res:
await res[0].delete()
try:
await conn.add(entry)
except bonsai.errors.AlreadyExists:
await conn.delete(entry.dn)
await conn.add(entry)
except:
pytest.fail("Unexpected error.")
entry["sn"] = "async_test2"
await entry.modify()
await entry.rename(newname)
res = await conn.search(entry.dn, 0, attrlist=["sn"])
assert entry["sn"] == res[0]["sn"]
res = await conn.search(oldname, 0)
assert res == []
await conn.delete(entry.dn)
def test_password_lockout(conn, ipaddr):
""" Test password locking with password policy. """
user_dn = "cn=jeff,ou=nerdherd,dc=bonsai,dc=test"
cli = LDAPClient("ldap://%s" % ipaddr)
cli.set_password_policy(True)
try:
cli.set_credentials("SIMPLE", user_dn, "wrong_pass")
test_conn, ctrl = cli.connect()
except bonsai.errors.AuthenticationError:
with pytest.raises(bonsai.errors.AccountLocked):
cli.set_credentials("SIMPLE", user_dn, "p@ssword")
test_conn, ctrl = cli.connect()
finally:
entry = conn.search(user_dn, 0, attrlist=["pwdAccountLockedTime"])[0]
if "pwdAccountLockedTime" in entry.keys():
del entry["pwdAccountLockedTime"]
entry.modify()
async def test_add_and_delete(client, basedn):
""" Test adding and deleting an LDAP entry. """
async with client.connect(True) as conn:
entry = LDAPEntry("cn=async_test,%s" % basedn)
entry["objectclass"] = [
"top",
"inetOrgPerson",
"person",
"organizationalPerson",
]
entry["sn"] = "async_test"
try:
await conn.add(entry)
except bonsai.errors.AlreadyExists:
await conn.delete(entry.dn)
await conn.add(entry)
except:
pytest.fail("Unexpected error.")
res = await conn.search()
assert entry in res
await entry.delete()
res = await conn.search()
assert entry not in res
def test_add_and_delete(gclient, basedn):
""" Test adding and deleting an LDAP entry. """
with gclient.connect(True) as conn:
entry = LDAPEntry("cn=async_test,%s" % basedn)
entry["objectclass"] = [
"top",
"inetOrgPerson",
"person",
"organizationalPerson",
]
entry["sn"] = "async_test"
try:
conn.add(entry)
except bonsai.errors.AlreadyExists:
conn.delete(entry.dn)
conn.add(entry)
except:
pytest.fail("Unexpected error.")
res = conn.search()
assert entry in res
entry.delete()
res = conn.search()
assert entry not in res
def test_recursive_delete(gclient, basedn):
""" Test removing a subtree recursively. """
org1 = bonsai.LDAPEntry("ou=testusers,%s" % basedn)
org1.update({"objectclass": ["organizationalUnit", "top"], "ou": "testusers"})
org2 = bonsai.LDAPEntry("ou=tops,ou=testusers,%s" % basedn)
org2.update({"objectclass": ["organizationalUnit", "top"], "ou": "tops"})
entry = bonsai.LDAPEntry("cn=tester,ou=tops,ou=testusers,%s" % basedn)
entry.update(
{"objectclass": ["top", "inetorgperson"], "cn": "tester", "sn": "example"}
)
try:
with gclient.connect(True) as conn:
conn.add(org1)
conn.add(org2)
conn.add(entry)
with pytest.raises(bonsai.errors.NotAllowedOnNonleaf):
conn.delete(org1.dn)
conn.delete(org1.dn, recursive=True)
res = conn.search(org1.dn, 2)
assert res == []
except bonsai.LDAPError as err:
pytest.fail("Recursive delete is failed: %s" % err)
def test_setitem():
""" Test setting RDNs for DN object. """
dnobj = LDAPDN("sn=some+gn=thing,dc=test,dc=local")
assert "sn=some+gn=thing" == dnobj[0]
dnobj[0] = "cn=user"
assert "cn=user,dc=test,dc=local" == dnobj
dnobj[1] = "ou=group1,ou=group2"
assert "cn=user,ou=group1,ou=group2,dc=local" == dnobj
dnobj[2:] = "dc=local"
assert "cn=user,ou=group1,dc=local" == dnobj
with pytest.raises(TypeError):
dnobj["invalid"] = "ou=group1,ou=group2"
with pytest.raises(ValueError):
dnobj[0] = 3
with pytest.raises(errors.InvalidDN):
dnobj[1] = "test,group"
def test_invaliddn():
""" Test InvalidDN exception. """
with pytest.raises(errors.InvalidDN):
_ = LDAPDN("cn=test,dc=one+two")
test_conn, ctrl = cli.connect()
entry = test_conn.search(user_dn, 0)[0]
entry["userPassword"] = "newvalidpassword"
entry.modify()
test_conn.close()
cli.set_credentials("SIMPLE", user_dn, "newvalidpassword")
time.sleep(2.0)
test_conn, ctrl = cli.connect()
if not (ctrl["expire"] <= 10 and ctrl["expire"] > 0):
pytest.fail("Expire time is in the wrong range (Expire: %d)." % ctrl["expire"])
test_conn.close()
time.sleep(10)
test_conn, ctrl = cli.connect()
assert ctrl["grace"] == 1
test_conn.close()
with pytest.raises(bonsai.errors.PasswordExpired):
test_conn, ctrl = cli.connect()
entry = conn.search(user_dn, 0, attrlist=["userPassword"])[0]
entry["userPassword"] = "p@ssword"
entry.modify()
entry = conn.search(user_dn, 0, attrlist=["pwdChangeTime", "pwdGraceUseTime"])[0]
if ("pwdChangeTime", "pwdGraceUseTime") in entry.keys():
del entry["pwdChangeTime"]
del entry["pwdGraceUseTime"]
entry.modify()
def test_password_modify(client):
"""
Test modifying password with simple modify operation and
password policy.
"""
cli = LDAPClient(client.url)
user_dn = "cn=jeff,ou=nerdherd,dc=bonsai,dc=test"
cli.set_password_policy(True)
cli.set_credentials("SIMPLE", user_dn, "p@ssword")
conn, _ = cli.connect()
entry = conn.search(user_dn, 0)[0]
try:
entry["userPassword"] = "newpassword"
entry.modify()
except Exception as exc:
assert isinstance(exc, bonsai.errors.PasswordModNotAllowed)
user_dn = "cn=skip,ou=nerdherd,dc=bonsai,dc=test"
cli.set_credentials("SIMPLE", user_dn, "p@ssword")
conn, _ = cli.connect()
entry = conn.search(user_dn, 0)[0]
try:
entry["userPassword"] = "short"
entry.modify()
except Exception as exc:
assert isinstance(exc, bonsai.errors.PasswordTooShort)
try:
entry["userPassword"] = "p@ssword"
entry.modify()
except Exception as exc:
assert isinstance(exc, bonsai.errors.PasswordInHistory)