def test_password_modify_extop(conn, ipaddr): """ Test Password Modify extended operation. """ user_dn = LDAPDN("cn=skip,ou=nerdherd,dc=bonsai,dc=test") cli = LDAPClient("ldap://%s" % ipaddr) cli.set_credentials("SIMPLE", str(user_dn), "p@ssword") test_conn = cli.connect() with pytest.raises(TypeError): test_conn.modify_password(new_password=0) test_conn.modify_password(user_dn, "newpassword", "p@ssword") test_conn.close() with pytest.raises(ClosedConnection): test_conn.modify_password() try: cli.set_credentials("SIMPLE", str(user_dn), "newpassword") cli.set_password_policy(True) test_conn, ctrl = cli.connect() newpass = test_conn.modify_password() test_conn.close() assert isinstance(newpass, str) cli.set_credentials("SIMPLE", str(user_dn), newpass) test_conn, ctrl = cli.connect() test_conn.close() except bonsai.AuthenticationError: pytest.fail("Failed to authenticate with the new password.") finally: entry = conn.search(user_dn, 0, attrlist=["userPassword"])[0] entry["userPassword"] = "******" 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_extop(self): """ Test Password Modify extended operation. """ user_dn = LDAPDN("cn=skip,ou=nerdherd,dc=bonsai,dc=test") cli = LDAPClient("ldap://%s" % self.ipaddr) cli.set_credentials("SIMPLE", (str(user_dn), "p@ssword")) conn = cli.connect() self.assertRaises(TypeError, lambda: conn.modify_password(new_password=0)) conn.modify_password(user_dn, "newpassword", "p@ssword") conn.close() self.assertRaises(ClosedConnection, conn.modify_password) try: cli.set_credentials("SIMPLE", (str(user_dn), "newpassword")) cli.set_password_policy(True) conn, ctrl = cli.connect() newpass = conn.modify_password() conn.close() self.assertIsInstance(newpass, str) cli.set_credentials("SIMPLE", (str(user_dn), newpass)) conn, ctrl = cli.connect() conn.close() except bonsai.AuthenticationError: self.fail("Failed to authenticate with the new password.") finally: entry = self.conn.search(user_dn, 0, attrlist=["userPassword"])[0] entry['userPassword'] = "******" entry.modify() entry = self.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_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_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_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_set_bind_properties(self): """ Test setting LDAPURL bind properties. """ url = LDAPURL() def invalid_basedn(): url.basedn = "test" self.assertRaises(InvalidDN, invalid_basedn) url.basedn = LDAPDN("cn=test") self.assertEqual(str(url.basedn), "cn=test")
def test_set_get(): """ Test LDAPEntry's SetItem, GetItem and get methods. """ entry = LDAPEntry("cn=test") entry["sn"] = "Test" assert entry == {"dn": LDAPDN("cn=test"), "sn": ["Test"]} entry["givenname"] = "Test" assert entry.get("None") is None assert entry.get("GivenName") == entry["givenNAME"] del entry["sn"] with pytest.raises(KeyError): _ = entry["sn"]
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_search_with_managedsait_ctrl(self): """ Test searching with manageDsaIT control. """ refdn = LDAPDN("o=admin-ref,ou=nerdherd,dc=bonsai,dc=test") cli = LDAPClient("ldap://%s" % self.ipaddr) with cli.connect() as conn: res = conn.search(refdn, LDAPSearchScope.BASE, attrlist=['ref'])[0] self.assertEqual(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] self.assertEqual(refdn, res.dn) self.assertEqual('ldap://bonsai.test/cn=admin,dc=bonsai,dc=test', res['ref'][0])
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_setitem(self): """ Test setting RDNs for DN object. """ dnobj = LDAPDN("sn=some+gn=thing,dc=test,dc=local") self.assertEqual("sn=some+gn=thing", dnobj[0]) dnobj[0] = "cn=user" self.assertEqual("cn=user,dc=test,dc=local", dnobj) dnobj[1] = "ou=group1,ou=group2" self.assertEqual("cn=user,ou=group1,ou=group2,dc=local", dnobj) dnobj[2:] = "dc=local" self.assertEqual("cn=user,ou=group1,dc=local", dnobj) def str_idx(): dnobj['invalid'] = "ou=group1,ou=group2" self.assertRaises(TypeError, str_idx) def not_str(): dnobj[0] = 3 self.assertRaises(ValueError, not_str) def invalid(): dnobj[1] = "test,group" self.assertRaises(errors.InvalidDN, invalid)
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_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_get_bind_properties(self): """ Test getting LDAPURL bind properties. """ self.assertEqual(self.url.basedn, LDAPDN("cn=test,dc=test")) self.assertEqual(self.url.scope, "base") self.assertEqual(self.url.filter, "(objectclass=*)") self.assertEqual(self.url.attributes, ["sn", "gn"])
def test_search_ldapdn(self): """ Test searching with LDAPDN object. """ ldap_dn = LDAPDN(self.basedn) obj = self.conn.search(ldap_dn, 1) self.assertIsNotNone(obj)
def test_get_bind_properties(valid_ldapurl): """ Test getting LDAPURL bind properties. """ assert valid_ldapurl.basedn == LDAPDN("cn=test,dc=test") assert valid_ldapurl.scope == "base" assert valid_ldapurl.filter_exp == "(objectclass=*)" assert valid_ldapurl.attributes == ["sn", "gn"]
def test_invaliddn(self): """ Test InvalidDN exception. """ self.assertRaises(errors.InvalidDN, lambda: LDAPDN("cn=test,dc=one+two"))
def test_equal(self): """ Test __eq__ method of LDAPDN object. """ self.assertEqual(self.dnobj, LDAPDN(self.strdn))
def test_emptydn(self): """ Test empty distinguished name. """ empty = LDAPDN("") self.assertEqual(empty[1:], "")
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()
def setUp(self): """ Set up distinguished name for testing. """ self.strdn = "cn=user,dc=test,dc=local" self.dnobj = LDAPDN(self.strdn)
def test_special_char(self): """ Test parsing special characters in DN string. """ spec = LDAPDN("cn=special\, name,dc=test,dc=local") self.assertEqual(str(spec), "cn=special\, name,dc=test,dc=local")
def test_invaliddn(): """ Test InvalidDN exception. """ with pytest.raises(errors.InvalidDN): _ = LDAPDN("cn=test,dc=one+two")
def dnobj(): """ Get a LDAPDN object with valid DN. """ return LDAPDN(VALID_STRDN)
def test_emptydn(): """ Test empty distinguished name. """ empty = LDAPDN("") assert empty[1:] == ""
def test_equal(self): """ Test __eq__ method of LDAPDN object. """ self.assertEqual(self.dnobj, LDAPDN(self.strdn)) self.assertEqual(self.dnobj, LDAPDN(self.strdn.title())) self.assertEqual(self.dnobj, self.strdn.upper())