Debian Codegraph @

dirsrvtests/tests/suites/clu/dsidm_account_test.py

raw
1 # --- BEGIN COPYRIGHT BLOCK ---
2 # Copyright (C) 2025 Red Hat, Inc.
3 # All rights reserved.
4 #
5 # License: GPL (version 3 or any later version).
6 # See LICENSE for details.
7 # --- END COPYRIGHT BLOCK ---
8 #
9
10 import logging
11 import os
12 import json
13 import pytest
14 import ldap
15 from lib389 import DEFAULT_SUFFIX
16 from lib389.cli_idm.account import list, get_dn, lock, unlock, delete, modify, rename, entry_status, \
17 subtree_status, reset_password, change_password
18 from lib389.cli_idm.user import create
19 from test389.topologies import topology_st
20 from lib389.cli_base import FakeArgs
21 from lib389.utils import ds_is_older, is_a_dn
22 from lib389.idm.user import nsUserAccounts
23 from . import check_value_in_log_and_reset
24
25 pytestmark = pytest.mark.tier0
26
27 logging.getLogger(__name__).setLevel(logging.DEBUG)
28 log = logging.getLogger(__name__)
29
30 test_user_name = 'test_user_1000'
31
32 @pytest.fixture(scope="function")
33 def create_test_user(topology_st, request):
34 log.info('Create test user')
35 users = nsUserAccounts(topology_st.standalone, DEFAULT_SUFFIX)
36
37 if users.exists(test_user_name):
38 test_user = users.get(test_user_name)
39 test_user.delete()
40
41 properties = FakeArgs()
42 properties.user_type = 'posix'
43 properties.uid = test_user_name
44 properties.cn = test_user_name
45 properties.sn = test_user_name
46 properties.uidNumber = '1000'
47 properties.gidNumber = '2000'
48 properties.homeDirectory = '/home/test_user_1000'
49 properties.displayName = test_user_name
50
51 create(topology_st.standalone, DEFAULT_SUFFIX, topology_st.logcap.log, properties)
52 test_user = users.get(test_user_name)
53
54 def fin():
55 log.info('Delete test user')
56 if test_user.exists():
57 test_user.delete()
58
59 request.addfinalizer(fin)
60
61
62 @pytest.mark.skipif(ds_is_older("1.4.2"), reason="Not implemented")
63 def test_dsidm_account_entry_status_with_lock(topology_st, create_test_user):
64 """ Test dsidm account entry-status option with account lock/unlock
65
66 :id: d911bbf2-3a65-42a4-ad76-df1114caa396
67 :setup: Standalone instance
68 :steps:
69 1. Create user account
70 2. Run dsidm account entry status
71 3. Run dsidm account lock
72 4. Run dsidm account subtree status
73 5. Run dsidm account entry status
74 6. Run dsidm account unlock
75 7. Run dsidm account subtree status
76 8. Run dsidm account entry status
77 :expectedresults:
78 1. Success
79 2. The state message should be Entry State: activated
80 3. Success
81 4. The state message should be Entry State: directly locked through nsAccountLock
82 5. Success
83 6. The state message should be Entry State: activated
84 7. Success
85 8. The state message should be Entry State: activated
86 """
87
88 standalone = topology_st.standalone
89 users = nsUserAccounts(standalone, DEFAULT_SUFFIX)
90 test_user = users.get(test_user_name)
91
92 entry_list = ['Entry DN: {}'.format(test_user.dn),
93 'Entry Creation Date',
94 'Entry Modification Date']
95
96 state_lock = 'Entry State: directly locked through nsAccountLock'
97 state_unlock = 'Entry State: activated'
98
99 lock_msg = 'Entry {} is locked'.format(test_user.dn)
100 unlock_msg = 'Entry {} is unlocked'.format(test_user.dn)
101
102 args = FakeArgs()
103 args.dn = test_user.dn
104 args.user_type = 'posix'
105 args.json = False
106 args.basedn = DEFAULT_SUFFIX
107 args.scope = ldap.SCOPE_SUBTREE
108 args.filter = "(uid=*)"
109 args.become_inactive_on = False
110 args.inactive_only = False
111 args.json = False
112
113 log.info('Test dsidm account entry-status')
114 entry_status(standalone, DEFAULT_SUFFIX, topology_st.logcap.log, args)
115 check_value_in_log_and_reset(topology_st, content_list=entry_list,
116 check_value=state_unlock)
117
118 log.info('Test dsidm account lock')
119 lock(standalone, DEFAULT_SUFFIX, topology_st.logcap.log, args)
120 check_value_in_log_and_reset(topology_st, check_value=lock_msg)
121
122 log.info('Test dsidm account subtree-status with locked account')
123 subtree_status(standalone, DEFAULT_SUFFIX, topology_st.logcap.log, args)
124 check_value_in_log_and_reset(topology_st, content_list=entry_list,
125 check_value=state_lock)
126
127 log.info('Test dsidm account entry-status with locked account')
128 entry_status(standalone, DEFAULT_SUFFIX, topology_st.logcap.log, args)
129 check_value_in_log_and_reset(topology_st, content_list=entry_list,
130 check_value=state_lock)
131
132 log.info('Test dsidm account unlock')
133 unlock(standalone, DEFAULT_SUFFIX, topology_st.logcap.log, args)
134 check_value_in_log_and_reset(topology_st, check_value=unlock_msg)
135
136 log.info('Test dsidm account subtree-status with unlocked account')
137 subtree_status(standalone, DEFAULT_SUFFIX, topology_st.logcap.log, args)
138 check_value_in_log_and_reset(topology_st, content_list=entry_list,
139 check_value=state_unlock)
140
141 log.info('Test dsidm account entry-status with unlocked account')
142 entry_status(standalone, DEFAULT_SUFFIX, topology_st.logcap.log, args)
143 check_value_in_log_and_reset(topology_st, content_list=entry_list,
144 check_value=state_unlock)
145
146
147 def test_dsidm_account_entry_get_by_dn(topology_st, create_test_user):
148 """ Test dsidm account get_dn works with non-json and json
149
150 :id: dd848f67c-9944-48a4-ae5e-98dce4fbc364
151 :setup: Standalone instance
152 :steps:
153 1. Get user by DN (non-json)
154 2. Get user by DN (json)
155 :expectedresults:
156 1. Success
157 2. Success
158 """
159
160 inst = topology_st.standalone
161 user_dn = "uid=test_user_1000,ou=people,dc=example,dc=com"
162
163 args = FakeArgs()
164 args.dn = user_dn
165 args.user_type = 'posix'
166 args.json = False
167 args.basedn = DEFAULT_SUFFIX
168 args.scope = ldap.SCOPE_SUBTREE
169 args.filter = "(uid=*)"
170 args.become_inactive_on = False
171 args.inactive_only = False
172
173 # Test non-json result
174 check_val = "homeDirectory: /home/test_user_1000"
175 get_dn(inst, DEFAULT_SUFFIX, topology_st.logcap.log, args)
176 check_value_in_log_and_reset(topology_st, check_value=check_val)
177
178 # Test json
179 args.json = True
180 get_dn(inst, DEFAULT_SUFFIX, topology_st.logcap.log, args)
181
182 result = topology_st.logcap.get_raw_outputs()
183 json_result = json.loads(result[0])
184 assert json_result['dn'] == user_dn
185
186
187 @pytest.mark.skipif(ds_is_older("1.4.2"), reason="Not implemented")
188 def test_dsidm_account_delete(topology_st, create_test_user):
189 """ Test dsidm account delete option
190
191 :id: a7960bc2-0282-4a82-8dfb-3af2088ec661
192 :setup: Standalone
193 :steps:
194 1. Run dsidm account delete on a created account
195 2. Check that a message is provided on deletion
196 3. Check that the account no longer exists
197 :expectedresults:
198 1. Success
199 2. Success
200 3. Success
201 """
202
203 standalone = topology_st.standalone
204 accounts = nsUserAccounts(standalone, DEFAULT_SUFFIX)
205 test_account = accounts.get(test_user_name)
206 output = 'Successfully deleted {}'.format(test_account.dn)
207
208 args = FakeArgs()
209 args.dn = test_account.dn
210 args.user_type = 'posix'
211
212 log.info('Test dsidm account delete')
213 delete(standalone, DEFAULT_SUFFIX, topology_st.logcap.log, args, warn=False)
214 check_value_in_log_and_reset(topology_st, check_value=output)
215
216 log.info('Check that the account no longer exists')
217 assert not test_account.exists()
218
219
220 @pytest.mark.skipif(ds_is_older("1.4.2"), reason="Not implemented")
221 def test_dsidm_account_list(topology_st, create_test_user):
222 """ Test dsidm account list option
223
224 :id: 4d173a3e-ee36-4a8b-8d0d-4955c792faca
225 :setup: Standalone instance
226 :steps:
227 1. Run dsidm account list without json
228 2. Check the output content is correct
229 3. Run dsidm account list with json
230 4. Check the output content is correct
231 5. Test full_dn option with list
232 6. Delete the account
233 7. Check the account is not in the list with json
234 8. Check the account is not in the list without json
235 :expectedresults:
236 1. Success
237 2. Success
238 3. Success
239 4. Success
240 5. Success
241 6. Success
242 7. Success
243 8. Success
244 """
245
246 standalone = topology_st.standalone
247 args = FakeArgs()
248 args.json = False
249 args.full_dn = False
250 json_list = ['type',
251 'list',
252 'items']
253
254 log.info('Empty the log file to prevent false data to check about group')
255 topology_st.logcap.flush()
256
257 log.info('Test dsidm account list without json')
258 list(standalone, DEFAULT_SUFFIX, topology_st.logcap.log, args)
259 check_value_in_log_and_reset(topology_st, check_value=test_user_name)
260
261 log.info('Test dsidm account list with json')
262 args.json = True
263 list(standalone, DEFAULT_SUFFIX, topology_st.logcap.log, args)
264 check_value_in_log_and_reset(topology_st, content_list=json_list, check_value=test_user_name)
265
266 log.info('Test full_dn option with list')
267 args.full_dn = True
268 list(standalone, DEFAULT_SUFFIX, topology_st.logcap.log, args)
269 result = topology_st.logcap.get_raw_outputs()
270 json_result = json.loads(result[0])
271 assert is_a_dn(json_result['items'][0])
272 args.full_dn = False
273 topology_st.logcap.flush()
274
275 log.info('Delete the account')
276 accounts = nsUserAccounts(standalone, DEFAULT_SUFFIX)
277 test_account = accounts.get(test_user_name)
278 test_account.delete()
279
280 log.info('Test empty dsidm account list with json')
281 list(standalone,DEFAULT_SUFFIX, topology_st.logcap.log, args)
282 check_value_in_log_and_reset(topology_st, content_list=json_list, check_value_not=test_user_name)
283
284 log.info('Test empty dsidm account list without json')
285 args.json = False
286 list(standalone,DEFAULT_SUFFIX, topology_st.logcap.log, args)
287 check_value_in_log_and_reset(topology_st, check_value_not=test_user_name)
288
289
290 @pytest.mark.xfail(reason='DS6515')
291 @pytest.mark.skipif(ds_is_older("1.4.2"), reason="Not implemented")
292 def test_dsidm_account_get_by_dn(topology_st, create_test_user):
293 """ Test dsidm account get-by-dn option
294
295 :id: 07945577-2da0-4fd9-9237-43dd2823f7b8
296 :setup: Standalone instance
297 :steps:
298 1. Run dsidm account get-by-dn for an account without json
299 2. Check the output content is correct
300 3. Run dsidm account get-by-dn for an account with json
301 4. Check the output content is correct
302 :expectedresults:
303 1. Success
304 2. Success
305 3. Success
306 4. Success
307 """
308
309 standalone = topology_st.standalone
310 accounts = nsUserAccounts(standalone, DEFAULT_SUFFIX)
311 test_account = accounts.get(test_user_name)
312
313 args = FakeArgs()
314 args.dn = test_account.dn
315 args.user_type = 'posix'
316 args.json = False
317
318 account_content = ['dn: {}'.format(test_account.dn),
319 'cn: {}'.format(test_account.rdn),
320 'displayName: {}'.format(test_user_name),
321 'gidNumber: 2000',
322 'homeDirectory: /home/{}'.format(test_user_name),
323 'objectClass: top',
324 'objectClass: nsPerson',
325 'objectClass: nsAccount',
326 'objectClass: nsOrgPerson',
327 'objectClass: posixAccount',
328 'uid: {}'.format(test_user_name),
329 'uidNumber: 1000']
330
331 json_content = ['attrs',
332 'objectclass',
333 'top',
334 'nsPerson',
335 'nsAccount',
336 'nsOrgPerson',
337 'posixAccount',
338 'cn',
339 test_account.rdn,
340 'gidnumber',
341 '2000',
342 'homedirectory',
343 '/home/{}'.format(test_user_name),
344 'displayname',
345 test_user_name,
346 'uidnumber',
347 '1000',
348 'creatorsname',
349 'cn=directory manager',
350 'modifiersname',
351 'createtimestamp',
352 'modifytimestamp',
353 'nsuniqueid',
354 'parentid',
355 'entryid',
356 'entryuuid',
357 'dsentrydn',
358 'entrydn',
359 test_account.dn]
360
361 log.info('Empty the log file to prevent false data to check about the account')
362 topology_st.logcap.flush()
363
364 log.info('Test dsidm account get-by-dn without json')
365 get_dn(standalone, DEFAULT_SUFFIX, topology_st.logcap.log, args)
366 check_value_in_log_and_reset(topology_st, content_list=account_content)
367
368 log.info('Test dsidm account get-by-dn with json')
369 args.json = True
370 get_dn(standalone, DEFAULT_SUFFIX, topology_st.logcap.log, args)
371 check_value_in_log_and_reset(topology_st, content_list=json_content)
372
373
374 @pytest.mark.skipif(ds_is_older("1.4.2"), reason="Not implemented")
375 def test_dsidm_account_modify_by_dn(topology_st, create_test_user):
376 """ Test dsidm account modify-by-dn
377
378 :id: e7288f8c-f0a8-4d8d-a00f-1b243eb117bc
379 :setup: Standalone instance
380 :steps:
381 1. Run dsidm account modify-by-dn add description value
382 2. Run dsidm account modify-by-dn replace description value
383 3. Run dsidm account modify-by-dn delete description value
384 :expectedresults:
385 1. A description value is added
386 2. The original description value is replaced and the previous is not present
387 3. The replaced description value is deleted
388 """
389
390 standalone = topology_st.standalone
391 accounts = nsUserAccounts(standalone, DEFAULT_SUFFIX)
392 test_account = accounts.get(test_user_name)
393 output = 'Successfully modified {}'.format(test_account.dn)
394
395 args = FakeArgs()
396 args.dn = test_account.dn
397 args.changes = ['add:description:new_description']
398 args.user_type = 'posix'
399
400 log.info('Test dsidm account modify add')
401 modify(standalone, DEFAULT_SUFFIX, topology_st.logcap.log, args, warn=False)
402 check_value_in_log_and_reset(topology_st, check_value=output)
403 assert test_account.present('description', 'new_description')
404
405 log.info('Test dsidm account modify replace')
406 args.changes = ['replace:description:replaced_description']
407 modify(standalone, DEFAULT_SUFFIX, topology_st.logcap.log, args, warn=False)
408 check_value_in_log_and_reset(topology_st, check_value=output)
409 assert test_account.present('description', 'replaced_description')
410 assert not test_account.present('description', 'new_description')
411
412 log.info('Test dsidm account modify delete')
413 args.changes = ['delete:description:replaced_description']
414 modify(standalone, DEFAULT_SUFFIX, topology_st.logcap.log, args, warn=False)
415 check_value_in_log_and_reset(topology_st, check_value=output)
416 assert not test_account.present('description', 'replaced_description')
417
418
419 @pytest.mark.skipif(ds_is_older("1.4.2"), reason="Not implemented")
420 def test_dsidm_account_rename_by_dn(topology_st, create_test_user):
421 """ Test dsidm account rename-by-dn option
422
423 :id: f4b8e491-35b1-4113-b9c4-e0a80f8985f3
424 :setup: Standalone instance
425 :steps:
426 1. Run dsidm account rename option on existing account
427 2. Check the account does not have another uid attribute with the old rdn
428 3. Check the old account is deleted
429 :expectedresults:
430 1. Success
431 2. Success
432 3. Success
433 """
434
435 standalone = topology_st.standalone
436 accounts = nsUserAccounts(standalone, DEFAULT_SUFFIX)
437 test_account = accounts.get(test_user_name)
438
439 args = FakeArgs()
440 args.dn = test_account.dn
441 args.new_name = 'renamed_account'
442 args.new_dn = 'uid=renamed_account,ou=people,{}'.format(DEFAULT_SUFFIX)
443 args.keep_old_rdn = False
444 args.user_type = 'posix'
445
446 log.info('Test dsidm account rename-by-dn')
447 rename(standalone, DEFAULT_SUFFIX, topology_st.logcap.log, args)
448 new_account = accounts.get(args.new_name)
449
450 try:
451 output = 'Successfully renamed to {}'.format(new_account.dn)
452 check_value_in_log_and_reset(topology_st, check_value=output)
453
454 log.info('Verify the new account does not have a uid attribute with the old rdn')
455 assert not new_account.present('uid', test_user_name)
456 assert new_account.present('displayName', test_user_name)
457
458 log.info('Verify the old account does not exist')
459 assert not test_account.exists()
460 finally:
461 log.info('Clean up')
462 new_account.delete()
463
464
465 @pytest.mark.skipif(ds_is_older("1.4.2"), reason="Not implemented")
466 def test_dsidm_account_rename_by_dn_keep_old_rdn(topology_st, create_test_user):
467 """ Test dsidm account rename-by-dn option with keep-old-rdn
468
469 :id: a128bdbb-c0a4-4d9d-9a95-9be2d3780094
470 :setup: Standalone instance
471 :steps:
472 1. Run dsidm account rename option on existing account
473 2. Check the account has another uid attribute with the old rdn
474 3. Check the old account is deleted
475 :expectedresults:
476 1. Success
477 2. Success
478 3. Success
479 """
480
481 standalone = topology_st.standalone
482 accounts = nsUserAccounts(standalone, DEFAULT_SUFFIX)
483 test_account = accounts.get(test_user_name)
484
485 args = FakeArgs()
486 args.dn = test_account.dn
487 args.new_name = 'renamed_account'
488 args.new_dn = 'uid=renamed_account,ou=people,{}'.format(DEFAULT_SUFFIX)
489 args.keep_old_rdn = True
490 args.user_type = 'posix'
491
492 log.info('Test dsidm account rename-by-dn')
493 rename(standalone, DEFAULT_SUFFIX, topology_st.logcap.log, args)
494 new_account = accounts.get(args.new_name)
495
496 try:
497 output = 'Successfully renamed to {}'.format(new_account.dn)
498 check_value_in_log_and_reset(topology_st, check_value=output)
499
500 log.info('Verify the new account does not have a uid attribute with the old rdn')
501 assert new_account.present('uid', test_user_name)
502 assert new_account.present('displayName', test_user_name)
503
504 log.info('Verify the old account does not exist')
505 assert not test_account.exists()
506 finally:
507 log.info('Clean up')
508 new_account.delete()
509
510
511 @pytest.mark.skipif(ds_is_older("1.4.2"), reason="Not implemented")
512 def test_dsidm_account_reset_password(topology_st, create_test_user):
513 """ Test dsidm account reset_password option
514
515 :id: 02ffa044-08ae-40c5-9108-b02d0c3b0521
516 :setup: Standalone instance
517 :steps:
518 1. Run dsidm account reset_password on an existing user
519 2. Verify that the user has now userPassword attribute set
520 :expectedresults:
521 1. Success
522 2. Success
523 """
524
525 standalone = topology_st.standalone
526 accounts = nsUserAccounts(standalone, DEFAULT_SUFFIX)
527 test_account = accounts.get(test_user_name)
528
529 args = FakeArgs()
530 args.dn = test_account.dn
531 args.new_password = 'newpasswd'
532 output = 'reset password for {}'.format(test_account.dn)
533 args.user_type = 'posix'
534
535 log.info('Test dsidm account reset_password')
536 reset_password(standalone, DEFAULT_SUFFIX, topology_st.logcap.log, args)
537 check_value_in_log_and_reset(topology_st, check_value=output)
538
539 log.info('Verify the userPassword attribute is set')
540 assert test_account.present('userPassword')
541
542
543 @pytest.mark.skipif(ds_is_older("1.4.2"), reason="Not implemented")
544 def test_dsidm_account_change_password(topology_st, create_test_user):
545 """ Test dsidm account change_password option
546
547 :id: 24c25b8f-df2b-4d43-a88e-47e24bc4ff36
548 :setup: Standalone instance
549 :steps:
550 1. Run dsidm account change_password on an existing user
551 2. Verify that the user has userPassword attribute set
552 :expectedresults:
553 1. Success
554 2. Success
555 """
556
557 standalone = topology_st.standalone
558 accounts = nsUserAccounts(standalone, DEFAULT_SUFFIX)
559 test_account = accounts.get(test_user_name)
560
561 args = FakeArgs()
562 args.dn = test_account.dn
563 args.new_password = 'newpasswd'
564 output = 'changed password for {}'.format(test_account.dn)
565 args.user_type = 'posix'
566
567 log.info('Test dsidm account change_password')
568 change_password(standalone, DEFAULT_SUFFIX, topology_st.logcap.log, args)
569 check_value_in_log_and_reset(topology_st, check_value=output)
570
571 log.info('Verify the userPassword attribute is set')
572 assert test_account.present('userPassword')
573
574
575 if __name__ == '__main__':
576 # Run isolated
577 # -s for DEBUG mode
578 CURRENT_FILE = os.path.realpath(__file__)
579 pytest.main("-s {}".format(CURRENT_FILE))