1
0
Fork 0
mirror of https://github.com/heartcombo/devise.git synced 2022-11-09 12:18:31 -05:00

Use Admin in old rememberable tests.

This commit is contained in:
José Valim 2010-09-25 12:04:38 +02:00
parent 2aa1d2f3b7
commit 1ed674afa8
5 changed files with 119 additions and 118 deletions

View file

@ -37,7 +37,7 @@ class MappingTest < ActiveSupport::TestCase
test 'has strategies depending on the model declaration' do test 'has strategies depending on the model declaration' do
assert_equal [:rememberable, :token_authenticatable, :database_authenticatable], Devise.mappings[:user].strategies assert_equal [:rememberable, :token_authenticatable, :database_authenticatable], Devise.mappings[:user].strategies
assert_equal [:database_authenticatable], Devise.mappings[:admin].strategies assert_equal [:rememberable, :database_authenticatable], Devise.mappings[:admin].strategies
end end
test 'find scope for a given object' do test 'find scope for a given object' do
@ -90,6 +90,6 @@ class MappingTest < ActiveSupport::TestCase
assert mapping.recoverable? assert mapping.recoverable?
assert mapping.lockable? assert mapping.lockable?
assert_not mapping.confirmable? assert_not mapping.confirmable?
assert_not mapping.rememberable? assert_not mapping.oauthable?
end end
end end

View file

