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
@@ -8,14 +8,17 @@ class ViewsGeneratorTest < Rails::Generators::TestCase
8
8
  test "Assert all views are properly created with no params" do
9
9
  run_generator
10
10
  assert_files
11
+ assert_shared_links
11
12
  end
12
13
 
13
- test "Assert all views are properly created with scope param param" do
14
+ test "Assert all views are properly created with scope param" do
14
15
  run_generator %w(users)
15
16
  assert_files "users"
17
+ assert_shared_links "users"
16
18
 
17
19
  run_generator %w(admins)
18
20
  assert_files "admins"
21
+ assert_shared_links "admins"
19
22
  end
20
23
 
21
24
  test "Assert views with simple form" do
@@ -30,7 +33,43 @@ class ViewsGeneratorTest < Rails::Generators::TestCase
30
33
 
31
34
  test "Assert views with markerb" do
32
35
  run_generator %w(--markerb)
33
- assert_files nil, :mail_template_engine => "markerb"
36
+ assert_files nil, mail_template_engine: "markerb"
37
+ end
38
+
39
+
40
+ test "Assert only views within specified directories" do
41
+ run_generator %w(-v sessions registrations)
42
+ assert_file "app/views/devise/sessions/new.html.erb"
43
+ assert_file "app/views/devise/registrations/new.html.erb"
44
+ assert_file "app/views/devise/registrations/edit.html.erb"
45
+ assert_no_file "app/views/devise/confirmations/new.html.erb"
46
+ assert_no_file "app/views/devise/mailer/confirmation_instructions.html.erb"
47
+ end
48
+
49
+ test "Assert mailer specific directory with simple form" do
50
+ run_generator %w(-v mailer -b simple_form_for)
51
+ assert_file "app/views/devise/mailer/confirmation_instructions.html.erb"
52
+ assert_file "app/views/devise/mailer/reset_password_instructions.html.erb"
53
+ assert_file "app/views/devise/mailer/unlock_instructions.html.erb"
54
+ end
55
+
56
+ test "Assert specified directories with scope" do
57
+ run_generator %w(users -v sessions)
58
+ assert_file "app/views/users/sessions/new.html.erb"
59
+ assert_no_file "app/views/users/confirmations/new.html.erb"
60
+ end
61
+
62
+ test "Assert specified directories with simple form" do
63
+ run_generator %w(-v registrations -b simple_form_for)
64
+ assert_file "app/views/devise/registrations/new.html.erb", /simple_form_for/
65
+ assert_no_file "app/views/devise/confirmations/new.html.erb"
66
+ end
67
+
68
+ test "Assert specified directories with markerb" do
69
+ run_generator %w(--markerb -v passwords mailer)
70
+ assert_file "app/views/devise/passwords/new.html.erb"
71
+ assert_no_file "app/views/devise/confirmations/new.html.erb"
72
+ assert_file "app/views/devise/mailer/reset_password_instructions.markerb"
34
73
  end
35
74
 
36
75
  def assert_files(scope = nil, options={})
@@ -46,7 +85,19 @@ class ViewsGeneratorTest < Rails::Generators::TestCase
46
85
  assert_file "app/views/#{scope}/registrations/new.html.erb"
47
86
  assert_file "app/views/#{scope}/registrations/edit.html.erb"
48
87
  assert_file "app/views/#{scope}/sessions/new.html.erb"
49
- assert_file "app/views/#{scope}/shared/_links.erb"
88
+ assert_file "app/views/#{scope}/shared/_links.html.erb"
50
89
  assert_file "app/views/#{scope}/unlocks/new.html.erb"
51
90
  end
