Debian Codegraph @

test/testAbstractionDefs.py

raw
1 """
2 """
3
4 # Created on 2014.01.12
5 #
6 # Author: Giovanni Cannata
7 #
8 # Copyright 2014 - 2020 Giovanni Cannata
9 #
10 # This file is part of ldap3.
11 #
12 # ldap3 is free software: you can redistribute it and/or modify
13 # it under the terms of the GNU Lesser General Public License as published
14 # by the Free Software Foundation, either version 3 of the License, or
15 # (at your option) any later version.
16 #
17 # ldap3 is distributed in the hope that it will be useful,
18 # but WITHOUT ANY WARRANTY; without even the implied warranty of
19 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 # GNU Lesser General Public License for more details.
21 #
22 # You should have received a copy of the GNU Lesser General Public License
23 # along with ldap3 in the COPYING and COPYING.LESSER files.
24 # If not, see <http://www.gnu.org/licenses/>.
25
26 import unittest
27
28 from ldap3 import ObjectDef, AttrDef, Reader
29 from ldap3.abstract.cursor import _create_query_dict
30 from test.config import test_base, get_connection, drop_connection
31
32
33 class Test(unittest.TestCase):
34 def setUp(self):
35 self.connection = get_connection(check_names=True)
36
37 def tearDown(self):
38 drop_connection(self.connection)
39
40 def test_create_query_dict(self):
41 query_text = 'Common Name:=|john;Bob, Surname:=smith'
42 query_dict = _create_query_dict(query_text)
43 self.assertEqual(query_dict['Common Name'], '=|john;Bob')
44 self.assertEqual(query_dict['Surname'], '=smith')
45 self.assertEqual(len(query_dict), 2)
46
47 def test_validate_query_filter(self):
48 o = ObjectDef()
49 o += AttrDef('cn', 'Common Name')
50 o += AttrDef('sn', 'Surname')
51 o += AttrDef('givenName', 'Given Name')
52
53 query_text = '|Common Name:=john;=Bob, Surname:=smith'
54 r = Reader(self.connection, o, test_base, query_text)
55
56 r._validate_query()
57
58 self.assertEqual('Surname: =smith, |CommonName: =Bob;=john', r.validated_query)
59
60 def test_create_query_filter(self):
61 o = ObjectDef()
62 o += AttrDef('cn', 'Common Name')
63 o += AttrDef('sn', 'Surname')
64 o += AttrDef('givenName', 'Given Name')
65
66 query_text = '|Common Name:=john;Bob, Surname:=smith'
67 r = Reader(self.connection, o, test_base, query_text)
68
69 r._create_query_filter()
70
71 self.assertEqual('(&(sn=smith)(|(cn=Bob)(cn=john)))', r.query_filter)
72
73 def test_create_query_filter_single_attribute_single_value(self):
74 o = ObjectDef()
75 o += AttrDef('cn', 'Common Name')
76
77 query_text = 'Common Name:John'
78 r = Reader(self.connection, o, test_base, query_text)
79
80 r._create_query_filter()
81
82 self.assertEqual('(cn=John)', r.query_filter)
83
84 def test_create_query_filter_single_attribute_multiple_value(self):
85 o = ObjectDef()
86 o += AttrDef('cn', 'Common Name')
87
88 query_text = '|Common Name:=john;=Bob'
89 r = Reader(self.connection, o, test_base, query_text)
90
91 r._create_query_filter()
92
93 self.assertEqual('(|(cn=Bob)(cn=john))', r.query_filter)
94
95 def test_create_query_filter_with_object_class(self):
96 o = ObjectDef('inetOrgPerson')
97 o += AttrDef('cn', 'Common Name')
98 o += AttrDef('sn', 'Surname')
99 o += AttrDef('givenName', 'Given Name')
100
101 query_text = '|Common Name:=john;=Bob, Surname:=smith'
102 r = Reader(self.connection, o, test_base, query_text)
103
104 r._create_query_filter()
105
106 self.assertEqual('(&(objectClass=inetOrgPerson)(sn=smith)(|(cn=Bob)(cn=john)))', r.query_filter)