devise 4.4.3 → 4.7.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (222) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +98 -4
  3. data/MIT-LICENSE +2 -1
  4. data/README.md +96 -59
  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 +4 -4
  21. data/lib/devise/controllers/helpers.rb +8 -8
  22. data/lib/devise/controllers/sign_in_out.rb +6 -3
  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 +18 -3
  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 +9 -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/routes.rb +6 -6
  39. data/lib/devise/secret_key_finder.rb +2 -0
  40. data/lib/devise/strategies/authenticatable.rb +1 -1
  41. data/lib/devise/strategies/database_authenticatable.rb +6 -1
  42. data/lib/devise/test/controller_helpers.rb +4 -2
  43. data/lib/devise/test/integration_helpers.rb +1 -1
  44. data/lib/devise/version.rb +1 -1
  45. data/lib/generators/active_record/devise_generator.rb +9 -9
  46. data/lib/generators/devise/controllers_generator.rb +1 -1
  47. data/lib/generators/devise/devise_generator.rb +1 -1
  48. data/lib/generators/devise/install_generator.rb +1 -5
  49. data/lib/generators/devise/orm_helpers.rb +2 -2
  50. data/lib/generators/devise/views_generator.rb +1 -1
  51. data/lib/generators/mongoid/devise_generator.rb +5 -5
  52. data/lib/generators/templates/README +9 -1
  53. data/lib/generators/templates/controllers/omniauth_callbacks_controller.rb +1 -1
  54. data/lib/generators/templates/devise.rb +34 -6
  55. data/lib/generators/templates/simple_form_for/confirmations/new.html.erb +5 -1
  56. data/lib/generators/templates/simple_form_for/passwords/edit.html.erb +10 -2
  57. data/lib/generators/templates/simple_form_for/passwords/new.html.erb +4 -1
  58. data/lib/generators/templates/simple_form_for/registrations/edit.html.erb +11 -3
  59. data/lib/generators/templates/simple_form_for/registrations/new.html.erb +11 -3
  60. data/lib/generators/templates/simple_form_for/sessions/new.html.erb +7 -2
  61. data/lib/generators/templates/simple_form_for/unlocks/new.html.erb +4 -1
  62. metadata +7 -312
  63. data/.gitignore +0 -10
  64. data/.travis.yml +0 -68
  65. data/.yardopts +0 -9
  66. data/CODE_OF_CONDUCT.md +0 -22
  67. data/CONTRIBUTING.md +0 -79
  68. data/Gemfile +0 -39
  69. data/Gemfile.lock +0 -193
  70. data/ISSUE_TEMPLATE.md +0 -19
  71. data/Rakefile +0 -37
  72. data/bin/test +0 -13
  73. data/devise.gemspec +0 -28
  74. data/devise.png +0 -0
  75. data/gemfiles/Gemfile.rails-4.1-stable +0 -32
  76. data/gemfiles/Gemfile.rails-4.1-stable.lock +0 -171
  77. data/gemfiles/Gemfile.rails-4.2-stable +0 -32
  78. data/gemfiles/Gemfile.rails-4.2-stable.lock +0 -192
  79. data/gemfiles/Gemfile.rails-5.0-stable +0 -33
  80. data/gemfiles/Gemfile.rails-5.0-stable.lock +0 -192
  81. data/gemfiles/Gemfile.rails-5.2-rc1 +0 -26
  82. data/gemfiles/Gemfile.rails-5.2-rc1.lock +0 -201
  83. data/guides/bug_report_templates/integration_test.rb +0 -106
  84. data/test/controllers/custom_registrations_controller_test.rb +0 -42
  85. data/test/controllers/custom_strategy_test.rb +0 -66
  86. data/test/controllers/helper_methods_test.rb +0 -24
  87. data/test/controllers/helpers_test.rb +0 -318
  88. data/test/controllers/inherited_controller_i18n_messages_test.rb +0 -53
  89. data/test/controllers/internal_helpers_test.rb +0 -129
  90. data/test/controllers/load_hooks_controller_test.rb +0 -21
  91. data/test/controllers/passwords_controller_test.rb +0 -34
  92. data/test/controllers/sessions_controller_test.rb +0 -108
  93. data/test/controllers/url_helpers_test.rb +0 -67
  94. data/test/delegator_test.rb +0 -21
  95. data/test/devise_test.rb +0 -109
  96. data/test/failure_app_test.rb +0 -340
  97. data/test/generators/active_record_generator_test.rb +0 -130
  98. data/test/generators/controllers_generator_test.rb +0 -50
  99. data/test/generators/devise_generator_test.rb +0 -41
  100. data/test/generators/install_generator_test.rb +0 -26
  101. data/test/generators/mongoid_generator_test.rb +0 -25
  102. data/test/generators/views_generator_test.rb +0 -105
  103. data/test/helpers/devise_helper_test.rb +0 -51
  104. data/test/integration/authenticatable_test.rb +0 -706
  105. data/test/integration/confirmable_test.rb +0 -326
  106. data/test/integration/database_authenticatable_test.rb +0 -97
  107. data/test/integration/http_authenticatable_test.rb +0 -114
  108. data/test/integration/lockable_test.rb +0 -242
  109. data/test/integration/mounted_engine_test.rb +0 -38
  110. data/test/integration/omniauthable_test.rb +0 -148
  111. data/test/integration/recoverable_test.rb +0 -349
  112. data/test/integration/registerable_test.rb +0 -365
  113. data/test/integration/rememberable_test.rb +0 -219
  114. data/test/integration/timeoutable_test.rb +0 -186
  115. data/test/integration/trackable_test.rb +0 -99
  116. data/test/mailers/confirmation_instructions_test.rb +0 -117
  117. data/test/mailers/email_changed_test.rb +0 -132
  118. data/test/mailers/mailer_test.rb +0 -20
  119. data/test/mailers/reset_password_instructions_test.rb +0 -98
  120. data/test/mailers/unlock_instructions_test.rb +0 -93
  121. data/test/mapping_test.rb +0 -136
  122. data/test/models/authenticatable_test.rb +0 -25
  123. data/test/models/confirmable_test.rb +0 -549
  124. data/test/models/database_authenticatable_test.rb +0 -283
  125. data/test/models/lockable_test.rb +0 -352
  126. data/test/models/omniauthable_test.rb +0 -9
  127. data/test/models/recoverable_test.rb +0 -263
  128. data/test/models/registerable_test.rb +0 -9
  129. data/test/models/rememberable_test.rb +0 -184
  130. data/test/models/serializable_test.rb +0 -60
  131. data/test/models/timeoutable_test.rb +0 -53
  132. data/test/models/trackable_test.rb +0 -62
  133. data/test/models/validatable_test.rb +0 -121
  134. data/test/models_test.rb +0 -155
  135. data/test/omniauth/config_test.rb +0 -61
  136. data/test/omniauth/url_helpers_test.rb +0 -53
  137. data/test/orm/active_record.rb +0 -24
  138. data/test/orm/mongoid.rb +0 -15
  139. data/test/parameter_sanitizer_test.rb +0 -77
  140. data/test/rails_app/Rakefile +0 -6
  141. data/test/rails_app/app/active_record/admin.rb +0 -8
  142. data/test/rails_app/app/active_record/shim.rb +0 -4
  143. data/test/rails_app/app/active_record/user.rb +0 -20
  144. data/test/rails_app/app/active_record/user_on_engine.rb +0 -9
  145. data/test/rails_app/app/active_record/user_on_main_app.rb +0 -9
  146. data/test/rails_app/app/active_record/user_with_validations.rb +0 -12
  147. data/test/rails_app/app/active_record/user_without_email.rb +0 -10
  148. data/test/rails_app/app/controllers/admins/sessions_controller.rb +0 -8
  149. data/test/rails_app/app/controllers/admins_controller.rb +0 -8
  150. data/test/rails_app/app/controllers/application_controller.rb +0 -13
  151. data/test/rails_app/app/controllers/application_with_fake_engine.rb +0 -32
  152. data/test/rails_app/app/controllers/custom/registrations_controller.rb +0 -33
  153. data/test/rails_app/app/controllers/home_controller.rb +0 -31
  154. data/test/rails_app/app/controllers/publisher/registrations_controller.rb +0 -4
  155. data/test/rails_app/app/controllers/publisher/sessions_controller.rb +0 -4
  156. data/test/rails_app/app/controllers/users/omniauth_callbacks_controller.rb +0 -16
  157. data/test/rails_app/app/controllers/users_controller.rb +0 -33
  158. data/test/rails_app/app/helpers/application_helper.rb +0 -5
  159. data/test/rails_app/app/mailers/users/from_proc_mailer.rb +0 -5
  160. data/test/rails_app/app/mailers/users/mailer.rb +0 -5
  161. data/test/rails_app/app/mailers/users/reply_to_mailer.rb +0 -6
  162. data/test/rails_app/app/mongoid/admin.rb +0 -31
  163. data/test/rails_app/app/mongoid/shim.rb +0 -25
  164. data/test/rails_app/app/mongoid/user.rb +0 -50
  165. data/test/rails_app/app/mongoid/user_on_engine.rb +0 -41
  166. data/test/rails_app/app/mongoid/user_on_main_app.rb +0 -41
  167. data/test/rails_app/app/mongoid/user_with_validations.rb +0 -37
  168. data/test/rails_app/app/mongoid/user_without_email.rb +0 -35
  169. data/test/rails_app/app/views/admins/index.html.erb +0 -1
  170. data/test/rails_app/app/views/admins/sessions/new.html.erb +0 -2
  171. data/test/rails_app/app/views/home/admin_dashboard.html.erb +0 -1
  172. data/test/rails_app/app/views/home/index.html.erb +0 -1
  173. data/test/rails_app/app/views/home/join.html.erb +0 -1
  174. data/test/rails_app/app/views/home/private.html.erb +0 -1
  175. data/test/rails_app/app/views/home/user_dashboard.html.erb +0 -1
  176. data/test/rails_app/app/views/layouts/application.html.erb +0 -24
  177. data/test/rails_app/app/views/users/edit_form.html.erb +0 -1
  178. data/test/rails_app/app/views/users/index.html.erb +0 -1
  179. data/test/rails_app/app/views/users/mailer/confirmation_instructions.erb +0 -1
  180. data/test/rails_app/app/views/users/sessions/new.html.erb +0 -1
  181. data/test/rails_app/bin/bundle +0 -3
  182. data/test/rails_app/bin/rails +0 -4
  183. data/test/rails_app/bin/rake +0 -4
  184. data/test/rails_app/config.ru +0 -4
  185. data/test/rails_app/config/application.rb +0 -48
  186. data/test/rails_app/config/boot.rb +0 -27
  187. data/test/rails_app/config/database.yml +0 -18
  188. data/test/rails_app/config/environment.rb +0 -7
  189. data/test/rails_app/config/environments/development.rb +0 -32
  190. data/test/rails_app/config/environments/production.rb +0 -88
  191. data/test/rails_app/config/environments/test.rb +0 -47
  192. data/test/rails_app/config/initializers/backtrace_silencers.rb +0 -9
  193. data/test/rails_app/config/initializers/devise.rb +0 -182
  194. data/test/rails_app/config/initializers/inflections.rb +0 -4
  195. data/test/rails_app/config/initializers/secret_token.rb +0 -5
  196. data/test/rails_app/config/initializers/session_store.rb +0 -3
  197. data/test/rails_app/config/routes.rb +0 -128
  198. data/test/rails_app/db/migrate/20100401102949_create_tables.rb +0 -77
  199. data/test/rails_app/db/schema.rb +0 -57
  200. data/test/rails_app/lib/shared_admin.rb +0 -23
  201. data/test/rails_app/lib/shared_user.rb +0 -32
  202. data/test/rails_app/lib/shared_user_without_email.rb +0 -28
  203. data/test/rails_app/lib/shared_user_without_omniauth.rb +0 -15
  204. data/test/rails_app/public/404.html +0 -26
  205. data/test/rails_app/public/422.html +0 -26
  206. data/test/rails_app/public/500.html +0 -26
  207. data/test/rails_app/public/favicon.ico +0 -0
  208. data/test/rails_test.rb +0 -11
  209. data/test/routes_test.rb +0 -281
  210. data/test/secret_key_finder_test.rb +0 -97
  211. data/test/support/action_controller/record_identifier.rb +0 -12
  212. data/test/support/assertions.rb +0 -30
  213. data/test/support/helpers.rb +0 -83
  214. data/test/support/http_method_compatibility.rb +0 -53
  215. data/test/support/integration.rb +0 -95
  216. data/test/support/locale/en.yml +0 -8
  217. data/test/support/mongoid.yml +0 -6
  218. data/test/support/webrat/integrations/rails.rb +0 -35
  219. data/test/test/controller_helpers_test.rb +0 -193
  220. data/test/test/integration_helpers_test.rb +0 -34
  221. data/test/test_helper.rb +0 -36
  222. data/test/test_models.rb +0 -35
