allegroplanet / allegro-planet

1 stars 1 forks source link

Fix User Password Validation #35

Closed MarkOates closed 7 years ago

MarkOates commented 7 years ago

Problem

An odd thing occurs when attempting to resave a User after pulling it from the database. Validation on password fails:

user allegro planet admin 2017-03-06 22-05-43

Why

This peculiarity happens because has_secure_password actually does the validation and presence checks for password when it automatically generates the value for password_digest. However, after the password_digest is present, the password field is set to nil. As a result, any custom validations added on the model for password will fail after that point since password is set to nil. So in our case, it is ok to have a nil password after creation.

An article discussing the issue can be found here.

Solution

Add allow_nil: true to User#password validation. Also, add tests to ensure the expected validations occur on create and update.