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_get_address(valid_ldapurl):
""" Test get_address method. """
ldapi_url = LDAPURL("ldapi://%2Ftmp%2Fldapi")
assert valid_ldapurl.get_address() == "ldaps://testurl:444"
assert ldapi_url.get_address() == "ldapi://%2Ftmp%2Fldapi"
def test_conversion():
""" Test ValueError exception for invalid URL format. """
with pytest.raises(ValueError):
_ = LDAPURL("ldap://failed.com/?falsedn?d")
def test_str(valid_ldapurl):
""" Test __str__ method of LDAPURL. """
assert (
str(valid_ldapurl)
== "ldaps://testurl:444/cn=test,dc=test?sn,gn?base?(objectclass=*)?1.2.3.4"
)
assert str(LDAPURL("ldap://127.0.0.1/cn=x?cn")) == "ldap://127.0.0.1:389/cn=x?cn"
assert str(LDAPURL("ldap:///")) == "ldap://localhost:389"
assert str(LDAPURL("ldapi:///")) == "ldapi://localhost"
assert not LDAPURL("ldap:///") == "http://localhost:389"
assert "
def test_references_prop(host_url):
""" Testing references property. """
client = LDAPClient(host_url)
reflist = [LDAPURL("ldap://localhost"), host_url]
ref = LDAPReference(client, reflist)
assert ref.references == reflist
with pytest.raises(ValueError):
ref.references = None
def test_get_host_properties(valid_ldapurl):
""" Test getting LDAPURL host properties. """
ldapi_url = LDAPURL("ldapi://%2Ftmp%2Fldapi")
assert valid_ldapurl.scheme == "ldaps"
assert valid_ldapurl.host == "testurl"
assert valid_ldapurl.port == 444
assert ldapi_url.scheme == "ldapi"
assert ldapi_url.port == 0
def test_set_host_properties():
""" Test setting LDAPURL host properties. """
url = LDAPURL()
with pytest.raises(ValueError):
url.host = ":malformed,@äđĐ-"
with pytest.raises(ValueError):
url.port = "9922"
with pytest.raises(ValueError):
url.scheme = "http"
url.host = "testurl2"
url.port = 589
url.scheme = "ldap"
assert url.scheme == "ldap"
assert url.host == "testurl2"
assert url.port == 589
def test_invalid():
""" Test invalid LDAP URLs. """
with pytest.raises(ValueError):
_ = LDAPURL("http://localhost")
with pytest.raises(ValueError):
_ = LDAPURL("ldaps://localost.")
def test_init_errors(host_url):
""" Testing errors during initialization of LDAPReference. """
client = LDAPClient(host_url)
with pytest.raises(TypeError):
_ = LDAPReference(None, ["ldap://a"])
with pytest.raises(TypeError):
_ = LDAPReference(client, [0])
with pytest.raises(ValueError):
_ = LDAPReference(client, ["asd", LDAPURL("ldap://b")])
def test_str(valid_ldapurl):
""" Test __str__ method of LDAPURL. """
assert (
str(valid_ldapurl)
== "ldaps://testurl:444/cn=test,dc=test?sn,gn?base?(objectclass=*)?1.2.3.4"
)
assert str(LDAPURL("ldap://127.0.0.1/cn=x?cn")) == "ldap://127.0.0.1:389/cn=x?cn"
assert str(LDAPURL("ldap:///")) == "ldap://localhost:389"
assert str(LDAPURL("ldapi:///")) == "ldapi://localhost"
assert not LDAPURL("ldap:///") == "http://localhost:389"
assert "
def test_invalid():
""" Test invalid LDAP URLs. """
with pytest.raises(ValueError):
_ = LDAPURL("http://localhost")
with pytest.raises(ValueError):
_ = LDAPURL("ldaps://localost.")