@ -2,217 +2,217 @@ require 'test_helper'
class RememberableTest < ActiveSupport::TestCase class RememberableTest < ActiveSupport::TestCase
test 'should respond to remember_me attribute' do test 'should respond to remember_me attribute' do
user = new_user assert Admin.new.respond_to?(:remember_me)
assert user.respond_to?(:remember_me) assert Admin.new.respond_to?(:remember_me=)
end end
test 'remember_me should generate a new token and save the record without validating' do test 'remember_me should generate a new token and save the record without validating' do
user = create_user admin = create_admin
user.expects(:valid?).never admin.expects(:valid?).never
token = user.remember_token token = admin.remember_token
user.remember_me! admin.remember_me!
assert_not_equal token, user.remember_token assert_not_equal token, admin.remember_token
assert_not user.changed? assert_not admin.changed?
end end
test 'forget_me should clear remember token and save the record without validating' do test 'forget_me should clear remember token and save the record without validating' do
user = create_user admin = create_admin
user.remember_me! admin.remember_me!
assert_not user.remember_token.nil? assert_not admin.remember_token.nil?
user.expects(:valid?).never admin.expects(:valid?).never
user.forget_me! admin.forget_me!
assert user.remember_token.nil? assert admin.remember_token.nil?
assert_not user.changed? assert_not admin.changed?
end end
test 'forget_me should clear remember_created_at' do test 'forget_me should clear remember_created_at' do
user = create_user admin = create_admin
user.remember_me! admin.remember_me!
assert_not user.remember_created_at.nil? assert_not admin.remember_created_at.nil?
user.forget_me! admin.forget_me!
assert user.remember_created_at.nil? assert admin.remember_created_at.nil?
end end
test 'forget should do nothing if no remember token exists' do test 'forget should do nothing if no remember token exists' do
user = create_user admin = create_admin
user.expects(:save).never admin.expects(:save).never
user.forget_me! admin.forget_me!
end end
test 'serialize into cookie' do test 'serialize into cookie' do
user = create_user admin = create_admin
user.remember_me! admin.remember_me!
assert_equal [user.id, user.remember_token], User.serialize_into_cookie(user) assert_equal [admin.id, admin.remember_token], Admin.serialize_into_cookie(admin)
end end
test 'serialize from cookie' do test 'serialize from cookie' do
user = create_user admin = create_admin
user.remember_me! admin.remember_me!
assert_equal user, User.serialize_from_cookie(user.id, user.remember_token) assert_equal admin, Admin.serialize_from_cookie(admin.id, admin.remember_token)
end end
test 'serialize should return nil if no user is found' do test 'serialize should return nil if no admin is found' do
assert_nil User.serialize_from_cookie(0, "123") assert_nil Admin.serialize_from_cookie(0, "123")
end end
test 'remember me return nil if is a valid user with invalid token' do test 'remember me return nil if is a valid admin with invalid token' do
user = create_user admin = create_admin
assert_nil User.serialize_from_cookie(user.id, "123") assert_nil Admin.serialize_from_cookie(admin.id, "123")
end end
test 'remember for should fallback to devise remember for default configuration' do test 'remember for should fallback to devise remember for default configuration' do
swap Devise, :remember_for => 1.day do swap Devise, :remember_for => 1.day do
user = create_user admin = create_admin
user.remember_me! admin.remember_me!
assert_not user.remember_expired? assert_not admin.remember_expired?
end end
end end
test 'remember expires at should sum date of creation with remember for configuration' do test 'remember expires at should sum date of creation with remember for configuration' do
swap Devise, :remember_for => 3.days do swap Devise, :remember_for => 3.days do
user = create_user admin = create_admin
user.remember_me! admin.remember_me!
assert_equal 3.days.from_now.to_date, user.remember_expires_at.to_date assert_equal 3.days.from_now.to_date, admin.remember_expires_at.to_date
Devise.remember_for = 5.days Devise.remember_for = 5.days
assert_equal 5.days.from_now.to_date, user.remember_expires_at.to_date assert_equal 5.days.from_now.to_date, admin.remember_expires_at.to_date
end end
end end
test 'remember should be expired if remember_for is zero' do test 'remember should be expired if remember_for is zero' do
swap Devise, :remember_for => 0.days do swap Devise, :remember_for => 0.days do
Devise.remember_for = 0.days Devise.remember_for = 0.days
user = create_user admin = create_admin
user.remember_me! admin.remember_me!
assert user.remember_expired? assert admin.remember_expired?
end end
end end
test 'remember should be expired if it was created before limit time' do test 'remember should be expired if it was created before limit time' do
swap Devise, :remember_for => 1.day do swap Devise, :remember_for => 1.day do
user = create_user admin = create_admin
user.remember_me! admin.remember_me!
user.remember_created_at = 2.days.ago admin.remember_created_at = 2.days.ago
user.save admin.save
assert user.remember_expired? assert admin.remember_expired?
end end
end end
test 'remember should not be expired if it was created whitin the limit time' do test 'remember should not be expired if it was created whitin the limit time' do
swap Devise, :remember_for => 30.days do swap Devise, :remember_for => 30.days do
user = create_user admin = create_admin
user.remember_me! admin.remember_me!
user.remember_created_at = (30.days.ago + 2.minutes) admin.remember_created_at = (30.days.ago + 2.minutes)
user.save admin.save
assert_not user.remember_expired? assert_not admin.remember_expired?
end end
end end
test 'if extend_remember_period is false, remember_me! should generate a new timestamp if expired' do test 'if extend_remember_period is false, remember_me! should generate a new timestamp if expired' do
swap Devise, :remember_for => 5.minutes do swap Devise, :remember_for => 5.minutes do
user = create_user admin = create_admin
user.remember_me!(false) admin.remember_me!(false)
assert user.remember_created_at assert admin.remember_created_at
user.remember_created_at = old = 10.minutes.ago admin.remember_created_at = old = 10.minutes.ago
user.save admin.save
user.remember_me!(false) admin.remember_me!(false)
assert_not_equal old.to_i, user.remember_created_at.to_i assert_not_equal old.to_i, admin.remember_created_at.to_i
end end
end end
test 'if extend_remember_period is false, remember_me! should not generate a new timestamp' do test 'if extend_remember_period is false, remember_me! should not generate a new timestamp' do
swap Devise, :remember_for => 1.year do swap Devise, :remember_for => 1.year do
user = create_user admin = create_admin
user.remember_me!(false) admin.remember_me!(false)
assert user.remember_created_at assert admin.remember_created_at
user.remember_created_at = old = 10.minutes.ago.utc admin.remember_created_at = old = 10.minutes.ago.utc
user.save admin.save
user.remember_me!(false) admin.remember_me!(false)
assert_equal old.to_i, user.remember_created_at.to_i assert_equal old.to_i, admin.remember_created_at.to_i
end end
end end
test 'if extend_remember_period is true, remember_me! should always generate a new timestamp' do test 'if extend_remember_period is true, remember_me! should always generate a new timestamp' do
swap Devise, :remember_for => 1.year do swap Devise, :remember_for => 1.year do
user = create_user admin = create_admin
user.remember_me!(true) admin.remember_me!(true)
assert user.remember_created_at assert admin.remember_created_at
user.remember_created_at = old = 10.minutes.ago admin.remember_created_at = old = 10.minutes.ago
user.save admin.save
user.remember_me!(true) admin.remember_me!(true)
assert_not_equal old, user.remember_created_at assert_not_equal old, admin.remember_created_at
end end
end end
test 'if remember_across_browsers is true, remember_me! should create a new token if no token exists' do test 'if remember_across_browsers is true, remember_me! should create a new token if no token exists' do
swap Devise, :remember_across_browsers => true, :remember_for => 1.year do swap Devise, :remember_across_browsers => true, :remember_for => 1.year do
user = create_user admin = create_admin
assert_equal nil, user.remember_token assert_equal nil, admin.remember_token
user.remember_me! admin.remember_me!
assert_not_equal nil, user.remember_token assert_not_equal nil, admin.remember_token
end end
end end
test 'if remember_across_browsers is true, remember_me! should create a new token if a token exists but has expired' do test 'if remember_across_browsers is true, remember_me! should create a new token if a token exists but has expired' do
swap Devise, :remember_across_browsers => true, :remember_for => 1.day do swap Devise, :remember_across_browsers => true, :remember_for => 1.day do
user = create_user admin = create_admin
user.remember_me! admin.remember_me!
user.remember_created_at = 2.days.ago admin.remember_created_at = 2.days.ago
user.save admin.save
token = user.remember_token token = admin.remember_token
user.remember_me! admin.remember_me!
assert_not_equal token, user.remember_token assert_not_equal token, admin.remember_token
end end
end end
test 'if remember_across_browsers is true, remember_me! should not create a new token if a token exists and has not expired' do test 'if remember_across_browsers is true, remember_me! should not create a new token if a token exists and has not expired' do
swap Devise, :remember_across_browsers => true, :remember_for => 2.days do swap Devise, :remember_across_browsers => true, :remember_for => 2.days do
user = create_user admin = create_admin
user.remember_me! admin.remember_me!
user.remember_created_at = 1.day.ago admin.remember_created_at = 1.day.ago
user.save admin.save
token = user.remember_token token = admin.remember_token
user.remember_me! admin.remember_me!
assert_equal token, user.remember_token assert_equal token, admin.remember_token
end end
end end
test 'if remember_across_browsers is false, remember_me! should create a new token if no token exists' do test 'if remember_across_browsers is false, remember_me! should create a new token if no token exists' do
swap Devise, :remember_across_browsers => false do swap Devise, :remember_across_browsers => false do
user = create_user admin = create_admin
assert_equal nil, user.remember_token assert_equal nil, admin.remember_token
user.remember_me! admin.remember_me!
assert_not_equal nil, user.remember_token assert_not_equal nil, admin.remember_token
end end
end end
test 'if remember_across_browsers is false, remember_me! should create a new token if a token exists but has expired' do test 'if remember_across_browsers is false, remember_me! should create a new token if a token exists but has expired' do
swap Devise, :remember_across_browsers => false, :remember_for => 1.day do swap Devise, :remember_across_browsers => false, :remember_for => 1.day do
user = create_user admin = create_admin
user.remember_me! admin.remember_me!
user.remember_created_at = 2.days.ago admin.remember_created_at = 2.days.ago
user.save admin.save
token = user.remember_token token = admin.remember_token
user.remember_me! admin.remember_me!
assert_not_equal token, user.remember_token assert_not_equal token, admin.remember_token
end end
end end
test 'if remember_across_browsers is false, remember_me! should create a new token if a token exists and has not expired' do test 'if remember_across_browsers is false, remember_me! should create a new token if a token exists and has not expired' do
swap Devise, :remember_across_browsers => false, :remember_for => 2.days do swap Devise, :remember_across_browsers => false, :remember_for => 2.days do
user = create_user admin = create_admin
user.remember_me! admin.remember_me!
user.remember_created_at = 1.day.ago admin.remember_created_at = 1.day.ago
user.save admin.save
token = user.remember_token token = admin.remember_token
user.remember_me! admin.remember_me!
assert_not_equal token, user.remember_token assert_not_equal token, admin.remember_token
end end
end end
end end

