loyal_devise 2.1.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (208) hide show
  1. data/.gitignore +10 -0
  2. data/.travis.yml +15 -0
  3. data/CHANGELOG.rdoc +881 -0
  4. data/CONTRIBUTING.md +12 -0
  5. data/Gemfile +31 -0
  6. data/Gemfile.lock +154 -0
  7. data/MIT-LICENSE +20 -0
  8. data/README.md +388 -0
  9. data/Rakefile +34 -0
  10. data/app/controllers/devise/confirmations_controller.rb +44 -0
  11. data/app/controllers/devise/omniauth_callbacks_controller.rb +31 -0
  12. data/app/controllers/devise/passwords_controller.rb +57 -0
  13. data/app/controllers/devise/registrations_controller.rb +120 -0
  14. data/app/controllers/devise/sessions_controller.rb +51 -0
  15. data/app/controllers/devise/unlocks_controller.rb +45 -0
  16. data/app/controllers/devise_controller.rb +193 -0
  17. data/app/helpers/devise_helper.rb +26 -0
  18. data/app/mailers/devise/mailer.rb +16 -0
  19. data/app/views/devise/_links.erb +3 -0
  20. data/app/views/devise/confirmations/new.html.erb +12 -0
  21. data/app/views/devise/mailer/confirmation_instructions.html.erb +5 -0
  22. data/app/views/devise/mailer/reset_password_instructions.html.erb +8 -0
  23. data/app/views/devise/mailer/unlock_instructions.html.erb +7 -0
  24. data/app/views/devise/passwords/edit.html.erb +16 -0
  25. data/app/views/devise/passwords/new.html.erb +12 -0
  26. data/app/views/devise/registrations/edit.html.erb +25 -0
  27. data/app/views/devise/registrations/new.html.erb +18 -0
  28. data/app/views/devise/sessions/new.html.erb +17 -0
  29. data/app/views/devise/shared/_links.erb +25 -0
  30. data/app/views/devise/unlocks/new.html.erb +12 -0
  31. data/config/locales/en.yml +59 -0
  32. data/devise.gemspec +26 -0
  33. data/gemfiles/Gemfile.rails-3.1.x +35 -0
  34. data/gemfiles/Gemfile.rails-3.1.x.lock +167 -0
  35. data/lib/devise/controllers/helpers.rb +273 -0
  36. data/lib/devise/controllers/rememberable.rb +53 -0
  37. data/lib/devise/controllers/scoped_views.rb +18 -0
  38. data/lib/devise/controllers/url_helpers.rb +68 -0
  39. data/lib/devise/delegator.rb +17 -0
  40. data/lib/devise/failure_app.rb +188 -0
  41. data/lib/devise/hooks/activatable.rb +12 -0
  42. data/lib/devise/hooks/forgetable.rb +10 -0
  43. data/lib/devise/hooks/lockable.rb +8 -0
  44. data/lib/devise/hooks/rememberable.rb +7 -0
  45. data/lib/devise/hooks/timeoutable.rb +26 -0
  46. data/lib/devise/hooks/trackable.rb +10 -0
  47. data/lib/devise/mailers/helpers.rb +92 -0
  48. data/lib/devise/mapping.rb +173 -0
  49. data/lib/devise/models/authenticatable.rb +269 -0
  50. data/lib/devise/models/confirmable.rb +271 -0
  51. data/lib/devise/models/database_authenticatable.rb +127 -0
  52. data/lib/devise/models/lockable.rb +194 -0
  53. data/lib/devise/models/omniauthable.rb +28 -0
  54. data/lib/devise/models/recoverable.rb +141 -0
  55. data/lib/devise/models/registerable.rb +26 -0
  56. data/lib/devise/models/rememberable.rb +126 -0
  57. data/lib/devise/models/timeoutable.rb +50 -0
  58. data/lib/devise/models/token_authenticatable.rb +90 -0
  59. data/lib/devise/models/trackable.rb +36 -0
  60. data/lib/devise/models/validatable.rb +67 -0
  61. data/lib/devise/models.rb +129 -0
  62. data/lib/devise/modules.rb +30 -0
  63. data/lib/devise/omniauth/config.rb +46 -0
  64. data/lib/devise/omniauth/url_helpers.rb +19 -0
  65. data/lib/devise/omniauth.rb +29 -0
  66. data/lib/devise/orm/active_record.rb +4 -0
  67. data/lib/devise/orm/mongoid.rb +4 -0
  68. data/lib/devise/param_filter.rb +42 -0
  69. data/lib/devise/rails/routes.rb +447 -0
  70. data/lib/devise/rails/warden_compat.rb +44 -0
  71. data/lib/devise/rails.rb +55 -0
  72. data/lib/devise/strategies/authenticatable.rb +177 -0
  73. data/lib/devise/strategies/base.rb +21 -0
  74. data/lib/devise/strategies/database_authenticatable.rb +21 -0
  75. data/lib/devise/strategies/rememberable.rb +56 -0
  76. data/lib/devise/strategies/token_authenticatable.rb +57 -0
  77. data/lib/devise/test_helpers.rb +132 -0
  78. data/lib/devise/time_inflector.rb +15 -0
  79. data/lib/devise/version.rb +4 -0
  80. data/lib/devise.rb +445 -0
  81. data/lib/generators/active_record/devise_generator.rb +80 -0
  82. data/lib/generators/active_record/templates/migration.rb +20 -0
  83. data/lib/generators/active_record/templates/migration_existing.rb +27 -0
  84. data/lib/generators/devise/devise_generator.rb +25 -0
  85. data/lib/generators/devise/install_generator.rb +25 -0
  86. data/lib/generators/devise/orm_helpers.rb +33 -0
  87. data/lib/generators/devise/views_generator.rb +117 -0
  88. data/lib/generators/mongoid/devise_generator.rb +58 -0
  89. data/lib/generators/templates/README +35 -0
  90. data/lib/generators/templates/devise.rb +241 -0
  91. data/lib/generators/templates/markerb/confirmation_instructions.markerb +5 -0
  92. data/lib/generators/templates/markerb/reset_password_instructions.markerb +8 -0
  93. data/lib/generators/templates/markerb/unlock_instructions.markerb +7 -0
  94. data/lib/generators/templates/simple_form_for/confirmations/new.html.erb +15 -0
  95. data/lib/generators/templates/simple_form_for/passwords/edit.html.erb +19 -0
  96. data/lib/generators/templates/simple_form_for/passwords/new.html.erb +15 -0
  97. data/lib/generators/templates/simple_form_for/registrations/edit.html.erb +22 -0
  98. data/lib/generators/templates/simple_form_for/registrations/new.html.erb +17 -0
  99. data/lib/generators/templates/simple_form_for/sessions/new.html.erb +15 -0
  100. data/lib/generators/templates/simple_form_for/unlocks/new.html.erb +15 -0
  101. data/test/controllers/custom_strategy_test.rb +63 -0
  102. data/test/controllers/helpers_test.rb +254 -0
  103. data/test/controllers/internal_helpers_test.rb +111 -0
  104. data/test/controllers/sessions_controller_test.rb +58 -0
  105. data/test/controllers/url_helpers_test.rb +60 -0
  106. data/test/delegator_test.rb +20 -0
  107. data/test/devise_test.rb +73 -0
  108. data/test/failure_app_test.rb +222 -0
  109. data/test/generators/active_record_generator_test.rb +76 -0
  110. data/test/generators/devise_generator_test.rb +40 -0
  111. data/test/generators/install_generator_test.rb +14 -0
  112. data/test/generators/mongoid_generator_test.rb +24 -0
  113. data/test/generators/views_generator_test.rb +53 -0
  114. data/test/helpers/devise_helper_test.rb +52 -0
  115. data/test/indifferent_hash.rb +34 -0
  116. data/test/integration/authenticatable_test.rb +634 -0
  117. data/test/integration/confirmable_test.rb +299 -0
  118. data/test/integration/database_authenticatable_test.rb +83 -0
  119. data/test/integration/http_authenticatable_test.rb +98 -0
  120. data/test/integration/lockable_test.rb +243 -0
  121. data/test/integration/omniauthable_test.rb +134 -0
  122. data/test/integration/recoverable_test.rb +307 -0
  123. data/test/integration/registerable_test.rb +346 -0
  124. data/test/integration/rememberable_test.rb +159 -0
  125. data/test/integration/timeoutable_test.rb +141 -0
  126. data/test/integration/token_authenticatable_test.rb +162 -0
  127. data/test/integration/trackable_test.rb +93 -0
  128. data/test/mailers/confirmation_instructions_test.rb +103 -0
  129. data/test/mailers/reset_password_instructions_test.rb +84 -0
  130. data/test/mailers/unlock_instructions_test.rb +78 -0
  131. data/test/mapping_test.rb +128 -0
  132. data/test/models/authenticatable_test.rb +8 -0
  133. data/test/models/confirmable_test.rb +392 -0
  134. data/test/models/database_authenticatable_test.rb +190 -0
  135. data/test/models/lockable_test.rb +274 -0
  136. data/test/models/omniauthable_test.rb +8 -0
  137. data/test/models/recoverable_test.rb +206 -0
  138. data/test/models/registerable_test.rb +8 -0
  139. data/test/models/rememberable_test.rb +175 -0
  140. data/test/models/serializable_test.rb +49 -0
  141. data/test/models/timeoutable_test.rb +47 -0
  142. data/test/models/token_authenticatable_test.rb +56 -0
  143. data/test/models/trackable_test.rb +14 -0
  144. data/test/models/validatable_test.rb +117 -0
  145. data/test/models_test.rb +180 -0
  146. data/test/omniauth/config_test.rb +58 -0
  147. data/test/omniauth/url_helpers_test.rb +52 -0
  148. data/test/orm/active_record.rb +10 -0
  149. data/test/orm/mongoid.rb +15 -0
  150. data/test/rails_app/Rakefile +10 -0
  151. data/test/rails_app/app/active_record/admin.rb +7 -0
  152. data/test/rails_app/app/active_record/shim.rb +3 -0
  153. data/test/rails_app/app/active_record/user.rb +7 -0
  154. data/test/rails_app/app/controllers/admins/sessions_controller.rb +7 -0
  155. data/test/rails_app/app/controllers/admins_controller.rb +12 -0
  156. data/test/rails_app/app/controllers/application_controller.rb +9 -0
  157. data/test/rails_app/app/controllers/home_controller.rb +26 -0
  158. data/test/rails_app/app/controllers/publisher/registrations_controller.rb +3 -0
  159. data/test/rails_app/app/controllers/publisher/sessions_controller.rb +3 -0
  160. data/test/rails_app/app/controllers/users/omniauth_callbacks_controller.rb +15 -0
  161. data/test/rails_app/app/controllers/users_controller.rb +24 -0
  162. data/test/rails_app/app/helpers/application_helper.rb +4 -0
  163. data/test/rails_app/app/mailers/users/mailer.rb +9 -0
  164. data/test/rails_app/app/mongoid/admin.rb +28 -0
  165. data/test/rails_app/app/mongoid/shim.rb +25 -0
  166. data/test/rails_app/app/mongoid/user.rb +43 -0
  167. data/test/rails_app/app/views/admins/index.html.erb +1 -0
  168. data/test/rails_app/app/views/admins/sessions/new.html.erb +2 -0
  169. data/test/rails_app/app/views/home/admin_dashboard.html.erb +1 -0
  170. data/test/rails_app/app/views/home/index.html.erb +1 -0
  171. data/test/rails_app/app/views/home/join.html.erb +1 -0
  172. data/test/rails_app/app/views/home/private.html.erb +1 -0
  173. data/test/rails_app/app/views/home/user_dashboard.html.erb +1 -0
  174. data/test/rails_app/app/views/layouts/application.html.erb +24 -0
  175. data/test/rails_app/app/views/users/index.html.erb +1 -0
  176. data/test/rails_app/app/views/users/mailer/confirmation_instructions.erb +1 -0
  177. data/test/rails_app/app/views/users/sessions/new.html.erb +1 -0
  178. data/test/rails_app/config/application.rb +42 -0
  179. data/test/rails_app/config/boot.rb +9 -0
  180. data/test/rails_app/config/database.yml +18 -0
  181. data/test/rails_app/config/environment.rb +6 -0
  182. data/test/rails_app/config/environments/development.rb +19 -0
  183. data/test/rails_app/config/environments/production.rb +34 -0
  184. data/test/rails_app/config/environments/test.rb +34 -0
  185. data/test/rails_app/config/initializers/backtrace_silencers.rb +8 -0
  186. data/test/rails_app/config/initializers/devise.rb +179 -0
  187. data/test/rails_app/config/initializers/inflections.rb +3 -0
  188. data/test/rails_app/config/initializers/secret_token.rb +3 -0
  189. data/test/rails_app/config/routes.rb +101 -0
  190. data/test/rails_app/config.ru +4 -0
  191. data/test/rails_app/db/migrate/20100401102949_create_tables.rb +75 -0
  192. data/test/rails_app/db/schema.rb +53 -0
  193. data/test/rails_app/lib/shared_admin.rb +15 -0
  194. data/test/rails_app/lib/shared_user.rb +27 -0
  195. data/test/rails_app/public/404.html +26 -0
  196. data/test/rails_app/public/422.html +26 -0
  197. data/test/rails_app/public/500.html +26 -0
  198. data/test/rails_app/public/favicon.ico +0 -0
  199. data/test/rails_app/script/rails +10 -0
  200. data/test/routes_test.rb +249 -0
  201. data/test/support/assertions.rb +41 -0
  202. data/test/support/helpers.rb +92 -0
  203. data/test/support/integration.rb +93 -0
  204. data/test/support/locale/en.yml +4 -0
  205. data/test/support/webrat/integrations/rails.rb +25 -0
  206. data/test/test_helper.rb +28 -0
  207. data/test/test_helpers_test.rb +152 -0
  208. metadata +407 -0
