From 86a48b4da3cbd925d30b0fbe472edbda7171ea9e Mon Sep 17 00:00:00 2001 From: Unathi Chonco Date: Wed, 12 Oct 2016 01:01:01 +0800 Subject: This addition will now allow configuring an attribute name for the existing `#has_secure_password`. This can be useful when one would like to store some secure field as a digest, just like a password. The method still defaults to `password`. It now also allows using the same `#authenticate` method which now accepts a second argument for specifying the attribute to be authenticated, or will default to 'password`. A new method is also added for generating a new token for an attribute by calling `#regenerate_XXXX` where `XXXX` is the attribute name. --- activemodel/test/cases/secure_password_test.rb | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'activemodel/test/cases') diff --git a/activemodel/test/cases/secure_password_test.rb b/activemodel/test/cases/secure_password_test.rb index 9423df2c09..abbd182f66 100644 --- a/activemodel/test/cases/secure_password_test.rb +++ b/activemodel/test/cases/secure_password_test.rb @@ -184,9 +184,13 @@ class SecurePasswordTest < ActiveModel::TestCase test "authenticate" do @user.password = "secret" + @user.activation_token = "new_token" assert !@user.authenticate("wrong") assert @user.authenticate("secret") + + assert !@user.authenticate("wrong", :activation_token) + assert @user.authenticate("new_token", :activation_token) end test "Password digest cost defaults to bcrypt default cost when min_cost is false" do @@ -215,4 +219,13 @@ class SecurePasswordTest < ActiveModel::TestCase @user.password = "secret" assert_equal BCrypt::Engine::MIN_COST, @user.password_digest.cost end + + test "regenerate attribute method" do + old_digest = @user.activation_token_digest + @user.regenerate_activation_token + + assert_not_nil @user.activation_token + assert_not_nil @user.activation_token_digest + assert_not_equal old_digest, @user.activation_token_digest + end end -- cgit v1.2.3 From 9b63bf1dfda36c61802165b2683761d2bb0d2110 Mon Sep 17 00:00:00 2001 From: Unathi Chonco Date: Wed, 12 Oct 2016 09:01:57 +0800 Subject: Remove method for regenerating a token, and update `#authenticate`. This change now creates a method `#authenticate_XXX` where XXX is the configured attribute name on `#has_secure_password`. `#authenticate` is now an alias to this method when the attribute name is the default 'password' --- activemodel/test/cases/secure_password_test.rb | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) (limited to 'activemodel/test/cases') diff --git a/activemodel/test/cases/secure_password_test.rb b/activemodel/test/cases/secure_password_test.rb index abbd182f66..279a908aeb 100644 --- a/activemodel/test/cases/secure_password_test.rb +++ b/activemodel/test/cases/secure_password_test.rb @@ -189,8 +189,8 @@ class SecurePasswordTest < ActiveModel::TestCase assert !@user.authenticate("wrong") assert @user.authenticate("secret") - assert !@user.authenticate("wrong", :activation_token) - assert @user.authenticate("new_token", :activation_token) + assert !@user.authenticate_activation_token("wrong") + assert @user.authenticate_activation_token("new_token") end test "Password digest cost defaults to bcrypt default cost when min_cost is false" do @@ -219,13 +219,4 @@ class SecurePasswordTest < ActiveModel::TestCase @user.password = "secret" assert_equal BCrypt::Engine::MIN_COST, @user.password_digest.cost end - - test "regenerate attribute method" do - old_digest = @user.activation_token_digest - @user.regenerate_activation_token - - assert_not_nil @user.activation_token - assert_not_nil @user.activation_token_digest - assert_not_equal old_digest, @user.activation_token_digest - end end -- cgit v1.2.3