devise 2.1.2 → 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 (242) hide show
  1. checksums.yaml +7 -0
  2. data/.travis.yml +39 -10
  3. data/.yardopts +9 -0
  4. data/{CHANGELOG.rdoc → CHANGELOG.md} +445 -112
  5. data/CODE_OF_CONDUCT.md +22 -0
  6. data/CONTRIBUTING.md +16 -0
  7. data/Gemfile +10 -15
  8. data/Gemfile.lock +151 -129
  9. data/MIT-LICENSE +1 -1
  10. data/README.md +256 -96
  11. data/Rakefile +4 -2
  12. data/app/controllers/devise/confirmations_controller.rb +15 -7
  13. data/app/controllers/devise/omniauth_callbacks_controller.rb +6 -2
  14. data/app/controllers/devise/passwords_controller.rb +33 -9
  15. data/app/controllers/devise/registrations_controller.rb +66 -26
  16. data/app/controllers/devise/sessions_controller.rb +52 -21
  17. data/app/controllers/devise/unlocks_controller.rb +11 -6
  18. data/app/controllers/devise_controller.rb +65 -58
  19. data/app/helpers/devise_helper.rb +2 -2
  20. data/app/mailers/devise/mailer.rb +19 -10
  21. data/app/views/devise/confirmations/new.html.erb +8 -4
  22. data/app/views/devise/mailer/confirmation_instructions.html.erb +2 -2
  23. data/app/views/devise/mailer/password_change.html.erb +3 -0
  24. data/app/views/devise/mailer/reset_password_instructions.html.erb +2 -2
  25. data/app/views/devise/mailer/unlock_instructions.html.erb +2 -2
  26. data/app/views/devise/passwords/edit.html.erb +15 -6
  27. data/app/views/devise/passwords/new.html.erb +8 -4
  28. data/app/views/devise/registrations/edit.html.erb +29 -15
  29. data/app/views/devise/registrations/new.html.erb +19 -8
  30. data/app/views/devise/sessions/new.html.erb +17 -8
  31. data/app/views/devise/shared/{_links.erb → _links.html.erb} +4 -4
  32. data/app/views/devise/unlocks/new.html.erb +8 -4
  33. data/config/locales/en.yml +51 -47
  34. data/devise.gemspec +8 -6
  35. data/devise.png +0 -0
  36. data/gemfiles/Gemfile.rails-3.2-stable +29 -0
  37. data/gemfiles/Gemfile.rails-3.2-stable.lock +172 -0
  38. data/gemfiles/Gemfile.rails-4.0-stable +30 -0
  39. data/gemfiles/Gemfile.rails-4.0-stable.lock +166 -0
  40. data/gemfiles/Gemfile.rails-4.1-stable +30 -0
  41. data/gemfiles/Gemfile.rails-4.1-stable.lock +171 -0
  42. data/gemfiles/Gemfile.rails-4.2-stable +30 -0
  43. data/gemfiles/Gemfile.rails-4.2-stable.lock +193 -0
  44. data/lib/devise/controllers/helpers.rb +126 -108
  45. data/lib/devise/controllers/rememberable.rb +19 -17
  46. data/lib/devise/controllers/scoped_views.rb +1 -1
  47. data/lib/devise/controllers/sign_in_out.rb +96 -0
  48. data/lib/devise/controllers/store_location.rb +58 -0
  49. data/lib/devise/controllers/url_helpers.rb +7 -7
  50. data/lib/devise/encryptor.rb +22 -0
  51. data/lib/devise/failure_app.rb +85 -25
  52. data/lib/devise/hooks/activatable.rb +5 -6
  53. data/lib/devise/hooks/csrf_cleaner.rb +7 -0
  54. data/lib/devise/hooks/forgetable.rb +1 -1
  55. data/lib/devise/hooks/lockable.rb +2 -2
  56. data/lib/devise/hooks/proxy.rb +21 -0
  57. data/lib/devise/hooks/rememberable.rb +5 -4
  58. data/lib/devise/hooks/timeoutable.rb +16 -8
  59. data/lib/devise/hooks/trackable.rb +1 -1
  60. data/lib/devise/mailers/helpers.rb +27 -23
  61. data/lib/devise/mapping.rb +11 -7
  62. data/lib/devise/models/authenticatable.rb +82 -66
  63. data/lib/devise/models/confirmable.rb +142 -55
  64. data/lib/devise/models/database_authenticatable.rb +59 -15
  65. data/lib/devise/models/lockable.rb +41 -30
  66. data/lib/devise/models/omniauthable.rb +3 -3
  67. data/lib/devise/models/recoverable.rb +56 -41
  68. data/lib/devise/models/rememberable.rb +65 -27
  69. data/lib/devise/models/timeoutable.rb +2 -8
  70. data/lib/devise/models/trackable.rb +6 -4
  71. data/lib/devise/models/validatable.rb +9 -9
  72. data/lib/devise/models.rb +4 -13
  73. data/lib/devise/modules.rb +10 -11
  74. data/lib/devise/omniauth/url_helpers.rb +2 -2
  75. data/lib/devise/orm/active_record.rb +1 -1
  76. data/lib/devise/orm/mongoid.rb +1 -1
  77. data/lib/devise/{param_filter.rb → parameter_filter.rb} +10 -11
  78. data/lib/devise/parameter_sanitizer.rb +99 -0
  79. data/lib/devise/rails/routes.rb +173 -115
  80. data/lib/devise/rails/warden_compat.rb +10 -31
  81. data/lib/devise/rails.rb +14 -12
  82. data/lib/devise/strategies/authenticatable.rb +26 -26
  83. data/lib/devise/strategies/base.rb +1 -1
  84. data/lib/devise/strategies/database_authenticatable.rb +8 -4
  85. data/lib/devise/strategies/rememberable.rb +15 -5
  86. data/lib/devise/test_helpers.rb +7 -5
  87. data/lib/devise/time_inflector.rb +14 -0
  88. data/lib/devise/token_generator.rb +70 -0
  89. data/lib/devise/version.rb +1 -1
  90. data/lib/devise.rb +110 -52
  91. data/lib/generators/active_record/devise_generator.rb +34 -18
  92. data/lib/generators/active_record/templates/migration.rb +5 -6
  93. data/lib/generators/active_record/templates/migration_existing.rb +5 -6
  94. data/lib/generators/devise/controllers_generator.rb +44 -0
  95. data/lib/generators/devise/devise_generator.rb +5 -3
  96. data/lib/generators/devise/install_generator.rb +5 -0
  97. data/lib/generators/devise/orm_helpers.rb +25 -6
  98. data/lib/generators/devise/views_generator.rb +52 -22
  99. data/lib/generators/mongoid/devise_generator.rb +21 -26
  100. data/lib/generators/templates/README +9 -5
  101. data/lib/generators/templates/controllers/README +14 -0
  102. data/lib/generators/templates/controllers/confirmations_controller.rb +28 -0
  103. data/lib/generators/templates/controllers/omniauth_callbacks_controller.rb +28 -0
  104. data/lib/generators/templates/controllers/passwords_controller.rb +32 -0
  105. data/lib/generators/templates/controllers/registrations_controller.rb +60 -0
  106. data/lib/generators/templates/controllers/sessions_controller.rb +25 -0
  107. data/lib/generators/templates/controllers/unlocks_controller.rb +28 -0
  108. data/lib/generators/templates/devise.rb +80 -43
  109. data/lib/generators/templates/markerb/confirmation_instructions.markerb +2 -2
  110. data/lib/generators/templates/markerb/password_change.markerb +3 -0
  111. data/lib/generators/templates/markerb/reset_password_instructions.markerb +1 -1
  112. data/lib/generators/templates/markerb/unlock_instructions.markerb +2 -2
  113. data/lib/generators/templates/simple_form_for/confirmations/new.html.erb +3 -2
  114. data/lib/generators/templates/simple_form_for/passwords/edit.html.erb +4 -4
  115. data/lib/generators/templates/simple_form_for/passwords/new.html.erb +2 -2
  116. data/lib/generators/templates/simple_form_for/registrations/edit.html.erb +11 -6
  117. data/lib/generators/templates/simple_form_for/registrations/new.html.erb +4 -4
  118. data/lib/generators/templates/simple_form_for/sessions/new.html.erb +6 -6
  119. data/lib/generators/templates/simple_form_for/unlocks/new.html.erb +3 -2
  120. data/script/cached-bundle +49 -0
  121. data/script/s3-put +71 -0
  122. data/test/controllers/custom_registrations_controller_test.rb +40 -0
  123. data/test/controllers/helper_methods_test.rb +21 -0
  124. data/test/controllers/helpers_test.rb +95 -32
  125. data/test/controllers/inherited_controller_i18n_messages_test.rb +51 -0
  126. data/test/controllers/internal_helpers_test.rb +39 -14
  127. data/test/controllers/load_hooks_controller_test.rb +19 -0
  128. data/test/controllers/passwords_controller_test.rb +31 -0
  129. data/test/controllers/sessions_controller_test.rb +66 -6
  130. data/test/controllers/url_helpers_test.rb +10 -4
  131. data/test/delegator_test.rb +1 -1
  132. data/test/devise_test.rb +45 -10
  133. data/test/failure_app_test.rb +121 -27
  134. data/test/generators/active_record_generator_test.rb +48 -8
  135. data/test/generators/controllers_generator_test.rb +48 -0
  136. data/test/generators/devise_generator_test.rb +2 -2
  137. data/test/generators/mongoid_generator_test.rb +3 -3
  138. data/test/generators/views_generator_test.rb +54 -3
  139. data/test/helpers/devise_helper_test.rb +18 -20
  140. data/test/integration/authenticatable_test.rb +161 -65
  141. data/test/integration/confirmable_test.rb +146 -77
  142. data/test/integration/database_authenticatable_test.rb +43 -30
  143. data/test/integration/http_authenticatable_test.rb +30 -22
  144. data/test/integration/lockable_test.rb +64 -49
  145. data/test/integration/omniauthable_test.rb +17 -15
  146. data/test/integration/recoverable_test.rb +111 -70
  147. data/test/integration/registerable_test.rb +114 -79
  148. data/test/integration/rememberable_test.rb +87 -31
  149. data/test/integration/timeoutable_test.rb +77 -33
  150. data/test/integration/trackable_test.rb +5 -5
  151. data/test/mailers/confirmation_instructions_test.rb +28 -8
  152. data/test/mailers/reset_password_instructions_test.rb +21 -8
  153. data/test/mailers/unlock_instructions_test.rb +20 -6
  154. data/test/mapping_test.rb +12 -5
  155. data/test/models/authenticatable_test.rb +17 -1
  156. data/test/models/confirmable_test.rb +216 -62
  157. data/test/models/database_authenticatable_test.rb +129 -49
  158. data/test/models/lockable_test.rb +132 -45
  159. data/test/models/recoverable_test.rb +100 -54
  160. data/test/models/rememberable_test.rb +89 -94
  161. data/test/models/serializable_test.rb +12 -11
  162. data/test/models/timeoutable_test.rb +6 -1
  163. data/test/models/trackable_test.rb +28 -0
  164. data/test/models/validatable_test.rb +31 -21
  165. data/test/models_test.rb +22 -48
  166. data/test/omniauth/config_test.rb +4 -4
  167. data/test/omniauth/url_helpers_test.rb +7 -4
  168. data/test/orm/active_record.rb +1 -0
  169. data/test/orm/mongoid.rb +2 -3
  170. data/test/parameter_sanitizer_test.rb +81 -0
  171. data/test/rails_app/Rakefile +0 -4
  172. data/test/rails_app/app/active_record/shim.rb +1 -1
  173. data/test/rails_app/app/active_record/user_on_engine.rb +7 -0
  174. data/test/rails_app/app/active_record/user_on_main_app.rb +7 -0
  175. data/test/rails_app/app/active_record/user_without_email.rb +8 -0
  176. data/test/rails_app/app/controllers/admins/sessions_controller.rb +1 -1
  177. data/test/rails_app/app/controllers/admins_controller.rb +0 -5
  178. data/test/rails_app/app/controllers/application_controller.rb +6 -2
  179. data/test/rails_app/app/controllers/application_with_fake_engine.rb +30 -0
  180. data/test/rails_app/app/controllers/custom/registrations_controller.rb +31 -0
  181. data/test/rails_app/app/controllers/home_controller.rb +1 -1
  182. data/test/rails_app/app/controllers/publisher/registrations_controller.rb +1 -1
  183. data/test/rails_app/app/controllers/publisher/sessions_controller.rb +1 -1
  184. data/test/rails_app/app/controllers/users/omniauth_callbacks_controller.rb +4 -4
  185. data/test/rails_app/app/controllers/users_controller.rb +12 -4
  186. data/test/rails_app/app/mailers/users/from_proc_mailer.rb +3 -0
  187. data/test/rails_app/app/mailers/users/mailer.rb +1 -1
  188. data/test/rails_app/app/mailers/users/reply_to_mailer.rb +4 -0
  189. data/test/rails_app/app/mongoid/admin.rb +12 -10
  190. data/test/rails_app/app/mongoid/shim.rb +4 -5
  191. data/test/rails_app/app/mongoid/user.rb +19 -22
  192. data/test/rails_app/app/mongoid/user_on_engine.rb +39 -0
  193. data/test/rails_app/app/mongoid/user_on_main_app.rb +39 -0
  194. data/test/rails_app/app/mongoid/user_without_email.rb +33 -0
  195. data/test/rails_app/app/views/admins/sessions/new.html.erb +1 -1
  196. data/test/rails_app/app/views/home/admin_dashboard.html.erb +1 -1
  197. data/test/rails_app/app/views/home/index.html.erb +1 -1
  198. data/test/rails_app/app/views/home/join.html.erb +1 -1
  199. data/test/rails_app/app/views/home/user_dashboard.html.erb +1 -1
  200. data/test/rails_app/app/views/layouts/application.html.erb +1 -1
  201. data/test/rails_app/app/views/users/edit_form.html.erb +1 -0
  202. data/test/rails_app/bin/bundle +3 -0
  203. data/test/rails_app/bin/rails +4 -0
  204. data/test/rails_app/bin/rake +4 -0
  205. data/test/rails_app/config/application.rb +4 -5
  206. data/test/rails_app/config/boot.rb +9 -3
  207. data/test/rails_app/config/environment.rb +2 -2
  208. data/test/rails_app/config/environments/development.rb +19 -7
  209. data/test/rails_app/config/environments/production.rb +68 -17
  210. data/test/rails_app/config/environments/test.rb +24 -16
  211. data/test/rails_app/config/initializers/devise.rb +22 -20
  212. data/test/rails_app/config/initializers/secret_token.rb +8 -2
  213. data/test/rails_app/config/initializers/session_store.rb +1 -0
  214. data/test/rails_app/config/routes.rb +71 -46
  215. data/test/rails_app/db/migrate/20100401102949_create_tables.rb +9 -12
  216. data/test/rails_app/db/schema.rb +21 -18
  217. data/test/rails_app/lib/shared_admin.rb +7 -4
  218. data/test/rails_app/lib/shared_user.rb +6 -3
  219. data/test/rails_app/lib/shared_user_without_email.rb +26 -0
  220. data/test/rails_app/lib/shared_user_without_omniauth.rb +13 -0
  221. data/test/rails_test.rb +9 -0
  222. data/test/routes_test.rb +94 -78
  223. data/test/support/action_controller/record_identifier.rb +10 -0
  224. data/test/support/assertions.rb +2 -3
  225. data/test/support/helpers.rb +18 -32
  226. data/test/support/integration.rb +17 -16
  227. data/test/support/locale/en.yml +4 -0
  228. data/test/support/mongoid.yml +6 -0
  229. data/test/test_helper.rb +8 -1
  230. data/test/test_helpers_test.rb +64 -20
  231. data/test/test_models.rb +33 -0
  232. data/test/time_helpers.rb +137 -0
  233. metadata +172 -51
  234. data/app/views/devise/_links.erb +0 -3
  235. data/gemfiles/Gemfile.rails-3.1.x +0 -35
  236. data/gemfiles/Gemfile.rails-3.1.x.lock +0 -167
  237. data/lib/devise/models/token_authenticatable.rb +0 -77
  238. data/lib/devise/strategies/token_authenticatable.rb +0 -56
  239. data/test/indifferent_hash.rb +0 -33
  240. data/test/integration/token_authenticatable_test.rb +0 -161
  241. data/test/models/token_authenticatable_test.rb +0 -55
  242. data/test/rails_app/script/rails +0 -10
