models_test.rb 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. require 'test_helper'
  2. class Configurable < User
  3. devise :database_authenticatable, :confirmable, :rememberable, :timeoutable, :lockable,
  4. :stretches => 15, :pepper => 'abcdef', :allow_unconfirmed_access_for => 5.days,
  5. :remember_for => 7.days, :timeout_in => 15.minutes, :unlock_in => 10.days
  6. end
  7. class WithValidation < Admin
  8. devise :database_authenticatable, :validatable, :password_length => 2..6
  9. end
  10. class UserWithValidation < User
  11. validates_presence_of :username
  12. end
  13. class Several < Admin
  14. devise :validatable
  15. devise :lockable
  16. end
  17. class Inheritable < Admin
  18. end
  19. class ActiveRecordTest < ActiveSupport::TestCase
  20. def include_module?(klass, mod)
  21. klass.devise_modules.include?(mod) &&
  22. klass.included_modules.include?(Devise::Models::const_get(mod.to_s.classify))
  23. end
  24. def assert_include_modules(klass, *modules)
  25. modules.each do |mod|
  26. assert include_module?(klass, mod)
  27. end
  28. (Devise::ALL - modules).each do |mod|
  29. assert_not include_module?(klass, mod)
  30. end
  31. end
  32. test 'can cherry pick modules' do
  33. assert_include_modules Admin, :database_authenticatable, :registerable, :timeoutable, :recoverable, :lockable, :confirmable
  34. end
  35. test 'validations options are not applied too late' do
  36. validators = WithValidation.validators_on :password
  37. length = validators.find { |v| v.kind == :length }
  38. assert_equal 2, length.options[:minimum]
  39. assert_equal 6, length.options[:maximum]
  40. end
  41. test 'validations are applied just once' do
  42. validators = Several.validators_on :password
  43. assert_equal 1, validators.select{ |v| v.kind == :length }.length
  44. end
  45. test 'chosen modules are inheritable' do
  46. assert_include_modules Inheritable, :database_authenticatable, :registerable, :timeoutable, :recoverable, :lockable, :confirmable
  47. end
  48. test 'order of module inclusion' do
  49. correct_module_order = [:database_authenticatable, :recoverable, :registerable, :confirmable, :lockable, :timeoutable]
  50. incorrect_module_order = [:database_authenticatable, :timeoutable, :registerable, :recoverable, :lockable, :confirmable]
  51. assert_include_modules Admin, *incorrect_module_order
  52. # get module constants from symbol list
  53. module_constants = correct_module_order.collect { |mod| Devise::Models::const_get(mod.to_s.classify) }
  54. # confirm that they adhere to the order in ALL
  55. # get included modules, filter out the noise, and reverse the order
  56. assert_equal module_constants, (Admin.included_modules & module_constants).reverse
  57. end
  58. test 'raise error on invalid module' do
  59. assert_raise NameError do
  60. # Mix valid an invalid modules.
  61. Configurable.class_eval { devise :database_authenticatable, :doesnotexit }
  62. end
  63. end
  64. test 'set a default value for stretches' do
  65. assert_equal 15, Configurable.stretches
  66. end
  67. test 'set a default value for pepper' do
  68. assert_equal 'abcdef', Configurable.pepper
  69. end
  70. test 'set a default value for allow_unconfirmed_access_for' do
  71. assert_equal 5.days, Configurable.allow_unconfirmed_access_for
  72. end
  73. test 'set a default value for remember_for' do
  74. assert_equal 7.days, Configurable.remember_for
  75. end
  76. test 'set a default value for timeout_in' do
  77. assert_equal 15.minutes, Configurable.timeout_in
  78. end
  79. test 'set a default value for unlock_in' do
  80. assert_equal 10.days, Configurable.unlock_in
  81. end
  82. test 'set null fields on migrations' do
  83. Admin.create!
  84. end
  85. end
  86. class CheckFieldsTest < ActiveSupport::TestCase
  87. test 'checks if the class respond_to the required fields' do
  88. Player = Class.new do
  89. extend Devise::Models
  90. def self.before_validation(instance)
  91. end
  92. devise :database_authenticatable
  93. attr_accessor :encrypted_password, :email
  94. end
  95. assert_nothing_raised Devise::Models::MissingAttribute do
  96. Devise::Models.check_fields!(Player)
  97. end
  98. end
  99. test 'raises Devise::Models::MissingAtrribute and shows the missing attribute if the class doesn\'t respond_to one of the attributes' do
  100. Clown = Class.new do
  101. extend Devise::Models
  102. def self.before_validation(instance)
  103. end
  104. devise :database_authenticatable
  105. attr_accessor :encrypted_password
  106. end
  107. assert_raise_with_message Devise::Models::MissingAttribute, "The following attribute(s) is (are) missing on your model: email" do
  108. Devise::Models.check_fields!(Clown)
  109. end
  110. end
  111. test 'raises Devise::Models::MissingAtrribute with all the missing attributes if there is more than one' do
  112. Magician = Class.new do
  113. extend Devise::Models
  114. def self.before_validation(instance)
  115. end
  116. devise :database_authenticatable
  117. end
  118. assert_raise_with_message Devise::Models::MissingAttribute, "The following attribute(s) is (are) missing on your model: encrypted_password, email" do
  119. Devise::Models.check_fields!(Magician)
  120. end
  121. end
  122. test "doesn't raise a NoMethodError exception when the module doesn't have a required_field(klass) class method" do
  123. driver = Class.new do
  124. extend Devise::Models
  125. def self.before_validation(instance)
  126. end
  127. attr_accessor :encrypted_password, :email
  128. devise :database_authenticatable
  129. end
  130. swap_module_method_existence Devise::Models::DatabaseAuthenticatable, :required_fields do
  131. assert_deprecated do
  132. Devise::Models.check_fields!(driver)
  133. end
  134. end
  135. end
  136. end