devise 4.1.1 → 4.8.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (255) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +224 -4
  3. data/MIT-LICENSE +2 -1
  4. data/README.md +275 -90
  5. data/app/controllers/devise/confirmations_controller.rb +2 -0
  6. data/app/controllers/devise/omniauth_callbacks_controller.rb +7 -5
  7. data/app/controllers/devise/passwords_controller.rb +3 -0
  8. data/app/controllers/devise/registrations_controller.rb +34 -13
  9. data/app/controllers/devise/sessions_controller.rb +3 -1
  10. data/app/controllers/devise/unlocks_controller.rb +2 -0
  11. data/app/controllers/devise_controller.rb +5 -3
  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 +39 -17
  26. data/lib/devise/controllers/helpers.rb +22 -9
  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 +2 -0
  34. data/lib/devise/failure_app.rb +63 -33
  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 +6 -3
  44. data/lib/devise/mapping.rb +3 -1
  45. data/lib/devise/models.rb +3 -1
  46. data/lib/devise/models/authenticatable.rb +63 -37
  47. data/lib/devise/models/confirmable.rb +79 -22
  48. data/lib/devise/models/database_authenticatable.rb +86 -17
  49. data/lib/devise/models/lockable.rb +17 -3
  50. data/lib/devise/models/omniauthable.rb +2 -0
  51. data/lib/devise/models/recoverable.rb +32 -20
  52. data/lib/devise/models/registerable.rb +4 -0
  53. data/lib/devise/models/rememberable.rb +5 -3
  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 +2 -51
  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 +15 -56
  65. data/lib/devise/rails.rb +6 -6
  66. data/lib/devise/rails/deprecated_constant_accessor.rb +39 -0
  67. data/lib/devise/rails/routes.rb +9 -7
  68. data/lib/devise/rails/warden_compat.rb +2 -0
  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 +8 -1
  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 -129
  77. data/lib/devise/time_inflector.rb +2 -0
  78. data/lib/devise/token_generator.rb +2 -0
  79. data/lib/devise/version.rb +3 -1
  80. data/lib/generators/active_record/devise_generator.rb +40 -12
  81. data/lib/generators/active_record/templates/migration.rb +3 -1
  82. data/lib/generators/active_record/templates/migration_existing.rb +2 -0
  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 +3 -5
  86. data/lib/generators/devise/orm_helpers.rb +9 -3
  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 +4 -2
  94. data/lib/generators/templates/controllers/sessions_controller.rb +3 -1
  95. data/lib/generators/templates/controllers/unlocks_controller.rb +2 -0
  96. data/lib/generators/templates/devise.rb +49 -6
  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 +16 -297
  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 -182
  114. data/Rakefile +0 -36
  115. data/bin/test +0 -13
  116. data/devise.gemspec +0 -26
  117. data/devise.png +0 -0
  118. data/gemfiles/Gemfile.rails-4.1-stable +0 -30
  119. data/gemfiles/Gemfile.rails-4.1-stable.lock +0 -170
  120. data/gemfiles/Gemfile.rails-4.2-stable +0 -30
  121. data/gemfiles/Gemfile.rails-4.2-stable.lock +0 -192
  122. data/gemfiles/Gemfile.rails-5.0-beta +0 -37
  123. data/gemfiles/Gemfile.rails-5.0-beta.lock +0 -199
  124. data/test/controllers/custom_registrations_controller_test.rb +0 -40
  125. data/test/controllers/custom_strategy_test.rb +0 -64
  126. data/test/controllers/helper_methods_test.rb +0 -22
  127. data/test/controllers/helpers_test.rb +0 -316
  128. data/test/controllers/inherited_controller_i18n_messages_test.rb +0 -51
  129. data/test/controllers/internal_helpers_test.rb +0 -127
  130. data/test/controllers/load_hooks_controller_test.rb +0 -19
  131. data/test/controllers/passwords_controller_test.rb +0 -32
  132. data/test/controllers/sessions_controller_test.rb +0 -106
  133. data/test/controllers/url_helpers_test.rb +0 -65
  134. data/test/delegator_test.rb +0 -19
  135. data/test/devise_test.rb +0 -107
  136. data/test/failure_app_test.rb +0 -320
  137. data/test/generators/active_record_generator_test.rb +0 -83
  138. data/test/generators/controllers_generator_test.rb +0 -48
  139. data/test/generators/devise_generator_test.rb +0 -39
  140. data/test/generators/install_generator_test.rb +0 -24
  141. data/test/generators/mongoid_generator_test.rb +0 -23
  142. data/test/generators/views_generator_test.rb +0 -103
  143. data/test/helpers/devise_helper_test.rb +0 -49
  144. data/test/integration/authenticatable_test.rb +0 -698
  145. data/test/integration/confirmable_test.rb +0 -324
  146. data/test/integration/database_authenticatable_test.rb +0 -95
  147. data/test/integration/http_authenticatable_test.rb +0 -106
  148. data/test/integration/lockable_test.rb +0 -240
  149. data/test/integration/omniauthable_test.rb +0 -135
  150. data/test/integration/recoverable_test.rb +0 -347
  151. data/test/integration/registerable_test.rb +0 -357
  152. data/test/integration/rememberable_test.rb +0 -211
  153. data/test/integration/timeoutable_test.rb +0 -184
  154. data/test/integration/trackable_test.rb +0 -92
  155. data/test/mailers/confirmation_instructions_test.rb +0 -115
  156. data/test/mailers/reset_password_instructions_test.rb +0 -96
  157. data/test/mailers/unlock_instructions_test.rb +0 -91
  158. data/test/mapping_test.rb +0 -134
  159. data/test/models/authenticatable_test.rb +0 -23
  160. data/test/models/confirmable_test.rb +0 -511
  161. data/test/models/database_authenticatable_test.rb +0 -269
  162. data/test/models/lockable_test.rb +0 -350
  163. data/test/models/omniauthable_test.rb +0 -7
  164. data/test/models/recoverable_test.rb +0 -251
  165. data/test/models/registerable_test.rb +0 -7
  166. data/test/models/rememberable_test.rb +0 -169
  167. data/test/models/serializable_test.rb +0 -49
  168. data/test/models/timeoutable_test.rb +0 -51
  169. data/test/models/trackable_test.rb +0 -41
  170. data/test/models/validatable_test.rb +0 -119
  171. data/test/models_test.rb +0 -153
  172. data/test/omniauth/config_test.rb +0 -57
  173. data/test/omniauth/url_helpers_test.rb +0 -51
  174. data/test/orm/active_record.rb +0 -17
  175. data/test/orm/mongoid.rb +0 -13
  176. data/test/parameter_sanitizer_test.rb +0 -131
  177. data/test/rails_app/Rakefile +0 -6
  178. data/test/rails_app/app/active_record/admin.rb +0 -6
  179. data/test/rails_app/app/active_record/shim.rb +0 -2
  180. data/test/rails_app/app/active_record/user.rb +0 -7
  181. data/test/rails_app/app/active_record/user_on_engine.rb +0 -7
  182. data/test/rails_app/app/active_record/user_on_main_app.rb +0 -7
  183. data/test/rails_app/app/active_record/user_without_email.rb +0 -8
  184. data/test/rails_app/app/controllers/admins/sessions_controller.rb +0 -6
  185. data/test/rails_app/app/controllers/admins_controller.rb +0 -6
  186. data/test/rails_app/app/controllers/application_controller.rb +0 -11
  187. data/test/rails_app/app/controllers/application_with_fake_engine.rb +0 -30
  188. data/test/rails_app/app/controllers/custom/registrations_controller.rb +0 -31
  189. data/test/rails_app/app/controllers/home_controller.rb +0 -29
  190. data/test/rails_app/app/controllers/publisher/registrations_controller.rb +0 -2
  191. data/test/rails_app/app/controllers/publisher/sessions_controller.rb +0 -2
  192. data/test/rails_app/app/controllers/users/omniauth_callbacks_controller.rb +0 -14
  193. data/test/rails_app/app/controllers/users_controller.rb +0 -31
  194. data/test/rails_app/app/helpers/application_helper.rb +0 -3
  195. data/test/rails_app/app/mailers/users/from_proc_mailer.rb +0 -3
  196. data/test/rails_app/app/mailers/users/mailer.rb +0 -3
  197. data/test/rails_app/app/mailers/users/reply_to_mailer.rb +0 -4
  198. data/test/rails_app/app/mongoid/admin.rb +0 -29
  199. data/test/rails_app/app/mongoid/shim.rb +0 -23
  200. data/test/rails_app/app/mongoid/user.rb +0 -39
  201. data/test/rails_app/app/mongoid/user_on_engine.rb +0 -39
  202. data/test/rails_app/app/mongoid/user_on_main_app.rb +0 -39
  203. data/test/rails_app/app/mongoid/user_without_email.rb +0 -33
  204. data/test/rails_app/app/views/admins/index.html.erb +0 -1
  205. data/test/rails_app/app/views/admins/sessions/new.html.erb +0 -2
  206. data/test/rails_app/app/views/home/admin_dashboard.html.erb +0 -1
  207. data/test/rails_app/app/views/home/index.html.erb +0 -1
  208. data/test/rails_app/app/views/home/join.html.erb +0 -1
  209. data/test/rails_app/app/views/home/private.html.erb +0 -1
  210. data/test/rails_app/app/views/home/user_dashboard.html.erb +0 -1
  211. data/test/rails_app/app/views/layouts/application.html.erb +0 -24
  212. data/test/rails_app/app/views/users/edit_form.html.erb +0 -1
  213. data/test/rails_app/app/views/users/index.html.erb +0 -1
  214. data/test/rails_app/app/views/users/mailer/confirmation_instructions.erb +0 -1
  215. data/test/rails_app/app/views/users/sessions/new.html.erb +0 -1
  216. data/test/rails_app/bin/bundle +0 -3
  217. data/test/rails_app/bin/rails +0 -4
  218. data/test/rails_app/bin/rake +0 -4
  219. data/test/rails_app/config.ru +0 -4
  220. data/test/rails_app/config/application.rb +0 -44
  221. data/test/rails_app/config/boot.rb +0 -14
  222. data/test/rails_app/config/database.yml +0 -18
  223. data/test/rails_app/config/environment.rb +0 -5
  224. data/test/rails_app/config/environments/development.rb +0 -30
  225. data/test/rails_app/config/environments/production.rb +0 -84
  226. data/test/rails_app/config/environments/test.rb +0 -46
  227. data/test/rails_app/config/initializers/backtrace_silencers.rb +0 -7
  228. data/test/rails_app/config/initializers/devise.rb +0 -180
  229. data/test/rails_app/config/initializers/inflections.rb +0 -2
  230. data/test/rails_app/config/initializers/secret_token.rb +0 -3
  231. data/test/rails_app/config/initializers/session_store.rb +0 -1
  232. data/test/rails_app/config/routes.rb +0 -126
  233. data/test/rails_app/db/migrate/20100401102949_create_tables.rb +0 -71
  234. data/test/rails_app/db/schema.rb +0 -55
  235. data/test/rails_app/lib/shared_admin.rb +0 -17
  236. data/test/rails_app/lib/shared_user.rb +0 -30
  237. data/test/rails_app/lib/shared_user_without_email.rb +0 -26
  238. data/test/rails_app/lib/shared_user_without_omniauth.rb +0 -13
  239. data/test/rails_app/public/404.html +0 -26
  240. data/test/rails_app/public/422.html +0 -26
  241. data/test/rails_app/public/500.html +0 -26
  242. data/test/rails_app/public/favicon.ico +0 -0
  243. data/test/rails_test.rb +0 -9
  244. data/test/routes_test.rb +0 -279
  245. data/test/support/action_controller/record_identifier.rb +0 -10
  246. data/test/support/assertions.rb +0 -39
  247. data/test/support/helpers.rb +0 -77
  248. data/test/support/http_method_compatibility.rb +0 -51
  249. data/test/support/integration.rb +0 -92
  250. data/test/support/locale/en.yml +0 -8
  251. data/test/support/mongoid.yml +0 -6
  252. data/test/support/webrat/integrations/rails.rb +0 -33
  253. data/test/test_helper.rb +0 -34
  254. data/test/test_helpers_test.rb +0 -178
  255. data/test/test_models.rb +0 -33
