File: test_ldapdn.py

package info (click to toggle)
python-bonsai 1.5.3%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,136 kB
  • sloc: python: 6,816; ansic: 5,549; makefile: 170; sh: 90
file content (92 lines) | stat: -rw-r--r-- 2,584 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import pytest

from bonsai import LDAPDN
from bonsai import errors

VALID_STRDN = "cn=user,dc=test,dc=local"


@pytest.fixture
def dnobj():
    """ Get a LDAPDN object with valid DN. """
    return LDAPDN(VALID_STRDN)


def test_rdn(dnobj):
    """ Test methods for retrieving and changing RDNs. """
    assert dnobj.rdns[0] == (("cn", "user"),)
    assert dnobj[0] == "cn=user"
    assert dnobj[1:] == "dc=test,dc=local"
    dnobj[1:] = "dc=test2"
    assert dnobj == "cn=user,dc=test2"
    with pytest.raises(IndexError):
        _ = dnobj[7]
    with pytest.raises(TypeError):
        _ = dnobj["test"]
    with pytest.raises(ValueError):
        dnobj.rdns = (("dc", "test"),)


def test_str(dnobj):
    """ Test __str__ method of LDAPDN object. """
    assert str(dnobj) == VALID_STRDN


def test_emptydn():
    """ Test empty distinguished name. """
    empty = LDAPDN("")
    assert 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 test_invaliddn():
    """ Test InvalidDN exception. """
    with pytest.raises(errors.InvalidDN):
        _ = LDAPDN("cn=test,dc=one+two")


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_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_repr(dnobj):
    """ Test representation. """
    assert "<LDAPDN" in repr(dnobj)


def test_space_after_comma():
    """ Test allowing space after comma for attribute type. """
    with pytest.raises(errors.InvalidDN):
        _ = LDAPDN("c n=user,dc=test,dc=local")
    with pytest.raises(errors.InvalidDN):
        _ = LDAPDN("cn=user,dc =test,dc=local")
    dn = LDAPDN("cn=user, dc=test, dc=local")
    assert str(dn) == "cn=user, dc=test, dc=local"
    assert dn.rdns[1][0][0] == "dc"