Commit 1453287a5b6c7adbcbb3f157d262bca830b8c984
Exists in
master
Merge pull request #1003 from yannvery/reset-password-validations
Disable user validation 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,6 +81,15 @@ class User | ||
| 81 | :auth_token | 81 | :auth_token |
| 82 | end | 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 | private def generate_authentication_token | 93 | private def generate_authentication_token |
| 85 | loop do | 94 | loop do |
| 86 | token = Devise.friendly_token | 95 | token = Devise.friendly_token |
spec/models/user_spec.rb
| @@ -35,6 +35,18 @@ describe User do | @@ -35,6 +35,18 @@ describe User do | ||
| 35 | user2.save | 35 | user2.save |
| 36 | expect(user2).to be_valid | 36 | expect(user2).to be_valid |
| 37 | end | 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 | end | 50 | end |
| 39 | 51 | ||
| 40 | context "First user" do | 52 | context "First user" do |