Fixed #31878 -- Made createsuperuser respect --database option in default usernames.
This commit is contained in:
parent
552bb82928
commit
b88f98738f
|
@ -101,14 +101,15 @@ def get_system_username():
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
||||||
def get_default_username(check_db=True):
|
def get_default_username(check_db=True, database=DEFAULT_DB_ALIAS):
|
||||||
"""
|
"""
|
||||||
Try to determine the current system user's username to use as a default.
|
Try to determine the current system user's username to use as a default.
|
||||||
|
|
||||||
:param check_db: If ``True``, requires that the username does not match an
|
:param check_db: If ``True``, requires that the username does not match an
|
||||||
existing ``auth.User`` (otherwise returns an empty string).
|
existing ``auth.User`` (otherwise returns an empty string).
|
||||||
|
:param database: The database where the unique check will be performed.
|
||||||
:returns: The username, or an empty string if no username can be
|
:returns: The username, or an empty string if no username can be
|
||||||
determined.
|
determined or the suggested username is already taken.
|
||||||
"""
|
"""
|
||||||
# This file is used in apps.py, it should not trigger models import.
|
# This file is used in apps.py, it should not trigger models import.
|
||||||
from django.contrib.auth import models as auth_app
|
from django.contrib.auth import models as auth_app
|
||||||
|
@ -137,7 +138,9 @@ def get_default_username(check_db=True):
|
||||||
# Don't return the default username if it is already taken.
|
# Don't return the default username if it is already taken.
|
||||||
if check_db and default_username:
|
if check_db and default_username:
|
||||||
try:
|
try:
|
||||||
auth_app.User._default_manager.get(username=default_username)
|
auth_app.User._default_manager.db_manager(database).get(
|
||||||
|
username=default_username,
|
||||||
|
)
|
||||||
except auth_app.User.DoesNotExist:
|
except auth_app.User.DoesNotExist:
|
||||||
pass
|
pass
|
||||||
else:
|
else:
|
||||||
|
|
|
@ -97,7 +97,7 @@ class Command(BaseCommand):
|
||||||
fake_user_data = {}
|
fake_user_data = {}
|
||||||
if hasattr(self.stdin, 'isatty') and not self.stdin.isatty():
|
if hasattr(self.stdin, 'isatty') and not self.stdin.isatty():
|
||||||
raise NotRunningInTTYException
|
raise NotRunningInTTYException
|
||||||
default_username = get_default_username()
|
default_username = get_default_username(database=database)
|
||||||
if username:
|
if username:
|
||||||
error_msg = self._validate_username(username, verbose_field_name, database)
|
error_msg = self._validate_username(username, verbose_field_name, database)
|
||||||
if error_msg:
|
if error_msg:
|
||||||
|
|
|
@ -102,6 +102,7 @@ class MockInputTests(TestCase):
|
||||||
|
|
||||||
|
|
||||||
class GetDefaultUsernameTestCase(TestCase):
|
class GetDefaultUsernameTestCase(TestCase):
|
||||||
|
databases = {'default', 'other'}
|
||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
self.old_get_system_username = management.get_system_username
|
self.old_get_system_username = management.get_system_username
|
||||||
|
@ -128,6 +129,15 @@ class GetDefaultUsernameTestCase(TestCase):
|
||||||
management.get_system_username = lambda: 'J\xfalia'
|
management.get_system_username = lambda: 'J\xfalia'
|
||||||
self.assertEqual(management.get_default_username(), 'julia')
|
self.assertEqual(management.get_default_username(), 'julia')
|
||||||
|
|
||||||
|
def test_with_database(self):
|
||||||
|
User.objects.create(username='joe')
|
||||||
|
management.get_system_username = lambda: 'joe'
|
||||||
|
self.assertEqual(management.get_default_username(), '')
|
||||||
|
self.assertEqual(management.get_default_username(database='other'), 'joe')
|
||||||
|
|
||||||
|
User.objects.using('other').create(username='joe')
|
||||||
|
self.assertEqual(management.get_default_username(database='other'), '')
|
||||||
|
|
||||||
|
|
||||||
@override_settings(AUTH_PASSWORD_VALIDATORS=[
|
@override_settings(AUTH_PASSWORD_VALIDATORS=[
|
||||||
{'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator'},
|
{'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator'},
|
||||||
|
@ -1047,6 +1057,36 @@ class MultiDBCreatesuperuserTestCase(TestCase):
|
||||||
user = User.objects.using('other').get(username='joe')
|
user = User.objects.using('other').get(username='joe')
|
||||||
self.assertEqual(user.email, 'joe@somewhere.org')
|
self.assertEqual(user.email, 'joe@somewhere.org')
|
||||||
|
|
||||||
|
def test_createsuperuser_command_suggested_username_with_database_option(self):
|
||||||
|
default_username = get_default_username(database='other')
|
||||||
|
qs = User.objects.using('other')
|
||||||
|
|
||||||
|
@mock_inputs({'password': 'nopasswd', 'username': '', 'email': ''})
|
||||||
|
def test_other_create_with_suggested_username(self):
|
||||||
|
call_command(
|
||||||
|
'createsuperuser',
|
||||||
|
interactive=True,
|
||||||
|
stdin=MockTTY(),
|
||||||
|
verbosity=0,
|
||||||
|
database='other',
|
||||||
|
)
|
||||||
|
self.assertIs(qs.filter(username=default_username).exists(), True)
|
||||||
|
|
||||||
|
test_other_create_with_suggested_username(self)
|
||||||
|
|
||||||
|
@mock_inputs({'password': 'nopasswd', 'Username: ': 'other', 'email': ''})
|
||||||
|
def test_other_no_suggestion(self):
|
||||||
|
call_command(
|
||||||
|
'createsuperuser',
|
||||||
|
interactive=True,
|
||||||
|
stdin=MockTTY(),
|
||||||
|
verbosity=0,
|
||||||
|
database='other',
|
||||||
|
)
|
||||||
|
self.assertIs(qs.filter(username='other').exists(), True)
|
||||||
|
|
||||||
|
test_other_no_suggestion(self)
|
||||||
|
|
||||||
|
|
||||||
class CreatePermissionsTests(TestCase):
|
class CreatePermissionsTests(TestCase):
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue