File: t_ldap_functions.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 (78 lines) | stat: -rw-r--r-- 2,016 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
"""
Automatic tests for python-ldap's module ldap.functions

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.dn import escape_dn_chars
from ldap.filter import escape_filter_chars


class TestFunction(unittest.TestCase):
    """
    test ldap.functions
    """

    def test_ldap_strf_secs(self):
        """
        test function ldap_strf_secs()
        """
        self.assertEqual(ldap.strf_secs(0), '19700101000000Z')
        self.assertEqual(ldap.strf_secs(1466947067), '20160626131747Z')

    def test_ldap_strp_secs(self):
        """
        test function ldap_strp_secs()
        """
        self.assertEqual(ldap.strp_secs('19700101000000Z'), 0)
        self.assertEqual(ldap.strp_secs('20160626131747Z'), 1466947067)

    def test_escape_str(self):
        """
        test function escape_string_tmpl()
        """
        self.assertEqual(
            ldap.escape_str(
                escape_filter_chars,
                '(&(objectClass=aeUser)(uid=%s))',
                'foo'
            ),
            '(&(objectClass=aeUser)(uid=foo))'
        )
        self.assertEqual(
            ldap.escape_str(
                escape_filter_chars,
                '(&(objectClass=aeUser)(uid=%s))',
                'foo)bar'
            ),
            '(&(objectClass=aeUser)(uid=foo\\29bar))'
        )
        self.assertEqual(
            ldap.escape_str(
                escape_dn_chars,
                'uid=%s',
                'foo=bar'
            ),
            'uid=foo\\=bar'
        )
        self.assertEqual(
            ldap.escape_str(
                escape_dn_chars,
                'uid=%s,cn=%s,cn=%s,dc=example,dc=com',
                'foo=bar',
                'foo+',
                '+bar',
            ),
            'uid=foo\\=bar,cn=foo\\+,cn=\\+bar,dc=example,dc=com'
        )


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