Explorar o código

HUE-8020 [admin] Adding test of username login length

Romain %!s(int64=5) %!d(string=hai) anos
pai
achega
c3adc42a91
Modificáronse 1 ficheiros con 45 adicións e 11 borrados
  1. 45 11
      desktop/core/src/desktop/auth/views_test.py

+ 45 - 11
desktop/core/src/desktop/auth/views_test.py

@@ -17,9 +17,10 @@
 
 from builtins import object
 import datetime
-from nose.tools import assert_true, assert_false, assert_equal
+from nose.tools import assert_true, assert_false, assert_equal, assert_raises
 
 from django_auth_ldap import backend as django_auth_ldap_backend
+from django.db.utils import DataError
 from django.conf import settings
 from django.test.client import Client
 
@@ -32,15 +33,18 @@ from useradmin.views import import_ldap_groups
 
 from desktop import conf, middleware
 from desktop.auth import backend
+from desktop.auth.backend import create_user
 from desktop.lib.django_test_util import make_logged_in_client
 from desktop.lib.test_utils import add_to_group
 
 
 def get_mocked_config():
-  return {'mocked_ldap': {
-    'users': {},
-    'groups': {}
-  }}
+  return {
+    'mocked_ldap': {
+      'users': {},
+      'groups': {}
+    }
+  }
 
 class TestLoginWithHadoop(PseudoHdfsTestBase):
   integration = True
@@ -746,15 +750,14 @@ class TestLogin(PseudoHdfsTestBase):
     response = client.post('/hue/accounts/login/', dict(username=self.test_username, password="test"), follow=True)
     assert_equal(200, response.status_code, "Expected ok status.")
 
-class TestLoginNoHadoop(object):
 
+class TestLogin(object):
   reset = []
-  test_username = "test_login_no_hadoop"
+  test_username = "test_login"
 
   @classmethod
   def setup_class(cls):
-    # Simulate first login ever
-    User.objects.all().delete()
+    User.objects.all().delete()  # Simulate first login ever
 
     cls.auth_backends = settings.AUTHENTICATION_BACKENDS
     settings.AUTHENTICATION_BACKENDS = ('desktop.auth.backend.AllowFirstUserDjangoBackend',)
@@ -766,7 +769,9 @@ class TestLoginNoHadoop(object):
   def setUp(self):
     self.c = Client()
 
-    self.reset.append( conf.AUTH.BACKEND.set_for_testing(['desktop.auth.backend.AllowFirstUserDjangoBackend']) )
+    self.reset.append(
+      conf.AUTH.BACKEND.set_for_testing(['desktop.auth.backend.AllowFirstUserDjangoBackend'])
+    )
 
   def tearDown(self):
     for finish in self.reset:
@@ -776,8 +781,11 @@ class TestLoginNoHadoop(object):
     if Group.objects.filter(name=self.test_username).exists():
       Group.objects.filter(name=self.test_username).delete()
 
+
   def test_login_does_not_reset_groups(self):
-    self.reset.append( conf.AUTH.BACKEND.set_for_testing(["desktop.auth.backend.AllowFirstUserDjangoBackend"]) )
+    self.reset.append(
+      conf.AUTH.BACKEND.set_for_testing(["desktop.auth.backend.AllowFirstUserDjangoBackend"])
+    )
 
     client = make_logged_in_client(username=self.test_username, password="test")
     client.get('/accounts/logout')
@@ -804,6 +812,32 @@ class TestLoginNoHadoop(object):
     assert_equal('desktop.auth.backend.AllowFirstUserDjangoBackend', existing_profile.data['auth_backend'])
 
 
+  def test_login_long_username(self):
+    self.reset.append(
+      conf.AUTH.BACKEND.set_for_testing(["desktop.auth.backend.AllowFirstUserDjangoBackend"])
+    )
+
+    c = Client()
+
+    username = 'a' * 15
+    user = create_user(username=username, password='test', is_superuser=False)
+
+    response = c.post('/hue/accounts/login/', {'username': username, 'password': 'test'})
+    assert_equal(302, response.status_code)
+
+    username = 'a' * 145
+    user = create_user(username=username, password='test', is_superuser=False)
+    response = c.post('/hue/accounts/login/', {'username': username, 'password': 'test'})
+    assert_equal(302, response.status_code)
+
+    # 250 is currently the max in the official Django User model.
+    # We can't create a previou user with more characters as the DB will truncate anyway.
+    username = 'a' * 255
+    response = c.post('/hue/accounts/login/', {'username': username, 'password': 'test'})
+    assert_equal(200, response.status_code)
+    assert_true(response.context[0]['login_errors'])
+
+
 class TestImpersonationBackend(object):
   test_username = "test_login_impersonation"
   test_login_as_username = "test_login_as_impersonation"