devise 4.4.0 → 4.7.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (220) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +125 -4
  3. data/MIT-LICENSE +2 -1
  4. data/README.md +124 -58
  5. data/app/controllers/devise/passwords_controller.rb +1 -0
  6. data/app/controllers/devise/registrations_controller.rb +26 -8
  7. data/app/controllers/devise_controller.rb +3 -3
  8. data/app/helpers/devise_helper.rb +21 -18
  9. data/app/mailers/devise/mailer.rb +5 -5
  10. data/app/views/devise/confirmations/new.html.erb +1 -1
  11. data/app/views/devise/passwords/edit.html.erb +3 -3
  12. data/app/views/devise/passwords/new.html.erb +1 -1
  13. data/app/views/devise/registrations/edit.html.erb +4 -4
  14. data/app/views/devise/registrations/new.html.erb +3 -3
  15. data/app/views/devise/sessions/new.html.erb +3 -3
  16. data/app/views/devise/shared/_error_messages.html.erb +15 -0
  17. data/app/views/devise/shared/_links.html.erb +7 -7
  18. data/app/views/devise/unlocks/new.html.erb +1 -1
  19. data/config/locales/en.yml +3 -2
  20. data/lib/devise.rb +5 -4
  21. data/lib/devise/controllers/helpers.rb +13 -13
  22. data/lib/devise/controllers/sign_in_out.rb +10 -4
  23. data/lib/devise/controllers/url_helpers.rb +1 -1
  24. data/lib/devise/failure_app.rb +25 -5
  25. data/lib/devise/hooks/timeoutable.rb +2 -2
  26. data/lib/devise/mapping.rb +1 -1
  27. data/lib/devise/models/authenticatable.rb +44 -45
  28. data/lib/devise/models/confirmable.rb +19 -4
  29. data/lib/devise/models/database_authenticatable.rb +50 -12
  30. data/lib/devise/models/lockable.rb +3 -3
  31. data/lib/devise/models/recoverable.rb +2 -2
  32. data/lib/devise/models/registerable.rb +2 -0
  33. data/lib/devise/models/rememberable.rb +1 -1
  34. data/lib/devise/models/trackable.rb +14 -2
  35. data/lib/devise/models/validatable.rb +1 -1
  36. data/lib/devise/parameter_filter.rb +2 -0
  37. data/lib/devise/parameter_sanitizer.rb +13 -1
  38. data/lib/devise/rails.rb +1 -5
  39. data/lib/devise/rails/routes.rb +6 -6
  40. data/lib/devise/secret_key_finder.rb +27 -0
  41. data/lib/devise/strategies/authenticatable.rb +1 -1
  42. data/lib/devise/strategies/database_authenticatable.rb +6 -1
  43. data/lib/devise/test/controller_helpers.rb +4 -1
  44. data/lib/devise/test/integration_helpers.rb +1 -1
  45. data/lib/devise/version.rb +1 -1
  46. data/lib/generators/active_record/devise_generator.rb +9 -9
  47. data/lib/generators/devise/controllers_generator.rb +1 -1
  48. data/lib/generators/devise/devise_generator.rb +1 -1
  49. data/lib/generators/devise/install_generator.rb +1 -5
  50. data/lib/generators/devise/orm_helpers.rb +2 -2
  51. data/lib/generators/devise/views_generator.rb +1 -1
  52. data/lib/generators/mongoid/devise_generator.rb +5 -5
  53. data/lib/generators/templates/README +9 -1
  54. data/lib/generators/templates/controllers/omniauth_callbacks_controller.rb +1 -1
  55. data/lib/generators/templates/devise.rb +37 -5
  56. data/lib/generators/templates/simple_form_for/confirmations/new.html.erb +5 -1
  57. data/lib/generators/templates/simple_form_for/passwords/edit.html.erb +10 -2
  58. data/lib/generators/templates/simple_form_for/passwords/new.html.erb +4 -1
  59. data/lib/generators/templates/simple_form_for/registrations/edit.html.erb +11 -3
  60. data/lib/generators/templates/simple_form_for/registrations/new.html.erb +11 -3
  61. data/lib/generators/templates/simple_form_for/sessions/new.html.erb +7 -2
  62. data/lib/generators/templates/simple_form_for/unlocks/new.html.erb +4 -1
  63. metadata +8 -308
  64. data/.gitignore +0 -10
  65. data/.travis.yml +0 -63
  66. data/.yardopts +0 -9
  67. data/CODE_OF_CONDUCT.md +0 -22
  68. data/CONTRIBUTING.md +0 -79
  69. data/Gemfile +0 -39
  70. data/Gemfile.lock +0 -193
  71. data/ISSUE_TEMPLATE.md +0 -19
  72. data/Rakefile +0 -37
  73. data/bin/test +0 -13
  74. data/devise.gemspec +0 -28
  75. data/devise.png +0 -0
  76. data/gemfiles/Gemfile.rails-4.1-stable +0 -32
  77. data/gemfiles/Gemfile.rails-4.1-stable.lock +0 -171
  78. data/gemfiles/Gemfile.rails-4.2-stable +0 -32
  79. data/gemfiles/Gemfile.rails-4.2-stable.lock +0 -192
  80. data/gemfiles/Gemfile.rails-5.0-stable +0 -33
  81. data/gemfiles/Gemfile.rails-5.0-stable.lock +0 -192
  82. data/guides/bug_report_templates/integration_test.rb +0 -106
  83. data/test/controllers/custom_registrations_controller_test.rb +0 -42
  84. data/test/controllers/custom_strategy_test.rb +0 -66
  85. data/test/controllers/helper_methods_test.rb +0 -24
  86. data/test/controllers/helpers_test.rb +0 -318
  87. data/test/controllers/inherited_controller_i18n_messages_test.rb +0 -53
  88. data/test/controllers/internal_helpers_test.rb +0 -129
  89. data/test/controllers/load_hooks_controller_test.rb +0 -21
  90. data/test/controllers/passwords_controller_test.rb +0 -34
  91. data/test/controllers/sessions_controller_test.rb +0 -108
  92. data/test/controllers/url_helpers_test.rb +0 -67
  93. data/test/delegator_test.rb +0 -21
  94. data/test/devise_test.rb +0 -109
  95. data/test/failure_app_test.rb +0 -340
  96. data/test/generators/active_record_generator_test.rb +0 -130
  97. data/test/generators/controllers_generator_test.rb +0 -50
  98. data/test/generators/devise_generator_test.rb +0 -41
  99. data/test/generators/install_generator_test.rb +0 -26
  100. data/test/generators/mongoid_generator_test.rb +0 -25
  101. data/test/generators/views_generator_test.rb +0 -105
  102. data/test/helpers/devise_helper_test.rb +0 -51
  103. data/test/integration/authenticatable_test.rb +0 -712
  104. data/test/integration/confirmable_test.rb +0 -326
  105. data/test/integration/database_authenticatable_test.rb +0 -97
  106. data/test/integration/http_authenticatable_test.rb +0 -108
  107. data/test/integration/lockable_test.rb +0 -242
  108. data/test/integration/mounted_engine_test.rb +0 -38
  109. data/test/integration/omniauthable_test.rb +0 -137
  110. data/test/integration/recoverable_test.rb +0 -349
  111. data/test/integration/registerable_test.rb +0 -365
  112. data/test/integration/rememberable_test.rb +0 -219
  113. data/test/integration/timeoutable_test.rb +0 -186
  114. data/test/integration/trackable_test.rb +0 -94
  115. data/test/mailers/confirmation_instructions_test.rb +0 -117
  116. data/test/mailers/email_changed_test.rb +0 -132
  117. data/test/mailers/mailer_test.rb +0 -20
  118. data/test/mailers/reset_password_instructions_test.rb +0 -98
  119. data/test/mailers/unlock_instructions_test.rb +0 -93
  120. data/test/mapping_test.rb +0 -136
  121. data/test/models/authenticatable_test.rb +0 -25
  122. data/test/models/confirmable_test.rb +0 -538
  123. data/test/models/database_authenticatable_test.rb +0 -283
  124. data/test/models/lockable_test.rb +0 -352
  125. data/test/models/omniauthable_test.rb +0 -9
  126. data/test/models/recoverable_test.rb +0 -263
  127. data/test/models/registerable_test.rb +0 -9
  128. data/test/models/rememberable_test.rb +0 -184
  129. data/test/models/serializable_test.rb +0 -60
  130. data/test/models/timeoutable_test.rb +0 -53
  131. data/test/models/trackable_test.rb +0 -52
  132. data/test/models/validatable_test.rb +0 -121
  133. data/test/models_test.rb +0 -155
  134. data/test/omniauth/config_test.rb +0 -61
  135. data/test/omniauth/url_helpers_test.rb +0 -53
  136. data/test/orm/active_record.rb +0 -19
  137. data/test/orm/mongoid.rb +0 -15
  138. data/test/parameter_sanitizer_test.rb +0 -77
  139. data/test/rails_app/Rakefile +0 -6
  140. data/test/rails_app/app/active_record/admin.rb +0 -8
  141. data/test/rails_app/app/active_record/shim.rb +0 -4
  142. data/test/rails_app/app/active_record/user.rb +0 -11
  143. data/test/rails_app/app/active_record/user_on_engine.rb +0 -9
  144. data/test/rails_app/app/active_record/user_on_main_app.rb +0 -9
  145. data/test/rails_app/app/active_record/user_with_validations.rb +0 -12
  146. data/test/rails_app/app/active_record/user_without_email.rb +0 -10
  147. data/test/rails_app/app/controllers/admins/sessions_controller.rb +0 -8
  148. data/test/rails_app/app/controllers/admins_controller.rb +0 -8
  149. data/test/rails_app/app/controllers/application_controller.rb +0 -13
  150. data/test/rails_app/app/controllers/application_with_fake_engine.rb +0 -32
  151. data/test/rails_app/app/controllers/custom/registrations_controller.rb +0 -33
  152. data/test/rails_app/app/controllers/home_controller.rb +0 -31
  153. data/test/rails_app/app/controllers/publisher/registrations_controller.rb +0 -4
  154. data/test/rails_app/app/controllers/publisher/sessions_controller.rb +0 -4
  155. data/test/rails_app/app/controllers/users/omniauth_callbacks_controller.rb +0 -16
  156. data/test/rails_app/app/controllers/users_controller.rb +0 -33
  157. data/test/rails_app/app/helpers/application_helper.rb +0 -5
  158. data/test/rails_app/app/mailers/users/from_proc_mailer.rb +0 -5
  159. data/test/rails_app/app/mailers/users/mailer.rb +0 -5
  160. data/test/rails_app/app/mailers/users/reply_to_mailer.rb +0 -6
  161. data/test/rails_app/app/mongoid/admin.rb +0 -31
  162. data/test/rails_app/app/mongoid/shim.rb +0 -25
  163. data/test/rails_app/app/mongoid/user.rb +0 -41
  164. data/test/rails_app/app/mongoid/user_on_engine.rb +0 -41
  165. data/test/rails_app/app/mongoid/user_on_main_app.rb +0 -41
  166. data/test/rails_app/app/mongoid/user_with_validations.rb +0 -37
  167. data/test/rails_app/app/mongoid/user_without_email.rb +0 -35
  168. data/test/rails_app/app/views/admins/index.html.erb +0 -1
  169. data/test/rails_app/app/views/admins/sessions/new.html.erb +0 -2
  170. data/test/rails_app/app/views/home/admin_dashboard.html.erb +0 -1
  171. data/test/rails_app/app/views/home/index.html.erb +0 -1
  172. data/test/rails_app/app/views/home/join.html.erb +0 -1
  173. data/test/rails_app/app/views/home/private.html.erb +0 -1
  174. data/test/rails_app/app/views/home/user_dashboard.html.erb +0 -1
  175. data/test/rails_app/app/views/layouts/application.html.erb +0 -24
  176. data/test/rails_app/app/views/users/edit_form.html.erb +0 -1
  177. data/test/rails_app/app/views/users/index.html.erb +0 -1
  178. data/test/rails_app/app/views/users/mailer/confirmation_instructions.erb +0 -1
  179. data/test/rails_app/app/views/users/sessions/new.html.erb +0 -1
  180. data/test/rails_app/bin/bundle +0 -3
  181. data/test/rails_app/bin/rails +0 -4
  182. data/test/rails_app/bin/rake +0 -4
  183. data/test/rails_app/config.ru +0 -4
  184. data/test/rails_app/config/application.rb +0 -46
  185. data/test/rails_app/config/boot.rb +0 -22
  186. data/test/rails_app/config/database.yml +0 -18
  187. data/test/rails_app/config/environment.rb +0 -7
  188. data/test/rails_app/config/environments/development.rb +0 -32
  189. data/test/rails_app/config/environments/production.rb +0 -88
  190. data/test/rails_app/config/environments/test.rb +0 -47
  191. data/test/rails_app/config/initializers/backtrace_silencers.rb +0 -9
  192. data/test/rails_app/config/initializers/devise.rb +0 -182
  193. data/test/rails_app/config/initializers/inflections.rb +0 -4
  194. data/test/rails_app/config/initializers/secret_token.rb +0 -5
  195. data/test/rails_app/config/initializers/session_store.rb +0 -3
  196. data/test/rails_app/config/routes.rb +0 -128
  197. data/test/rails_app/db/migrate/20100401102949_create_tables.rb +0 -77
  198. data/test/rails_app/db/schema.rb +0 -57
  199. data/test/rails_app/lib/shared_admin.rb +0 -23
  200. data/test/rails_app/lib/shared_user.rb +0 -32
  201. data/test/rails_app/lib/shared_user_without_email.rb +0 -28
  202. data/test/rails_app/lib/shared_user_without_omniauth.rb +0 -15
  203. data/test/rails_app/public/404.html +0 -26
  204. data/test/rails_app/public/422.html +0 -26
  205. data/test/rails_app/public/500.html +0 -26
  206. data/test/rails_app/public/favicon.ico +0 -0
  207. data/test/rails_test.rb +0 -11
  208. data/test/routes_test.rb +0 -281
  209. data/test/support/action_controller/record_identifier.rb +0 -12
  210. data/test/support/assertions.rb +0 -30
  211. data/test/support/helpers.rb +0 -83
  212. data/test/support/http_method_compatibility.rb +0 -53
  213. data/test/support/integration.rb +0 -94
  214. data/test/support/locale/en.yml +0 -8
  215. data/test/support/mongoid.yml +0 -6
  216. data/test/support/webrat/integrations/rails.rb +0 -35
  217. data/test/test/controller_helpers_test.rb +0 -188
  218. data/test/test/integration_helpers_test.rb +0 -34
  219. data/test/test_helper.rb +0 -36
  220. data/test/test_models.rb +0 -35
