| 1 |
""" |
| 2 |
""" |
| 3 |
|
| 4 |
# Created on 2014.07.14 |
| 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 ALL |
| 29 |
from test.config import test_base, test_name_attr, random_id, get_connection, add_user, drop_connection, test_int_attr, test_server_type, get_response_values |
| 30 |
|
| 31 |
testcase_id = '' |
| 32 |
|
| 33 |
|
| 34 |
class Test(unittest.TestCase): |
| 35 |
def setUp(self): |
| 36 |
global testcase_id |
| 37 |
testcase_id = random_id() |
| 38 |
self.connection = get_connection(check_names=True, get_info=ALL) |
| 39 |
self.delete_at_teardown = [] |
| 40 |
if test_server_type == 'EDIR': |
| 41 |
self.delete_at_teardown.append(add_user(self.connection, testcase_id, 'chk-1', attributes={'loginGraceLimit': 0})) |
| 42 |
elif test_server_type == 'AD': |
| 43 |
self.delete_at_teardown.append(add_user(self.connection, testcase_id, 'chk-1')) |
| 44 |
else: |
| 45 |
self.delete_at_teardown.append(add_user(self.connection, testcase_id, 'chk-1')) |
| 46 |
|
| 47 |
def tearDown(self): |
| 48 |
drop_connection(self.connection, self.delete_at_teardown) |
| 49 |
self.assertFalse(self.connection.bound) |
| 50 |
|
| 51 |
def test_search_checked_attributes(self): |
| 52 |
status, result, response, request = get_response_values(self.connection.search(search_base=test_base, search_filter='(' + test_name_attr + '=' + testcase_id + 'chk-1*)', attributes=[test_name_attr, 'sn', test_int_attr]), self.connection) |
| 53 |
self.assertEqual(result['description'], 'success') |
| 54 |
self.assertEqual(len(response), 1) |
| 55 |
if test_server_type == 'AD': |
| 56 |
self.assertEqual(response[0]['attributes']['sn'], 'chk-1') # sn is single-value in Active Directory |
| 57 |
else: |
| 58 |
self.assertEqual(response[0]['attributes']['sn'][0], 'chk-1') |
| 59 |
self.assertEqual(response[0]['attributes'][test_int_attr], 0) |
| 60 |
if str != bytes: # python3 |
| 61 |
self.assertTrue(isinstance(response[0]['attributes']['sn'][0], str)) |
| 62 |
else: # python2 |
| 63 |
self.assertTrue(isinstance(response[0]['attributes']['sn'][0], unicode)) |
| 64 |
self.assertTrue(isinstance(response[0]['attributes'][test_int_attr], int)) |
| 65 |
|
| 66 |
def test_custom_formatter(self): |
| 67 |
def to_upper(byte_value): |
| 68 |
if str != bytes: # python 3 |
| 69 |
return str(byte_value, encoding='UTF-8').upper() |
| 70 |
else: |
| 71 |
return unicode(byte_value, encoding='UTF-8').upper() |
| 72 |
if str != bytes: # python3 |
| 73 |
formatter = {'cn': to_upper, # name to upper |
| 74 |
'2.5.4.4': lambda v: str(v, encoding='UTF-8')[::-1], # sn reversed |
| 75 |
'1.3.6.1.4.1.1466.115.121.1.27': lambda v: int(v) + 1000} # integer syntax incremented by 1000 |
| 76 |
else: |
| 77 |
formatter = {'cn': to_upper, # name to upper |
| 78 |
'2.5.4.4': lambda v: unicode(v, encoding='UTF-8')[::-1], # sn reversed |
| 79 |
'1.3.6.1.4.1.1466.115.121.1.27': lambda v: int(v) + 1000} # integer syntax incremented by 1000 |
| 80 |
self.connection.server.custom_formatter = formatter |
| 81 |
status, result, response, request = get_response_values(self.connection.search(search_base=test_base, search_filter='(' + test_name_attr + '=' + testcase_id + 'chk-1*)', attributes=[test_name_attr, 'sn', test_int_attr]), self.connection) |
| 82 |
self.assertEqual(result['description'], 'success') |
| 83 |
self.assertEqual(len(response), 1) |
| 84 |
if test_server_type == 'AD': |
| 85 |
self.assertTrue('CHK-1' in response[0]['attributes']['cn']) # cn is single-valued in Active Directory |
| 86 |
self.assertEqual(response[0]['attributes']['sn'], '1-khc') # sn is single-valued in Active Directory |
| 87 |
else: |
| 88 |
self.assertTrue('CHK-1' in response[0]['attributes']['cn'][0]) |
| 89 |
self.assertEqual(response[0]['attributes']['sn'][0], '1-khc') |
| 90 |
self.assertEqual(response[0]['attributes'][test_int_attr], 1000) |