Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brianhempel/d49bce6f96a40f273c78 to your computer and use it in GitHub Desktop.
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
# 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