2009-09-18 08:01:29 -04:00
|
|
|
require 'test_helper'
|
|
|
|
|
2009-09-20 09:35:26 -04:00
|
|
|
class PerishableTest < ActiveSupport::TestCase
|
2009-09-18 08:01:29 -04:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2009-09-18 09:20:45 -04:00
|
|
|
test 'should reset perisable token each time' do
|
|
|
|
user = new_user
|
|
|
|
3.times do
|
|
|
|
token = user.perishable_token
|
|
|
|
user.reset_perishable_token
|
|
|
|
assert_not_equal token, user.perishable_token
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'should reset perishable token and save the record' do
|
|
|
|
user = new_user
|
|
|
|
user.reset_perishable_token!
|
|
|
|
assert !user.new_record?
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'should save without validations when reseting perisable token' do
|
|
|
|
user = new_user
|
|
|
|
user.expects(:valid?).never
|
|
|
|
user.reset_perishable_token!
|
|
|
|
end
|
|
|
|
|
2009-09-18 08:01:29 -04:00
|
|
|
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')
|
2009-09-18 11:03:41 -04:00
|
|
|
expected_token = ::Digest::SHA1.hexdigest("--#{now.utc}--random_string--123456--")
|
2009-09-18 08:01:29 -04:00
|
|
|
user = create_user
|
|
|
|
assert_equal expected_token, user.perishable_token
|
|
|
|
end
|
|
|
|
end
|