ticket48369_test.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. import os
  2. import time
  3. import ldap
  4. import logging
  5. import pytest
  6. from lib389 import DirSrv, Entry
  7. from lib389._constants import *
  8. from lib389.properties import *
  9. from lib389.tasks import *
  10. from lib389.utils import *
  11. from ldap.controls.ppolicy import PasswordPolicyControl
  12. logging.getLogger(__name__).setLevel(logging.DEBUG)
  13. log = logging.getLogger(__name__)
  14. installation1_prefix = None
  15. class TopologyStandalone(object):
  16. def __init__(self, standalone):
  17. standalone.open()
  18. self.standalone = standalone
  19. @pytest.fixture(scope="module")
  20. def topology(request):
  21. global installation1_prefix
  22. if installation1_prefix:
  23. args_instance[SER_DEPLOYED_DIR] = installation1_prefix
  24. # Creating standalone instance ...
  25. standalone = DirSrv(verbose=False)
  26. args_instance[SER_HOST] = HOST_STANDALONE
  27. args_instance[SER_PORT] = PORT_STANDALONE
  28. args_instance[SER_SERVERID_PROP] = SERVERID_STANDALONE
  29. args_instance[SER_CREATION_SUFFIX] = DEFAULT_SUFFIX
  30. args_standalone = args_instance.copy()
  31. standalone.allocate(args_standalone)
  32. instance_standalone = standalone.exists()
  33. if instance_standalone:
  34. standalone.delete()
  35. standalone.create()
  36. standalone.open()
  37. # Delete each instance in the end
  38. def fin():
  39. standalone.delete()
  40. request.addfinalizer(fin)
  41. # Clear out the tmp dir
  42. standalone.clearTmpDir(__file__)
  43. return TopologyStandalone(standalone)
  44. def test_ticket48369(topology):
  45. """
  46. Test RFE 48369 - return password policy controls by default without needing
  47. to be requested.
  48. """
  49. DN = 'uid=test,' + DEFAULT_SUFFIX
  50. #
  51. # Setup password policy
  52. #
  53. try:
  54. topology.standalone.modify_s('cn=config', [(ldap.MOD_REPLACE,
  55. 'passwordExp',
  56. 'on'),
  57. (ldap.MOD_REPLACE,
  58. 'passwordMaxAge',
  59. '864000'),
  60. (ldap.MOD_REPLACE,
  61. 'passwordSendExpiringTime',
  62. 'on')])
  63. except ldap.LDAPError as e:
  64. log.fatal('Failed to set config: %s' % str(e))
  65. assert False
  66. #
  67. # Add entry
  68. #
  69. try:
  70. topology.standalone.add_s(Entry((DN,
  71. {'objectclass': 'top extensibleObject'.split(),
  72. 'uid': 'test',
  73. 'userpassword': 'password'})))
  74. except ldap.LDAPError as e:
  75. log.fatal('Failed to add user entry: %s' % str(e))
  76. assert False
  77. time.sleep(1)
  78. #
  79. # Bind as the new user, and request the control
  80. #
  81. try:
  82. msgid = topology.standalone.simple_bind(DN, "password",
  83. serverctrls=[PasswordPolicyControl()])
  84. res_type, res_data, res_msgid, res_ctrls = \
  85. topology.standalone.result3(msgid)
  86. except ldap.LDAPError as e:
  87. log.fatal('Failed to bind: %s: Error %s' % (ctl_resp, str(e)))
  88. assert False
  89. if res_ctrls[0].controlType == PasswordPolicyControl.controlType:
  90. ppolicy_ctrl = res_ctrls[0]
  91. else:
  92. log.fatal('Control not found')
  93. assert False
  94. log.info('Time until expiration (%s)' %
  95. repr(ppolicy_ctrl.timeBeforeExpiration))
  96. log.info('Test complete')
  97. if __name__ == '__main__':
  98. # Run isolated
  99. # -s for DEBUG mode
  100. CURRENT_FILE = os.path.realpath(__file__)
  101. pytest.main("-s %s" % CURRENT_FILE)