-
Notifications
You must be signed in to change notification settings - Fork 5.5k
/
Copy pathmodels_test.rb
155 lines (119 loc) · 4.63 KB
/
models_test.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# frozen_string_literal: true
require 'test_helper'
require 'test_models'
class ActiveRecordTest < ActiveSupport::TestCase
def include_module?(klass, mod)
klass.devise_modules.include?(mod) &&
klass.included_modules.include?(Devise::Models::const_get(mod.to_s.classify))
end
def assert_include_modules(klass, *modules)
modules.each do |mod|
assert include_module?(klass, mod)
end
(Devise::ALL - modules).each do |mod|
refute include_module?(klass, mod)
end
end
test 'can cherry pick modules' do
assert_include_modules Admin, :database_authenticatable, :registerable, :timeoutable, :recoverable, :lockable, :confirmable
end
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
test 'validations are applied just once' do
validators = Several.validators_on :password
assert_equal 1, validators.select{ |v| v.kind == :length }.length
end
test 'chosen modules are inheritable' do
assert_include_modules Inheritable, :database_authenticatable, :registerable, :timeoutable, :recoverable, :lockable, :confirmable
end
test 'order of module inclusion' do
correct_module_order = [:database_authenticatable, :recoverable, :registerable, :confirmable, :lockable, :timeoutable]
incorrect_module_order = [:database_authenticatable, :timeoutable, :registerable, :recoverable, :lockable, :confirmable]
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
end
test 'raise error on invalid module' do
assert_raise NameError do
# Mix valid an invalid modules.
Configurable.class_eval { devise :database_authenticatable, :doesnotexit }
end
end
test 'set a default value for stretches' do
assert_equal 15, Configurable.stretches
end
test 'set a default value for pepper' do
assert_equal 'abcdef', Configurable.pepper
end
test 'set a default value for allow_unconfirmed_access_for' do
assert_equal 5.days, Configurable.allow_unconfirmed_access_for
end
test 'set a default value for remember_for' do
assert_equal 7.days, Configurable.remember_for
end
test 'set a default value for timeout_in' do
assert_equal 15.minutes, Configurable.timeout_in
end
test 'set a default value for unlock_in' do
assert_equal 10.days, Configurable.unlock_in
end
test 'set null fields on migrations' do
# Ignore email sending since no email exists.
klass = Class.new(Admin) do
def send_devise_notification(*); end
end
klass.create!
end
end
module StubModelFilters
def stub_filter(name)
define_singleton_method(name) { |*| nil }
end
end
class CheckFieldsTest < ActiveSupport::TestCase
test 'checks if the class respond_to the required fields' do
Player = Class.new do
extend Devise::Models
extend StubModelFilters
stub_filter :before_validation
stub_filter :after_update
devise :database_authenticatable
attr_accessor :encrypted_password, :email
end
assert_nothing_raised do
Devise::Models.check_fields!(Player)
end
end
test 'raises Devise::Models::MissingAtrribute and shows the missing attribute if the class doesn\'t respond_to one of the attributes' do
Clown = Class.new do
extend Devise::Models
extend StubModelFilters
stub_filter :before_validation
stub_filter :after_update
devise :database_authenticatable
attr_accessor :encrypted_password
end
assert_raise_with_message Devise::Models::MissingAttribute, "The following attribute(s) is (are) missing on your model: email" do
Devise::Models.check_fields!(Clown)
end
end
test 'raises Devise::Models::MissingAtrribute with all the missing attributes if there is more than one' do
Magician = Class.new do
extend Devise::Models
extend StubModelFilters
stub_filter :before_validation
stub_filter :after_update
devise :database_authenticatable
end
assert_raise_with_message Devise::Models::MissingAttribute, "The following attribute(s) is (are) missing on your model: encrypted_password, email" do
Devise::Models.check_fields!(Magician)
end
end
end