password_test.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. def fin():
  46. standalone.delete()
  47. request.addfinalizer(fin)
  48. return TopologyStandalone(standalone)
  49. def test_password_init(topology):
  50. '''
  51. Do init, if necessary
  52. '''
  53. return
  54. def test_password_delete_specific_password(topology):
  55. '''
  56. Delete a specific userpassword, and make sure it is actually deleted from the entry
  57. '''
  58. log.info('Running test_password_delete_specific_password...')
  59. USER_DN = 'uid=test_entry,' + DEFAULT_SUFFIX
  60. #
  61. # Add a test user with a password
  62. #
  63. try:
  64. topology.standalone.add_s(Entry((USER_DN, {'objectclass': "top extensibleObject".split(),
  65. 'sn': '1',
  66. 'cn': 'user 1',
  67. 'uid': 'user1',
  68. 'userpassword': PASSWORD})))
  69. except ldap.LDAPError as e:
  70. log.fatal('test_password_delete_specific_password: Failed to add test user ' +
  71. USER_DN + ': error ' + e.message['desc'])
  72. assert False
  73. #
  74. # Delete the exact password
  75. #
  76. try:
  77. topology.standalone.modify_s(USER_DN, [(ldap.MOD_DELETE, 'userpassword', PASSWORD)])
  78. except ldap.LDAPError as e:
  79. log.fatal('test_password_delete_specific_password: Failed to delete userpassword: error ' +
  80. e.message['desc'])
  81. assert False
  82. #
  83. # Check the password is actually deleted
  84. #
  85. try:
  86. entry = topology.standalone.search_s(USER_DN, ldap.SCOPE_BASE, 'objectclass=top')
  87. if entry[0].hasAttr('userpassword'):
  88. log.fatal('test_password_delete_specific_password: Entry incorrectly still have the userpassword attribute')
  89. assert False
  90. except ldap.LDAPError as e:
  91. log.fatal('test_password_delete_specific_password: Failed to search for user(%s), error: %s' %
  92. (USER_DN, e.message('desc')))
  93. assert False
  94. #
  95. # Cleanup
  96. #
  97. try:
  98. topology.standalone.delete_s(USER_DN)
  99. except ldap.LDAPError as e:
  100. log.fatal('test_password_delete_specific_password: Failed to delete user(%s), error: %s' %
  101. (USER_DN, e.message('desc')))
  102. assert False
  103. log.info('test_password_delete_specific_password: PASSED')
  104. if __name__ == '__main__':
  105. # Run isolated
  106. # -s for DEBUG mode
  107. CURRENT_FILE = os.path.realpath(__file__)
  108. pytest.main("-s %s" % CURRENT_FILE)