| 1 |
""" |
| 2 |
""" |
| 3 |
|
| 4 |
# Created on 2013.06.06 |
| 5 |
# |
| 6 |
# Author: Giovanni Cannata |
| 7 |
# |
| 8 |
# Copyright 2013 - 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 |
import json |
| 28 |
|
| 29 |
from ldap3 import SUBTREE |
| 30 |
from ldap3.utils.conv import escape_bytes |
| 31 |
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 |
| 32 |
|
| 33 |
testcase_id = '' |
| 34 |
|
| 35 |
|
| 36 |
class Test(unittest.TestCase): |
| 37 |
def setUp(self): |
| 38 |
global testcase_id |
| 39 |
testcase_id = random_id() |
| 40 |
self.connection = get_connection() |
| 41 |
self.delete_at_teardown = [] |
| 42 |
if test_server_type == 'EDIR': |
| 43 |
self.delete_at_teardown.append(add_user(self.connection, testcase_id, 'sea-1', attributes={'givenName': 'givenname-1', test_int_attr: 0})) |
| 44 |
self.delete_at_teardown.append(add_user(self.connection, testcase_id, 'sea-2', attributes={'givenName': 'givenname-2', test_int_attr: 0})) |
| 45 |
elif test_server_type == 'AD': |
| 46 |
self.delete_at_teardown.append(add_user(self.connection, testcase_id, 'sea-1', attributes={'givenName': 'givenname-1'})) |
| 47 |
self.delete_at_teardown.append(add_user(self.connection, testcase_id, 'sea-2', attributes={'givenName': 'givenname-2'})) |
| 48 |
else: |
| 49 |
self.delete_at_teardown.append(add_user(self.connection, testcase_id, 'sea-1', attributes={'givenName': 'givenname-1'})) |
| 50 |
self.delete_at_teardown.append(add_user(self.connection, testcase_id, 'sea-2', attributes={'givenName': 'givenname-2'})) |
| 51 |
|
| 52 |
def tearDown(self): |
| 53 |
drop_connection(self.connection, self.delete_at_teardown) |
| 54 |
self.assertFalse(self.connection.bound) |
| 55 |
|
| 56 |
def test_search_exact_match(self): |
| 57 |
status, result, response, request = get_response_values(self.connection.search(search_base=test_base, search_filter='(' + test_name_attr + '=' + testcase_id + 'sea-1)', attributes=[test_name_attr, 'givenName']), self.connection) |
| 58 |
json_response = self.connection.response_to_json(search_result=response) |
| 59 |
json_entries = json.loads(json_response)['entries'] |
| 60 |
self.assertEqual(len(json_entries), 1) |
| 61 |
|
| 62 |
def test_search_extensible_match(self): |
| 63 |
if test_server_type == 'EDIR' and not self.connection.strategy.no_real_dsa: |
| 64 |
status, result, response, request = get_response_values(self.connection.search(search_base=test_base, search_filter='(&(ou:dn:=fixtures)(objectclass=inetOrgPerson))', attributes=[test_name_attr, 'givenName', 'sn']), self.connection) |
| 65 |
json_response = self.connection.response_to_json(search_result=response) |
| 66 |
json_entries = json.loads(json_response)['entries'] |
| 67 |
self.assertTrue(len(json_entries) >= 2) |
| 68 |
|
| 69 |
def test_search_present(self): |
| 70 |
status, result, response, request = get_response_values(self.connection.search(search_base=test_base, search_filter='(' + test_name_attr + '=*)', search_scope=SUBTREE, attributes=[test_name_attr, 'givenName']), self.connection) |
| 71 |
json_response = self.connection.response_to_json(search_result=response) |
| 72 |
json_entries = json.loads(json_response)['entries'] |
| 73 |
self.assertTrue(len(json_entries) >= 2) |
| 74 |
|
| 75 |
def test_search_substring_many(self): |
| 76 |
status, result, response, request = get_response_values(self.connection.search(search_base=test_base, search_filter='(' + test_name_attr + '=' + testcase_id + '*)', attributes=[test_name_attr, 'givenName']), self.connection) |
| 77 |
json_response = self.connection.response_to_json(search_result=response) |
| 78 |
json_entries = json.loads(json_response)['entries'] |
| 79 |
self.assertEqual(len(json_entries), 2) |
| 80 |
|
| 81 |
def test_search_with_operational_attributes(self): |
| 82 |
if test_server_type == 'EDIR': |
| 83 |
test_operation_attribute = 'entryDN' |
| 84 |
elif test_server_type == 'SLAPD': |
| 85 |
test_operation_attribute = 'entryDN' |
| 86 |
else: |
| 87 |
test_operation_attribute = 'xxx' |
| 88 |
status, result, response, request = get_response_values(self.connection.search(search_base=test_base, search_filter='(' + test_name_attr + '=' + testcase_id + 'sea-1)', search_scope=SUBTREE, attributes=[test_name_attr, 'givenName'], get_operational_attributes=True), self.connection) |
| 89 |
json_response = self.connection.response_to_json(search_result=response) |
| 90 |
json_entries = json.loads(json_response)['entries'] |
| 91 |
|
| 92 |
if self.connection.check_names: |
| 93 |
if test_server_type == 'AD': |
| 94 |
self.assertEqual(json_entries[0]['dn'].lower(), self.delete_at_teardown[0][0].lower()) |
| 95 |
elif test_server_type == 'SLAPD': |
| 96 |
self.assertEqual(json_entries[0]['attributes'][test_operation_attribute], self.delete_at_teardown[0][0]) |
| 97 |
else: |
| 98 |
self.assertEqual(json_entries[0]['attributes'][test_operation_attribute], self.delete_at_teardown[0][0]) |
| 99 |
|
| 100 |
def test_search_exact_match_with_parentheses_in_filter(self): |
| 101 |
self.delete_at_teardown.append(add_user(self.connection, testcase_id, '(search)-3', attributes={'givenName': 'givenname-3'})) |
| 102 |
status, result, response, request = get_response_values(self.connection.search(search_base=test_base, search_filter='(&(' + test_name_attr + '=' + testcase_id + '*)(' + test_name_attr + '=*' + escape_bytes(')') + '*))', attributes=[test_name_attr, 'sn']), self.connection) |
| 103 |
json_response = self.connection.response_to_json(search_result=response) |
| 104 |
json_entries = json.loads(json_response)['entries'] |
| 105 |
|
| 106 |
self.assertEqual(len(json_entries), 1) |
| 107 |
if test_server_type == 'AD': |
| 108 |
self.assertEqual(json_entries[0]['attributes'][test_name_attr], testcase_id + '(search)-3') |
| 109 |
else: |
| 110 |
self.assertEqual(json_entries[0]['attributes'][test_name_attr][0], testcase_id + '(search)-3') |
| 111 |
|
| 112 |
def test_search_integer_exact_match(self): |
| 113 |
status, result, response, request = get_response_values(self.connection.search(search_base=test_base, search_filter='(&(' + test_name_attr + '=' + testcase_id + '*)(' + test_int_attr + '=0))', attributes=[test_name_attr, test_int_attr]), self.connection) |
| 114 |
json_response = self.connection.response_to_json(search_result=response) |
| 115 |
json_entries = json.loads(json_response)['entries'] |
| 116 |
self.assertEqual(len(json_entries), 2) |
| 117 |
|
| 118 |
def test_search_integer_less_than(self): |
| 119 |
status, result, response, request = get_response_values(self.connection.search(search_base=test_base, search_filter='(&(' + test_name_attr + '=' + testcase_id + '*)(' + test_int_attr + ' <=1))', attributes=[test_name_attr, test_int_attr]), self.connection) |
| 120 |
json_response = self.connection.response_to_json(search_result=response) |
| 121 |
json_entries = json.loads(json_response)['entries'] |
| 122 |
self.assertEqual(len(json_entries), 2) |
| 123 |
|
| 124 |
def test_search_integer_greater_than(self): |
| 125 |
status, result, response, request = get_response_values(self.connection.search(search_base=test_base, search_filter='(&(' + test_name_attr + '=' + testcase_id + '*)(' + test_int_attr + '>=-1))', attributes=[test_name_attr, test_int_attr]), self.connection) |
| 126 |
json_response = self.connection.response_to_json(search_result=response) |
| 127 |
json_entries = json.loads(json_response)['entries'] |
| 128 |
self.assertEqual(len(json_entries), 2) |