password_test.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. # --- BEGIN COPYRIGHT BLOCK ---
  2. # Copyright (C) 2015 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. import os
  10. import sys
  11. import time
  12. import ldap
  13. import logging
  14. import pytest
  15. from lib389 import DirSrv, Entry, tools, tasks
  16. from lib389.tools import DirSrvTools
  17. from lib389._constants import *
  18. from lib389.properties import *
  19. from lib389.tasks import *
  20. logging.getLogger(__name__).setLevel(logging.DEBUG)
  21. log = logging.getLogger(__name__)
  22. installation1_prefix = None
  23. class TopologyStandalone(object):
  24. def __init__(self, standalone):
  25. standalone.open()
  26. self.standalone = standalone
  27. @pytest.fixture(scope="module")
  28. def topology(request):
  29. global installation1_prefix
  30. if installation1_prefix:
  31. args_instance[SER_DEPLOYED_DIR] = installation1_prefix
  32. # Creating standalone instance ...
  33. standalone = DirSrv(verbose=False)
  34. args_instance[SER_HOST] = HOST_STANDALONE
  35. args_instance[SER_PORT] = PORT_STANDALONE
  36. args_instance[SER_SERVERID_PROP] = SERVERID_STANDALONE
  37. args_instance[SER_CREATION_SUFFIX] = DEFAULT_SUFFIX
  38. args_standalone = args_instance.copy()
  39. standalone.allocate(args_standalone)
  40. instance_standalone = standalone.exists()
  41. if instance_standalone:
  42. standalone.delete()
  43. standalone.create()
  44. standalone.open()
  45. # Clear out the tmp dir
  46. standalone.clearTmpDir(__file__)
  47. return TopologyStandalone(standalone)
  48. def test_password_init(topology):
  49. '''
  50. Do init, if necessary
  51. '''
  52. return
  53. def test_password_delete_specific_password(topology):
  54. '''
  55. Delete a specific userpassword, and make sure it is actually deleted from the entry
  56. '''
  57. log.info('Running test_password_delete_specific_password...')
  58. USER_DN = 'uid=test_entry,' + DEFAULT_SUFFIX
  59. #
  60. # Add a test user with a password
  61. #
  62. try:
  63. topology.standalone.add_s(Entry((USER_DN, {'objectclass': "top extensibleObject".split(),
  64. 'sn': '1',
  65. 'cn': 'user 1',
  66. 'uid': 'user1',
  67. 'userpassword': PASSWORD})))
  68. except ldap.LDAPError as e:
  69. log.fatal('test_password_delete_specific_password: Failed to add test user ' +
  70. USER_DN + ': error ' + e.message['desc'])
  71. assert False
  72. #
  73. # Delete the exact password
  74. #
  75. try:
  76. topology.standalone.modify_s(USER_DN, [(ldap.MOD_DELETE, 'userpassword', PASSWORD)])
  77. except ldap.LDAPError as e:
  78. log.fatal('test_password_delete_specific_password: Failed to delete userpassword: error ' +
  79. e.message['desc'])
  80. assert False
  81. #
  82. # Check the password is actually deleted
  83. #
  84. try:
  85. entry = topology.standalone.search_s(USER_DN, ldap.SCOPE_BASE, 'objectclass=top')
  86. if entry[0].hasAttr('userpassword'):
  87. log.fatal('test_password_delete_specific_password: Entry incorrectly still have the userpassword attribute')
  88. assert False
  89. except ldap.LDAPError as e:
  90. log.fatal('test_password_delete_specific_password: Failed to search for user(%s), error: %s' %
  91. (USER_DN, e.message('desc')))
  92. assert False
  93. #
  94. # Cleanup
  95. #
  96. try:
  97. topology.standalone.delete_s(USER_DN)
  98. except ldap.LDAPError as e:
  99. log.fatal('test_password_delete_specific_password: Failed to delete user(%s), error: %s' %
  100. (USER_DN, e.message('desc')))
  101. assert False
  102. log.info('test_password_delete_specific_password: PASSED')
  103. def test_password_final(topology):
  104. topology.standalone.delete()
  105. log.info('Password test suite PASSED')
  106. def run_isolated():
  107. global installation1_prefix
  108. installation1_prefix = None
  109. topo = topology(True)
  110. test_password_init(topo)
  111. test_password_delete_specific_password(topo)
  112. test_password_final(topo)
  113. if __name__ == '__main__':
  114. run_isolated()