From fcaf8eae14d0a7f884328bdf0238dd1e2881c681 Mon Sep 17 00:00:00 2001 From: Jannis Leidel Date: Fri, 2 Mar 2012 16:56:20 +0000 Subject: [PATCH] Fixed #17046 -- Added a check if the username passed to User.objects.create_user is empty or not. Thanks, kwadrat. git-svn-id: http://code.djangoproject.com/svn/django/trunk@17628 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/contrib/auth/models.py | 2 ++ django/contrib/auth/tests/models.py | 5 +++++ docs/topics/auth.txt | 4 +++- 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/django/contrib/auth/models.py b/django/contrib/auth/models.py index eb39868ca9..4e1584970c 100644 --- a/django/contrib/auth/models.py +++ b/django/contrib/auth/models.py @@ -149,6 +149,8 @@ class UserManager(models.Manager): Creates and saves a User with the given username, email and password. """ now = timezone.now() + if not username: + raise ValueError('The given username must be set') email = UserManager.normalize_email(email) user = self.model(username=username, email=email, is_staff=False, is_active=True, is_superuser=False, diff --git a/django/contrib/auth/tests/models.py b/django/contrib/auth/tests/models.py index 2d570aee15..25121966a0 100644 --- a/django/contrib/auth/tests/models.py +++ b/django/contrib/auth/tests/models.py @@ -91,3 +91,8 @@ class UserManagerTestCase(TestCase): def test_create_user_email_domain_normalize_with_whitespace(self): returned = UserManager.normalize_email('email\ with_whitespace@D.COM') self.assertEquals(returned, 'email\ with_whitespace@d.com') + + def test_empty_username(self): + self.assertRaisesMessage(ValueError, + 'The given username must be set', + User.objects.create_user, username='') diff --git a/docs/topics/auth.txt b/docs/topics/auth.txt index 08a8f342c5..6b262be47f 100644 --- a/docs/topics/auth.txt +++ b/docs/topics/auth.txt @@ -280,7 +280,9 @@ Manager functions .. method:: models.UserManager.create_user(username, email=None, password=None) .. versionchanged:: 1.4 - The ``email`` parameter was made optional. + The ``email`` parameter was made optional. The username + parameter is now checked for emptiness and raises a + :exc:`ValueError` in case of a negative result. Creates, saves and returns a :class:`~django.contrib.auth.models.User`.