@@ -1,10 +1,15 @@
1
1
  require 'test_helper'
2
2
 
3
- class RememberMeTest < ActionController::IntegrationTest
3
+ class RememberMeTest < ActionDispatch::IntegrationTest
4
+ if (Rails::VERSION::MAJOR < 4) || (Rails::VERSION::MAJOR >= 4 && Rails::VERSION::MINOR < 1)
5
+ require 'time_helpers'
6
+ include ActiveSupport::Testing::TimeHelpers
7
+ end
8
+
4
9
  def create_user_and_remember(add_to_token='')
5
10
  user = create_user
6
11
  user.remember_me!
7
- raw_cookie = User.serialize_into_cookie(user).tap { |a| a.last << add_to_token }
12
+ raw_cookie = User.serialize_into_cookie(user).tap { |a| a[1] << add_to_token }
8
13
  cookies['remember_user_token'] = generate_signed_cookie(raw_cookie)
9
14
  user
10
15
  end
@@ -25,13 +30,13 @@ class RememberMeTest < ActionController::IntegrationTest
25
30
  Time.parse(expires).utc
26
31
  end
27
32
 
28
- test 'do not remember the user if he has not checked remember me option' do
29
- user = sign_in_as_user
33
+ test 'do not remember the user if they have not checked remember me option' do
34
+ sign_in_as_user
30
35
  assert_nil request.cookies["remember_user_cookie"]
