devise 3.3.0 → 3.5.10

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of devise might be problematic. Click here for more details.

Files changed (138) hide show
  1. checksums.yaml +4 -4
  2. data/.travis.yml +29 -20
  3. data/CHANGELOG.md +219 -102
  4. data/CODE_OF_CONDUCT.md +22 -0
  5. data/CONTRIBUTING.md +2 -0
  6. data/Gemfile +3 -2
  7. data/Gemfile.lock +101 -80
  8. data/MIT-LICENSE +1 -1
  9. data/README.md +87 -43
  10. data/Rakefile +2 -1
  11. data/app/controllers/devise/confirmations_controller.rb +5 -1
  12. data/app/controllers/devise/omniauth_callbacks_controller.rb +4 -0
  13. data/app/controllers/devise/passwords_controller.rb +14 -4
  14. data/app/controllers/devise/registrations_controller.rb +10 -11
  15. data/app/controllers/devise/sessions_controller.rb +7 -2
  16. data/app/controllers/devise/unlocks_controller.rb +3 -0
  17. data/app/controllers/devise_controller.rb +44 -21
  18. data/app/mailers/devise/mailer.rb +4 -0
  19. data/app/views/devise/confirmations/new.html.erb +7 -3
  20. data/app/views/devise/mailer/password_change.html.erb +3 -0
  21. data/app/views/devise/passwords/edit.html.erb +14 -5
  22. data/app/views/devise/passwords/new.html.erb +7 -3
  23. data/app/views/devise/registrations/edit.html.erb +19 -9
  24. data/app/views/devise/registrations/new.html.erb +18 -7
  25. data/app/views/devise/sessions/new.html.erb +15 -6
  26. data/app/views/devise/shared/{_links.erb → _links.html.erb} +1 -1
  27. data/app/views/devise/unlocks/new.html.erb +7 -3
  28. data/config/locales/en.yml +4 -2
  29. data/devise.gemspec +2 -2
  30. data/gemfiles/Gemfile.rails-3.2-stable.lock +54 -48
  31. data/gemfiles/Gemfile.rails-4.0-stable +1 -0
  32. data/gemfiles/Gemfile.rails-4.0-stable.lock +63 -59
  33. data/gemfiles/{Gemfile.rails-head → Gemfile.rails-4.1-stable} +3 -5
  34. data/gemfiles/Gemfile.rails-4.1-stable.lock +171 -0
  35. data/gemfiles/Gemfile.rails-4.2-stable +30 -0
  36. data/gemfiles/Gemfile.rails-4.2-stable.lock +193 -0
  37. data/lib/devise/controllers/helpers.rb +12 -6
  38. data/lib/devise/controllers/rememberable.rb +9 -2
  39. data/lib/devise/controllers/sign_in_out.rb +2 -8
  40. data/lib/devise/controllers/store_location.rb +3 -1
  41. data/lib/devise/controllers/url_helpers.rb +7 -9
  42. data/lib/devise/encryptor.rb +22 -0
  43. data/lib/devise/failure_app.rb +56 -14
  44. data/lib/devise/hooks/timeoutable.rb +5 -7
  45. data/lib/devise/mapping.rb +2 -1
  46. data/lib/devise/models/authenticatable.rb +28 -28
  47. data/lib/devise/models/confirmable.rb +51 -17
  48. data/lib/devise/models/database_authenticatable.rb +17 -11
  49. data/lib/devise/models/lockable.rb +7 -3
  50. data/lib/devise/models/recoverable.rb +23 -15
  51. data/lib/devise/models/rememberable.rb +56 -22
  52. data/lib/devise/models/timeoutable.rb +0 -6
  53. data/lib/devise/models/trackable.rb +1 -2
  54. data/lib/devise/models/validatable.rb +3 -3
  55. data/lib/devise/models.rb +1 -1
  56. data/lib/devise/rails/routes.rb +33 -27
  57. data/lib/devise/rails.rb +1 -1
  58. data/lib/devise/strategies/authenticatable.rb +8 -6
  59. data/lib/devise/strategies/database_authenticatable.rb +2 -1
  60. data/lib/devise/strategies/rememberable.rb +13 -3
  61. data/lib/devise/test_helpers.rb +2 -2
  62. data/lib/devise/version.rb +1 -1
  63. data/lib/devise.rb +39 -37
  64. data/lib/generators/active_record/devise_generator.rb +2 -1
  65. data/lib/generators/active_record/templates/migration.rb +1 -1
  66. data/lib/generators/active_record/templates/migration_existing.rb +1 -1
  67. data/lib/generators/devise/controllers_generator.rb +44 -0
  68. data/lib/generators/devise/views_generator.rb +14 -3
  69. data/lib/generators/templates/controllers/README +14 -0
  70. data/lib/generators/templates/controllers/confirmations_controller.rb +28 -0
  71. data/lib/generators/templates/controllers/omniauth_callbacks_controller.rb +28 -0
  72. data/lib/generators/templates/controllers/passwords_controller.rb +32 -0
  73. data/lib/generators/templates/controllers/registrations_controller.rb +60 -0
  74. data/lib/generators/templates/controllers/sessions_controller.rb +25 -0
  75. data/lib/generators/templates/controllers/unlocks_controller.rb +28 -0
  76. data/lib/generators/templates/devise.rb +19 -13
  77. data/lib/generators/templates/markerb/confirmation_instructions.markerb +1 -1
  78. data/lib/generators/templates/markerb/password_change.markerb +3 -0
  79. data/lib/generators/templates/markerb/reset_password_instructions.markerb +1 -1
  80. data/lib/generators/templates/markerb/unlock_instructions.markerb +1 -1
  81. data/lib/generators/templates/simple_form_for/passwords/edit.html.erb +1 -1
  82. data/lib/generators/templates/simple_form_for/registrations/new.html.erb +1 -1
  83. data/lib/generators/templates/simple_form_for/sessions/new.html.erb +2 -2
  84. data/test/controllers/custom_registrations_controller_test.rb +6 -1
  85. data/test/controllers/helper_methods_test.rb +21 -0
  86. data/test/controllers/helpers_test.rb +5 -0
  87. data/test/controllers/inherited_controller_i18n_messages_test.rb +51 -0
  88. data/test/controllers/internal_helpers_test.rb +10 -4
  89. data/test/controllers/load_hooks_controller_test.rb +19 -0
  90. data/test/controllers/passwords_controller_test.rb +1 -1
  91. data/test/controllers/sessions_controller_test.rb +3 -3
  92. data/test/controllers/url_helpers_test.rb +6 -0
  93. data/test/devise_test.rb +3 -3
  94. data/test/failure_app_test.rb +47 -0
  95. data/test/generators/controllers_generator_test.rb +48 -0
  96. data/test/generators/views_generator_test.rb +8 -1
  97. data/test/helpers/devise_helper_test.rb +9 -12
  98. data/test/integration/authenticatable_test.rb +1 -1
  99. data/test/integration/database_authenticatable_test.rb +11 -0
  100. data/test/integration/http_authenticatable_test.rb +1 -1
  101. data/test/integration/omniauthable_test.rb +12 -10
  102. data/test/integration/recoverable_test.rb +13 -0
  103. data/test/integration/rememberable_test.rb +50 -3
  104. data/test/integration/timeoutable_test.rb +13 -18
  105. data/test/mailers/confirmation_instructions_test.rb +1 -1
  106. data/test/mapping_test.rb +7 -0
  107. data/test/models/authenticatable_test.rb +10 -0
  108. data/test/models/confirmable_test.rb +99 -42
  109. data/test/models/database_authenticatable_test.rb +20 -0
  110. data/test/models/lockable_test.rb +45 -17
  111. data/test/models/recoverable_test.rb +62 -7
  112. data/test/models/rememberable_test.rb +68 -97
  113. data/test/models/validatable_test.rb +5 -5
  114. data/test/models_test.rb +15 -6
  115. data/test/rails_app/app/active_record/user_without_email.rb +8 -0
  116. data/test/rails_app/app/controllers/admins_controller.rb +0 -5
  117. data/test/rails_app/app/controllers/custom/registrations_controller.rb +10 -0
  118. data/test/rails_app/app/mailers/users/from_proc_mailer.rb +3 -0
  119. data/test/rails_app/app/mailers/users/mailer.rb +0 -9
  120. data/test/rails_app/app/mailers/users/reply_to_mailer.rb +4 -0
  121. data/test/rails_app/app/mongoid/user_without_email.rb +33 -0
  122. data/test/rails_app/config/application.rb +1 -1
  123. data/test/rails_app/config/environments/production.rb +6 -2
  124. data/test/rails_app/config/environments/test.rb +7 -2
  125. data/test/rails_app/config/initializers/devise.rb +12 -15
  126. data/test/rails_app/config/routes.rb +6 -3
  127. data/test/rails_app/db/migrate/20100401102949_create_tables.rb +2 -2
  128. data/test/rails_app/lib/shared_user.rb +1 -1
  129. data/test/rails_app/lib/shared_user_without_email.rb +26 -0
  130. data/test/rails_test.rb +9 -0
  131. data/test/support/helpers.rb +13 -6
  132. data/test/support/integration.rb +2 -2
  133. data/test/test_helper.rb +5 -0
  134. data/test/test_helpers_test.rb +22 -7
  135. data/test/test_models.rb +2 -2
  136. data/test/time_helpers.rb +137 -0
  137. metadata +58 -8
  138. data/gemfiles/Gemfile.rails-head.lock +0 -190