91
+
92
+ def assert_shared_links(scope = nil)
93
+ scope = "devise" if scope.nil?
94
+ link = /<%= render \"#{scope}\/shared\/links\" %>/
95
+
96
+ assert_file "app/views/#{scope}/passwords/edit.html.erb", link
97
+ assert_file "app/views/#{scope}/passwords/new.html.erb", link
98
+ assert_file "app/views/#{scope}/confirmations/new.html.erb", link
99
+ assert_file "app/views/#{scope}/registrations/new.html.erb", link
100
+ assert_file "app/views/#{scope}/sessions/new.html.erb", link
101
+ assert_file "app/views/#{scope}/unlocks/new.html.erb", link
102
+ end
52
103
  end
@@ -1,35 +1,34 @@
1
1
  require 'test_helper'
2
2
 
3
- class DeviseHelperTest < ActionController::IntegrationTest
3
+ class DeviseHelperTest < ActionDispatch::IntegrationTest
4
4
  setup do
5
- model_labels = { :models => { :user => "utilisateur" } }
6
-
7
- I18n.backend.store_translations :fr,
8
- {
9
- :errors => { :messages => { :not_saved => {
10
- :one => "Erreur lors de l'enregistrement de '%{resource}': 1 erreur.",
11
- :other => "Erreur lors de l'enregistrement de '%{resource}': %{count} erreurs."
5
+ model_labels = { models: { user: "the user" } }
6
+ translations = {
7
+ errors: { messages: { not_saved: {
8
+ one: "Can't save %{resource} because of 1 error",
9
+ other: "Can't save %{resource} because of %{count} errors",
12
10
  } } },
13
- :activerecord => model_labels,
14
- :mongoid => model_labels
11
+ activerecord: model_labels,
12
+ mongoid: model_labels
15
13
  }
16
14
 
17
- I18n.locale = 'fr'
15
+ I18n.available_locales
16
+ I18n.backend.store_translations(:en, translations)
18
17
  end
19
18
 
20
19
  teardown do
21
- I18n.locale = 'en'
20
+ I18n.reload!
22
21
  end
23
22
 
24
23
  test 'test errors.messages.not_saved with single error from i18n' do
25
24
  get new_user_registration_path
26
25
 
27
- fill_in 'password', :with => 'new_user123'
28
- fill_in 'password confirmation', :with => 'new_user123'
26
+ fill_in 'password', with: 'new_user123'
27
+ fill_in 'password confirmation', with: 'new_user123'
29
28
  click_button 'Sign up'
30
29
 
31
30
  assert_have_selector '#error_explanation'
32
- assert_contain "Erreur lors de l'enregistrement de 'utilisateur': 1 erreur"
31
+ assert_contain "Can't save the user because of 1 error"
33
32
  end
34
33
 
35
34
  test 'test errors.messages.not_saved with multiple errors from i18n' do
@@ -39,13 +38,12 @@ class DeviseHelperTest < ActionController::IntegrationTest
39
38
 
40
39
  get new_user_registration_path
41
40
 
42
- fill_in 'email', :with => 'invalid_email'
43
- fill_in 'password', :with => 'new_user123'
44
- fill_in 'password confirmation', :with => 'new_user321'
41
+ fill_in 'email', with: 'invalid_email'
42
+ fill_in 'password', with: 'new_user123'
43
+ fill_in 'password confirmation', with: 'new_user321'
45
44
  click_button 'Sign up'
46
45
 
47
46
  assert_have_selector '#error_explanation'
48
- assert_contain "Erreur lors de l'enregistrement de 'utilisateur': 2 erreurs"
47
+ assert_contain "Can't save the user because of 2 errors"
49
48
  end
50
49
  end
51
-
@@ -1,6 +1,6 @@
1
1
  require 'test_helper'
2
2
 
3
- class AuthenticationSanityTest < ActionController::IntegrationTest
3
+ class AuthenticationSanityTest < ActionDispatch::IntegrationTest
4
4
  test 'home should be accessible without sign in' do
5
5
  visit '/'
6
6
  assert_response :success
@@ -27,7 +27,7 @@ class AuthenticationSanityTest < ActionController::IntegrationTest
27
27
  end
28
28
 
29
29
  test 'sign out as user should not touch admin authentication if sign_out_all_scopes is false' do
30
- swap Devise, :sign_out_all_scopes => false do
30
+ swap Devise, sign_out_all_scopes: false do
31
31
  sign_in_as_user
32
32
  sign_in_as_admin
33
33
  get destroy_user_session_path
@@ -37,7 +37,7 @@ class AuthenticationSanityTest < ActionController::IntegrationTest
37
37
  end
38
38
 
39
39
  test 'sign out as admin should not touch user authentication if sign_out_all_scopes is false' do
40
- swap Devise, :sign_out_all_scopes => false do
40
+ swap Devise, sign_out_all_scopes: false do
41
41
  sign_in_as_user
42
42
  sign_in_as_admin
43
43
 
@@ -48,7 +48,7 @@ class AuthenticationSanityTest < ActionController::IntegrationTest
48
48
  end
49
49
 
50
50
  test 'sign out as user should also sign out admin if sign_out_all_scopes is true' do
51
- swap Devise, :sign_out_all_scopes => true do
51
+ swap Devise, sign_out_all_scopes: true do
52
52
  sign_in_as_user
53
53
  sign_in_as_admin
54
54
 
@@ -59,7 +59,7 @@ class AuthenticationSanityTest < ActionController::IntegrationTest
59
59
  end
60
60
 
61
61
  test 'sign out as admin should also sign out user if sign_out_all_scopes is true' do
62
- swap Devise, :sign_out_all_scopes => true do
62
+ swap Devise, sign_out_all_scopes: true do
63
63
  sign_in_as_user
64
64
  sign_in_as_admin
65
65
 
@@ -118,13 +118,13 @@ class AuthenticationSanityTest < ActionController::IntegrationTest
118
118
  assert_not warden.authenticated?(:admin)
119
119
  end
120
120
 
121
- test 'unauthenticated admin does not set message on sign out' do
121
+ test 'unauthenticated admin set message on sign out' do
122
122
  get destroy_admin_session_path
123
123
  assert_response :redirect
124
124
  assert_redirected_to root_path
125
125
 
126
126
  get root_path
127
- assert_not_contain 'Signed out successfully'
127
+ assert_contain 'Signed out successfully'
128
128
  end
129
129
 
130
130
  test 'scope uses custom failure app' do
@@ -134,7 +134,7 @@ class AuthenticationSanityTest < ActionController::IntegrationTest
134
134
  end
135
135
  end
136
136
 
137
- class AuthenticationRoutesRestrictions < ActionController::IntegrationTest
137
+ class AuthenticationRoutesRestrictions < ActionDispatch::IntegrationTest
138
138
  test 'not signed in should not be able to access private route (authenticate denied)' do
139
139
  get private_path
140
140
  assert_redirected_to new_admin_session_path
@@ -162,7 +162,7 @@ class AuthenticationRoutesRestrictions < ActionController::IntegrationTest
162
162
  end
163
163
 
164
164
  test 'signed in as inactive admin should not be able to access private/active route restricted to active admins (authenticate denied)' do
165
- sign_in_as_admin(:active => false)
165
+ sign_in_as_admin(active: false)
166
166
  assert warden.authenticated?(:admin)
167
167
  assert_not warden.authenticated?(:user)
168
168
 
@@ -172,7 +172,7 @@ class AuthenticationRoutesRestrictions < ActionController::IntegrationTest
172
172
  end
173
173
 
174
174
  test 'signed in as active admin should be able to access private/active route restricted to active admins (authenticate accepted)' do
175
- sign_in_as_admin(:active => true)
175
+ sign_in_as_admin(active: true)
176
176
  assert warden.authenticated?(:admin)
177
177
  assert_not warden.authenticated?(:user)
178
178
 
@@ -191,7 +191,7 @@ class AuthenticationRoutesRestrictions < ActionController::IntegrationTest
191
191
  get dashboard_path
192
192
 
193
193
  assert_response :success
194
- assert_template 'home/admin'
194
+ assert_template 'home/admin_dashboard'
195
195
  assert_contain 'Admin dashboard'
196
196
  end
197
197
 
@@ -203,7 +203,7 @@ class AuthenticationRoutesRestrictions < ActionController::IntegrationTest
203
203
  get dashboard_path
204
204
 
205
205
  assert_response :success
206
- assert_template 'home/user'
206
+ assert_template 'home/user_dashboard'
207
207
  assert_contain 'User dashboard'
208
208
  end
209
209
 
@@ -214,7 +214,7 @@ class AuthenticationRoutesRestrictions < ActionController::IntegrationTest
214
214
  end
215
215
 
216
216
  test 'signed in as inactive admin should not be able to access dashboard/active route restricted to active admins (authenticated denied)' do
217
- sign_in_as_admin(:active => false)
217
+ sign_in_as_admin(active: false)
218
218
  assert warden.authenticated?(:admin)
219
219
  assert_not warden.authenticated?(:user)
220
220
 
@@ -224,7 +224,7 @@ class AuthenticationRoutesRestrictions < ActionController::IntegrationTest
224
224
  end
225
225
 
226
226
  test 'signed in as active admin should be able to access dashboard/active route restricted to active admins (authenticated accepted)' do
227
- sign_in_as_admin(:active => true)
227
+ sign_in_as_admin(active: true)
228
228
  assert warden.authenticated?(:admin)
229
229
  assert_not warden.authenticated?(:user)
230
230
 
@@ -254,7 +254,7 @@ class AuthenticationRoutesRestrictions < ActionController::IntegrationTest
254
254
  end
255
255
  end
256
256
 
257
- class AuthenticationRedirectTest < ActionController::IntegrationTest
257
+ class AuthenticationRedirectTest < ActionDispatch::IntegrationTest
258
258
  test 'redirect from warden shows sign in or sign up message' do
259
259
  get admins_path
260
260
 
@@ -277,7 +277,7 @@ class AuthenticationRedirectTest < ActionController::IntegrationTest
277
277
  assert_equal users_path, session[:"user_return_to"]
278
278
 
279
279
  follow_redirect!
280
- sign_in_as_user :visit => false
280
+ sign_in_as_user visit: false
281
281
 
282
282
  assert_current_url '/users'
283
283
  assert_nil session[:"user_return_to"]
@@ -293,7 +293,7 @@ class AuthenticationRedirectTest < ActionController::IntegrationTest
293
293
  assert_equal users_path, session[:"user_return_to"]
294
294
 
295
295
  follow_redirect!
296
- sign_in_as_user :visit => false
296
+ sign_in_as_user visit: false
297
297
 
298
298
  assert_current_url '/users'
299
299
  assert_nil session[:"user_return_to"]
@@ -317,7 +317,7 @@ class AuthenticationRedirectTest < ActionController::IntegrationTest
317
317
  end
318
318
  end
319
319
 
320
- class AuthenticationSessionTest < ActionController::IntegrationTest
320
+ class AuthenticationSessionTest < ActionDispatch::IntegrationTest
321
321
  test 'destroyed account is signed out' do
322
322
  sign_in_as_user
323
323
  get '/users'
@@ -327,28 +327,54 @@ class AuthenticationSessionTest < ActionController::IntegrationTest
327
327
  assert_redirected_to new_user_session_path
328
328
  end
329
329
 
330
+ test 'refreshes _csrf_token' do
331
+ ApplicationController.allow_forgery_protection = true
332
+
333
+ begin
334
+ get new_user_session_path
335
+ token = request.session[:_csrf_token]
336
+
337
+ sign_in_as_user
338
+ assert_not_equal request.session[:_csrf_token], token
339
+ ensure
340
+ ApplicationController.allow_forgery_protection = false
341
+ end
342
+ end
343
+
330
344
  test 'allows session to be set for a given scope' do
331
345
  sign_in_as_user
332
346
  get '/users'
333
347
  assert_equal "Cart", @controller.user_session[:cart]
334
348
  end
335
349
 
336
- test 'does not explode when invalid user class is stored in session' do
337
- klass = User
338
- paths = ActiveSupport::Dependencies.autoload_paths.dup
339
-
350
+ test 'does not explode when class name is still stored in session' do
351
+ # In order to test that old sessions do not break with the new scoped
352
+ # deserialization, we need to serialize the session the old way. This is
353
+ # done by removing the newly used scoped serialization method
354
+ # (#user_serialize) and bringing back the old uncsoped #serialize method
355
+ # that includes the record's class name in the serialization.
340
356
  begin
357
+ Warden::SessionSerializer.class_eval do
358
+ alias_method :original_serialize, :serialize
359
+ alias_method :original_user_serialize, :user_serialize
360
+ remove_method :user_serialize
361
+
362
+ def serialize(record)
363
+ klass = record.class
364
+ array = klass.serialize_into_session(record)
365
+ array.unshift(klass.name)
366
+ end
367
+ end
368
+
341
369
  sign_in_as_user
342
370
  assert warden.authenticated?(:user)
343
-
344
- Object.send :remove_const, :User
345
- ActiveSupport::Dependencies.autoload_paths.clear
346
-
347
- visit "/users"
348
- assert_not warden.authenticated?(:user)
349
371
  ensure
350
- Object.const_set(:User, klass)
351
- ActiveSupport::Dependencies.autoload_paths.replace(paths)
372
+ Warden::SessionSerializer.class_eval do
373
+ alias_method :serialize, :original_serialize
374
+ remove_method :original_serialize
375
+ alias_method :user_serialize, :original_user_serialize
376
+ remove_method :original_user_serialize
377
+ end
352
378
  end
353
379
  end
354
380
 
@@ -364,9 +390,9 @@ class AuthenticationSessionTest < ActionController::IntegrationTest
364
390
  end
365
391
  end
366
392
 
367
- class AuthenticationWithScopedViewsTest < ActionController::IntegrationTest
393
+ class AuthenticationWithScopedViewsTest < ActionDispatch::IntegrationTest
368
394
  test 'renders the scoped view if turned on and view is available' do
369
- swap Devise, :scoped_views => true do
395
+ swap Devise, scoped_views: true do
370
396
  assert_raise Webrat::NotFoundError do
371
397
  sign_in_as_user
372
398
  end
@@ -389,7 +415,7 @@ class AuthenticationWithScopedViewsTest < ActionController::IntegrationTest
389
415
  end
390
416
 
391
417
  test 'does not render the scoped view if turned off' do
392
- swap Devise, :scoped_views => false do
418
+ swap Devise, scoped_views: false do
393
419
  assert_nothing_raised do
394
420
  sign_in_as_user
395
421
  end
@@ -397,7 +423,7 @@ class AuthenticationWithScopedViewsTest < ActionController::IntegrationTest
397
423
  end
398
424
 
399
425
  test 'does not render the scoped view if not available' do
400
- swap Devise, :scoped_views => true do
426
+ swap Devise, scoped_views: true do
401
427
  assert_nothing_raised do
402
428
  sign_in_as_admin
403
429
  end
@@ -405,9 +431,9 @@ class AuthenticationWithScopedViewsTest < ActionController::IntegrationTest
405
431
  end
406
432
  end
407
433
 
408
- class AuthenticationOthersTest < ActionController::IntegrationTest
434
+ class AuthenticationOthersTest < ActionDispatch::IntegrationTest
409
435
  test 'handles unverified requests gets rid of caches' do
410
- swap UsersController, :allow_forgery_protection => true do
436
+ swap ApplicationController, allow_forgery_protection: true do
411
437
  post exhibit_user_url(1)
412
438
  assert_not warden.authenticated?(:user)
413
439
 
@@ -422,7 +448,7 @@ class AuthenticationOthersTest < ActionController::IntegrationTest
422
448
 
423
449
  test 'uses the custom controller with the custom controller view' do
424
450
  get '/admin_area/sign_in'
425
- assert_contain 'Sign in'
451
+ assert_contain 'Log in'
426
452
  assert_contain 'Welcome to "admins/sessions" controller!'
427
453
  assert_contain 'Welcome to "sessions/new" view!'
428
454
  end
@@ -447,28 +473,28 @@ class AuthenticationOthersTest < ActionController::IntegrationTest
447
473
  test 'sign in with script name' do
448
474
  assert_nothing_raised do
449
475
  get new_user_session_path, {}, "SCRIPT_NAME" => "/omg"
450
- fill_in "email", :with => "user@test.com"
476
+ fill_in "email", with: "user@test.com"
451
477
  end
452
478
  end
453
479
 
454
480
  test 'sign in stub in xml format' do
455
- get new_user_session_path(:format => 'xml')
481
+ get new_user_session_path(format: 'xml')
456
482
  assert_match '<?xml version="1.0" encoding="UTF-8"?>', response.body
457
483
  assert_match /<user>.*<\/user>/m, response.body
458
484
  assert_match '<email></email>', response.body
459
- assert_match '<password nil="true"></password>', response.body
485
+ assert_match '<password nil="true"', response.body
460
486
  end
461
487
 
462
488
  test 'sign in stub in json format' do
463
- get new_user_session_path(:format => 'json')
489
+ get new_user_session_path(format: 'json')
464
490
  assert_match '{"user":{', response.body
465
491
  assert_match '"email":""', response.body
466
492
  assert_match '"password":null', response.body
467
493
  end
468
494
 
469
495
  test 'sign in stub in json with non attribute key' do
470
- swap Devise, :authentication_keys => [:other_key] do
471
- get new_user_session_path(:format => 'json')
496
+ swap Devise, authentication_keys: [:other_key] do
497
+ get new_user_session_path(format: 'json')
472
498
  assert_match '{"user":{', response.body
473
499
  assert_match '"other_key":null', response.body
474
500
  assert_match '"password":null', response.body
@@ -476,79 +502,110 @@ class AuthenticationOthersTest < ActionController::IntegrationTest
476
502
  end
477
503
 
478
504
  test 'uses the mapping from router' do
479
- sign_in_as_user :visit => "/as/sign_in"
505
+ sign_in_as_user visit: "/as/sign_in"
480
506
  assert warden.authenticated?(:user)
481
507
  assert_not warden.authenticated?(:admin)
482
508
  end
483
509
 
484
510
  test 'sign in with xml format returns xml response' do
485
511
  create_user
486
- post user_session_path(:format => 'xml'), :user => {:email => "user@test.com", :password => '123456'}
512
+ post user_session_path(format: 'xml'), user: {email: "user@test.com", password: '12345678'}
487
513
  assert_response :success
488
514
  assert response.body.include? %(<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<user>)
489
515
  end
490
516
 
491
517
  test 'sign in with xml format is idempotent' do
492
- get new_user_session_path(:format => 'xml')
518
+ get new_user_session_path(format: 'xml')
493
519
  assert_response :success
494
520
 
495
521
  create_user
496
- post user_session_path(:format => 'xml'), :user => {:email => "user@test.com", :password => '123456'}
522
+ post user_session_path(format: 'xml'), user: {email: "user@test.com", password: '12345678'}
497
523
  assert_response :success
498
524
 
499
- get new_user_session_path(:format => 'xml')
525
+ get new_user_session_path(format: 'xml')
500
526
  assert_response :success
501
527
 
502
- post user_session_path(:format => 'xml'), :user => {:email => "user@test.com", :password => '123456'}
528
+ post user_session_path(format: 'xml'), user: {email: "user@test.com", password: '12345678'}
503
529
  assert_response :success
504
530
  assert response.body.include? %(<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<user>)
505
531
  end
506
532
 
507
- test 'sign out with xml format returns ok response' do
533
+ test 'sign out with html redirects' do
534
+ sign_in_as_user
535
+ get destroy_user_session_path
536
+ assert_response :redirect
537
+ assert_current_url '/'
538
+
539
+ sign_in_as_user
540
+ get destroy_user_session_path(format: 'html')
541
+ assert_response :redirect
542
+ assert_current_url '/'
543
+ end
544
+
545
+ test 'sign out with xml format returns no content' do
508
546
  sign_in_as_user
509
- get destroy_user_session_path(:format => 'xml')
547
+ get destroy_user_session_path(format: 'xml')
510
548
  assert_response :no_content
511
549
  assert_not warden.authenticated?(:user)
512
550
  end
513
551
 
514
- test 'sign out with json format returns empty json response' do
552
+ test 'sign out with json format returns no content' do
515
553
  sign_in_as_user
516
- get destroy_user_session_path(:format => 'json')
554
+ get destroy_user_session_path(format: 'json')
517
555
  assert_response :no_content
518
556
  assert_not warden.authenticated?(:user)
519
557
  end
558
+
559
+ test 'sign out with non-navigational format via XHR does not redirect' do
560
+ swap Devise, navigational_formats: ['*/*', :html] do
561
+ sign_in_as_user
562
+ xml_http_request :get, destroy_user_session_path, {}, { "HTTP_ACCEPT" => "application/json,text/javascript,*/*" } # NOTE: Bug is triggered by combination of XHR and */*.
563
+ assert_response :no_content
564
+ assert_not warden.authenticated?(:user)
565
+ end
566
+ end
567
+
568
+ # Belt and braces ... Perhaps this test is not necessary?
569
+ test 'sign out with navigational format via XHR does redirect' do
570
+ swap Devise, navigational_formats: ['*/*', :html] do
571
+ sign_in_as_user
572
+ xml_http_request :get, destroy_user_session_path, {}, { "HTTP_ACCEPT" => "text/html,*/*" }
573
+ assert_response :redirect
574
+ assert_not warden.authenticated?(:user)
575
+ end
576
+ end
520
577
  end
521
578
 
522
- class AuthenticationKeysTest < ActionController::IntegrationTest
579
+ class AuthenticationKeysTest < ActionDispatch::IntegrationTest
523
580
  test 'missing authentication keys cause authentication to abort' do
524
- swap Devise, :authentication_keys => [:subdomain] do
581
+ swap Devise, authentication_keys: [:subdomain] do
525
582
  sign_in_as_user
526
- assert_contain "Invalid email or password."
583
+ assert_contain "Invalid subdomain or password."
527
584
  assert_not warden.authenticated?(:user)
528
585
  end
529
586
  end
530
587
 
531
588
  test 'missing authentication keys cause authentication to abort unless marked as not required' do
532
- swap Devise, :authentication_keys => { :email => true, :subdomain => false } do
589
+ swap Devise, authentication_keys: { email: true, subdomain: false } do
533
590
  sign_in_as_user
534
591
  assert warden.authenticated?(:user)
535
592
  end
536
593
  end
537
594
  end
538
595
 
539
- class AuthenticationRequestKeysTest < ActionController::IntegrationTest
596
+ class AuthenticationRequestKeysTest < ActionDispatch::IntegrationTest
540
597
  test 'request keys are used on authentication' do
541
598
  host! 'foo.bar.baz'
542
599
 
543
- swap Devise, :request_keys => [:subdomain] do
544
- User.expects(:find_for_authentication).with(:subdomain => 'foo', :email => 'user@test.com').returns(create_user)
600
+ swap Devise, request_keys: [:subdomain] do
601
+ User.expects(:find_for_authentication).with(subdomain: 'foo', email: 'user@test.com').returns(create_user)
545
602
  sign_in_as_user
546
603
  assert warden.authenticated?(:user)
547
604
  end
548
605
  end
549
606
 
550
607
  test 'invalid request keys raises NoMethodError' do
551
- swap Devise, :request_keys => [:unknown_method] do
608
+ swap Devise, request_keys: [:unknown_method] do
552
609
  assert_raise NoMethodError do
553
610
  sign_in_as_user
554
611
  end
@@ -560,7 +617,7 @@ class AuthenticationRequestKeysTest < ActionController::IntegrationTest
560
617
  test 'blank request keys cause authentication to abort' do
561
618
  host! 'test.com'
562
619
 
563
- swap Devise, :request_keys => [:subdomain] do
620
+ swap Devise, request_keys: [:subdomain] do
564
621
  sign_in_as_user
565
622
  assert_contain "Invalid email or password."
566
623
  assert_not warden.authenticated?(:user)
@@ -570,16 +627,16 @@ class AuthenticationRequestKeysTest < ActionController::IntegrationTest
570
627
  test 'blank request keys cause authentication to abort unless if marked as not required' do
571
628
  host! 'test.com'
572
629
 
573
- swap Devise, :request_keys => { :subdomain => false } do
630
+ swap Devise, request_keys: { subdomain: false } do
574
631
  sign_in_as_user
575
632
  assert warden.authenticated?(:user)
576
633
  end
577
634
  end
578
635
  end
579
636
 
580
- class AuthenticationSignOutViaTest < ActionController::IntegrationTest
637
+ class AuthenticationSignOutViaTest < ActionDispatch::IntegrationTest
581
638
  def sign_in!(scope)
582
- sign_in_as_admin(:visit => send("new_#{scope}_session_path"))
639
+ sign_in_as_admin(visit: send("new_#{scope}_session_path"))
583
640
  assert warden.authenticated?(scope)
584
641
  end
585
642
 
@@ -631,3 +688,42 @@ class AuthenticationSignOutViaTest < ActionController::IntegrationTest
631
688
  assert warden.authenticated?(:sign_out_via_delete_or_post)
632
689
  end
633
690
  end
691
+
692
+ class DoubleAuthenticationRedirectTest < ActionDispatch::IntegrationTest
693
+ test 'signed in as user redirects when visiting user sign in page' do
694
+ sign_in_as_user
695
+ get new_user_session_path(format: :html)
696
+ assert_redirected_to '/'
697
+ end
698
+
699
+ test 'signed in as admin redirects when visiting admin sign in page' do
700
+ sign_in_as_admin
701
+ get new_admin_session_path(format: :html)
702
+ assert_redirected_to '/admin_area/home'
703
+ end
704
+
705
+ test 'signed in as both user and admin redirects when visiting admin sign in page' do
706
+ sign_in_as_user
707
+ sign_in_as_admin
708
+ get new_user_session_path(format: :html)
709
+ assert_redirected_to '/'
710
+ get new_admin_session_path(format: :html)
711
+ assert_redirected_to '/admin_area/home'
712
+ end
713
+ end
714
+
715
+ class DoubleSignOutRedirectTest < ActionDispatch::IntegrationTest
716
+ test 'sign out after already having signed out redirects to sign in' do
717
+ sign_in_as_user
718
+
719
+ post destroy_sign_out_via_delete_or_post_session_path
720
+
721
+ get root_path
722
+ assert_contain 'Signed out successfully.'
723
+
724
+ post destroy_sign_out_via_delete_or_post_session_path
725
+
726
+ get root_path
727
+ assert_contain 'Signed out successfully.'
728
+ end
729
+ end