Skip to content

Commit 1351d54

Browse files
authored
Merge pull request rails#55232 from callmesangio/fix-gh-55225
`has_secure_password`: fix password validation.
2 parents 8380604 + 9fc0eff commit 1351d54

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

activemodel/lib/active_model/secure_password.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ def has_secure_password(attribute = :password, validations: true, reset_token: t
155155
end
156156
end
157157

158-
validates_confirmation_of attribute, allow_blank: true
158+
validates_confirmation_of attribute, allow_nil: true
159159
end
160160

161161
# Only generate tokens for records that are capable of doing so (Active Records, not vanilla Active Models)

activemodel/test/cases/secure_password_test.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,14 @@ class SecurePasswordTest < ActiveModel::TestCase
104104
assert_equal ["doesn't match Password"], @user.errors[:password_confirmation]
105105
end
106106

107+
test "create a new user with validation, a spaces only password, and an incorrect password confirmation" do
108+
@user.password = " "
109+
@user.password_confirmation = "something else"
110+
assert_not @user.valid?(:create), "user should be invalid"
111+
assert_equal 1, @user.errors.count
112+
assert_equal ["doesn't match Password"], @user.errors[:password_confirmation]
113+
end
114+
107115
test "resetting password to nil clears the password cache" do
108116
@user.password = "password"
109117
@user.password = nil
@@ -179,6 +187,14 @@ class SecurePasswordTest < ActiveModel::TestCase
179187
assert_equal ["doesn't match Password"], @existing_user.errors[:password_confirmation]
180188
end
181189

190+
test "updating an existing user with validation, a spaces only password, and an incorrect password confirmation" do
191+
@existing_user.password = " "
192+
@existing_user.password_confirmation = "something else"
193+
assert_not @existing_user.valid?(:update), "user should be invalid"
194+
assert_equal 1, @existing_user.errors.count
195+
assert_equal ["doesn't match Password"], @existing_user.errors[:password_confirmation]
196+
end
197+
182198
test "updating an existing user with validation and a correct password challenge" do
183199
@existing_user.password = "new password"
184200
@existing_user.password_challenge = "password"

0 commit comments

Comments
 (0)