mirror of
https://github.com/django/django.git
synced 2025-10-31 09:41:08 +00:00
Fixed #33613 -- Made createsuperuser detect uniqueness of USERNAME_FIELD when using Meta.constraints.
This commit is contained in:
committed by
Mariusz Felisiak
parent
ae506181f7
commit
13a9cde133
@@ -11,6 +11,7 @@ from .with_foreign_key import CustomUserWithFK, Email
|
||||
from .with_integer_username import IntegerUsernameUser
|
||||
from .with_last_login_attr import UserWithDisabledLastLoginField
|
||||
from .with_many_to_many import CustomUserWithM2M, CustomUserWithM2MThrough, Organization
|
||||
from .with_unique_constraint import CustomUserWithUniqueConstraint
|
||||
|
||||
__all__ = (
|
||||
"CustomEmailField",
|
||||
@@ -20,6 +21,7 @@ __all__ = (
|
||||
"CustomUserWithFK",
|
||||
"CustomUserWithM2M",
|
||||
"CustomUserWithM2MThrough",
|
||||
"CustomUserWithUniqueConstraint",
|
||||
"CustomUserWithoutIsActiveField",
|
||||
"Email",
|
||||
"ExtensionUser",
|
||||
|
||||
22
tests/auth_tests/models/with_unique_constraint.py
Normal file
22
tests/auth_tests/models/with_unique_constraint.py
Normal file
@@ -0,0 +1,22 @@
|
||||
from django.contrib.auth.models import AbstractBaseUser, BaseUserManager
|
||||
from django.db import models
|
||||
|
||||
|
||||
class CustomUserWithUniqueConstraintManager(BaseUserManager):
|
||||
def create_superuser(self, username, password):
|
||||
user = self.model(username=username)
|
||||
user.set_password(password)
|
||||
user.save(using=self._db)
|
||||
return user
|
||||
|
||||
|
||||
class CustomUserWithUniqueConstraint(AbstractBaseUser):
|
||||
username = models.CharField(max_length=150)
|
||||
|
||||
objects = CustomUserWithUniqueConstraintManager()
|
||||
USERNAME_FIELD = "username"
|
||||
|
||||
class Meta:
|
||||
constraints = [
|
||||
models.UniqueConstraint(fields=["username"], name="unique_custom_username"),
|
||||
]
|
||||
@@ -23,6 +23,7 @@ from .models import (
|
||||
CustomUserNonUniqueUsername,
|
||||
CustomUserWithFK,
|
||||
CustomUserWithM2M,
|
||||
CustomUserWithUniqueConstraint,
|
||||
Email,
|
||||
Organization,
|
||||
UserProxy,
|
||||
@@ -1065,6 +1066,41 @@ class CreatesuperuserManagementCommandTestCase(TestCase):
|
||||
|
||||
test(self)
|
||||
|
||||
@override_settings(AUTH_USER_MODEL="auth_tests.CustomUserWithUniqueConstraint")
|
||||
def test_existing_username_meta_unique_constraint(self):
|
||||
"""
|
||||
Creation fails if the username already exists and a custom user model
|
||||
has UniqueConstraint.
|
||||
"""
|
||||
user = CustomUserWithUniqueConstraint.objects.create(username="janet")
|
||||
new_io = StringIO()
|
||||
entered_passwords = ["password", "password"]
|
||||
# Enter the existing username first and then a new one.
|
||||
entered_usernames = [user.username, "joe"]
|
||||
|
||||
def return_passwords():
|
||||
return entered_passwords.pop(0)
|
||||
|
||||
def return_usernames():
|
||||
return entered_usernames.pop(0)
|
||||
|
||||
@mock_inputs({"password": return_passwords, "username": return_usernames})
|
||||
def test(self):
|
||||
call_command(
|
||||
"createsuperuser",
|
||||
interactive=True,
|
||||
stdin=MockTTY(),
|
||||
stdout=new_io,
|
||||
stderr=new_io,
|
||||
)
|
||||
self.assertEqual(
|
||||
new_io.getvalue().strip(),
|
||||
"Error: That username is already taken.\n"
|
||||
"Superuser created successfully.",
|
||||
)
|
||||
|
||||
test(self)
|
||||
|
||||
def test_existing_username_non_interactive(self):
|
||||
"""Creation fails if the username already exists."""
|
||||
User.objects.create(username="janet")
|
||||
|
||||
Reference in New Issue
Block a user