ticket48026_test.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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 time
  11. import ldap
  12. import logging
  13. import pytest
  14. from lib389 import DirSrv, Entry
  15. from lib389._constants import *
  16. from lib389.properties import *
  17. from lib389.tasks import *
  18. from lib389.utils import *
  19. logging.getLogger(__name__).setLevel(logging.DEBUG)
  20. log = logging.getLogger(__name__)
  21. installation1_prefix = None
  22. USER1_DN = 'uid=user1,' + DEFAULT_SUFFIX
  23. USER2_DN = 'uid=user2,' + DEFAULT_SUFFIX
  24. class TopologyStandalone(object):
  25. def __init__(self, standalone):
  26. standalone.open()
  27. self.standalone = standalone
  28. @pytest.fixture(scope="module")
  29. def topology(request):
  30. global installation1_prefix
  31. if installation1_prefix:
  32. args_instance[SER_DEPLOYED_DIR] = installation1_prefix
  33. # Creating standalone instance ...
  34. standalone = DirSrv(verbose=False)
  35. args_instance[SER_HOST] = HOST_STANDALONE
  36. args_instance[SER_PORT] = PORT_STANDALONE
  37. args_instance[SER_SERVERID_PROP] = SERVERID_STANDALONE
  38. args_instance[SER_CREATION_SUFFIX] = DEFAULT_SUFFIX
  39. args_standalone = args_instance.copy()
  40. standalone.allocate(args_standalone)
  41. instance_standalone = standalone.exists()
  42. if instance_standalone:
  43. standalone.delete()
  44. standalone.create()
  45. standalone.open()
  46. def fin():
  47. standalone.delete()
  48. request.addfinalizer(fin)
  49. return TopologyStandalone(standalone)
  50. def test_ticket48026(topology):
  51. '''
  52. Test that multiple attribute uniqueness works correctly.
  53. '''
  54. # Configure the plugin
  55. inst = topology.standalone
  56. inst.plugins.enable(name=PLUGIN_ATTR_UNIQUENESS)
  57. try:
  58. # This plugin enable / disable doesn't seem to create the nsslapd-pluginId correctly?
  59. inst.modify_s('cn=' + PLUGIN_ATTR_UNIQUENESS + ',cn=plugins,cn=config',
  60. [(ldap.MOD_REPLACE, 'uniqueness-attribute-name', 'mail'),
  61. (ldap.MOD_ADD, 'uniqueness-attribute-name',
  62. 'mailAlternateAddress'),
  63. ])
  64. except ldap.LDAPError as e:
  65. log.fatal('test_ticket48026: Failed to configure plugin for "mail": error ' + e.message['desc'])
  66. assert False
  67. inst.restart(timeout=30)
  68. # Add an entry
  69. try:
  70. inst.add_s(Entry((USER1_DN, {'objectclass': "top extensibleObject".split(),
  71. 'sn': '1',
  72. 'cn': 'user 1',
  73. 'uid': 'user1',
  74. 'mail': '[email protected]',
  75. 'mailAlternateAddress' : '[email protected]',
  76. 'userpassword': 'password'})))
  77. except ldap.LDAPError as e:
  78. log.fatal('test_ticket48026: Failed to add test user' + USER1_DN + ': error ' + e.message['desc'])
  79. assert False
  80. try:
  81. inst.add_s(Entry((USER2_DN, {'objectclass': "top extensibleObject".split(),
  82. 'sn': '2',
  83. 'cn': 'user 2',
  84. 'uid': 'user2',
  85. 'mail': '[email protected]',
  86. 'userpassword': 'password'})))
  87. except ldap.CONSTRAINT_VIOLATION:
  88. pass
  89. else:
  90. log.error('test_ticket48026: Adding of 1st entry(mail v mail) incorrectly succeeded')
  91. assert False
  92. try:
  93. inst.add_s(Entry((USER2_DN, {'objectclass': "top extensibleObject".split(),
  94. 'sn': '2',
  95. 'cn': 'user 2',
  96. 'uid': 'user2',
  97. 'mailAlternateAddress': '[email protected]',
  98. 'userpassword': 'password'})))
  99. except ldap.CONSTRAINT_VIOLATION:
  100. pass
  101. else:
  102. log.error('test_ticket48026: Adding of 2nd entry(mailAlternateAddress v mailAlternateAddress) incorrectly succeeded')
  103. assert False
  104. try:
  105. inst.add_s(Entry((USER2_DN, {'objectclass': "top extensibleObject".split(),
  106. 'sn': '2',
  107. 'cn': 'user 2',
  108. 'uid': 'user2',
  109. 'mail': '[email protected]',
  110. 'userpassword': 'password'})))
  111. except ldap.CONSTRAINT_VIOLATION:
  112. pass
  113. else:
  114. log.error('test_ticket48026: Adding of 3rd entry(mail v mailAlternateAddress) incorrectly succeeded')
  115. assert False
  116. try:
  117. inst.add_s(Entry((USER2_DN, {'objectclass': "top extensibleObject".split(),
  118. 'sn': '2',
  119. 'cn': 'user 2',
  120. 'uid': 'user2',
  121. 'mailAlternateAddress': '[email protected]',
  122. 'userpassword': 'password'})))
  123. except ldap.CONSTRAINT_VIOLATION:
  124. pass
  125. else:
  126. log.error('test_ticket48026: Adding of 4th entry(mailAlternateAddress v mail) incorrectly succeeded')
  127. assert False
  128. log.info('Test complete')
  129. if __name__ == '__main__':
  130. # Run isolated
  131. # -s for DEBUG mode
  132. CURRENT_FILE = os.path.realpath(__file__)
  133. pytest.main("-s %s" % CURRENT_FILE)