@@ -7,16 +7,16 @@ class LockableTest < ActiveSupport::TestCase
7
7
 
8
8
  test "should respect maximum attempts configuration" do
9
9
  user = create_user
10
- user.confirm!
10
+ user.confirm
11
11
  swap Devise, maximum_attempts: 2 do
12
12
  2.times { user.valid_for_authentication?{ false } }
13
13
  assert user.reload.access_locked?
14
14
  end
15
15
  end
16
16
 
17
- test "should increment failed_attempts on successfull validation if the user is already locked" do
17
+ test "should increment failed_attempts on successful validation if the user is already locked" do
18
18
  user = create_user
19
- user.confirm!
19
+ user.confirm
20
20
 
21
21
  swap Devise, maximum_attempts: 2 do
22
22
  2.times { user.valid_for_authentication?{ false } }
@@ -29,7 +29,7 @@ class LockableTest < ActiveSupport::TestCase
29
29
 
30
30
  test "should not touch failed_attempts if lock_strategy is none" do
31
31
  user = create_user
32
- user.confirm!
32
+ user.confirm
33
33
  swap Devise, lock_strategy: :none, maximum_attempts: 2 do
34
34
  3.times { user.valid_for_authentication?{ false } }
35
35
  assert !user.access_locked?
@@ -53,7 +53,7 @@ class LockableTest < ActiveSupport::TestCase
53
53
 
