require "test_helper" class RememberableTest < ActiveSupport::TestCase def setup @user = Fabricate(:user) end test 'remember_me!' do @user.remember_me! assert_equal Time.now.utc.to_s, @user.remember_created_at.to_s end test 'remember_me! should not update remember_created_at' do Timecop.freeze Time.now @user.remember_me! remember_created_at = Time.now.utc Timecop.freeze (2.weeks - 1.second).from_now @user.remember_me! assert_equal remember_created_at.to_s, @user.remember_created_at.to_s Timecop.return end test 'remember_me! should update remember_created_at if config.extend_remember_period == true' do swap Goma.config(:user), extend_remember_period: true do @user.remember_me! assert_equal Time.now.utc.to_s, @user.remember_created_at.to_s Timecop.freeze 1.day.from_now do @user.remember_me! assert_equal Time.now.utc.to_s, @user.remember_created_at.to_s end end end test 'remember_me! should update remember_created_at if remember period expired' do @user.remember_me! assert_equal Time.now.utc.to_s, @user.remember_created_at.to_s Timecop.freeze 2.weeks.from_now do @user.remember_me! assert_equal Time.now.utc.to_s, @user.remember_created_at.to_s end end test 'forget_me!' do @user.remember_me! @user.forget_me! assert_nil @user.remember_created_at end test 'remember_me! should generate remember_token if config.remember_token_attribute_name != nil' do @user.singleton_class.send :attr_accessor, :remember_token swap Goma.config(:user), remember_token_attribute_name: :remember_token do @user.remember_me! assert @user.remember_token end end test 'remember_me! should regenerate remember_token if config.remember_token_attribute_name != nil and remember period expired' do @user.singleton_class.send :attr_accessor, :remember_token swap Goma.config(:user), remember_token_attribute_name: :remember_token do Timecop.freeze Time.now @user.remember_me! token = @user.remember_token Timecop.freeze (2.weeks - 1.second).from_now @user.remember_me! assert_equal token, @user.remember_token Timecop.freeze 1.second.from_now @user.remember_me! assert_not_equal token, @user.remember_token assert @user.remember_token Timecop.return end end test 'serialize_into_cookie and serialize_from_cookie' do @user.remember_me! serialized = User.serialize_into_cookie(@user) assert serialized assert_equal @user, User.serialize_from_cookie(*serialized) end test 'serialize_from_cookie return nil if it is wrong' do @user.remember_me! serialized = User.serialize_into_cookie(@user) serialized.last << 'aaa' assert_nil User.serialize_from_cookie(*serialized) end test 'serialize_from_cookie return nil if user changed her password' do @user.remember_me! serialized = User.serialize_into_cookie(@user) @user.update(password: 'newpassword', password_confirmation: 'newpassword') assert_nil User.serialize_from_cookie(*serialized) end test 'serialize_from_cookie return nil if remember period expired' do Timecop.freeze Time.now @user.remember_me! serialized = User.serialize_into_cookie(@user) Timecop.freeze (2.weeks - 1.second).from_now assert_equal @user, User.serialize_from_cookie(*serialized) Timecop.freeze 1.second.from_now assert_nil User.serialize_from_cookie(*serialized) Timecop.return end end