mirror of
https://github.com/heartcombo/devise.git
synced 2022-11-09 12:18:31 -05:00
77 lines
2.2 KiB
Ruby
77 lines
2.2 KiB
Ruby
![]() |
require 'test/test_helper'
|
||
|
|
||
|
class RememberableTest < ActiveSupport::TestCase
|
||
|
|
||
|
test 'should respond to remember_me attribute' do
|
||
|
user = new_user
|
||
|
assert user.respond_to?(:remember_me)
|
||
|
end
|
||
|
|
||
|
test 'should have remember_me accessible' do
|
||
|
assert field_accessible?(:remember_me)
|
||
|
end
|
||
|
|
||
|
test 'remember_me should generate a new token and save the record without validating' do
|
||
|
user = create_user
|
||
|
user.expects(:valid?).never
|
||
|
token = user.remember_token
|
||
|
user.remember_me!
|
||
|
assert_not_equal token, user.remember_token
|
||
|
assert_not user.changed?
|
||
|
end
|
||
|
|
||
|
test 'forget_me should clear remember token and save the record without validating' do
|
||
|
user = create_user
|
||
|
user.remember_me!
|
||
|
assert_not_nil user.remember_token
|
||
|
user.expects(:valid?).never
|
||
|
user.forget_me!
|
||
|
assert_nil user.remember_token
|
||
|
assert_not user.changed?
|
||
|
end
|
||
|
|
||
|
test 'forget should do nothing if no remember token exists' do
|
||
|
user = create_user
|
||
|
user.expects(:save).never
|
||
|
user.forget_me!
|
||
|
end
|
||
|
|
||
|
test 'valid remember token' do
|
||
|
user = create_user
|
||
|
assert_not user.valid_remember_token?(user.remember_token)
|
||
|
user.remember_me!
|
||
|
assert user.valid_remember_token?(user.remember_token)
|
||
|
user.forget_me!
|
||
|
assert_not user.valid_remember_token?(user.remember_token)
|
||
|
end
|
||
|
|
||
|
test 'find a user by its id and remember it if the token is valid' do
|
||
|
user = create_user
|
||
|
user.remember_me!
|
||
|
remembered_user = User.remember_me!(:id => user.id, :remember_token => user.remember_token)
|
||
|
assert_not_nil remembered_user
|
||
|
assert_equal remembered_user, user
|
||
|
end
|
||
|
|
||
|
test 'remember me should return nil if no user is found' do
|
||
|
assert_nil User.remember_me!(:id => 0)
|
||
|
end
|
||
|
|
||
|
test 'remember me return nil if is a valid user with invalid token' do
|
||
|
user = create_user
|
||
|
assert_nil User.remember_me!(:id => user.id, :remember_token => 'invalid_token')
|
||
|
end
|
||
|
|
||
|
test 'serialize into cookie' do
|
||
|
user = create_user
|
||
|
user.remember_me!
|
||
|
assert_equal "#{user.id}::#{user.remember_token}", User.serialize_into_cookie(user)
|
||
|
end
|
||
|
|
||
|
test 'serialize from cookie' do
|
||
|
user = create_user
|
||
|
user.remember_me!
|
||
|
assert_equal user, User.serialize_from_cookie("#{user.id}::#{user.remember_token}")
|
||
|
end
|
||
|
end
|