54
54
  test "active_for_authentication? should be the opposite of locked?" do
55
55
  user = create_user
56
- user.confirm!
56
+ user.confirm
57
57
  assert user.active_for_authentication?
58
58
  user.lock_access!
59
59
  assert_not user.active_for_authentication?
@@ -230,7 +230,7 @@ class LockableTest < ActiveSupport::TestCase
230
230
  test 'should unlock account if lock has expired and increase attempts on failure' do
231
231
  swap Devise, unlock_in: 1.minute do
232
232
  user = create_user
233
- user.confirm!
233
+ user.confirm
234
234
 
235
235
  user.failed_attempts = 2
236
236
  user.locked_at = 2.minutes.ago
@@ -243,7 +243,7 @@ class LockableTest < ActiveSupport::TestCase
243
243
  test 'should unlock account if lock has expired on success' do
244
244
  swap Devise, unlock_in: 1.minute do
245
245
  user = create_user
246
- user.confirm!
246
+ user.confirm
247
247
 
248
248
  user.failed_attempts = 2
249
249
  user.locked_at = 2.minutes.ago
@@ -299,18 +299,24 @@ class LockableTest < ActiveSupport::TestCase
299
299
  end
300
300
 
301
301
  test 'should return last attempt message if user made next-to-last attempt of password entering' do
302
- swap Devise, last_attempt_warning: :true do
303
- swap Devise, lock_strategy: :failed_attempts do
304
- user = create_user
305
- user.failed_attempts = Devise.maximum_attempts - 2
306
- assert_equal :invalid, user.unauthenticated_message
302
+ swap Devise, last_attempt_warning: true, lock_strategy: :failed_attempts do
303
+ user = create_user
304
+ user.failed_attempts = Devise.maximum_attempts - 2
305
+ assert_equal :invalid, user.unauthenticated_message
307
306
 
308
- user.failed_attempts = Devise.maximum_attempts - 1
309
- assert_equal :last_attempt, user.unauthenticated_message
307
+ user.failed_attempts = Devise.maximum_attempts - 1
308
+ assert_equal :last_attempt, user.unauthenticated_message
310
309
 
311
- user.failed_attempts = Devise.maximum_attempts
312
- assert_equal :locked, user.unauthenticated_message
313
- end
310
+ user.failed_attempts = Devise.maximum_attempts
311
+ assert_equal :locked, user.unauthenticated_message
312
+ end
313
+ end
314
+
315
+ test 'should not return last attempt message if last_attempt_warning is disabled' do
316
+ swap Devise, last_attempt_warning: false, lock_strategy: :failed_attempts do
317
+ user = create_user
318
+ user.failed_attempts = Devise.maximum_attempts - 1
319
+ assert_equal :invalid, user.unauthenticated_message
314
320
  end
315
321
  end
316
322
 
@@ -319,4 +325,26 @@ class LockableTest < ActiveSupport::TestCase
319
325
  user.lock_access!
320
326
  assert_equal :locked, user.unauthenticated_message
321
327
  end
