ticket48370_test.py 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. import os
  2. import ldap
  3. import logging
  4. import pytest
  5. from lib389 import DirSrv, Entry
  6. from lib389._constants import *
  7. from lib389.properties import *
  8. from lib389.tasks import *
  9. from lib389.utils import *
  10. logging.getLogger(__name__).setLevel(logging.DEBUG)
  11. log = logging.getLogger(__name__)
  12. installation1_prefix = None
  13. class TopologyStandalone(object):
  14. def __init__(self, standalone):
  15. standalone.open()
  16. self.standalone = standalone
  17. @pytest.fixture(scope="module")
  18. def topology(request):
  19. global installation1_prefix
  20. if installation1_prefix:
  21. args_instance[SER_DEPLOYED_DIR] = installation1_prefix
  22. # Creating standalone instance ...
  23. standalone = DirSrv(verbose=False)
  24. args_instance[SER_HOST] = HOST_STANDALONE
  25. args_instance[SER_PORT] = PORT_STANDALONE
  26. args_instance[SER_SERVERID_PROP] = SERVERID_STANDALONE
  27. args_instance[SER_CREATION_SUFFIX] = DEFAULT_SUFFIX
  28. args_standalone = args_instance.copy()
  29. standalone.allocate(args_standalone)
  30. instance_standalone = standalone.exists()
  31. if instance_standalone:
  32. standalone.delete()
  33. standalone.create()
  34. standalone.open()
  35. # Delete each instance in the end
  36. def fin():
  37. standalone.delete()
  38. request.addfinalizer(fin)
  39. # Clear out the tmp dir
  40. standalone.clearTmpDir(__file__)
  41. return TopologyStandalone(standalone)
  42. def test_ticket48370(topology):
  43. """
  44. Deleting attirbute values and readding a value does not properly update
  45. the pres index. The values are not actually deleted from the index
  46. """
  47. DN = 'uid=user0099,' + DEFAULT_SUFFIX
  48. #
  49. # Add an entry
  50. #
  51. topology.standalone.add_s(Entry((DN, {
  52. 'objectclass': ['top', 'person',
  53. 'organizationalPerson',
  54. 'inetorgperson',
  55. 'posixAccount'],
  56. 'givenname': 'test',
  57. 'sn': 'user',
  58. 'loginshell': '/bin/bash',
  59. 'uidNumber': '10099',
  60. 'gidNumber': '10099',
  61. 'gecos': 'Test User',
  62. 'mail': ['[email protected]',
  63. '[email protected]',
  64. '[email protected]'],
  65. 'cn': 'Test User',
  66. 'homeDirectory': '/home/user0099',
  67. 'uid': 'admin2',
  68. 'userpassword': 'password'})))
  69. #
  70. # Perform modify (delete & add mail attributes)
  71. #
  72. try:
  73. topology.standalone.modify_s(DN, [(ldap.MOD_DELETE,
  74. 'mail',
  75. '[email protected]'),
  76. (ldap.MOD_DELETE,
  77. 'mail',
  78. '[email protected]'),
  79. (ldap.MOD_ADD,
  80. 'mail', '[email protected]')])
  81. except ldap.LDAPError as e:
  82. log.fatal('Failedto modify user: ' + str(e))
  83. assert False
  84. #
  85. # Search using deleted attribute value- no entries should be returned
  86. #
  87. try:
  88. entry = topology.standalone.search_s(DEFAULT_SUFFIX,
  89. ldap.SCOPE_SUBTREE,
  90. '[email protected]')
  91. if entry:
  92. log.fatal('Entry incorrectly returned')
  93. assert False
  94. except ldap.LDAPError as e:
  95. log.fatal('Failed to search for user: ' + str(e))
  96. assert False
  97. #
  98. # Search using existing attribute value - the entry should be returned
  99. #
  100. try:
  101. entry = topology.standalone.search_s(DEFAULT_SUFFIX,
  102. ldap.SCOPE_SUBTREE,
  103. '[email protected]')
  104. if entry is None:
  105. log.fatal('Entry not found, but it should have been')
  106. assert False
  107. except ldap.LDAPError as e:
  108. log.fatal('Failed to search for user: ' + str(e))
  109. assert False
  110. #
  111. # Delete the last values
  112. #
  113. try:
  114. topology.standalone.modify_s(DN, [(ldap.MOD_DELETE,
  115. 'mail',
  116. '[email protected]'),
  117. (ldap.MOD_DELETE,
  118. 'mail',
  119. '[email protected]')
  120. ])
  121. except ldap.LDAPError as e:
  122. log.fatal('Failed to modify user: ' + str(e))
  123. assert False
  124. #
  125. # Search using deleted attribute value - no entries should be returned
  126. #
  127. try:
  128. entry = topology.standalone.search_s(DEFAULT_SUFFIX,
  129. ldap.SCOPE_SUBTREE,
  130. '[email protected]')
  131. if entry:
  132. log.fatal('Entry incorrectly returned')
  133. assert False
  134. except ldap.LDAPError as e:
  135. log.fatal('Failed to search for user: ' + str(e))
  136. assert False
  137. #
  138. # Make sure presence index is correctly updated - no entries should be
  139. # returned
  140. #
  141. try:
  142. entry = topology.standalone.search_s(DEFAULT_SUFFIX,
  143. ldap.SCOPE_SUBTREE,
  144. 'mail=*')
  145. if entry:
  146. log.fatal('Entry incorrectly returned')
  147. assert False
  148. except ldap.LDAPError as e:
  149. log.fatal('Failed to search for user: ' + str(e))
  150. assert False
  151. #
  152. # Now add the attributes back, and lets run a different set of tests with
  153. # a different number of attributes
  154. #
  155. try:
  156. topology.standalone.modify_s(DN, [(ldap.MOD_ADD,
  157. 'mail',
  158. ['[email protected]',
  159. '[email protected]'])])
  160. except ldap.LDAPError as e:
  161. log.fatal('Failedto modify user: ' + str(e))
  162. assert False
  163. #
  164. # Remove and readd some attibutes
  165. #
  166. try:
  167. topology.standalone.modify_s(DN, [(ldap.MOD_DELETE,
  168. 'mail',
  169. '[email protected]'),
  170. (ldap.MOD_DELETE,
  171. 'mail',
  172. '[email protected]'),
  173. (ldap.MOD_ADD,
  174. 'mail', '[email protected]')])
  175. except ldap.LDAPError as e:
  176. log.fatal('Failedto modify user: ' + str(e))
  177. assert False
  178. #
  179. # Search using deleted attribute value - no entries should be returned
  180. #
  181. try:
  182. entry = topology.standalone.search_s(DEFAULT_SUFFIX,
  183. ldap.SCOPE_SUBTREE,
  184. '[email protected]')
  185. if entry:
  186. log.fatal('Entry incorrectly returned')
  187. assert False
  188. except ldap.LDAPError as e:
  189. log.fatal('Failed to search for user: ' + str(e))
  190. assert False
  191. #
  192. # Search using existing attribute value - the entry should be returned
  193. #
  194. try:
  195. entry = topology.standalone.search_s(DEFAULT_SUFFIX,
  196. ldap.SCOPE_SUBTREE,
  197. '[email protected]')
  198. if entry is None:
  199. log.fatal('Entry not found, but it should have been')
  200. assert False
  201. except ldap.LDAPError as e:
  202. log.fatal('Failed to search for user: ' + str(e))
  203. assert False
  204. log.info('Test PASSED')
  205. if __name__ == '__main__':
  206. # Run isolated
  207. # -s for DEBUG mode
  208. CURRENT_FILE = os.path.realpath(__file__)
  209. pytest.main("-s %s" % CURRENT_FILE)