summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorLucidiot <lucidiot@brainshit.fr>2022-03-31 14:39:28 +0200
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2022-04-01 11:39:41 +0200
commit13a9cde133ac82e33dd091ca9bb9c677804afbe1 (patch)
tree1b85c8c9e75d3aada9e513d6a0cf8e8347b5b82b /tests
parentae506181f7fb9d9e74f4935686540bef29b60255 (diff)
Fixed #33613 -- Made createsuperuser detect uniqueness of USERNAME_FIELD when using Meta.constraints.
Diffstat (limited to 'tests')
-rw-r--r--tests/auth_tests/models/__init__.py2
-rw-r--r--tests/auth_tests/models/with_unique_constraint.py22
-rw-r--r--tests/auth_tests/test_management.py36
3 files changed, 60 insertions, 0 deletions
diff --git a/tests/auth_tests/models/__init__.py b/tests/auth_tests/models/__init__.py
index bce5edf883..ed0647b90d 100644
--- a/tests/auth_tests/models/__init__.py
+++ b/tests/auth_tests/models/__init__.py
@@ -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",
diff --git a/tests/auth_tests/models/with_unique_constraint.py b/tests/auth_tests/models/with_unique_constraint.py
new file mode 100644
index 0000000000..fa8dc8f1f0
--- /dev/null
+++ b/tests/auth_tests/models/with_unique_constraint.py
@@ -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"),
+ ]
diff --git a/tests/auth_tests/test_management.py b/tests/auth_tests/test_management.py
index 071ea85a65..2e82c1bb14 100644
--- a/tests/auth_tests/test_management.py
+++ b/tests/auth_tests/test_management.py
@@ -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")