Debian Codegraph @

test/testAbstractionSearch.py

raw
1 """
2 """
3 # Created on 2014.01.19
4 #
5 # Author: Giovanni Cannata
6 #
7 # Copyright 2014 - 2020 Giovanni Cannata
8 #
9 # This file is part of ldap3.
10 #
11 # ldap3 is free software: you can redistribute it and/or modify
12 # it under the terms of the GNU Lesser General Public License as published
13 # by the Free Software Foundation, either version 3 of the License, or
14 # (at your option) any later version.
15 #
16 # ldap3 is distributed in the hope that it will be useful,
17 # but WITHOUT ANY WARRANTY; without even the implied warranty of
18 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 # GNU Lesser General Public License for more details.
20 #
21 # You should have received a copy of the GNU Lesser General Public License
22 # along with ldap3 in the COPYING and COPYING.LESSER files.
23 # If not, see <http://www.gnu.org/licenses/>.
24
25 import unittest
26
27 from ldap3 import ObjectDef, AttrDef, Reader
28 from test.config import test_base, add_user, add_group, get_connection, drop_connection, random_id, test_server_type, test_multivalued_attribute, test_singlevalued_attribute
29
30 testcase_id = ''
31
32
33 class Test(unittest.TestCase):
34 def setUp(self):
35 global testcase_id
36 testcase_id = random_id()
37 self.connection = get_connection()
38 self.delete_at_teardown = []
39
40 def tearDown(self):
41 drop_connection(self.connection, self.delete_at_teardown)
42 self.assertFalse(self.connection.bound)
43
44 def test_search_filter_with_object_class(self):
45 self.delete_at_teardown.append(add_user(self.connection, testcase_id, 'abs-1'))
46 self.delete_at_teardown.append(add_user(self.connection, testcase_id, 'abs-2'))
47 self.delete_at_teardown.append(add_user(self.connection, testcase_id, 'abs-3'))
48 self.delete_at_teardown.append(add_group(self.connection, testcase_id, 'abs-grp', self.delete_at_teardown))
49 reverse = lambda a, e: e[::-1]
50 o = ObjectDef('inetOrgPerson')
51 o += AttrDef('cn', 'Common Name')
52 o += AttrDef('sn', 'Surname')
53 o += AttrDef(test_multivalued_attribute, 'Given Name', post_query=reverse)
54
55 query_text = 'Common Name:=' + testcase_id + 'abs-*'
56 r = Reader(self.connection, o, test_base, query_text)
57
58 results = r.search()
59 self.assertEqual(len(results), 3)
60
61 def test_search_with_dereference(self):
62 reverse = lambda a, e: e[::-1]
63
64 def raise_parentheses_rank(_, l):
65 up = {'(': '[', ')': ']', '[': '{', ']': '}', '{': '<', '}': '>'}
66 r = []
67 for e in l:
68 s = ''
69 for c in e:
70 s += up[c] if c in up else c
71 r.append(s)
72
73 return r
74
75 self.delete_at_teardown.append(add_user(self.connection, testcase_id, 'abs-4'))
76 self.delete_at_teardown.append(add_user(self.connection, testcase_id, 'abs-5'))
77 self.delete_at_teardown.append(add_user(self.connection, testcase_id, 'abs-6'))
78 self.delete_at_teardown.append(add_group(self.connection, testcase_id, 'abstract-group', self.delete_at_teardown))
79 ou = ObjectDef('inetOrgPerson')
80 ou += AttrDef('cn', 'Common Name', post_query=reverse)
81 ou += AttrDef('sn', 'Surname')
82 ou += AttrDef(test_multivalued_attribute, 'Given Name', post_query=raise_parentheses_rank)
83 ou += AttrDef('ACL')
84 qu = 'Common Name: ' + testcase_id + 'abs-*'
85 ru = Reader(self.connection, ou, test_base, qu)
86 lu = ru.search()
87 self.assertEqual(len(lu), 3)
88
89 og = ObjectDef('groupOfNames')
90 og += AttrDef('member', dereference_dn=ou)
91 og += 'cn'
92 qg = 'cn := ' + testcase_id + 'abstract-group'
93 rg = Reader(self.connection, og, test_base, qg)
94 lg = rg.search()
95 self.assertEqual(len(lg), 1)
96
97 eg = lg[0]
98 mg = eg.member
99 self.assertEqual(len(mg), 3)
100 ug = eg.member[0]
101 self.assertTrue(str(ug.surname) in ['abs-4', 'abs-5', 'abs-6'])
102
103 def test_search_with_pre_query(self):
104 change = lambda attr, value: testcase_id + 'abs-*'
105
106 self.delete_at_teardown.append(add_user(self.connection, testcase_id, 'abs-7'))
107 self.delete_at_teardown.append(add_user(self.connection, testcase_id, 'abs-8'))
108 self.delete_at_teardown.append(add_user(self.connection, testcase_id, 'abs-9'))
109 self.delete_at_teardown.append(add_group(self.connection, testcase_id, 'abstract-group', self.delete_at_teardown))
110
111 ou = ObjectDef('inetOrgPerson')
112 ou += AttrDef('cn', 'Common Name', pre_query=change)
113 ou += AttrDef('sn', 'Surname')
114 ou += AttrDef(test_multivalued_attribute, 'Given Name')
115 ou += AttrDef('ACL')
116 qu = 'Common Name := bug'
117 ru = Reader(self.connection, ou, test_base, qu)
118 lu = ru.search()
119 self.assertEqual(len(lu), 3)
120
121 def test_search_with_default(self):
122 self.delete_at_teardown.append(add_user(self.connection, testcase_id, 'abs-10'))
123
124 ou = ObjectDef('inetOrgPerson')
125 ou += AttrDef('cn', 'CommonName')
126 ou += AttrDef('employeeType', key='Employee', default='not employed')
127 qu = 'CommonName := ' + testcase_id + 'abs-10'
128 ru = Reader(self.connection, ou, test_base, qu)
129 lu = ru.search()
130 self.assertEqual(str(lu[0].employee), 'not employed')
131
132 def test_search_with_falsy_default(self):
133 self.delete_at_teardown.append(add_user(self.connection, testcase_id, 'abs-11'))
134
135 ou = ObjectDef('inetOrgPerson')
136 ou += AttrDef('cn', 'CommonName')
137 ou += AttrDef('employeeType', key='Employee', default='')
138 qu = 'CommonName := ' + testcase_id + 'abs-11'
139 ru = Reader(self.connection, ou, test_base, qu)
140 lu = ru.search()
141 self.assertEqual(lu[0].employee.value, '')
142
143 def test_search_with_None_default(self):
144 self.delete_at_teardown.append(add_user(self.connection, testcase_id, 'abs-12'))
145
146 ou = ObjectDef('inetOrgPerson')
147 ou += AttrDef('cn', 'CommonName')
148 ou += AttrDef('employeeType', key='Employee', default=None)
149 qu = 'CommonName := ' + testcase_id + 'abs-12'
150 ru = Reader(self.connection, ou, test_base, qu)
151 lu = ru.search()
152 self.assertEqual(lu[0].employee.value, None)
153
154 def test_find_entry_with_text_index_match(self):
155 self.delete_at_teardown.append(add_user(self.connection, testcase_id, 'mat-1'))
156 self.delete_at_teardown.append(add_user(self.connection, testcase_id, 'mat-2'))
157 self.delete_at_teardown.append(add_user(self.connection, testcase_id, 'mat-3'))
158 o = ObjectDef('inetOrgPerson')
159 o += AttrDef('cn', 'Common Name')
160 o += AttrDef('sn', 'Surname')
161 o += AttrDef(test_multivalued_attribute, 'Given Name')
162
163 query_text = 'Common Name:=' + testcase_id + 'mat-*'
164 r = Reader(self.connection, o, test_base, query_text)
165
166 results = r.search()
167 self.assertEqual(len(results), 3)
168 try: # multiple matches
169 e = r['match']
170 except KeyError:
171 pass
172
173 e = r['-3'] # exact match. use -3 because there's domains with -20xx for the year in their name
174 self.assertTrue('mat-3' in e.entry_dn)
175
176 try:
177 e = r['no-match'] # no match
178 except KeyError:
179 pass
180
181 def test_match_dn_in_cursor(self):
182 self.delete_at_teardown.append(add_user(self.connection, testcase_id, 'mat-1'))
183 self.delete_at_teardown.append(add_user(self.connection, testcase_id, 'mat-2'))
184 self.delete_at_teardown.append(add_user(self.connection, testcase_id, 'mat-3'))
185 o = ObjectDef('inetOrgPerson')
186 o += AttrDef('cn', 'Common Name')
187 o += AttrDef('sn', 'Surname')
188 o += AttrDef(test_multivalued_attribute, 'Given Name')
189
190 query_text = 'Common Name:=' + testcase_id + 'mat-*'
191 r = Reader(self.connection, o, test_base, query_text)
192
193 results = r.search()
194 self.assertEqual(len(results), 3)
195
196 e = r.match_dn('mat') # multiple matches
197 self.assertEqual(len(e), 3)
198 e = r.match_dn('-3') # single match. there's domains with -20xx in their names, so use -3 to match
199 self.assertEqual(len(e), 1)
200 e = r.match_dn('no-match') # no match
201 self.assertEqual(len(e), 0)
202
203 def test_match_in_single_attribute(self):
204 self.delete_at_teardown.append(add_user(self.connection, testcase_id, 'mat-1', attributes={test_multivalued_attribute: ['givenname-1', 'givenname-1a']}))
205 self.delete_at_teardown.append(add_user(self.connection, testcase_id, 'mat-2', attributes={test_multivalued_attribute: ['givenname-2', 'givenname-2a']}))
206 self.delete_at_teardown.append(add_user(self.connection, testcase_id, 'mat-3', attributes={test_multivalued_attribute: ['givenname-3', 'givenname-3a']}))
207 o = ObjectDef('inetOrgPerson')
208 o += AttrDef('cn', 'Common Name')
209 o += AttrDef('sn', 'Surname')
210 o += AttrDef(test_multivalued_attribute, 'Given Name')
211
212 query_text = 'Common Name:=' + testcase_id + 'mat-*'
213 r = Reader(self.connection, o, test_base, query_text)
214
215 results = r.search()
216 self.assertEqual(len(results), 3)
217
218 e = r.match('Given Name', 'name') # multiple matches
219 self.assertEqual(len(e), 3)
220 e = r.match('Given Name', '2a') # single match
221 self.assertEqual(len(e), 1)
222 e = r.match('Given Name', 'no-match') # no match
223 self.assertEqual(len(e), 0)
224
225 def test_match_in_multiple_attribute(self):
226 self.delete_at_teardown.append(add_user(self.connection, testcase_id, 'mat-1', attributes={test_multivalued_attribute: ['givenname-1', 'givenname-1a'], 'street': '1a'}))
227 self.delete_at_teardown.append(add_user(self.connection, testcase_id, 'mat-2', attributes={test_multivalued_attribute: ['givenname-2', 'givenname-2a'], 'street': '3a'}))
228 self.delete_at_teardown.append(add_user(self.connection, testcase_id, 'mat-3', attributes={test_multivalued_attribute: ['givenname-3', 'givenname-3a'], 'street': '4a'}))
229 o = ObjectDef('inetOrgPerson')
230 o += AttrDef('cn', 'Common Name')
231 o += AttrDef('sn', 'Surname')
232 o += AttrDef(test_multivalued_attribute, 'Given Name')
233 o += AttrDef('street', 'Street')
234
235 query_text = 'Common Name:=' + testcase_id + 'mat-*'
236 r = Reader(self.connection, o, test_base, query_text)
237
238 results = r.search()
239 self.assertEqual(len(results), 3)
240
241 e = r.match(['Given Name', 'Street'], '3a') # multiple matches
242 self.assertEqual(len(e), 2)
243 e = r.match(['Given Name', 'street'], '1a') # single match
244 self.assertEqual(len(e), 1)
245 e = r.match(['Given Name', 'street'], 'no-match') # no match
246 self.assertEqual(len(e), 0)
247
248 def test_match_in_single_attribute_with_schema(self):
249 self.delete_at_teardown.append(add_user(self.connection, testcase_id, 'mat-1', attributes={test_singlevalued_attribute: 'FALSE'}))
250 self.delete_at_teardown.append(add_user(self.connection, testcase_id, 'mat-2', attributes={test_singlevalued_attribute: 'FALSE'}))
251 self.delete_at_teardown.append(add_user(self.connection, testcase_id, 'mat-3', attributes={test_singlevalued_attribute: 'TRUE'}))
252 r = Reader(self.connection, 'inetorgperson', test_base, 'cn:=' + testcase_id + 'mat-*')
253
254 results = r.search()
255 self.assertEqual(len(results), 3)
256
257 e = r.match(test_singlevalued_attribute, 'FALSE')
258 self.assertEqual(len(e), 2)
259 e = r.match(test_singlevalued_attribute, 'fAlSe')
260 self.assertEqual(len(e), 2)
261
262 def test_paged_search_accumulator_with_schema(self):
263 self.delete_at_teardown.append(add_user(self.connection, testcase_id, 'mat-1'))
264 self.delete_at_teardown.append(add_user(self.connection, testcase_id, 'mat-2'))
265 self.delete_at_teardown.append(add_user(self.connection, testcase_id, 'mat-3'))
266 self.delete_at_teardown.append(add_user(self.connection, testcase_id, 'mat-4'))
267 self.delete_at_teardown.append(add_user(self.connection, testcase_id, 'mat-5'))
268 r = Reader(self.connection, 'inetorgperson', test_base, 'cn:=' + testcase_id + 'mat-*')
269 entries = r.search_paged(2, True, generator=False, attributes=['cn'])
270 self.assertEqual(len(entries), 5)
271
272 def test_paged_search_generator_with_schema(self):
273 self.delete_at_teardown.append(add_user(self.connection, testcase_id, 'mat-1'))
274 self.delete_at_teardown.append(add_user(self.connection, testcase_id, 'mat-2'))
275 self.delete_at_teardown.append(add_user(self.connection, testcase_id, 'mat-3'))
276 self.delete_at_teardown.append(add_user(self.connection, testcase_id, 'mat-4'))
277 self.delete_at_teardown.append(add_user(self.connection, testcase_id, 'mat-5'))
278 r = Reader(self.connection, 'inetorgperson', test_base, 'cn:=' + testcase_id + 'mat-*')
279 entries = r.search_paged(2, True, generator=True, attributes=['cn'])
280 cont = 0
281 for _ in entries:
282 cont += 1
283 self.assertEqual(cont, 5)
284
285 def test_paged_search_accumulator_with_schema_single_entry(self):
286 self.delete_at_teardown.append(add_user(self.connection, testcase_id, 'mat-1'))
287 r = Reader(self.connection, 'inetorgperson', test_base, 'cn:=' + testcase_id + 'mat-*')
288 entries = r.search_paged(2, True, generator=False, attributes=['cn'])
289 self.assertEqual(len(entries), 1)
290
291 def test_paged_search_generator_with_schema_single_entry(self):
292 self.delete_at_teardown.append(add_user(self.connection, testcase_id, 'mat-1'))
293 r = Reader(self.connection, 'inetorgperson', test_base, 'cn:=' + testcase_id + 'mat-*')
294 entries = r.search_paged(2, True, generator=True, attributes=['cn'])
295 cont = 0
296 for _ in entries:
297 cont += 1
298 self.assertEqual(cont, 1)
299
300 def test_paged_search_accumulator_with_schema_base_object(self):
301 self.delete_at_teardown.append(add_user(self.connection, testcase_id, 'mat-1'))
302 r = Reader(self.connection, 'inetorgperson', self.delete_at_teardown[0][0])
303 entries = r.search_paged(2, True, generator=False, attributes=['cn'])
304 self.assertEqual(len(entries), 1)
305
306 def test_paged_search_generator_with_schema_base_object(self):
307 self.delete_at_teardown.append(add_user(self.connection, testcase_id, 'mat-1'))
308 r = Reader(self.connection, 'inetorgperson', self.delete_at_teardown[0][0])
309 entries = r.search_paged(2, True, generator=True, attributes=['cn'])
310 cont = 0
311 for _ in entries:
312 cont += 1
313 self.assertEqual(cont, 1)