| 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 |
from time import sleep |
| 27 |
|
| 28 |
from ldap3 import Writer, Reader, AttrDef, ObjectDef |
| 29 |
from ldap3.core.exceptions import LDAPCursorError |
| 30 |
from test.config import test_base, get_connection, drop_connection, random_id, test_moved, add_user, test_multivalued_attribute, test_server_type |
| 31 |
from ldap3.abstract import STATUS_COMMITTED, STATUS_MANDATORY_MISSING, STATUS_DELETED, STATUS_PENDING_CHANGES, STATUS_READ, \ |
| 32 |
STATUS_READY_FOR_DELETION, STATUS_READY_FOR_MOVING, STATUS_READY_FOR_RENAMING, STATUS_VIRTUAL, STATUS_WRITABLE |
| 33 |
|
| 34 |
testcase_id = '' |
| 35 |
|
| 36 |
|
| 37 |
class Test(unittest.TestCase): |
| 38 |
def setUp(self): |
| 39 |
global testcase_id |
| 40 |
testcase_id = random_id() |
| 41 |
self.connection = get_connection() |
| 42 |
self.delete_at_teardown = [] |
| 43 |
|
| 44 |
def tearDown(self): |
| 45 |
drop_connection(self.connection, self.delete_at_teardown) |
| 46 |
self.assertFalse(self.connection.bound) |
| 47 |
|
| 48 |
def test_create_entry_with_attribute_from_auxiliary_class(self): |
| 49 |
if test_server_type != 'AD': |
| 50 |
w = Writer(self.connection, 'inetorgperson', auxiliary_class='homeInfo') |
| 51 |
n = w.new('cn=' + testcase_id + 'new-1,' + test_base) |
| 52 |
n.sn = 'sn-test-1' |
| 53 |
n.homeState = 'state-test-1' |
| 54 |
self.assertEqual(n.entry_status, STATUS_PENDING_CHANGES) |
| 55 |
n.entry_commit_changes() |
| 56 |
self.assertEqual(n.sn, 'sn-test-1') |
| 57 |
self.assertEqual(n.homeState, 'state-test-1') |
| 58 |
self.assertEqual(n.entry_status, STATUS_COMMITTED) |
| 59 |
n.entry_delete() |
| 60 |
self.assertEqual(n.entry_status, STATUS_READY_FOR_DELETION) |
| 61 |
n.entry_commit_changes() |
| 62 |
self.assertEqual(n.entry_status, STATUS_DELETED) |
| 63 |
|
| 64 |
def test_create_invalid_entry_with_attribute_from_auxiliary_class_not_declared(self): |
| 65 |
w = Writer(self.connection, 'inetorgperson') |
| 66 |
n = w.new('cn=' + testcase_id + 'new-2,' + test_base) |
| 67 |
n.sn = 'sn-test-2' |
| 68 |
with self.assertRaises(LDAPCursorError): |
| 69 |
n.homeState = 'state-test-2' |
| 70 |
|
| 71 |
def test_read_entry_with_attribute_from_auxiliary_class(self): |
| 72 |
if test_server_type != 'AD': |
| 73 |
w = Writer(self.connection, 'inetorgperson', auxiliary_class='homeInfo') |
| 74 |
n = w.new('cn=' + testcase_id + 'new-3,' + test_base) |
| 75 |
n.sn = 'sn-test-3' |
| 76 |
n.homeState = 'state-test-3' |
| 77 |
self.assertEqual(n.entry_status, STATUS_PENDING_CHANGES) |
| 78 |
n.entry_commit_changes() |
| 79 |
self.assertEqual(n.sn, 'sn-test-3') |
| 80 |
self.assertEqual(n.homeState, 'state-test-3') |
| 81 |
self.assertEqual(n.entry_status, STATUS_COMMITTED) |
| 82 |
|
| 83 |
r = Reader(self.connection, 'inetorgperson', test_base, '(cn=' + testcase_id + 'new-3', auxiliary_class='homeInfo') |
| 84 |
r.search() |
| 85 |
self.assertTrue(r[0].cn, testcase_id + 'new-3') |
| 86 |
self.assertTrue(r[0].homeState, testcase_id + 'state-test-3') |
| 87 |
w1 = Writer.from_cursor(r) |
| 88 |
w1[0].entry_delete() |
| 89 |
w1.commit() |
| 90 |
|
| 91 |
def test_read_entry_with_attribute_from_missing_auxiliary_class(self): |
| 92 |
if test_server_type != 'AD': |
| 93 |
w = Writer(self.connection, 'inetorgperson', auxiliary_class='homeInfo') |
| 94 |
n = w.new('cn=' + testcase_id + 'new-4,' + test_base) |
| 95 |
n.sn = 'sn-test-4' |
| 96 |
n.homeState = 'state-test-4' |
| 97 |
self.assertEqual(n.entry_status, STATUS_PENDING_CHANGES) |
| 98 |
n.entry_commit_changes() |
| 99 |
self.assertEqual(n.sn, 'sn-test-4') |
| 100 |
self.assertEqual(n.homeState, 'state-test-4') |
| 101 |
self.assertEqual(n.entry_status, STATUS_COMMITTED) |
| 102 |
|
| 103 |
r = Reader(self.connection, 'inetorgperson', test_base, '(cn=' + testcase_id + 'new-4') |
| 104 |
r.search() |
| 105 |
self.assertTrue(r[0].cn, testcase_id + 'new-4') |
| 106 |
with self.assertRaises(LDAPCursorError): |
| 107 |
self.assertTrue(r[0].homeState, testcase_id + 'state-test-3') |
| 108 |
w1 = Writer.from_cursor(r) |
| 109 |
w1[0].entry_delete() |
| 110 |
w1.commit() |