loyal_devise 2.1.2
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +10 -0
- data/.travis.yml +15 -0
- data/CHANGELOG.rdoc +881 -0
- data/CONTRIBUTING.md +12 -0
- data/Gemfile +31 -0
- data/Gemfile.lock +154 -0
- data/MIT-LICENSE +20 -0
- data/README.md +388 -0
- data/Rakefile +34 -0
- data/app/controllers/devise/confirmations_controller.rb +44 -0
- data/app/controllers/devise/omniauth_callbacks_controller.rb +31 -0
- data/app/controllers/devise/passwords_controller.rb +57 -0
- data/app/controllers/devise/registrations_controller.rb +120 -0
- data/app/controllers/devise/sessions_controller.rb +51 -0
- data/app/controllers/devise/unlocks_controller.rb +45 -0
- data/app/controllers/devise_controller.rb +193 -0
- data/app/helpers/devise_helper.rb +26 -0
- data/app/mailers/devise/mailer.rb +16 -0
- data/app/views/devise/_links.erb +3 -0
- data/app/views/devise/confirmations/new.html.erb +12 -0
- data/app/views/devise/mailer/confirmation_instructions.html.erb +5 -0
- data/app/views/devise/mailer/reset_password_instructions.html.erb +8 -0
- data/app/views/devise/mailer/unlock_instructions.html.erb +7 -0
- data/app/views/devise/passwords/edit.html.erb +16 -0
- data/app/views/devise/passwords/new.html.erb +12 -0
- data/app/views/devise/registrations/edit.html.erb +25 -0
- data/app/views/devise/registrations/new.html.erb +18 -0
- data/app/views/devise/sessions/new.html.erb +17 -0
- data/app/views/devise/shared/_links.erb +25 -0
- data/app/views/devise/unlocks/new.html.erb +12 -0
- data/config/locales/en.yml +59 -0
- data/devise.gemspec +26 -0
- data/gemfiles/Gemfile.rails-3.1.x +35 -0
- data/gemfiles/Gemfile.rails-3.1.x.lock +167 -0
- data/lib/devise/controllers/helpers.rb +273 -0
- data/lib/devise/controllers/rememberable.rb +53 -0
- data/lib/devise/controllers/scoped_views.rb +18 -0
- data/lib/devise/controllers/url_helpers.rb +68 -0
- data/lib/devise/delegator.rb +17 -0
- data/lib/devise/failure_app.rb +188 -0
- data/lib/devise/hooks/activatable.rb +12 -0
- data/lib/devise/hooks/forgetable.rb +10 -0
- data/lib/devise/hooks/lockable.rb +8 -0
- data/lib/devise/hooks/rememberable.rb +7 -0
- data/lib/devise/hooks/timeoutable.rb +26 -0
- data/lib/devise/hooks/trackable.rb +10 -0
- data/lib/devise/mailers/helpers.rb +92 -0
- data/lib/devise/mapping.rb +173 -0
- data/lib/devise/models/authenticatable.rb +269 -0
- data/lib/devise/models/confirmable.rb +271 -0
- data/lib/devise/models/database_authenticatable.rb +127 -0
- data/lib/devise/models/lockable.rb +194 -0
- data/lib/devise/models/omniauthable.rb +28 -0
- data/lib/devise/models/recoverable.rb +141 -0
- data/lib/devise/models/registerable.rb +26 -0
- data/lib/devise/models/rememberable.rb +126 -0
- data/lib/devise/models/timeoutable.rb +50 -0
- data/lib/devise/models/token_authenticatable.rb +90 -0
- data/lib/devise/models/trackable.rb +36 -0
- data/lib/devise/models/validatable.rb +67 -0
- data/lib/devise/models.rb +129 -0
- data/lib/devise/modules.rb +30 -0
- data/lib/devise/omniauth/config.rb +46 -0
- data/lib/devise/omniauth/url_helpers.rb +19 -0
- data/lib/devise/omniauth.rb +29 -0
- data/lib/devise/orm/active_record.rb +4 -0
- data/lib/devise/orm/mongoid.rb +4 -0
- data/lib/devise/param_filter.rb +42 -0
- data/lib/devise/rails/routes.rb +447 -0
- data/lib/devise/rails/warden_compat.rb +44 -0
- data/lib/devise/rails.rb +55 -0
- data/lib/devise/strategies/authenticatable.rb +177 -0
- data/lib/devise/strategies/base.rb +21 -0
- data/lib/devise/strategies/database_authenticatable.rb +21 -0
- data/lib/devise/strategies/rememberable.rb +56 -0
- data/lib/devise/strategies/token_authenticatable.rb +57 -0
- data/lib/devise/test_helpers.rb +132 -0
- data/lib/devise/time_inflector.rb +15 -0
- data/lib/devise/version.rb +4 -0
- data/lib/devise.rb +445 -0
- data/lib/generators/active_record/devise_generator.rb +80 -0
- data/lib/generators/active_record/templates/migration.rb +20 -0
- data/lib/generators/active_record/templates/migration_existing.rb +27 -0
- data/lib/generators/devise/devise_generator.rb +25 -0
- data/lib/generators/devise/install_generator.rb +25 -0
- data/lib/generators/devise/orm_helpers.rb +33 -0
- data/lib/generators/devise/views_generator.rb +117 -0
- data/lib/generators/mongoid/devise_generator.rb +58 -0
- data/lib/generators/templates/README +35 -0
- data/lib/generators/templates/devise.rb +241 -0
- data/lib/generators/templates/markerb/confirmation_instructions.markerb +5 -0
- data/lib/generators/templates/markerb/reset_password_instructions.markerb +8 -0
- data/lib/generators/templates/markerb/unlock_instructions.markerb +7 -0
- data/lib/generators/templates/simple_form_for/confirmations/new.html.erb +15 -0
- data/lib/generators/templates/simple_form_for/passwords/edit.html.erb +19 -0
- data/lib/generators/templates/simple_form_for/passwords/new.html.erb +15 -0
- data/lib/generators/templates/simple_form_for/registrations/edit.html.erb +22 -0
- data/lib/generators/templates/simple_form_for/registrations/new.html.erb +17 -0
- data/lib/generators/templates/simple_form_for/sessions/new.html.erb +15 -0
- data/lib/generators/templates/simple_form_for/unlocks/new.html.erb +15 -0
- data/test/controllers/custom_strategy_test.rb +63 -0
- data/test/controllers/helpers_test.rb +254 -0
- data/test/controllers/internal_helpers_test.rb +111 -0
- data/test/controllers/sessions_controller_test.rb +58 -0
- data/test/controllers/url_helpers_test.rb +60 -0
- data/test/delegator_test.rb +20 -0
- data/test/devise_test.rb +73 -0
- data/test/failure_app_test.rb +222 -0
- data/test/generators/active_record_generator_test.rb +76 -0
- data/test/generators/devise_generator_test.rb +40 -0
- data/test/generators/install_generator_test.rb +14 -0
- data/test/generators/mongoid_generator_test.rb +24 -0
- data/test/generators/views_generator_test.rb +53 -0
- data/test/helpers/devise_helper_test.rb +52 -0
- data/test/indifferent_hash.rb +34 -0
- data/test/integration/authenticatable_test.rb +634 -0
- data/test/integration/confirmable_test.rb +299 -0
- data/test/integration/database_authenticatable_test.rb +83 -0
- data/test/integration/http_authenticatable_test.rb +98 -0
- data/test/integration/lockable_test.rb +243 -0
- data/test/integration/omniauthable_test.rb +134 -0
- data/test/integration/recoverable_test.rb +307 -0
- data/test/integration/registerable_test.rb +346 -0
- data/test/integration/rememberable_test.rb +159 -0
- data/test/integration/timeoutable_test.rb +141 -0
- data/test/integration/token_authenticatable_test.rb +162 -0
- data/test/integration/trackable_test.rb +93 -0
- data/test/mailers/confirmation_instructions_test.rb +103 -0
- data/test/mailers/reset_password_instructions_test.rb +84 -0
- data/test/mailers/unlock_instructions_test.rb +78 -0
- data/test/mapping_test.rb +128 -0
- data/test/models/authenticatable_test.rb +8 -0
- data/test/models/confirmable_test.rb +392 -0
- data/test/models/database_authenticatable_test.rb +190 -0
- data/test/models/lockable_test.rb +274 -0
- data/test/models/omniauthable_test.rb +8 -0
- data/test/models/recoverable_test.rb +206 -0
- data/test/models/registerable_test.rb +8 -0
- data/test/models/rememberable_test.rb +175 -0
- data/test/models/serializable_test.rb +49 -0
- data/test/models/timeoutable_test.rb +47 -0
- data/test/models/token_authenticatable_test.rb +56 -0
- data/test/models/trackable_test.rb +14 -0
- data/test/models/validatable_test.rb +117 -0
- data/test/models_test.rb +180 -0
- data/test/omniauth/config_test.rb +58 -0
- data/test/omniauth/url_helpers_test.rb +52 -0
- data/test/orm/active_record.rb +10 -0
- data/test/orm/mongoid.rb +15 -0
- data/test/rails_app/Rakefile +10 -0
- data/test/rails_app/app/active_record/admin.rb +7 -0
- data/test/rails_app/app/active_record/shim.rb +3 -0
- data/test/rails_app/app/active_record/user.rb +7 -0
- data/test/rails_app/app/controllers/admins/sessions_controller.rb +7 -0
- data/test/rails_app/app/controllers/admins_controller.rb +12 -0
- data/test/rails_app/app/controllers/application_controller.rb +9 -0
- data/test/rails_app/app/controllers/home_controller.rb +26 -0
- data/test/rails_app/app/controllers/publisher/registrations_controller.rb +3 -0
- data/test/rails_app/app/controllers/publisher/sessions_controller.rb +3 -0
- data/test/rails_app/app/controllers/users/omniauth_callbacks_controller.rb +15 -0
- data/test/rails_app/app/controllers/users_controller.rb +24 -0
- data/test/rails_app/app/helpers/application_helper.rb +4 -0
- data/test/rails_app/app/mailers/users/mailer.rb +9 -0
- data/test/rails_app/app/mongoid/admin.rb +28 -0
- data/test/rails_app/app/mongoid/shim.rb +25 -0
- data/test/rails_app/app/mongoid/user.rb +43 -0
- data/test/rails_app/app/views/admins/index.html.erb +1 -0
- data/test/rails_app/app/views/admins/sessions/new.html.erb +2 -0
- data/test/rails_app/app/views/home/admin_dashboard.html.erb +1 -0
- data/test/rails_app/app/views/home/index.html.erb +1 -0
- data/test/rails_app/app/views/home/join.html.erb +1 -0
- data/test/rails_app/app/views/home/private.html.erb +1 -0
- data/test/rails_app/app/views/home/user_dashboard.html.erb +1 -0
- data/test/rails_app/app/views/layouts/application.html.erb +24 -0
- data/test/rails_app/app/views/users/index.html.erb +1 -0
- data/test/rails_app/app/views/users/mailer/confirmation_instructions.erb +1 -0
- data/test/rails_app/app/views/users/sessions/new.html.erb +1 -0
- data/test/rails_app/config/application.rb +42 -0
- data/test/rails_app/config/boot.rb +9 -0
- data/test/rails_app/config/database.yml +18 -0
- data/test/rails_app/config/environment.rb +6 -0
- data/test/rails_app/config/environments/development.rb +19 -0
- data/test/rails_app/config/environments/production.rb +34 -0
- data/test/rails_app/config/environments/test.rb +34 -0
- data/test/rails_app/config/initializers/backtrace_silencers.rb +8 -0
- data/test/rails_app/config/initializers/devise.rb +179 -0
- data/test/rails_app/config/initializers/inflections.rb +3 -0
- data/test/rails_app/config/initializers/secret_token.rb +3 -0
- data/test/rails_app/config/routes.rb +101 -0
- data/test/rails_app/config.ru +4 -0
- data/test/rails_app/db/migrate/20100401102949_create_tables.rb +75 -0
- data/test/rails_app/db/schema.rb +53 -0
- data/test/rails_app/lib/shared_admin.rb +15 -0
- data/test/rails_app/lib/shared_user.rb +27 -0
- data/test/rails_app/public/404.html +26 -0
- data/test/rails_app/public/422.html +26 -0
- data/test/rails_app/public/500.html +26 -0
- data/test/rails_app/public/favicon.ico +0 -0
- data/test/rails_app/script/rails +10 -0
- data/test/routes_test.rb +249 -0
- data/test/support/assertions.rb +41 -0
- data/test/support/helpers.rb +92 -0
- data/test/support/integration.rb +93 -0
- data/test/support/locale/en.yml +4 -0
- data/test/support/webrat/integrations/rails.rb +25 -0
- data/test/test_helper.rb +28 -0
- data/test/test_helpers_test.rb +152 -0
- metadata +407 -0
@@ -0,0 +1,274 @@
|
|
1
|
+
# -*- encoding : utf-8 -*-
|
2
|
+
require 'test_helper'
|
3
|
+
|
4
|
+
class LockableTest < ActiveSupport::TestCase
|
5
|
+
def setup
|
6
|
+
setup_mailer
|
7
|
+
end
|
8
|
+
|
9
|
+
test "should respect maximum attempts configuration" do
|
10
|
+
user = create_user
|
11
|
+
user.confirm!
|
12
|
+
swap Devise, :maximum_attempts => 2 do
|
13
|
+
3.times { user.valid_for_authentication?{ false } }
|
14
|
+
assert user.reload.access_locked?
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
test "should increment failed_attempts on successfull validation if the user is already locked" do
|
19
|
+
user = create_user
|
20
|
+
user.confirm!
|
21
|
+
|
22
|
+
swap Devise, :maximum_attempts => 2 do
|
23
|
+
3.times { user.valid_for_authentication?{ false } }
|
24
|
+
assert user.reload.access_locked?
|
25
|
+
end
|
26
|
+
|
27
|
+
user.valid_for_authentication?{ true }
|
28
|
+
assert_equal 4, user.reload.failed_attempts
|
29
|
+
end
|
30
|
+
|
31
|
+
test "should not touch failed_attempts if lock_strategy is none" do
|
32
|
+
user = create_user
|
33
|
+
user.confirm!
|
34
|
+
swap Devise, :lock_strategy => :none, :maximum_attempts => 2 do
|
35
|
+
3.times { user.valid_for_authentication?{ false } }
|
36
|
+
assert !user.access_locked?
|
37
|
+
assert_equal 0, user.failed_attempts
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
test 'should be valid for authentication with a unlocked user' do
|
42
|
+
user = create_user
|
43
|
+
user.lock_access!
|
44
|
+
user.unlock_access!
|
45
|
+
assert user.valid_for_authentication?{ true }
|
46
|
+
end
|
47
|
+
|
48
|
+
test "should verify whether a user is locked or not" do
|
49
|
+
user = create_user
|
50
|
+
assert_not user.access_locked?
|
51
|
+
user.lock_access!
|
52
|
+
assert user.access_locked?
|
53
|
+
end
|
54
|
+
|
55
|
+
test "active_for_authentication? should be the opposite of locked?" do
|
56
|
+
user = create_user
|
57
|
+
user.confirm!
|
58
|
+
assert user.active_for_authentication?
|
59
|
+
user.lock_access!
|
60
|
+
assert_not user.active_for_authentication?
|
61
|
+
end
|
62
|
+
|
63
|
+
test "should unlock a user by cleaning locked_at, falied_attempts and unlock_token" do
|
64
|
+
user = create_user
|
65
|
+
user.lock_access!
|
66
|
+
assert_not_nil user.reload.locked_at
|
67
|
+
assert_not_nil user.reload.unlock_token
|
68
|
+
|
69
|
+
user.unlock_access!
|
70
|
+
assert_nil user.reload.locked_at
|
71
|
+
assert_nil user.reload.unlock_token
|
72
|
+
assert_equal 0, user.reload.failed_attempts
|
73
|
+
end
|
74
|
+
|
75
|
+
test "new user should not be locked and should have zero failed_attempts" do
|
76
|
+
assert_not new_user.access_locked?
|
77
|
+
assert_equal 0, create_user.failed_attempts
|
78
|
+
end
|
79
|
+
|
80
|
+
test "should unlock user after unlock_in period" do
|
81
|
+
swap Devise, :unlock_in => 3.hours do
|
82
|
+
user = new_user
|
83
|
+
user.locked_at = 2.hours.ago
|
84
|
+
assert user.access_locked?
|
85
|
+
|
86
|
+
Devise.unlock_in = 1.hour
|
87
|
+
assert_not user.access_locked?
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
test "should not unlock in 'unlock_in' if :time unlock strategy is not set" do
|
92
|
+
swap Devise, :unlock_strategy => :email do
|
93
|
+
user = new_user
|
94
|
+
user.locked_at = 2.hours.ago
|
95
|
+
assert user.access_locked?
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
test "should set unlock_token when locking" do
|
100
|
+
user = create_user
|
101
|
+
assert_nil user.unlock_token
|
102
|
+
user.lock_access!
|
103
|
+
assert_not_nil user.unlock_token
|
104
|
+
end
|
105
|
+
|
106
|
+
test "should never generate the same unlock token for different users" do
|
107
|
+
unlock_tokens = []
|
108
|
+
3.times do
|
109
|
+
user = create_user
|
110
|
+
user.lock_access!
|
111
|
+
token = user.unlock_token
|
112
|
+
assert !unlock_tokens.include?(token)
|
113
|
+
unlock_tokens << token
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
117
|
+
test "should not generate unlock_token when :email is not an unlock strategy" do
|
118
|
+
swap Devise, :unlock_strategy => :time do
|
119
|
+
user = create_user
|
120
|
+
user.lock_access!
|
121
|
+
assert_nil user.unlock_token
|
122
|
+
end
|
123
|
+
end
|
124
|
+
|
125
|
+
test "should send email with unlock instructions when :email is an unlock strategy" do
|
126
|
+
swap Devise, :unlock_strategy => :email do
|
127
|
+
user = create_user
|
128
|
+
assert_email_sent do
|
129
|
+
user.lock_access!
|
130
|
+
end
|
131
|
+
end
|
132
|
+
end
|
133
|
+
|
134
|
+
test "should not send email with unlock instructions when :email is not an unlock strategy" do
|
135
|
+
swap Devise, :unlock_strategy => :time do
|
136
|
+
user = create_user
|
137
|
+
assert_email_not_sent do
|
138
|
+
user.lock_access!
|
139
|
+
end
|
140
|
+
end
|
141
|
+
end
|
142
|
+
|
143
|
+
test 'should find and unlock a user automatically' do
|
144
|
+
user = create_user
|
145
|
+
user.lock_access!
|
146
|
+
locked_user = User.unlock_access_by_token(user.unlock_token)
|
147
|
+
assert_equal locked_user, user
|
148
|
+
assert_not user.reload.access_locked?
|
149
|
+
end
|
150
|
+
|
151
|
+
test 'should return a new record with errors when a invalid token is given' do
|
152
|
+
locked_user = User.unlock_access_by_token('invalid_token')
|
153
|
+
assert_not locked_user.persisted?
|
154
|
+
assert_equal "is invalid", locked_user.errors[:unlock_token].join
|
155
|
+
end
|
156
|
+
|
157
|
+
test 'should return a new record with errors when a blank token is given' do
|
158
|
+
locked_user = User.unlock_access_by_token('')
|
159
|
+
assert_not locked_user.persisted?
|
160
|
+
assert_equal "can't be blank", locked_user.errors[:unlock_token].join
|
161
|
+
end
|
162
|
+
|
163
|
+
test 'should find a user to send unlock instructions' do
|
164
|
+
user = create_user
|
165
|
+
user.lock_access!
|
166
|
+
unlock_user = User.send_unlock_instructions(:email => user.email)
|
167
|
+
assert_equal unlock_user, user
|
168
|
+
end
|
169
|
+
|
170
|
+
test 'should return a new user if no email was found' do
|
171
|
+
unlock_user = User.send_unlock_instructions(:email => "invalid@example.com")
|
172
|
+
assert_not unlock_user.persisted?
|
173
|
+
end
|
174
|
+
|
175
|
+
test 'should add error to new user email if no email was found' do
|
176
|
+
unlock_user = User.send_unlock_instructions(:email => "invalid@example.com")
|
177
|
+
assert_equal 'not found', unlock_user.errors[:email].join
|
178
|
+
end
|
179
|
+
|
180
|
+
test 'should find a user to send unlock instructions by authentication_keys' do
|
181
|
+
swap Devise, :authentication_keys => [:username, :email] do
|
182
|
+
user = create_user
|
183
|
+
unlock_user = User.send_unlock_instructions(:email => user.email, :username => user.username)
|
184
|
+
assert_equal unlock_user, user
|
185
|
+
end
|
186
|
+
end
|
187
|
+
|
188
|
+
test 'should require all unlock_keys' do
|
189
|
+
swap Devise, :unlock_keys => [:username, :email] do
|
190
|
+
user = create_user
|
191
|
+
unlock_user = User.send_unlock_instructions(:email => user.email)
|
192
|
+
assert_not unlock_user.persisted?
|
193
|
+
assert_equal "can't be blank", unlock_user.errors[:username].join
|
194
|
+
end
|
195
|
+
end
|
196
|
+
|
197
|
+
test 'should not be able to send instructions if the user is not locked' do
|
198
|
+
user = create_user
|
199
|
+
assert_not user.resend_unlock_token
|
200
|
+
assert_not user.access_locked?
|
201
|
+
assert_equal 'was not locked', user.errors[:email].join
|
202
|
+
end
|
203
|
+
|
204
|
+
test 'should unlock account if lock has expired and increase attempts on failure' do
|
205
|
+
swap Devise, :unlock_in => 1.minute do
|
206
|
+
user = create_user
|
207
|
+
user.confirm!
|
208
|
+
|
209
|
+
user.failed_attempts = 2
|
210
|
+
user.locked_at = 2.minutes.ago
|
211
|
+
|
212
|
+
user.valid_for_authentication? { false }
|
213
|
+
assert_equal 1, user.failed_attempts
|
214
|
+
end
|
215
|
+
end
|
216
|
+
|
217
|
+
test 'should unlock account if lock has expired on success' do
|
218
|
+
swap Devise, :unlock_in => 1.minute do
|
219
|
+
user = create_user
|
220
|
+
user.confirm!
|
221
|
+
|
222
|
+
user.failed_attempts = 2
|
223
|
+
user.locked_at = 2.minutes.ago
|
224
|
+
|
225
|
+
user.valid_for_authentication? { true }
|
226
|
+
assert_equal 0, user.failed_attempts
|
227
|
+
assert_nil user.locked_at
|
228
|
+
end
|
229
|
+
end
|
230
|
+
|
231
|
+
test 'required_fields should contain the all the fields when all the strategies are enabled' do
|
232
|
+
swap Devise, :unlock_strategy => :both do
|
233
|
+
swap Devise, :lock_strategy => :failed_attempts do
|
234
|
+
assert_same_content Devise::Models::Lockable.required_fields(User), [
|
235
|
+
:failed_attempts,
|
236
|
+
:locked_at,
|
237
|
+
:unlock_token
|
238
|
+
]
|
239
|
+
end
|
240
|
+
end
|
241
|
+
end
|
242
|
+
|
243
|
+
test 'required_fields should contain only failed_attempts and locked_at when the strategies are time and failed_attempts are enabled' do
|
244
|
+
swap Devise, :unlock_strategy => :time do
|
245
|
+
swap Devise, :lock_strategy => :failed_attempts do
|
246
|
+
assert_same_content Devise::Models::Lockable.required_fields(User), [
|
247
|
+
:failed_attempts,
|
248
|
+
:locked_at
|
249
|
+
]
|
250
|
+
end
|
251
|
+
end
|
252
|
+
end
|
253
|
+
|
254
|
+
test 'required_fields should contain only failed_attempts and unlock_token when the strategies are token and failed_attempts are enabled' do
|
255
|
+
swap Devise, :unlock_strategy => :email do
|
256
|
+
swap Devise, :lock_strategy => :failed_attempts do
|
257
|
+
assert_same_content Devise::Models::Lockable.required_fields(User), [
|
258
|
+
:failed_attempts,
|
259
|
+
:unlock_token
|
260
|
+
]
|
261
|
+
end
|
262
|
+
end
|
263
|
+
end
|
264
|
+
|
265
|
+
test 'should not return a locked unauthenticated message if in paranoid mode' do
|
266
|
+
swap Devise, :paranoid => :true do
|
267
|
+
user = create_user
|
268
|
+
user.failed_attempts = Devise.maximum_attempts + 1
|
269
|
+
user.lock_access!
|
270
|
+
|
271
|
+
assert_equal :invalid, user.unauthenticated_message
|
272
|
+
end
|
273
|
+
end
|
274
|
+
end
|
@@ -0,0 +1,206 @@
|
|
1
|
+
# -*- encoding : utf-8 -*-
|
2
|
+
require 'test_helper'
|
3
|
+
|
4
|
+
class RecoverableTest < ActiveSupport::TestCase
|
5
|
+
|
6
|
+
def setup
|
7
|
+
setup_mailer
|
8
|
+
end
|
9
|
+
|
10
|
+
test 'should not generate reset password token after creating a record' do
|
11
|
+
assert_nil new_user.reset_password_token
|
12
|
+
end
|
13
|
+
|
14
|
+
test 'should never generate the same reset password token for different users' do
|
15
|
+
reset_password_tokens = []
|
16
|
+
3.times do
|
17
|
+
user = create_user
|
18
|
+
user.send_reset_password_instructions
|
19
|
+
token = user.reset_password_token
|
20
|
+
assert !reset_password_tokens.include?(token)
|
21
|
+
reset_password_tokens << token
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
test 'should reset password and password confirmation from params' do
|
26
|
+
user = create_user
|
27
|
+
user.reset_password!('123456789', '987654321')
|
28
|
+
assert_equal '123456789', user.password
|
29
|
+
assert_equal '987654321', user.password_confirmation
|
30
|
+
end
|
31
|
+
|
32
|
+
test 'should reset password and save the record' do
|
33
|
+
assert create_user.reset_password!('123456789', '123456789')
|
34
|
+
end
|
35
|
+
|
36
|
+
test 'should clear reset password token while reseting the password' do
|
37
|
+
user = create_user
|
38
|
+
assert_nil user.reset_password_token
|
39
|
+
|
40
|
+
user.send_reset_password_instructions
|
41
|
+
assert_present user.reset_password_token
|
42
|
+
assert user.reset_password!('123456789', '123456789')
|
43
|
+
assert_nil user.reset_password_token
|
44
|
+
end
|
45
|
+
|
46
|
+
test 'should not clear reset password token if record is invalid' do
|
47
|
+
user = create_user
|
48
|
+
user.send_reset_password_instructions
|
49
|
+
assert_present user.reset_password_token
|
50
|
+
assert_not user.reset_password!('123456789', '987654321')
|
51
|
+
assert_present user.reset_password_token
|
52
|
+
end
|
53
|
+
|
54
|
+
test 'should not reset password with invalid data' do
|
55
|
+
user = create_user
|
56
|
+
user.stubs(:valid?).returns(false)
|
57
|
+
assert_not user.reset_password!('123456789', '987654321')
|
58
|
+
end
|
59
|
+
|
60
|
+
test 'should reset reset password token and send instructions by email' do
|
61
|
+
user = create_user
|
62
|
+
assert_email_sent do
|
63
|
+
token = user.reset_password_token
|
64
|
+
user.send_reset_password_instructions
|
65
|
+
assert_not_equal token, user.reset_password_token
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
test 'should find a user to send instructions by email' do
|
70
|
+
user = create_user
|
71
|
+
reset_password_user = User.send_reset_password_instructions(:email => user.email)
|
72
|
+
assert_equal reset_password_user, user
|
73
|
+
end
|
74
|
+
|
75
|
+
test 'should return a new record with errors if user was not found by e-mail' do
|
76
|
+
reset_password_user = User.send_reset_password_instructions(:email => "invalid@example.com")
|
77
|
+
assert_not reset_password_user.persisted?
|
78
|
+
assert_equal "not found", reset_password_user.errors[:email].join
|
79
|
+
end
|
80
|
+
|
81
|
+
test 'should find a user to send instructions by authentication_keys' do
|
82
|
+
swap Devise, :authentication_keys => [:username, :email] do
|
83
|
+
user = create_user
|
84
|
+
reset_password_user = User.send_reset_password_instructions(:email => user.email, :username => user.username)
|
85
|
+
assert_equal reset_password_user, user
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
test 'should require all reset_password_keys' do
|
90
|
+
swap Devise, :reset_password_keys => [:username, :email] do
|
91
|
+
user = create_user
|
92
|
+
reset_password_user = User.send_reset_password_instructions(:email => user.email)
|
93
|
+
assert_not reset_password_user.persisted?
|
94
|
+
assert_equal "can't be blank", reset_password_user.errors[:username].join
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
test 'should reset reset_password_token before send the reset instructions email' do
|
99
|
+
user = create_user
|
100
|
+
token = user.reset_password_token
|
101
|
+
User.send_reset_password_instructions(:email => user.email)
|
102
|
+
assert_not_equal token, user.reload.reset_password_token
|
103
|
+
end
|
104
|
+
|
105
|
+
test 'should send email instructions to the user reset his password' do
|
106
|
+
user = create_user
|
107
|
+
assert_email_sent do
|
108
|
+
User.send_reset_password_instructions(:email => user.email)
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
112
|
+
test 'should find a user to reset his password based on reset_password_token' do
|
113
|
+
user = create_user
|
114
|
+
user.send :generate_reset_password_token!
|
115
|
+
|
116
|
+
reset_password_user = User.reset_password_by_token(:reset_password_token => user.reset_password_token)
|
117
|
+
assert_equal reset_password_user, user
|
118
|
+
end
|
119
|
+
|
120
|
+
test 'should return a new record with errors if no reset_password_token is found' do
|
121
|
+
reset_password_user = User.reset_password_by_token(:reset_password_token => 'invalid_token')
|
122
|
+
assert_not reset_password_user.persisted?
|
123
|
+
assert_equal "is invalid", reset_password_user.errors[:reset_password_token].join
|
124
|
+
end
|
125
|
+
|
126
|
+
test 'should return a new record with errors if reset_password_token is blank' do
|
127
|
+
reset_password_user = User.reset_password_by_token(:reset_password_token => '')
|
128
|
+
assert_not reset_password_user.persisted?
|
129
|
+
assert_match "can't be blank", reset_password_user.errors[:reset_password_token].join
|
130
|
+
end
|
131
|
+
|
132
|
+
test 'should return a new record with errors if password is blank' do
|
133
|
+
user = create_user
|
134
|
+
user.send :generate_reset_password_token!
|
135
|
+
|
136
|
+
reset_password_user = User.reset_password_by_token(:reset_password_token => user.reset_password_token, :password => '')
|
137
|
+
assert_not reset_password_user.errors.empty?
|
138
|
+
assert_match "can't be blank", reset_password_user.errors[:password].join
|
139
|
+
end
|
140
|
+
|
141
|
+
test 'should reset successfully user password given the new password and confirmation' do
|
142
|
+
user = create_user
|
143
|
+
old_password = user.password
|
144
|
+
user.send :generate_reset_password_token!
|
145
|
+
|
146
|
+
User.reset_password_by_token(
|
147
|
+
:reset_password_token => user.reset_password_token,
|
148
|
+
:password => 'new_password',
|
149
|
+
:password_confirmation => 'new_password'
|
150
|
+
)
|
151
|
+
user.reload
|
152
|
+
|
153
|
+
assert_not user.valid_password?(old_password)
|
154
|
+
assert user.valid_password?('new_password')
|
155
|
+
end
|
156
|
+
|
157
|
+
test 'should not reset reset password token during reset_password_within time' do
|
158
|
+
swap Devise, :reset_password_within => 1.hour do
|
159
|
+
user = create_user
|
160
|
+
user.send_reset_password_instructions
|
161
|
+
3.times do
|
162
|
+
token = user.reset_password_token
|
163
|
+
user.send_reset_password_instructions
|
164
|
+
assert_equal token, user.reset_password_token
|
165
|
+
end
|
166
|
+
end
|
167
|
+
end
|
168
|
+
|
169
|
+
test 'should reset reset password token after reset_password_within time' do
|
170
|
+
swap Devise, :reset_password_within => 1.hour do
|
171
|
+
user = create_user
|
172
|
+
user.reset_password_sent_at = 2.days.ago
|
173
|
+
token = user.reset_password_token
|
174
|
+
user.send_reset_password_instructions
|
175
|
+
assert_not_equal token, user.reset_password_token
|
176
|
+
end
|
177
|
+
end
|
178
|
+
|
179
|
+
test 'should not reset password after reset_password_within time' do
|
180
|
+
swap Devise, :reset_password_within => 1.hour do
|
181
|
+
user = create_user
|
182
|
+
old_password = user.password
|
183
|
+
user.send :generate_reset_password_token!
|
184
|
+
user.reset_password_sent_at = 2.days.ago
|
185
|
+
user.save!
|
186
|
+
|
187
|
+
reset_password_user = User.reset_password_by_token(
|
188
|
+
:reset_password_token => user.reset_password_token,
|
189
|
+
:password => 'new_password',
|
190
|
+
:password_confirmation => 'new_password'
|
191
|
+
)
|
192
|
+
user.reload
|
193
|
+
|
194
|
+
assert user.valid_password?(old_password)
|
195
|
+
assert_not user.valid_password?('new_password')
|
196
|
+
assert_equal "has expired, please request a new one", reset_password_user.errors[:reset_password_token].join
|
197
|
+
end
|
198
|
+
end
|
199
|
+
|
200
|
+
test 'required_fields should contain the fields that Devise uses' do
|
201
|
+
assert_same_content Devise::Models::Recoverable.required_fields(User), [
|
202
|
+
:reset_password_sent_at,
|
203
|
+
:reset_password_token
|
204
|
+
]
|
205
|
+
end
|
206
|
+
end
|
@@ -0,0 +1,175 @@
|
|
1
|
+
# -*- encoding : utf-8 -*-
|
2
|
+
require 'test_helper'
|
3
|
+
|
4
|
+
class RememberableTest < ActiveSupport::TestCase
|
5
|
+
def resource_class
|
6
|
+
User
|
7
|
+
end
|
8
|
+
|
9
|
+
def create_resource
|
10
|
+
create_user
|
11
|
+
end
|
12
|
+
|
13
|
+
test 'remember_me should not generate a new token if using salt' do
|
14
|
+
user = create_user
|
15
|
+
user.expects(:valid?).never
|
16
|
+
user.remember_me!
|
17
|
+
end
|
18
|
+
|
19
|
+
test 'forget_me should not clear remember token if using salt' do
|
20
|
+
user = create_user
|
21
|
+
user.remember_me!
|
22
|
+
user.expects(:valid?).never
|
23
|
+
user.forget_me!
|
24
|
+
end
|
25
|
+
|
26
|
+
test 'serialize into cookie' do
|
27
|
+
user = create_user
|
28
|
+
user.remember_me!
|
29
|
+
assert_equal [user.to_key, user.authenticatable_salt], User.serialize_into_cookie(user)
|
30
|
+
end
|
31
|
+
|
32
|
+
test 'serialize from cookie' do
|
33
|
+
user = create_user
|
34
|
+
user.remember_me!
|
35
|
+
assert_equal user, User.serialize_from_cookie(user.to_key, user.authenticatable_salt)
|
36
|
+
end
|
37
|
+
|
38
|
+
test 'raises a RuntimeError if authenticatable_salt is nil' do
|
39
|
+
user = User.new
|
40
|
+
user.encrypted_password = nil
|
41
|
+
assert_raise RuntimeError do
|
42
|
+
user.rememberable_value
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
test 'should respond to remember_me attribute' do
|
47
|
+
assert resource_class.new.respond_to?(:remember_me)
|
48
|
+
assert resource_class.new.respond_to?(:remember_me=)
|
49
|
+
end
|
50
|
+
|
51
|
+
test 'forget_me should clear remember_created_at' do
|
52
|
+
resource = create_resource
|
53
|
+
resource.remember_me!
|
54
|
+
assert_not resource.remember_created_at.nil?
|
55
|
+
resource.forget_me!
|
56
|
+
assert resource.remember_created_at.nil?
|
57
|
+
end
|
58
|
+
|
59
|
+
test 'forget_me should not try to update resource if it has been destroyed' do
|
60
|
+
resource = create_resource
|
61
|
+
resource.destroy
|
62
|
+
resource.expects(:remember_created_at).never
|
63
|
+
resource.expects(:save).never
|
64
|
+
resource.forget_me!
|
65
|
+
end
|
66
|
+
|
67
|
+
test 'remember is expired if not created at timestamp is set' do
|
68
|
+
assert create_resource.remember_expired?
|
69
|
+
end
|
70
|
+
|
71
|
+
test 'serialize should return nil if no resource is found' do
|
72
|
+
assert_nil resource_class.serialize_from_cookie([0], "123")
|
73
|
+
end
|
74
|
+
|
75
|
+
test 'remember me return nil if is a valid resource with invalid token' do
|
76
|
+
resource = create_resource
|
77
|
+
assert_nil resource_class.serialize_from_cookie([resource.id], "123")
|
78
|
+
end
|
79
|
+
|
80
|
+
test 'remember for should fallback to devise remember for default configuration' do
|
81
|
+
swap Devise, :remember_for => 1.day do
|
82
|
+
resource = create_resource
|
83
|
+
resource.remember_me!
|
84
|
+
assert_not resource.remember_expired?
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
test 'remember expires at should sum date of creation with remember for configuration' do
|
89
|
+
swap Devise, :remember_for => 3.days do
|
90
|
+
resource = create_resource
|
91
|
+
resource.remember_me!
|
92
|
+
assert_equal 3.days.from_now.to_date, resource.remember_expires_at.to_date
|
93
|
+
|
94
|
+
Devise.remember_for = 5.days
|
95
|
+
assert_equal 5.days.from_now.to_date, resource.remember_expires_at.to_date
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
test 'remember should be expired if remember_for is zero' do
|
100
|
+
swap Devise, :remember_for => 0.days do
|
101
|
+
Devise.remember_for = 0.days
|
102
|
+
resource = create_resource
|
103
|
+
resource.remember_me!
|
104
|
+
assert resource.remember_expired?
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
test 'remember should be expired if it was created before limit time' do
|
109
|
+
swap Devise, :remember_for => 1.day do
|
110
|
+
resource = create_resource
|
111
|
+
resource.remember_me!
|
112
|
+
resource.remember_created_at = 2.days.ago
|
113
|
+
resource.save
|
114
|
+
assert resource.remember_expired?
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
118
|
+
test 'remember should not be expired if it was created whitin the limit time' do
|
119
|
+
swap Devise, :remember_for => 30.days do
|
120
|
+
resource = create_resource
|
121
|
+
resource.remember_me!
|
122
|
+
resource.remember_created_at = (30.days.ago + 2.minutes)
|
123
|
+
resource.save
|
124
|
+
assert_not resource.remember_expired?
|
125
|
+
end
|
126
|
+
end
|
127
|
+
|
128
|
+
test 'if extend_remember_period is false, remember_me! should generate a new timestamp if expired' do
|
129
|
+
swap Devise, :remember_for => 5.minutes do
|
130
|
+
resource = create_resource
|
131
|
+
resource.remember_me!(false)
|
132
|
+
assert resource.remember_created_at
|
133
|
+
|
134
|
+
resource.remember_created_at = old = 10.minutes.ago
|
135
|
+
resource.save
|
136
|
+
|
137
|
+
resource.remember_me!(false)
|
138
|
+
assert_not_equal old.to_i, resource.remember_created_at.to_i
|
139
|
+
end
|
140
|
+
end
|
141
|
+
|
142
|
+
test 'if extend_remember_period is false, remember_me! should not generate a new timestamp' do
|
143
|
+
swap Devise, :remember_for => 1.year do
|
144
|
+
resource = create_resource
|
145
|
+
resource.remember_me!(false)
|
146
|
+
assert resource.remember_created_at
|
147
|
+
|
148
|
+
resource.remember_created_at = old = 10.minutes.ago.utc
|
149
|
+
resource.save
|
150
|
+
|
151
|
+
resource.remember_me!(false)
|
152
|
+
assert_equal old.to_i, resource.remember_created_at.to_i
|
153
|
+
end
|
154
|
+
end
|
155
|
+
|
156
|
+
test 'if extend_remember_period is true, remember_me! should always generate a new timestamp' do
|
157
|
+
swap Devise, :remember_for => 1.year do
|
158
|
+
resource = create_resource
|
159
|
+
resource.remember_me!(true)
|
160
|
+
assert resource.remember_created_at
|
161
|
+
|
162
|
+
resource.remember_created_at = old = 10.minutes.ago
|
163
|
+
resource.save
|
164
|
+
|
165
|
+
resource.remember_me!(true)
|
166
|
+
assert_not_equal old, resource.remember_created_at
|
167
|
+
end
|
168
|
+
end
|
169
|
+
|
170
|
+
test 'should have the required_fiels array' do
|
171
|
+
assert_same_content Devise::Models::Rememberable.required_fields(User), [
|
172
|
+
:remember_created_at
|
173
|
+
]
|
174
|
+
end
|
175
|
+
end
|