Debian Codegraph @

tests/test_ldapdn.py

raw symbols SCIP
1 import pytest
2
3 from bonsai import LDAPDN
4 from bonsai import errors
5
6 VALID_STRDN = "cn=user,dc=test,dc=local"
7
8
9 @pytest.fixture
10 def dnobj():
11 """ Get a LDAPDN object with valid DN. """
12 return LDAPDN(VALID_STRDN)
13
14
15 def test_rdn(dnobj):
16 """ Test methods for retrieving and changing RDNs. """
17 assert dnobj.rdns[0] == (("cn", "user"),)
18 assert dnobj[0] == "cn=user"
19 assert dnobj[1:] == "dc=test,dc=local"
20 dnobj[1:] = "dc=test2"
21 assert dnobj == "cn=user,dc=test2"
22 with pytest.raises(IndexError):
23 _ = dnobj[7]
24 with pytest.raises(TypeError):
25 _ = dnobj["test"]
26 with pytest.raises(ValueError):
27 dnobj.rdns = (("dc", "test"),)
28
29
30 def test_str(dnobj):
31 """ Test __str__ method of LDAPDN object. """
32 assert str(dnobj) == VALID_STRDN
33
34
35 def test_emptydn():
36 """ Test empty distinguished name. """
37 empty = LDAPDN("")
38 assert empty[1:] == ""
39
40
41 def test_equal(dnobj):
42 """ Test __eq__ method of LDAPDN object. """
43 assert dnobj == LDAPDN(VALID_STRDN)
44 assert dnobj == LDAPDN(VALID_STRDN.title())
45 assert dnobj == VALID_STRDN.upper()
46
47
48 def test_invaliddn():
49 """ Test InvalidDN exception. """
50 with pytest.raises(errors.InvalidDN):
51 _ = LDAPDN("cn=test,dc=one+two")
52
53
54 def test_special_char():
55 """ Test parsing special characters in DN string. """
56 spec = LDAPDN(r"cn=special\, name,dc=test,dc=local")
57 assert str(spec) == r"cn=special\, name,dc=test,dc=local"
58
59
60 def test_setitem():
61 """ Test setting RDNs for DN object. """
62 dnobj = LDAPDN("sn=some+gn=thing,dc=test,dc=local")
63 assert "sn=some+gn=thing" == dnobj[0]
64 dnobj[0] = "cn=user"
65 assert "cn=user,dc=test,dc=local" == dnobj
66 dnobj[1] = "ou=group1,ou=group2"
67 assert "cn=user,ou=group1,ou=group2,dc=local" == dnobj
68 dnobj[2:] = "dc=local"
69 assert "cn=user,ou=group1,dc=local" == dnobj
70
71 with pytest.raises(TypeError):
72 dnobj["invalid"] = "ou=group1,ou=group2"
73 with pytest.raises(ValueError):
74 dnobj[0] = 3
75 with pytest.raises(errors.InvalidDN):
76 dnobj[1] = "test,group"
77
78
79 def test_repr(dnobj):
80 """ Test representation. """
81 assert "<LDAPDN" in repr(dnobj)
82
83
84 def test_space_after_comma():
85 """ Test allowing space after comma for attribute type. """
86 with pytest.raises(errors.InvalidDN):
87 _ = LDAPDN("c n=user,dc=test,dc=local")
88 with pytest.raises(errors.InvalidDN):
89 _ = LDAPDN("cn=user,dc =test,dc=local")
90 dn = LDAPDN("cn=user, dc=test, dc=local")
91 assert str(dn) == "cn=user, dc=test, dc=local"
92 assert dn.rdns[1][0][0] == "dc"