ticket47714_test.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. import os
  2. import sys
  3. import time
  4. import ldap
  5. import logging
  6. import pytest
  7. import shutil
  8. from lib389 import DirSrv, Entry, tools
  9. from lib389.tools import DirSrvTools
  10. from lib389._constants import *
  11. from lib389.properties import *
  12. log = logging.getLogger(__name__)
  13. installation_prefix = None
  14. ACCT_POLICY_CONFIG_DN = 'cn=config,cn=%s,cn=plugins,cn=config' % PLUGIN_ACCT_POLICY
  15. ACCT_POLICY_DN = 'cn=Account Inactivation Pplicy,%s' % SUFFIX
  16. INACTIVITY_LIMIT = '9'
  17. SEARCHFILTER = '(objectclass=*)'
  18. TEST_USER = 'ticket47714user'
  19. TEST_USER_DN = 'uid=%s,%s' % (TEST_USER, SUFFIX)
  20. TEST_USER_PW = '%s' % TEST_USER
  21. class TopologyStandalone(object):
  22. def __init__(self, standalone):
  23. standalone.open()
  24. self.standalone = standalone
  25. @pytest.fixture(scope="module")
  26. def topology(request):
  27. '''
  28. This fixture is used to standalone topology for the 'module'.
  29. '''
  30. global installation_prefix
  31. if installation_prefix:
  32. args_instance[SER_DEPLOYED_DIR] = installation_prefix
  33. standalone = DirSrv(verbose=False)
  34. # Args for the standalone instance
  35. args_instance[SER_HOST] = HOST_STANDALONE
  36. args_instance[SER_PORT] = PORT_STANDALONE
  37. args_instance[SER_SERVERID_PROP] = SERVERID_STANDALONE
  38. args_standalone = args_instance.copy()
  39. standalone.allocate(args_standalone)
  40. # Get the status of the instance and restart it if it exists
  41. instance_standalone = standalone.exists()
  42. # Remove the instance
  43. if instance_standalone:
  44. standalone.delete()
  45. # Create the instance
  46. standalone.create()
  47. # Used to retrieve configuration information (dbdir, confdir...)
  48. standalone.open()
  49. # clear the tmp directory
  50. standalone.clearTmpDir(__file__)
  51. # Here we have standalone instance up and running
  52. return TopologyStandalone(standalone)
  53. def _header(topology, label):
  54. topology.standalone.log.info("\n\n###############################################")
  55. topology.standalone.log.info("#######")
  56. topology.standalone.log.info("####### %s" % label)
  57. topology.standalone.log.info("#######")
  58. topology.standalone.log.info("###############################################")
  59. def test_ticket47714_init(topology):
  60. """
  61. 1. Add account policy entry to the DB
  62. 2. Add a test user to the DB
  63. """
  64. _header(topology, 'Testing Ticket 47714 - [RFE] Update lastLoginTime also in Account Policy plugin if account lockout is based on passwordExpirationTime.')
  65. topology.standalone.simple_bind_s(DN_DM, PASSWORD)
  66. log.info("\n######################### Adding Account Policy entry: %s ######################\n" % ACCT_POLICY_DN)
  67. topology.standalone.add_s(Entry((ACCT_POLICY_DN, {'objectclass': "top ldapsubentry extensibleObject accountpolicy".split(),
  68. 'accountInactivityLimit': INACTIVITY_LIMIT})))
  69. log.info("\n######################### Adding Test User entry: %s ######################\n" % TEST_USER_DN)
  70. topology.standalone.add_s(Entry((TEST_USER_DN, {'objectclass': "top person organizationalPerson inetOrgPerson".split(),
  71. 'cn': TEST_USER,
  72. 'sn': TEST_USER,
  73. 'givenname': TEST_USER,
  74. 'userPassword': TEST_USER_PW,
  75. 'acctPolicySubentry': ACCT_POLICY_DN})))
  76. def test_ticket47714_run_0(topology):
  77. """
  78. Check this change has no inpact to the existing functionality.
  79. 1. Set account policy config without the new attr alwaysRecordLoginAttr
  80. 2. Bind as a test user
  81. 3. Bind as the test user again and check the lastLoginTime is updated
  82. 4. Waint longer than the accountInactivityLimit time and bind as the test user,
  83. which should fail with CONSTANT_VIOLATION.
  84. """
  85. _header(topology, 'Account Policy - No new attr alwaysRecordLoginAttr in config')
  86. topology.standalone.simple_bind_s(DN_DM, PASSWORD)
  87. # Modify Account Policy config entry
  88. topology.standalone.modify_s(ACCT_POLICY_CONFIG_DN, [(ldap.MOD_REPLACE, 'alwaysrecordlogin', 'yes'),
  89. (ldap.MOD_REPLACE, 'stateattrname', 'lastLoginTime'),
  90. (ldap.MOD_REPLACE, 'altstateattrname', 'createTimestamp'),
  91. (ldap.MOD_REPLACE, 'specattrname', 'acctPolicySubentry'),
  92. (ldap.MOD_REPLACE, 'limitattrname', 'accountInactivityLimit')])
  93. # Enable the plugins
  94. topology.standalone.plugins.enable(name=PLUGIN_ACCT_POLICY)
  95. topology.standalone.restart(timeout=120)
  96. log.info("\n######################### Bind as %s ######################\n" % TEST_USER_DN)
  97. try:
  98. topology.standalone.simple_bind_s(TEST_USER_DN, TEST_USER_PW)
  99. except ldap.CONSTRAINT_VIOLATION, e:
  100. log.error('CONSTRAINT VIOLATION ' + e.message['desc'])
  101. topology.standalone.simple_bind_s(DN_DM, PASSWORD)
  102. entry = topology.standalone.search_s(TEST_USER_DN, ldap.SCOPE_BASE, SEARCHFILTER, ['lastLoginTime'])
  103. lastLoginTime0 = entry[0].lastLoginTime
  104. time.sleep(2)
  105. log.info("\n######################### Bind as %s again ######################\n" % TEST_USER_DN)
  106. try:
  107. topology.standalone.simple_bind_s(TEST_USER_DN, TEST_USER_PW)
  108. except ldap.CONSTRAINT_VIOLATION, e:
  109. log.error('CONSTRAINT VIOLATION ' + e.message['desc'])
  110. topology.standalone.simple_bind_s(DN_DM, PASSWORD)
  111. entry = topology.standalone.search_s(TEST_USER_DN, ldap.SCOPE_BASE, SEARCHFILTER, ['lastLoginTime'])
  112. lastLoginTime1 = entry[0].lastLoginTime
  113. log.info("First lastLoginTime: %s, Second lastLoginTime: %s" % (lastLoginTime0, lastLoginTime1))
  114. assert lastLoginTime0 < lastLoginTime1
  115. topology.standalone.simple_bind_s(DN_DM, PASSWORD)
  116. entry = topology.standalone.search_s(ACCT_POLICY_DN, ldap.SCOPE_BASE, SEARCHFILTER)
  117. log.info("\n######################### %s ######################\n" % ACCT_POLICY_CONFIG_DN)
  118. log.info("accountInactivityLimit: %s" % entry[0].accountInactivityLimit)
  119. log.info("\n######################### %s DONE ######################\n" % ACCT_POLICY_CONFIG_DN)
  120. time.sleep(10)
  121. log.info("\n######################### Bind as %s again to fail ######################\n" % TEST_USER_DN)
  122. try:
  123. topology.standalone.simple_bind_s(TEST_USER_DN, TEST_USER_PW)
  124. except ldap.CONSTRAINT_VIOLATION, e:
  125. log.info('CONSTRAINT VIOLATION ' + e.message['desc'])
  126. log.info("%s was successfully inactivated." % TEST_USER_DN)
  127. pass
  128. def test_ticket47714_run_1(topology):
  129. """
  130. Verify a new config attr alwaysRecordLoginAttr
  131. 1. Set account policy config with the new attr alwaysRecordLoginAttr: lastLoginTime
  132. Note: bogus attr is set to stateattrname.
  133. altstateattrname type value is used for checking whether the account is idle or not.
  134. 2. Bind as a test user
  135. 3. Bind as the test user again and check the alwaysRecordLoginAttr: lastLoginTime is updated
  136. """
  137. _header(topology, 'Account Policy - With new attr alwaysRecordLoginAttr in config')
  138. topology.standalone.simple_bind_s(DN_DM, PASSWORD)
  139. topology.standalone.modify_s(TEST_USER_DN, [(ldap.MOD_DELETE, 'lastLoginTime', None)])
  140. # Modify Account Policy config entry
  141. topology.standalone.modify_s(ACCT_POLICY_CONFIG_DN, [(ldap.MOD_REPLACE, 'alwaysrecordlogin', 'yes'),
  142. (ldap.MOD_REPLACE, 'stateattrname', 'bogus'),
  143. (ldap.MOD_REPLACE, 'altstateattrname', 'modifyTimestamp'),
  144. (ldap.MOD_REPLACE, 'alwaysRecordLoginAttr', 'lastLoginTime'),
  145. (ldap.MOD_REPLACE, 'specattrname', 'acctPolicySubentry'),
  146. (ldap.MOD_REPLACE, 'limitattrname', 'accountInactivityLimit')])
  147. # Enable the plugins
  148. topology.standalone.plugins.enable(name=PLUGIN_ACCT_POLICY)
  149. topology.standalone.restart(timeout=120)
  150. log.info("\n######################### Bind as %s ######################\n" % TEST_USER_DN)
  151. try:
  152. topology.standalone.simple_bind_s(TEST_USER_DN, TEST_USER_PW)
  153. except ldap.CONSTRAINT_VIOLATION, e:
  154. log.error('CONSTRAINT VIOLATION ' + e.message['desc'])
  155. topology.standalone.simple_bind_s(DN_DM, PASSWORD)
  156. entry = topology.standalone.search_s(TEST_USER_DN, ldap.SCOPE_BASE, SEARCHFILTER, ['lastLoginTime'])
  157. lastLoginTime0 = entry[0].lastLoginTime
  158. time.sleep(2)
  159. log.info("\n######################### Bind as %s again ######################\n" % TEST_USER_DN)
  160. try:
  161. topology.standalone.simple_bind_s(TEST_USER_DN, TEST_USER_PW)
  162. except ldap.CONSTRAINT_VIOLATION, e:
  163. log.error('CONSTRAINT VIOLATION ' + e.message['desc'])
  164. topology.standalone.simple_bind_s(DN_DM, PASSWORD)
  165. entry = topology.standalone.search_s(TEST_USER_DN, ldap.SCOPE_BASE, SEARCHFILTER, ['lastLoginTime'])
  166. lastLoginTime1 = entry[0].lastLoginTime
  167. log.info("First lastLoginTime: %s, Second lastLoginTime: %s" % (lastLoginTime0, lastLoginTime1))
  168. assert lastLoginTime0 < lastLoginTime1
  169. topology.standalone.log.info("ticket47714 was successfully verified.")
  170. def test_ticket47714_final(topology):
  171. topology.standalone.delete()
  172. log.info('Testcase PASSED')
  173. def run_isolated():
  174. '''
  175. run_isolated is used to run these test cases independently of a test scheduler (xunit, py.test..)
  176. To run isolated without py.test, you need to
  177. - edit this file and comment '@pytest.fixture' line before 'topology' function.
  178. - set the installation prefix
  179. - run this program
  180. '''
  181. global installation_prefix
  182. installation_prefix = None
  183. topo = topology(True)
  184. test_ticket47714_init(topo)
  185. test_ticket47714_run_0(topo)
  186. test_ticket47714_run_1(topo)
  187. test_ticket47714_final(topo)
  188. if __name__ == '__main__':
  189. run_isolated()