2016-08-06 12:38:23 -04:00
|
|
|
require "cases/helper"
|
|
|
|
require "models/user"
|
|
|
|
require "models/visitor"
|
2010-12-18 16:38:05 -05:00
|
|
|
|
|
|
|
class SecurePasswordTest < ActiveModel::TestCase
|
|
|
|
setup do
|
2014-03-11 03:34:51 -04:00
|
|
|
# Used only to speed up tests
|
2014-03-10 09:22:22 -04:00
|
|
|
@original_min_cost = ActiveModel::SecurePassword.min_cost
|
2012-11-19 20:07:12 -05:00
|
|
|
ActiveModel::SecurePassword.min_cost = true
|
|
|
|
|
2010-12-18 16:38:05 -05:00
|
|
|
@user = User.new
|
2012-05-08 18:54:47 -04:00
|
|
|
@visitor = Visitor.new
|
2014-01-20 08:06:03 -05:00
|
|
|
|
|
|
|
# Simulate loading an existing user from the DB
|
|
|
|
@existing_user = User.new
|
2016-08-06 12:38:23 -04:00
|
|
|
@existing_user.password_digest = BCrypt::Password.create("password", cost: BCrypt::Engine::MIN_COST)
|
2010-12-18 16:38:05 -05:00
|
|
|
end
|
|
|
|
|
2012-11-19 20:07:12 -05:00
|
|
|
teardown do
|
2014-03-10 09:22:22 -04:00
|
|
|
ActiveModel::SecurePassword.min_cost = @original_min_cost
|
2012-11-19 20:07:12 -05:00
|
|
|
end
|
|
|
|
|
2014-07-02 17:39:41 -04:00
|
|
|
test "automatically include ActiveModel::Validations when validations are enabled" do
|
2014-07-02 17:21:12 -04:00
|
|
|
assert_respond_to @user, :valid?
|
|
|
|
end
|
|
|
|
|
2014-07-02 17:39:41 -04:00
|
|
|
test "don't include ActiveModel::Validations when validations are disabled" do
|
|
|
|
assert_not_respond_to @visitor, :valid?
|
2011-04-14 17:54:25 -04:00
|
|
|
end
|
|
|
|
|
2014-06-14 03:39:43 -04:00
|
|
|
test "create a new user with validations and valid password/confirmation" do
|
2016-08-06 12:38:23 -04:00
|
|
|
@user.password = "password"
|
|
|
|
@user.password_confirmation = "password"
|
2014-06-14 03:39:43 -04:00
|
|
|
|
2016-08-06 12:38:23 -04:00
|
|
|
assert @user.valid?(:create), "user should be valid"
|
2014-06-14 03:39:43 -04:00
|
|
|
|
2016-08-06 12:38:23 -04:00
|
|
|
@user.password = "a" * 72
|
|
|
|
@user.password_confirmation = "a" * 72
|
2014-06-14 03:39:43 -04:00
|
|
|
|
2016-08-06 12:38:23 -04:00
|
|
|
assert @user.valid?(:create), "user should be valid"
|
2014-06-14 03:39:43 -04:00
|
|
|
end
|
|
|
|
|
2014-08-06 10:41:36 -04:00
|
|
|
test "create a new user with validation and a spaces only password" do
|
2016-08-06 12:38:23 -04:00
|
|
|
@user.password = " " * 72
|
|
|
|
assert @user.valid?(:create), "user should be valid"
|
2014-08-06 10:41:36 -04:00
|
|
|
end
|
|
|
|
|
2014-01-20 08:06:03 -05:00
|
|
|
test "create a new user with validation and a blank password" do
|
2016-08-06 12:38:23 -04:00
|
|
|
@user.password = ""
|
|
|
|
assert !@user.valid?(:create), "user should be invalid"
|
2014-01-20 08:06:03 -05:00
|
|
|
assert_equal 1, @user.errors.count
|
|
|
|
assert_equal ["can't be blank"], @user.errors[:password]
|
|
|
|
end
|
|
|
|
|
|
|
|
test "create a new user with validation and a nil password" do
|
|
|
|
@user.password = nil
|
2016-08-06 12:38:23 -04:00
|
|
|
assert !@user.valid?(:create), "user should be invalid"
|
2014-01-20 08:06:03 -05:00
|
|
|
assert_equal 1, @user.errors.count
|
|
|
|
assert_equal ["can't be blank"], @user.errors[:password]
|
|
|
|
end
|
|
|
|
|
2016-08-06 12:38:23 -04:00
|
|
|
test "create a new user with validation and password length greater than 72" do
|
|
|
|
@user.password = "a" * 73
|
|
|
|
@user.password_confirmation = "a" * 73
|
|
|
|
assert !@user.valid?(:create), "user should be invalid"
|
2014-06-14 03:05:31 -04:00
|
|
|
assert_equal 1, @user.errors.count
|
|
|
|
assert_equal ["is too long (maximum is 72 characters)"], @user.errors[:password]
|
|
|
|
end
|
|
|
|
|
2014-01-20 08:06:03 -05:00
|
|
|
test "create a new user with validation and a blank password confirmation" do
|
2016-08-06 12:38:23 -04:00
|
|
|
@user.password = "password"
|
|
|
|
@user.password_confirmation = ""
|
|
|
|
assert !@user.valid?(:create), "user should be invalid"
|
2014-01-20 08:06:03 -05:00
|
|
|
assert_equal 1, @user.errors.count
|
|
|
|
assert_equal ["doesn't match Password"], @user.errors[:password_confirmation]
|
2012-04-24 13:16:01 -04:00
|
|
|
end
|
|
|
|
|
2014-01-20 08:06:03 -05:00
|
|
|
test "create a new user with validation and a nil password confirmation" do
|
2016-08-06 12:38:23 -04:00
|
|
|
@user.password = "password"
|
2014-01-20 08:06:03 -05:00
|
|
|
@user.password_confirmation = nil
|
2016-08-06 12:38:23 -04:00
|
|
|
assert @user.valid?(:create), "user should be valid"
|
2010-12-18 16:38:05 -05:00
|
|
|
end
|
2010-12-19 04:39:54 -05:00
|
|
|
|
2014-01-20 08:06:03 -05:00
|
|
|
test "create a new user with validation and an incorrect password confirmation" do
|
2016-08-06 12:38:23 -04:00
|
|
|
@user.password = "password"
|
|
|
|
@user.password_confirmation = "something else"
|
|
|
|
assert !@user.valid?(:create), "user should be invalid"
|
2014-01-20 08:06:03 -05:00
|
|
|
assert_equal 1, @user.errors.count
|
|
|
|
assert_equal ["doesn't match Password"], @user.errors[:password_confirmation]
|
|
|
|
end
|
2010-12-19 04:39:54 -05:00
|
|
|
|
2014-01-20 08:06:03 -05:00
|
|
|
test "update an existing user with validation and no change in password" do
|
2016-08-06 12:38:23 -04:00
|
|
|
assert @existing_user.valid?(:update), "user should be valid"
|
2014-01-20 08:06:03 -05:00
|
|
|
end
|
2010-12-19 04:39:54 -05:00
|
|
|
|
2014-06-14 03:39:43 -04:00
|
|
|
test "update an existing user with validations and valid password/confirmation" do
|
2016-08-06 12:38:23 -04:00
|
|
|
@existing_user.password = "password"
|
|
|
|
@existing_user.password_confirmation = "password"
|
2014-06-14 03:39:43 -04:00
|
|
|
|
2016-08-06 12:38:23 -04:00
|
|
|
assert @existing_user.valid?(:update), "user should be valid"
|
2014-06-14 03:39:43 -04:00
|
|
|
|
2016-08-06 12:38:23 -04:00
|
|
|
@existing_user.password = "a" * 72
|
|
|
|
@existing_user.password_confirmation = "a" * 72
|
2014-06-14 03:39:43 -04:00
|
|
|
|
2016-08-06 12:38:23 -04:00
|
|
|
assert @existing_user.valid?(:update), "user should be valid"
|
2014-06-14 03:39:43 -04:00
|
|
|
end
|
|
|
|
|
2014-01-20 08:06:03 -05:00
|
|
|
test "updating an existing user with validation and a blank password" do
|
2016-08-06 12:38:23 -04:00
|
|
|
@existing_user.password = ""
|
|
|
|
assert @existing_user.valid?(:update), "user should be valid"
|
2010-12-18 16:38:05 -05:00
|
|
|
end
|
2010-12-19 04:39:54 -05:00
|
|
|
|
2014-08-06 10:41:36 -04:00
|
|
|
test "updating an existing user with validation and a spaces only password" do
|
2016-08-06 12:38:23 -04:00
|
|
|
@user.password = " " * 72
|
|
|
|
assert @user.valid?(:update), "user should be valid"
|
2014-08-06 10:41:36 -04:00
|
|
|
end
|
|
|
|
|
2014-01-20 08:06:03 -05:00
|
|
|
test "updating an existing user with validation and a blank password and password_confirmation" do
|
2016-08-06 12:38:23 -04:00
|
|
|
@existing_user.password = ""
|
|
|
|
@existing_user.password_confirmation = ""
|
|
|
|
assert @existing_user.valid?(:update), "user should be valid"
|
2014-01-20 08:06:03 -05:00
|
|
|
end
|
2010-12-19 04:39:54 -05:00
|
|
|
|
2014-01-20 08:06:03 -05:00
|
|
|
test "updating an existing user with validation and a nil password" do
|
|
|
|
@existing_user.password = nil
|
2016-08-06 12:38:23 -04:00
|
|
|
assert !@existing_user.valid?(:update), "user should be invalid"
|
2014-01-20 08:06:03 -05:00
|
|
|
assert_equal 1, @existing_user.errors.count
|
|
|
|
assert_equal ["can't be blank"], @existing_user.errors[:password]
|
|
|
|
end
|
|
|
|
|
2016-08-06 12:38:23 -04:00
|
|
|
test "updating an existing user with validation and password length greater than 72" do
|
|
|
|
@existing_user.password = "a" * 73
|
|
|
|
@existing_user.password_confirmation = "a" * 73
|
|
|
|
assert !@existing_user.valid?(:update), "user should be invalid"
|
2014-06-14 03:05:31 -04:00
|
|
|
assert_equal 1, @existing_user.errors.count
|
|
|
|
assert_equal ["is too long (maximum is 72 characters)"], @existing_user.errors[:password]
|
|
|
|
end
|
|
|
|
|
2014-01-20 08:06:03 -05:00
|
|
|
test "updating an existing user with validation and a blank password confirmation" do
|
2016-08-06 12:38:23 -04:00
|
|
|
@existing_user.password = "password"
|
|
|
|
@existing_user.password_confirmation = ""
|
|
|
|
assert !@existing_user.valid?(:update), "user should be invalid"
|
2014-01-20 08:06:03 -05:00
|
|
|
assert_equal 1, @existing_user.errors.count
|
|
|
|
assert_equal ["doesn't match Password"], @existing_user.errors[:password_confirmation]
|
2010-12-18 16:38:05 -05:00
|
|
|
end
|
2011-01-25 21:35:02 -05:00
|
|
|
|
2014-01-20 08:06:03 -05:00
|
|
|
test "updating an existing user with validation and a nil password confirmation" do
|
2016-08-06 12:38:23 -04:00
|
|
|
@existing_user.password = "password"
|
2014-01-20 08:06:03 -05:00
|
|
|
@existing_user.password_confirmation = nil
|
2016-08-06 12:38:23 -04:00
|
|
|
assert @existing_user.valid?(:update), "user should be valid"
|
2012-05-08 18:54:47 -04:00
|
|
|
end
|
2012-11-14 10:42:54 -05:00
|
|
|
|
2014-01-20 08:06:03 -05:00
|
|
|
test "updating an existing user with validation and an incorrect password confirmation" do
|
2016-08-06 12:38:23 -04:00
|
|
|
@existing_user.password = "password"
|
|
|
|
@existing_user.password_confirmation = "something else"
|
|
|
|
assert !@existing_user.valid?(:update), "user should be invalid"
|
2014-01-20 08:06:03 -05:00
|
|
|
assert_equal 1, @existing_user.errors.count
|
|
|
|
assert_equal ["doesn't match Password"], @existing_user.errors[:password_confirmation]
|
|
|
|
end
|
|
|
|
|
|
|
|
test "updating an existing user with validation and a blank password digest" do
|
2016-08-06 12:38:23 -04:00
|
|
|
@existing_user.password_digest = ""
|
|
|
|
assert !@existing_user.valid?(:update), "user should be invalid"
|
2014-01-20 08:06:03 -05:00
|
|
|
assert_equal 1, @existing_user.errors.count
|
|
|
|
assert_equal ["can't be blank"], @existing_user.errors[:password]
|
|
|
|
end
|
|
|
|
|
|
|
|
test "updating an existing user with validation and a nil password digest" do
|
|
|
|
@existing_user.password_digest = nil
|
2016-08-06 12:38:23 -04:00
|
|
|
assert !@existing_user.valid?(:update), "user should be invalid"
|
2014-01-20 08:06:03 -05:00
|
|
|
assert_equal 1, @existing_user.errors.count
|
|
|
|
assert_equal ["can't be blank"], @existing_user.errors[:password]
|
|
|
|
end
|
|
|
|
|
|
|
|
test "setting a blank password should not change an existing password" do
|
2016-08-06 12:38:23 -04:00
|
|
|
@existing_user.password = ""
|
|
|
|
assert @existing_user.password_digest == "password"
|
2014-01-20 08:06:03 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
test "setting a nil password should clear an existing password" do
|
|
|
|
@existing_user.password = nil
|
|
|
|
assert_equal nil, @existing_user.password_digest
|
2014-03-07 12:59:57 -05:00
|
|
|
end
|
2014-01-20 08:06:03 -05:00
|
|
|
|
|
|
|
test "authenticate" do
|
|
|
|
@user.password = "secret"
|
|
|
|
|
|
|
|
assert !@user.authenticate("wrong")
|
|
|
|
assert @user.authenticate("secret")
|
2012-07-31 16:16:21 -04:00
|
|
|
end
|
2012-11-14 10:42:54 -05:00
|
|
|
|
2012-11-19 20:07:12 -05:00
|
|
|
test "Password digest cost defaults to bcrypt default cost when min_cost is false" do
|
|
|
|
ActiveModel::SecurePassword.min_cost = false
|
|
|
|
|
2014-03-10 09:22:22 -04:00
|
|
|
@user.password = "secret"
|
|
|
|
assert_equal BCrypt::Engine::DEFAULT_COST, @user.password_digest.cost
|
2012-11-14 10:42:54 -05:00
|
|
|
end
|
|
|
|
|
2013-09-23 14:46:41 -04:00
|
|
|
test "Password digest cost honors bcrypt cost attribute when min_cost is false" do
|
2014-03-07 12:59:57 -05:00
|
|
|
begin
|
2014-03-10 09:22:22 -04:00
|
|
|
original_bcrypt_cost = BCrypt::Engine.cost
|
|
|
|
ActiveModel::SecurePassword.min_cost = false
|
|
|
|
BCrypt::Engine.cost = 5
|
|
|
|
|
2014-03-07 12:59:57 -05:00
|
|
|
@user.password = "secret"
|
|
|
|
assert_equal BCrypt::Engine.cost, @user.password_digest.cost
|
|
|
|
ensure
|
2014-03-10 09:22:22 -04:00
|
|
|
BCrypt::Engine.cost = original_bcrypt_cost
|
2014-03-07 12:59:57 -05:00
|
|
|
end
|
2013-09-23 14:46:41 -04:00
|
|
|
end
|
|
|
|
|
2012-11-14 10:42:54 -05:00
|
|
|
test "Password digest cost can be set to bcrypt min cost to speed up tests" do
|
|
|
|
ActiveModel::SecurePassword.min_cost = true
|
2012-11-19 20:07:12 -05:00
|
|
|
|
2014-03-10 09:22:22 -04:00
|
|
|
@user.password = "secret"
|
|
|
|
assert_equal BCrypt::Engine::MIN_COST, @user.password_digest.cost
|
2012-11-14 10:42:54 -05:00
|
|
|
end
|
2010-12-19 11:58:14 -05:00
|
|
|
end
|