| 1 |
import pytest |
| 2 |
|
| 3 |
from base64 import b64decode |
| 4 |
from io import StringIO, BytesIO |
| 5 |
from bonsai import LDIFWriter |
| 6 |
from bonsai import LDAPEntry, LDAPModOp |
| 7 |
|
| 8 |
|
| 9 |
def test_init_params(): |
| 10 |
""" Test constructor parameters for LDIFWriter. """ |
| 11 |
with pytest.raises(TypeError): |
| 12 |
_ = LDIFWriter("wrong") |
| 13 |
with pytest.raises(TypeError): |
| 14 |
_ = LDIFWriter(StringIO(), max_length=None) |
| 15 |
with pytest.raises(TypeError): |
| 16 |
_ = LDIFWriter(BytesIO()) |
| 17 |
out = StringIO() |
| 18 |
ldif = LDIFWriter(out, 100) |
| 19 |
assert ldif.output_file == out |
| 20 |
assert ldif.max_length == 100 |
| 21 |
|
| 22 |
|
| 23 |
def test_write_entry(): |
| 24 |
""" Test serialising an LDAP entry. """ |
| 25 |
ent = LDAPEntry("cn=test") |
| 26 |
ent["cn"] = "test" |
| 27 |
ent["jpegPhoto"] = b"1223122130008283938282931232" |
| 28 |
ent["sn"] = "test😊" |
| 29 |
ent["sn"].append(" test2") |
| 30 |
|
| 31 |
with StringIO() as out: |
| 32 |
ldif = LDIFWriter(out, max_length=32) |
| 33 |
ldif.write_entry(ent) |
| 34 |
content = out.getvalue() |
| 35 |
contlines = content.split("\n") |
| 36 |
surnames = { |
| 37 |
b64decode(line.split(":: ")[1]).decode("UTF-8") |
| 38 |
for line in contlines |
| 39 |
if "sn" in line |
| 40 |
} |
| 41 |
jpeg_lines = [] |
| 42 |
for idx, line in enumerate(contlines): |
| 43 |
if "jpegPhoto::" in line: |
| 44 |
jpeg_lines.append(line.split(" ")[1]) |
| 45 |
jpeg_lines.append(contlines[idx + 1][1:]) |
| 46 |
jpegPhoto = b64decode("".join(jpeg_lines)) |
| 47 |
|
| 48 |
assert all(len(line) <= 32 for line in contlines) |
| 49 |
assert f"dn: {ent.dn}" == contlines[0] |
| 50 |
assert f"cn: {ent['cn'][0]}\n" in content |
| 51 |
assert content.count("sn:: ") == 2 |
| 52 |
assert surnames == set(ent["sn"]) |
| 53 |
assert content.count("jpegPhoto:: ") == 1 |
| 54 |
assert jpegPhoto == ent["jpegPhoto"][0] |
| 55 |
|
| 56 |
|
| 57 |
def test_write_entries(): |
| 58 |
""" Test writing multiple entries into the same output. """ |
| 59 |
ent0 = LDAPEntry("cn=test0") |
| 60 |
ent0["cn"] = "test0" |
| 61 |
ent1 = LDAPEntry("cn=test1") |
| 62 |
ent1["cn"] = "test1" |
| 63 |
entries = (ent0, ent1) |
| 64 |
|
| 65 |
with StringIO() as out: |
| 66 |
ldif = LDIFWriter(out) |
| 67 |
ldif.write_entries(entries, write_version=False) |
| 68 |
content = out.getvalue() |
| 69 |
|
| 70 |
assert f"dn: {ent0.dn}" in content |
| 71 |
assert f"dn: {ent1.dn}" in content |
| 72 |
assert f"cn: {ent0['cn'][0]}" in content |
| 73 |
assert f"cn: {ent1['cn'][0]}" in content |
| 74 |
assert "version" not in content |
| 75 |
|
| 76 |
with StringIO() as out: |
| 77 |
ldif = LDIFWriter(out) |
| 78 |
ldif.write_entries(entries) |
| 79 |
content = out.getvalue() |
| 80 |
|
| 81 |
assert f"dn: {ent0.dn}" in content |
| 82 |
assert f"dn: {ent1.dn}" in content |
| 83 |
assert "version: 1" == content.split("\n")[0] |
| 84 |
|
| 85 |
|
| 86 |
def test_write_changes(): |
| 87 |
""" Test writing LDIF changes of an LDAP Entry. """ |
| 88 |
ent = LDAPEntry("cn=test") |
| 89 |
ent["cn"] = "test" |
| 90 |
ent["sn"] = ["sntest1", "sntest2"] |
| 91 |
ent.change_attribute("givenName", LDAPModOp.REPLACE, "test") |
| 92 |
ent.change_attribute("uidNumber", LDAPModOp.DELETE, 0) |
| 93 |
ent.change_attribute("gidNumber", LDAPModOp.DELETE) |
| 94 |
|
| 95 |
with StringIO() as out: |
| 96 |
ldif = LDIFWriter(out) |
| 97 |
ldif.write_changes(ent) |
| 98 |
content = out.getvalue() |
| 99 |
lines = content.split("\n") |
| 100 |
|
| 101 |
assert f"dn: {ent.dn}" == lines.pop(0) |
| 102 |
assert "changetype: modify" == lines.pop(0) |
| 103 |
assert "add: cn" in lines |
| 104 |
assert f"cn: {ent['cn'][0]}" == lines[lines.index("add: cn") + 1] |
| 105 |
assert "add: sn" in lines |
| 106 |
assert set(ent["sn"]) == {lin.split("sn: ")[1] for lin in lines if "sn: " in lin} |
| 107 |
assert "replace: givenName" in lines |
| 108 |
assert ( |
| 109 |
f"givenName: {ent['givenName'][0]}" |
| 110 |
== lines[lines.index("replace: givenName") + 1] |
| 111 |
) |
| 112 |
assert "delete: uidNumber" in lines |
| 113 |
assert "uidNumber: 0" == lines[lines.index("delete: uidNumber") + 1] |
| 114 |
assert "delete: gidNumber" in lines |
| 115 |
|
| 116 |
assert "-" == lines[lines.index("delete: gidNumber") + 1] |
| 117 |
|
| 118 |
|
| 119 |
def test_output_file(): |
| 120 |
""" Test output_file property. """ |
| 121 |
out = StringIO() |
| 122 |
ldif = LDIFWriter(out) |
| 123 |
assert ldif.output_file == out |
| 124 |
with pytest.raises(TypeError): |
| 125 |
ldif.output_file = None |
| 126 |
out2 = StringIO() |
| 127 |
ldif.output_file = out2 |
| 128 |
assert ldif.output_file == out2 |