31
36
  end
32
37
 
33
- test 'handles unverified requests gets rid of caches' do
34
- swap UsersController, :allow_forgery_protection => true do
38
+ test 'handle unverified requests gets rid of caches' do
39
+ swap ApplicationController, allow_forgery_protection: true do
35
40
  post exhibit_user_url(1)
36
41
  assert_not warden.authenticated?(:user)
37
42
 
@@ -42,25 +47,44 @@ class RememberMeTest < ActionController::IntegrationTest
42
47
  end
43
48
  end
44
49
 
50
+ test 'handle unverified requests does not create cookies on sign in' do
51
+ swap ApplicationController, allow_forgery_protection: true do
52
+ get new_user_session_path
53
+ assert request.session[:_csrf_token]
54
+
55
+ post user_session_path, authenticity_token: "oops", user:
56
+ { email: "jose.valim@gmail.com", password: "123456", remember_me: "1" }
57
+ assert_not warden.authenticated?(:user)
58
+ assert_not request.cookies['remember_user_token']
59
+ end
60
+ end
61
+
45
62
  test 'generate remember token after sign in' do
46
- user = sign_in_as_user :remember_me => true
47
- assert request.cookies["remember_user_token"]
63
+ sign_in_as_user remember_me: true
64
+ assert request.cookies['remember_user_token']
48
65
  end