@@ -1,12 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- # Since webrat uses ActionController::RecordIdentifier class that was moved to
4
- # ActionView namespace in Rails 4.1+
5
-
6
- unless defined?(ActionController::RecordIdentifier)
7
- require 'action_view/record_identifier'
8
-
9
- module ActionController
10
- RecordIdentifier = ActionView::RecordIdentifier
11
- end
12
- end
@@ -1,30 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'active_support/test_case'
4
-
5
- class ActiveSupport::TestCase
6
- def assert_blank(assertion)
7
- assert assertion.blank?
8
- end
9
-
10
- def assert_present(assertion)
11
- assert assertion.present?
12
- end
13
-
14
- def assert_email_sent(address = nil, &block)
15
- assert_difference('ActionMailer::Base.deliveries.size', &block)
16
- if address.present?
17
- assert_equal address, ActionMailer::Base.deliveries.last['to'].to_s
18
- end
19
- end
20
-
21
- def assert_email_not_sent(&block)
22
- assert_no_difference('ActionMailer::Base.deliveries.size', &block)
23
- end
24
-
25
- def assert_raise_with_message(exception_klass, message, &block)
26
- exception = assert_raise exception_klass, &block
27
- assert_equal exception.message, message,
28
- "The expected message was #{message} but your exception throwed #{exception.message}"
29
- end
30
- end
@@ -1,83 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'active_support/test_case'
4
-
5
- class ActiveSupport::TestCase
6
- VALID_AUTHENTICATION_TOKEN = 'AbCdEfGhIjKlMnOpQrSt'.freeze
7
-
8
- def setup_mailer
9
- ActionMailer::Base.deliveries = []
10
- end
11
-
12
- def store_translations(locale, translations, &block)
13
- # Calling 'available_locales' before storing the translations to ensure
14
- # that the I18n backend will be initialized before we store our custom
15
- # translations, so they will always override the translations for the
16
- # YML file.
17
- I18n.available_locales
18
- I18n.backend.store_translations(locale, translations)
19
- yield
20
- ensure
21
- I18n.reload!
22
- end
23
-
24
- def generate_unique_email
25
- @@email_count ||= 0
26
- @@email_count += 1
27
- "test#{@@email_count}@example.com"
28
- end
29
-
30
- def valid_attributes(attributes={})
31
- { username: "usertest",
32
- email: generate_unique_email,
33
- password: '12345678',
34
- password_confirmation: '12345678' }.update(attributes)
35
- end
36
-
37
- def new_user(attributes={})
38
- User.new(valid_attributes(attributes))
39
- end
40
-
41
- def create_user(attributes={})
42
- User.create!(valid_attributes(attributes))
43
- end
44
-
45
- def create_admin(attributes={})
46
- valid_attributes = valid_attributes(attributes)
47
- valid_attributes.delete(:username)
48
- Admin.create!(valid_attributes)
49
- end
50
-
51
- def create_user_without_email(attributes={})
52
- UserWithoutEmail.create!(valid_attributes(attributes))
53
- end
54
-
55
- def create_user_with_validations(attributes={})
56
- UserWithValidations.create!(valid_attributes(attributes))
57
- end
58
-
59
- # Execute the block setting the given values and restoring old values after
60
- # the block is executed.
61
- def swap(object, new_values)
62
- old_values = {}
63
- new_values.each do |key, value|
64
- old_values[key] = object.send key
65
- object.send :"#{key}=", value
66
- end
67
- clear_cached_variables(new_values)
68
- yield
69
- ensure
70
- clear_cached_variables(new_values)
71
- old_values.each do |key, value|
72
- object.send :"#{key}=", value
73
- end
74
- end
75
-
76
- def clear_cached_variables(options)
77
- if options.key?(:case_insensitive_keys) || options.key?(:strip_whitespace_keys)
78
- Devise.mappings.each do |_, mapping|
79
- mapping.to.instance_variable_set(:@devise_parameter_filter, nil)
80
- end
81
- end
82
- end
83
- end
@@ -1,53 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Devise
4
- class IntegrationTest < ActionDispatch::IntegrationTest
5
- # %w( get post patch put head delete xml_http_request
6
- # xhr get_via_redirect post_via_redirect
7
- # ).each do |method|
8
- %w( get post put ).each do |method|
9
- if Rails.version >= '5.0.0'
10
- define_method(method) do |url, options={}|
11
- if options.empty?
12
- super url
13
- else
14
- super url, options
15
- end
16
- end
17
- else
18
- define_method(method) do |url, options={}|
19
- if options[:xhr]==true
20
- xml_http_request __method__, url, options[:params] || {}, options[:headers]
21
- else
22
- super url, options[:params] || {}, options[:headers]
23
- end
24
- end
25
- end
26
- end
27
- end
28
-
29
- class ControllerTestCase < ActionController::TestCase
30
- # %w( get post patch put head delete xml_http_request
31
- # xhr get_via_redirect post_via_redirect
32
- # ).each do |method|
33
- %w( get post put ).each do |method|
34
- if Rails.version >= '5.0.0'
35
- define_method(method) do |action, options={}|
36
- if options.empty?
37
- super action
38
- else
39
- super action, options
40
- end
41
- end
42
- else
43
- define_method(method) do |action, options={}|
44
- if options[:xhr]==true
45
- xml_http_request __method__, action, options[:params] || {}, options[:headers]
46
- else
47
- super action, options[:params] || {}, options[:headers]
48
- end
49
- end
50
- end
51
- end
52
- end
53
- end
@@ -1,94 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'action_dispatch/testing/integration'
4
-
5
- class ActionDispatch::IntegrationTest
6
- def warden
7
- request.env['warden']
8
- end
9
-
10
- def create_user(options={})
11
- @user ||= begin
12
- user = User.create!(
13
- username: 'usertest',
14
- email: options[:email] || 'user@test.com',
15
- password: options[:password] || '12345678',
16
- password_confirmation: options[:password] || '12345678',
17
- created_at: Time.now.utc
18
- )
19
- user.update_attribute(:confirmation_sent_at, options[:confirmation_sent_at]) if options[:confirmation_sent_at]
20
- user.confirm unless options[:confirm] == false
21
- user.lock_access! if options[:locked] == true
22
- user
23
- end
24
- end
25
-
26
- def create_admin(options={})
27
- @admin ||= begin
28
- admin = Admin.create!(
29
- email: options[:email] || 'admin@test.com',
30
- password: '123456', password_confirmation: '123456',
31
- active: options[:active]
32
- )
33
- admin.confirm unless options[:confirm] == false
34
- admin
35
- end
36
- end
37
-
38
- def sign_in_as_user(options={}, &block)
39
- user = create_user(options)
40
- visit_with_option options[:visit], new_user_session_path
41
- fill_in 'email', with: options[:email] || 'user@test.com'
42
- fill_in 'password', with: options[:password] || '12345678'
43
- check 'remember me' if options[:remember_me] == true
44
- yield if block_given?
45
- click_button 'Log In'
46
- user
47
- end
48
-
49
- def sign_in_as_admin(options={}, &block)
50
- admin = create_admin(options)
51
- visit_with_option options[:visit], new_admin_session_path
52
- fill_in 'email', with: 'admin@test.com'
53
- fill_in 'password', with: '123456'
54
- yield if block_given?
55
- click_button 'Log In'
56
- admin
57
- end
58
-
59
- # Fix assert_redirect_to in integration sessions because they don't take into
60
- # account Middleware redirects.
61
- #
62
- def assert_redirected_to(url)
63
- assert [301, 302].include?(@integration_session.status),
64
- "Expected status to be 301 or 302, got #{@integration_session.status}"
65
-
66
- assert_url url, @integration_session.headers["Location"]
67
- end
68
-
69
- def assert_current_url(expected)
70
- assert_url expected, current_url
71
- end
72
-
73
- def assert_url(expected, actual)
74
- assert_equal prepend_host(expected), prepend_host(actual)
75
- end
76
-
77
- protected
78
-
79
- def visit_with_option(given, default)
80
- case given
81
- when String
82
- visit given
83
- when FalseClass
84
- # Do nothing
85
- else
86
- visit default
87
- end
88
- end
89
-
90
- def prepend_host(url)
91
- url = "http://#{request.host}#{url}" if url[0] == ?/
92
- url
93
- end
94
- end
@@ -1,8 +0,0 @@
1
- en:
2
- devise:
3
- failure:
4
- user:
5
- does_not_exist: "User %{name} does not exist"
6
- errors:
7
- messages:
8
- taken: "has already been taken"
@@ -1,6 +0,0 @@
1
- test:
2
- <%= Mongoid::VERSION.to_i > 4 ? 'clients' : 'sessions' %>:
3
- default:
4
- database: devise-test-suite
5
- hosts:
6
- - localhost:<%= ENV['MONGODB_PORT'] || '27017' %>
@@ -1,35 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'webrat/core/elements/form'
4
- require 'action_dispatch/testing/integration'
5
-
6
- module Webrat
7
- Form.class_eval do
8
- def self.parse_rails_request_params(params)
9
- Rack::Utils.parse_nested_query(params)
10
- end
11
- end
12
-
13
- module Logging
14
- # Avoid RAILS_DEFAULT_LOGGER deprecation warning
15
- def logger # :nodoc:
16
- ::Rails.logger
17
- end
18
- end
19
-
20
- class RailsAdapter
21
- protected
22
-
23
- def do_request(http_method, url, data, headers)
24
- update_protocol(url)
25
- integration_session.send(http_method, normalize_url(url), params: data, headers: headers)
26
- end
27
- end
28
- end
29
-
30
- module ActionDispatch #:nodoc:
31
- IntegrationTest.class_eval do
32
- include Webrat::Methods
33
- include Webrat::Matchers
34
- end
35
- end
@@ -1,188 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'test_helper'
4
-
5
- class TestControllerHelpersTest < Devise::ControllerTestCase
6
- tests UsersController
7
- include Devise::Test::ControllerHelpers
8
-
9
- test "redirects if attempting to access a page unauthenticated" do
10
- get :index
11
- assert_redirected_to new_user_session_path
12
- assert_equal "You need to sign in or sign up before continuing.", flash[:alert]
13
- end
14
-
15
- test "redirects if attempting to access a page with an unconfirmed account" do
16
- swap Devise, allow_unconfirmed_access_for: 0.days do
17
- user = create_user
18
- assert !user.active_for_authentication?
19
-
20
- sign_in user
21
- get :index
22
- assert_redirected_to new_user_session_path
23
- end
24
- end
25
-
26
- test "returns nil if accessing current_user with an unconfirmed account" do
27
- swap Devise, allow_unconfirmed_access_for: 0.days do
28
- user = create_user
29
- assert !user.active_for_authentication?
30
-
31
- sign_in user
32
- get :accept, params: { id: user }
33
- assert_nil assigns(:current_user)
34
- end
35
- end
36
-
37
- test "does not redirect with valid user" do
38
- user = create_user
39
- user.confirm
40
-
41
- sign_in user
42
- get :index
43
- assert_response :success
44
- end
45
-
46
- test "does not redirect with valid user after failed first attempt" do
47
- get :index
48
- assert_response :redirect
49
-
50
- user = create_user
51
- user.confirm
52
-
53
- sign_in user
54
- get :index
55
- assert_response :success
56
- end
57
-
58
- test "redirects if valid user signed out" do
59
- user = create_user
60
- user.confirm
61
-
62
- sign_in user
63
- get :index
64
-
65
- sign_out user
66
- get :index
67
- assert_redirected_to new_user_session_path
68
- end
69
-
70
- test "respects custom failure app" do
71
- custom_failure_app = Class.new(Devise::FailureApp) do
72
- def redirect
73
- self.status = 300
74
- end
75
- end
76
-
77
- swap Devise.warden_config, failure_app: custom_failure_app do
78
- get :index
79
- assert_response 300
80
- end
81
- end
82
-
83
- test "passes given headers from the failure app to the response" do
84
- custom_failure_app = Class.new(Devise::FailureApp) do
85
- def respond
86
- self.status = 401
87
- self.response.headers["CUSTOMHEADER"] = 1
88
- end
89
- end
90
-
91
- swap Devise.warden_config, failure_app: custom_failure_app do
92
- sign_in create_user
93
- get :index
94
- assert_equal 1, @response.headers["CUSTOMHEADER"]
95
- end
96
- end
97
-
98
- test "returns the body of a failure app" do
99
- get :index
100
- assert_equal response.body, "<html><body>You are being <a href=\"http://test.host/users/sign_in\">redirected</a>.</body></html>"
101
- end
102
-
103
- test "defined Warden after_authentication callback should not be called when sign_in is called" do
104
- begin
105
- Warden::Manager.after_authentication do |user, auth, opts|
106
- flunk "callback was called while it should not"
107
- end
108
-
109
- user = create_user
110
- user.confirm
111
- sign_in user
112
- ensure
113
- Warden::Manager._after_set_user.pop
114
- end
115
- end
116
-
117
- test "defined Warden before_logout callback should not be called when sign_out is called" do
118
- begin
119
- Warden::Manager.before_logout do |user, auth, opts|
120
- flunk "callback was called while it should not"
121
- end
122
- user = create_user
123
- user.confirm
124
-
125
- sign_in user
126
- sign_out user
127
- ensure
128
- Warden::Manager._before_logout.pop
129
- end
130
- end
131
-
132
- test "before_failure call should work" do
133
- begin
134
- executed = false
135
- Warden::Manager.before_failure do |env,opts|
136
- executed = true
137
- end
138
-
139
- user = create_user
140
- sign_in user
141
-
142
- get :index
143
- assert executed
144
- ensure
145
- Warden::Manager._before_failure.pop
146
- end
147
- end
148
-
149
- test "allows to sign in with different users" do
150
- first_user = create_user
151
- first_user.confirm
152
-
153
- sign_in first_user
154
- get :index
155
- assert_match /User ##{first_user.id}/, @response.body
156
- sign_out first_user
157
-
158
- second_user = create_user
159
- second_user.confirm
160
-
161
- sign_in second_user
162
- get :index
163
- assert_match /User ##{second_user.id}/, @response.body
164
- end
165
-
166
- test "creates a new warden proxy if the request object has changed" do
167
- old_warden_proxy = warden
168
-
169
- @request = if Devise::Test.rails51?
170
- ActionController::TestRequest.create(Class.new) # needs a "controller class"
171
- elsif Devise::Test.rails5?
172
- ActionController::TestRequest.create
173
- else
174
- ActionController::TestRequest.new
175
- end
176
-
177
- new_warden_proxy = warden
178
-
179
- assert_not_equal old_warden_proxy, new_warden_proxy
180
- end
181
-
182
- test "doesn't create a new warden proxy if the request object hasn't changed" do
183
- old_warden_proxy = warden
184
- new_warden_proxy = warden
185
-
186
- assert_equal old_warden_proxy, new_warden_proxy
187
- end
188
- end