2010-03-26 06:27:19 -04:00
|
|
|
require 'test_helper'
|
2009-10-19 22:31:33 -04:00
|
|
|
|
2011-12-04 17:58:19 -05:00
|
|
|
class RememberableTest < ActiveSupport::TestCase
|
|
|
|
def resource_class
|
|
|
|
User
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_resource
|
|
|
|
create_user
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'remember_me should not generate a new token if using salt' do
|
|
|
|
user = create_user
|
|
|
|
user.expects(:valid?).never
|
|
|
|
user.remember_me!
|
2015-12-16 11:13:46 -05:00
|
|
|
assert user.remember_created_at
|
2011-12-04 17:58:19 -05:00
|
|
|
end
|
|
|
|
|
2016-05-15 10:48:12 -04:00
|
|
|
test 'remember_me should not generate a new token if valid token exists' do
|
|
|
|
user = create_user
|
|
|
|
user.singleton_class.send(:attr_accessor, :remember_token)
|
|
|
|
User.to_adapter.expects(:find_first).returns(nil)
|
|
|
|
|
|
|
|
user.remember_me!
|
|
|
|
existing_token = user.remember_token
|
|
|
|
|
|
|
|
user.remember_me!
|
|
|
|
assert_equal existing_token, user.remember_token
|
|
|
|
end
|
|
|
|
|
2011-12-04 17:58:19 -05:00
|
|
|
test 'forget_me should not clear remember token if using salt' do
|
|
|
|
user = create_user
|
|
|
|
user.remember_me!
|
|
|
|
user.expects(:valid?).never
|
|
|
|
user.forget_me!
|
|
|
|
end
|
|
|
|
|
2013-08-19 14:48:36 -04:00
|
|
|
test 'can generate remember token' do
|
|
|
|
user = create_user
|
|
|
|
user.singleton_class.send(:attr_accessor, :remember_token)
|
|
|
|
User.to_adapter.expects(:find_first).returns(nil)
|
|
|
|
user.remember_me!
|
|
|
|
assert user.remember_token
|
|
|
|
end
|
|
|
|
|
2011-12-04 17:58:19 -05:00
|
|
|
test 'serialize into cookie' do
|
|
|
|
user = create_user
|
|
|
|
user.remember_me!
|
2015-12-16 11:13:46 -05:00
|
|
|
id, token, date = User.serialize_into_cookie(user)
|
|
|
|
assert_equal id, user.to_key
|
|
|
|
assert_equal token, user.authenticatable_salt
|
2016-01-28 12:01:06 -05:00
|
|
|
assert date.is_a?(String)
|
2011-12-04 17:58:19 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
test 'serialize from cookie' do
|
|
|
|
user = create_user
|
|
|
|
user.remember_me!
|
2015-12-16 11:13:46 -05:00
|
|
|
assert_equal user, User.serialize_from_cookie(user.to_key, user.authenticatable_salt, Time.now.utc)
|
|
|
|
end
|
|
|
|
|
2016-01-28 12:01:06 -05:00
|
|
|
test 'serialize from cookie should accept a String with the datetime seconds and microseconds' do
|
|
|
|
user = create_user
|
|
|
|
user.remember_me!
|
|
|
|
assert_equal user, User.serialize_from_cookie(user.to_key, user.authenticatable_salt, Time.now.utc.to_f.to_json)
|
|
|
|
end
|
|
|
|
|
2016-01-25 05:17:05 -05:00
|
|
|
test 'serialize from cookie should return nil with invalid datetime' do
|
|
|
|
user = create_user
|
|
|
|
user.remember_me!
|
|
|
|
assert_nil User.serialize_from_cookie(user.to_key, user.authenticatable_salt, "2013")
|
|
|
|
end
|
|
|
|
|
2015-12-16 11:13:46 -05:00
|
|
|
test 'serialize from cookie should return nil if no resource is found' do
|
|
|
|
assert_nil resource_class.serialize_from_cookie([0], "123", Time.now.utc)
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'serialize from cookie should return nil if no timestamp' do
|
|
|
|
user = create_user
|
|
|
|
user.remember_me!
|
|
|
|
assert_nil User.serialize_from_cookie(user.to_key, user.authenticatable_salt)
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'serialize from cookie should return nil if timestamp is earlier than token creation' do
|
|
|
|
user = create_user
|
|
|
|
user.remember_me!
|
|
|
|
assert_nil User.serialize_from_cookie(user.to_key, user.authenticatable_salt, 1.day.ago)
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'serialize from cookie should return nil if timestamp is older than remember_for' do
|
|
|
|
user = create_user
|
|
|
|
user.remember_created_at = 1.month.ago
|
|
|
|
user.remember_me!
|
|
|
|
assert_nil User.serialize_from_cookie(user.to_key, user.authenticatable_salt, 3.weeks.ago)
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'serialize from cookie me return nil if is a valid resource with invalid token' do
|
|
|
|
user = create_user
|
|
|
|
user.remember_me!
|
|
|
|
assert_nil User.serialize_from_cookie(user.to_key, "123", Time.now.utc)
|
2011-12-04 17:58:19 -05:00
|
|
|
end
|
|
|
|
|
2015-05-26 09:04:27 -04:00
|
|
|
test 'raises a RuntimeError if authenticatable_salt is nil or empty' do
|
2011-12-04 17:58:19 -05:00
|
|
|
user = User.new
|
2015-05-26 09:04:27 -04:00
|
|
|
def user.authenticable_salt; nil; end
|
|
|
|
assert_raise RuntimeError do
|
|
|
|
user.rememberable_value
|
|
|
|
end
|
|
|
|
|
|
|
|
user = User.new
|
|
|
|
def user.authenticable_salt; ""; end
|
2011-12-04 17:58:19 -05:00
|
|
|
assert_raise RuntimeError do
|
|
|
|
user.rememberable_value
|
|
|
|
end
|
|
|
|
end
|
2009-10-19 22:31:33 -04:00
|
|
|
|
2010-09-25 06:37:06 -04:00
|
|
|
test 'should respond to remember_me attribute' do
|
|
|
|
assert resource_class.new.respond_to?(:remember_me)
|
|
|
|
assert resource_class.new.respond_to?(:remember_me=)
|
2009-10-19 22:31:33 -04:00
|
|
|
end
|
|
|
|
|
2014-04-04 14:45:52 -04:00
|
|
|
test 'forget_me should clear remember_created_at if expire_all_remember_me_on_sign_out is true' do
|
|
|
|
swap Devise, expire_all_remember_me_on_sign_out: true do
|
|
|
|
resource = create_resource
|
|
|
|
resource.remember_me!
|
|
|
|
assert_not_nil resource.remember_created_at
|
|
|
|
|
|
|
|
resource.forget_me!
|
|
|
|
assert_nil resource.remember_created_at
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'forget_me should not clear remember_created_at if expire_all_remember_me_on_sign_out is false' do
|
|
|
|
swap Devise, expire_all_remember_me_on_sign_out: false do
|
|
|
|
resource = create_resource
|
|
|
|
resource.remember_me!
|
|
|
|
|
|
|
|
assert_not_nil resource.remember_created_at
|
|
|
|
|
|
|
|
resource.forget_me!
|
|
|
|
assert_not_nil resource.remember_created_at
|
|
|
|
end
|
2009-10-22 07:09:34 -04:00
|
|
|
end
|
2012-02-20 06:54:45 -05:00
|
|
|
|
2011-04-22 20:56:54 -04:00
|
|
|
test 'forget_me should not try to update resource if it has been destroyed' do
|
|
|
|
resource = create_resource
|
|
|
|
resource.expects(:remember_created_at).never
|
|
|
|
resource.expects(:save).never
|
2013-04-13 09:57:10 -04:00
|
|
|
|
|
|
|
resource.destroy
|
2011-04-22 20:56:54 -04:00
|
|
|
resource.forget_me!
|
|
|
|
end
|
2009-10-22 07:09:34 -04:00
|
|
|
|
2015-12-16 11:13:46 -05:00
|
|
|
test 'remember expires at uses remember for configuration' do
|
2014-02-25 11:42:55 -05:00
|
|
|
swap Devise, remember_for: 3.days do
|
2010-09-25 06:37:06 -04:00
|
|
|
resource = create_resource
|
|
|
|
resource.remember_me!
|
|
|
|
assert_equal 3.days.from_now.to_date, resource.remember_expires_at.to_date
|
2009-11-24 20:19:12 -05:00
|
|
|
|
|
|
|
Devise.remember_for = 5.days
|
2010-09-25 06:37:06 -04:00
|
|
|
assert_equal 5.days.from_now.to_date, resource.remember_expires_at.to_date
|
2009-11-24 20:19:12 -05:00
|
|
|
end
|
2009-10-22 07:09:34 -04:00
|
|
|
end
|
|
|
|
|
2013-04-18 00:54:38 -04:00
|
|
|
test 'should have the required_fields array' do
|
2016-05-03 13:18:35 -04:00
|
|
|
assert_equal Devise::Models::Rememberable.required_fields(User), [
|
2012-03-30 03:13:28 -04:00
|
|
|
:remember_created_at
|
2012-02-20 06:54:45 -05:00
|
|
|
]
|
|
|
|
end
|
2010-09-25 06:37:06 -04:00
|
|
|
end
|