49
66
 
50
67
  test 'generate remember token after sign in setting cookie options' do
51
68
  # We test this by asserting the cookie is not sent after the redirect
52
69
  # since we changed the domain. This is the only difference with the
53
70
  # previous test.
54
- swap Devise, :rememberable_options => { :domain => "omg.somewhere.com" } do
55
- user = sign_in_as_user :remember_me => true
71
+ swap Devise, rememberable_options: { domain: "omg.somewhere.com" } do
72
+ sign_in_as_user remember_me: true
56
73
  assert_nil request.cookies["remember_user_token"]
57
74
  end
58
75
  end
59
76
 
77
+ test 'generate remember token with a custom key' do
78
+ swap Devise, rememberable_options: { key: "v1lat_token" } do
79
+ sign_in_as_user remember_me: true
80
+ assert request.cookies["v1lat_token"]
81
+ end
82
+ end
83
+
60
84
  test 'generate remember token after sign in setting session options' do
61
85
  begin
62
86
  Rails.configuration.session_options[:domain] = "omg.somewhere.com"
63
- user = sign_in_as_user :remember_me => true
87
+ sign_in_as_user remember_me: true
64
88
  assert_nil request.cookies["remember_user_token"]
65
89
  ensure
66
90
  Rails.configuration.session_options.delete(:domain)
