File: t_ldap_modlist.py

package info (click to toggle)
python-ldap 3.4.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,756 kB
  • sloc: python: 9,558; ansic: 3,052; makefile: 139; sh: 79
file content (158 lines) | stat: -rw-r--r-- 4,721 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
"""
Automatic tests for python-ldap's module ldap.modlist

See https://www.python-ldap.org/ for details.
"""

import os
import unittest

# Switch off processing .ldaprc or ldap.conf before importing _ldap
os.environ['LDAPNOINIT'] = '1'

import ldap
from ldap.modlist import addModlist,modifyModlist


class TestModlist(unittest.TestCase):

    addModlist_tests = [
        (
            {
                'objectClass': [b'person',b'pilotPerson'],
                'cn':[b'Michael Str\303\266der',b'Michael Stroeder'],
                'sn':[b'Str\303\266der'],
                'dummy1':[],
                'dummy2':[b'2'],
                'dummy3':[b''],
            },
            [
                ('objectClass',[b'person',b'pilotPerson']),
                ('cn',[b'Michael Str\303\266der',b'Michael Stroeder']),
                ('sn',[b'Str\303\266der']),
                ('dummy2',[b'2']),
                ('dummy3',[b'']),
            ]
        ),
    ]

    def test_addModlist(self):
        for entry,test_modlist in self.addModlist_tests:
            test_modlist.sort()
            result_modlist = addModlist(entry)
            result_modlist.sort()
            self.assertEqual(
                test_modlist, result_modlist,
                'addModlist({}) returns\n{}\ninstead of\n{}.'.format(
                    repr(entry),repr(result_modlist),repr(test_modlist)
                )
            )

    modifyModlist_tests = [
        (
            {
                'objectClass':[b'person',b'pilotPerson'],
                'cn':[b'Michael Str\303\266der',b'Michael Stroeder'],
                'sn':[b'Str\303\266der'],
                'enum':[b'a',b'b',b'c'],
                'c':[b'DE'],
            },
            {
                'objectClass':[b'person',b'inetOrgPerson'],
                'cn':[b'Michael Str\303\266der',b'Michael Stroeder'],
                'sn':[],
                'enum':[b'a',b'b',b'd'],
                'mail':[b'michael@stroeder.com'],
            },
            [],
            [
                (ldap.MOD_DELETE,'objectClass',None),
                (ldap.MOD_ADD,'objectClass',[b'person',b'inetOrgPerson']),
                (ldap.MOD_DELETE,'c',None),
                (ldap.MOD_DELETE,'sn',None),
                (ldap.MOD_ADD,'mail',[b'michael@stroeder.com']),
                (ldap.MOD_DELETE,'enum',None),
                (ldap.MOD_ADD,'enum',[b'a',b'b',b'd']),
            ]
        ),

        (
            {
                'c':[b'DE'],
            },
            {
                'c':[b'FR'],
            },
            [],
            [
                (ldap.MOD_DELETE,'c',None),
                (ldap.MOD_ADD,'c',[b'FR']),
            ]
        ),

        # Now a weird test-case for catching all possibilities
        # of removing an attribute with MOD_DELETE,attr_type,None
        (
            {
                'objectClass':[b'person'],
                'cn':[None],
                'sn':[b''],
                'c':[b'DE'],
            },
            {
                'objectClass':[],
                'cn':[],
                'sn':[None],
            },
            [],
            [
                (ldap.MOD_DELETE,'c',None),
                (ldap.MOD_DELETE,'objectClass',None),
                (ldap.MOD_DELETE,'sn',None),
            ]
        ),

        (
            {
                'objectClass':[b'person'],
                'cn':[b'Michael Str\303\266der',b'Michael Stroeder'],
                'sn':[b'Str\303\266der'],
                'enum':[b'a',b'b',b'C'],
            },
            {
                'objectClass':[b'Person'],
                'cn':[b'Michael Str\303\266der',b'Michael Stroeder'],
                'sn':[],
                'enum':[b'a',b'b',b'c'],
            },
            ['objectClass'],
            [
                (ldap.MOD_DELETE,'sn',None),
                (ldap.MOD_DELETE,'enum',None),
                (ldap.MOD_ADD,'enum',[b'a',b'b',b'c']),
            ]
        ),

    ]

    def test_modifyModlist(self):
        for old_entry, new_entry, case_ignore_attr_types, test_modlist in self.modifyModlist_tests:
            test_modlist.sort()
            result_modlist = modifyModlist(
                old_entry, new_entry,
                case_ignore_attr_types=case_ignore_attr_types)
            result_modlist.sort()

            self.assertEqual(
                test_modlist, result_modlist,
                'modifyModlist({},{}) returns\n{}\ninstead of\n{}.'.format(
                    repr(old_entry),
                    repr(new_entry),
                    repr(result_modlist),
                    repr(test_modlist),
                )
            )


if __name__ == '__main__':
    unittest.main()