devise-jdguyot 1.2.rc

Sign up to get free protection for your applications and to get access to all the features.
Files changed (185) hide show
  1. data/.gitignore +10 -0
  2. data/CHANGELOG.rdoc +532 -0
  3. data/Gemfile +29 -0
  4. data/Gemfile.lock +152 -0
  5. data/MIT-LICENSE +20 -0
  6. data/README.rdoc +353 -0
  7. data/Rakefile +36 -0
  8. data/TODO +4 -0
  9. data/app/controllers/devise/confirmations_controller.rb +33 -0
  10. data/app/controllers/devise/omniauth_callbacks_controller.rb +26 -0
  11. data/app/controllers/devise/passwords_controller.rb +41 -0
  12. data/app/controllers/devise/registrations_controller.rb +110 -0
  13. data/app/controllers/devise/sessions_controller.rb +25 -0
  14. data/app/controllers/devise/unlocks_controller.rb +34 -0
  15. data/app/helpers/devise_helper.rb +19 -0
  16. data/app/mailers/devise/mailer.rb +88 -0
  17. data/app/views/devise/confirmations/new.html.erb +12 -0
  18. data/app/views/devise/mailer/confirmation_instructions.html.erb +5 -0
  19. data/app/views/devise/mailer/reset_password_instructions.html.erb +8 -0
  20. data/app/views/devise/mailer/unlock_instructions.html.erb +7 -0
  21. data/app/views/devise/passwords/edit.html.erb +16 -0
  22. data/app/views/devise/passwords/new.html.erb +12 -0
  23. data/app/views/devise/registrations/edit.html.erb +25 -0
  24. data/app/views/devise/registrations/new.html.erb +18 -0
  25. data/app/views/devise/sessions/new.html.erb +17 -0
  26. data/app/views/devise/shared/_links.erb +25 -0
  27. data/app/views/devise/unlocks/new.html.erb +12 -0
  28. data/config/locales/en.yml +46 -0
  29. data/devise.gemspec +25 -0
  30. data/lib/devise/controllers/helpers.rb +227 -0
  31. data/lib/devise/controllers/internal_helpers.rb +119 -0
  32. data/lib/devise/controllers/scoped_views.rb +33 -0
  33. data/lib/devise/controllers/url_helpers.rb +39 -0
  34. data/lib/devise/encryptors/authlogic_sha512.rb +19 -0
  35. data/lib/devise/encryptors/base.rb +20 -0
  36. data/lib/devise/encryptors/clearance_sha1.rb +17 -0
  37. data/lib/devise/encryptors/restful_authentication_sha1.rb +22 -0
  38. data/lib/devise/encryptors/sha1.rb +25 -0
  39. data/lib/devise/encryptors/sha512.rb +25 -0
  40. data/lib/devise/failure_app.rb +132 -0
  41. data/lib/devise/hooks/activatable.rb +11 -0
  42. data/lib/devise/hooks/forgetable.rb +12 -0
  43. data/lib/devise/hooks/rememberable.rb +48 -0
  44. data/lib/devise/hooks/timeoutable.rb +22 -0
  45. data/lib/devise/hooks/trackable.rb +9 -0
  46. data/lib/devise/mapping.rb +110 -0
  47. data/lib/devise/models/authenticatable.rb +146 -0
  48. data/lib/devise/models/confirmable.rb +160 -0
  49. data/lib/devise/models/database_authenticatable.rb +100 -0
  50. data/lib/devise/models/encryptable.rb +72 -0
  51. data/lib/devise/models/lockable.rb +169 -0
  52. data/lib/devise/models/omniauthable.rb +23 -0
  53. data/lib/devise/models/recoverable.rb +123 -0
  54. data/lib/devise/models/registerable.rb +21 -0
  55. data/lib/devise/models/rememberable.rb +130 -0
  56. data/lib/devise/models/timeoutable.rb +43 -0
  57. data/lib/devise/models/token_authenticatable.rb +72 -0
  58. data/lib/devise/models/trackable.rb +30 -0
  59. data/lib/devise/models/validatable.rb +65 -0
  60. data/lib/devise/models.rb +68 -0
  61. data/lib/devise/modules.rb +30 -0
  62. data/lib/devise/omniauth/config.rb +30 -0
  63. data/lib/devise/omniauth/test_helpers.rb +57 -0
  64. data/lib/devise/omniauth/url_helpers.rb +29 -0
  65. data/lib/devise/omniauth.rb +47 -0
  66. data/lib/devise/orm/active_record.rb +38 -0
  67. data/lib/devise/orm/mongoid.rb +31 -0
  68. data/lib/devise/path_checker.rb +18 -0
  69. data/lib/devise/rails/routes.rb +292 -0
  70. data/lib/devise/rails/warden_compat.rb +125 -0
  71. data/lib/devise/rails.rb +50 -0
  72. data/lib/devise/schema.rb +97 -0
  73. data/lib/devise/strategies/authenticatable.rb +150 -0
  74. data/lib/devise/strategies/base.rb +15 -0
  75. data/lib/devise/strategies/database_authenticatable.rb +21 -0
  76. data/lib/devise/strategies/rememberable.rb +51 -0
  77. data/lib/devise/strategies/token_authenticatable.rb +53 -0
  78. data/lib/devise/test_helpers.rb +100 -0
  79. data/lib/devise/version.rb +3 -0
  80. data/lib/devise.rb +381 -0
  81. data/lib/generators/active_record/devise_generator.rb +28 -0
  82. data/lib/generators/active_record/templates/migration.rb +31 -0
  83. data/lib/generators/devise/devise_generator.rb +17 -0
  84. data/lib/generators/devise/install_generator.rb +24 -0
  85. data/lib/generators/devise/orm_helpers.rb +23 -0
  86. data/lib/generators/devise/views_generator.rb +106 -0
  87. data/lib/generators/mongoid/devise_generator.rb +17 -0
  88. data/lib/generators/templates/README +25 -0
  89. data/lib/generators/templates/devise.rb +186 -0
  90. data/test/controllers/helpers_test.rb +237 -0
  91. data/test/controllers/internal_helpers_test.rb +72 -0
  92. data/test/controllers/url_helpers_test.rb +59 -0
  93. data/test/devise_test.rb +65 -0
  94. data/test/encryptors_test.rb +30 -0
  95. data/test/failure_app_test.rb +187 -0
  96. data/test/generators/active_record_generator_test.rb +24 -0
  97. data/test/generators/install_generator_test.rb +13 -0
  98. data/test/generators/mongoid_generator_test.rb +22 -0
  99. data/test/generators/views_generator_test.rb +35 -0
  100. data/test/indifferent_hash.rb +33 -0
  101. data/test/integration/authenticatable_test.rb +447 -0
  102. data/test/integration/confirmable_test.rb +104 -0
  103. data/test/integration/database_authenticatable_test.rb +60 -0
  104. data/test/integration/http_authenticatable_test.rb +74 -0
  105. data/test/integration/lockable_test.rb +109 -0
  106. data/test/integration/omniauthable_test.rb +107 -0
  107. data/test/integration/recoverable_test.rb +160 -0
  108. data/test/integration/registerable_test.rb +179 -0
  109. data/test/integration/rememberable_test.rb +180 -0
  110. data/test/integration/timeoutable_test.rb +89 -0
  111. data/test/integration/token_authenticatable_test.rb +99 -0
  112. data/test/integration/trackable_test.rb +64 -0
  113. data/test/mailers/confirmation_instructions_test.rb +84 -0
  114. data/test/mailers/reset_password_instructions_test.rb +72 -0
  115. data/test/mailers/unlock_instructions_test.rb +66 -0
  116. data/test/mapping_test.rb +119 -0
  117. data/test/models/confirmable_test.rb +221 -0
  118. data/test/models/database_authenticatable_test.rb +98 -0
  119. data/test/models/encryptable_test.rb +65 -0
  120. data/test/models/lockable_test.rb +204 -0
  121. data/test/models/recoverable_test.rb +190 -0
  122. data/test/models/rememberable_test.rb +279 -0
  123. data/test/models/timeoutable_test.rb +28 -0
  124. data/test/models/token_authenticatable_test.rb +37 -0
  125. data/test/models/trackable_test.rb +5 -0
  126. data/test/models/validatable_test.rb +99 -0
  127. data/test/models_test.rb +84 -0
  128. data/test/omniauth/url_helpers_test.rb +47 -0
  129. data/test/orm/active_record.rb +9 -0
  130. data/test/orm/mongoid.rb +11 -0
  131. data/test/rails_app/Rakefile +10 -0
  132. data/test/rails_app/app/active_record/admin.rb +6 -0
  133. data/test/rails_app/app/active_record/shim.rb +2 -0
  134. data/test/rails_app/app/active_record/user.rb +8 -0
  135. data/test/rails_app/app/controllers/admins/sessions_controller.rb +6 -0
  136. data/test/rails_app/app/controllers/admins_controller.rb +6 -0
  137. data/test/rails_app/app/controllers/application_controller.rb +8 -0
  138. data/test/rails_app/app/controllers/home_controller.rb +16 -0
  139. data/test/rails_app/app/controllers/publisher/registrations_controller.rb +2 -0
  140. data/test/rails_app/app/controllers/publisher/sessions_controller.rb +2 -0
  141. data/test/rails_app/app/controllers/users/omniauth_callbacks_controller.rb +7 -0
  142. data/test/rails_app/app/controllers/users_controller.rb +18 -0
  143. data/test/rails_app/app/helpers/application_helper.rb +3 -0
  144. data/test/rails_app/app/mongoid/admin.rb +9 -0
  145. data/test/rails_app/app/mongoid/shim.rb +29 -0
  146. data/test/rails_app/app/mongoid/user.rb +10 -0
  147. data/test/rails_app/app/views/admins/index.html.erb +1 -0
  148. data/test/rails_app/app/views/admins/sessions/new.html.erb +2 -0
  149. data/test/rails_app/app/views/home/index.html.erb +1 -0
  150. data/test/rails_app/app/views/home/private.html.erb +1 -0
  151. data/test/rails_app/app/views/layouts/application.html.erb +24 -0
  152. data/test/rails_app/app/views/users/index.html.erb +1 -0
  153. data/test/rails_app/app/views/users/mailer/confirmation_instructions.erb +1 -0
  154. data/test/rails_app/app/views/users/sessions/new.html.erb +1 -0
  155. data/test/rails_app/config/application.rb +40 -0
  156. data/test/rails_app/config/boot.rb +13 -0
  157. data/test/rails_app/config/database.yml +18 -0
  158. data/test/rails_app/config/environment.rb +5 -0
  159. data/test/rails_app/config/environments/development.rb +19 -0
  160. data/test/rails_app/config/environments/production.rb +33 -0
  161. data/test/rails_app/config/environments/test.rb +33 -0
  162. data/test/rails_app/config/initializers/backtrace_silencers.rb +7 -0
  163. data/test/rails_app/config/initializers/devise.rb +176 -0
  164. data/test/rails_app/config/initializers/inflections.rb +2 -0
  165. data/test/rails_app/config/initializers/secret_token.rb +2 -0
  166. data/test/rails_app/config/routes.rb +55 -0
  167. data/test/rails_app/config.ru +4 -0
  168. data/test/rails_app/db/migrate/20100401102949_create_tables.rb +31 -0
  169. data/test/rails_app/db/schema.rb +52 -0
  170. data/test/rails_app/lib/shared_admin.rb +9 -0
  171. data/test/rails_app/lib/shared_user.rb +23 -0
  172. data/test/rails_app/public/404.html +26 -0
  173. data/test/rails_app/public/422.html +26 -0
  174. data/test/rails_app/public/500.html +26 -0
  175. data/test/rails_app/public/favicon.ico +0 -0
  176. data/test/rails_app/script/rails +10 -0
  177. data/test/routes_test.rb +179 -0
  178. data/test/support/assertions.rb +24 -0
  179. data/test/support/helpers.rb +60 -0
  180. data/test/support/integration.rb +88 -0
  181. data/test/support/locale/en.yml +4 -0
  182. data/test/support/webrat/integrations/rails.rb +24 -0
  183. data/test/test_helper.rb +29 -0
  184. data/test/test_helpers_test.rb +118 -0
  185. metadata +388 -0
