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_comment():
""" Test parsing comment lines in LDIF files. """
ldif = "# DN: cn=test\ndn: cn=test\n#Other comment line.\ncn: test\n"
with StringIO(ldif) as test:
reader = LDIFReader(test)
ent = next(reader)
assert ent.dn == "cn=test"
assert ent["cn"] == ["test"]
multiline = "# A long multiline comment\n in an LDIF file.\ndn: cn=test\n"
with StringIO(multiline) as test:
reader = LDIFReader(test)
ent = next(reader)
assert ent.dn == "cn=test"
def test_init_params():
""" Test constructor parameters for LDIFReader. """
with pytest.raises(TypeError):
_ = LDIFReader("wrong")
with pytest.raises(TypeError):
_ = LDIFReader(StringIO(), max_length=None)
with pytest.raises(TypeError):
_ = LDIFReader(BytesIO())
inp = StringIO()
ldif = LDIFReader(inp, max_length=100)
assert ldif.input_file == inp
assert ldif.max_length == 100
def test_encoded_attributes():
""" Test parsing base64 encoded attributes. """
attr = "test"
text = "version: 1\ndn: cn=test\ncn:: {0}\n".format(
base64.b64encode(attr.encode("UTF-8")).decode("UTF-8")
)
with StringIO(text) as test:
reader = LDIFReader(test)
ent = next(reader)
assert ent.dn == "cn=test"
assert ent["cn"][0] == attr
def test_init_params():
""" Test constructor parameters for LDIFReader. """
with pytest.raises(TypeError):
_ = LDIFReader("wrong")
with pytest.raises(TypeError):
_ = LDIFReader(StringIO(), max_length=None)
with pytest.raises(TypeError):
_ = LDIFReader(BytesIO())
inp = StringIO()
ldif = LDIFReader(inp, max_length=100)
assert ldif.input_file == inp
assert ldif.max_length == 100
def test_url_attribute():
""" Test URL attribute in LDIF file. """
text = "dn: cn=test\ncn: test1\njpegPhoto:< file://./testenv/test.jpeg\n"
with StringIO(text) as test:
test.name = __file__
reader = LDIFReader(test)
ent = next(reader)
assert ent.dn == "cn=test"
assert len(ent["jpegPhoto"][0]) == 1959
assert isinstance(ent["jpegPhoto"][0], bytes)
def test_changetype():
""" Test changetype attribute in LDIF file. """
text = "dn: cn=test\nchangetype: add\ncn: test\n"
with StringIO(text) as test:
reader = LDIFReader(test)
ent = next(reader)
assert ent.dn == "cn=test"
assert "cn" in ent
assert "changetype" not in ent
def test_init_params():
""" Test constructor parameters for LDIFReader. """
with pytest.raises(TypeError):
_ = LDIFReader("wrong")
with pytest.raises(TypeError):
_ = LDIFReader(StringIO(), max_length=None)
with pytest.raises(TypeError):
_ = LDIFReader(BytesIO())
inp = StringIO()
ldif = LDIFReader(inp, max_length=100)
assert ldif.input_file == inp
assert ldif.max_length == 100
def test_autoload():
""" Test autoload property. """
inp = StringIO()
ldif = LDIFReader(inp)
assert ldif.autoload == True
with pytest.raises(TypeError):
ldif.autoload = "Yes"
ldif.autoload = False
assert ldif.autoload == False
def test_input_file():
""" Test input_file property. """
inp = StringIO()
ldif = LDIFReader(inp)
assert ldif.input_file == inp
with pytest.raises(TypeError):
ldif.input_file = None
inp2 = StringIO()
ldif.input_file = inp2
assert ldif.input_file == inp2
def test_multiline_attribute():
""" Test parsing multiline attributes in LDIF. """
text = "dn: cn=unimaginably+sn=very,ou=very,dc=very,dc=long,\n dc=line\ncn: unimaginably\nsn: very\nsn: long\n"
with StringIO(text) as test:
reader = LDIFReader(test)
ent = next(reader)
assert ent.dn == "cn=unimaginably+sn=very,ou=very,dc=very,dc=long,dc=line"
assert ent["cn"][0] == "unimaginably"
assert ent["sn"][0] == "very"
assert ent["sn"][1] == "long"