@@ -1,320 +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
- # Passing nil for action_dispatch.request.formats prevents the default from being used in Rails 5, need to remove it
62
- if env.has_key?('action_dispatch.request.formats') && env['action_dispatch.request.formats'].nil?
63
- env.delete 'action_dispatch.request.formats' unless env['action_dispatch.request.formats']
64
- end
65
-
66
- @response = (env.delete(:app) || Devise::FailureApp).call(env).to_a
67
- @request = ActionDispatch::Request.new(env)
68
- end
69
-
70
- context 'When redirecting' do
71
- test 'returns to the default redirect location' do
72
- call_failure
73
- assert_equal 302, @response.first
74
- assert_equal 'You need to sign in or sign up before continuing.', @request.flash[:alert]
75
- assert_equal 'http://test.host/users/sign_in', @response.second['Location']
76
- end
77
-
78
- test 'returns to the default redirect location considering subdomain' do
79
- call_failure('warden.options' => { scope: :subdomain_user })
80
- assert_equal 302, @response.first
81
- assert_equal 'You need to sign in or sign up before continuing.', @request.flash[:alert]
82
- assert_equal 'http://sub.test.host/subdomain_users/sign_in', @response.second['Location']
83
- end
84
-
85
- test 'returns to the default redirect location for wildcard requests' do
86
- call_failure 'action_dispatch.request.formats' => nil, 'HTTP_ACCEPT' => '*/*'
87
- assert_equal 302, @response.first
88
- assert_equal 'http://test.host/users/sign_in', @response.second['Location']
89
- end
90
-
91
- test 'returns to the root path if no session path is available' do
92
- swap Devise, router_name: :fake_app do
93
- call_failure app: RootFailureApp
94
- assert_equal 302, @response.first
95
- assert_equal 'You need to sign in or sign up before continuing.', @request.flash[:alert]
96
- assert_equal 'http://test.host/', @response.second['Location']
97
- end
98
- end
99
-
100
- test 'returns to the root path considering subdomain if no session path is available' do
101
- swap Devise, router_name: :fake_app do
102
- call_failure app: FailureWithSubdomain
103
- assert_equal 302, @response.first
104
- assert_equal 'You need to sign in or sign up before continuing.', @request.flash[:alert]
105
- assert_equal 'http://sub.test.host/', @response.second['Location']
106
- end
107
- end
108
-
109
- test 'returns to the default redirect location considering the router for supplied scope' do
110
- call_failure app: FakeEngineApp, 'warden.options' => { scope: :user_on_engine }
111
- assert_equal 302, @response.first
112
- assert_equal 'You need to sign in or sign up before continuing.', @request.flash[:alert]
113
- assert_equal 'http://test.host/user_on_engines/sign_in', @response.second['Location']
114
- end
115
-
116
- if Rails.application.config.respond_to?(:relative_url_root)
117
- test 'returns to the default redirect location considering the relative url root' do
118
- swap Rails.application.config, relative_url_root: "/sample" do
119
- call_failure
120
- assert_equal 302, @response.first
121
- assert_equal 'http://test.host/sample/users/sign_in', @response.second['Location']
122
- end
123
- end
124
-
125
- test 'returns to the default redirect location considering the relative url root and subdomain' do
126
- swap Rails.application.config, relative_url_root: "/sample" do
127
- call_failure('warden.options' => { scope: :subdomain_user })
128
- assert_equal 302, @response.first
129
- assert_equal 'http://sub.test.host/sample/subdomain_users/sign_in', @response.second['Location']
130
- end
131
- end
132
- end
133
-
134
- test 'uses the proxy failure message as symbol' do
135
- call_failure('warden' => OpenStruct.new(message: :invalid))
136
- assert_equal 'Invalid Email or password.', @request.flash[:alert]
137
- assert_equal 'http://test.host/users/sign_in', @response.second["Location"]
138
- end
139
-
140
- test 'supports authentication_keys as a Hash for the flash message' do
141
- swap Devise, authentication_keys: { email: true, login: true } do
142
- call_failure('warden' => OpenStruct.new(message: :invalid))
143
- assert_equal 'Invalid Email, Login or password.', @request.flash[:alert]
144
- end
145
- end
146
-
147
- test 'uses custom i18n options' do
148
- call_failure('warden' => OpenStruct.new(message: :does_not_exist), app: FailureWithI18nOptions)
149
- assert_equal 'User Steve does not exist', @request.flash[:alert]
150
- end
151
-
152
- test 'uses the proxy failure message as string' do
153
- call_failure('warden' => OpenStruct.new(message: 'Hello world'))
154
- assert_equal 'Hello world', @request.flash[:alert]
155
- assert_equal 'http://test.host/users/sign_in', @response.second["Location"]
156
- end
157
-
158
- test 'set content type to default text/html' do
159
- call_failure
160
- assert_equal 'text/html; charset=utf-8', @response.second['Content-Type']
161
- end
162
-
163
- test 'set up a default message' do
164
- call_failure
165
- assert_match(/You are being/, @response.last.body)
166
- assert_match(/redirected/, @response.last.body)
167
- assert_match(/users\/sign_in/, @response.last.body)
168
- end
169
-
170
- test 'works for any navigational format' do
171
- swap Devise, navigational_formats: [:xml] do
172
- call_failure('formats' => Mime[:xml])
173
- assert_equal 302, @response.first
174
- end
175
- end
176
-
177
- test 'redirects the correct format if it is a non-html format request' do
178
- swap Devise, navigational_formats: [:js] do
179
- call_failure('formats' => Mime[:js])
180
- assert_equal 'http://test.host/users/sign_in.js', @response.second["Location"]
181
- end
182
- end
183
- end
184
-
185
- context 'For HTTP request' do
186
- test 'return 401 status' do
187
- call_failure('formats' => Mime[:xml])
188
- assert_equal 401, @response.first
189
- end
190
-
191
- test 'return appropriate body for xml' do
192
- call_failure('formats' => Mime[:xml])
193
- 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)
194
- assert_equal result, @response.last.body
195
- end
196
-
197
- test 'return appropriate body for json' do
198
- call_failure('formats' => Mime[:json])
199
- result = %({"error":"You need to sign in or sign up before continuing."})
200
- assert_equal result, @response.last.body
201
- end
202
-
203
- test 'return 401 status for unknown formats' do
204
- call_failure 'formats' => []
205
- assert_equal 401, @response.first
206
- end
207
-
208
- test 'return WWW-authenticate headers if model allows' do
209
- call_failure('formats' => Mime[:xml])
210
- assert_equal 'Basic realm="Application"', @response.second["WWW-Authenticate"]
211
- end
212
-
213
- test 'does not return WWW-authenticate headers if model does not allow' do
214
- swap Devise, http_authenticatable: false do
215
- call_failure('formats' => Mime[:xml])
216
- assert_nil @response.second["WWW-Authenticate"]
217
- end
218
- end
219
-
220
- test 'works for any non navigational format' do
221
- swap Devise, navigational_formats: [] do
222
- call_failure('formats' => Mime[:html])
223
- assert_equal 401, @response.first
224
- end
225
- end
226
-
227
- test 'uses the failure message as response body' do
228
- call_failure('formats' => Mime[:xml], 'warden' => OpenStruct.new(message: :invalid))
229
- assert_match '<error>Invalid Email or password.</error>', @response.third.body
230
- end
231
-
232
- context 'on ajax call' do
233
- context 'when http_authenticatable_on_xhr is false' do
234
- test 'dont return 401 with navigational formats' do
235
- swap Devise, http_authenticatable_on_xhr: false do
236
- call_failure('formats' => Mime[:html], 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest')
237
- assert_equal 302, @response.first
238
- assert_equal 'http://test.host/users/sign_in', @response.second["Location"]
239
- end
240
- end
241
-
242
- test 'dont return 401 with non navigational formats' do
243
- swap Devise, http_authenticatable_on_xhr: false do
244
- call_failure('formats' => Mime[:json], 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest')
245
- assert_equal 302, @response.first
246
- assert_equal 'http://test.host/users/sign_in.json', @response.second["Location"]
247
- end
248
- end
249
- end
250
-
251
- context 'when http_authenticatable_on_xhr is true' do
252
- test 'return 401' do
253
- swap Devise, http_authenticatable_on_xhr: true do
254
- call_failure('formats' => Mime[:html], 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest')
255
- assert_equal 401, @response.first
256
- end
257
- end
258
-
259
- test 'skip WWW-Authenticate header' do
260
- swap Devise, http_authenticatable_on_xhr: true do
261
- call_failure('formats' => Mime[:html], 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest')
262
- assert_nil @response.second['WWW-Authenticate']
263
- end
264
- end
265
- end
266
- end
267
- end
268
-
269
- context 'With recall' do
270
- test 'calls the original controller if invalid email or password' do
271
- env = {
272
- "warden.options" => { recall: "devise/sessions#new", attempted_path: "/users/sign_in" },
273
- "devise.mapping" => Devise.mappings[:user],
274
- "warden" => stub_everything
275
- }
276
- call_failure(env)
277
- assert @response.third.body.include?('<h2>Log in</h2>')
278
- assert @response.third.body.include?('Invalid Email or password.')
279
- end
280
-
281
- test 'calls the original controller if not confirmed email' do
282
- env = {
283
- "warden.options" => { recall: "devise/sessions#new", attempted_path: "/users/sign_in", message: :unconfirmed },
284
- "devise.mapping" => Devise.mappings[:user],
285
- "warden" => stub_everything
286
- }
287
- call_failure(env)
288
- assert @response.third.body.include?('<h2>Log in</h2>')
289
- assert @response.third.body.include?('You have to confirm your email address before continuing.')
290
- end
291
-
292
- test 'calls the original controller if inactive account' do
293
- env = {
294
- "warden.options" => { recall: "devise/sessions#new", attempted_path: "/users/sign_in", message: :inactive },
295
- "devise.mapping" => Devise.mappings[:user],
296
- "warden" => stub_everything
297
- }
298
- call_failure(env)
299
- assert @response.third.body.include?('<h2>Log in</h2>')
300
- assert @response.third.body.include?('Your account is not activated yet.')
301
- end
302
-
303
- if Rails.application.config.respond_to?(:relative_url_root)
304
- test 'calls the original controller with the proper environment considering the relative url root' do
305
- swap Rails.application.config, relative_url_root: "/sample" do
306
- env = {
307
- "warden.options" => { recall: "devise/sessions#new", attempted_path: "/sample/users/sign_in"},
308
- "devise.mapping" => Devise.mappings[:user],
309
- "warden" => stub_everything
310
- }
311
- call_failure(env)
312
- assert @response.third.body.include?('<h2>Log in</h2>')
313
- assert @response.third.body.include?('Invalid Email or password.')
314
- assert_equal @request.env["SCRIPT_NAME"], '/sample'
315
- assert_equal @request.env["PATH_INFO"], '/users/sign_in'
316
- end
317
- end
318
- end
319
- end
320
- end
@@ -1,83 +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
- simulate_inside_engine(RailsEngine::Engine, RailsEngine) do
73
- run_generator ["monster"]
74
-
75
- assert_file "app/models/rails_engine/monster.rb", /devise/
76
- assert_file "app/models/rails_engine/monster.rb" do |content|
77
- assert_no_match %r{attr_accessible :email}, content
78
- end
79
- end
80
- end
81
-
82
- end
83
- 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