diff options
| author | Lucidiot <lucidiot@brainshit.fr> | 2022-03-31 14:39:28 +0200 |
|---|---|---|
| committer | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2022-04-01 11:39:41 +0200 |
| commit | 13a9cde133ac82e33dd091ca9bb9c677804afbe1 (patch) | |
| tree | 1b85c8c9e75d3aada9e513d6a0cf8e8347b5b82b /tests/auth_tests/test_management.py | |
| parent | ae506181f7fb9d9e74f4935686540bef29b60255 (diff) | |
Fixed #33613 -- Made createsuperuser detect uniqueness of USERNAME_FIELD when using Meta.constraints.
Diffstat (limited to 'tests/auth_tests/test_management.py')
| -rw-r--r-- | tests/auth_tests/test_management.py | 36 |
1 files changed, 36 insertions, 0 deletions
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") |
