From 67c34c1a3718f6d0a18bd72ca6a9795d9f4ee251 Mon Sep 17 00:00:00 2001 From: Marcelo Galigniana Date: Mon, 26 Sep 2022 09:56:50 -0300 Subject: [PATCH] Completed test coverage for createsuperuser command. --- .../auth/management/commands/createsuperuser.py | 12 +++++------- tests/auth_tests/test_management.py | 13 +++++++++++++ 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/django/contrib/auth/management/commands/createsuperuser.py b/django/contrib/auth/management/commands/createsuperuser.py index 314c5151c4..48994e5371 100644 --- a/django/contrib/auth/management/commands/createsuperuser.py +++ b/django/contrib/auth/management/commands/createsuperuser.py @@ -282,13 +282,11 @@ class Command(BaseCommand): def username_is_unique(self): if self.username_field.unique: return True - for unique_constraint in self.UserModel._meta.total_unique_constraints: - if ( - len(unique_constraint.fields) == 1 - and unique_constraint.fields[0] == self.username_field.name - ): - return True - return False + return any( + len(unique_constraint.fields) == 1 + and unique_constraint.fields[0] == self.username_field.name + for unique_constraint in self.UserModel._meta.total_unique_constraints + ) def _validate_username(self, username, verbose_field_name, database): """Validate username. If invalid, return a string error message.""" diff --git a/tests/auth_tests/test_management.py b/tests/auth_tests/test_management.py index 5d695e42a6..f567fd0dc1 100644 --- a/tests/auth_tests/test_management.py +++ b/tests/auth_tests/test_management.py @@ -312,6 +312,19 @@ class CreatesuperuserManagementCommandTestCase(TestCase): # created password should be unusable 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): @mock_inputs( {