af-devise 2.1.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (207) hide show
  1. data/.gitignore +10 -0
  2. data/.travis.yml +15 -0
  3. data/CHANGELOG.rdoc +885 -0
  4. data/CONTRIBUTING.md +14 -0
  5. data/Gemfile +29 -0
  6. data/Gemfile.lock +155 -0
  7. data/MIT-LICENSE +20 -0
  8. data/README.md +394 -0
  9. data/Rakefile +34 -0
  10. data/app/controllers/devise/confirmations_controller.rb +43 -0
  11. data/app/controllers/devise/omniauth_callbacks_controller.rb +30 -0
  12. data/app/controllers/devise/passwords_controller.rb +65 -0
  13. data/app/controllers/devise/registrations_controller.rb +119 -0
  14. data/app/controllers/devise/sessions_controller.rb +50 -0
  15. data/app/controllers/devise/unlocks_controller.rb +44 -0
  16. data/app/controllers/devise_controller.rb +184 -0
  17. data/app/helpers/devise_helper.rb +25 -0
  18. data/app/mailers/devise/mailer.rb +15 -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 +25 -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.rb +444 -0
  36. data/lib/devise/controllers/helpers.rb +285 -0
  37. data/lib/devise/controllers/rememberable.rb +52 -0
  38. data/lib/devise/controllers/scoped_views.rb +17 -0
  39. data/lib/devise/controllers/url_helpers.rb +67 -0
  40. data/lib/devise/delegator.rb +16 -0
  41. data/lib/devise/failure_app.rb +187 -0
  42. data/lib/devise/hooks/activatable.rb +11 -0
  43. data/lib/devise/hooks/forgetable.rb +9 -0
  44. data/lib/devise/hooks/lockable.rb +7 -0
  45. data/lib/devise/hooks/rememberable.rb +6 -0
  46. data/lib/devise/hooks/timeoutable.rb +25 -0
  47. data/lib/devise/hooks/trackable.rb +9 -0
  48. data/lib/devise/mailers/helpers.rb +91 -0
  49. data/lib/devise/mapping.rb +172 -0
  50. data/lib/devise/models.rb +128 -0
  51. data/lib/devise/models/authenticatable.rb +268 -0
  52. data/lib/devise/models/confirmable.rb +270 -0
  53. data/lib/devise/models/database_authenticatable.rb +127 -0
  54. data/lib/devise/models/lockable.rb +193 -0
  55. data/lib/devise/models/omniauthable.rb +27 -0
  56. data/lib/devise/models/recoverable.rb +140 -0
  57. data/lib/devise/models/registerable.rb +25 -0
  58. data/lib/devise/models/rememberable.rb +125 -0
  59. data/lib/devise/models/timeoutable.rb +49 -0
  60. data/lib/devise/models/token_authenticatable.rb +89 -0
  61. data/lib/devise/models/trackable.rb +35 -0
  62. data/lib/devise/models/validatable.rb +66 -0
  63. data/lib/devise/modules.rb +29 -0
  64. data/lib/devise/omniauth.rb +28 -0
  65. data/lib/devise/omniauth/config.rb +45 -0
  66. data/lib/devise/omniauth/url_helpers.rb +18 -0
  67. data/lib/devise/orm/active_record.rb +3 -0
  68. data/lib/devise/orm/mongoid.rb +3 -0
  69. data/lib/devise/param_filter.rb +41 -0
  70. data/lib/devise/rails.rb +54 -0
  71. data/lib/devise/rails/routes.rb +446 -0
  72. data/lib/devise/rails/warden_compat.rb +43 -0
  73. data/lib/devise/strategies/authenticatable.rb +176 -0
  74. data/lib/devise/strategies/base.rb +20 -0
  75. data/lib/devise/strategies/database_authenticatable.rb +20 -0
  76. data/lib/devise/strategies/rememberable.rb +55 -0
  77. data/lib/devise/strategies/token_authenticatable.rb +56 -0
  78. data/lib/devise/test_helpers.rb +131 -0
  79. data/lib/devise/time_inflector.rb +14 -0
  80. data/lib/devise/version.rb +3 -0
  81. data/lib/generators/active_record/devise_generator.rb +79 -0
  82. data/lib/generators/active_record/templates/migration.rb +19 -0
  83. data/lib/generators/active_record/templates/migration_existing.rb +26 -0
  84. data/lib/generators/devise/devise_generator.rb +24 -0
  85. data/lib/generators/devise/install_generator.rb +24 -0
  86. data/lib/generators/devise/orm_helpers.rb +32 -0
  87. data/lib/generators/devise/views_generator.rb +116 -0
  88. data/lib/generators/mongoid/devise_generator.rb +57 -0
  89. data/lib/generators/templates/README +35 -0
  90. data/lib/generators/templates/devise.rb +240 -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 +62 -0
  102. data/test/controllers/helpers_test.rb +253 -0
  103. data/test/controllers/internal_helpers_test.rb +110 -0
  104. data/test/controllers/sessions_controller_test.rb +85 -0
  105. data/test/controllers/url_helpers_test.rb +59 -0
  106. data/test/delegator_test.rb +19 -0
  107. data/test/devise_test.rb +72 -0
  108. data/test/failure_app_test.rb +221 -0
  109. data/test/generators/active_record_generator_test.rb +75 -0
  110. data/test/generators/devise_generator_test.rb +39 -0
  111. data/test/generators/install_generator_test.rb +13 -0
  112. data/test/generators/mongoid_generator_test.rb +23 -0
  113. data/test/generators/views_generator_test.rb +52 -0
  114. data/test/helpers/devise_helper_test.rb +51 -0
  115. data/test/integration/authenticatable_test.rb +633 -0
  116. data/test/integration/confirmable_test.rb +298 -0
  117. data/test/integration/database_authenticatable_test.rb +82 -0
  118. data/test/integration/http_authenticatable_test.rb +97 -0
  119. data/test/integration/lockable_test.rb +242 -0
  120. data/test/integration/omniauthable_test.rb +133 -0
  121. data/test/integration/recoverable_test.rb +334 -0
  122. data/test/integration/registerable_test.rb +345 -0
  123. data/test/integration/rememberable_test.rb +158 -0
  124. data/test/integration/timeoutable_test.rb +140 -0
  125. data/test/integration/token_authenticatable_test.rb +161 -0
  126. data/test/integration/trackable_test.rb +92 -0
  127. data/test/mailers/confirmation_instructions_test.rb +102 -0
  128. data/test/mailers/reset_password_instructions_test.rb +83 -0
  129. data/test/mailers/unlock_instructions_test.rb +77 -0
  130. data/test/mapping_test.rb +127 -0
  131. data/test/models/authenticatable_test.rb +7 -0
  132. data/test/models/confirmable_test.rb +391 -0
  133. data/test/models/database_authenticatable_test.rb +196 -0
  134. data/test/models/lockable_test.rb +273 -0
  135. data/test/models/omniauthable_test.rb +7 -0
  136. data/test/models/recoverable_test.rb +205 -0
  137. data/test/models/registerable_test.rb +7 -0
  138. data/test/models/rememberable_test.rb +174 -0
  139. data/test/models/serializable_test.rb +49 -0
  140. data/test/models/timeoutable_test.rb +46 -0
  141. data/test/models/token_authenticatable_test.rb +55 -0
  142. data/test/models/trackable_test.rb +13 -0
  143. data/test/models/validatable_test.rb +117 -0
  144. data/test/models_test.rb +179 -0
  145. data/test/omniauth/config_test.rb +57 -0
  146. data/test/omniauth/url_helpers_test.rb +51 -0
  147. data/test/orm/active_record.rb +9 -0
  148. data/test/orm/mongoid.rb +13 -0
  149. data/test/rails_app/Rakefile +10 -0
  150. data/test/rails_app/app/active_record/admin.rb +6 -0
  151. data/test/rails_app/app/active_record/shim.rb +2 -0
  152. data/test/rails_app/app/active_record/user.rb +6 -0
  153. data/test/rails_app/app/controllers/admins/sessions_controller.rb +6 -0
  154. data/test/rails_app/app/controllers/admins_controller.rb +11 -0
  155. data/test/rails_app/app/controllers/application_controller.rb +8 -0
  156. data/test/rails_app/app/controllers/home_controller.rb +25 -0
  157. data/test/rails_app/app/controllers/publisher/registrations_controller.rb +2 -0
  158. data/test/rails_app/app/controllers/publisher/sessions_controller.rb +2 -0
  159. data/test/rails_app/app/controllers/users/omniauth_callbacks_controller.rb +14 -0
  160. data/test/rails_app/app/controllers/users_controller.rb +23 -0
  161. data/test/rails_app/app/helpers/application_helper.rb +3 -0
  162. data/test/rails_app/app/mailers/users/mailer.rb +8 -0
  163. data/test/rails_app/app/mongoid/admin.rb +29 -0
  164. data/test/rails_app/app/mongoid/shim.rb +24 -0
  165. data/test/rails_app/app/mongoid/user.rb +42 -0
  166. data/test/rails_app/app/views/admins/index.html.erb +1 -0
  167. data/test/rails_app/app/views/admins/sessions/new.html.erb +2 -0
  168. data/test/rails_app/app/views/home/admin_dashboard.html.erb +1 -0
  169. data/test/rails_app/app/views/home/index.html.erb +1 -0
  170. data/test/rails_app/app/views/home/join.html.erb +1 -0
  171. data/test/rails_app/app/views/home/private.html.erb +1 -0
  172. data/test/rails_app/app/views/home/user_dashboard.html.erb +1 -0
  173. data/test/rails_app/app/views/layouts/application.html.erb +24 -0
  174. data/test/rails_app/app/views/users/index.html.erb +1 -0
  175. data/test/rails_app/app/views/users/mailer/confirmation_instructions.erb +1 -0
  176. data/test/rails_app/app/views/users/sessions/new.html.erb +1 -0
  177. data/test/rails_app/config.ru +4 -0
  178. data/test/rails_app/config/application.rb +41 -0
  179. data/test/rails_app/config/boot.rb +8 -0
  180. data/test/rails_app/config/database.yml +18 -0
  181. data/test/rails_app/config/environment.rb +5 -0
  182. data/test/rails_app/config/environments/development.rb +18 -0
  183. data/test/rails_app/config/environments/production.rb +33 -0
  184. data/test/rails_app/config/environments/test.rb +33 -0
  185. data/test/rails_app/config/initializers/backtrace_silencers.rb +7 -0
  186. data/test/rails_app/config/initializers/devise.rb +178 -0
  187. data/test/rails_app/config/initializers/inflections.rb +2 -0
  188. data/test/rails_app/config/initializers/secret_token.rb +2 -0
  189. data/test/rails_app/config/routes.rb +100 -0
  190. data/test/rails_app/db/migrate/20100401102949_create_tables.rb +74 -0
  191. data/test/rails_app/db/schema.rb +52 -0
  192. data/test/rails_app/lib/shared_admin.rb +14 -0
  193. data/test/rails_app/lib/shared_user.rb +26 -0
  194. data/test/rails_app/public/404.html +26 -0
  195. data/test/rails_app/public/422.html +26 -0
  196. data/test/rails_app/public/500.html +26 -0
  197. data/test/rails_app/public/favicon.ico +0 -0
  198. data/test/rails_app/script/rails +10 -0
  199. data/test/routes_test.rb +248 -0
  200. data/test/support/assertions.rb +40 -0
  201. data/test/support/helpers.rb +91 -0
  202. data/test/support/integration.rb +92 -0
  203. data/test/support/locale/en.yml +4 -0
  204. data/test/support/webrat/integrations/rails.rb +24 -0
  205. data/test/test_helper.rb +27 -0
  206. data/test/test_helpers_test.rb +151 -0
  207. metadata +421 -0
