| 1 |
""" |
| 2 |
""" |
| 3 |
|
| 4 |
# Created on 2014.05.01 |
| 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 ldap3.core.exceptions import LDAPAttributeError, LDAPObjectClassError |
| 30 |
from test.config import test_base, generate_dn, test_name_attr, random_id, get_connection, add_user, drop_connection, get_response_values |
| 31 |
|
| 32 |
testcase_id = '' |
| 33 |
|
| 34 |
|
| 35 |
class Test(unittest.TestCase): |
| 36 |
def setUp(self): |
| 37 |
global testcase_id |
| 38 |
testcase_id = random_id() |
| 39 |
self.connection = get_connection(check_names=True, get_info=ALL) |
| 40 |
self.delete_at_teardown = [] |
| 41 |
|
| 42 |
def tearDown(self): |
| 43 |
drop_connection(self.connection, self.delete_at_teardown) |
| 44 |
self.assertFalse(self.connection.bound) |
| 45 |
|
| 46 |
def test_wrong_assertion(self): |
| 47 |
if not self.connection.strategy.pooled: |
| 48 |
self.assertRaises(LDAPAttributeError, self.connection.search, search_base=test_base, search_filter='(xxx=yyy)', attributes=[test_name_attr]) |
| 49 |
|
| 50 |
def test_wrong_attribute(self): |
| 51 |
if not self.connection.strategy.pooled: |
| 52 |
self.assertRaises(LDAPAttributeError, self.connection.search, search_base=test_base, search_filter='(cn=yyy)', attributes=[test_name_attr, 'xxx']) |
| 53 |
|
| 54 |
def test_wrong_object_class_add(self): |
| 55 |
if not self.connection.strategy.pooled: |
| 56 |
self.assertRaises(LDAPObjectClassError, self.connection.add, generate_dn(test_base, testcase_id, 'test-add-operation-wrong'), 'inetOrgPerson', {'objectClass': ['inetOrgPerson', 'xxx'], 'sn': 'test-add', test_name_attr: 'test-add-operation'}) |
| 57 |
|
| 58 |
def test_valid_assertion(self): |
| 59 |
self.delete_at_teardown.append(add_user(self.connection, testcase_id, 'chk-1')) |
| 60 |
|
| 61 |
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]), self.connection) |
| 62 |
self.assertEqual(result['description'], 'success') |
| 63 |
self.assertEqual(len(response), 1) |
| 64 |
|
| 65 |
def test_valid_attribute(self): |
| 66 |
self.delete_at_teardown.append(add_user(self.connection, testcase_id, 'chk-2', attributes={'givenName': 'given-name-2'})) |
| 67 |
status, result, response, request = get_response_values(self.connection.search(search_base=test_base, search_filter='(' + test_name_attr + '=' + testcase_id + 'chk-2)', attributes=[test_name_attr, 'givenName']), self.connection) |
| 68 |
self.assertEqual(result['description'], 'success') |
| 69 |
self.assertEqual(len(response), 1) |
| 70 |
|
| 71 |
def test_valid_object_class_add(self): |
| 72 |
self.delete_at_teardown.append(add_user(self.connection, testcase_id, 'chk-3', attributes={'objectClass': ['inetOrgPerson', 'Person']})) |
| 73 |
self.assertEqual(self.delete_at_teardown[0][1]['description'], 'success') |