def test_with_field_name_and_password_matches(self): """Testing validate_old_password with explicit field_name and password matching User's current password """ form = self.DummyForm({ 'alt_password': '******', }) validate_old_password(form, self.user, field_name='alt_password') self.assertNotIn('alt_password', form.errors)
def test_with_password_matches(self): """Testing validate_old_password with password matching User's current password """ form = self.DummyForm({ 'password': '******', }) validate_old_password(form, self.user) self.assertNotIn('password', form.errors)
def test_with_field_name_and_password_does_not_match(self): """Testing validate_old_password with explicit field name and password not matching User's current password """ form = self.DummyForm({ 'alt_password': '******', }) validate_old_password(form, self.user, field_name='alt_password') self.assertIn('alt_password', form.errors) self.assertEqual(form.errors['alt_password'], ['Incorrect password.'])
def test_with_existing_errors(self): """Testing validate_old_password with field already containing errors """ form = self.DummyForm({ 'password': '******', }) form.errors['password'] = form.error_class(['Something went wrong.']) validate_old_password(form, self.user) self.assertIn('password', form.errors) self.assertEqual(form.errors['password'], ['Something went wrong.'])
def test_with_password_does_not_match(self): """Testing validate_old_password with password not matching User's current password """ form = self.DummyForm({ 'password': '******', }) validate_old_password(form, self.user) self.assertIn('password', form.errors) self.assertEqual(form.errors['password'], ['Incorrect password.'])
def do_change_password(request): form = ChangePasswordForm(request.POST) form.full_clean() validate_old_password(form, request.user, 'old_password') if not form.errors: # XXX Compatibility with Django 0.96 and 1.0 formdata = getattr(form, "cleaned_data", getattr(form, "clean_data", None)) request.user.set_password(formdata['new_password1']) request.user.save() request.user.message_set.create(message="Your password was changed successfully.") return form
def test_with_field_name_and_existing_errors(self): """Testing validate_old_password with explicit field name and field already containing errors """ form = self.DummyForm({ 'alt_password': '******', }) form.errors['alt_password'] = form.error_class( ['Something went wrong.']) validate_old_password(form, self.user, field_name='alt_password') self.assertIn('alt_password', form.errors) self.assertEqual(form.errors['alt_password'], ['Something went wrong.'])
def do_change_password(request): form = ChangePasswordForm(request.POST) form.full_clean() validate_old_password(form, request.user, 'old_password') if not form.errors: # XXX Compatibility with Django 0.96 and 1.0 formdata = getattr(form, "cleaned_data", getattr(form, "clean_data", None)) request.user.set_password(formdata['new_password1']) request.user.save() request.user.message_set.create( message="Your password was changed successfully.") return form