Browse Source

HUE-8737 [useradmin] Fix useradmin unit tests in py3

Ying Chen 5 years ago
parent
commit
db72f9bfc4

+ 3 - 0
apps/useradmin/src/useradmin/templates/sync_ldap_users_groups.mako

@@ -14,7 +14,10 @@
 ## See the License for the specific language governing permissions and
 ## See the License for the specific language governing permissions and
 ## limitations under the License.
 ## limitations under the License.
 <%!
 <%!
+import sys
 from django.utils.translation import ugettext as _
 from django.utils.translation import ugettext as _
+if sys.version_info[0] > 2:
+  unicode = str
 %>
 %>
 
 
 <%def name="render_field(field)">
 <%def name="render_field(field)">

+ 7 - 4
apps/useradmin/src/useradmin/test_ldap.py

@@ -565,9 +565,12 @@ class TestUserAdminLdap(BaseUserAdminTests):
 
 
       # Test dn with spaces in username and dn (should fail)
       # Test dn with spaces in username and dn (should fail)
       response = c.post(URL, dict(server='multi_ldap_conf', username_pattern='uid=user with space,ou=People,dc=example,dc=com', password1='test', password2='test', dn=True))
       response = c.post(URL, dict(server='multi_ldap_conf', username_pattern='uid=user with space,ou=People,dc=example,dc=com', password1='test', password2='test', dn=True))
-      assert_true("Could not get LDAP details for users in pattern" in response.content, response.content)
+      assert_true(b"Could not get LDAP details for users in pattern" in response.content, response.content)
       response = c.get(reverse(desktop.views.log_view))
       response = c.get(reverse(desktop.views.log_view))
-      assert_true("{username}: Username must not contain whitespaces".format(username='user with space') in response.content, response.content)
+      whitespaces_message = "{username}: Username must not contain whitespaces".format(username='user with space')
+      if not isinstance(whitespaces_message, bytes):
+        whitespaces_message = whitespaces_message.encode('utf-8')
+      assert_true(whitespaces_message in response.content, response.content)
 
 
       # Test dn with spaces in dn, but not username (should succeed)
       # Test dn with spaces in dn, but not username (should succeed)
       response = c.post(URL, dict(server='multi_ldap_conf', username_pattern='uid=user without space,ou=People,dc=example,dc=com', password1='test', password2='test', dn=True))
       response = c.post(URL, dict(server='multi_ldap_conf', username_pattern='uid=user without space,ou=People,dc=example,dc=com', password1='test', password2='test', dn=True))
@@ -652,7 +655,7 @@ class TestUserAdminLdap(BaseUserAdminTests):
       # Import test_longfirstname user
       # Import test_longfirstname user
       ldap_access.CACHED_LDAP_CONN.add_user_group_for_test('uid=test_longfirstname,ou=People,dc=example,dc=com', 'TestUsers')
       ldap_access.CACHED_LDAP_CONN.add_user_group_for_test('uid=test_longfirstname,ou=People,dc=example,dc=com', 'TestUsers')
       response = c.post(URL, dict(server='multi_ldap_conf', groupname_pattern='TestUsers', import_members=True), follow=True)
       response = c.post(URL, dict(server='multi_ldap_conf', groupname_pattern='TestUsers', import_members=True), follow=True)
-      assert_true('Failed to import following users: test_toolongusernametoolongusername, test_longfirstname' in response.content, response.content)
+      assert_true(b'Failed to import following users: test_toolongusernametoolongusername, test_longfirstname' in response.content, response.content)
 
 
       # Test with space
       # Test with space
       response = c.post(URL, dict(server='multi_ldap_conf', groupname_pattern='Test Administrators'))
       response = c.post(URL, dict(server='multi_ldap_conf', groupname_pattern='Test Administrators'))
@@ -712,7 +715,7 @@ class TestUserAdminLdap(BaseUserAdminTests):
 
 
     try:
     try:
       response = c.post(reverse(add_ldap_users), dict(server='multi_ldap_conf', username_pattern='moe', password1='test', password2='test'), follow=True)
       response = c.post(reverse(add_ldap_users), dict(server='multi_ldap_conf', username_pattern='moe', password1='test', password2='test'), follow=True)
-      assert_true('There was an error when communicating with LDAP' in response.content, response)
+      assert_true(b'There was an error when communicating with LDAP' in response.content, response)
     finally:
     finally:
       for finish in reset:
       for finish in reset:
         finish()
         finish()

+ 6 - 3
apps/useradmin/src/useradmin/test_ldap_deprecated.py

@@ -477,9 +477,12 @@ class TestUserAdminLdapDeprecated(BaseUserAdminTests):
 
 
       # Test dn with spaces in username and dn (should fail)
       # Test dn with spaces in username and dn (should fail)
       response = c.post(URL, dict(username_pattern='uid=user with space,ou=People,dc=example,dc=com', password1='test', password2='test', dn=True))
       response = c.post(URL, dict(username_pattern='uid=user with space,ou=People,dc=example,dc=com', password1='test', password2='test', dn=True))
-      assert_true("Could not get LDAP details for users in pattern" in response.content, response)
+      assert_true(b"Could not get LDAP details for users in pattern" in response.content, response)
       response = c.get(reverse(desktop.views.log_view))
       response = c.get(reverse(desktop.views.log_view))
-      assert_true("{username}: Username must not contain whitespaces".format(username='user with space') in response.content, response.content)
+      whitespaces_message = "{username}: Username must not contain whitespaces".format(username='user with space')
+      if not isinstance(whitespaces_message, bytes):
+        whitespaces_message = whitespaces_message.encode('utf-8')
+      assert_true(whitespaces_message in response.content, response.content)
 
 
       # Test dn with spaces in dn, but not username (should succeed)
       # Test dn with spaces in dn, but not username (should succeed)
       response = c.post(URL, dict(username_pattern='uid=user without space,ou=People,dc=example,dc=com', password1='test', password2='test', dn=True))
       response = c.post(URL, dict(username_pattern='uid=user without space,ou=People,dc=example,dc=com', password1='test', password2='test', dn=True))
@@ -584,7 +587,7 @@ class TestUserAdminLdapDeprecated(BaseUserAdminTests):
     c = make_logged_in_client('test', is_superuser=True)
     c = make_logged_in_client('test', is_superuser=True)
 
 
     response = c.post(reverse(add_ldap_users), dict(username_pattern='moe', password1='test', password2='test'), follow=True)
     response = c.post(reverse(add_ldap_users), dict(username_pattern='moe', password1='test', password2='test'), follow=True)
-    assert_true('There was an error when communicating with LDAP' in response.content, response)
+    assert_true(b'There was an error when communicating with LDAP' in response.content, response)
 
 
 class TestUserAdminLdapDeprecatedWithHadoop(BaseUserAdminTests):
 class TestUserAdminLdapDeprecatedWithHadoop(BaseUserAdminTests):
   requires_hadoop = True
   requires_hadoop = True