def change(): _form_title = 'User' _template = 'add-form.html' _func_name = 'user.change' _form_seq = [ ['old_password'], ['new_password','retry_password'] ] user = User.query.filter_by(id=current_user.id).first_or_404() form = ChangePasswordForm() if request.method == 'POST' and form.validate(): if not bcrypt.check_password_hash(user.password, form.old_password.data): flash('Wrong Old Password.') return redirect(url_for(_func_name)) if not (form.new_password.data == form.retry_password.data): flash('Password did not match') return redirect(url_for(_func_name)) else: user.password = bcrypt.generate_password_hash(form.new_password.data) db.session.add(user) db.session.commit() flash('Password Successfully Change.') return redirect(url_for(_func_name)) return render_template(_template, form=form, form_title = _form_title, form_seq = _form_seq)
def test_validate_invalid_change_password_format(self): # Ensure invalid email format throws error. form = ChangePasswordForm(password='******', confirm='123') self.assertFalse(form.validate())
def test_validate_invalid_change_password(self): # Ensure passwords must match. form = ChangePasswordForm(password='******', confirm='unknown') self.assertFalse(form.validate())
def test_validate_success_change_password_form(self): # Ensure correct data validates. form = ChangePasswordForm(password='******', confirm='update') self.assertTrue(form.validate())
def test_check_invalid_change_password(self): """ Tests that passwords must match when chaning password """ form = ChangePasswordForm(password='******', confirm='unknown') self.assertFalse(form.validate())
def test_check_success_change_password(self): """ Tests that correct data changes the password. """ form = ChangePasswordForm(password='******', confirm='update') self.assertTrue(form.validate())
def test_check_invalid_change_password_format(self): """ Tests that invalid password format throws error. """ form = ChangePasswordForm(password='******', confirm='123') self.assertFalse(form.validate())