mirror of
https://github.com/heartcombo/devise.git
synced 2026-01-09 14:58:05 -05:00
Moving perishable token into separated module.
This commit is contained in:
@@ -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