@@ -73,35 +97,24 @@ class RememberMeTest < ActionController::IntegrationTest
73
97
  assert_response :success
74
98
  assert warden.authenticated?(:user)
75
99
  assert warden.user(:user) == user
76
- assert_match /remember_user_token[^\n]*HttpOnly/, response.headers["Set-Cookie"], "Expected Set-Cookie header in response to set HttpOnly flag on remember_user_token cookie."
77
100
  end
78
101
 
79
- test 'remember the user before sign up and redirect him to his home' do
80
- user = create_user_and_remember
102
+ test 'remember the user before sign up and redirect them to their home' do
103
+ create_user_and_remember
81
104
  get new_user_registration_path
82
105
  assert warden.authenticated?(:user)
83
106
  assert_redirected_to root_path
84
107
  end
85
108
 
86
- test 'cookies are destroyed on unverified requests' do
87
- swap ApplicationController, :allow_forgery_protection => true do
88
- user = create_user_and_remember
89
- get users_path
90
- assert warden.authenticated?(:user)
91
- post root_path, :authenticity_token => 'INVALID'
92
- assert_not warden.authenticated?(:user)
93
- end
94
- end
95
-
96
109
  test 'does not extend remember period through sign in' do
97
- swap Devise, :extend_remember_period => true, :remember_for => 1.year do
110
+ swap Devise, extend_remember_period: true, remember_for: 1.year do
98
111
  user = create_user
99
112
  user.remember_me!
100
113
 
101
114
  user.remember_created_at = old = 10.days.ago
102
115
  user.save
103
116
 
104
- sign_in_as_user :remember_me => true
117
+ sign_in_as_user remember_me: true
105
118
  user.reload
106
119
 
107
120
  assert warden.user(:user) == user
@@ -109,8 +122,42 @@ class RememberMeTest < ActionController::IntegrationTest
109
122
  end
110
123
  end
111
124
 
125
+ test 'extends remember period when extend remember period config is true' do
126
+ swap Devise, extend_remember_period: true, remember_for: 1.year do
127
+ user = create_user_and_remember
128
+ old_remember_token = nil
129
+
130
+ travel_to 1.day.ago do
131
+ get root_path
132
+ old_remember_token = request.cookies['remember_user_token']
133
+ end
134
+
135
+ get root_path
136
+ current_remember_token = request.cookies['remember_user_token']
137
+
138
+ refute_equal old_remember_token, current_remember_token
139
+ end
140
+ end
141
+
142
+ test 'does not extend remember period when extend period config is false' do
143
+ swap Devise, extend_remember_period: false, remember_for: 1.year do
144
+ user = create_user_and_remember
145
+ old_remember_token = nil
146
+
147
+ travel_to 1.day.ago do
148
+ get root_path
149
+ old_remember_token = request.cookies['remember_user_token']
150
+ end
151
+
152
+ get root_path
153
+ current_remember_token = request.cookies['remember_user_token']
154
+
155
+ assert_equal old_remember_token, current_remember_token
156
+ end
157
+ end
158
+
112
159
  test 'do not remember other scopes' do
