From 39b55537ec2ff0f968ffb778d9e0a076db8f7a0e Mon Sep 17 00:00:00 2001 From: David Gibbons Date: Tue, 18 Aug 2015 02:28:22 +0100 Subject: [PATCH] Added two more tests for user-entered passwords when creating a superuser. --- tests/auth_tests/test_management.py | 66 +++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) diff --git a/tests/auth_tests/test_management.py b/tests/auth_tests/test_management.py index a63dfbc6fd..70d5ceac5b 100644 --- a/tests/auth_tests/test_management.py +++ b/tests/auth_tests/test_management.py @@ -498,6 +498,72 @@ class CreatesuperuserManagementCommandTestCase(TestCase): test(self) + def test_validation_mismatched_passwords(self): + """ + Creation should fail if the user enters mismatched passwords. + """ + new_io = six.StringIO() + + # The first two passwords do not match, but the second two do match and + # are valid. + entered_passwords = ["password", "not password", "password2", "password2"] + + def mismatched_passwords_then_matched(): + return entered_passwords.pop(0) + + @mock_inputs({ + 'password': mismatched_passwords_then_matched, + 'username': 'joe1234567890', + }) + def test(self): + call_command( + "createsuperuser", + interactive=True, + stdin=MockTTY(), + stdout=new_io, + stderr=new_io, + ) + self.assertEqual( + new_io.getvalue().strip(), + "Error: Your passwords didn't match.\n" + "Superuser created successfully." + ) + + test(self) + + def test_validation_blank_password_entered(self): + """ + Creation should fail if the user enters blank passwords. + """ + new_io = six.StringIO() + + # The first two passwords are empty strings, but the second two are + # valid. + entered_passwords = ["", "", "password2", "password2"] + + def blank_passwords_then_valid(): + return entered_passwords.pop(0) + + @mock_inputs({ + 'password': blank_passwords_then_valid, + 'username': 'joe1234567890', + }) + def test(self): + call_command( + "createsuperuser", + interactive=True, + stdin=MockTTY(), + stdout=new_io, + stderr=new_io, + ) + self.assertEqual( + new_io.getvalue().strip(), + "Error: Blank passwords aren't allowed.\n" + "Superuser created successfully." + ) + + test(self) + class CustomUserModelValidationTestCase(SimpleTestCase): @override_settings(AUTH_USER_MODEL='auth.CustomUserNonListRequiredFields')