328
+
329
+ test 'unlock_strategy_enabled? should return true for both, email, and time strategies if :both is used' do
330
+ swap Devise, unlock_strategy: :both do
331
+ user = create_user
332
+ assert_equal true, user.unlock_strategy_enabled?(:both)
333
+ assert_equal true, user.unlock_strategy_enabled?(:time)
334
+ assert_equal true, user.unlock_strategy_enabled?(:email)
335
+ assert_equal false, user.unlock_strategy_enabled?(:none)
336
+ assert_equal false, user.unlock_strategy_enabled?(:an_undefined_strategy)
337
+ end
338
+ end
339
+
340
+ test 'unlock_strategy_enabled? should return true only for the configured strategy' do
341
+ swap Devise, unlock_strategy: :email do
342
+ user = create_user
343
+ assert_equal false, user.unlock_strategy_enabled?(:both)
344
+ assert_equal false, user.unlock_strategy_enabled?(:time)
345
+ assert_equal true, user.unlock_strategy_enabled?(:email)
346
+ assert_equal false, user.unlock_strategy_enabled?(:none)
347
+ assert_equal false, user.unlock_strategy_enabled?(:an_undefined_strategy)
348
+ end
349
+ end
322
350
  end
@@ -23,13 +23,13 @@ class RecoverableTest < ActiveSupport::TestCase
23
23
 
24
24
  test 'should reset password and password confirmation from params' do
25
25
  user = create_user
26
- user.reset_password!('123456789', '987654321')
26
+ user.reset_password('123456789', '987654321')
27
27
  assert_equal '123456789', user.password
28
28
  assert_equal '987654321', user.password_confirmation
29
29
  end
30
30
 
31
31
  test 'should reset password and save the record' do
32
- assert create_user.reset_password!('123456789', '123456789')
32
+ assert create_user.reset_password('123456789', '123456789')
33
33
  end
34
34
 
35
35
  test 'should clear reset password token while reseting the password' do
@@ -38,7 +38,53 @@ class RecoverableTest < ActiveSupport::TestCase
38
38
 
39
39
  user.send_reset_password_instructions
40
40
  assert_present user.reset_password_token
41
- assert user.reset_password!('123456789', '123456789')
41
+ assert user.reset_password('123456789', '123456789')
42
+ assert_nil user.reset_password_token
43
+ end
44
+
45
+ test 'should not clear reset password token for new user' do
46
+ user = new_user
47
+ assert_nil user.reset_password_token
48
+
49
+ user.send_reset_password_instructions
50
+ assert_present user.reset_password_token
51
+
52
+ user.save
53
+ assert_present user.reset_password_token
54
+ end
55
+
56
+ test 'should clear reset password token if changing password' do
57
+ user = create_user
58
+ assert_nil user.reset_password_token
59
+
60
+ user.send_reset_password_instructions
61
+ assert_present user.reset_password_token
62
+ user.password = "123456678"
63
+ user.password_confirmation = "123456678"
64
+ user.save!
65
+ assert_nil user.reset_password_token
66
+ end
67
+
68
+ test 'should clear reset password token if changing email' do
69
+ user = create_user
70
+ assert_nil user.reset_password_token
71
+
72
+ user.send_reset_password_instructions
73
+ assert_present user.reset_password_token
74
+ user.email = "another@example.com"
75
+ user.save!
76
+ assert_nil user.reset_password_token
77
+ end
78
+
79
+ test 'should clear reset password successfully even if there is no email' do
80
+ user = create_user_without_email
81
+ assert_nil user.reset_password_token
82
+
83
+ user.send_reset_password_instructions
84
+ assert_present user.reset_password_token
85
+ user.password = "123456678"
86
+ user.password_confirmation = "123456678"
87
+ user.save!
42
88
  assert_nil user.reset_password_token
43
89
  end
44
90
 
@@ -46,14 +92,14 @@ class RecoverableTest < ActiveSupport::TestCase
46
92
  user = create_user
47
93
  user.send_reset_password_instructions
48
94
  assert_present user.reset_password_token
49
- assert_not user.reset_password!('123456789', '987654321')
95
+ assert_not user.reset_password('123456789', '987654321')
50
96
  assert_present user.reset_password_token
51
97
  end
52
98
 
53
99
  test 'should not reset password with invalid data' do
54
100
  user = create_user
55
101
  user.stubs(:valid?).returns(false)
56
- assert_not user.reset_password!('123456789', '987654321')
102
+ assert_not user.reset_password('123456789', '987654321')
57
103
  end
58
104
 
59
105
  test 'should reset reset password token and send instructions by email' do
@@ -135,6 +181,7 @@ class RecoverableTest < ActiveSupport::TestCase
135
181
  reset_password_user = User.reset_password_by_token(reset_password_token: raw, password: '')
