Debian Codegraph @

dirsrvtests/tests/suites/plugins/account_policy_login_attr_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 import logging
9 import os
10 import time
11 import pytest
12 import ldap
13 from test389.topologies import topology_st
14 from lib389.idm.user import UserAccounts
15 from lib389.idm.directorymanager import DirectoryManager
16 from lib389.plugins import AccountPolicyPlugin, AccountPolicyConfig
17 from lib389._constants import DEFAULT_SUFFIX, DN_DM, PASSWORD, PLUGIN_ACCT_POLICY
18
19 pytestmark = pytest.mark.tier2
20 logging.getLogger(__name__).setLevel(logging.DEBUG)
21 log = logging.getLogger(__name__)
22
23 ACCP_CONF = f"cn=config,cn={PLUGIN_ACCT_POLICY},cn=plugins,cn=config"
24 ACCT_POLICY_DN = f'cn=Account Inactivation Policy,{DEFAULT_SUFFIX}'
25 INACTIVITY_LIMIT = '3000'
26
27 @pytest.fixture(scope="function")
28 def account_policy_setup(topology_st, request):
29 """Setup account policy plugin and test user"""
30 inst = topology_st.standalone
31
32 users = UserAccounts(inst, DEFAULT_SUFFIX)
33 policy_user = users.create(properties={
34 'uid': 'test_policy_user',
35 'cn': 'test_policy_user',
36 'sn': 'test_policy_user',
37 'givenname': 'test_policy_user',
38 'userPassword': PASSWORD,
39 'uidNumber': '5000',
40 'gidNumber': '5000',
41 'homeDirectory': '/home/test_policy_user',
42 'acctPolicySubentry': ACCT_POLICY_DN
43 })
44
45 policy_entry = AccountPolicyConfig(inst, dn=ACCT_POLICY_DN)
46 policy_entry.create(properties={
47 'cn': 'Account Inactivation Policy',
48 'accountInactivityLimit': INACTIVITY_LIMIT
49 })
50
51 plugin = AccountPolicyPlugin(inst)
52 plugin.enable()
53
54 def fin():
55 policy_user.delete()
56 policy_entry.delete()
57 plugin.disable()
58
59 request.addfinalizer(fin)
60 return inst, policy_user
61
62
63 def test_account_policy_without_always_record_login_attr(account_policy_setup):
64 """Test account policy functionality without alwaysRecordLoginAttr
65
66 :id: e8f4a3b2-1c9d-4e7f-8a6b-5d2c9e1f0a4b
67 :setup: Standalone Instance with Account Policy plugin and test user
68 :steps:
69 1. Configure account policy without alwaysRecordLoginAttr
70 2. Bind as test user
71 3. Check lastLoginTime is updated
72 4. Bind as test user again
73 5. Verify lastLoginTime is updated to a newer value
74 6. Set very low inactivity limit
75 7. Try to bind again should fail with constraint violation
76 :expectedresults:
77 1. Configuration should succeed
78 2. Bind should succeed
79 3. lastLoginTime should be present
80 4. Second bind should succeed
81 5. lastLoginTime should be newer
82 6. Inactivity limit should be set
83 7. Bind should fail due to account inactivity
84 """
85 inst, policy_user = account_policy_setup
86
87 log.info("Configure account policy without alwaysRecordLoginAttr")
88 accp = AccountPolicyConfig(inst, dn=ACCP_CONF)
89 accp.replace_many(
90 ('alwaysrecordlogin', 'yes'),
91 ('stateattrname', 'lastLoginTime'),
92 ('altstateattrname', 'createTimestamp'),
93 ('specattrname', 'acctPolicySubentry'),
94 ('limitattrname', 'accountInactivityLimit')
95 )
96
97 inst.restart()
98
99 log.info("Bind as test user")
100 policy_user.bind(PASSWORD)
101 time.sleep(1)
102
103 log.info("Check lastLoginTime was added")
104 dm = DirectoryManager(inst)
105 dm.bind(PASSWORD)
106 first_login_time = policy_user.get_attr_val_utf8('lastLoginTime')
107 assert first_login_time
108
109 log.info("Bind as test user again")
110 policy_user.bind(PASSWORD)
111 time.sleep(1)
112
113 log.info("Verify lastLoginTime was updated")
114 dm = DirectoryManager(inst)
115 dm.bind(PASSWORD)
116 second_login_time = policy_user.get_attr_val_utf8('lastLoginTime')
117 assert first_login_time < second_login_time
118
119 log.info("Change inactivity limit to trigger account lockout")
120 policy_entry = AccountPolicyConfig(inst, dn=ACCT_POLICY_DN)
121 policy_entry.replace('accountInactivityLimit', '1')
122 time.sleep(1)
123
124 log.info("Bind should fail due to account inactivity")
125 with pytest.raises(ldap.CONSTRAINT_VIOLATION):
126 policy_user.bind(PASSWORD)
127
128 policy_entry.replace('accountInactivityLimit', INACTIVITY_LIMIT)
129
130
131 def test_account_policy_with_always_record_login_attr(account_policy_setup):
132 """Test account policy with alwaysRecordLoginAttr functionality
133
134 :id: b7c2f9a1-8d6e-4c3b-9f5a-2e8d1c7a0b4f
135 :setup: Standalone Instance with Account Policy plugin and test user
136 :steps:
137 1. Configure account policy with alwaysRecordLoginAttr set to lastLoginTime
138 2. Set stateattrname to bogus value and altstateattrname to modifyTimestamp
139 3. Remove any existing lastLoginTime from user
140 4. Bind as test user
141 5. Check lastLoginTime was added via alwaysRecordLoginAttr
142 6. Bind as test user again
143 7. Verify lastLoginTime was updated to newer value
144 :expectedresults:
145 1. Configuration should succeed
146 2. Configuration should be updated
147 3. lastLoginTime should be removed
148 4. Bind should succeed
149 5. lastLoginTime should be present due to alwaysRecordLoginAttr
150 6. Second bind should succeed
151 7. lastLoginTime should be updated to newer value
152 """
153 inst, policy_user = account_policy_setup
154
155 log.info("Remove any existing lastLoginTime from user")
156 try:
157 policy_user.remove('lastLoginTime', None)
158 except ldap.NO_SUCH_ATTRIBUTE:
159 pass
160
161 log.info("Configure account policy with alwaysRecordLoginAttr")
162 accp = AccountPolicyConfig(inst, dn=ACCP_CONF)
163 accp.replace_many(
164 ('alwaysrecordlogin', 'yes'),
165 ('stateattrname', 'bogus'),
166 ('altstateattrname', 'modifyTimestamp'),
167 ('alwaysRecordLoginAttr', 'lastLoginTime'),
168 ('specattrname', 'acctPolicySubentry'),
169 ('limitattrname', 'accountInactivityLimit')
170 )
171
172 inst.restart()
173
174 log.info("Bind as test user")
175 policy_user.bind(PASSWORD)
176 time.sleep(1)
177
178 log.info("Check lastLoginTime was added via alwaysRecordLoginAttr")
179 dm = DirectoryManager(inst)
180 dm.bind(PASSWORD)
181 first_login_time = policy_user.get_attr_val_utf8('lastLoginTime')
182 assert first_login_time
183
184 log.info("Bind as test user again")
185 policy_user.bind(PASSWORD)
186 time.sleep(1)
187
188 log.info("Verify lastLoginTime was updated")
189 dm = DirectoryManager(inst)
190 dm.bind(PASSWORD)
191 second_login_time = policy_user.get_attr_val_utf8('lastLoginTime')
192 assert first_login_time < second_login_time
193
194
195 if __name__ == '__main__':
196 CURRENT_FILE = os.path.realpath(__file__)
197 pytest.main(["-s", CURRENT_FILE])