@@ -1,340 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'test_helper'
4
- require 'ostruct'
5
-
6
- class FailureTest < ActiveSupport::TestCase
7
- class RootFailureApp < Devise::FailureApp
8
- def fake_app
9
- Object.new
10
- end
11
- end
12
-
13
- class FailureWithSubdomain < RootFailureApp
14
- routes = ActionDispatch::Routing::RouteSet.new
15
-
16
- routes.draw do
17
- scope subdomain: 'sub' do
18
- root to: 'foo#bar'
19
- end
20
- end
21
-
22
- include routes.url_helpers
23
- end
24
-
25
- class FailureWithI18nOptions < Devise::FailureApp
26
- def i18n_options(options)
27
- options.merge(name: 'Steve')
28
- end
29
- end
30
-
31
- class FakeEngineApp < Devise::FailureApp
32
- class FakeEngine
33
- def new_user_on_engine_session_url _
34
- '/user_on_engines/sign_in'
35
- end
36
- end
37
-
38
- def main_app
39
- raise 'main_app router called instead of fake_engine'
40
- end
41
-
42
- def fake_engine
43
- @fake_engine ||= FakeEngine.new
44
- end
45
- end
46
-
47
- def self.context(name, &block)
48
- instance_eval(&block)
49
- end
50
-
51
- def call_failure(env_params={})
52
- env = {
53
- 'REQUEST_URI' => 'http://test.host/',
54
- 'HTTP_HOST' => 'test.host',
55
- 'REQUEST_METHOD' => 'GET',
56
- 'warden.options' => { scope: :user },
57
- 'rack.session' => {},
58
- 'action_dispatch.request.formats' => Array(env_params.delete('formats') || Mime[:html]),
59
- 'rack.input' => "",
60
- 'warden' => OpenStruct.new(message: nil)
61
- }.merge!(env_params)
62
-
63
- # Passing nil for action_dispatch.request.formats prevents the default from being used in Rails 5, need to remove it
64
- if env.has_key?('action_dispatch.request.formats') && env['action_dispatch.request.formats'].nil?
65
- env.delete 'action_dispatch.request.formats' unless env['action_dispatch.request.formats']
66
- end
67
-
68
- @response = (env.delete(:app) || Devise::FailureApp).call(env).to_a
69
- @request = ActionDispatch::Request.new(env)
70
- end
71
-
72
- context 'When redirecting' do
73
- test 'returns to the default redirect location' do
74
- call_failure
75
- assert_equal 302, @response.first
76
- assert_equal 'You need to sign in or sign up before continuing.', @request.flash[:alert]
77
- assert_equal 'http://test.host/users/sign_in', @response.second['Location']
78
- end
79
-
80
- test 'returns to the default redirect location considering subdomain' do
81
- call_failure('warden.options' => { scope: :subdomain_user })
82
- assert_equal 302, @response.first
83
- assert_equal 'You need to sign in or sign up before continuing.', @request.flash[:alert]
84
- assert_equal 'http://sub.test.host/subdomain_users/sign_in', @response.second['Location']
85
- end
86
-
87
- test 'returns to the default redirect location for wildcard requests' do
88
- call_failure 'action_dispatch.request.formats' => nil, 'HTTP_ACCEPT' => '*/*'
89
- assert_equal 302, @response.first
90
- assert_equal 'http://test.host/users/sign_in', @response.second['Location']
91
- end
92
-
93
- test 'returns to the root path if no session path is available' do
94
- swap Devise, router_name: :fake_app do
95
- call_failure app: RootFailureApp
96
- assert_equal 302, @response.first
97
- assert_equal 'You need to sign in or sign up before continuing.', @request.flash[:alert]
98
- assert_equal 'http://test.host/', @response.second['Location']
99
- end
100
- end
101
-
102
- test 'returns to the root path considering subdomain if no session path is available' do
103
- swap Devise, router_name: :fake_app do
104
- call_failure app: FailureWithSubdomain
105
- assert_equal 302, @response.first
106
- assert_equal 'You need to sign in or sign up before continuing.', @request.flash[:alert]
107
- assert_equal 'http://sub.test.host/', @response.second['Location']
108
- end
109
- end
110
-
111
- test 'returns to the default redirect location considering the router for supplied scope' do
112
- call_failure app: FakeEngineApp, 'warden.options' => { scope: :user_on_engine }
113
- assert_equal 302, @response.first
114
- assert_equal 'You need to sign in or sign up before continuing.', @request.flash[:alert]
115
- assert_equal 'http://test.host/user_on_engines/sign_in', @response.second['Location']
116
- end
117
-
118
- if Rails.application.config.respond_to?(:relative_url_root)
119
- test 'returns to the default redirect location considering the relative url root' do
120
- swap Rails.application.config, relative_url_root: "/sample" do
121
- call_failure
122
- assert_equal 302, @response.first
123
- assert_equal 'http://test.host/sample/users/sign_in', @response.second['Location']
124
- end
125
- end
126
-
127
- test 'returns to the default redirect location considering the relative url root and subdomain' do
128
- swap Rails.application.config, relative_url_root: "/sample" do
129
- call_failure('warden.options' => { scope: :subdomain_user })
130
- assert_equal 302, @response.first
131
- assert_equal 'http://sub.test.host/sample/subdomain_users/sign_in', @response.second['Location']
132
- end
133
- end
134
- end
135
-
136
- if Rails.application.config.action_controller.respond_to?(:relative_url_root)
137
- test "returns to the default redirect location considering action_controller's relative url root" do
138
- swap Rails.application.config.action_controller, relative_url_root: "/sample" do
139
- call_failure
140
- assert_equal 302, @response.first
141
- assert_equal 'http://test.host/sample/users/sign_in', @response.second['Location']
142
- end
143
- end
144
-
145
- test "returns to the default redirect location considering action_controller's relative url root and subdomain" do
146
- swap Rails.application.config.action_controller, relative_url_root: "/sample" do
147
- call_failure('warden.options' => { scope: :subdomain_user })
148
- assert_equal 302, @response.first
149
- assert_equal 'http://sub.test.host/sample/subdomain_users/sign_in', @response.second['Location']
150
- end
151
- end
152
- end
153
-
154
- test 'uses the proxy failure message as symbol' do
155
- call_failure('warden' => OpenStruct.new(message: :invalid))
156
- assert_equal 'Invalid Email or password.', @request.flash[:alert]
157
- assert_equal 'http://test.host/users/sign_in', @response.second["Location"]
158
- end
159
-
160
- test 'supports authentication_keys as a Hash for the flash message' do
161
- swap Devise, authentication_keys: { email: true, login: true } do
162
- call_failure('warden' => OpenStruct.new(message: :invalid))
163
- assert_equal 'Invalid Email, Login or password.', @request.flash[:alert]
164
- end
165
- end
166
-
167
- test 'uses custom i18n options' do
168
- call_failure('warden' => OpenStruct.new(message: :does_not_exist), app: FailureWithI18nOptions)
169
- assert_equal 'User Steve does not exist', @request.flash[:alert]
170
- end
171
-
172
- test 'uses the proxy failure message as string' do
173
- call_failure('warden' => OpenStruct.new(message: 'Hello world'))
174
- assert_equal 'Hello world', @request.flash[:alert]
175
- assert_equal 'http://test.host/users/sign_in', @response.second["Location"]
176
- end
177
-
178
- test 'set content type to default text/html' do
179
- call_failure
180
- assert_equal 'text/html; charset=utf-8', @response.second['Content-Type']
181
- end
182
-
183
- test 'set up a default message' do
184
- call_failure
185
- assert_match(/You are being/, @response.last.body)
186
- assert_match(/redirected/, @response.last.body)
187
- assert_match(/users\/sign_in/, @response.last.body)
188
- end
189
-
190
- test 'works for any navigational format' do
191
- swap Devise, navigational_formats: [:xml] do
192
- call_failure('formats' => Mime[:xml])
193
- assert_equal 302, @response.first
194
- end
195
- end
196
-
197
- test 'redirects the correct format if it is a non-html format request' do
198
- swap Devise, navigational_formats: [:js] do
199
- call_failure('formats' => Mime[:js])
200
- assert_equal 'http://test.host/users/sign_in.js', @response.second["Location"]
201
- end
202
- end
203
- end
204
-
205
- context 'For HTTP request' do
206
- test 'return 401 status' do
207
- call_failure('formats' => Mime[:xml])
208
- assert_equal 401, @response.first
209
- end
210
-
211
- test 'return appropriate body for xml' do
212
- call_failure('formats' => Mime[:xml])
213
- result = %(<?xml version="1.0" encoding="UTF-8"?>\n<errors>\n <error>You need to sign in or sign up before continuing.</error>\n</errors>\n)
214
- assert_equal result, @response.last.body
215
- end
216
-
217
- test 'return appropriate body for json' do
218
- call_failure('formats' => Mime[:json])
219
- result = %({"error":"You need to sign in or sign up before continuing."})
220
- assert_equal result, @response.last.body
221
- end
222
-
223
- test 'return 401 status for unknown formats' do
224
- call_failure 'formats' => []
225
- assert_equal 401, @response.first
226
- end
227
-
228
- test 'return WWW-authenticate headers if model allows' do
229
- call_failure('formats' => Mime[:xml])
230
- assert_equal 'Basic realm="Application"', @response.second["WWW-Authenticate"]
231
- end
232
-
233
- test 'does not return WWW-authenticate headers if model does not allow' do
234
- swap Devise, http_authenticatable: false do
235
- call_failure('formats' => Mime[:xml])
236
- assert_nil @response.second["WWW-Authenticate"]
237
- end
238
- end
239
-
240
- test 'works for any non navigational format' do
241
- swap Devise, navigational_formats: [] do
242
- call_failure('formats' => Mime[:html])
243
- assert_equal 401, @response.first
244
- end
245
- end
246
-
247
- test 'uses the failure message as response body' do
248
- call_failure('formats' => Mime[:xml], 'warden' => OpenStruct.new(message: :invalid))
249
- assert_match '<error>Invalid Email or password.</error>', @response.third.body
250
- end
251
-
252
- context 'on ajax call' do
253
- context 'when http_authenticatable_on_xhr is false' do
254
- test 'dont return 401 with navigational formats' do
255
- swap Devise, http_authenticatable_on_xhr: false do
256
- call_failure('formats' => Mime[:html], 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest')
257
- assert_equal 302, @response.first
258
- assert_equal 'http://test.host/users/sign_in', @response.second["Location"]
259
- end
260
- end
261
-
262
- test 'dont return 401 with non navigational formats' do
263
- swap Devise, http_authenticatable_on_xhr: false do
264
- call_failure('formats' => Mime[:json], 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest')
265
- assert_equal 302, @response.first
266
- assert_equal 'http://test.host/users/sign_in.json', @response.second["Location"]
267
- end
268
- end
269
- end
270
-
271
- context 'when http_authenticatable_on_xhr is true' do
272
- test 'return 401' do
273
- swap Devise, http_authenticatable_on_xhr: true do
274
- call_failure('formats' => Mime[:html], 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest')
275
- assert_equal 401, @response.first
276
- end
277
- end
278
-
279
- test 'skip WWW-Authenticate header' do
280
- swap Devise, http_authenticatable_on_xhr: true do
281
- call_failure('formats' => Mime[:html], 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest')
282
- assert_nil @response.second['WWW-Authenticate']
283
- end
284
- end
285
- end
286
- end
287
- end
288
-
289
- context 'With recall' do
290
- test 'calls the original controller if invalid email or password' do
291
- env = {
292
- "warden.options" => { recall: "devise/sessions#new", attempted_path: "/users/sign_in" },
293
- "devise.mapping" => Devise.mappings[:user],
294
- "warden" => stub_everything
295
- }
296
- call_failure(env)
297
- assert @response.third.body.include?('<h2>Log in</h2>')
298
- assert @response.third.body.include?('Invalid Email or password.')
299
- end
300
-
301
- test 'calls the original controller if not confirmed email' do
302
- env = {
303
- "warden.options" => { recall: "devise/sessions#new", attempted_path: "/users/sign_in", message: :unconfirmed },
304
- "devise.mapping" => Devise.mappings[:user],
305
- "warden" => stub_everything
306
- }
307
- call_failure(env)
308
- assert @response.third.body.include?('<h2>Log in</h2>')
309
- assert @response.third.body.include?('You have to confirm your email address before continuing.')
310
- end
311
-
312
- test 'calls the original controller if inactive account' do
313
- env = {
314
- "warden.options" => { recall: "devise/sessions#new", attempted_path: "/users/sign_in", message: :inactive },
315
- "devise.mapping" => Devise.mappings[:user],
316
- "warden" => stub_everything
317
- }
318
- call_failure(env)
319
- assert @response.third.body.include?('<h2>Log in</h2>')
320
- assert @response.third.body.include?('Your account is not activated yet.')
321
- end
322
-
323
- if Rails.application.config.respond_to?(:relative_url_root)
324
- test 'calls the original controller with the proper environment considering the relative url root' do
325
- swap Rails.application.config, relative_url_root: "/sample" do
326
- env = {
327
- "warden.options" => { recall: "devise/sessions#new", attempted_path: "/sample/users/sign_in"},
328
- "devise.mapping" => Devise.mappings[:user],
329
- "warden" => stub_everything
330
- }
331
- call_failure(env)
332
- assert @response.third.body.include?('<h2>Log in</h2>')
333
- assert @response.third.body.include?('Invalid Email or password.')
334
- assert_equal @request.env["SCRIPT_NAME"], '/sample'
335
- assert_equal @request.env["PATH_INFO"], '/users/sign_in'
336
- end
337
- end
338
- end
339
- end
340
- end
@@ -1,130 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "test_helper"
4
-
5
- if DEVISE_ORM == :active_record
6
- require "generators/active_record/devise_generator"
7
-
8
- class ActiveRecordGeneratorTest < Rails::Generators::TestCase
9
- tests ActiveRecord::Generators::DeviseGenerator
10
- destination File.expand_path("../../tmp", __FILE__)
11
- setup :prepare_destination
12
-
13
- test "all files are properly created with rails31 migration syntax" do
14
- run_generator %w(monster)
15
- assert_migration "db/migrate/devise_create_monsters.rb", /def change/
16
- end
17
-
18
- test "all files are properly created with changed db/migrate path in application configuration" do
19
- old_paths = Rails.application.config.paths["db/migrate"]
20
- Rails.application.config.paths.add "db/migrate", with: "db2/migrate"
21
-
22
- run_generator %w(monster)
23
- if Rails.version >= '5.0.3'
24
- assert_migration "db2/migrate/devise_create_monsters.rb", /def change/
25
- else
26
- assert_migration "db/migrate/devise_create_monsters.rb", /def change/
27
- end
28
-
29
- Rails.application.config.paths["db/migrate"] = old_paths
30
- end
31
-
32
- test "all files for namespaced model are properly created" do
33
- run_generator %w(admin/monster)
34
- assert_migration "db/migrate/devise_create_admin_monsters.rb", /def change/
35
- end
36
-
37
- test "update model migration when model exists" do
38
- run_generator %w(monster)
39
- assert_file "app/models/monster.rb"
40
- run_generator %w(monster)
41
- assert_migration "db/migrate/add_devise_to_monsters.rb"
42
- end
43
-
44
- test "update model migration when model exists with changed db/migrate path in application configuration" do
45
- old_paths = Rails.application.config.paths["db/migrate"]
46
- Rails.application.config.paths.add "db/migrate", with: "db2/migrate"
47
-
48
- run_generator %w(monster)
49
- assert_file "app/models/monster.rb"
50
- run_generator %w(monster)
51
-
52
- if Rails.version >= '5.0.3'
53
- assert_migration "db2/migrate/add_devise_to_monsters.rb"
54
- else
55
- assert_migration "db/migrate/add_devise_to_monsters.rb"
56
- end
57
-
58
- Rails.application.config.paths["db/migrate"] = old_paths
59
- end
60
-
61
- test "all files are properly deleted" do
62
- run_generator %w(monster)
63
- run_generator %w(monster)
64
- assert_migration "db/migrate/devise_create_monsters.rb"
65
- assert_migration "db/migrate/add_devise_to_monsters.rb"
66
- run_generator %w(monster), behavior: :revoke
67
- assert_no_migration "db/migrate/add_devise_to_monsters.rb"
68
- assert_migration "db/migrate/devise_create_monsters.rb"
69
- run_generator %w(monster), behavior: :revoke
70
- assert_no_file "app/models/monster.rb"
71
- assert_no_migration "db/migrate/devise_create_monsters.rb"
72
- end
73
-
74
- test "use string column type for ip addresses" do
75
- run_generator %w(monster)
76
- assert_migration "db/migrate/devise_create_monsters.rb", /t.string :current_sign_in_ip/
77
- assert_migration "db/migrate/devise_create_monsters.rb", /t.string :last_sign_in_ip/
78
- end
79
-
80
- test "do NOT add primary key type when NOT specified in rails generator" do
81
- run_generator %w(monster)
82
- assert_migration "db/migrate/devise_create_monsters.rb", /create_table :monsters do/
83
- end
84
-
85
- test "add primary key type with rails 5 when specified in rails generator" do
86
- run_generator ["monster", "--primary_key_type=uuid"]
87
- if Rails.version.start_with? '5'
88
- assert_migration "db/migrate/devise_create_monsters.rb", /create_table :monsters, id: :uuid do/
89
- else
90
- assert_migration "db/migrate/devise_create_monsters.rb", /create_table :monsters do/
91
- end
92
- end
93
- end
94
-
95
- module RailsEngine
96
- class Engine < Rails::Engine
97
- isolate_namespace RailsEngine
98
- end
99
- end
100
-
101
- def simulate_inside_engine(engine, namespace)
102
- if Rails::Generators.respond_to?(:namespace=)
103
- swap Rails::Generators, namespace: namespace do
104
- yield
105
- end
106
- else
107
- swap Rails, application: engine.instance do
108
- yield
109
- end
110
- end
111
- end
112
-
113
- class ActiveRecordEngineGeneratorTest < Rails::Generators::TestCase
114
- tests ActiveRecord::Generators::DeviseGenerator
115
- destination File.expand_path("../../tmp", __FILE__)
116
- setup :prepare_destination
117
-
118
- test "all files are properly created in rails 4.0" do
119
- simulate_inside_engine(RailsEngine::Engine, RailsEngine) do
120
- run_generator ["monster"]
121
-
122
- assert_file "app/models/rails_engine/monster.rb", /devise/
123
- assert_file "app/models/rails_engine/monster.rb" do |content|
124
- assert_no_match %r{attr_accessible :email}, content
125
- end
126
- end
127
- end
128
-
129
- end
130
- end