def test_search_with_None_default(self): self.delete_at_teardown.append(add_user(self.connection, testcase_id, 'abstract-member-12')) ou = ObjectDef('inetOrgPerson') ou += AttrDef('cn', 'CommonName') ou += AttrDef('employeeType', key='Employee', default=None) qu = 'CommonName := ' + testcase_id + 'abstract-member-12' ru = Reader(self.connection, ou, qu, test_base) lu = ru.search() self.assertEqual(lu[0].employee.value, None)
def test_create_query_filter_with_object_class(self): o = ObjectDef('inetOrgPerson') o += AttrDef('cn', 'Common Name') o += AttrDef('sn', 'Surname') o += AttrDef('givenName', 'Given Name') query_text = '|Common Name:=john;=Bob, Surname:=smith' r = Reader(None, o, query_text, base=test_base) r._create_query_filter() self.assertEqual('(&(objectClass=inetOrgPerson)(sn=smith)(|(cn=Bob)(cn=john)))', r.query_filter)
def test_validate_query_filter(self): o = ObjectDef() o += AttrDef('cn', 'Common Name') o += AttrDef('sn', 'Surname') o += AttrDef('givenName', 'Given Name') query_text = '|Common Name:=john;=Bob, Surname:=smith' r = Reader(None, o, query_text, base=test_base) r._validate_query() self.assertEqual('Surname: =smith, |CommonName: =Bob;=john', r.validated_query)
def test_search_with_dereference(self): reverse = lambda a, e: e[::-1] def raise_parentheses_rank(_, l): up = {'(': '[', ')': ']', '[': '{', ']': '}', '{': '<', '}': '>'} r = [] for e in l: s = '' for c in e: s += up[c] if c in up else c r.append(s) return r self.delete_at_teardown.append( add_user(self.connection, testcase_id, 'abstract-member-4')) self.delete_at_teardown.append( add_user(self.connection, testcase_id, 'abstract-member-5')) self.delete_at_teardown.append( add_user(self.connection, testcase_id, 'abstract-member-6')) self.delete_at_teardown.append( add_group(self.connection, testcase_id, 'abstract-group', self.delete_at_teardown)) ou = ObjectDef('inetOrgPerson') ou += AttrDef('cn', 'Common Name', post_query=reverse) ou += AttrDef('sn', 'Surname') ou += AttrDef('givenName', 'Given Name', post_query=raise_parentheses_rank) ou += AttrDef('ACL') qu = 'Common Name: ' + testcase_id + 'abstract-member-*' ru = Reader(self.connection, ou, qu, test_base) lu = ru.search() self.assertEqual(len(lu), 3) og = ObjectDef('groupOfNames') og += AttrDef('member', dereference_dn=ou) og += 'cn' qg = 'cn := ' + testcase_id + 'abstract-group' rg = Reader(self.connection, og, qg, test_base) lg = rg.search() self.assertEqual(len(lg), 1) eg = lg[0] mg = eg.member self.assertEqual(len(mg), 3) ug = eg.member[0] self.assertTrue( str(ug.surname) in ['abstract-member-4', 'abstract-member-5', 'abstract-member-6'])
def test_search_filter_with_object_class(self): self.delete_at_teardown.append(add_user(self.connection, testcase_id, 'abs-1')) self.delete_at_teardown.append(add_user(self.connection, testcase_id, 'abs-2')) self.delete_at_teardown.append(add_user(self.connection, testcase_id, 'abs-3')) self.delete_at_teardown.append(add_group(self.connection, testcase_id, 'abs-grp', self.delete_at_teardown)) reverse = lambda a, e: e[::-1] o = ObjectDef('inetOrgPerson') o += AttrDef('cn', 'Common Name') o += AttrDef('sn', 'Surname') o += AttrDef('givenName', 'Given Name', post_query=reverse) query_text = 'Common Name:=' + testcase_id + 'abs-*' r = Reader(self.connection, o, query_text, test_base) results = r.search() self.assertEqual(len(results), 3)
def test_search_with_pre_query(self): change = lambda attr, value: testcase_id + 'abstract-member-*' self.delete_at_teardown.append(add_user(self.connection, testcase_id, 'abstract-member-7')) self.delete_at_teardown.append(add_user(self.connection, testcase_id, 'abstract-member-8')) self.delete_at_teardown.append(add_user(self.connection, testcase_id, 'abstract-member-9')) self.delete_at_teardown.append(add_group(self.connection, testcase_id, 'abstract-group', self.delete_at_teardown)) ou = ObjectDef('inetOrgPerson') ou += AttrDef('cn', 'Common Name', pre_query=change) ou += AttrDef('sn', 'Surname') ou += AttrDef('givenName', 'Given Name') ou += AttrDef('ACL') qu = 'Common Name := bug' ru = Reader(self.connection, ou, qu, test_base) lu = ru.search() self.assertEqual(len(lu), 3)
def test_create_query_filter_single_attribute_multiple_value(self): o = ObjectDef() o += AttrDef('cn', 'Common Name') query_text = '|Common Name:=john;=Bob' r = Reader(None, o, query_text, base=test_base) r._create_query_filter() self.assertEqual('(|(cn=Bob)(cn=john))', r.query_filter)
def test_create_query_filter_single_attribute_single_value(self): o = ObjectDef() o += AttrDef('cn', 'Common Name') query_text = 'Common Name:John' r = Reader(None, o, query_text, base=test_base) r._create_query_filter() self.assertEqual('(cn=John)', r.query_filter)
from ldap3 import Server, Connection from ldap3.abstract import ObjectDef, AttrDef, Reader person = ObjectDef('inetOrgPerson') engineer = ObjectDef(['inetOrgPerson', 'auxEngineer']) cnAttribute = AttrDef('cn') person.add(cnAttribute) # person += AttrDef('cn') # same as above # person += 'cn' # same as above person += [AttrDef('cn', key='Common Name'), AttrDef('sn', key='Surname')] # person += ['cn', 'sn'] # as above, but keys are the attribute names cnAttrDef = person['Common Name'] cnAttrDef = person['commonName'] # same as above cnAttrDef = person.CommonName # same as above deps = {'A': 'Accounting', 'F': 'Finance', 'E': 'Engineering'} # checks that the parameter in query is in a specific range validDepartment = lambda attr, value: True if value in deps.values() else False # person += AttrDef('employeeStatus', key = 'Department', validate = validDepartment) # transform value to be search def get_department_code(attr, value): for dep in deps.items(): if dep[1] == value: return dep[0] return 'not a department' # person += AttrDef('employeeStatus', key = 'Department', preQuery = getDepartmentCode)