136
182
  assert_not reset_password_user.errors.empty?
137
183
  assert_match "can't be blank", reset_password_user.errors[:password].join
184
+ assert_equal raw, reset_password_user.reset_password_token
138
185
  end
139
186
 
140
187
  test 'should reset successfully user password given the new password and confirmation' do
@@ -142,15 +189,17 @@ class RecoverableTest < ActiveSupport::TestCase
142
189
  old_password = user.password
143
190
  raw = user.send_reset_password_instructions
144
191
 
145
- User.reset_password_by_token(
192
+ reset_password_user = User.reset_password_by_token(
146
193
  reset_password_token: raw,
147
194
  password: 'new_password',
148
195
  password_confirmation: 'new_password'
149
196
  )
150
- user.reload
197
+ assert_nil reset_password_user.reset_password_token
151
198
 
199
+ user.reload
152
200
  assert_not user.valid_password?(old_password)
153
201
  assert user.valid_password?('new_password')
202
+ assert_nil user.reset_password_token
154
203
  end
155
204
 
156
205
  test 'should not reset password after reset_password_within time' do
@@ -189,6 +238,12 @@ class RecoverableTest < ActiveSupport::TestCase
189
238
  assert_equal User.with_reset_password_token(raw), user
190
239
  end
191
240
 
241
+ test 'should return the same reset password token as generated' do
242
+ user = create_user
243
+ raw = user.send_reset_password_instructions
244
+ assert_equal Devise.token_generator.digest(self.class, :reset_password_token, raw), user.reset_password_token
245
+ end
246
+
192
247
  test 'should return nil if a user based on the raw token is not found' do
193
248
  assert_equal User.with_reset_password_token('random-token'), nil
194
249
  end
@@ -13,6 +13,19 @@ class RememberableTest < ActiveSupport::TestCase
13
13
  user = create_user
14
14
  user.expects(:valid?).never
15
15
  user.remember_me!
16
+ assert user.remember_created_at
17
+ end
18
+
19
+ test 'remember_me should not generate a new token if valid token exists' do
20
+ user = create_user
21
+ user.singleton_class.send(:attr_accessor, :remember_token)
22
+ User.to_adapter.expects(:find_first).returns(nil)
23
+
24
+ user.remember_me!
25
+ existing_token = user.remember_token
26
+
27
+ user.remember_me!
28
+ assert_equal existing_token, user.remember_token
16
29
  end
17
30
 
18
31
  test 'forget_me should not clear remember token if using salt' do
@@ -33,18 +46,68 @@ class RememberableTest < ActiveSupport::TestCase
33
46
  test 'serialize into cookie' do
34
47
  user = create_user
35
48
  user.remember_me!
36
- assert_equal [user.to_key, user.authenticatable_salt], User.serialize_into_cookie(user)
49
+ id, token, date = User.serialize_into_cookie(user)
50
+ assert_equal id, user.to_key
51
+ assert_equal token, user.authenticatable_salt
52
+ assert date.is_a?(String)
37
53
  end
38
54
 
39
55
  test 'serialize from cookie' do
40
56
  user = create_user
41
57
  user.remember_me!
42
- assert_equal user, User.serialize_from_cookie(user.to_key, user.authenticatable_salt)
58
+ assert_equal user, User.serialize_from_cookie(user.to_key, user.authenticatable_salt, Time.now.utc)
59
+ end
60
+
61
+ test 'serialize from cookie should accept a String with the datetime seconds and microseconds' do
62
+ user = create_user
63
+ user.remember_me!
64
+ assert_equal user, User.serialize_from_cookie(user.to_key, user.authenticatable_salt, Time.now.utc.to_f.to_json)
65
+ end
66
+
67
+ test 'serialize from cookie should return nil with invalid datetime' do
68
+ user = create_user
69
+ user.remember_me!
70
+ assert_nil User.serialize_from_cookie(user.to_key, user.authenticatable_salt, "2013")
43
71
  end
44
72
 
45
- test 'raises a RuntimeError if authenticatable_salt is nil' do
73
+ test 'serialize from cookie should return nil if no resource is found' do
74
+ assert_nil resource_class.serialize_from_cookie([0], "123", Time.now.utc)
75
+ end
76
+
77
+ test 'serialize from cookie should return nil if no timestamp' do
78
+ user = create_user
79
+ user.remember_me!
80
+ assert_nil User.serialize_from_cookie(user.to_key, user.authenticatable_salt)
81
+ end
82
+
83
+ test 'serialize from cookie should return nil if timestamp is earlier than token creation' do
84
+ user = create_user
85
+ user.remember_me!
86
+ assert_nil User.serialize_from_cookie(user.to_key, user.authenticatable_salt, 1.day.ago)
87
+ end
88
+
89
+ test 'serialize from cookie should return nil if timestamp is older than remember_for' do
90
+ user = create_user
91
+ user.remember_created_at = 1.month.ago
92
+ user.remember_me!
93
+ assert_nil User.serialize_from_cookie(user.to_key, user.authenticatable_salt, 3.weeks.ago)
94
+ end
95
+
96
+ test 'serialize from cookie me return nil if is a valid resource with invalid token' do
97
+ user = create_user
98
+ user.remember_me!
99
+ assert_nil User.serialize_from_cookie(user.to_key, "123", Time.now.utc)
100
+ end
101
+
102
+ test 'raises a RuntimeError if authenticatable_salt is nil or empty' do
103
+ user = User.new
104
+ def user.authenticable_salt; nil; end
105
+ assert_raise RuntimeError do
106
+ user.rememberable_value
107
+ end
108
+
46
109
  user = User.new
47
- user.encrypted_password = nil
110
+ def user.authenticable_salt; ""; end
48
111
  assert_raise RuntimeError do
49
112
  user.rememberable_value
50
113
  end
@@ -87,28 +150,7 @@ class RememberableTest < ActiveSupport::TestCase
87
150
  resource.forget_me!
88
151
  end
89
152
 
90
- test 'remember is expired if not created at timestamp is set' do
91
- assert create_resource.remember_expired?
92
- end
93
-
94
- test 'serialize should return nil if no resource is found' do
95
- assert_nil resource_class.serialize_from_cookie([0], "123")
96
- end
97
-
98
- test 'remember me return nil if is a valid resource with invalid token' do
99
- resource = create_resource
100
- assert_nil resource_class.serialize_from_cookie([resource.id], "123")
101
- end
102
-
103
- test 'remember for should fallback to devise remember for default configuration' do
104
- swap Devise, remember_for: 1.day do
105
- resource = create_resource
106
- resource.remember_me!
107
- assert_not resource.remember_expired?
108
- end
109
- end
110
-
111
- test 'remember expires at should sum date of creation with remember for configuration' do
153
+ test 'remember expires at uses remember for configuration' do
112
154
  swap Devise, remember_for: 3.days do
113
155
  resource = create_resource
114
156
  resource.remember_me!
@@ -119,77 +161,6 @@ class RememberableTest < ActiveSupport::TestCase
119
161
  end
120
162
  end
121
163
 
122
- test 'remember should be expired if remember_for is zero' do
123
- swap Devise, remember_for: 0.days do
124
- Devise.remember_for = 0.days
125
- resource = create_resource
126
- resource.remember_me!
127
- assert resource.remember_expired?
128
- end
129
- end
130
-
131
- test 'remember should be expired if it was created before limit time' do
132
- swap Devise, remember_for: 1.day do
133
- resource = create_resource
134
- resource.remember_me!
135
- resource.remember_created_at = 2.days.ago
136
- resource.save
137
- assert resource.remember_expired?
138
- end
139
- end
140
-
141
- test 'remember should not be expired if it was created within the limit time' do
142
- swap Devise, remember_for: 30.days do
143
- resource = create_resource
144
- resource.remember_me!
145
- resource.remember_created_at = (30.days.ago + 2.minutes)
146
- resource.save
147
- assert_not resource.remember_expired?
148
- end
149
- end
150
-
151
- test 'if extend_remember_period is false, remember_me! should generate a new timestamp if expired' do
152
- swap Devise, remember_for: 5.minutes do
153
- resource = create_resource
154
- resource.remember_me!(false)
155
- assert resource.remember_created_at
156
-
157
- resource.remember_created_at = old = 10.minutes.ago
158
- resource.save
159
-
160
- resource.remember_me!(false)
161
- assert_not_equal old.to_i, resource.remember_created_at.to_i
162
- end
163
- end
164
-
165
- test 'if extend_remember_period is false, remember_me! should not generate a new timestamp' do
166
- swap Devise, remember_for: 1.year do
167
- resource = create_resource
168
- resource.remember_me!(false)
169
- assert resource.remember_created_at
170
-
171
- resource.remember_created_at = old = 10.minutes.ago.utc
172
- resource.save
173
-
174
- resource.remember_me!(false)
175
- assert_equal old.to_i, resource.remember_created_at.to_i
176
- end
177
- end
178
-
179
- test 'if extend_remember_period is true, remember_me! should always generate a new timestamp' do
180
- swap Devise, remember_for: 1.year do
181
- resource = create_resource
182
- resource.remember_me!(true)
183
- assert resource.remember_created_at
184
-
185
- resource.remember_created_at = old = 10.minutes.ago
186
- resource.save
187
-
188
- resource.remember_me!(true)
189
- assert_not_equal old, resource.remember_created_at
190
- end
191
- end
192
-
193
164
  test 'should have the required_fields array' do
194
165
  assert_same_content Devise::Models::Rememberable.required_fields(User), [
195
166
  :remember_created_at
@@ -92,10 +92,10 @@ class ValidatableTest < ActiveSupport::TestCase
92
92
  assert_equal 'is too short (minimum is 7 characters)', user.errors[:password].join
93
93
  end
94
94
 
95
- test 'should require a password with maximum of 128 characters long' do
96
- user = new_user(password: 'x'*129, password_confirmation: 'x'*129)
95
+ test 'should require a password with maximum of 72 characters long' do
96
+ user = new_user(password: 'x'*73, password_confirmation: 'x'*73)
97
97
  assert user.invalid?
98
- assert_equal 'is too long (maximum is 128 characters)', user.errors[:password].join
98
+ assert_equal 'is too long (maximum is 72 characters)', user.errors[:password].join
99
99
  end
100
100
 
101
101
  test 'should not require password length when it\'s not changed' do
@@ -109,10 +109,10 @@ class ValidatableTest < ActiveSupport::TestCase
109
109
  end
110
110
 
111
111
  test 'should complain about length even if password is not required' do
112
- user = new_user(password: 'x'*129, password_confirmation: 'x'*129)
112
+ user = new_user(password: 'x'*73, password_confirmation: 'x'*73)
113
113
  user.stubs(:password_required?).returns(false)
114
114
  assert user.invalid?
115
- assert_equal 'is too long (maximum is 128 characters)', user.errors[:password].join
115
+ assert_equal 'is too long (maximum is 72 characters)', user.errors[:password].join
116
116
  end
117
117
 
118
118
  test 'should not be included in objects with invalid API' do
data/test/models_test.rb CHANGED
@@ -92,13 +92,20 @@ class ActiveRecordTest < ActiveSupport::TestCase
92
92
  end
93
93
  end
94
94
 
95
+ module StubModelFilters
96
+ def stub_filter(name)
97
+ define_singleton_method(name) { |*| nil }
98
+ end
99
+ end
100
+
95
101
  class CheckFieldsTest < ActiveSupport::TestCase
96
102
  test 'checks if the class respond_to the required fields' do
97
103
  Player = Class.new do
98
104
  extend Devise::Models
105
+ extend StubModelFilters
99
106
 
100
- def self.before_validation(instance)
101
- end
107
+ stub_filter :before_validation
108
+ stub_filter :after_update
102
109
 
103
110
  devise :database_authenticatable
104
111
 
@@ -113,9 +120,10 @@ class CheckFieldsTest < ActiveSupport::TestCase
113
120
  test 'raises Devise::Models::MissingAtrribute and shows the missing attribute if the class doesn\'t respond_to one of the attributes' do
114
121
  Clown = Class.new do
115
122
  extend Devise::Models
123
+ extend StubModelFilters
116
124
 
117
- def self.before_validation(instance)
118
- end
125
+ stub_filter :before_validation
126
+ stub_filter :after_update
119
127
 
120
128
  devise :database_authenticatable
121
129
 
@@ -130,9 +138,10 @@ class CheckFieldsTest < ActiveSupport::TestCase
130
138
  test 'raises Devise::Models::MissingAtrribute with all the missing attributes if there is more than one' do
131
139
  Magician = Class.new do
132
140
  extend Devise::Models
141
+ extend StubModelFilters
133
142
 
134
- def self.before_validation(instance)
135
- end
143
+ stub_filter :before_validation
144
+ stub_filter :after_update
136
145
 
137
146
  devise :database_authenticatable
138
147
  end
@@ -0,0 +1,8 @@
1
+ require "shared_user_without_email"
2
+
3
+ class UserWithoutEmail < ActiveRecord::Base
4
+ self.table_name = 'users'
5
+ include Shim
6
+ include SharedUserWithoutEmail
7
+ end
8
+
@@ -3,9 +3,4 @@ class AdminsController < ApplicationController
3
3
 
4
4
  def index
5
5
  end
6
-
7
- def expire
8
- admin_session['last_request_at'] = 31.minutes.ago.utc
9
- render text: 'Admin will be expired on next request'
10
- end
11
6
  end
@@ -1,4 +1,10 @@
1
1
  class Custom::RegistrationsController < Devise::RegistrationsController
2
+ def new
3
+ super do |resource|
4
+ @new_block_called = true
5
+ end
6
+ end
7
+
2
8
  def create
3
9
  super do |resource|
4
10
  @create_block_called = true
@@ -18,4 +24,8 @@ class Custom::RegistrationsController < Devise::RegistrationsController
18
24
  def update_block_called?
19
25
  @update_block_called == true
20
26
  end
27
+
28
+ def new_block_called?
29
+ @new_block_called == true
30
+ end
21
31
  end
@@ -0,0 +1,3 @@
1
+ class Users::FromProcMailer < Devise::Mailer
2
+ default from: proc { 'custom@example.com' }
3
+ end
@@ -1,12 +1,3 @@
1
1
  class Users::Mailer < Devise::Mailer
2
2
  default from: 'custom@example.com'
3
3
  end
4
-
5
- class Users::ReplyToMailer < Devise::Mailer
6
- default from: 'custom@example.com'
7
- default reply_to: 'custom_reply_to@example.com'
8
- end
9
-
10
- class Users::FromProcMailer < Devise::Mailer
11
- default from: proc { 'custom@example.com' }
12
- end
@@ -0,0 +1,4 @@
1
+ class Users::ReplyToMailer < Devise::Mailer
2
+ default from: 'custom@example.com'
3
+ default reply_to: 'custom_reply_to@example.com'
4
+ end
@@ -0,0 +1,33 @@
1
+ require "shared_user_without_email"
2
+
3
+ class UserWithoutEmail
4
+ include Mongoid::Document
5
+ include Shim
6
+ include SharedUserWithoutEmail
7
+
8
+ field :username, type: String
9
+ field :facebook_token, type: String
10
+
11
+ ## Database authenticatable
12
+ field :email, type: String, default: ""
13
+ field :encrypted_password, type: String, default: ""
14
+
15
+ ## Recoverable
16
+ field :reset_password_token, type: String
17
+ field :reset_password_sent_at, type: Time
18
+
19
+ ## Rememberable
20
+ field :remember_created_at, type: Time
21
+
22
+ ## Trackable
23
+ field :sign_in_count, type: Integer, default: 0
24
+ field :current_sign_in_at, type: Time
25
+ field :last_sign_in_at, type: Time
26
+ field :current_sign_in_ip, type: String
27
+ field :last_sign_in_ip, type: String
28
+
29
+ ## Lockable
30
+ field :failed_attempts, type: Integer, default: 0 # Only if lock strategy is :failed_attempts
31
+ field :unlock_token, type: String # Only if unlock strategy is :email or :both
32
+ field :locked_at, type: Time
33
+ end
@@ -17,7 +17,7 @@ module RailsApp
17
17
  class Application < Rails::Application
18
18
  # Add additional load paths for your own custom dirs
19
19
  config.autoload_paths.reject!{ |p| p =~ /\/app\/(\w+)$/ && !%w(controllers helpers mailers views).include?($1) }
20
- config.autoload_paths += [ "#{config.root}/app/#{DEVISE_ORM}" ]
20
+ config.autoload_paths += ["#{config.root}/app/#{DEVISE_ORM}"]
21
21
 
22
22
  # Configure generators values. Many other options are available, be sure to check the documentation.
23
23
  # config.generators do |g|
@@ -20,7 +20,11 @@ RailsApp::Application.configure do
20
20
  # config.action_dispatch.rack_cache = true
21
21
 
22
22
  # Disable Rails's static asset server (Apache or nginx will already do this).
23
- config.serve_static_assets = false
23
+ if Rails.version >= "4.2.0"
24
+ config.serve_static_files = false
25
+ else
26
+ config.serve_static_assets = false
27
+ end
24
28
 
25
29
  # Compress JavaScripts and CSS.
26
30
  config.assets.js_compressor = :uglifier
@@ -46,7 +50,7 @@ RailsApp::Application.configure do
46
50
  config.log_level = :info
47
51
 
48
52
  # Prepend all log lines with the following tags.
49
- # config.log_tags = [ :subdomain, :uuid ]
53
+ # config.log_tags = [:subdomain, :uuid]
50
54
 
51
55
  # Use a different logger for distributed setups.
52
56
  # config.logger = ActiveSupport::TaggedLogging.new(SyslogLogger.new)
@@ -12,8 +12,13 @@ RailsApp::Application.configure do
12
12
  # preloads Rails for running tests, you may have to set it to true.
13
13
  config.eager_load = false
14
14
 
15
- # Configure static asset server for tests with Cache-Control for performance.
16
- config.serve_static_assets = true
15
+ # Disable serving static files from the `/public` folder by default since
16
+ # Apache or NGINX already handles this.
17
+ if Rails.version >= "4.2.0"
18
+ config.serve_static_files = true
19
+ else
20
+ config.serve_static_assets = true
21
+ end
17
22
  config.static_cache_control = "public, max-age=3600"
18
23
 
19
24
  # Show full error reports and disable caching.