Improved technique for matching input prompts in contrib.auth management tests.
This commit is contained in:
parent
3d22121a0b
commit
872be5976d
|
@ -25,6 +25,12 @@ from .models import (
|
||||||
CustomUser, CustomUserNonUniqueUsername, CustomUserWithFK, Email,
|
CustomUser, CustomUserNonUniqueUsername, CustomUserWithFK, Email,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
MOCK_INPUT_KEY_TO_PROMPTS = {
|
||||||
|
# @mock_inputs dict key: [expected prompt messages],
|
||||||
|
'email': ['Email address: '],
|
||||||
|
'username': ['Username: ', lambda: "Username (leave blank to use '%s'): " % get_default_username()],
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
def mock_inputs(inputs):
|
def mock_inputs(inputs):
|
||||||
"""
|
"""
|
||||||
|
@ -42,14 +48,21 @@ def mock_inputs(inputs):
|
||||||
|
|
||||||
def mock_input(prompt):
|
def mock_input(prompt):
|
||||||
assert '__proxy__' not in prompt
|
assert '__proxy__' not in prompt
|
||||||
response = ''
|
response = None
|
||||||
for key, val in inputs.items():
|
for key, val in inputs.items():
|
||||||
if key in prompt.lower():
|
# get() fallback because sometimes 'key' is the actual
|
||||||
|
# prompt rather than a shortcut name.
|
||||||
|
prompt_msgs = MOCK_INPUT_KEY_TO_PROMPTS.get(key, key)
|
||||||
|
if isinstance(prompt_msgs, list):
|
||||||
|
prompt_msgs = [msg() if callable(msg) else msg for msg in prompt_msgs]
|
||||||
|
if prompt in prompt_msgs:
|
||||||
if callable(val):
|
if callable(val):
|
||||||
response = val()
|
response = val()
|
||||||
else:
|
else:
|
||||||
response = val
|
response = val
|
||||||
break
|
break
|
||||||
|
if response is None:
|
||||||
|
raise ValueError('Mock input for %r not found.' % prompt)
|
||||||
return response
|
return response
|
||||||
|
|
||||||
old_getpass = createsuperuser.getpass
|
old_getpass = createsuperuser.getpass
|
||||||
|
@ -74,6 +87,13 @@ class MockTTY:
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
class MockInputTests(TestCase):
|
||||||
|
@mock_inputs({'username': 'alice'})
|
||||||
|
def test_input_not_found(self):
|
||||||
|
with self.assertRaisesMessage(ValueError, "Mock input for 'Email address: ' not found."):
|
||||||
|
call_command('createsuperuser', stdin=MockTTY())
|
||||||
|
|
||||||
|
|
||||||
class GetDefaultUsernameTestCase(TestCase):
|
class GetDefaultUsernameTestCase(TestCase):
|
||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
|
@ -236,27 +256,31 @@ 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())
|
||||||
|
|
||||||
@mock_inputs({
|
|
||||||
'password': "nopasswd",
|
|
||||||
'u\u017eivatel': 'foo', # username (cz)
|
|
||||||
'email': 'nolocale@somewhere.org'})
|
|
||||||
def test_non_ascii_verbose_name(self):
|
def test_non_ascii_verbose_name(self):
|
||||||
username_field = User._meta.get_field('username')
|
@mock_inputs({
|
||||||
old_verbose_name = username_field.verbose_name
|
'password': "nopasswd",
|
||||||
username_field.verbose_name = _('u\u017eivatel')
|
"Uživatel (leave blank to use '%s'): " % get_default_username(): 'foo', # username (cz)
|
||||||
new_io = StringIO()
|
'email': 'nolocale@somewhere.org',
|
||||||
try:
|
})
|
||||||
call_command(
|
def test(self):
|
||||||
"createsuperuser",
|
username_field = User._meta.get_field('username')
|
||||||
interactive=True,
|
old_verbose_name = username_field.verbose_name
|
||||||
stdout=new_io,
|
username_field.verbose_name = _('u\u017eivatel')
|
||||||
stdin=MockTTY(),
|
new_io = StringIO()
|
||||||
)
|
try:
|
||||||
finally:
|
call_command(
|
||||||
username_field.verbose_name = old_verbose_name
|
"createsuperuser",
|
||||||
|
interactive=True,
|
||||||
|
stdout=new_io,
|
||||||
|
stdin=MockTTY(),
|
||||||
|
)
|
||||||
|
finally:
|
||||||
|
username_field.verbose_name = old_verbose_name
|
||||||
|
|
||||||
command_output = new_io.getvalue().strip()
|
command_output = new_io.getvalue().strip()
|
||||||
self.assertEqual(command_output, 'Superuser created successfully.')
|
self.assertEqual(command_output, 'Superuser created successfully.')
|
||||||
|
|
||||||
|
test(self)
|
||||||
|
|
||||||
def test_verbosity_zero(self):
|
def test_verbosity_zero(self):
|
||||||
# We can suppress output on the management command
|
# We can suppress output on the management command
|
||||||
|
@ -445,9 +469,9 @@ class CreatesuperuserManagementCommandTestCase(TestCase):
|
||||||
|
|
||||||
@mock_inputs({
|
@mock_inputs({
|
||||||
'password': 'nopasswd',
|
'password': 'nopasswd',
|
||||||
'username (email.id)': email.pk,
|
'Username (Email.id): ': email.pk,
|
||||||
'email (email.email)': email.email,
|
'Email (Email.email): ': email.email,
|
||||||
'group (group.id)': group.pk,
|
'Group (Group.id): ': group.pk,
|
||||||
})
|
})
|
||||||
def test(self):
|
def test(self):
|
||||||
call_command(
|
call_command(
|
||||||
|
@ -475,7 +499,7 @@ class CreatesuperuserManagementCommandTestCase(TestCase):
|
||||||
def return_passwords():
|
def return_passwords():
|
||||||
return entered_passwords.pop(0)
|
return entered_passwords.pop(0)
|
||||||
|
|
||||||
@mock_inputs({'password': return_passwords, 'username': ''})
|
@mock_inputs({'password': return_passwords, 'username': '', 'email': ''})
|
||||||
def test(self):
|
def test(self):
|
||||||
call_command(
|
call_command(
|
||||||
'createsuperuser',
|
'createsuperuser',
|
||||||
|
@ -506,6 +530,7 @@ class CreatesuperuserManagementCommandTestCase(TestCase):
|
||||||
@mock_inputs({
|
@mock_inputs({
|
||||||
'password': bad_then_good_password,
|
'password': bad_then_good_password,
|
||||||
'username': 'joe1234567890',
|
'username': 'joe1234567890',
|
||||||
|
'email': '',
|
||||||
})
|
})
|
||||||
def test(self):
|
def test(self):
|
||||||
call_command(
|
call_command(
|
||||||
|
@ -554,7 +579,7 @@ class CreatesuperuserManagementCommandTestCase(TestCase):
|
||||||
def return_usernames():
|
def return_usernames():
|
||||||
return entered_usernames.pop(0)
|
return entered_usernames.pop(0)
|
||||||
|
|
||||||
@mock_inputs({'password': return_passwords, 'username': return_usernames})
|
@mock_inputs({'password': return_passwords, 'username': return_usernames, 'email': ''})
|
||||||
def test(self):
|
def test(self):
|
||||||
call_command(
|
call_command(
|
||||||
'createsuperuser',
|
'createsuperuser',
|
||||||
|
@ -585,7 +610,7 @@ class CreatesuperuserManagementCommandTestCase(TestCase):
|
||||||
def return_usernames():
|
def return_usernames():
|
||||||
return entered_usernames.pop(0)
|
return entered_usernames.pop(0)
|
||||||
|
|
||||||
@mock_inputs({'password': return_passwords, 'username': return_usernames})
|
@mock_inputs({'password': return_passwords, 'username': return_usernames, 'email': ''})
|
||||||
def test(self):
|
def test(self):
|
||||||
call_command(
|
call_command(
|
||||||
'createsuperuser',
|
'createsuperuser',
|
||||||
|
@ -618,6 +643,7 @@ class CreatesuperuserManagementCommandTestCase(TestCase):
|
||||||
@mock_inputs({
|
@mock_inputs({
|
||||||
'password': mismatched_passwords_then_matched,
|
'password': mismatched_passwords_then_matched,
|
||||||
'username': 'joe1234567890',
|
'username': 'joe1234567890',
|
||||||
|
'email': '',
|
||||||
})
|
})
|
||||||
def test(self):
|
def test(self):
|
||||||
call_command(
|
call_command(
|
||||||
|
@ -651,6 +677,7 @@ class CreatesuperuserManagementCommandTestCase(TestCase):
|
||||||
@mock_inputs({
|
@mock_inputs({
|
||||||
'password': blank_passwords_then_valid,
|
'password': blank_passwords_then_valid,
|
||||||
'username': 'joe1234567890',
|
'username': 'joe1234567890',
|
||||||
|
'email': '',
|
||||||
})
|
})
|
||||||
def test(self):
|
def test(self):
|
||||||
call_command(
|
call_command(
|
||||||
|
|
Loading…
Reference in New Issue