loyal_devise 2.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
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,346 @@
1
+ # -*- encoding : utf-8 -*-
2
+ require 'test_helper'
3
+
4
+ class RegistrationTest < ActionController::IntegrationTest
5
+
6
+ test 'a guest admin should be able to sign in successfully' do
7
+ get new_admin_session_path
8
+ click_link 'Sign up'
9
+
10
+ assert_template 'registrations/new'
11
+
12
+ fill_in 'email', :with => 'new_user@test.com'
13
+ fill_in 'password', :with => 'new_user123'
14
+ fill_in 'password confirmation', :with => 'new_user123'
15
+ click_button 'Sign up'
16
+
17
+ assert_contain 'You have signed up successfully'
18
+ assert warden.authenticated?(:admin)
19
+ assert_current_url "/admin_area/home"
20
+
21
+ admin = Admin.last :order => "id"
22
+ assert_equal admin.email, 'new_user@test.com'
23
+ end
24
+
25
+ test 'a guest admin should be able to sign in and be redirected to a custom location' do
26
+ Devise::RegistrationsController.any_instance.stubs(:after_sign_up_path_for).returns("/?custom=1")
27
+ get new_admin_session_path
28
+ click_link 'Sign up'
29
+
30
+ fill_in 'email', :with => 'new_user@test.com'
31
+ fill_in 'password', :with => 'new_user123'
32
+ fill_in 'password confirmation', :with => 'new_user123'
33
+ click_button 'Sign up'
34
+
35
+ assert_contain 'Welcome! You have signed up successfully.'
36
+ assert warden.authenticated?(:admin)
37
+ assert_current_url "/?custom=1"
38
+ end
39
+
40
+ def user_sign_up
41
+ ActionMailer::Base.deliveries.clear
42
+
43
+ get new_user_registration_path
44
+
45
+ fill_in 'email', :with => 'new_user@test.com'
46
+ fill_in 'password', :with => 'new_user123'
47
+ fill_in 'password confirmation', :with => 'new_user123'
48
+ click_button 'Sign up'
49
+ end
50
+
51
+ test 'a guest user should be able to sign up successfully and be blocked by confirmation' do
52
+ user_sign_up
53
+
54
+ assert_contain 'A message with a confirmation link has been sent to your email address. Please open the link to activate your account.'
55
+ assert_not_contain 'You have to confirm your account before continuing'
56
+ assert_current_url "/"
57
+
58
+ assert_not warden.authenticated?(:user)
59
+
60
+ user = User.last :order => "id"
61
+ assert_equal user.email, 'new_user@test.com'
62
+ assert_not user.confirmed?
63
+ end
64
+
65
+ test 'a guest user should receive the confirmation instructions from the default mailer' do
66
+ user_sign_up
67
+ assert_equal ['please-change-me@config-initializers-devise.com'], ActionMailer::Base.deliveries.first.from
68
+ end
69
+
70
+ test 'a guest user should receive the confirmation instructions from a custom mailer' do
71
+ User.any_instance.stubs(:devise_mailer).returns(Users::Mailer)
72
+ user_sign_up
73
+ assert_equal ['custom@example.com'], ActionMailer::Base.deliveries.first.from
74
+ end
75
+
76
+ test 'a guest user should be blocked by confirmation and redirected to a custom path' do
77
+ Devise::RegistrationsController.any_instance.stubs(:after_inactive_sign_up_path_for).returns("/?custom=1")
78
+ get new_user_registration_path
79
+
80
+ fill_in 'email', :with => 'new_user@test.com'
81
+ fill_in 'password', :with => 'new_user123'
82
+ fill_in 'password confirmation', :with => 'new_user123'
83
+ click_button 'Sign up'
84
+
85
+ assert_current_url "/?custom=1"
86
+ assert_not warden.authenticated?(:user)
87
+ end
88
+
89
+ test 'a guest user cannot sign up with invalid information' do
90
+ # Dirty tracking behavior prevents email validations from being applied:
91
+ # https://github.com/mongoid/mongoid/issues/756
92
+ (pending "Fails on Mongoid < 2.1"; break) if defined?(Mongoid) && Mongoid::VERSION.to_f < 2.1
93
+
94
+ get new_user_registration_path
95
+
96
+ fill_in 'email', :with => 'invalid_email'
97
+ fill_in 'password', :with => 'new_user123'
98
+ fill_in 'password confirmation', :with => 'new_user321'
99
+ click_button 'Sign up'
100
+
101
+ assert_template 'registrations/new'
102
+ assert_have_selector '#error_explanation'
103
+ assert_contain "Email is invalid"
104
+ assert_contain "Password doesn't match confirmation"
105
+ assert_contain "2 errors prohibited"
106
+ assert_nil User.first
107
+
108
+ assert_not warden.authenticated?(:user)
109
+ end
110
+
111
+ test 'a guest should not sign up with email/password that already exists' do
112
+ # Dirty tracking behavior prevents email validations from being applied:
113
+ # https://github.com/mongoid/mongoid/issues/756
114
+ (pending "Fails on Mongoid < 2.1"; break) if defined?(Mongoid) && Mongoid::VERSION.to_f < 2.1
115
+
116
+ user = create_user
117
+ get new_user_registration_path
118
+
119
+ fill_in 'email', :with => 'user@test.com'
120
+ fill_in 'password', :with => '123456'
121
+ fill_in 'password confirmation', :with => '123456'
122
+ click_button 'Sign up'
123
+
124
+ assert_current_url '/users'
125
+ assert_contain(/Email.*already.*taken/)
126
+
127
+ assert_not warden.authenticated?(:user)
128
+ end
129
+
130
+ test 'a guest should not be able to change account' do
131
+ get edit_user_registration_path
132
+ assert_redirected_to new_user_session_path
133
+ follow_redirect!
134
+ assert_contain 'You need to sign in or sign up before continuing.'
135
+ end
136
+
137
+ test 'a signed in user should not be able to access sign up' do
138
+ sign_in_as_user
139
+ get new_user_registration_path
140
+ assert_redirected_to root_path
141
+ end
142
+
143
+ test 'a signed in user should be able to edit his account' do
144
+ sign_in_as_user
145
+ get edit_user_registration_path
146
+
147
+ fill_in 'email', :with => 'user.new@example.com'
148
+ fill_in 'current password', :with => '12345678'
149
+ click_button 'Update'
150
+
151
+ assert_current_url '/'
152
+ assert_contain 'You updated your account successfully.'
153
+
154
+ assert_equal "user.new@example.com", User.first.email
155
+ end
156
+
157
+ test 'a signed in user should still be able to use the website after changing his password' do
158
+ sign_in_as_user
159
+ get edit_user_registration_path
160
+
161
+ fill_in 'password', :with => '1234567890'
162
+ fill_in 'password confirmation', :with => '1234567890'
163
+ fill_in 'current password', :with => '12345678'
164
+ click_button 'Update'
165
+
166
+ assert_contain 'You updated your account successfully.'
167
+ get users_path
168
+ assert warden.authenticated?(:user)
169
+ end
170
+
171
+ test 'a signed in user should not change his current user with invalid password' do
172
+ sign_in_as_user
173
+ get edit_user_registration_path
174
+
175
+ fill_in 'email', :with => 'user.new@example.com'
176
+ fill_in 'current password', :with => 'invalid'
177
+ click_button 'Update'
178
+
179
+ assert_template 'registrations/edit'
180
+ assert_contain 'user@test.com'
181
+ assert_have_selector 'form input[value="user.new@example.com"]'
182
+
183
+ assert_equal "user@test.com", User.first.email
184
+ end
185
+
186
+ test 'a signed in user should be able to edit his password' do
187
+ sign_in_as_user
188
+ get edit_user_registration_path
189
+
190
+ fill_in 'password', :with => 'pass1234'
191
+ fill_in 'password confirmation', :with => 'pass1234'
192
+ fill_in 'current password', :with => '12345678'
193
+ click_button 'Update'
194
+
195
+ assert_current_url '/'
196
+ assert_contain 'You updated your account successfully.'
197
+
198
+ assert User.first.valid_password?('pass1234')
199
+ end
200
+
201
+ test 'a signed in user should not be able to edit his password with invalid confirmation' do
202
+ sign_in_as_user
203
+ get edit_user_registration_path
204
+
205
+ fill_in 'password', :with => 'pas123'
206
+ fill_in 'password confirmation', :with => ''
207
+ fill_in 'current password', :with => '123456'
208
+ click_button 'Update'
209
+
210
+ assert_contain "Password doesn't match confirmation"
211
+ assert_not User.first.valid_password?('pas123')
212
+ end
213
+
214
+ test 'a signed in user should be able to cancel his account' do
215
+ sign_in_as_user
216
+ get edit_user_registration_path
217
+
218
+ click_link "Cancel my account", :method => :delete
219
+ assert_contain "Bye! Your account was successfully cancelled. We hope to see you again soon."
220
+
221
+ assert User.all.empty?
222
+ end
223
+
224
+ test 'a user should be able to cancel sign up by deleting data in the session' do
225
+ get "/set"
226
+ assert_equal "something", @request.session["devise.foo_bar"]
227
+
228
+ get "/users/sign_up"
229
+ assert_equal "something", @request.session["devise.foo_bar"]
230
+
231
+ get "/users/cancel"
232
+ assert_nil @request.session["devise.foo_bar"]
233
+ assert_redirected_to new_user_registration_path
234
+ end
235
+
236
+ test 'a user with XML sign up stub' do
237
+ get new_user_registration_path(:format => 'xml')
238
+ assert_response :success
239
+ assert_match %(<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<user>), response.body
240
+ assert_no_match(/<confirmation-token/, response.body)
241
+ end
242
+
243
+ test 'a user with JSON sign up stub' do
244
+ get new_user_registration_path(:format => 'json')
245
+ assert_response :success
246
+ assert_match %({"user":), response.body
247
+ assert_no_match(/"confirmation_token"/, response.body)
248
+ end
249
+
250
+ test 'an admin sign up with valid information in XML format should return valid response' do
251
+ post admin_registration_path(:format => 'xml'), :admin => { :email => 'new_user@test.com', :password => 'new_user123', :password_confirmation => 'new_user123' }
252
+ assert_response :success
253
+ assert response.body.include? %(<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<admin>)
254
+
255
+ admin = Admin.last :order => "id"
256
+ assert_equal admin.email, 'new_user@test.com'
257
+ end
258
+
259
+ test 'a user sign up with valid information in XML format should return valid response' do
260
+ post user_registration_path(:format => 'xml'), :user => { :email => 'new_user@test.com', :password => 'new_user123', :password_confirmation => 'new_user123' }
261
+ assert_response :success
262
+ assert response.body.include? %(<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<user>)
263
+
264
+ user = User.last :order => "id"
265
+ assert_equal user.email, 'new_user@test.com'
266
+ end
267
+
268
+ test 'a user sign up with invalid information in XML format should return invalid response' do
269
+ post user_registration_path(:format => 'xml'), :user => { :email => 'new_user@test.com', :password => 'new_user123', :password_confirmation => 'invalid' }
270
+ assert_response :unprocessable_entity
271
+ assert response.body.include? %(<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<errors>)
272
+ end
273
+
274
+ test 'a user update information with valid data in XML format should return valid response' do
275
+ user = sign_in_as_user
276
+ put user_registration_path(:format => 'xml'), :user => { :current_password => '12345678', :email => 'user.new@test.com' }
277
+ assert_response :success
278
+ assert_equal user.reload.email, 'user.new@test.com'
279
+ end
280
+
281
+ test 'a user update information with invalid data in XML format should return invalid response' do
282
+ user = sign_in_as_user
283
+ put user_registration_path(:format => 'xml'), :user => { :current_password => 'invalid', :email => 'user.new@test.com' }
284
+ assert_response :unprocessable_entity
285
+ assert_equal user.reload.email, 'user@test.com'
286
+ end
287
+
288
+ test 'a user cancel his account in XML format should return valid response' do
289
+ user = sign_in_as_user
290
+ delete user_registration_path(:format => 'xml')
291
+ assert_response :success
292
+ assert_equal User.count, 0
293
+ end
294
+ end
295
+
296
+ class ReconfirmableRegistrationTest < ActionController::IntegrationTest
297
+ test 'a signed in admin should see a more appropriate flash message when editing his account if reconfirmable is enabled' do
298
+ sign_in_as_admin
299
+ get edit_admin_registration_path
300
+
301
+ fill_in 'email', :with => 'admin.new@example.com'
302
+ fill_in 'current password', :with => '123456'
303
+ click_button 'Update'
304
+
305
+ assert_current_url '/admin_area/home'
306
+ assert_contain 'but we need to verify your new email address'
307
+
308
+ assert_equal "admin.new@example.com", Admin.first.unconfirmed_email
309
+ end
310
+
311
+ test 'a signed in admin should not see a reconfirmation message if they did not change their password' do
312
+ sign_in_as_admin
313
+ get edit_admin_registration_path
314
+
315
+ fill_in 'password', :with => 'pas123'
316
+ fill_in 'password confirmation', :with => 'pas123'
317
+ fill_in 'current password', :with => '123456'
318
+ click_button 'Update'
319
+
320
+ assert_current_url '/admin_area/home'
321
+ assert_contain 'You updated your account successfully.'
322
+
323
+ assert Admin.first.valid_password?('pas123')
324
+ end
325
+
326
+ test 'a signed in admin should not see a reconfirmation message if he did not change his email, despite having an unconfirmed email' do
327
+ sign_in_as_admin
328
+
329
+ get edit_admin_registration_path
330
+ fill_in 'email', :with => 'admin.new@example.com'
331
+ fill_in 'current password', :with => '123456'
332
+ click_button 'Update'
333
+
334
+ get edit_admin_registration_path
335
+ fill_in 'password', :with => 'pas123'
336
+ fill_in 'password confirmation', :with => 'pas123'
337
+ fill_in 'current password', :with => '123456'
338
+ click_button 'Update'
339
+
340
+ assert_current_url '/admin_area/home'
341
+ assert_contain 'You updated your account successfully.'
342
+
343
+ assert_equal "admin.new@example.com", Admin.first.unconfirmed_email
344
+ assert Admin.first.valid_password?('pas123')
345
+ end
346
+ end
@@ -0,0 +1,159 @@
1
+ # -*- encoding : utf-8 -*-
2
+ require 'test_helper'
3
+
4
+ class RememberMeTest < ActionController::IntegrationTest
5
+ def create_user_and_remember(add_to_token='')
6
+ user = create_user
7
+ user.remember_me!
8
+ raw_cookie = User.serialize_into_cookie(user).tap { |a| a.last << add_to_token }
9
+ cookies['remember_user_token'] = generate_signed_cookie(raw_cookie)
10
+ user
11
+ end
12
+
13
+ def generate_signed_cookie(raw_cookie)
14
+ request = ActionDispatch::TestRequest.new
15
+ request.cookie_jar.signed['raw_cookie'] = raw_cookie
16
+ request.cookie_jar['raw_cookie']
17
+ end
18
+
19
+ def signed_cookie(key)
20
+ controller.send(:cookies).signed[key]
21
+ end
22
+
23
+ def cookie_expires(key)
24
+ cookie = response.headers["Set-Cookie"].split("\n").grep(/^#{key}/).first
25
+ expires = cookie.split(";").map(&:strip).grep(/^expires=/).first
26
+ Time.parse(expires).utc
27
+ end
28
+
29
+ test 'do not remember the user if he has not checked remember me option' do
30
+ user = sign_in_as_user
31
+ assert_nil request.cookies["remember_user_cookie"]
32
+ end
33
+
34
+ test 'handles unverified requests gets rid of caches' do
35
+ swap UsersController, :allow_forgery_protection => true do
36
+ post exhibit_user_url(1)
37
+ assert_not warden.authenticated?(:user)
38
+
39
+ create_user_and_remember
40
+ post exhibit_user_url(1)
41
+ assert_equal "User is not authenticated", response.body
42
+ assert_not warden.authenticated?(:user)
43
+ end
44
+ end
45
+
46
+ test 'generate remember token after sign in' do
47
+ user = sign_in_as_user :remember_me => true
48
+ assert request.cookies["remember_user_token"]
49
+ end
50
+
51
+ test 'generate remember token after sign in setting cookie options' do
52
+ # We test this by asserting the cookie is not sent after the redirect
53
+ # since we changed the domain. This is the only difference with the
54
+ # previous test.
55
+ swap Devise, :rememberable_options => { :domain => "omg.somewhere.com" } do
56
+ user = sign_in_as_user :remember_me => true
57
+ assert_nil request.cookies["remember_user_token"]
58
+ end
59
+ end
60
+
61
+ test 'generate remember token after sign in setting session options' do
62
+ begin
63
+ Rails.configuration.session_options[:domain] = "omg.somewhere.com"
64
+ user = sign_in_as_user :remember_me => true
65
+ assert_nil request.cookies["remember_user_token"]
66
+ ensure
67
+ Rails.configuration.session_options.delete(:domain)
68
+ end
69
+ end
70
+
71
+ test 'remember the user before sign in' do
72
+ user = create_user_and_remember
73
+ get users_path
74
+ assert_response :success
75
+ assert warden.authenticated?(:user)
76
+ assert warden.user(:user) == user
77
+ assert_match /remember_user_token[^\n]*HttpOnly/, response.headers["Set-Cookie"], "Expected Set-Cookie header in response to set HttpOnly flag on remember_user_token cookie."
78
+ end
79
+
80
+ test 'remember the user before sign up and redirect him to his home' do
81
+ user = create_user_and_remember
82
+ get new_user_registration_path
83
+ assert warden.authenticated?(:user)
84
+ assert_redirected_to root_path
85
+ end
86
+
87
+ test 'cookies are destroyed on unverified requests' do
88
+ swap ApplicationController, :allow_forgery_protection => true do
89
+ user = create_user_and_remember
90
+ get users_path
91
+ assert warden.authenticated?(:user)
92
+ post root_path, :authenticity_token => 'INVALID'
93
+ assert_not warden.authenticated?(:user)
94
+ end
95
+ end
96
+
97
+ test 'does not extend remember period through sign in' do
98
+ swap Devise, :extend_remember_period => true, :remember_for => 1.year do
99
+ user = create_user
100
+ user.remember_me!
101
+
102
+ user.remember_created_at = old = 10.days.ago
103
+ user.save
104
+
105
+ sign_in_as_user :remember_me => true
106
+ user.reload
107
+
108
+ assert warden.user(:user) == user
109
+ assert_equal old.to_i, user.remember_created_at.to_i
110
+ end
111
+ end
112
+
113
+ test 'do not remember other scopes' do
114
+ user = create_user_and_remember
115
+ get root_path
116
+ assert_response :success
117
+ assert warden.authenticated?(:user)
118
+ assert_not warden.authenticated?(:admin)
119
+ end
120
+
121
+ test 'do not remember with invalid token' do
122
+ user = create_user_and_remember('add')
123
+ get users_path
124
+ assert_not warden.authenticated?(:user)
125
+ assert_redirected_to new_user_session_path
126
+ end
127
+
128
+ test 'do not remember with expired token' do
129
+ user = create_user_and_remember
130
+ swap Devise, :remember_for => 0 do
131
+ get users_path
132
+ assert_not warden.authenticated?(:user)
133
+ assert_redirected_to new_user_session_path
134
+ end
135
+ end
136
+
137
+ test 'do not remember the user anymore after forget' do
138
+ user = create_user_and_remember
139
+ get users_path
140
+ assert warden.authenticated?(:user)
141
+
142
+ get destroy_user_session_path
143
+ assert_not warden.authenticated?(:user)
144
+ assert_nil warden.cookies['remember_user_token']
145
+
146
+ get users_path
147
+ assert_not warden.authenticated?(:user)
148
+ end
149
+
150
+ test 'changing user password expires remember me token' do
151
+ user = create_user_and_remember
152
+ user.password = "another_password"
153
+ user.password_confirmation = "another_password"
154
+ user.save!
155
+
156
+ get users_path
157
+ assert_not warden.authenticated?(:user)
158
+ end
159
+ end
@@ -0,0 +1,141 @@
1
+ # -*- encoding : utf-8 -*-
2
+ require 'test_helper'
3
+
4
+ class SessionTimeoutTest < ActionController::IntegrationTest
5
+
6
+ def last_request_at
7
+ @controller.user_session['last_request_at']
8
+ end
9
+
10
+ test 'set last request at in user session after each request' do
11
+ sign_in_as_user
12
+ old_last_request = last_request_at
13
+ assert_not_nil last_request_at
14
+
15
+ get users_path
16
+ assert_not_nil last_request_at
17
+ assert_not_equal old_last_request, last_request_at
18
+ end
19
+
20
+ test 'set last request at in user session after each request is skipped if tracking is disabled' do
21
+ sign_in_as_user
22
+ old_last_request = last_request_at
23
+ assert_not_nil last_request_at
24
+
25
+ get users_path, {}, 'devise.skip_trackable' => true
26
+ assert_equal old_last_request, last_request_at
27
+ end
28
+
29
+ test 'does not time out user session before default limit time' do
30
+ sign_in_as_user
31
+ assert_response :success
32
+ assert warden.authenticated?(:user)
33
+
34
+ get users_path
35
+ assert_response :success
36
+ assert warden.authenticated?(:user)
37
+ end
38
+
39
+ test 'time out user session after default limit time' do
40
+ user = sign_in_as_user
41
+ get expire_user_path(user)
42
+ assert_not_nil last_request_at
43
+
44
+ get users_path
45
+ assert_redirected_to users_path
46
+ assert_not warden.authenticated?(:user)
47
+ end
48
+
49
+ test 'time out is not triggered on sign out' do
50
+ user = sign_in_as_user
51
+ get expire_user_path(user)
52
+
53
+ get destroy_user_session_path
54
+
55
+ assert_response :redirect
56
+ assert_redirected_to root_path
57
+ follow_redirect!
58
+ assert_contain 'Signed out successfully'
59
+ end
60
+
61
+ test 'time out is not triggered on sign in' do
62
+ user = sign_in_as_user
63
+ get expire_user_path(user)
64
+
65
+ post "/users/sign_in", :email => user.email, :password => "123456"
66
+
67
+ assert_response :redirect
68
+ follow_redirect!
69
+ assert_contain 'You are signed in'
70
+ end
71
+
72
+ test 'admin does not explode on time out' do
73
+ admin = sign_in_as_admin
74
+ get expire_admin_path(admin)
75
+
76
+ Admin.send :define_method, :reset_authentication_token! do
77
+ nil
78
+ end
79
+
80
+ begin
81
+ get admins_path
82
+ assert_redirected_to admins_path
83
+ assert_not warden.authenticated?(:admin)
84
+ ensure
85
+ Admin.send(:remove_method, :reset_authentication_token!)
86
+ end
87
+ end
88
+
89
+ test 'user configured timeout limit' do
90
+ swap Devise, :timeout_in => 8.minutes do
91
+ user = sign_in_as_user
92
+
93
+ get users_path
94
+ assert_not_nil last_request_at
95
+ assert_response :success
96
+ assert warden.authenticated?(:user)
97
+
98
+ get expire_user_path(user)
99
+ get users_path
100
+ assert_redirected_to users_path
101
+ assert_not warden.authenticated?(:user)
102
+ end
103
+ end
104
+
105
+ test 'error message with i18n' do
106
+ store_translations :en, :devise => {
107
+ :failure => { :user => { :timeout => 'Session expired!' } }
108
+ } do
109
+ user = sign_in_as_user
110
+
111
+ get expire_user_path(user)
112
+ get root_path
113
+ follow_redirect!
114
+ assert_contain 'Session expired!'
115
+ end
116
+ end
117
+
118
+ test 'error message with i18n with double redirect' do
119
+ store_translations :en, :devise => {
120
+ :failure => { :user => { :timeout => 'Session expired!' } }
121
+ } do
122
+ user = sign_in_as_user
123
+
124
+ get expire_user_path(user)
125
+ get users_path
126
+ follow_redirect!
127
+ follow_redirect!
128
+ assert_contain 'Session expired!'
129
+ end
130
+ end
131
+
132
+ test 'time out not triggered if remembered' do
133
+ user = sign_in_as_user :remember_me => true
134
+ get expire_user_path(user)
135
+ assert_not_nil last_request_at
136
+
137
+ get users_path
138
+ assert_response :success
139
+ assert warden.authenticated?(:user)
140
+ end
141
+ end