@@ -0,0 +1,634 @@
1
+ # -*- encoding : utf-8 -*-
2
+ require 'test_helper'
3
+
4
+ class AuthenticationSanityTest < ActionController::IntegrationTest
5
+ test 'home should be accessible without sign in' do
6
+ visit '/'
7
+ assert_response :success
8
+ assert_template 'home/index'
9
+ end
10
+
11
+ test 'sign in as user should not authenticate admin scope' do
12
+ sign_in_as_user
13
+ assert warden.authenticated?(:user)
14
+ assert_not warden.authenticated?(:admin)
15
+ end
16
+
17
+ test 'sign in as admin should not authenticate user scope' do
18
+ sign_in_as_admin
19
+ assert warden.authenticated?(:admin)
20
+ assert_not warden.authenticated?(:user)
21
+ end
22
+
23
+ test 'sign in as both user and admin at same time' do
24
+ sign_in_as_user
25
+ sign_in_as_admin
26
+ assert warden.authenticated?(:user)
27
+ assert warden.authenticated?(:admin)
28
+ end
29
+
30
+ test 'sign out as user should not touch admin authentication if sign_out_all_scopes is false' do
31
+ swap Devise, :sign_out_all_scopes => false do
32
+ sign_in_as_user
33
+ sign_in_as_admin
34
+ get destroy_user_session_path
35
+ assert_not warden.authenticated?(:user)
36
+ assert warden.authenticated?(:admin)
37
+ end
38
+ end
39
+
40
+ test 'sign out as admin should not touch user authentication if sign_out_all_scopes is false' do
41
+ swap Devise, :sign_out_all_scopes => false do
42
+ sign_in_as_user
43
+ sign_in_as_admin
44
+
45
+ get destroy_admin_session_path
46
+ assert_not warden.authenticated?(:admin)
47
+ assert warden.authenticated?(:user)
48
+ end
49
+ end
50
+
51
+ test 'sign out as user should also sign out admin if sign_out_all_scopes is true' do
52
+ swap Devise, :sign_out_all_scopes => true do
53
+ sign_in_as_user
54
+ sign_in_as_admin
55
+
56
+ get destroy_user_session_path
57
+ assert_not warden.authenticated?(:user)
58
+ assert_not warden.authenticated?(:admin)
59
+ end
60
+ end
61
+
62
+ test 'sign out as admin should also sign out user if sign_out_all_scopes is true' do
63
+ swap Devise, :sign_out_all_scopes => true do
64
+ sign_in_as_user
65
+ sign_in_as_admin
66
+
67
+ get destroy_admin_session_path
68
+ assert_not warden.authenticated?(:admin)
69
+ assert_not warden.authenticated?(:user)
70
+ end
71
+ end
72
+
73
+ test 'not signed in as admin should not be able to access admins actions' do
74
+ get admins_path
75
+ assert_redirected_to new_admin_session_path
76
+ assert_not warden.authenticated?(:admin)
77
+ end
78
+
79
+ test 'signed in as user should not be able to access admins actions' do
80
+ sign_in_as_user
81
+ assert warden.authenticated?(:user)
82
+ assert_not warden.authenticated?(:admin)
83
+
84
+ get admins_path
85
+ assert_redirected_to new_admin_session_path
86
+ end
87
+
88
+ test 'signed in as admin should be able to access admin actions' do
89
+ sign_in_as_admin
90
+ assert warden.authenticated?(:admin)
91
+ assert_not warden.authenticated?(:user)
92
+
93
+ get admins_path
94
+
95
+ assert_response :success
96
+ assert_template 'admins/index'
97
+ assert_contain 'Welcome Admin'
98
+ end
99
+
100
+ test 'authenticated admin should not be able to sign as admin again' do
101
+ sign_in_as_admin
102
+ get new_admin_session_path
103
+
104
+ assert_response :redirect
105
+ assert_redirected_to admin_root_path
106
+ assert warden.authenticated?(:admin)
107
+ end
108
+
109
+ test 'authenticated admin should be able to sign out' do
110
+ sign_in_as_admin
111
+ assert warden.authenticated?(:admin)
112
+
113
+ get destroy_admin_session_path
114
+ assert_response :redirect
115
+ assert_redirected_to root_path
116
+
117
+ get root_path
118
+ assert_contain 'Signed out successfully'
119
+ assert_not warden.authenticated?(:admin)
120
+ end
121
+
122
+ test 'unauthenticated admin does not set message on sign out' do
123
+ get destroy_admin_session_path
124
+ assert_response :redirect
125
+ assert_redirected_to root_path
126
+
127
+ get root_path
128
+ assert_not_contain 'Signed out successfully'
129
+ end
130
+
131
+ test 'scope uses custom failure app' do
132
+ put "/en/accounts/management"
133
+ assert_equal "Oops, not found", response.body
134
+ assert_equal 404, response.status
135
+ end
136
+ end
137
+
138
+ class AuthenticationRoutesRestrictions < ActionController::IntegrationTest
139
+ test 'not signed in should not be able to access private route (authenticate denied)' do
140
+ get private_path
141
+ assert_redirected_to new_admin_session_path
142
+ assert_not warden.authenticated?(:admin)
143
+ end
144
+
145
+ test 'signed in as user should not be able to access private route restricted to admins (authenticate denied)' do
146
+ sign_in_as_user
147
+ assert warden.authenticated?(:user)
148
+ assert_not warden.authenticated?(:admin)
149
+ get private_path
150
+ assert_redirected_to new_admin_session_path
151
+ end
152
+
153
+ test 'signed in as admin should be able to access private route restricted to admins (authenticate accepted)' do
154
+ sign_in_as_admin
155
+ assert warden.authenticated?(:admin)
156
+ assert_not warden.authenticated?(:user)
157
+
158
+ get private_path
159
+
160
+ assert_response :success
161
+ assert_template 'home/private'
162
+ assert_contain 'Private!'
163
+ end
164
+
165
+ test 'signed in as inactive admin should not be able to access private/active route restricted to active admins (authenticate denied)' do
166
+ sign_in_as_admin(:active => false)
167
+ assert warden.authenticated?(:admin)
168
+ assert_not warden.authenticated?(:user)
169
+
170
+ assert_raises ActionController::RoutingError do
171
+ get "/private/active"
172
+ end
173
+ end
174
+
175
+ test 'signed in as active admin should be able to access private/active route restricted to active admins (authenticate accepted)' do
176
+ sign_in_as_admin(:active => true)
177
+ assert warden.authenticated?(:admin)
178
+ assert_not warden.authenticated?(:user)
179
+
180
+ get private_active_path
181
+
182
+ assert_response :success
183
+ assert_template 'home/private'
184
+ assert_contain 'Private!'
185
+ end
186
+
187
+ test 'signed in as admin should get admin dashboard (authenticated accepted)' do
188
+ sign_in_as_admin
189
+ assert warden.authenticated?(:admin)
190
+ assert_not warden.authenticated?(:user)
191
+
192
+ get dashboard_path
193
+
194
+ assert_response :success
195
+ assert_template 'home/admin'
196
+ assert_contain 'Admin dashboard'
197
+ end
198
+
199
+ test 'signed in as user should get user dashboard (authenticated accepted)' do
200
+ sign_in_as_user
201
+ assert warden.authenticated?(:user)
202
+ assert_not warden.authenticated?(:admin)
203
+
204
+ get dashboard_path
205
+
206
+ assert_response :success
207
+ assert_template 'home/user'
208
+ assert_contain 'User dashboard'
209
+ end
210
+
211
+ test 'not signed in should get no dashboard (authenticated denied)' do
212
+ assert_raises ActionController::RoutingError do
213
+ get dashboard_path
214
+ end
215
+ end
216
+
217
+ test 'signed in as inactive admin should not be able to access dashboard/active route restricted to active admins (authenticated denied)' do
218
+ sign_in_as_admin(:active => false)
219
+ assert warden.authenticated?(:admin)
220
+ assert_not warden.authenticated?(:user)
221
+
222
+ assert_raises ActionController::RoutingError do
223
+ get "/dashboard/active"
224
+ end
225
+ end
226
+
227
+ test 'signed in as active admin should be able to access dashboard/active route restricted to active admins (authenticated accepted)' do
228
+ sign_in_as_admin(:active => true)
229
+ assert warden.authenticated?(:admin)
230
+ assert_not warden.authenticated?(:user)
231
+
232
+ get dashboard_active_path
233
+
234
+ assert_response :success
235
+ assert_template 'home/admin_dashboard'
236
+ assert_contain 'Admin dashboard'
237
+ end
238
+
239
+ test 'signed in user should not see unauthenticated page (unauthenticated denied)' do
240
+ sign_in_as_user
241
+ assert warden.authenticated?(:user)
242
+ assert_not warden.authenticated?(:admin)
243
+
244
+ assert_raises ActionController::RoutingError do
245
+ get join_path
246
+ end
247
+ end
248
+
249
+ test 'not signed in users should see unautheticated page (unauthenticated accepted)' do
250
+ get join_path
251
+
252
+ assert_response :success
253
+ assert_template 'home/join'
254
+ assert_contain 'Join'
255
+ end
256
+ end
257
+
258
+ class AuthenticationRedirectTest < ActionController::IntegrationTest
259
+ test 'redirect from warden shows sign in or sign up message' do
260
+ get admins_path
261
+
262
+ warden_path = new_admin_session_path
263
+ assert_redirected_to warden_path
264
+
265
+ get warden_path
266
+ assert_contain 'You need to sign in or sign up before continuing.'
267
+ end
268
+
269
+ test 'redirect to default url if no other was configured' do
270
+ sign_in_as_user
271
+ assert_template 'home/index'
272
+ assert_nil session[:"user_return_to"]
273
+ end
274
+
275
+ test 'redirect to requested url after sign in' do
276
+ get users_path
277
+ assert_redirected_to new_user_session_path
278
+ assert_equal users_path, session[:"user_return_to"]
279
+
280
+ follow_redirect!
281
+ sign_in_as_user :visit => false
282
+
283
+ assert_current_url '/users'
284
+ assert_nil session[:"user_return_to"]
285
+ end
286
+
287
+ test 'redirect to last requested url overwriting the stored return_to option' do
288
+ get expire_user_path(create_user)
289
+ assert_redirected_to new_user_session_path
290
+ assert_equal expire_user_path(create_user), session[:"user_return_to"]
291
+
292
+ get users_path
293
+ assert_redirected_to new_user_session_path
294
+ assert_equal users_path, session[:"user_return_to"]
295
+
296
+ follow_redirect!
297
+ sign_in_as_user :visit => false
298
+
299
+ assert_current_url '/users'
300
+ assert_nil session[:"user_return_to"]
301
+ end
302
+
303
+ test 'xml http requests does not store urls for redirect' do
304
+ get users_path, {}, 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest'
305
+ assert_equal 401, response.status
306
+ assert_nil session[:"user_return_to"]
307
+ end
308
+
309
+ test 'redirect to configured home path for a given scope after sign in' do
310
+ sign_in_as_admin
311
+ assert_equal "/admin_area/home", @request.path
312
+ end
313
+
314
+ test 'require_no_authentication should set the already_authenticated flash message' do
315
+ sign_in_as_user
316
+ visit new_user_session_path
317
+ assert_equal flash[:alert], I18n.t("devise.failure.already_authenticated")
318
+ end
319
+ end
320
+
321
+ class AuthenticationSessionTest < ActionController::IntegrationTest
322
+ test 'destroyed account is signed out' do
323
+ sign_in_as_user
324
+ get '/users'
325
+
326
+ User.destroy_all
327
+ get '/users'
328
+ assert_redirected_to new_user_session_path
329
+ end
330
+
331
+ test 'allows session to be set for a given scope' do
332
+ sign_in_as_user
333
+ get '/users'
334
+ assert_equal "Cart", @controller.user_session[:cart]
335
+ end
336
+
337
+ test 'does not explode when invalid user class is stored in session' do
338
+ klass = User
339
+ paths = ActiveSupport::Dependencies.autoload_paths.dup
340
+
341
+ begin
342
+ sign_in_as_user
343
+ assert warden.authenticated?(:user)
344
+
345
+ Object.send :remove_const, :User
346
+ ActiveSupport::Dependencies.autoload_paths.clear
347
+
348
+ visit "/users"
349
+ assert_not warden.authenticated?(:user)
350
+ ensure
351
+ Object.const_set(:User, klass)
352
+ ActiveSupport::Dependencies.autoload_paths.replace(paths)
353
+ end
354
+ end
355
+
356
+ test 'session id is changed on sign in' do
357
+ get '/users'
358
+ session_id = request.session["session_id"]
359
+
360
+ get '/users'
361
+ assert_equal session_id, request.session["session_id"]
362
+
363
+ sign_in_as_user
364
+ assert_not_equal session_id, request.session["session_id"]
365
+ end
366
+ end
367
+
368
+ class AuthenticationWithScopedViewsTest < ActionController::IntegrationTest
369
+ test 'renders the scoped view if turned on and view is available' do
370
+ swap Devise, :scoped_views => true do
371
+ assert_raise Webrat::NotFoundError do
372
+ sign_in_as_user
373
+ end
374
+ assert_match /Special user view/, response.body
375
+ end
376
+ end
377
+
378
+ test 'renders the scoped view if turned on in an specific controller' do
379
+ begin
380
+ Devise::SessionsController.scoped_views = true
381
+ assert_raise Webrat::NotFoundError do
382
+ sign_in_as_user
383
+ end
384
+
385
+ assert_match /Special user view/, response.body
386
+ assert !Devise::PasswordsController.scoped_views?
387
+ ensure
388
+ Devise::SessionsController.send :remove_instance_variable, :@scoped_views
389
+ end
390
+ end
391
+
392
+ test 'does not render the scoped view if turned off' do
393
+ swap Devise, :scoped_views => false do
394
+ assert_nothing_raised do
395
+ sign_in_as_user
396
+ end
397
+ end
398
+ end
399
+
400
+ test 'does not render the scoped view if not available' do
401
+ swap Devise, :scoped_views => true do
402
+ assert_nothing_raised do
403
+ sign_in_as_admin
404
+ end
405
+ end
406
+ end
407
+ end
408
+
409
+ class AuthenticationOthersTest < ActionController::IntegrationTest
410
+ test 'handles unverified requests gets rid of caches' do
411
+ swap UsersController, :allow_forgery_protection => true do
412
+ post exhibit_user_url(1)
413
+ assert_not warden.authenticated?(:user)
414
+
415
+ sign_in_as_user
416
+ assert warden.authenticated?(:user)
417
+
418
+ post exhibit_user_url(1)
419
+ assert_not warden.authenticated?(:user)
420
+ assert_equal "User is not authenticated", response.body
421
+ end
422
+ end
423
+
424
+ test 'uses the custom controller with the custom controller view' do
425
+ get '/admin_area/sign_in'
426
+ assert_contain 'Sign in'
427
+ assert_contain 'Welcome to "admins/sessions" controller!'
428
+ assert_contain 'Welcome to "sessions/new" view!'
429
+ end
430
+
431
+ test 'render 404 on roles without routes' do
432
+ assert_raise ActionController::RoutingError do
433
+ get '/admin_area/password/new'
434
+ end
435
+ end
436
+
437
+ test 'does not intercept Rails 401 responses' do
438
+ get '/unauthenticated'
439
+ assert_equal 401, response.status
440
+ end
441
+
442
+ test 'render 404 on roles without mapping' do
443
+ assert_raise AbstractController::ActionNotFound do
444
+ get '/sign_in'
445
+ end
446
+ end
447
+
448
+ test 'sign in with script name' do
449
+ assert_nothing_raised do
450
+ get new_user_session_path, {}, "SCRIPT_NAME" => "/omg"
451
+ fill_in "email", :with => "user@test.com"
452
+ end
453
+ end
454
+
455
+ test 'sign in stub in xml format' do
456
+ get new_user_session_path(:format => 'xml')
457
+ assert_match '<?xml version="1.0" encoding="UTF-8"?>', response.body
458
+ assert_match /<user>.*<\/user>/m, response.body
459
+ assert_match '<email></email>', response.body
460
+ assert_match '<password nil="true"></password>', response.body
461
+ end
462
+
463
+ test 'sign in stub in json format' do
464
+ get new_user_session_path(:format => 'json')
465
+ assert_match '{"user":{', response.body
466
+ assert_match '"email":""', response.body
467
+ assert_match '"password":null', response.body
468
+ end
469
+
470
+ test 'sign in stub in json with non attribute key' do
471
+ swap Devise, :authentication_keys => [:other_key] do
472
+ get new_user_session_path(:format => 'json')
473
+ assert_match '{"user":{', response.body
474
+ assert_match '"other_key":null', response.body
475
+ assert_match '"password":null', response.body
476
+ end
477
+ end
478
+
479
+ test 'uses the mapping from router' do
480
+ sign_in_as_user :visit => "/as/sign_in"
481
+ assert warden.authenticated?(:user)
482
+ assert_not warden.authenticated?(:admin)
483
+ end
484
+
485
+ test 'sign in with xml format returns xml response' do
486
+ create_user
487
+ post user_session_path(:format => 'xml'), :user => {:email => "user@test.com", :password => '12345678'}
488
+ assert_response :success
489
+ assert response.body.include? %(<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<user>)
490
+ end
491
+
492
+ test 'sign in with xml format is idempotent' do
493
+ get new_user_session_path(:format => 'xml')
494
+ assert_response :success
495
+
496
+ create_user
497
+ post user_session_path(:format => 'xml'), :user => {:email => "user@test.com", :password => '12345678'}
498
+ assert_response :success
499
+
500
+ get new_user_session_path(:format => 'xml')
501
+ assert_response :success
502
+
503
+ post user_session_path(:format => 'xml'), :user => {:email => "user@test.com", :password => '12345678'}
504
+ assert_response :success
505
+ assert response.body.include? %(<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<user>)
506
+ end
507
+
508
+ test 'sign out with xml format returns ok response' do
509
+ sign_in_as_user
510
+ get destroy_user_session_path(:format => 'xml')
511
+ assert_response :no_content
512
+ assert_not warden.authenticated?(:user)
513
+ end
514
+
515
+ test 'sign out with json format returns empty json response' do
516
+ sign_in_as_user
517
+ get destroy_user_session_path(:format => 'json')
518
+ assert_response :no_content
519
+ assert_not warden.authenticated?(:user)
520
+ end
521
+ end
522
+
523
+ class AuthenticationKeysTest < ActionController::IntegrationTest
524
+ test 'missing authentication keys cause authentication to abort' do
525
+ swap Devise, :authentication_keys => [:subdomain] do
526
+ sign_in_as_user
527
+ assert_contain "Invalid email or password."
528
+ assert_not warden.authenticated?(:user)
529
+ end
530
+ end
531
+
532
+ test 'missing authentication keys cause authentication to abort unless marked as not required' do
533
+ swap Devise, :authentication_keys => { :email => true, :subdomain => false } do
534
+ sign_in_as_user
535
+ assert warden.authenticated?(:user)
536
+ end
537
+ end
538
+ end
539
+
540
+ class AuthenticationRequestKeysTest < ActionController::IntegrationTest
541
+ test 'request keys are used on authentication' do
542
+ host! 'foo.bar.baz'
543
+
544
+ swap Devise, :request_keys => [:subdomain] do
545
+ User.expects(:find_for_authentication).with(:subdomain => 'foo', :email => 'user@test.com').returns(create_user)
546
+ sign_in_as_user
547
+ assert warden.authenticated?(:user)
548
+ end
549
+ end
550
+
551
+ test 'invalid request keys raises NoMethodError' do
552
+ swap Devise, :request_keys => [:unknown_method] do
553
+ assert_raise NoMethodError do
554
+ sign_in_as_user
555
+ end
556
+
557
+ assert_not warden.authenticated?(:user)
558
+ end
559
+ end
560
+
561
+ test 'blank request keys cause authentication to abort' do
562
+ host! 'test.com'
563
+
564
+ swap Devise, :request_keys => [:subdomain] do
565
+ sign_in_as_user
566
+ assert_contain "Invalid email or password."
567
+ assert_not warden.authenticated?(:user)
568
+ end
569
+ end
570
+
571
+ test 'blank request keys cause authentication to abort unless if marked as not required' do
572
+ host! 'test.com'
573
+
574
+ swap Devise, :request_keys => { :subdomain => false } do
575
+ sign_in_as_user
576
+ assert warden.authenticated?(:user)
577
+ end
578
+ end
579
+ end
580
+
581
+ class AuthenticationSignOutViaTest < ActionController::IntegrationTest
582
+ def sign_in!(scope)
583
+ sign_in_as_admin(:visit => send("new_#{scope}_session_path"))
584
+ assert warden.authenticated?(scope)
585
+ end
586
+
587
+ test 'allow sign out via delete when sign_out_via provides only delete' do
588
+ sign_in!(:sign_out_via_delete)
589
+ delete destroy_sign_out_via_delete_session_path
590
+ assert_not warden.authenticated?(:sign_out_via_delete)
591
+ end
592
+
593
+ test 'do not allow sign out via get when sign_out_via provides only delete' do
594
+ sign_in!(:sign_out_via_delete)
595
+ assert_raise ActionController::RoutingError do
596
+ get destroy_sign_out_via_delete_session_path
597
+ end
598
+ assert warden.authenticated?(:sign_out_via_delete)
599
+ end
600
+
601
+ test 'allow sign out via post when sign_out_via provides only post' do
602
+ sign_in!(:sign_out_via_post)
603
+ post destroy_sign_out_via_post_session_path
604
+ assert_not warden.authenticated?(:sign_out_via_post)
605
+ end
606
+
607
+ test 'do not allow sign out via get when sign_out_via provides only post' do
608
+ sign_in!(:sign_out_via_post)
609
+ assert_raise ActionController::RoutingError do
610
+ get destroy_sign_out_via_delete_session_path
611
+ end
612
+ assert warden.authenticated?(:sign_out_via_post)
613
+ end
614
+
615
+ test 'allow sign out via delete when sign_out_via provides delete and post' do
616
+ sign_in!(:sign_out_via_delete_or_post)
617
+ delete destroy_sign_out_via_delete_or_post_session_path
618
+ assert_not warden.authenticated?(:sign_out_via_delete_or_post)
619
+ end
620
+
621
+ test 'allow sign out via post when sign_out_via provides delete and post' do
622
+ sign_in!(:sign_out_via_delete_or_post)
623
+ post destroy_sign_out_via_delete_or_post_session_path
624
+ assert_not warden.authenticated?(:sign_out_via_delete_or_post)
625
+ end
626
+
627
+ test 'do not allow sign out via get when sign_out_via provides delete and post' do
628
+ sign_in!(:sign_out_via_delete_or_post)
629
+ assert_raise ActionController::RoutingError do
630
+ get destroy_sign_out_via_delete_or_post_session_path
631
+ end
632
+ assert warden.authenticated?(:sign_out_via_delete_or_post)
633
+ end
634
+ end