mirror of
https://github.com/heartcombo/devise.git
synced 2026-01-10 23:38:10 -05:00
Moving perishable token into separated module.
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
require 'devise/authenticable'
|
||||
require 'devise/perishable_token'
|
||||
require 'devise/confirmable'
|
||||
|
||||
require 'devise/notifier'
|
||||
|
||||
@@ -4,8 +4,8 @@ module Devise
|
||||
def self.included(base)
|
||||
base.class_eval do
|
||||
extend ClassMethods
|
||||
include ::Devise::PerishableToken
|
||||
|
||||
before_create :generate_confirmation_token
|
||||
after_create :send_confirmation_instructions
|
||||
end
|
||||
end
|
||||
@@ -30,12 +30,6 @@ module Devise
|
||||
|
||||
private
|
||||
|
||||
# Generates a new random token for confirmation, based on actual Time and salt
|
||||
#
|
||||
def generate_confirmation_token
|
||||
self.confirmation_token = secure_digest(Time.now.utc, random_string, password)
|
||||
end
|
||||
|
||||
# Send confirmation instructions by email
|
||||
#
|
||||
def send_confirmation_instructions
|
||||
@@ -57,11 +51,11 @@ module Devise
|
||||
# If the user is already confirmed, create an error for the user
|
||||
#
|
||||
def find_and_confirm(confirmation_token)
|
||||
confirmable = find_or_initialize_by_confirmation_token(confirmation_token)
|
||||
confirmable = find_or_initialize_by_perishable_token(confirmation_token)
|
||||
unless confirmable.new_record?
|
||||
confirmable.confirm!
|
||||
else
|
||||
confirmable.errors.add(:confirmation_token, :invalid, :default => "invalid confirmation")
|
||||
confirmable.errors.add(:perishable_token, :invalid, :default => "invalid confirmation")
|
||||
end
|
||||
confirmable
|
||||
end
|
||||
|
||||
21
lib/devise/perishable_token.rb
Normal file
21
lib/devise/perishable_token.rb
Normal file
@@ -0,0 +1,21 @@
|
||||
module Devise
|
||||
module PerishableToken
|
||||
|
||||
def self.included(base)
|
||||
base.class_eval do
|
||||
# extend ClassMethods
|
||||
|
||||
before_create :generate_perishable_token
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
# Generates a new random token for confirmation, based on actual Time and salt
|
||||
#
|
||||
def generate_perishable_token
|
||||
self.perishable_token = secure_digest(Time.now.utc, random_string, password)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -8,45 +8,10 @@ class ConfirmableTest < ActiveSupport::TestCase
|
||||
setup_mailer
|
||||
end
|
||||
|
||||
test 'should not have confirmation code accessible' do
|
||||
assert_not field_accessible?(:confirmation_token)
|
||||
end
|
||||
|
||||
test 'should not have confirmed at accessible' do
|
||||
assert_not field_accessible?(:confirmed_at)
|
||||
end
|
||||
|
||||
test 'should generate confirmation token after creating a record' do
|
||||
assert_nil new_user.confirmation_token
|
||||
assert_not_nil create_user.confirmation_token
|
||||
end
|
||||
|
||||
test 'should generate a sha1 hash for confirmation token' do
|
||||
now = Time.now
|
||||
Time.stubs(:now).returns(now)
|
||||
User.any_instance.stubs(:random_string).returns('random_string')
|
||||
expected_token = ::Digest::SHA1.hexdigest("--#{now.utc}--random_string--12345--")
|
||||
user = create_user
|
||||
assert_equal expected_token, user.confirmation_token
|
||||
end
|
||||
|
||||
test 'should never generate the same confirmation_token for different users' do
|
||||
confirmation_tokens = []
|
||||
10.times do
|
||||
token = create_user.confirmation_token
|
||||
assert !confirmation_tokens.include?(token)
|
||||
confirmation_tokens << token
|
||||
end
|
||||
end
|
||||
|
||||
test 'should not change confirmation token when updating' do
|
||||
user = create_user
|
||||
token = user.confirmation_token
|
||||
user.expects(:confirmation_token=).never
|
||||
user.save!
|
||||
assert_equal token, user.confirmation_token
|
||||
end
|
||||
|
||||
test 'should confirm a user updating confirmed at' do
|
||||
user = create_user
|
||||
assert_nil user.confirmed_at
|
||||
@@ -73,23 +38,23 @@ class ConfirmableTest < ActiveSupport::TestCase
|
||||
|
||||
test 'should find and confirm an user automatically' do
|
||||
user = create_user
|
||||
confirmed_user = User.find_and_confirm(user.confirmation_token)
|
||||
confirmed_user = User.find_and_confirm(user.perishable_token)
|
||||
assert_not_nil confirmed_user
|
||||
assert_equal confirmed_user, user
|
||||
assert user.reload.confirmed?
|
||||
end
|
||||
|
||||
test 'should return a new user with errors if no user exists while trying to confirm' do
|
||||
confirmed_user = User.find_and_confirm('invalid_confirmation_token')
|
||||
confirmed_user = User.find_and_confirm('invalid_perishable_token')
|
||||
assert confirmed_user.new_record?
|
||||
assert_not_nil confirmed_user.errors[:confirmation_token]
|
||||
assert_equal "invalid confirmation", confirmed_user.errors[:confirmation_token]
|
||||
assert_not_nil confirmed_user.errors[:perishable_token]
|
||||
assert_equal "invalid confirmation", confirmed_user.errors[:perishable_token]
|
||||
end
|
||||
|
||||
test 'should generate errors for a user email if user is already confirmed' do
|
||||
user = create_user
|
||||
user.confirm!
|
||||
confirmed_user = User.find_and_confirm(user.confirmation_token)
|
||||
confirmed_user = User.find_and_confirm(user.perishable_token)
|
||||
assert confirmed_user.confirmed?
|
||||
assert confirmed_user.errors[:email]
|
||||
end
|
||||
|
||||
44
test/perishable_token_test.rb
Normal file
44
test/perishable_token_test.rb
Normal file
@@ -0,0 +1,44 @@
|
||||
require 'test_helper'
|
||||
|
||||
class PerishableTokenTest < ActiveSupport::TestCase
|
||||
|
||||
def setup
|
||||
User.send :include, ::Devise::PerishableToken unless User.included_modules.include?(::Devise::PerishableToken)
|
||||
end
|
||||
|
||||
test 'should not have perishable token accessible' do
|
||||
assert_not field_accessible?(:perishable_token)
|
||||
end
|
||||
|
||||
test 'should generate perishable token after creating a record' do
|
||||
assert_nil new_user.perishable_token
|
||||
assert_not_nil create_user.perishable_token
|
||||
end
|
||||
|
||||
test 'should never generate the same perishable token for different users' do
|
||||
perishable_tokens = []
|
||||
10.times do
|
||||
token = create_user.perishable_token
|
||||
assert !perishable_tokens.include?(token)
|
||||
perishable_tokens << token
|
||||
end
|
||||
end
|
||||
|
||||
test 'should not change perishable token when updating' do
|
||||
user = create_user
|
||||
token = user.perishable_token
|
||||
user.expects(:perishable_token=).never
|
||||
user.save!
|
||||
assert_equal token, user.perishable_token
|
||||
end
|
||||
|
||||
test 'should generate a sha1 hash for perishable token' do
|
||||
now = Time.now
|
||||
Time.stubs(:now).returns(now)
|
||||
User.any_instance.stubs(:random_string).returns('random_string')
|
||||
expected_token = ::Digest::SHA1.hexdigest("--#{now.utc}--random_string--12345--")
|
||||
user = create_user
|
||||
assert_equal expected_token, user.perishable_token
|
||||
end
|
||||
end
|
||||
|
||||
@@ -19,7 +19,7 @@ ActiveRecord::Schema.define(:version => 1) do
|
||||
t.string :email, :null => false
|
||||
t.string :encrypted_password, :null => false
|
||||
t.string :password_salt, :null => false
|
||||
t.string :confirmation_token
|
||||
t.string :perishable_token
|
||||
t.datetime :confirmed_at
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user