From e23dcd76e7e7023f8fa9fc7b13b4a5bdc97c2e88 Mon Sep 17 00:00:00 2001 From: Yann VERY Date: Thu, 3 Dec 2015 15:17:34 +0100 Subject: [PATCH] Fixes #981 disable user validations when reset password --- app/models/user.rb | 9 +++++++++ spec/models/user_spec.rb | 12 ++++++++++++ 2 files changed, 21 insertions(+), 0 deletions(-) diff --git a/app/models/user.rb b/app/models/user.rb index a58ae15..e6a2a16 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -81,6 +81,15 @@ class User :auth_token end + def reset_password(new_password, new_password_confirmation) + self.password = new_password + self.password_confirmation = new_password_confirmation + + self.class.validators_on(:password).map { |v| v.validate_each(self, :password, password) } + return false if errors.any? + save(validate: false) + end + private def generate_authentication_token loop do token = Devise.friendly_token diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index e31eee7..5cc66f6 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -35,6 +35,18 @@ describe User do user2.save expect(user2).to be_valid end + + it "disables validations when reset password" do + user = Fabricate.build(:user, email: '') + user.save(validate: false) + expect(user.reset_password('Password123', 'Password123')).to be_truthy + end + + it 'should require a password with minimum of 6 characters' do + user = Fabricate.build(:user) + user.reset_password('12345', '12345') + expect(user.errors[:password]).to include("is too short (minimum is 6 characters)", "is too short (minimum is 6 characters)") + end end context "First user" do -- libgit2 0.21.2