View file

@ -26,16 +26,16 @@ class ActiveRecordTest < ActiveSupport::TestCase
end end
test 'can cherry pick modules' do test 'can cherry pick modules' do
assert_include_modules Admin, :database_authenticatable, :registerable, :timeoutable, :recoverable, :lockable assert_include_modules Admin, :database_authenticatable, :registerable, :timeoutable, :recoverable, :lockable, :rememberable
end end
test 'chosen modules are inheritable' do test 'chosen modules are inheritable' do
assert_include_modules Inheritable, :database_authenticatable, :registerable, :timeoutable, :recoverable, :lockable assert_include_modules Inheritable, :database_authenticatable, :registerable, :timeoutable, :recoverable, :lockable, :rememberable
end end
test 'order of module inclusion' do test 'order of module inclusion' do
correct_module_order = [:database_authenticatable, :recoverable, :registerable, :lockable, :timeoutable] correct_module_order = [:database_authenticatable, :rememberable, :recoverable, :registerable, :lockable, :timeoutable]
incorrect_module_order = [:database_authenticatable, :timeoutable, :registerable, :recoverable, :lockable] incorrect_module_order = [:database_authenticatable, :timeoutable, :registerable, :recoverable, :lockable, :rememberable]
assert_include_modules Admin, *incorrect_module_order assert_include_modules Admin, *incorrect_module_order

View file

@ -16,6 +16,7 @@ class CreateTables < ActiveRecord::Migration
create_table :admins do |t| create_table :admins do |t|
t.database_authenticatable :null => true, :encryptor => :bcrypt t.database_authenticatable :null => true, :encryptor => :bcrypt
t.rememberable
t.recoverable t.recoverable
t.lockable t.lockable
t.timestamps t.timestamps

View file

@ -2,6 +2,6 @@ module SharedAdmin
extend ActiveSupport::Concern extend ActiveSupport::Concern
included do included do
devise :database_authenticatable, :registerable, :timeoutable, :recoverable, :lockable, :unlock_strategy => :time devise :database_authenticatable, :registerable, :timeoutable, :recoverable, :rememberable, :lockable, :unlock_strategy => :time
end end
end end