attr_uniqueness_test.py 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. # --- BEGIN COPYRIGHT BLOCK ---
  2. # Copyright (C) 2016 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 time
  10. import ldap
  11. import logging
  12. import pytest
  13. from lib389 import DirSrv, Entry
  14. from lib389._constants import *
  15. from lib389.properties import *
  16. from lib389.tasks import *
  17. from lib389.utils import *
  18. logging.getLogger(__name__).setLevel(logging.DEBUG)
  19. log = logging.getLogger(__name__)
  20. USER1_DN = 'uid=user1,' + DEFAULT_SUFFIX
  21. USER2_DN = 'uid=user2,' + DEFAULT_SUFFIX
  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. def fin():
  48. standalone.delete()
  49. request.addfinalizer(fin)
  50. return TopologyStandalone(standalone)
  51. def test_attr_uniqueness_init(topology):
  52. '''
  53. Enable dynamic plugins - makes things easier
  54. '''
  55. try:
  56. topology.standalone.modify_s(DN_CONFIG, [(ldap.MOD_REPLACE, 'nsslapd-dynamic-plugins', 'on')])
  57. except ldap.LDAPError as e:
  58. ldap.fatal('Failed to enable dynamic plugin!' + e.message['desc'])
  59. assert False
  60. topology.standalone.plugins.enable(name=PLUGIN_ATTR_UNIQUENESS)
  61. def test_attr_uniqueness(topology):
  62. log.info('Running test_attr_uniqueness...')
  63. #
  64. # Configure plugin
  65. #
  66. try:
  67. topology.standalone.modify_s('cn=' + PLUGIN_ATTR_UNIQUENESS + ',cn=plugins,cn=config',
  68. [(ldap.MOD_REPLACE, 'uniqueness-attribute-name', 'uid')])
  69. except ldap.LDAPError as e:
  70. log.fatal('test_attr_uniqueness: Failed to configure plugin for "uid": error ' + e.message['desc'])
  71. assert False
  72. # Add an entry
  73. try:
  74. topology.standalone.add_s(Entry((USER1_DN, {'objectclass': "top extensibleObject".split(),
  75. 'sn': '1',
  76. 'cn': 'user 1',
  77. 'uid': 'user1',
  78. 'mail': '[email protected]',
  79. 'mailAlternateAddress': '[email protected]',
  80. 'userpassword': 'password'})))
  81. except ldap.LDAPError as e:
  82. log.fatal('test_attr_uniqueness: Failed to add test user' + USER1_DN + ': error ' + e.message['desc'])
  83. assert False
  84. # Add an entry with a duplicate "uid"
  85. try:
  86. topology.standalone.add_s(Entry((USER2_DN, {'objectclass': "top extensibleObject".split(),
  87. 'sn': '2',
  88. 'cn': 'user 2',
  89. 'uid': 'user2',
  90. 'uid': 'user1',
  91. 'userpassword': 'password'})))
  92. except ldap.CONSTRAINT_VIOLATION:
  93. pass
  94. else:
  95. log.fatal('test_attr_uniqueness: Adding of 2nd entry(uid) incorrectly succeeded')
  96. assert False
  97. #
  98. # Change config to use "mail" instead of "uid"
  99. #
  100. try:
  101. topology.standalone.modify_s('cn=' + PLUGIN_ATTR_UNIQUENESS + ',cn=plugins,cn=config',
  102. [(ldap.MOD_REPLACE, 'uniqueness-attribute-name', 'mail')])
  103. except ldap.LDAPError as e:
  104. log.fatal('test_attr_uniqueness: Failed to configure plugin for "mail": error ' + e.message['desc'])
  105. assert False
  106. #
  107. # Test plugin - Add an entry, that has a duplicate "mail" value
  108. #
  109. try:
  110. topology.standalone.add_s(Entry((USER2_DN, {'objectclass': "top extensibleObject".split(),
  111. 'sn': '2',
  112. 'cn': 'user 2',
  113. 'uid': 'user2',
  114. 'mail': '[email protected]',
  115. 'userpassword': 'password'})))
  116. except ldap.CONSTRAINT_VIOLATION:
  117. pass
  118. else:
  119. log.fatal('test_attr_uniqueness: Adding of 2nd entry(mail) incorrectly succeeded')
  120. assert False
  121. #
  122. # Reconfigure plugin for mail and mailAlternateAddress
  123. #
  124. try:
  125. topology.standalone.modify_s('cn=' + PLUGIN_ATTR_UNIQUENESS + ',cn=plugins,cn=config',
  126. [(ldap.MOD_REPLACE, 'uniqueness-attribute-name', 'mail'),
  127. (ldap.MOD_ADD, 'uniqueness-attribute-name',
  128. 'mailAlternateAddress')])
  129. except ldap.LDAPError as e:
  130. log.error('test_attr_uniqueness: Failed to reconfigure plugin for "mail mailAlternateAddress": error ' +
  131. e.message['desc'])
  132. assert False
  133. #
  134. # Test plugin - Add an entry, that has a duplicate "mail" value
  135. #
  136. try:
  137. topology.standalone.add_s(Entry((USER2_DN, {'objectclass': "top extensibleObject".split(),
  138. 'sn': '2',
  139. 'cn': 'user 2',
  140. 'uid': 'user2',
  141. 'mail': '[email protected]',
  142. 'userpassword': 'password'})))
  143. except ldap.CONSTRAINT_VIOLATION:
  144. pass
  145. else:
  146. log.error('test_attr_uniqueness: Adding of 3rd entry(mail) incorrectly succeeded')
  147. assert False
  148. #
  149. # Test plugin - Add an entry, that has a duplicate "mailAlternateAddress" value
  150. #
  151. try:
  152. topology.standalone.add_s(Entry((USER2_DN, {'objectclass': "top extensibleObject".split(),
  153. 'sn': '2',
  154. 'cn': 'user 2',
  155. 'uid': 'user2',
  156. 'mailAlternateAddress': '[email protected]',
  157. 'userpassword': 'password'})))
  158. except ldap.CONSTRAINT_VIOLATION:
  159. pass
  160. else:
  161. log.error('test_attr_uniqueness: Adding of 4th entry(mailAlternateAddress) incorrectly succeeded')
  162. assert False
  163. #
  164. # Test plugin - Add an entry, that has a duplicate "mail" value conflicting mailAlternateAddress
  165. #
  166. try:
  167. topology.standalone.add_s(Entry((USER2_DN, {'objectclass': "top extensibleObject".split(),
  168. 'sn': '2',
  169. 'cn': 'user 2',
  170. 'uid': 'user2',
  171. 'mail': '[email protected]',
  172. 'userpassword': 'password'})))
  173. except ldap.CONSTRAINT_VIOLATION:
  174. pass
  175. else:
  176. log.error('test_attr_uniqueness: Adding of 5th entry(mailAlternateAddress) incorrectly succeeded')
  177. assert False
  178. #
  179. # Test plugin - Add an entry, that has a duplicate "mailAlternateAddress" conflicting mail
  180. #
  181. try:
  182. topology.standalone.add_s(Entry((USER2_DN, {'objectclass': "top extensibleObject".split(),
  183. 'sn': '2',
  184. 'cn': 'user 2',
  185. 'uid': 'user2',
  186. 'mailAlternateAddress': '[email protected]',
  187. 'userpassword': 'password'})))
  188. except ldap.CONSTRAINT_VIOLATION:
  189. pass
  190. else:
  191. log.error('test_attr_uniqueness: Adding of 6th entry(mail) incorrectly succeeded')
  192. assert False
  193. #
  194. # Cleanup
  195. #
  196. try:
  197. topology.standalone.delete_s(USER1_DN)
  198. except ldap.LDAPError as e:
  199. log.fatal('test_attr_uniqueness: Failed to delete test entry: ' + e.message['desc'])
  200. assert False
  201. log.info('test_attr_uniqueness: PASS\n')
  202. def run_isolated():
  203. global installation1_prefix
  204. installation1_prefix = None
  205. topo = topology(True)
  206. test_attr_uniqueness_init(topo)
  207. test_attr_uniqueness(topo)
  208. if __name__ == '__main__':
  209. run_isolated()