Debian Codegraph @

test/testMicrosoftAD.py

raw
1 # -*- coding: utf-8 -*-
2 """
3 """
4
5 # Created on 2013.06.06
6 #
7 # Author: Giovanni Cannata
8 #
9 # Copyright 2013 - 2020 Giovanni Cannata
10 #
11 # This file is part of ldap3.
12 #
13 # ldap3 is free software: you can redistribute it and/or modify
14 # it under the terms of the GNU Lesser General Public License as published
15 # by the Free Software Foundation, either version 3 of the License, or
16 # (at your option) any later version.
17 #
18 # ldap3 is distributed in the hope that it will be useful,
19 # but WITHOUT ANY WARRANTY; without even the implied warranty of
20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 # GNU Lesser General Public License for more details.
22 #
23 # You should have received a copy of the GNU Lesser General Public License
24 # along with ldap3 in the COPYING and COPYING.LESSER files.
25 # If not, see <http://www.gnu.org/licenses/>.
26
27 import unittest
28 from time import sleep
29
30 from ldap3 import SUBTREE, MODIFY_ADD, MODIFY_REPLACE, MODIFY_DELETE, SIMPLE, REUSABLE
31 from ldap3.protocol.microsoft import extended_dn_control, show_deleted_control
32 from test.config import test_base, test_name_attr, random_id, get_connection, add_user, drop_connection, test_server_type, test_root_partition, test_strategy
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 if test_server_type == 'AD':
42 self.connection = get_connection(use_ssl=True)
43 self.delete_at_teardown = []
44
45 def tearDown(self):
46 if test_server_type == 'AD':
47 drop_connection(self.connection, self.delete_at_teardown)
48 self.assertFalse(self.connection.bound)
49
50 def test_search_extended_dn_ad(self):
51 if test_server_type == 'AD':
52 self.delete_at_teardown.append(add_user(self.connection, testcase_id, 'sea-1', attributes={'givenName': 'sea-1'}))
53
54 result = self.connection.search(search_base=test_base, search_filter='(' + test_name_attr + '=' + testcase_id + 'sea-1)', attributes=[test_name_attr], controls=[extended_dn_control(), show_deleted_control()])
55 if not self.connection.strategy.sync:
56 response, result = self.connection.get_response(result)
57 else:
58 response = self.connection.response
59 result = self.connection.result
60
61 self.assertEqual(result['description'], 'success')
62 self.assertTrue('<GUID=' in response[0]['dn'])
63 self.assertTrue('SID=' in response[0]['dn'])
64 self.assertTrue('>;' in response[0]['dn'])
65
66 def test_search_deleted_objects_ad(self):
67 if test_server_type == 'AD':
68 dn_to_delete, _ = add_user(self.connection, testcase_id, 'del-1', attributes={'givenName': 'del-1'})
69 sleep(2)
70 self.connection.delete(dn_to_delete)
71 sleep(5)
72 result = self.connection.search(search_base=test_root_partition,
73 search_filter='(&(isDeleted=TRUE)(cn=*' + testcase_id + '*del-1*))',
74 search_scope=SUBTREE,
75 attributes=[],
76 controls=[show_deleted_control(criticality=True)])
77 if not self.connection.strategy.sync:
78 response, result = self.connection.get_response(result)
79 else:
80 response = self.connection.response
81 result = self.connection.result
82 found = False
83 for entry in response:
84 if entry['type'] == 'searchResEntry' and testcase_id in entry['dn']:
85 found = True
86 break
87
88 self.assertTrue(found)
89
90 def test_dir_sync(self):
91 # if False: # takes a long long time to complete
92 if test_server_type == 'AD':
93 sync = self.connection.extend.microsoft.dir_sync(test_root_partition, attributes=['*'], incremental_values=True)
94 # read all previous changes
95 while sync.more_results:
96 print('PREV', len(sync.loop()))
97
98 # add a new object and verify the sync
99 dn, _ = add_user(self.connection, testcase_id, 'to-be-deleted-1', attributes={'givenName': 'to-be-deleted-1'})
100 sleep(1)
101 response = sync.loop()
102 print('ADD OBJ', len(response), response[0]['attributes'])
103 found = False
104 for entry in response:
105 if entry['type'] == 'searchResEntry' and testcase_id + 'to-be-deleted-1' in entry['dn']:
106 found = True
107 break
108 self.assertTrue(found)
109
110 # modify-add an attribute and verify the sync
111 result = self.connection.modify(dn, {'businessCategory': (MODIFY_ADD, ['businessCategory-1-added'])})
112 if not self.connection.strategy.sync:
113 _, result = self.connection.get_response(result)
114 else:
115 result = self.connection.result
116 self.assertEqual(result['description'], 'success')
117 sleep(1)
118 response = sync.loop()
119 print('MOD-ADD ATTR', len(response), response[0]['attributes'])
120 found = False
121 for entry in response:
122 if entry['type'] == 'searchResEntry' and testcase_id + 'to-be-deleted-1' in entry['dn']:
123 found = True
124 break
125 self.assertTrue(found)
126
127 # modify-replace an attribute and verify the sync
128 result = self.connection.modify(dn, {'businessCategory': (MODIFY_REPLACE, ['businessCategory-1-replaced']), 'sn': (MODIFY_REPLACE, ['sn-replaced'])})
129 if not self.connection.strategy.sync:
130 _, result = self.connection.get_response(result)
131 else:
132 result = self.connection.result
133 self.assertEqual(result['description'], 'success')
134 sleep(1)
135 response = sync.loop()
136 print('MOD-REPLACE ATTR', len(response), response[0]['attributes'])
137 found = False
138 for entry in response:
139 if entry['type'] == 'searchResEntry' and testcase_id + 'to-be-deleted-1' in entry['dn']:
140 found = True
141 break
142 self.assertTrue(found)
143
144 # modify-delete an attribute and verify the sync
145 # result = self.connection.modify(dn, {'businessCategory': (MODIFY_ADD, ['businessCategory-2-added', 'businessCategory-3-added'])})
146 # if not self.connection.strategy.sync:
147 # _, result = self.connection.get_response(result)
148 # else:
149 # result = self.connection.result
150 # self.assertEqual(result['description'], 'success')
151 result = self.connection.modify(dn, {'businessCategory': (MODIFY_DELETE, ['businessCategory-1-replaced'])})
152 if not self.connection.strategy.sync:
153 _, result = self.connection.get_response(result)
154 else:
155 result = self.connection.result
156 self.assertEqual(result['description'], 'success')
157 sleep(1)
158 response = sync.loop()
159 print('MOD-DEL ATTR', len(response), response[0]['attributes'])
160 found = False
161 for entry in response:
162 if entry['type'] == 'searchResEntry' and testcase_id + 'to-be-deleted-1' in entry['dn']:
163 found = True
164 break
165 self.assertTrue(found)
166
167 # delete object and verify the sync
168 self.connection.delete(dn)
169 sleep(1)
170 response = sync.loop()
171 print('DEL OBJ', len(response), response[0]['attributes'])
172 found = False
173 for entry in response:
174 if entry['type'] == 'searchResEntry' and testcase_id + 'to-be-deleted-1' in entry['dn']:
175 found = True
176 break
177
178 self.assertTrue(found)
179
180 def test_modify_password_as_administrator(self):
181 if test_server_type == 'AD' and test_strategy != REUSABLE:
182 self.delete_at_teardown.append(add_user(self.connection, testcase_id, 'pwd-1', attributes={'givenName': 'changed-password-1'}))
183 dn = self.delete_at_teardown[-1][0]
184 # test_connection = get_connection(bind=False, authentication=SIMPLE, simple_credentials=(dn, 'Rc1234abcd'))
185 # test_connection.bind()
186 # self.assertTrue(test_connection.bound)
187 # connected_user = test_connection.extend.standard.who_am_i()
188 # test_connection.unbind()
189 # self.assertTrue('changed-password-1' in connected_user)
190
191 new_password = 'Rc567812àèìòù'
192 result = self.connection.extend.microsoft.modify_password(dn, new_password)
193 self.assertEqual(result, True)
194
195 # creates a second connection and tries to bind with the new password
196 test_connection = get_connection(bind=False, authentication=SIMPLE, simple_credentials=(dn, new_password))
197 test_connection.bind()
198 connected_user = test_connection.extend.standard.who_am_i()
199 test_connection.unbind()
200
201 self.assertTrue('pwd-1' in connected_user)
202
203 # def test_modify_password_as_normal_user(self):
204 # if test_server_type == 'AD':
205 # old_password = 'Ab123456cdef'
206 # new_password = 'Gh567890ijkl'
207 # self.delete_at_teardown.append(add_user(self.connection, testcase_id, 'pwd-2', password=old_password, attributes={'givenName': 'changed-password-2'}))
208 # dn = self.delete_at_teardown[-1][0]
209 # # creates a second connection and tries to bind with the new password
210 # test_connection = get_connection(bind=False, use_ssl=True, authentication=SIMPLE, simple_credentials=(dn, old_password))
211 # test_connection.bind()
212 # self.assertTrue(test_connection.bound)
213 # connected_user = test_connection.extend.standard.who_am_i()
214 # test_connection.unbind()
215 # self.assertTrue('pwd-2' in connected_user)
216 #
217 # # changee the password
218 # result = self.connection.extend.microsoft.modify_password(dn, new_password, old_password)
219 # self.assertEqual(result, True)
220 #
221 # # tries to bind with the new password
222 # test_connection.password = new_password
223 # test_connection.bind()
224 # connected_user = test_connection.extend.standard.who_am_i()
225 # test_connection.unbind()
226 #
227 # self.assertTrue('changed-password-2' in connected_user)
228
229 def test_modify_existing_password_as_administrator(self):
230 if test_server_type == 'AD' and test_strategy != REUSABLE:
231 self.delete_at_teardown.append(add_user(self.connection, testcase_id, 'pwd-3', attributes={'givenName': 'pwd-3'}))
232 dn = self.delete_at_teardown[-1][0]
233 new_password = 'Rc56789efgh'
234 result = self.connection.extend.microsoft.modify_password(dn, new_password)
235 self.assertEqual(result, True)
236 # creates a second connection and tries to bind with the new password
237 test_connection = get_connection(bind=False, authentication=SIMPLE, simple_credentials=(dn, new_password))
238 test_connection.bind()
239 connected_user = test_connection.extend.standard.who_am_i()
240 test_connection.unbind()
241 self.assertTrue('pwd-3' in connected_user)
242
243 # def test_search_with_auto_range(self):
244 # if test_server_type == 'AD':
245 # user_dns = []
246 # for i in range(0, 6999):
247 # try:
248 # user_dn, _ = add_user(self.connection, '', 'user-' + str(i).zfill(4), attributes={'givenName': 'givenname-' + str(i).zfill(4)})
249 # user_dns.append(user_dn)
250 # print('created', user_dn)
251 # except Exception as e:
252 # # if 'entryAlreadyExists' not in e.args[0]:
253 # # raise
254 # pass
255 # self.connection.extend.microsoft.add_members_to_groups(user_dns, 'CN=testgrp,OU=test,DC=AD2012,DC=LAB', fix=True)
256 # print(self.connection.auto_range)
257 # self.connection.auto_range = False
258 # result = self.connection.search(search_base=test_base, search_filter='(' + test_name_attr + '=testgrp)', attributes=[test_name_attr, 'member'])
259 # print(result)
260 # print(self.connection.response[0]['attributes'].keys())
261 # print (len(self.connection.response[0]['attributes']['member']))