浏览代码

HUE-3526 [useradmin] Fix LDAP tests for force_username_uppercase

Jenny Kim 9 年之前
父节点
当前提交
8e35712
共有 2 个文件被更改,包括 61 次插入23 次删除
  1. 1 1
      apps/useradmin/src/useradmin/ldap_access.py
  2. 60 22
      apps/useradmin/src/useradmin/test_ldap.py

+ 1 - 1
apps/useradmin/src/useradmin/ldap_access.py

@@ -103,7 +103,7 @@ def get_or_create_ldap_user(username):
   else:
     if desktop.conf.LDAP.FORCE_USERNAME_LOWERCASE.get():
       username = username.lower()
-    elif desktop.conf.LDAP.FORCE_USERNAME_LOWERCASE.get():
+    elif desktop.conf.LDAP.FORCE_USERNAME_UPPERCASE.get():
       username = username.upper()
     return User.objects.create(username=username), True
 

+ 60 - 22
apps/useradmin/src/useradmin/test_ldap.py

@@ -474,21 +474,32 @@ class TestUserAdminLdap(BaseUserAdminTests):
       assert_false(User.objects.filter(username='Rock').exists())
       assert_true(User.objects.filter(username='rock').exists())
 
-      # Test upper case
-      User.objects.filter(username__iexact='rock').delete()
-      import_ldap_users(ldap_access.CACHED_LDAP_CONN, 'rock', sync_groups=False, import_by_dn=False)
-      assert_false(User.objects.filter(username='Rock').exists())
-      assert_true(User.objects.filter(username='ROCK').exists())
+    finally:
+      for finish in done:
+        finish()
 
-      done.append(desktop.conf.LDAP.FORCE_USERNAME_UPPERCASE.set_for_testing(True))
 
-      import_ldap_users(ldap_access.CACHED_LDAP_CONN, 'Rock', sync_groups=False, import_by_dn=False)
-      assert_false(User.objects.filter(username='Rock').exists())
-      assert_true(User.objects.filter(username='ROCK').exists())
+  def test_useradmin_ldap_force_uppercase(self):
+    if is_live_cluster():
+      raise SkipTest('HUE-2897: Skipping because the DB may not be case sensitive')
+
+    done = []
+
+    # Set to nonsensical value just to force new config usage.
+    # Should continue to use cached connection.
+    done.append(desktop.conf.LDAP.LDAP_SERVERS.set_for_testing(get_nonsense_config()))
+
+    try:
+      # Set up LDAP tests to use a LdapTestConnection instead of an actual LDAP connection
+      ldap_access.CACHED_LDAP_CONN = LdapTestConnection()
+
+      # Test upper case
+      User.objects.filter(username__iexact='Rock').delete()
+      done.append(desktop.conf.LDAP.IGNORE_USERNAME_CASE.set_for_testing(False))
+      done.append(desktop.conf.LDAP.FORCE_USERNAME_LOWERCASE.set_for_testing(False))
+      done.append(desktop.conf.LDAP.FORCE_USERNAME_UPPERCASE.set_for_testing(True))
 
-      User.objects.filter(username='Rock').delete()
       import_ldap_users(ldap_access.CACHED_LDAP_CONN, 'Rock', sync_groups=False, import_by_dn=False)
-      assert_false(User.objects.filter(username='Rock').exists())
       assert_true(User.objects.filter(username='ROCK').exists())
     finally:
       for finish in done:
@@ -548,17 +559,6 @@ class TestUserAdminLdap(BaseUserAdminTests):
       assert_false(User.objects.filter(username='Rock').exists())
       assert_true(User.objects.filter(username='rock').exists())
 
-      # Test upper case
-      done.append(desktop.conf.LDAP.FORCE_USERNAME_UPPERCASE.set_for_testing(True))
-      User.objects.filter(username__iexact='Rock').delete()
-      assert_false(User.objects.filter(username='Rock').exists())
-      assert_false(User.objects.filter(username='ROCK').exists())
-      response = c.post(URL, dict(server='nonsense', username_pattern='ROCK', password1='test', password2='test'))
-      assert_true('Location' in response, response)
-      assert_true('/useradmin/users' in response['Location'], response)
-      assert_false(User.objects.filter(username='Rock').exists())
-      assert_true(User.objects.filter(username='ROCK').exists())
-
       # Test regular with spaces (should fail)
       response = c.post(URL, dict(server='nonsense', username_pattern='user with space', password1='test', password2='test'))
       assert_true("Username must not contain whitespaces and ':'" in response.context['form'].errors['username_pattern'][0], response)
@@ -578,6 +578,44 @@ class TestUserAdminLdap(BaseUserAdminTests):
         finish()
 
 
+  def test_add_ldap_users_force_uppercase(self):
+    if is_live_cluster():
+      raise SkipTest('HUE-2897: Skipping because the DB may not be case sensitive')
+
+    done = []
+
+    # Set to nonsensical value just to force new config usage.
+    # Should continue to use cached connection.
+    done.append(desktop.conf.LDAP.LDAP_SERVERS.set_for_testing(get_nonsense_config()))
+
+    try:
+      URL = reverse(add_ldap_users)
+
+      # Set up LDAP tests to use a LdapTestConnection instead of an actual LDAP connection
+      ldap_access.CACHED_LDAP_CONN = LdapTestConnection()
+
+      c = make_logged_in_client('test', is_superuser=True)
+
+      assert_true(c.get(URL))
+
+      # Test upper case
+      done.append(desktop.conf.LDAP.IGNORE_USERNAME_CASE.set_for_testing(False))
+      done.append(desktop.conf.LDAP.FORCE_USERNAME_LOWERCASE.set_for_testing(False))
+      done.append(desktop.conf.LDAP.FORCE_USERNAME_UPPERCASE.set_for_testing(True))
+
+      User.objects.filter(username='rock').delete()
+      assert_false(User.objects.filter(username='Rock').exists())
+      assert_false(User.objects.filter(username='ROCK').exists())
+
+      response = c.post(URL, dict(server='nonsense', username_pattern='Rock', password1='test', password2='test'))
+      assert_true('Location' in response, response)
+      assert_true('/useradmin/users' in response['Location'], response)
+      assert_true(User.objects.filter(username='ROCK').exists())
+    finally:
+      for finish in done:
+        finish()
+
+
   def test_add_ldap_groups(self):
     URL = reverse(add_ldap_groups)