def test_user_already_exists(self): """Raise errors if user already exists""" data = { "email": "*****@*****.**", "password1": "test123", "password2": "test123", } form = UserCreationForm(data) self.assertFalse(form.is_valid()) self.assertEqual(form["email"].errors, [str(form.error_messages["duplicate_email"])])
def test_user_already_exists(self): """Raise errors if user already exists""" data = { 'email': '*****@*****.**', 'password1': 'test123', 'password2': 'test123', } form = UserCreationForm(data) self.assertFalse(form.is_valid()) self.assertEqual(form['email'].errors, [str(form.error_messages['duplicate_email'])])
def test_validates_password_similarity_length(self): """Test misconfigured-similarity validator catches email""" data = { 'email': '*****@*****.**', 'password1': 'jsmith', 'password2': 'jsmith', } form = UserCreationForm(data) self.assertFalse(form.is_valid()) self.assertIn('The password is too similar to the email address.', form['password1'].errors)
def test_password_verification(self): """The verification password is incorrect.""" data = { 'email': '*****@*****.**', 'password1': 'test123', 'password2': 'test', } form = UserCreationForm(data) self.assertFalse(form.is_valid()) self.assertEqual(form['password1'].errors, [str(form.error_messages['password_mismatch'])])
def test_invalid_data(self): """Raise errors if invalid email format""" data = { "email": "%%%", "password1": "test123", "password2": "test123", } form = UserCreationForm(data) self.assertFalse(form.is_valid()) validator = next(v for v in User._meta.get_field("email").validators if v.code == "invalid") self.assertEqual(form["email"].errors, [str(validator.message)])
def test_invalid_data(self): """Raise errors if invalid email format""" data = { 'email': '%%%', 'password1': 'test123', 'password2': 'test123', } form = UserCreationForm(data) self.assertFalse(form.is_valid()) validator = next(v for v in User._meta.get_field('email').validators if v.code == 'invalid') self.assertEqual(form['email'].errors, [str(validator.message)])
def test_validates_password_with_all_data(self): """Test correctly configured similarity validatior catches name""" data = { 'email': '*****@*****.**', 'password1': 'johndoesmith', 'password2': 'johndoesmith', 'short_name': 'John', 'full_name': 'John Smith', } form = UserCreationForm(data) self.assertFalse(form.is_valid()) self.assertEqual(form['password1'].errors, ['The password is too similar to the full name.'])
def test_password_verification(self): """The verification password is incorrect.""" data = { "email": "*****@*****.**", "password1": "test123", "password2": "test", } form = UserCreationForm(data) self.assertFalse(form.is_valid()) self.assertEqual( form["password1"].errors, [str(form.error_messages["password_mismatch"])], )
def test_validates_password_similarity_length(self): """Test misconfigured-similarity validator catches email""" data = { "email": "*****@*****.**", "password1": "jsmith", "password2": "jsmith", } form = UserCreationForm(data) self.assertFalse(form.is_valid()) self.assertIn( "The password is too similar to the email address.", form["password1"].errors, )
def test_validates_password_with_all_data(self): """Test correctly configured similarity validatior catches name""" data = { "email": "*****@*****.**", "password1": "johndoesmith", "password2": "johndoesmith", "short_name": "John", "full_name": "John Smith", } form = UserCreationForm(data) self.assertFalse(form.is_valid()) self.assertEqual( form["password1"].errors, ["The password is too similar to the full name."], )
def test_common_password(self): """Ensure form works with Password Validation""" data = { 'email': '*****@*****.**', 'password1': 'password', 'password2': 'password', } form = UserCreationForm(data) self.assertFalse(form.is_valid()) self.assertEqual(len(form['password1'].errors), 2) self.assertIn('This password is too common.', form['password1'].errors) self.assertIn( 'This password is too short. ' 'It must contain at least 12 characters.', form['password1'].errors)
def test_success_pre_19(self): """Successful submission of form data""" data = { 'email': '*****@*****.**', 'full_name': 'John Smith', # optional field 'short_name': 'John', # optional field 'password1': 'test123', 'password2': 'test123', } form = UserCreationForm(data) self.assertTrue(form.is_valid()) user = form.save() self.assertEqual(repr(user), '<User: [email protected]>') self.assertEqual(user.get_short_name(), 'John') self.assertEqual(user.get_full_name(), 'John Smith') self.assertTrue(user.check_password('test123'))
def test_password_help_text(self): """Ensure UserAttributeSimilarityValidator help text is shown""" form = UserCreationForm() self.assertEqual( form.fields['password1'].help_text, '<ul><li>Your password can't be too similar to' ' your other personal information.</li></ul>')
def test_password_whitespace_not_stripped(self): """Ensure we aren't mangling passwords by removing whitespaces Starting in Django 1.9, form Charfield and subclasses allow for whitestrip to be stripped automatically during the clean field phase. This is a test to ensure that we are not stripping whitespace from the password. """ data = { 'email': '*****@*****.**', 'password1': ' test password ', 'password2': ' test password ', } form = UserCreationForm(data) self.assertTrue(form.is_valid()) self.assertEqual(form.cleaned_data['password1'], data['password1']) self.assertEqual(form.cleaned_data['password2'], data['password2'])
def test_common_password(self): """Ensure form works with Password Validation""" data = { "email": "*****@*****.**", "password1": "password", "password2": "password", } form = UserCreationForm(data) self.assertFalse(form.is_valid()) self.assertEqual(len(form["password1"].errors), 2) self.assertIn("This password is too common.", form["password1"].errors) self.assertIn( "This password is too short. " "It must contain at least 12 characters.", form["password1"].errors, )
def test_password_help_text(self): """Ensure UserAttributeSimilarityValidator help text is shown""" form = UserCreationForm() if DJANGO_VERSION >= (3, 0): self.assertEqual( form.fields["password1"].help_text, "<ul><li>Your password can’t be too similar to" " your other personal information.</li></ul>", ) else: self.assertEqual( form.fields["password1"].help_text, "<ul><li>Your password can't be too similar to" " your other personal information.</li></ul>", )
def test_both_passwords(self): """One (or both) passwords weren't given""" data = {"email": "*****@*****.**"} form = UserCreationForm(data) required_error = [str(Field.default_error_messages["required"])] self.assertFalse(form.is_valid()) self.assertEqual(form["password1"].errors, required_error) self.assertEqual(form["password2"].errors, required_error) data["password2"] = "test123" form = UserCreationForm(data) self.assertFalse(form.is_valid()) self.assertEqual(form["password1"].errors, required_error) self.assertEqual(form["password2"].errors, [])
def test_both_passwords(self): """One (or both) passwords weren't given""" data = {'email': '*****@*****.**'} form = UserCreationForm(data) required_error = [str(Field.default_error_messages['required'])] self.assertFalse(form.is_valid()) self.assertEqual(form['password1'].errors, required_error) self.assertEqual(form['password2'].errors, required_error) data['password2'] = 'test123' form = UserCreationForm(data) self.assertFalse(form.is_valid()) self.assertEqual(form['password1'].errors, required_error) self.assertEqual(form['password2'].errors, [])
def test_success(self, password_changed): """Successful submission of form data""" data = { "email": "*****@*****.**", "full_name": "John Smith", # optional field "short_name": "John", # optional field "password1": "test123", "password2": "test123", } form = UserCreationForm(data) self.assertTrue(form.is_valid()) form.save(commit=False) self.assertEqual(password_changed.call_count, 0) user = form.save() self.assertEqual(password_changed.call_count, 1) self.assertEqual(repr(user), "<User: [email protected]>") self.assertEqual(user.get_short_name(), "John") self.assertEqual(user.get_full_name(), "John Smith") self.assertTrue(user.check_password("test123"))