Debian Codegraph @

dirsrvtests/tests/suites/password/pwdPolicy_controls_sequence_test.py

raw
1 # --- BEGIN COPYRIGHT BLOCK ---
2 # Copyright (C) 2021 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 pytest
12 import os
13 import ldap
14 import time
15 import ast
16
17 from ldap.controls.ppolicy import PasswordPolicyControl
18 from ldap.controls.pwdpolicy import PasswordExpiredControl
19 from test389.topologies import topology_st as topo
20 from lib389.idm.user import UserAccounts
21 from lib389._constants import (DN_DM, PASSWORD, DEFAULT_SUFFIX)
22
23 pytestmark = pytest.mark.tier1
24
25 DEBUGGING = os.getenv("DEBUGGING", default=False)
26 if DEBUGGING:
27 logging.getLogger(__name__).setLevel(logging.DEBUG)
28 else:
29 logging.getLogger(__name__).setLevel(logging.INFO)
30 log = logging.getLogger(__name__)
31
32
33 USER_DN = 'uid=test entry,ou=people,dc=example,dc=com'
34 USER_PW = b'password123'
35
36
37 @pytest.fixture
38 def init_user(topo, request):
39 users = UserAccounts(topo.standalone, DEFAULT_SUFFIX)
40 user_data = {'uid': 'test entry',
41 'cn': 'test entry',
42 'sn': 'test entry',
43 'uidNumber': '3000',
44 'gidNumber': '4000',
45 'homeDirectory': '/home/test_entry',
46 'userPassword': USER_PW}
47 test_user = users.create(properties=user_data)
48
49 def fin():
50 log.info('Delete test user')
51 if test_user.exists():
52 test_user.delete()
53
54 request.addfinalizer(fin)
55
56
57 def bind_and_get_control(topo):
58 log.info('Bind as the user, and return any controls')
59 res_type = res_data = res_msgid = res_ctrls = None
60 result_id = ''
61
62 try:
63 result_id = topo.standalone.simple_bind(USER_DN, USER_PW,
64 serverctrls=[PasswordPolicyControl()])
65 res_type, res_data, res_msgid, res_ctrls = topo.standalone.result3(result_id)
66 except ldap.LDAPError as e:
67 log.info('Got expected error: {}'.format(str(e)))
68 res_ctrls = ast.literal_eval(str(e))
69 pass
70
71 topo.standalone.simple_bind(DN_DM, PASSWORD)
72 return res_ctrls
73
74
75 def change_passwd(topo):
76 log.info('Reset user password as the user, then re-bind as Directory Manager')
77 users = UserAccounts(topo.standalone, DEFAULT_SUFFIX)
78 user = users.get('test entry')
79 user.rebind(USER_PW)
80 user.reset_password(USER_PW)
81 topo.standalone.simple_bind(DN_DM, PASSWORD)
82
83
84 def test_controltype_expired_grace_limit(topo, init_user):
85 """Test for expiration control when password is expired with available and exhausted grace login
86
87 :id: 0392a73c-6467-49f9-bdb6-3648f6971896
88 :setup: Standalone instance, a user for testing
89 :steps:
90 1. Configure password policy, reset password and allow it to expire
91 2. Bind and check sequence of controlType
92 3. Bind (one grace login remaining) and check sequence of controlType
93 4. Bind (grace login exhausted) and check sequence of controlType
94 :expectedresults:
95 1. Config update and password reset are successful
96 2. ControlType sequence is in correct order
97 3. ControlType sequence is in correct order
98 4. ControlType sequence is in correct order
99 """
100
101 log.info('Configure password policy with grace limit set to 2')
102 topo.standalone.config.set('passwordExp', 'on')
103 topo.standalone.config.set('passwordMaxAge', '5')
104 topo.standalone.config.set('passwordGraceLimit', '2')
105
106 log.info('Change password and wait for it to expire')
107 change_passwd(topo)
108 time.sleep(6)
109
110 log.info('Bind and use up one grace login (only one left)')
111 controls = bind_and_get_control(topo)
112 assert (controls[0].controlType == "1.3.6.1.4.1.42.2.27.8.5.1")
113 assert (controls[1].controlType == "2.16.840.1.113730.3.4.4")
114
115 log.info('Bind again and check the sequence')
116 controls = bind_and_get_control(topo)
117 assert (controls[0].controlType == "1.3.6.1.4.1.42.2.27.8.5.1")
118 assert (controls[1].controlType == "2.16.840.1.113730.3.4.4")
119
120 log.info('Bind with expired grace login and check the sequence')
121 # No grace login available, bind should fail, controls will be returned in error message
122 controls = bind_and_get_control(topo)
123 assert (controls['ctrls'][0][0] == "1.3.6.1.4.1.42.2.27.8.5.1")
124 assert (controls['ctrls'][1][0] == "2.16.840.1.113730.3.4.4")
125
126
127 if __name__ == '__main__':
128 # Run isolated
129 # -s for DEBUG mode
130 CURRENT_FILE = os.path.realpath(__file__)
131 pytest.main("-s %s" % CURRENT_FILE)