devise 4.4.3 → 4.9.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (234) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +159 -3
  3. data/MIT-LICENSE +2 -1
  4. data/README.md +128 -64
  5. data/app/controllers/devise/confirmations_controller.rb +1 -0
  6. data/app/controllers/devise/passwords_controller.rb +3 -2
  7. data/app/controllers/devise/registrations_controller.rb +27 -9
  8. data/app/controllers/devise/sessions_controller.rb +1 -1
  9. data/app/controllers/devise/unlocks_controller.rb +1 -0
  10. data/app/controllers/devise_controller.rb +4 -3
  11. data/app/helpers/devise_helper.rb +21 -18
  12. data/app/mailers/devise/mailer.rb +5 -5
  13. data/app/views/devise/confirmations/new.html.erb +1 -1
  14. data/app/views/devise/passwords/edit.html.erb +3 -3
  15. data/app/views/devise/passwords/new.html.erb +1 -1
  16. data/app/views/devise/registrations/edit.html.erb +5 -5
  17. data/app/views/devise/registrations/new.html.erb +3 -3
  18. data/app/views/devise/sessions/new.html.erb +3 -3
  19. data/app/views/devise/shared/_error_messages.html.erb +15 -0
  20. data/app/views/devise/shared/_links.html.erb +8 -8
  21. data/app/views/devise/unlocks/new.html.erb +1 -1
  22. data/config/locales/en.yml +3 -2
  23. data/lib/devise/controllers/helpers.rb +8 -8
  24. data/lib/devise/controllers/responder.rb +35 -0
  25. data/lib/devise/controllers/sign_in_out.rb +8 -3
  26. data/lib/devise/controllers/url_helpers.rb +1 -1
  27. data/lib/devise/failure_app.rb +31 -7
  28. data/lib/devise/hooks/csrf_cleaner.rb +6 -1
  29. data/lib/devise/hooks/lockable.rb +2 -5
  30. data/lib/devise/hooks/timeoutable.rb +2 -2
  31. data/lib/devise/mapping.rb +1 -1
  32. data/lib/devise/models/authenticatable.rb +51 -48
  33. data/lib/devise/models/confirmable.rb +34 -40
  34. data/lib/devise/models/database_authenticatable.rb +54 -35
  35. data/lib/devise/models/lockable.rb +13 -5
  36. data/lib/devise/models/omniauthable.rb +2 -2
  37. data/lib/devise/models/recoverable.rb +8 -19
  38. data/lib/devise/models/registerable.rb +2 -0
  39. data/lib/devise/models/rememberable.rb +2 -2
  40. data/lib/devise/models/timeoutable.rb +1 -1
  41. data/lib/devise/models/trackable.rb +9 -2
  42. data/lib/devise/models/validatable.rb +4 -9
  43. data/lib/devise/models.rb +1 -0
  44. data/lib/devise/omniauth.rb +2 -5
  45. data/lib/devise/orm.rb +71 -0
  46. data/lib/devise/parameter_filter.rb +2 -0
  47. data/lib/devise/parameter_sanitizer.rb +13 -1
  48. data/lib/devise/rails/deprecated_constant_accessor.rb +39 -0
  49. data/lib/devise/rails/routes.rb +6 -6
  50. data/lib/devise/secret_key_finder.rb +2 -0
  51. data/lib/devise/strategies/authenticatable.rb +1 -1
  52. data/lib/devise/strategies/database_authenticatable.rb +6 -1
  53. data/lib/devise/test/controller_helpers.rb +4 -2
  54. data/lib/devise/test/integration_helpers.rb +1 -1
  55. data/lib/devise/version.rb +1 -1
  56. data/lib/devise.rb +34 -11
  57. data/lib/generators/active_record/devise_generator.rb +26 -11
  58. data/lib/generators/devise/controllers_generator.rb +1 -1
  59. data/lib/generators/devise/devise_generator.rb +1 -1
  60. data/lib/generators/devise/install_generator.rb +1 -5
  61. data/lib/generators/devise/orm_helpers.rb +2 -2
  62. data/lib/generators/devise/views_generator.rb +1 -1
  63. data/lib/generators/mongoid/devise_generator.rb +5 -5
  64. data/lib/generators/templates/README +9 -1
  65. data/lib/generators/templates/controllers/omniauth_callbacks_controller.rb +1 -1
  66. data/lib/generators/templates/devise.rb +38 -8
  67. data/lib/generators/templates/simple_form_for/confirmations/new.html.erb +5 -1
  68. data/lib/generators/templates/simple_form_for/passwords/edit.html.erb +10 -2
  69. data/lib/generators/templates/simple_form_for/passwords/new.html.erb +4 -1
  70. data/lib/generators/templates/simple_form_for/registrations/edit.html.erb +12 -4
  71. data/lib/generators/templates/simple_form_for/registrations/new.html.erb +11 -3
  72. data/lib/generators/templates/simple_form_for/sessions/new.html.erb +7 -2
  73. data/lib/generators/templates/simple_form_for/unlocks/new.html.erb +4 -1
  74. metadata +23 -316
  75. data/.gitignore +0 -10
  76. data/.travis.yml +0 -68
  77. data/.yardopts +0 -9
  78. data/CODE_OF_CONDUCT.md +0 -22
  79. data/CONTRIBUTING.md +0 -79
  80. data/Gemfile +0 -39
  81. data/Gemfile.lock +0 -193
  82. data/ISSUE_TEMPLATE.md +0 -19
  83. data/Rakefile +0 -37
  84. data/bin/test +0 -13
  85. data/devise.gemspec +0 -28
  86. data/devise.png +0 -0
  87. data/gemfiles/Gemfile.rails-4.1-stable +0 -32
  88. data/gemfiles/Gemfile.rails-4.1-stable.lock +0 -171
  89. data/gemfiles/Gemfile.rails-4.2-stable +0 -32
  90. data/gemfiles/Gemfile.rails-4.2-stable.lock +0 -192
  91. data/gemfiles/Gemfile.rails-5.0-stable +0 -33
  92. data/gemfiles/Gemfile.rails-5.0-stable.lock +0 -192
  93. data/gemfiles/Gemfile.rails-5.2-rc1 +0 -26
  94. data/gemfiles/Gemfile.rails-5.2-rc1.lock +0 -201
  95. data/guides/bug_report_templates/integration_test.rb +0 -106
  96. data/test/controllers/custom_registrations_controller_test.rb +0 -42
  97. data/test/controllers/custom_strategy_test.rb +0 -66
  98. data/test/controllers/helper_methods_test.rb +0 -24
  99. data/test/controllers/helpers_test.rb +0 -318
  100. data/test/controllers/inherited_controller_i18n_messages_test.rb +0 -53
  101. data/test/controllers/internal_helpers_test.rb +0 -129
  102. data/test/controllers/load_hooks_controller_test.rb +0 -21
  103. data/test/controllers/passwords_controller_test.rb +0 -34
  104. data/test/controllers/sessions_controller_test.rb +0 -108
  105. data/test/controllers/url_helpers_test.rb +0 -67
  106. data/test/delegator_test.rb +0 -21
  107. data/test/devise_test.rb +0 -109
  108. data/test/failure_app_test.rb +0 -340
  109. data/test/generators/active_record_generator_test.rb +0 -130
  110. data/test/generators/controllers_generator_test.rb +0 -50
  111. data/test/generators/devise_generator_test.rb +0 -41
  112. data/test/generators/install_generator_test.rb +0 -26
  113. data/test/generators/mongoid_generator_test.rb +0 -25
  114. data/test/generators/views_generator_test.rb +0 -105
  115. data/test/helpers/devise_helper_test.rb +0 -51
  116. data/test/integration/authenticatable_test.rb +0 -706
  117. data/test/integration/confirmable_test.rb +0 -326
  118. data/test/integration/database_authenticatable_test.rb +0 -97
  119. data/test/integration/http_authenticatable_test.rb +0 -114
  120. data/test/integration/lockable_test.rb +0 -242
  121. data/test/integration/mounted_engine_test.rb +0 -38
  122. data/test/integration/omniauthable_test.rb +0 -148
  123. data/test/integration/recoverable_test.rb +0 -349
  124. data/test/integration/registerable_test.rb +0 -365
  125. data/test/integration/rememberable_test.rb +0 -219
  126. data/test/integration/timeoutable_test.rb +0 -186
  127. data/test/integration/trackable_test.rb +0 -99
  128. data/test/mailers/confirmation_instructions_test.rb +0 -117
  129. data/test/mailers/email_changed_test.rb +0 -132
  130. data/test/mailers/mailer_test.rb +0 -20
  131. data/test/mailers/reset_password_instructions_test.rb +0 -98
  132. data/test/mailers/unlock_instructions_test.rb +0 -93
  133. data/test/mapping_test.rb +0 -136
  134. data/test/models/authenticatable_test.rb +0 -25
  135. data/test/models/confirmable_test.rb +0 -549
  136. data/test/models/database_authenticatable_test.rb +0 -283
  137. data/test/models/lockable_test.rb +0 -352
  138. data/test/models/omniauthable_test.rb +0 -9
  139. data/test/models/recoverable_test.rb +0 -263
  140. data/test/models/registerable_test.rb +0 -9
  141. data/test/models/rememberable_test.rb +0 -184
  142. data/test/models/serializable_test.rb +0 -60
  143. data/test/models/timeoutable_test.rb +0 -53
  144. data/test/models/trackable_test.rb +0 -62
  145. data/test/models/validatable_test.rb +0 -121
  146. data/test/models_test.rb +0 -155
  147. data/test/omniauth/config_test.rb +0 -61
  148. data/test/omniauth/url_helpers_test.rb +0 -53
  149. data/test/orm/active_record.rb +0 -24
  150. data/test/orm/mongoid.rb +0 -15
  151. data/test/parameter_sanitizer_test.rb +0 -77
  152. data/test/rails_app/Rakefile +0 -6
  153. data/test/rails_app/app/active_record/admin.rb +0 -8
  154. data/test/rails_app/app/active_record/shim.rb +0 -4
  155. data/test/rails_app/app/active_record/user.rb +0 -20
  156. data/test/rails_app/app/active_record/user_on_engine.rb +0 -9
  157. data/test/rails_app/app/active_record/user_on_main_app.rb +0 -9
  158. data/test/rails_app/app/active_record/user_with_validations.rb +0 -12
  159. data/test/rails_app/app/active_record/user_without_email.rb +0 -10
  160. data/test/rails_app/app/controllers/admins/sessions_controller.rb +0 -8
  161. data/test/rails_app/app/controllers/admins_controller.rb +0 -8
  162. data/test/rails_app/app/controllers/application_controller.rb +0 -13
  163. data/test/rails_app/app/controllers/application_with_fake_engine.rb +0 -32
  164. data/test/rails_app/app/controllers/custom/registrations_controller.rb +0 -33
  165. data/test/rails_app/app/controllers/home_controller.rb +0 -31
  166. data/test/rails_app/app/controllers/publisher/registrations_controller.rb +0 -4
  167. data/test/rails_app/app/controllers/publisher/sessions_controller.rb +0 -4
  168. data/test/rails_app/app/controllers/users/omniauth_callbacks_controller.rb +0 -16
  169. data/test/rails_app/app/controllers/users_controller.rb +0 -33
  170. data/test/rails_app/app/helpers/application_helper.rb +0 -5
  171. data/test/rails_app/app/mailers/users/from_proc_mailer.rb +0 -5
  172. data/test/rails_app/app/mailers/users/mailer.rb +0 -5
  173. data/test/rails_app/app/mailers/users/reply_to_mailer.rb +0 -6
  174. data/test/rails_app/app/mongoid/admin.rb +0 -31
  175. data/test/rails_app/app/mongoid/shim.rb +0 -25
  176. data/test/rails_app/app/mongoid/user.rb +0 -50
  177. data/test/rails_app/app/mongoid/user_on_engine.rb +0 -41
  178. data/test/rails_app/app/mongoid/user_on_main_app.rb +0 -41
  179. data/test/rails_app/app/mongoid/user_with_validations.rb +0 -37
  180. data/test/rails_app/app/mongoid/user_without_email.rb +0 -35
  181. data/test/rails_app/app/views/admins/index.html.erb +0 -1
  182. data/test/rails_app/app/views/admins/sessions/new.html.erb +0 -2
  183. data/test/rails_app/app/views/home/admin_dashboard.html.erb +0 -1
  184. data/test/rails_app/app/views/home/index.html.erb +0 -1
  185. data/test/rails_app/app/views/home/join.html.erb +0 -1
  186. data/test/rails_app/app/views/home/private.html.erb +0 -1
  187. data/test/rails_app/app/views/home/user_dashboard.html.erb +0 -1
  188. data/test/rails_app/app/views/layouts/application.html.erb +0 -24
  189. data/test/rails_app/app/views/users/edit_form.html.erb +0 -1
  190. data/test/rails_app/app/views/users/index.html.erb +0 -1
  191. data/test/rails_app/app/views/users/mailer/confirmation_instructions.erb +0 -1
  192. data/test/rails_app/app/views/users/sessions/new.html.erb +0 -1
  193. data/test/rails_app/bin/bundle +0 -3
  194. data/test/rails_app/bin/rails +0 -4
  195. data/test/rails_app/bin/rake +0 -4
  196. data/test/rails_app/config/application.rb +0 -48
  197. data/test/rails_app/config/boot.rb +0 -27
  198. data/test/rails_app/config/database.yml +0 -18
  199. data/test/rails_app/config/environment.rb +0 -7
  200. data/test/rails_app/config/environments/development.rb +0 -32
  201. data/test/rails_app/config/environments/production.rb +0 -88
  202. data/test/rails_app/config/environments/test.rb +0 -47
  203. data/test/rails_app/config/initializers/backtrace_silencers.rb +0 -9
  204. data/test/rails_app/config/initializers/devise.rb +0 -182
  205. data/test/rails_app/config/initializers/inflections.rb +0 -4
  206. data/test/rails_app/config/initializers/secret_token.rb +0 -5
  207. data/test/rails_app/config/initializers/session_store.rb +0 -3
  208. data/test/rails_app/config/routes.rb +0 -128
  209. data/test/rails_app/config.ru +0 -4
  210. data/test/rails_app/db/migrate/20100401102949_create_tables.rb +0 -77
  211. data/test/rails_app/db/schema.rb +0 -57
  212. data/test/rails_app/lib/shared_admin.rb +0 -23
  213. data/test/rails_app/lib/shared_user.rb +0 -32
  214. data/test/rails_app/lib/shared_user_without_email.rb +0 -28
  215. data/test/rails_app/lib/shared_user_without_omniauth.rb +0 -15
  216. data/test/rails_app/public/404.html +0 -26
  217. data/test/rails_app/public/422.html +0 -26
  218. data/test/rails_app/public/500.html +0 -26
  219. data/test/rails_app/public/favicon.ico +0 -0
  220. data/test/rails_test.rb +0 -11
  221. data/test/routes_test.rb +0 -281
  222. data/test/secret_key_finder_test.rb +0 -97
  223. data/test/support/action_controller/record_identifier.rb +0 -12
  224. data/test/support/assertions.rb +0 -30
  225. data/test/support/helpers.rb +0 -83
  226. data/test/support/http_method_compatibility.rb +0 -53
  227. data/test/support/integration.rb +0 -95
  228. data/test/support/locale/en.yml +0 -8
  229. data/test/support/mongoid.yml +0 -6
  230. data/test/support/webrat/integrations/rails.rb +0 -35
  231. data/test/test/controller_helpers_test.rb +0 -193
  232. data/test/test/integration_helpers_test.rb +0 -34
  233. data/test/test_helper.rb +0 -36
  234. 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
@@ -1,50 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "test_helper"
4
-
5
- class ControllersGeneratorTest < Rails::Generators::TestCase
6
- tests Devise::Generators::ControllersGenerator
7
- destination File.expand_path("../../tmp", __FILE__)
8
- setup :prepare_destination
9
-
10
- test "Assert no controllers are created with no params" do
11
- capture(:stderr) { run_generator }
12
- assert_no_file "app/controllers/sessions_controller.rb"
13
- assert_no_file "app/controllers/registrations_controller.rb"
14
- assert_no_file "app/controllers/confirmations_controller.rb"
15
- assert_no_file "app/controllers/passwords_controller.rb"
16
- assert_no_file "app/controllers/unlocks_controller.rb"
17
- assert_no_file "app/controllers/omniauth_callbacks_controller.rb"
18
- end
19
-
20
- test "Assert all controllers are properly created with scope param" do
21
- run_generator %w(users)
22
- assert_class_names 'users'
23
-
24
- run_generator %w(admins)
25
- assert_class_names 'admins'
26
- end
27
-
28
- test "Assert specified controllers with scope" do
29
- run_generator %w(users -c sessions)
30
- assert_file "app/controllers/users/sessions_controller.rb"
31
- assert_no_file "app/controllers/users/registrations_controller.rb"
32
- assert_no_file "app/controllers/users/confirmations_controller.rb"
33
- assert_no_file "app/controllers/users/passwords_controller.rb"
34
- assert_no_file "app/controllers/users/unlocks_controller.rb"
35
- assert_no_file "app/controllers/users/omniauth_callbacks_controller.rb"
36
- end
37
-
38
- private
39
-
40
- def assert_class_names(scope, options = {})
41
- base_dir = "app/controllers#{scope.blank? ? '' : ('/' + scope)}"
42
- scope_prefix = scope.blank? ? '' : (scope.camelize + '::')
43
- controllers = options[:controllers] ||
44
- %w(confirmations passwords registrations sessions unlocks omniauth_callbacks)
45
-
46
- controllers.each do |c|
47
- assert_file "#{base_dir}/#{c}_controller.rb", /#{scope_prefix + c.camelize}/
48
- end
49
- end
50
- end
@@ -1,41 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'test_helper'
4
-
5
- require "generators/devise/devise_generator"
6
-
7
- class DeviseGeneratorTest < Rails::Generators::TestCase
8
- tests Devise::Generators::DeviseGenerator
9
- destination File.expand_path("../../tmp", __FILE__)
10
-
11
- setup do
12
- prepare_destination
13
- copy_routes
14
- end
15
-
16
- test "route generation for simple model names" do
17
- run_generator %w(monster name:string)
18
- assert_file "config/routes.rb", /devise_for :monsters/
19
- end
20
-
21
- test "route generation for namespaced model names" do
22
- run_generator %w(monster/goblin name:string)
23
- match = /devise_for :goblins, class_name: "Monster::Goblin"/
24
- assert_file "config/routes.rb", match
25
- end
26
-
27
- test "route generation with skip routes" do
28
- run_generator %w(monster name:string --skip-routes)
29
- match = /devise_for :monsters, skip: :all/
30
- assert_file "config/routes.rb", match
31
- end
32
-
33
- def copy_routes
34
- routes = File.expand_path("../../rails_app/config/routes.rb", __FILE__)
35
- destination = File.join(destination_root, "config")
36
-
37
- FileUtils.mkdir_p(destination)
38
- FileUtils.cp routes, destination
39
- end
40
-
41
- end
@@ -1,26 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "test_helper"
4
-
5
- class InstallGeneratorTest < Rails::Generators::TestCase
6
- tests Devise::Generators::InstallGenerator
7
- destination File.expand_path("../../tmp", __FILE__)
8
- setup :prepare_destination
9
-
10
- test "assert all files are properly created" do
11
- run_generator(["--orm=active_record"])
12
- assert_file "config/initializers/devise.rb", /devise\/orm\/active_record/
13
- assert_file "config/locales/devise.en.yml"
14
- end
15
-
16
- test "fails if no ORM is specified" do
17
- stderr = capture(:stderr) do
18
- run_generator
19
- end
20
-
21
- assert_match %r{An ORM must be set to install Devise}, stderr
22
-
23
- assert_no_file "config/initializers/devise.rb"
24
- assert_no_file "config/locales/devise.en.yml"
25
- end
26
- end
@@ -1,25 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "test_helper"
4
-
5
- if DEVISE_ORM == :mongoid
6
- require "generators/mongoid/devise_generator"
7
-
8
- class MongoidGeneratorTest < Rails::Generators::TestCase
9
- tests Mongoid::Generators::DeviseGenerator
10
- destination File.expand_path("../../tmp", __FILE__)
11
- setup :prepare_destination
12
-
13
- test "all files are properly created" do
14
- run_generator %w(monster)
15
- assert_file "app/models/monster.rb", /devise/
16
- end
17
-
18
- test "all files are properly deleted" do
19
- run_generator %w(monster)
20
- run_generator %w(monster), behavior: :revoke
21
- assert_no_file "app/models/monster.rb"
22
- end
23
- end
24
- end
25
-