Completed test coverage for createsuperuser command.

This commit is contained in:
Marcelo Galigniana 2022-09-26 09:56:50 -03:00 committed by Mariusz Felisiak
parent f08651c06c
commit 67c34c1a37
2 changed files with 18 additions and 7 deletions

View File

@ -282,13 +282,11 @@ class Command(BaseCommand):
def username_is_unique(self): def username_is_unique(self):
if self.username_field.unique: if self.username_field.unique:
return True return True
for unique_constraint in self.UserModel._meta.total_unique_constraints: return any(
if (
len(unique_constraint.fields) == 1 len(unique_constraint.fields) == 1
and unique_constraint.fields[0] == self.username_field.name and unique_constraint.fields[0] == self.username_field.name
): for unique_constraint in self.UserModel._meta.total_unique_constraints
return True )
return False
def _validate_username(self, username, verbose_field_name, database): def _validate_username(self, username, verbose_field_name, database):
"""Validate username. If invalid, return a string error message.""" """Validate username. If invalid, return a string error message."""

View File

@ -312,6 +312,19 @@ class CreatesuperuserManagementCommandTestCase(TestCase):
# created password should be unusable # created password should be unusable
self.assertFalse(u.has_usable_password()) self.assertFalse(u.has_usable_password())
def test_validate_username(self):
msg = (
"Enter a valid username. This value may contain only letters, numbers, "
"and @/./+/-/_ characters."
)
with self.assertRaisesMessage(CommandError, msg):
call_command(
"createsuperuser",
interactive=False,
username="🤠",
email="joe@somewhere.org",
)
def test_non_ascii_verbose_name(self): def test_non_ascii_verbose_name(self):
@mock_inputs( @mock_inputs(
{ {