@@ -0,0 +1,345 @@
1
+ require 'test_helper'
2
+
3
+ class RegistrationTest < ActionController::IntegrationTest
4
+
5
+ test 'a guest admin should be able to sign in successfully' do
6
+ get new_admin_session_path
7
+ click_link 'Sign up'
8
+
9
+ assert_template 'registrations/new'
10
+
11
+ fill_in 'email', :with => 'new_user@test.com'
12
+ fill_in 'password', :with => 'new_user123'
13
+ fill_in 'password confirmation', :with => 'new_user123'
14
+ click_button 'Sign up'
15
+
16
+ assert_contain 'You have signed up successfully'
17
+ assert warden.authenticated?(:admin)
18
+ assert_current_url "/admin_area/home"
19
+
20
+ admin = Admin.last :order => "id"
21
+ assert_equal admin.email, 'new_user@test.com'
22
+ end
23
+
24
+ test 'a guest admin should be able to sign in and be redirected to a custom location' do
25
+ Devise::RegistrationsController.any_instance.stubs(:after_sign_up_path_for).returns("/?custom=1")
26
+ get new_admin_session_path
27
+ click_link 'Sign up'
28
+
29
+ fill_in 'email', :with => 'new_user@test.com'
30
+ fill_in 'password', :with => 'new_user123'
31
+ fill_in 'password confirmation', :with => 'new_user123'
32
+ click_button 'Sign up'
33
+
34
+ assert_contain 'Welcome! You have signed up successfully.'
35
+ assert warden.authenticated?(:admin)
36
+ assert_current_url "/?custom=1"
37
+ end
38
+
39
+ def user_sign_up
40
+ ActionMailer::Base.deliveries.clear
41
+
42
+ get new_user_registration_path
43
+
44
+ fill_in 'email', :with => 'new_user@test.com'
45
+ fill_in 'password', :with => 'new_user123'
46
+ fill_in 'password confirmation', :with => 'new_user123'
47
+ click_button 'Sign up'
48
+ end
49
+
50
+ test 'a guest user should be able to sign up successfully and be blocked by confirmation' do
51
+ user_sign_up
52
+
53
+ assert_contain 'A message with a confirmation link has been sent to your email address. Please open the link to activate your account.'
54
+ assert_not_contain 'You have to confirm your account before continuing'
55
+ assert_current_url "/"
56
+
57
+ assert_not warden.authenticated?(:user)
58
+
59
+ user = User.last :order => "id"
60
+ assert_equal user.email, 'new_user@test.com'
61
+ assert_not user.confirmed?
62
+ end
63
+
64
+ test 'a guest user should receive the confirmation instructions from the default mailer' do
65
+ user_sign_up
66
+ assert_equal ['please-change-me@config-initializers-devise.com'], ActionMailer::Base.deliveries.first.from
67
+ end
68
+
69
+ test 'a guest user should receive the confirmation instructions from a custom mailer' do
70
+ User.any_instance.stubs(:devise_mailer).returns(Users::Mailer)
71
+ user_sign_up
72
+ assert_equal ['custom@example.com'], ActionMailer::Base.deliveries.first.from
73
+ end
74
+
75
+ test 'a guest user should be blocked by confirmation and redirected to a custom path' do
76
+ Devise::RegistrationsController.any_instance.stubs(:after_inactive_sign_up_path_for).returns("/?custom=1")
77
+ get new_user_registration_path
78
+
79
+ fill_in 'email', :with => 'new_user@test.com'
80
+ fill_in 'password', :with => 'new_user123'
81
+ fill_in 'password confirmation', :with => 'new_user123'
82
+ click_button 'Sign up'
83
+
84
+ assert_current_url "/?custom=1"
85
+ assert_not warden.authenticated?(:user)
86
+ end
87
+
88
+ test 'a guest user cannot sign up with invalid information' do
89
+ # Dirty tracking behavior prevents email validations from being applied:
90
+ # https://github.com/mongoid/mongoid/issues/756
91
+ (pending "Fails on Mongoid < 2.1"; break) if defined?(Mongoid) && Mongoid::VERSION.to_f < 2.1
92
+
93
+ get new_user_registration_path
94
+
95
+ fill_in 'email', :with => 'invalid_email'
96
+ fill_in 'password', :with => 'new_user123'
97
+ fill_in 'password confirmation', :with => 'new_user321'
98
+ click_button 'Sign up'
99
+
100
+ assert_template 'registrations/new'
101
+ assert_have_selector '#error_explanation'
102
+ assert_contain "Email is invalid"
103
+ assert_contain "Password doesn't match confirmation"
104
+ assert_contain "2 errors prohibited"
105
+ assert_nil User.first
106
+
107
+ assert_not warden.authenticated?(:user)
108
+ end
109
+
110
+ test 'a guest should not sign up with email/password that already exists' do
111
+ # Dirty tracking behavior prevents email validations from being applied:
112
+ # https://github.com/mongoid/mongoid/issues/756
113
+ (pending "Fails on Mongoid < 2.1"; break) if defined?(Mongoid) && Mongoid::VERSION.to_f < 2.1
114
+
115
+ user = create_user
116
+ get new_user_registration_path
117
+
118
+ fill_in 'email', :with => 'user@test.com'
119
+ fill_in 'password', :with => '123456'
120
+ fill_in 'password confirmation', :with => '123456'
121
+ click_button 'Sign up'
122
+
123
+ assert_current_url '/users'
124
+ assert_contain(/Email.*already.*taken/)
125
+
126
+ assert_not warden.authenticated?(:user)
127
+ end
128
+
129
+ test 'a guest should not be able to change account' do
130
+ get edit_user_registration_path
131
+ assert_redirected_to new_user_session_path
132
+ follow_redirect!
133
+ assert_contain 'You need to sign in or sign up before continuing.'
134
+ end
135
+
136
+ test 'a signed in user should not be able to access sign up' do
137
+ sign_in_as_user
138
+ get new_user_registration_path
139
+ assert_redirected_to root_path
140
+ end
141
+
142
+ test 'a signed in user should be able to edit his account' do
143
+ sign_in_as_user
144
+ get edit_user_registration_path
145
+
146
+ fill_in 'email', :with => 'user.new@example.com'
147
+ fill_in 'current password', :with => '12345678'
148
+ click_button 'Update'
149
+
150
+ assert_current_url '/'
151
+ assert_contain 'You updated your account successfully.'
152
+
153
+ assert_equal "user.new@example.com", User.first.email
154
+ end
155
+
156
+ test 'a signed in user should still be able to use the website after changing his password' do
157
+ sign_in_as_user
158
+ get edit_user_registration_path
159
+
160
+ fill_in 'password', :with => '1234567890'
161
+ fill_in 'password confirmation', :with => '1234567890'
162
+ fill_in 'current password', :with => '12345678'
163
+ click_button 'Update'
164
+
165
+ assert_contain 'You updated your account successfully.'
166
+ get users_path
167
+ assert warden.authenticated?(:user)
168
+ end
169
+
170
+ test 'a signed in user should not change his current user with invalid password' do
171
+ sign_in_as_user
172
+ get edit_user_registration_path
173
+
174
+ fill_in 'email', :with => 'user.new@example.com'
175
+ fill_in 'current password', :with => 'invalid'
176
+ click_button 'Update'
177
+
178
+ assert_template 'registrations/edit'
179
+ assert_contain 'user@test.com'
180
+ assert_have_selector 'form input[value="user.new@example.com"]'
181
+
182
+ assert_equal "user@test.com", User.first.email
183
+ end
184
+
185
+ test 'a signed in user should be able to edit his password' do
186
+ sign_in_as_user
187
+ get edit_user_registration_path
188
+
189
+ fill_in 'password', :with => 'pass1234'
190
+ fill_in 'password confirmation', :with => 'pass1234'
191
+ fill_in 'current password', :with => '12345678'
192
+ click_button 'Update'
193
+
194
+ assert_current_url '/'
195
+ assert_contain 'You updated your account successfully.'
196
+
197
+ assert User.first.valid_password?('pass1234')
198
+ end
199
+
200
+ test 'a signed in user should not be able to edit his password with invalid confirmation' do
201
+ sign_in_as_user
202
+ get edit_user_registration_path
203
+
204
+ fill_in 'password', :with => 'pas123'
205
+ fill_in 'password confirmation', :with => ''
206
+ fill_in 'current password', :with => '123456'
207
+ click_button 'Update'
208
+
209
+ assert_contain "Password doesn't match confirmation"
210
+ assert_not User.first.valid_password?('pas123')
211
+ end
212
+
213
+ test 'a signed in user should be able to cancel his account' do
214
+ sign_in_as_user
215
+ get edit_user_registration_path
216
+
217
+ click_link "Cancel my account", :method => :delete
218
+ assert_contain "Bye! Your account was successfully cancelled. We hope to see you again soon."
219
+
220
+ assert User.all.empty?
221
+ end
222
+
223
+ test 'a user should be able to cancel sign up by deleting data in the session' do
224
+ get "/set"
225
+ assert_equal "something", @request.session["devise.foo_bar"]
226
+
227
+ get "/users/sign_up"
228
+ assert_equal "something", @request.session["devise.foo_bar"]
229
+
230
+ get "/users/cancel"
231
+ assert_nil @request.session["devise.foo_bar"]
232
+ assert_redirected_to new_user_registration_path
233
+ end
234
+
235
+ test 'a user with XML sign up stub' do
236
+ get new_user_registration_path(:format => 'xml')
237
+ assert_response :success
238
+ assert_match %(<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<user>), response.body
239
+ assert_no_match(/<confirmation-token/, response.body)
240
+ end
241
+
242
+ test 'a user with JSON sign up stub' do
243
+ get new_user_registration_path(:format => 'json')
244
+ assert_response :success
245
+ assert_match %({"user":), response.body
246
+ assert_no_match(/"confirmation_token"/, response.body)
247
+ end
248
+
249
+ test 'an admin sign up with valid information in XML format should return valid response' do
250
+ post admin_registration_path(:format => 'xml'), :admin => { :email => 'new_user@test.com', :password => 'new_user123', :password_confirmation => 'new_user123' }
251
+ assert_response :success
252
+ assert response.body.include? %(<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<admin>)
253
+
254
+ admin = Admin.last :order => "id"
255
+ assert_equal admin.email, 'new_user@test.com'
256
+ end
257
+
258
+ test 'a user sign up with valid information in XML format should return valid response' do
259
+ post user_registration_path(:format => 'xml'), :user => { :email => 'new_user@test.com', :password => 'new_user123', :password_confirmation => 'new_user123' }
260
+ assert_response :success
261
+ assert response.body.include? %(<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<user>)
262
+
263
+ user = User.last :order => "id"
264
+ assert_equal user.email, 'new_user@test.com'
265
+ end
266
+
267
+ test 'a user sign up with invalid information in XML format should return invalid response' do
268
+ post user_registration_path(:format => 'xml'), :user => { :email => 'new_user@test.com', :password => 'new_user123', :password_confirmation => 'invalid' }
269
+ assert_response :unprocessable_entity
270
+ assert response.body.include? %(<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<errors>)
271
+ end
272
+
273
+ test 'a user update information with valid data in XML format should return valid response' do
274
+ user = sign_in_as_user
275
+ put user_registration_path(:format => 'xml'), :user => { :current_password => '12345678', :email => 'user.new@test.com' }
276
+ assert_response :success
277
+ assert_equal user.reload.email, 'user.new@test.com'
278
+ end
279
+
280
+ test 'a user update information with invalid data in XML format should return invalid response' do
281
+ user = sign_in_as_user
282
+ put user_registration_path(:format => 'xml'), :user => { :current_password => 'invalid', :email => 'user.new@test.com' }
283
+ assert_response :unprocessable_entity
284
+ assert_equal user.reload.email, 'user@test.com'
285
+ end
286
+
287
+ test 'a user cancel his account in XML format should return valid response' do
288
+ user = sign_in_as_user
289
+ delete user_registration_path(:format => 'xml')
290
+ assert_response :success
291
+ assert_equal User.count, 0
292
+ end
293
+ end
294
+
295
+ class ReconfirmableRegistrationTest < ActionController::IntegrationTest
296
+ test 'a signed in admin should see a more appropriate flash message when editing his account if reconfirmable is enabled' do
297
+ sign_in_as_admin
298
+ get edit_admin_registration_path
299
+
300
+ fill_in 'email', :with => 'admin.new@example.com'
301
+ fill_in 'current password', :with => '123456'
302
+ click_button 'Update'
303
+
304
+ assert_current_url '/admin_area/home'
305
+ assert_contain 'but we need to verify your new email address'
306
+
307
+ assert_equal "admin.new@example.com", Admin.first.unconfirmed_email
308
+ end
309
+
310
+ test 'a signed in admin should not see a reconfirmation message if they did not change their password' do
311
+ sign_in_as_admin
312
+ get edit_admin_registration_path
313
+
314
+ fill_in 'password', :with => 'pas123'
315
+ fill_in 'password confirmation', :with => 'pas123'
316
+ fill_in 'current password', :with => '123456'
317
+ click_button 'Update'
318
+
319
+ assert_current_url '/admin_area/home'
320
+ assert_contain 'You updated your account successfully.'
321
+
322
+ assert Admin.first.valid_password?('pas123')
323
+ end
324
+
325
+ test 'a signed in admin should not see a reconfirmation message if he did not change his email, despite having an unconfirmed email' do
326
+ sign_in_as_admin
327
+
328
+ get edit_admin_registration_path
329
+ fill_in 'email', :with => 'admin.new@example.com'
330
+ fill_in 'current password', :with => '123456'
331
+ click_button 'Update'
332
+
333
+ get edit_admin_registration_path
334
+ fill_in 'password', :with => 'pas123'
335
+ fill_in 'password confirmation', :with => 'pas123'
336
+ fill_in 'current password', :with => '123456'
337
+ click_button 'Update'
338
+
339
+ assert_current_url '/admin_area/home'
340
+ assert_contain 'You updated your account successfully.'
341
+
342
+ assert_equal "admin.new@example.com", Admin.first.unconfirmed_email
343
+ assert Admin.first.valid_password?('pas123')
344
+ end
345
+ end
@@ -0,0 +1,158 @@
1
+ require 'test_helper'
2
+
3
+ class RememberMeTest < ActionController::IntegrationTest
4
+ def create_user_and_remember(add_to_token='')
5
+ user = create_user
6
+ user.remember_me!
7
+ raw_cookie = User.serialize_into_cookie(user).tap { |a| a.last << add_to_token }
8
+ cookies['remember_user_token'] = generate_signed_cookie(raw_cookie)
9
+ user
10
+ end
11
+
12
+ def generate_signed_cookie(raw_cookie)
13
+ request = ActionDispatch::TestRequest.new
14
+ request.cookie_jar.signed['raw_cookie'] = raw_cookie
15
+ request.cookie_jar['raw_cookie']
16
+ end
17
+
18
+ def signed_cookie(key)
19
+ controller.send(:cookies).signed[key]
20
+ end
21
+
22
+ def cookie_expires(key)
23
+ cookie = response.headers["Set-Cookie"].split("\n").grep(/^#{key}/).first
24
+ expires = cookie.split(";").map(&:strip).grep(/^expires=/).first
25
+ Time.parse(expires).utc
26
+ end
27
+
28
+ test 'do not remember the user if he has not checked remember me option' do
29
+ user = sign_in_as_user
30
+ assert_nil request.cookies["remember_user_cookie"]
31
+ end
32
+
33
+ test 'handles unverified requests gets rid of caches' do
34
+ swap UsersController, :allow_forgery_protection => true do
35
+ post exhibit_user_url(1)
36
+ assert_not warden.authenticated?(:user)
37
+
38
+ create_user_and_remember
39
+ post exhibit_user_url(1)
40
+ assert_equal "User is not authenticated", response.body
41
+ assert_not warden.authenticated?(:user)
42
+ end
43
+ end
44
+
45
+ test 'generate remember token after sign in' do
46
+ user = sign_in_as_user :remember_me => true
47
+ assert request.cookies["remember_user_token"]
48
+ end
49
+
50
+ test 'generate remember token after sign in setting cookie options' do
51
+ # We test this by asserting the cookie is not sent after the redirect
52
+ # since we changed the domain. This is the only difference with the
53
+ # previous test.
54
+ swap Devise, :rememberable_options => { :domain => "omg.somewhere.com" } do
55
+ user = sign_in_as_user :remember_me => true
56
+ assert_nil request.cookies["remember_user_token"]
57
+ end
58
+ end
59
+
60
+ test 'generate remember token after sign in setting session options' do
61
+ begin
62
+ Rails.configuration.session_options[:domain] = "omg.somewhere.com"
63
+ user = sign_in_as_user :remember_me => true
64
+ assert_nil request.cookies["remember_user_token"]
65
+ ensure
66
+ Rails.configuration.session_options.delete(:domain)
67
+ end
68
+ end
69
+
70
+ test 'remember the user before sign in' do
71
+ user = create_user_and_remember
72
+ get users_path
73
+ assert_response :success
74
+ assert warden.authenticated?(:user)
75
+ assert warden.user(:user) == user
76
+ 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."
77
+ end
78
+
79
+ test 'remember the user before sign up and redirect him to his home' do
80
+ user = create_user_and_remember
81
+ get new_user_registration_path
82
+ assert warden.authenticated?(:user)
83
+ assert_redirected_to root_path
84
+ end
85
+
86
+ test 'cookies are destroyed on unverified requests' do
87
+ swap ApplicationController, :allow_forgery_protection => true do
88
+ user = create_user_and_remember
89
+ get users_path
90
+ assert warden.authenticated?(:user)
91
+ post root_path, :authenticity_token => 'INVALID'
92
+ assert_not warden.authenticated?(:user)
93
+ end
94
+ end
95
+
96
+ test 'does not extend remember period through sign in' do
97
+ swap Devise, :extend_remember_period => true, :remember_for => 1.year do
98
+ user = create_user
99
+ user.remember_me!
100
+
101
+ user.remember_created_at = old = 10.days.ago
102
+ user.save
103
+
104
+ sign_in_as_user :remember_me => true
105
+ user.reload
106
+
107
+ assert warden.user(:user) == user
108
+ assert_equal old.to_i, user.remember_created_at.to_i
109
+ end
110
+ end
111
+
112
+ test 'do not remember other scopes' do
113
+ user = create_user_and_remember
114
+ get root_path
115
+ assert_response :success
116
+ assert warden.authenticated?(:user)
117
+ assert_not warden.authenticated?(:admin)
118
+ end
119
+
120
+ test 'do not remember with invalid token' do
121
+ user = create_user_and_remember('add')
122
+ get users_path
123
+ assert_not warden.authenticated?(:user)
124
+ assert_redirected_to new_user_session_path
125
+ end
126
+
127
+ test 'do not remember with expired token' do
128
+ user = create_user_and_remember
129
+ swap Devise, :remember_for => 0 do
130
+ get users_path
131
+ assert_not warden.authenticated?(:user)
132
+ assert_redirected_to new_user_session_path
133
+ end
134
+ end
135
+
136
+ test 'do not remember the user anymore after forget' do
137
+ user = create_user_and_remember
138
+ get users_path
139
+ assert warden.authenticated?(:user)
140
+
141
+ get destroy_user_session_path
142
+ assert_not warden.authenticated?(:user)
143
+ assert_nil warden.cookies['remember_user_token']
144
+
145
+ get users_path
146
+ assert_not warden.authenticated?(:user)
147
+ end
148
+
149
+ test 'changing user password expires remember me token' do
150
+ user = create_user_and_remember
151
+ user.password = "another_password"
152
+ user.password_confirmation = "another_password"
153
+ user.save!
154
+
155
+ get users_path
156
+ assert_not warden.authenticated?(:user)
157
+ end
158
+ end