2010-03-26 06:27:19 -04:00
|
|
|
require 'test_helper'
|
2012-12-01 22:06:03 -05:00
|
|
|
require 'test_models'
|
2010-04-15 02:34:49 -04:00
|
|
|
|
2009-10-09 20:11:58 -04:00
|
|
|
class ActiveRecordTest < ActiveSupport::TestCase
|
2009-10-20 09:08:40 -04:00
|
|
|
def include_module?(klass, mod)
|
|
|
|
klass.devise_modules.include?(mod) &&
|
2012-05-15 16:14:05 -04:00
|
|
|
klass.included_modules.include?(Devise::Models::const_get(mod.to_s.classify))
|
2009-10-20 09:08:40 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def assert_include_modules(klass, *modules)
|
|
|
|
modules.each do |mod|
|
|
|
|
assert include_module?(klass, mod)
|
|
|
|
end
|
|
|
|
|
2009-11-24 12:18:42 -05:00
|
|
|
(Devise::ALL - modules).each do |mod|
|
2009-10-20 09:08:40 -04:00
|
|
|
assert_not include_module?(klass, mod)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-04-15 02:34:49 -04:00
|
|
|
test 'can cherry pick modules' do
|
2012-05-07 15:21:35 -04:00
|
|
|
assert_include_modules Admin, :database_authenticatable, :registerable, :timeoutable, :recoverable, :lockable, :confirmable
|
2010-03-25 08:29:09 -04:00
|
|
|
end
|
|
|
|
|
2011-08-06 08:28:19 -04:00
|
|
|
test 'validations options are not applied too late' do
|
|
|
|
validators = WithValidation.validators_on :password
|
|
|
|
length = validators.find { |v| v.kind == :length }
|
|
|
|
assert_equal 2, length.options[:minimum]
|
|
|
|
assert_equal 6, length.options[:maximum]
|
|
|
|
end
|
2011-04-16 07:15:31 -04:00
|
|
|
|
2011-08-06 08:28:19 -04:00
|
|
|
test 'validations are applied just once' do
|
|
|
|
validators = Several.validators_on :password
|
|
|
|
assert_equal 1, validators.select{ |v| v.kind == :length }.length
|
2011-04-09 16:47:06 -04:00
|
|
|
end
|
|
|
|
|
2010-04-15 02:34:49 -04:00
|
|
|
test 'chosen modules are inheritable' do
|
2012-05-07 15:21:35 -04:00
|
|
|
assert_include_modules Inheritable, :database_authenticatable, :registerable, :timeoutable, :recoverable, :lockable, :confirmable
|
2010-04-15 02:34:49 -04:00
|
|
|
end
|
|
|
|
|
2010-03-25 08:29:09 -04:00
|
|
|
test 'order of module inclusion' do
|
2012-05-07 15:21:35 -04:00
|
|
|
correct_module_order = [:database_authenticatable, :recoverable, :registerable, :confirmable, :lockable, :timeoutable]
|
|
|
|
incorrect_module_order = [:database_authenticatable, :timeoutable, :registerable, :recoverable, :lockable, :confirmable]
|
2010-03-25 08:29:09 -04:00
|
|
|
|
|
|
|
assert_include_modules Admin, *incorrect_module_order
|
|
|
|
|
|
|
|
# get module constants from symbol list
|
|
|
|
module_constants = correct_module_order.collect { |mod| Devise::Models::const_get(mod.to_s.classify) }
|
|
|
|
|
|
|
|
# confirm that they adhere to the order in ALL
|
|
|
|
# get included modules, filter out the noise, and reverse the order
|
|
|
|
assert_equal module_constants, (Admin.included_modules & module_constants).reverse
|
2009-10-09 08:03:01 -04:00
|
|
|
end
|
|
|
|
|
2011-01-14 16:38:35 -05:00
|
|
|
test 'raise error on invalid module' do
|
|
|
|
assert_raise NameError do
|
2011-01-14 18:10:46 -05:00
|
|
|
# Mix valid an invalid modules.
|
|
|
|
Configurable.class_eval { devise :database_authenticatable, :doesnotexit }
|
2011-01-14 16:38:35 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-10-20 09:08:40 -04:00
|
|
|
test 'set a default value for stretches' do
|
2009-11-22 19:32:54 -05:00
|
|
|
assert_equal 15, Configurable.stretches
|
2009-10-09 08:03:01 -04:00
|
|
|
end
|
|
|
|
|
2009-10-20 09:08:40 -04:00
|
|
|
test 'set a default value for pepper' do
|
2009-11-22 19:32:54 -05:00
|
|
|
assert_equal 'abcdef', Configurable.pepper
|
2009-10-22 07:49:19 -04:00
|
|
|
end
|
|
|
|
|
2011-12-11 14:18:02 -05:00
|
|
|
test 'set a default value for allow_unconfirmed_access_for' do
|
|
|
|
assert_equal 5.days, Configurable.allow_unconfirmed_access_for
|
2009-10-22 07:49:19 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
test 'set a default value for remember_for' do
|
2009-11-22 19:32:54 -05:00
|
|
|
assert_equal 7.days, Configurable.remember_for
|
2009-10-09 08:03:01 -04:00
|
|
|
end
|
2009-10-22 17:26:10 -04:00
|
|
|
|
2009-11-24 21:11:49 -05:00
|
|
|
test 'set a default value for timeout_in' do
|
|
|
|
assert_equal 15.minutes, Configurable.timeout_in
|
2009-11-22 19:19:29 -05:00
|
|
|
end
|
|
|
|
|
2010-01-13 13:51:20 -05:00
|
|
|
test 'set a default value for unlock_in' do
|
|
|
|
assert_equal 10.days, Configurable.unlock_in
|
|
|
|
end
|
|
|
|
|
2009-10-22 17:26:10 -04:00
|
|
|
test 'set null fields on migrations' do
|
|
|
|
Admin.create!
|
|
|
|
end
|
2009-10-09 08:03:01 -04:00
|
|
|
end
|
2012-02-17 11:37:44 -05:00
|
|
|
|
|
|
|
class CheckFieldsTest < ActiveSupport::TestCase
|
|
|
|
test 'checks if the class respond_to the required fields' do
|
|
|
|
Player = Class.new do
|
|
|
|
extend Devise::Models
|
|
|
|
|
2012-02-18 19:50:59 -05:00
|
|
|
def self.before_validation(instance)
|
2012-02-17 11:37:44 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
devise :database_authenticatable
|
|
|
|
|
|
|
|
attr_accessor :encrypted_password, :email
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_nothing_raised Devise::Models::MissingAttribute do
|
|
|
|
Devise::Models.check_fields!(Player)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-02-21 07:31:38 -05:00
|
|
|
test 'raises Devise::Models::MissingAtrribute and shows the missing attribute if the class doesn\'t respond_to one of the attributes' do
|
2012-02-17 11:37:44 -05:00
|
|
|
Clown = Class.new do
|
|
|
|
extend Devise::Models
|
|
|
|
|
|
|
|
def self.before_validation(instance)
|
|
|
|
end
|
|
|
|
|
|
|
|
devise :database_authenticatable
|
|
|
|
|
|
|
|
attr_accessor :encrypted_password
|
|
|
|
end
|
|
|
|
|
2012-02-22 10:48:41 -05:00
|
|
|
assert_raise_with_message Devise::Models::MissingAttribute, "The following attribute(s) is (are) missing on your model: email" do
|
2012-02-17 11:37:44 -05:00
|
|
|
Devise::Models.check_fields!(Clown)
|
|
|
|
end
|
|
|
|
end
|
2012-02-21 07:31:38 -05:00
|
|
|
|
|
|
|
test 'raises Devise::Models::MissingAtrribute with all the missing attributes if there is more than one' do
|
|
|
|
Magician = Class.new do
|
|
|
|
extend Devise::Models
|
|
|
|
|
|
|
|
def self.before_validation(instance)
|
|
|
|
end
|
|
|
|
|
|
|
|
devise :database_authenticatable
|
|
|
|
end
|
|
|
|
|
2012-05-15 16:14:05 -04:00
|
|
|
assert_raise_with_message Devise::Models::MissingAttribute, "The following attribute(s) is (are) missing on your model: encrypted_password, email" do
|
2012-02-21 07:31:38 -05:00
|
|
|
Devise::Models.check_fields!(Magician)
|
|
|
|
end
|
|
|
|
end
|
2012-03-03 12:30:53 -05:00
|
|
|
|
2012-03-03 13:10:17 -05:00
|
|
|
test "doesn't raise a NoMethodError exception when the module doesn't have a required_field(klass) class method" do
|
2012-05-15 16:14:05 -04:00
|
|
|
driver = Class.new do
|
2012-03-03 12:30:53 -05:00
|
|
|
extend Devise::Models
|
|
|
|
|
|
|
|
def self.before_validation(instance)
|
|
|
|
end
|
|
|
|
|
2012-03-03 12:49:59 -05:00
|
|
|
attr_accessor :encrypted_password, :email
|
|
|
|
|
2012-03-03 12:30:53 -05:00
|
|
|
devise :database_authenticatable
|
|
|
|
end
|
|
|
|
|
|
|
|
swap_module_method_existence Devise::Models::DatabaseAuthenticatable, :required_fields do
|
2012-03-03 13:10:17 -05:00
|
|
|
assert_deprecated do
|
|
|
|
Devise::Models.check_fields!(driver)
|
2012-03-03 12:30:53 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2012-02-21 07:31:38 -05:00
|
|
|
end
|