113
- user = create_user_and_remember
160
+ create_user_and_remember
114
161
  get root_path
115
162
  assert_response :success
116
163
  assert warden.authenticated?(:user)
@@ -118,15 +165,15 @@ class RememberMeTest < ActionController::IntegrationTest
118
165
  end
119
166
 
120
167
  test 'do not remember with invalid token' do
121
- user = create_user_and_remember('add')
168
+ create_user_and_remember('add')
122
169
  get users_path
123
170
  assert_not warden.authenticated?(:user)
124
171
  assert_redirected_to new_user_session_path
125
172
  end
126
173
 
127
174
  test 'do not remember with expired token' do
128
- user = create_user_and_remember
129
- swap Devise, :remember_for => 0 do
175
+ create_user_and_remember
176
+ swap Devise, remember_for: 0.days do
130
177
  get users_path
131
178
  assert_not warden.authenticated?(:user)
132
179
  assert_redirected_to new_user_session_path
@@ -134,7 +181,7 @@ class RememberMeTest < ActionController::IntegrationTest
134
181
  end
135
182
 
136
183
  test 'do not remember the user anymore after forget' do
137
- user = create_user_and_remember
184
+ create_user_and_remember
138
185
  get users_path
139
186
  assert warden.authenticated?(:user)
140
187
 
@@ -155,4 +202,13 @@ class RememberMeTest < ActionController::IntegrationTest
155
202
  get users_path
156
203
  assert_not warden.authenticated?(:user)
157
204
  end
205
+
206
+ test 'valid sign in calls after_remembered callback' do
207
+ user = create_user_and_remember
208
+
209
+ User.expects(:serialize_from_cookie).returns user
210
+ user.expects :after_remembered
211
+
212
+ get new_user_registration_path
213
+ end
158
214
  end
@@ -1,6 +1,6 @@
1
1
  require 'test_helper'
2
2
 
3
- class SessionTimeoutTest < ActionController::IntegrationTest
3
+ class SessionTimeoutTest < ActionDispatch::IntegrationTest
4
4
 
5
5
  def last_request_at
6
6
  @controller.user_session['last_request_at']
@@ -8,12 +8,11 @@ class SessionTimeoutTest < ActionController::IntegrationTest
8
8
 
9
9
  test 'set last request at in user session after each request' do
10
10
  sign_in_as_user
11
- old_last_request = last_request_at
12
11
  assert_not_nil last_request_at
13
12
 
13
+ @controller.user_session.delete('last_request_at')
14
14
  get users_path
15
15
  assert_not_nil last_request_at
16
- assert_not_equal old_last_request, last_request_at
17
16
  end
18
17
 
19
18
  test 'set last request at in user session after each request is skipped if tracking is disabled' do
@@ -25,6 +24,18 @@ class SessionTimeoutTest < ActionController::IntegrationTest
25
24
  assert_equal old_last_request, last_request_at
26
25
  end
27
26
 
27
+ test 'does not set last request at in user session after each request if timeoutable is disabled' do
28
+ sign_in_as_user
29
+ old_last_request = last_request_at
30
+ assert_not_nil last_request_at
31
+
32
+ new_time = 2.seconds.from_now
33
+ Time.stubs(:now).returns(new_time)
34
+
35
+ get users_path, {}, 'devise.skip_timeoutable' => true
36
+ assert_equal old_last_request, last_request_at
37
+ end
38
+
28
39
  test 'does not time out user session before default limit time' do
29
40
  sign_in_as_user
30
41
  assert_response :success
@@ -35,14 +46,43 @@ class SessionTimeoutTest < ActionController::IntegrationTest
35
46
  assert warden.authenticated?(:user)
36
47
  end
37
48
 
38
- test 'time out user session after default limit time' do
49
+ test 'time out user session after default limit time when sign_out_all_scopes is false' do
50
+ swap Devise, sign_out_all_scopes: false do
51
+ sign_in_as_admin
52
+
53
+ user = sign_in_as_user
54
+ get expire_user_path(user)
55
+ assert_not_nil last_request_at
56
+
57
+ get users_path
58
+ assert_redirected_to users_path
59
+ assert_not warden.authenticated?(:user)
60
+ assert warden.authenticated?(:admin)
61
+ end
62
+ end
63
+
64
+ test 'time out all sessions after default limit time when sign_out_all_scopes is true' do
65
+ swap Devise, sign_out_all_scopes: true do
66
+ sign_in_as_admin
67
+
68
+ user = sign_in_as_user
69
+ get expire_user_path(user)
70
+ assert_not_nil last_request_at
71
+
72
+ get root_path
73
+ assert_not warden.authenticated?(:user)
74
+ assert_not warden.authenticated?(:admin)
75
+ end
76
+ end
77
+
78
+ test 'time out user session after deault limit time and redirect to latest get request' do
39
79
  user = sign_in_as_user