@@ -0,0 +1,447 @@
1
+ require 'test_helper'
2
+
3
+ class AuthenticationSanityTest < ActionController::IntegrationTest
4
+ test 'home should be accessible without sign in' do
5
+ visit '/'
6
+ assert_response :success
7
+ assert_template 'home/index'
8
+ end
9
+
10
+ test 'sign in as user should not authenticate admin scope' do
11
+ sign_in_as_user
12
+ assert warden.authenticated?(:user)
13
+ assert_not warden.authenticated?(:admin)
14
+ end
15
+
16
+ test 'sign in as admin should not authenticate user scope' do
17
+ sign_in_as_admin
18
+ assert warden.authenticated?(:admin)
19
+ assert_not warden.authenticated?(:user)
20
+ end
21
+
22
+ test 'sign in as both user and admin at same time' do
23
+ sign_in_as_user
24
+ sign_in_as_admin
25
+ assert warden.authenticated?(:user)
26
+ assert warden.authenticated?(:admin)
27
+ end
28
+
29
+ test 'sign out as user should not touch admin authentication if sign_out_all_scopes is false' do
30
+ swap Devise, :sign_out_all_scopes => false do
31
+ sign_in_as_user
32
+ sign_in_as_admin
33
+ get destroy_user_session_path
34
+ assert_not warden.authenticated?(:user)
35
+ assert warden.authenticated?(:admin)
36
+ end
37
+ end
38
+
39
+ test 'sign out as admin should not touch user authentication if sign_out_all_scopes is false' do
40
+ swap Devise, :sign_out_all_scopes => false do
41
+ sign_in_as_user
42
+ sign_in_as_admin
43
+
44
+ get destroy_admin_session_path
45
+ assert_not warden.authenticated?(:admin)
46
+ assert warden.authenticated?(:user)
47
+ end
48
+ end
49
+
50
+ test 'sign out as user should also sign out admin if sign_out_all_scopes is true' do
51
+ swap Devise, :sign_out_all_scopes => true do
52
+ sign_in_as_user
53
+ sign_in_as_admin
54
+
55
+ get destroy_user_session_path
56
+ assert_not warden.authenticated?(:user)
57
+ assert_not warden.authenticated?(:admin)
58
+ end
59
+ end
60
+
61
+ test 'sign out as admin should also sign out user if sign_out_all_scopes is true' do
62
+ swap Devise, :sign_out_all_scopes => true do
63
+ sign_in_as_user
64
+ sign_in_as_admin
65
+
66
+ get destroy_admin_session_path
67
+ assert_not warden.authenticated?(:admin)
68
+ assert_not warden.authenticated?(:user)
69
+ end
70
+ end
71
+
72
+ test 'not signed in as admin should not be able to access admins actions' do
73
+ get admins_path
74
+ assert_redirected_to new_admin_session_path
75
+ assert_not warden.authenticated?(:admin)
76
+ end
77
+
78
+ test 'not signed in as admin should not be able to access private route restricted to admins' do
79
+ get private_path
80
+ assert_redirected_to new_admin_session_path
81
+ assert_not warden.authenticated?(:admin)
82
+ end
83
+
84
+ test 'signed in as user should not be able to access private route restricted to admins' do
85
+ sign_in_as_user
86
+ assert warden.authenticated?(:user)
87
+ assert_not warden.authenticated?(:admin)
88
+ get private_path
89
+ assert_redirected_to new_admin_session_path
90
+ end
91
+
92
+ test 'signed in as admin should be able to access private route restricted to admins' do
93
+ sign_in_as_admin
94
+ assert warden.authenticated?(:admin)
95
+ assert_not warden.authenticated?(:user)
96
+
97
+ get private_path
98
+
99
+ assert_response :success
100
+ assert_template 'home/private'
101
+ assert_contain 'Private!'
102
+ end
103
+
104
+ test 'signed in as user should not be able to access admins actions' do
105
+ sign_in_as_user
106
+ assert warden.authenticated?(:user)
107
+ assert_not warden.authenticated?(:admin)
108
+
109
+ get admins_path
110
+ assert_redirected_to new_admin_session_path
111
+ end
112
+
113
+ test 'signed in as admin should be able to access admin actions' do
114
+ sign_in_as_admin
115
+ assert warden.authenticated?(:admin)
116
+ assert_not warden.authenticated?(:user)
117
+
118
+ get admins_path
119
+
120
+ assert_response :success
121
+ assert_template 'admins/index'
122
+ assert_contain 'Welcome Admin'
123
+ end
124
+
125
+ test 'authenticated admin should not be able to sign as admin again' do
126
+ sign_in_as_admin
127
+ get new_admin_session_path
128
+
129
+ assert_response :redirect
130
+ assert_redirected_to admin_root_path
131
+ assert warden.authenticated?(:admin)
132
+ end
133
+
134
+ test 'authenticated admin should be able to sign out' do
135
+ sign_in_as_admin
136
+ assert warden.authenticated?(:admin)
137
+
138
+ get destroy_admin_session_path
139
+ assert_response :redirect
140
+ assert_redirected_to root_path
141
+
142
+ get root_path
143
+ assert_contain 'Signed out successfully'
144
+ assert_not warden.authenticated?(:admin)
145
+ end
146
+
147
+ test 'unauthenticated admin does not set message on sign out' do
148
+ get destroy_admin_session_path
149
+ assert_response :redirect
150
+ assert_redirected_to root_path
151
+
152
+ get root_path
153
+ assert_not_contain 'Signed out successfully'
154
+ end
155
+ end
156
+
157
+ class AuthenticationRedirectTest < ActionController::IntegrationTest
158
+ test 'redirect from warden shows sign in or sign up message' do
159
+ get admins_path
160
+
161
+ warden_path = new_admin_session_path
162
+ assert_redirected_to warden_path
163
+
164
+ get warden_path
165
+ assert_contain 'You need to sign in or sign up before continuing.'
166
+ end
167
+
168
+ test 'redirect to default url if no other was configured' do
169
+ sign_in_as_user
170
+ assert_template 'home/index'
171
+ assert_nil session[:"user_return_to"]
172
+ end
173
+
174
+ test 'redirect to requested url after sign in' do
175
+ get users_path
176
+ assert_redirected_to new_user_session_path
177
+ assert_equal users_path, session[:"user_return_to"]
178
+
179
+ follow_redirect!
180
+ sign_in_as_user :visit => false
181
+
182
+ assert_current_url '/users'
183
+ assert_nil session[:"user_return_to"]
184
+ end
185
+
186
+ test 'redirect to last requested url overwriting the stored return_to option' do
187
+ get expire_user_path(create_user)
188
+ assert_redirected_to new_user_session_path
189
+ assert_equal expire_user_path(create_user), session[:"user_return_to"]
190
+
191
+ get users_path
192
+ assert_redirected_to new_user_session_path
193
+ assert_equal users_path, session[:"user_return_to"]
194
+
195
+ follow_redirect!
196
+ sign_in_as_user :visit => false
197
+
198
+ assert_current_url '/users'
199
+ assert_nil session[:"user_return_to"]
200
+ end
201
+
202
+ test 'xml http requests does not store urls for redirect' do
203
+ get users_path, {}, 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest'
204
+ assert_equal 401, response.status
205
+ assert_nil session[:"user_return_to"]
206
+ end
207
+
208
+ test 'sign in with xml format returns xml response' do
209
+ create_user
210
+ post user_session_path(:format => 'xml', :user => {:email => "user@test.com", :password => '123456'})
211
+ assert_response :success
212
+ assert_match /<\?xml version="1.0" encoding="UTF-8"\?>/, response.body
213
+ end
214
+
215
+ test 'redirect to configured home path for a given scope after sign in' do
216
+ sign_in_as_admin
217
+ assert_equal "/admin_area/home", @request.path
218
+ end
219
+ end
220
+
221
+ class AuthenticationSessionTest < ActionController::IntegrationTest
222
+ test 'destroyed account is signed out' do
223
+ sign_in_as_user
224
+ get '/users'
225
+
226
+ User.destroy_all
227
+ get '/users'
228
+ assert_redirected_to new_user_session_path
229
+ end
230
+
231
+ test 'allows session to be set for a given scope' do
232
+ sign_in_as_user
233
+ get '/users'
234
+ assert_equal "Cart", @controller.user_session[:cart]
235
+ end
236
+
237
+ test 'does not explode when invalid user class is stored in session' do
238
+ klass = User
239
+ paths = ActiveSupport::Dependencies.autoload_paths.dup
240
+
241
+ begin
242
+ sign_in_as_user
243
+ assert warden.authenticated?(:user)
244
+
245
+ Object.send :remove_const, :User
246
+ ActiveSupport::Dependencies.autoload_paths.clear
247
+
248
+ visit "/users"
249
+ assert_not warden.authenticated?(:user)
250
+ ensure
251
+ Object.const_set(:User, klass)
252
+ ActiveSupport::Dependencies.autoload_paths.replace(paths)
253
+ end
254
+ end
255
+
256
+ test 'session id is changed on sign in' do
257
+ get '/users'
258
+ session_id = request.session["session_id"]
259
+
260
+ get '/users'
261
+ assert_equal session_id, request.session["session_id"]
262
+
263
+ sign_in_as_user
264
+ assert_not_equal session_id, request.session["session_id"]
265
+ end
266
+ end
267
+
268
+ class AuthenticationWithScopesTest < ActionController::IntegrationTest
269
+ test 'renders the scoped view if turned on and view is available' do
270
+ swap Devise, :scoped_views => true do
271
+ assert_raise Webrat::NotFoundError do
272
+ sign_in_as_user
273
+ end
274
+ assert_match /Special user view/, response.body
275
+ end
276
+ end
277
+
278
+ test 'renders the scoped view if turned on in an specific controller' do
279
+ begin
280
+ Devise::SessionsController.scoped_views = true
281
+ assert_raise Webrat::NotFoundError do
282
+ sign_in_as_user
283
+ end
284
+
285
+ assert_match /Special user view/, response.body
286
+ assert !Devise::PasswordsController.scoped_views?
287
+ ensure
288
+ Devise::SessionsController.send :remove_instance_variable, :@scoped_views
289
+ end
290
+ end
291
+
292
+ test 'does not render the scoped view if turned off' do
293
+ swap Devise, :scoped_views => false do
294
+ assert_nothing_raised do
295
+ sign_in_as_user
296
+ end
297
+ end
298
+ end
299
+
300
+ test 'does not render the scoped view if not available' do
301
+ swap Devise, :scoped_views => true do
302
+ assert_nothing_raised do
303
+ sign_in_as_admin
304
+ end
305
+ end
306
+ end
307
+ end
308
+
309
+ class AuthenticationOthersTest < ActionController::IntegrationTest
310
+ test 'uses the custom controller with the custom controller view' do
311
+ get '/admin_area/sign_in'
312
+ assert_contain 'Sign in'
313
+ assert_contain 'Welcome to "admins/sessions" controller!'
314
+ assert_contain 'Welcome to "sessions/new" view!'
315
+ end
316
+
317
+ test 'render 404 on roles without routes' do
318
+ get '/admin_area/password/new'
319
+ assert_equal 404, response.status
320
+ end
321
+
322
+ test 'does not intercept Rails 401 responses' do
323
+ get '/unauthenticated'
324
+ assert_equal 401, response.status
325
+ end
326
+
327
+ test 'render 404 on roles without mapping' do
328
+ assert_raise AbstractController::ActionNotFound do
329
+ get '/sign_in'
330
+ end
331
+ end
332
+
333
+ test 'sign in with script name' do
334
+ assert_nothing_raised do
335
+ get new_user_session_path, {}, "SCRIPT_NAME" => "/omg"
336
+ fill_in "email", :with => "user@test.com"
337
+ end
338
+ end
339
+
340
+ test 'registration in xml format works when recognizing path' do
341
+ assert_nothing_raised do
342
+ post user_registration_path(:format => 'xml', :user => {:email => "test@example.com", :password => "invalid"} )
343
+ end
344
+ end
345
+
346
+ test 'uses the mapping from router' do
347
+ sign_in_as_user :visit => "/as/sign_in"
348
+ assert warden.authenticated?(:user)
349
+ assert_not warden.authenticated?(:admin)
350
+ end
351
+
352
+ test 'uses the mapping from nested devise_for call' do
353
+ sign_in_as_user :visit => "/devise_for/sign_in"
354
+ assert warden.authenticated?(:user)
355
+ assert_not warden.authenticated?(:admin)
356
+ end
357
+ end
358
+
359
+ class AuthenticationRequestKeysTest < ActionController::IntegrationTest
360
+ test 'request keys are used on authentication' do
361
+ host! 'foo.bar.baz'
362
+
363
+ swap Devise, :request_keys => [:subdomain] do
364
+ User.expects(:find_for_authentication).with(:subdomain => 'foo', :email => 'user@test.com').returns(create_user)
365
+ sign_in_as_user
366
+ assert warden.authenticated?(:user)
367
+ end
368
+ end
369
+
370
+ test 'invalid request keys raises NoMethodError' do
371
+ swap Devise, :request_keys => [:unknown_method] do
372
+ assert_raise NoMethodError do
373
+ sign_in_as_user
374
+ end
375
+
376
+ assert_not warden.authenticated?(:user)
377
+ end
378
+ end
379
+
380
+ test 'blank request keys cause authentication to abort' do
381
+ host! 'test.com'
382
+
383
+ swap Devise, :request_keys => [:subdomain] do
384
+ sign_in_as_user
385
+ assert_contain "Invalid email or password."
386
+ assert_not warden.authenticated?(:user)
387
+ end
388
+ end
389
+
390
+ test 'blank request keys cause authentication to abort unless if marked as not required' do
391
+ host! 'test.com'
392
+
393
+ swap Devise, :request_keys => { :subdomain => false } do
394
+ sign_in_as_user
395
+ assert warden.authenticated?(:user)
396
+ end
397
+ end
398
+ end
399
+
400
+ class AuthenticationSignOutViaTest < ActionController::IntegrationTest
401
+ def sign_in!(scope)
402
+ sign_in_as_admin(:visit => send("new_#{scope}_session_path"))
403
+ assert warden.authenticated?(scope)
404
+ end
405
+
406
+ test 'allow sign out via delete when sign_out_via provides only delete' do
407
+ sign_in!(:sign_out_via_delete)
408
+ delete destroy_sign_out_via_delete_session_path
409
+ assert_not warden.authenticated?(:sign_out_via_delete)
410
+ end
411
+
412
+ test 'do not allow sign out via get when sign_out_via provides only delete' do
413
+ sign_in!(:sign_out_via_delete)
414
+ get destroy_sign_out_via_delete_session_path
415
+ assert warden.authenticated?(:sign_out_via_delete)
416
+ end
417
+
418
+ test 'allow sign out via post when sign_out_via provides only post' do
419
+ sign_in!(:sign_out_via_post)
420
+ post destroy_sign_out_via_post_session_path
421
+ assert_not warden.authenticated?(:sign_out_via_post)
422
+ end
423
+
424
+ test 'do not allow sign out via get when sign_out_via provides only post' do
425
+ sign_in!(:sign_out_via_post)
426
+ get destroy_sign_out_via_delete_session_path
427
+ assert warden.authenticated?(:sign_out_via_post)
428
+ end
429
+
430
+ test 'allow sign out via delete when sign_out_via provides delete and post' do
431
+ sign_in!(:sign_out_via_delete_or_post)
432
+ delete destroy_sign_out_via_delete_or_post_session_path
433
+ assert_not warden.authenticated?(:sign_out_via_delete_or_post)
434
+ end
435
+
436
+ test 'allow sign out via post when sign_out_via provides delete and post' do
437
+ sign_in!(:sign_out_via_delete_or_post)
438
+ post destroy_sign_out_via_delete_or_post_session_path
439
+ assert_not warden.authenticated?(:sign_out_via_delete_or_post)
440
+ end
441
+
442
+ test 'do not allow sign out via get when sign_out_via provides delete and post' do
443
+ sign_in!(:sign_out_via_delete_or_post)
444
+ get destroy_sign_out_via_delete_or_post_session_path
445
+ assert warden.authenticated?(:sign_out_via_delete_or_post)
446
+ end
447
+ end
@@ -0,0 +1,104 @@
1
+ require 'test_helper'
2
+
3
+ class ConfirmationTest < ActionController::IntegrationTest
4
+
5
+ def visit_user_confirmation_with_token(confirmation_token)
6
+ visit user_confirmation_path(:confirmation_token => confirmation_token)
7
+ end
8
+
9
+ test 'user should be able to request a new confirmation' do
10
+ user = create_user(:confirm => false)
11
+ ActionMailer::Base.deliveries.clear
12
+
13
+ visit new_user_session_path
14
+ click_link "Didn't receive confirmation instructions?"
15
+
16
+ fill_in 'email', :with => user.email
17
+ click_button 'Resend confirmation instructions'
18
+
19
+ assert_current_url '/users/sign_in'
20
+ assert_contain 'You will receive an email with instructions about how to confirm your account in a few minutes'
21
+ assert_equal 1, ActionMailer::Base.deliveries.size
22
+ end
23
+
24
+ test 'user with invalid confirmation token should not be able to confirm an account' do
25
+ visit_user_confirmation_with_token('invalid_confirmation')
26
+ assert_have_selector '#error_explanation'
27
+ assert_contain /Confirmation token(.*)invalid/
28
+ end
29
+
30
+ test 'user with valid confirmation token should be able to confirm an account' do
31
+ user = create_user(:confirm => false)
32
+ assert_not user.confirmed?
33
+ visit_user_confirmation_with_token(user.confirmation_token)
34
+
35
+ assert_contain 'Your account was successfully confirmed.'
36
+ assert_current_url '/'
37
+ assert user.reload.confirmed?
38
+ end
39
+
40
+ test 'already confirmed user should not be able to confirm the account again' do
41
+ user = create_user(:confirm => false)
42
+ user.confirmed_at = Time.now
43
+ user.save
44
+ visit_user_confirmation_with_token(user.confirmation_token)
45
+
46
+ assert_have_selector '#error_explanation'
47
+ assert_contain 'already confirmed'
48
+ end
49
+
50
+ test 'already confirmed user should not be able to confirm the account again neither request confirmation' do
51
+ user = create_user(:confirm => false)
52
+ user.confirmed_at = Time.now
53
+ user.save
54
+
55
+ visit_user_confirmation_with_token(user.confirmation_token)
56
+ assert_contain 'already confirmed'
57
+
58
+ fill_in 'email', :with => user.email
59
+ click_button 'Resend confirmation instructions'
60
+ assert_contain 'already confirmed'
61
+ end
62
+
63
+ test 'sign in user automatically after confirming it\'s email' do
64
+ user = create_user(:confirm => false)
65
+ visit_user_confirmation_with_token(user.confirmation_token)
66
+
67
+ assert warden.authenticated?(:user)
68
+ end
69
+
70
+ test 'increases sign count when signed in through confirmation' do
71
+ user = create_user(:confirm => false)
72
+ visit_user_confirmation_with_token(user.confirmation_token)
73
+
74
+ user.reload
75
+ assert_equal 1, user.sign_in_count
76
+ end
77
+
78
+ test 'not confirmed user with setup to block without confirmation should not be able to sign in' do
79
+ swap Devise, :confirm_within => 0.days do
80
+ sign_in_as_user(:confirm => false)
81
+
82
+ assert_contain 'You have to confirm your account before continuing'
83
+ assert_not warden.authenticated?(:user)
84
+ end
85
+ end
86
+
87
+ test 'not confirmed user but configured with some days to confirm should be able to sign in' do
88
+ swap Devise, :confirm_within => 1.day do
89
+ sign_in_as_user(:confirm => false)
90
+
91
+ assert_response :success
92
+ assert warden.authenticated?(:user)
93
+ end
94
+ end
95
+
96
+ test 'error message is configurable by resource name' do
97
+ store_translations :en, :devise => {
98
+ :failure => { :user => { :unconfirmed => "Not confirmed user" } }
99
+ } do
100
+ sign_in_as_user(:confirm => false)
101
+ assert_contain 'Not confirmed user'
102
+ end
103
+ end
104
+ end
@@ -0,0 +1,60 @@
1
+ require 'test_helper'
2
+
3
+ class DatabaseAuthenticationTest < ActionController::IntegrationTest
4
+ test 'sign in with email of different case should succeed when email is in the list of case insensitive keys' do
5
+ create_user(:email => 'Foo@Bar.com')
6
+
7
+ sign_in_as_user do
8
+ fill_in 'email', :with => 'foo@bar.com'
9
+ end
10
+
11
+ assert warden.authenticated?(:user)
12
+ end
13
+
14
+ test 'sign in with email of different case should fail when email is NOT the list of case insensitive keys' do
15
+ swap Devise, :case_insensitive_keys => [] do
16
+ create_user(:email => 'Foo@Bar.com')
17
+
18
+ sign_in_as_user do
19
+ fill_in 'email', :with => 'foo@bar.com'
20
+ end
21
+
22
+ assert_not warden.authenticated?(:user)
23
+ end
24
+ end
25
+
26
+ test 'sign in should not authenticate if not using proper authentication keys' do
27
+ swap Devise, :authentication_keys => [:username] do
28
+ sign_in_as_user
29
+ assert_not warden.authenticated?(:user)
30
+ end
31
+ end
32
+
33
+ test 'sign in with invalid email should return to sign in form with error message' do
34
+ sign_in_as_admin do
35
+ fill_in 'email', :with => 'wrongemail@test.com'
36
+ end
37
+
38
+ assert_contain 'Invalid email or password'
39
+ assert_not warden.authenticated?(:admin)
40
+ end
41
+
42
+ test 'sign in with invalid pasword should return to sign in form with error message' do
43
+ sign_in_as_admin do
44
+ fill_in 'password', :with => 'abcdef'
45
+ end
46
+
47
+ assert_contain 'Invalid email or password'
48
+ assert_not warden.authenticated?(:admin)
49
+ end
50
+
51
+ test 'error message is configurable by resource name' do
52
+ store_translations :en, :devise => { :failure => { :admin => { :invalid => "Invalid credentials" } } } do
53
+ sign_in_as_admin do
54
+ fill_in 'password', :with => 'abcdef'
55
+ end
56
+
57
+ assert_contain 'Invalid credentials'
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,74 @@
1
+ require 'test_helper'
2
+
3
+ class HttpAuthenticationTest < ActionController::IntegrationTest
4
+
5
+ test 'sign in should authenticate with http' do
6
+ sign_in_as_new_user_with_http
7
+ assert_response :success
8
+ assert_match '<email>user@test.com</email>', response.body
9
+ assert warden.authenticated?(:user)
10
+ end
11
+
12
+ test 'returns a custom response with www-authenticate header on failures' do
13
+ sign_in_as_new_user_with_http("unknown")
14
+ assert_equal 401, status
15
+ assert_equal 'Basic realm="Application"', headers["WWW-Authenticate"]
16
+ end
17
+
18
+ test 'uses the request format as response content type' do
19
+ sign_in_as_new_user_with_http("unknown")
20
+ assert_equal 401, status
21
+ assert_equal "application/xml; charset=utf-8", headers["Content-Type"]
22
+ assert_match "<error>Invalid email or password.</error>", response.body
23
+ end
24
+
25
+ test 'returns a custom response with www-authenticate and chosen realm' do
26
+ swap Devise, :http_authentication_realm => "MyApp" do
27
+ sign_in_as_new_user_with_http("unknown")
28
+ assert_equal 401, status
29
+ assert_equal 'Basic realm="MyApp"', headers["WWW-Authenticate"]
30
+ end
31
+ end
32
+
33
+ test 'sign in should authenticate with http even with specific authentication keys' do
34
+ swap Devise, :authentication_keys => [:username] do
35
+ sign_in_as_new_user_with_http("usertest")
36
+ assert_response :success
37
+ assert_match '<email>user@test.com</email>', response.body
38
+ assert warden.authenticated?(:user)
39
+ end
40
+ end
41
+
42
+ test 'test request with oauth2 header doesnt get mistaken for basic authentication' do
43
+ swap Devise, :http_authenticatable => true do
44
+ add_oauth2_header
45
+ assert_equal 401, status
46
+ assert_equal 'Basic realm="Application"', headers["WWW-Authenticate"]
47
+ end
48
+ end
49
+
50
+ test 'sign in should authenticate with really long token' do
51
+ token = "token_containing_so_many_characters_that_the_base64_encoding_will_wrap"
52
+ user = create_user
53
+ user.update_attribute :authentication_token, token
54
+ get users_path(:format => :xml), {}, "HTTP_AUTHORIZATION" => "Basic #{ActiveSupport::Base64.encode64("#{token}:x")}"
55
+ assert_response :success
56
+ assert_match "<email>user@test.com</email>", response.body
57
+ assert warden.authenticated?(:user)
58
+ end
59
+
60
+ private
61
+
62
+ def sign_in_as_new_user_with_http(username="user@test.com", password="123456")
63
+ user = create_user
64
+ get users_path(:format => :xml), {}, "HTTP_AUTHORIZATION" => "Basic #{ActiveSupport::Base64.encode64("#{username}:#{password}")}"
65
+ user
66
+ end
67
+
68
+ # Sign in with oauth2 token. This is just to test that it isn't misinterpreted as basic authentication
69
+ def add_oauth2_header
70
+ user = create_user
71
+ get users_path(:format => :xml), {}, "HTTP_AUTHORIZATION" => "OAuth #{ActiveSupport::Base64.encode64("#{user.email}:123456")}"
72
+ end
73
+
74
+ end