Repository: rails/rails
File: activemodel/test/cases/secure_password_test.rb (Ruby)

Code before:
  test "Password digest cost can be set to bcrypt min cost to speed up tests" do
    ActiveModel::SecurePassword.min_cost = true

    @user.password = "secret"
    assert_equal BCrypt::Engine::MIN_COST, @user.password_digest.cost
  end

  test "password reset token" do
    assert_not @person.respond_to? :password_reset_token
    assert_equal "password_reset-token-900", @pilot.password_reset_token

    assert_equal "finding-for-password_reset-by-999", Pilot.find_by_password_reset_token("999")
    assert_equal "finding-for-password_reset-by-999!", Pilot.find_by_password_reset_token!("999")
  end

  test "password reset token duration" do
    assert_equal "password_reset-token-3600", @slow_pilot.password_reset_token
    assert_equal 1.hour, @slow_pilot.password_reset_token_expires_in
  end

  test "password algorithm defaults to bcrypt" do
    assert_equal :bcrypt, @user.password_algorithm
  end

  test "custom password algorithm is supported" do
    user_with_diff_algo = UserWithCustomAlgorithim.new

    user_with_diff_algo.password = "secret"
    assert_equal "hashed-secret", user_with_diff_algo.password_digest

    assert_equal false, user_with_diff_algo.authenticate("wrong")
    assert_equal user_with_diff_algo, user_with_diff_algo.authenticate("secret")

    assert_equal false, user_with_diff_algo.authenticate_password("wrong")
    assert_equal user_with_diff_algo, user_with_diff_algo.authenticate_password("secret")

    assert_equal "custom-salt", user_with_diff_algo.password_salt

    assert_equal :custom_algorithm, user_with_diff_algo.password_algorithm
  end
end


Review comment (suggestion):
```suggestion
    user_with_diff_algo = UserWithCustomAlgorithm.new
```

Code after:
  test "Password digest cost can be set to bcrypt min cost to speed up tests" do
    ActiveModel::SecurePassword.min_cost = true

    @user.password = "secret"
    assert_equal BCrypt::Engine::MIN_COST, @user.password_digest.cost
  end

  test "password reset token" do
    assert_not @person.respond_to? :password_reset_token
    assert_equal "password_reset-token-900", @pilot.password_reset_token

    assert_equal "finding-for-password_reset-by-999", Pilot.find_by_password_reset_token("999")
    assert_equal "finding-for-password_reset-by-999!", Pilot.find_by_password_reset_token!("999")
  end

  test "password reset token duration" do
    assert_equal "password_reset-token-3600", @slow_pilot.password_reset_token
    assert_equal 1.hour, @slow_pilot.password_reset_token_expires_in
  end

  test "password algorithm defaults to bcrypt" do
    assert_equal :bcrypt, @user.password_algorithm
  end

  test "custom password algorithm is supported" do
    user_with_diff_algo = UserWithCustomAlgorithm.new

    user_with_diff_algo.password = "secret"
    assert_equal "hashed-secret", user_with_diff_algo.password_digest

    assert_equal false, user_with_diff_algo.authenticate("wrong")
    assert_equal user_with_diff_algo, user_with_diff_algo.authenticate("secret")

    assert_equal false, user_with_diff_algo.authenticate_password("wrong")
    assert_equal user_with_diff_algo, user_with_diff_algo.authenticate_password("secret")

    assert_equal "custom-salt", user_with_diff_algo.password_salt

    assert_equal :custom_algorithm, user_with_diff_algo.password_algorithm
  end
end