40
- get expire_user_path(user)
41
- assert_not_nil last_request_at
80
+ visit edit_form_user_path(user)
42
81
 
43
- get users_path
44
- assert_redirected_to users_path
45
- assert_not warden.authenticated?(:user)
82
+ click_button 'Update'
83
+ sign_in_as_user
84
+
85
+ assert_equal edit_form_user_url(user), current_url
46
86
  end
47
87
 
48
88
  test 'time out is not triggered on sign out' do
@@ -57,36 +97,33 @@ class SessionTimeoutTest < ActionController::IntegrationTest
57
97
  assert_contain 'Signed out successfully'
58
98
  end
59
99
 
60
- test 'time out is not triggered on sign in' do
100
+ test 'expired session is not extended by sign in page' do
61
101
  user = sign_in_as_user
62
102
  get expire_user_path(user)
103
+ assert warden.authenticated?(:user)
63
104
 
64
- post "/users/sign_in", :email => user.email, :password => "123456"
65
-
66
- assert_response :redirect
105
+ get "/users/sign_in"
106
+ assert_redirected_to "/users/sign_in"
67
107
  follow_redirect!
68
- assert_contain 'You are signed in'
108
+
109
+ assert_response :success
110
+ assert_contain 'Sign in'
111
+ assert_not warden.authenticated?(:user)
69
112
  end
70
113
 
71
- test 'admin does not explode on time out' do
72
- admin = sign_in_as_admin
73
- get expire_admin_path(admin)
114
+ test 'time out is not triggered on sign in' do
115
+ user = sign_in_as_user
116
+ get expire_user_path(user)
74
117
 
75
- Admin.send :define_method, :reset_authentication_token! do
76
- nil
77
- end
118
+ post "/users/sign_in", email: user.email, password: "123456"
78
119
 
79
- begin
80
- get admins_path
81
- assert_redirected_to admins_path
82
- assert_not warden.authenticated?(:admin)
83
- ensure
84
- Admin.send(:remove_method, :reset_authentication_token!)
85
- end
120
+ assert_response :redirect
121
+ follow_redirect!
122
+ assert_contain 'You are signed in'
86
123
  end
87
124
 
88
125
  test 'user configured timeout limit' do
89
- swap Devise, :timeout_in => 8.minutes do
126
+ swap Devise, timeout_in: 8.minutes do
90
127
  user = sign_in_as_user
91
128
 
92
129
  get users_path
@@ -102,8 +139,8 @@ class SessionTimeoutTest < ActionController::IntegrationTest
102
139
  end
103
140
 
104
141
  test 'error message with i18n' do
