Created
May 16, 2014 17:07
-
-
Save brianhempel/d49bce6f96a40f273c78 to your computer and use it in GitHub Desktop.
Has_secure_password `validate :password, if: :present?` not working on an existing record
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Activate the gem you are reporting the issue against. | |
gem 'activerecord', '4.1.1' | |
require 'sqlite3' | |
require 'active_record' | |
require 'minitest/autorun' | |
require 'logger' | |
# Ensure backward compatibility with Minitest 4 | |
Minitest::Test = MiniTest::Unit::TestCase unless defined?(Minitest::Test) | |
# This connection will do for database-independent bug reports. | |
ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:') | |
ActiveRecord::Base.logger = Logger.new(STDOUT) | |
ActiveRecord::Schema.define do | |
create_table :users do |t| | |
t.string :password_digest | |
end | |
end | |
class User < ActiveRecord::Base | |
has_secure_password | |
validates :password, length: { minimum: 8 }, if: :present? | |
end | |
class HasSecurePasswordPasswordValidationBugTest < Minitest::Test | |
def test_has_secure_password_validates_if_present_correctly | |
user = User.create!(password: "password") | |
assert user.valid? # passes | |
assert user.reload.valid? # passes | |
user = User.last | |
assert user.valid? # fails | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment