devise 3.5.10 → 4.8.0

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