summaryrefslogtreecommitdiff
path: root/tests/auth_tests/test_management.py
diff options
context:
space:
mode:
authorTim Graham <timograham@gmail.com>2017-11-02 11:15:40 -0400
committerTim Graham <timograham@gmail.com>2017-11-03 20:00:08 -0400
commit872be5976d1009ee8b90944a2fb14b1515172825 (patch)
treeab6929e5d00fed25877f3218109947d02d57f833 /tests/auth_tests/test_management.py
parent3d22121a0b5b4087487ada62b7f2a20fb30a3856 (diff)
Improved technique for matching input prompts in contrib.auth management tests.
Diffstat (limited to 'tests/auth_tests/test_management.py')
-rw-r--r--tests/auth_tests/test_management.py81
1 files changed, 54 insertions, 27 deletions
diff --git a/tests/auth_tests/test_management.py b/tests/auth_tests/test_management.py
index 5463707dad..539ac84424 100644
--- a/tests/auth_tests/test_management.py
+++ b/tests/auth_tests/test_management.py
@@ -25,6 +25,12 @@ from .models import (
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):
"""
@@ -42,14 +48,21 @@ def mock_inputs(inputs):
def mock_input(prompt):
assert '__proxy__' not in prompt
- response = ''
+ response = None
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):
response = val()
else:
response = val
break
+ if response is None:
+ raise ValueError('Mock input for %r not found.' % prompt)
return response
old_getpass = createsuperuser.getpass
@@ -74,6 +87,13 @@ class MockTTY:
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):
def setUp(self):
@@ -236,27 +256,31 @@ class CreatesuperuserManagementCommandTestCase(TestCase):
# created password should be unusable
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):
- username_field = User._meta.get_field('username')
- old_verbose_name = username_field.verbose_name
- username_field.verbose_name = _('u\u017eivatel')
- new_io = StringIO()
- try:
- call_command(
- "createsuperuser",
- interactive=True,
- stdout=new_io,
- stdin=MockTTY(),
- )
- finally:
- username_field.verbose_name = old_verbose_name
+ @mock_inputs({
+ 'password': "nopasswd",
+ "Uživatel (leave blank to use '%s'): " % get_default_username(): 'foo', # username (cz)
+ 'email': 'nolocale@somewhere.org',
+ })
+ def test(self):
+ username_field = User._meta.get_field('username')
+ old_verbose_name = username_field.verbose_name
+ username_field.verbose_name = _('u\u017eivatel')
+ new_io = StringIO()
+ try:
+ call_command(
+ "createsuperuser",
+ interactive=True,
+ stdout=new_io,
+ stdin=MockTTY(),
+ )
+ finally:
+ username_field.verbose_name = old_verbose_name
- command_output = new_io.getvalue().strip()
- self.assertEqual(command_output, 'Superuser created successfully.')
+ command_output = new_io.getvalue().strip()
+ self.assertEqual(command_output, 'Superuser created successfully.')
+
+ test(self)
def test_verbosity_zero(self):
# We can suppress output on the management command
@@ -445,9 +469,9 @@ class CreatesuperuserManagementCommandTestCase(TestCase):
@mock_inputs({
'password': 'nopasswd',
- 'username (email.id)': email.pk,
- 'email (email.email)': email.email,
- 'group (group.id)': group.pk,
+ 'Username (Email.id): ': email.pk,
+ 'Email (Email.email): ': email.email,
+ 'Group (Group.id): ': group.pk,
})
def test(self):
call_command(
@@ -475,7 +499,7 @@ class CreatesuperuserManagementCommandTestCase(TestCase):
def return_passwords():
return entered_passwords.pop(0)
- @mock_inputs({'password': return_passwords, 'username': ''})
+ @mock_inputs({'password': return_passwords, 'username': '', 'email': ''})
def test(self):
call_command(
'createsuperuser',
@@ -506,6 +530,7 @@ class CreatesuperuserManagementCommandTestCase(TestCase):
@mock_inputs({
'password': bad_then_good_password,
'username': 'joe1234567890',
+ 'email': '',
})
def test(self):
call_command(
@@ -554,7 +579,7 @@ class CreatesuperuserManagementCommandTestCase(TestCase):
def return_usernames():
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):
call_command(
'createsuperuser',
@@ -585,7 +610,7 @@ class CreatesuperuserManagementCommandTestCase(TestCase):
def return_usernames():
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):
call_command(
'createsuperuser',
@@ -618,6 +643,7 @@ class CreatesuperuserManagementCommandTestCase(TestCase):
@mock_inputs({
'password': mismatched_passwords_then_matched,
'username': 'joe1234567890',
+ 'email': '',
})
def test(self):
call_command(
@@ -651,6 +677,7 @@ class CreatesuperuserManagementCommandTestCase(TestCase):
@mock_inputs({
'password': blank_passwords_then_valid,
'username': 'joe1234567890',
+ 'email': '',
})
def test(self):
call_command(