105
- store_translations :en, :devise => {
106
- :failure => { :user => { :timeout => 'Session expired!' } }
142
+ store_translations :en, devise: {
143
+ failure: { user: { timeout: 'Session expired!' } }
107
144
  } do
108
145
  user = sign_in_as_user
109
146
 
@@ -115,8 +152,8 @@ class SessionTimeoutTest < ActionController::IntegrationTest
115
152
  end
116
153
 
117
154
  test 'error message with i18n with double redirect' do
118
- store_translations :en, :devise => {
119
- :failure => { :user => { :timeout => 'Session expired!' } }
155
+ store_translations :en, devise: {
156
+ failure: { user: { timeout: 'Session expired!' } }
120
157
  } do
121
158
  user = sign_in_as_user
122
159
 
@@ -129,7 +166,7 @@ class SessionTimeoutTest < ActionController::IntegrationTest
129
166
  end
130
167
 
131
168
  test 'time out not triggered if remembered' do
132
- user = sign_in_as_user :remember_me => true
169
+ user = sign_in_as_user remember_me: true
133
170
  get expire_user_path(user)
134
171
  assert_not_nil last_request_at
135
172
 
@@ -137,4 +174,11 @@ class SessionTimeoutTest < ActionController::IntegrationTest
137
174
  assert_response :success
138
175
  assert warden.authenticated?(:user)
139
176
  end
177
+
178
+ test 'does not crash when the last_request_at is a String' do
179
+ user = sign_in_as_user
180
+
181
+ get edit_form_user_path(user, last_request_at: Time.now.utc.to_s)
182
+ get users_path
183
+ end
140
184
  end
@@ -1,6 +1,6 @@
1
1
  require 'test_helper'
2
2
 
3
- class TrackableHooksTest < ActionController::IntegrationTest
3
+ class TrackableHooksTest < ActionDispatch::IntegrationTest
4
4
 
5
5
  test "current and last sign in timestamps are updated on each sign in" do
6
6
  user = create_user
@@ -10,8 +10,8 @@ class TrackableHooksTest < ActionController::IntegrationTest
10
10
  sign_in_as_user
11
11
  user.reload
12
12
 
13
- assert_kind_of Time, user.current_sign_in_at
14
- assert_kind_of Time, user.last_sign_in_at
13
+ assert user.current_sign_in_at.acts_like?(:time)
14
+ assert user.last_sign_in_at.acts_like?(:time)
15
15
 
16
16
  assert_equal user.current_sign_in_at, user.last_sign_in_at
17
17
  assert user.current_sign_in_at >= user.created_at
@@ -63,8 +63,8 @@ class TrackableHooksTest < ActionController::IntegrationTest
63
63
  end
64
64
 
65
65
  test "does not update anything if user has signed out along the way" do
66
- swap Devise, :allow_unconfirmed_access_for => 0 do
67
- user = create_user(:confirm => false)
66
+ swap Devise, allow_unconfirmed_access_for: 0.days do
67
+ user = create_user(confirm: false)
68
68
  sign_in_as_user
69
69
 
70
70
  user.reload
@@ -46,34 +46,54 @@ class ConfirmationInstructionsTest < ActionMailer::TestCase
46
46
  assert_equal ['custom@example.com'], mail.from
47
47
  end
48
48
 
49
+ test 'setup sender from custom mailer defaults with proc' do
50
+ Devise.mailer = 'Users::FromProcMailer'
51
+ assert_equal ['custom@example.com'], mail.from
52
+ end
53
+
54
+ test 'custom mailer renders parent mailer template' do
55
+ Devise.mailer = 'Users::Mailer'
56
+ assert_present mail.body.encoded
57
+ end
58
+
49
59
  test 'setup reply to as copy from sender' do
50
60
  assert_equal ['test@example.com'], mail.reply_to
51
61
  end
52
62
 
63
+ test 'setup reply to as different if set in defaults' do
64
+ Devise.mailer = 'Users::ReplyToMailer'
65
+ assert_equal ['custom@example.com'], mail.from
66
+ assert_equal ['custom_reply_to@example.com'], mail.reply_to
67
+ end
68
+
53
69
  test 'setup subject from I18n' do
54
- store_translations :en, :devise => { :mailer => { :confirmation_instructions => { :subject => 'Account Confirmation' } } } do
70
+ store_translations :en, devise: { mailer: { confirmation_instructions: { subject: 'Account Confirmation' } } } do
55
71
  assert_equal 'Account Confirmation', mail.subject
56
72
  end
57
73
  end
58
74
 
59
75
  test 'subject namespaced by model' do
60
- store_translations :en, :devise => { :mailer => { :confirmation_instructions => { :user_subject => 'User Account Confirmation' } } } do
76
+ store_translations :en, devise: { mailer: { confirmation_instructions: { user_subject: 'User Account Confirmation' } } } do
61
77
  assert_equal 'User Account Confirmation', mail.subject
62
78
  end
63
79
  end
64
80
 
65
81
  test 'body should have user info' do
66
- assert_match /#{user.email}/, mail.body.encoded
82
+ assert_match user.email, mail.body.encoded
67
83
  end
68
84
 
69
85
  test 'body should have link to confirm the account' do
70
- host = ActionMailer::Base.default_url_options[:host]
71
- confirmation_url_regexp = %r{<a href=\"http://#{host}/users/confirmation\?confirmation_token=#{user.confirmation_token}">}
72
- assert_match confirmation_url_regexp, mail.body.encoded
86
+ host, port = ActionMailer::Base.default_url_options.values_at :host, :port
87
+
88
+ if mail.body.encoded =~ %r{<a href=\"http://#{host}:#{port}/users/confirmation\?confirmation_token=([^"]+)">}
89
+ assert_equal $1, user.confirmation_token
90
+ else
91
+ flunk "expected confirmation url regex to match"
92
+ end
73
93
  end
74
94
 
75
95
  test 'renders a scoped if scoped_views is set to true' do
76
- swap Devise, :scoped_views => true do
96
+ swap Devise, scoped_views: true do
77
97
  assert_equal user.email, mail.body.decoded
78
98
  end
79
99
  end
@@ -88,7 +108,7 @@ class ConfirmationInstructionsTest < ActionMailer::TestCase
88
108
  end
89
109
 
90
110
  test 'mailer sender accepts a proc' do
91
- swap Devise, :mailer_sender => proc { "another@example.com" } do
111
+ swap Devise, mailer_sender: proc { "another@example.com" } do
92
112
  assert_equal ['another@example.com'], mail.from
93
113
  end
94
114
  end