Commit e23dcd76e7e7023f8fa9fc7b13b4a5bdc97c2e88

Authored by Yann VERY
1 parent 1031f6c9
Exists in master

Fixes #981 disable user validations when reset password

Showing 2 changed files with 21 additions and 0 deletions   Show diff stats
app/models/user.rb
... ... @@ -81,6 +81,15 @@ class User
81 81 :auth_token
82 82 end
83 83  
  84 + def reset_password(new_password, new_password_confirmation)
  85 + self.password = new_password
  86 + self.password_confirmation = new_password_confirmation
  87 +
  88 + self.class.validators_on(:password).map { |v| v.validate_each(self, :password, password) }
  89 + return false if errors.any?
  90 + save(validate: false)
  91 + end
  92 +
84 93 private def generate_authentication_token
85 94 loop do
86 95 token = Devise.friendly_token
... ...
spec/models/user_spec.rb
... ... @@ -35,6 +35,18 @@ describe User do
35 35 user2.save
36 36 expect(user2).to be_valid
37 37 end
  38 +
  39 + it "disables validations when reset password" do
  40 + user = Fabricate.build(:user, email: '')
  41 + user.save(validate: false)
  42 + expect(user.reset_password('Password123', 'Password123')).to be_truthy
  43 + end
  44 +
  45 + it 'should require a password with minimum of 6 characters' do
  46 + user = Fabricate.build(:user)
  47 + user.reset_password('12345', '12345')
  48 + expect(user.errors[:password]).to include("is too short (minimum is 6 characters)", "is too short (minimum is 6 characters)")
  49 + end
38 50 end
39 51  
40 52 context "First user" do
... ...