rmello-devise 2.1.0.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 +12 -0
  2. data/.travis.yml +15 -0
  3. data/CHANGELOG.rdoc +845 -0
  4. data/Gemfile +35 -0
  5. data/Gemfile.lock +165 -0
  6. data/MIT-LICENSE +20 -0
  7. data/README.md +383 -0
  8. data/Rakefile +34 -0
  9. data/app/controllers/devise/confirmations_controller.rb +43 -0
  10. data/app/controllers/devise/omniauth_callbacks_controller.rb +24 -0
  11. data/app/controllers/devise/passwords_controller.rb +47 -0
  12. data/app/controllers/devise/registrations_controller.rb +107 -0
  13. data/app/controllers/devise/sessions_controller.rb +49 -0
  14. data/app/controllers/devise/unlocks_controller.rb +44 -0
  15. data/app/controllers/devise_controller.rb +184 -0
  16. data/app/helpers/devise_helper.rb +25 -0
  17. data/app/mailers/devise/mailer.rb +15 -0
  18. data/app/views/devise/_links.erb +3 -0
  19. data/app/views/devise/confirmations/new.html.erb +12 -0
  20. data/app/views/devise/mailer/confirmation_instructions.html.erb +5 -0
  21. data/app/views/devise/mailer/reset_password_instructions.html.erb +8 -0
  22. data/app/views/devise/mailer/unlock_instructions.html.erb +7 -0
  23. data/app/views/devise/passwords/edit.html.erb +16 -0
  24. data/app/views/devise/passwords/new.html.erb +12 -0
  25. data/app/views/devise/registrations/edit.html.erb +25 -0
  26. data/app/views/devise/registrations/new.html.erb +18 -0
  27. data/app/views/devise/sessions/new.html.erb +17 -0
  28. data/app/views/devise/shared/_links.erb +25 -0
  29. data/app/views/devise/unlocks/new.html.erb +12 -0
  30. data/config/locales/en.yml +57 -0
  31. data/devise.gemspec +25 -0
  32. data/gemfiles/Gemfile.rails-3.1.x +35 -0
  33. data/gemfiles/Gemfile.rails-3.1.x.lock +167 -0
  34. data/lib/devise.rb +440 -0
  35. data/lib/devise/controllers/helpers.rb +269 -0
  36. data/lib/devise/controllers/rememberable.rb +52 -0
  37. data/lib/devise/controllers/scoped_views.rb +17 -0
  38. data/lib/devise/controllers/url_helpers.rb +67 -0
  39. data/lib/devise/delegator.rb +16 -0
  40. data/lib/devise/failure_app.rb +187 -0
  41. data/lib/devise/hooks/activatable.rb +11 -0
  42. data/lib/devise/hooks/forgetable.rb +9 -0
  43. data/lib/devise/hooks/lockable.rb +7 -0
  44. data/lib/devise/hooks/rememberable.rb +6 -0
  45. data/lib/devise/hooks/timeoutable.rb +22 -0
  46. data/lib/devise/hooks/trackable.rb +9 -0
  47. data/lib/devise/mailers/helpers.rb +86 -0
  48. data/lib/devise/mapping.rb +172 -0
  49. data/lib/devise/models.rb +128 -0
  50. data/lib/devise/models/authenticatable.rb +231 -0
  51. data/lib/devise/models/confirmable.rb +268 -0
  52. data/lib/devise/models/database_authenticatable.rb +126 -0
  53. data/lib/devise/models/lockable.rb +185 -0
  54. data/lib/devise/models/omniauthable.rb +27 -0
  55. data/lib/devise/models/recoverable.rb +140 -0
  56. data/lib/devise/models/registerable.rb +25 -0
  57. data/lib/devise/models/rememberable.rb +125 -0
  58. data/lib/devise/models/timeoutable.rb +49 -0
  59. data/lib/devise/models/token_authenticatable.rb +77 -0
  60. data/lib/devise/models/trackable.rb +35 -0
  61. data/lib/devise/models/validatable.rb +66 -0
  62. data/lib/devise/modules.rb +29 -0
  63. data/lib/devise/omniauth.rb +28 -0
  64. data/lib/devise/omniauth/config.rb +45 -0
  65. data/lib/devise/omniauth/url_helpers.rb +33 -0
  66. data/lib/devise/orm/active_record.rb +3 -0
  67. data/lib/devise/orm/mongoid.rb +3 -0
  68. data/lib/devise/param_filter.rb +41 -0
  69. data/lib/devise/rails.rb +54 -0
  70. data/lib/devise/rails/routes.rb +426 -0
  71. data/lib/devise/rails/warden_compat.rb +43 -0
  72. data/lib/devise/strategies/authenticatable.rb +176 -0
  73. data/lib/devise/strategies/base.rb +15 -0
  74. data/lib/devise/strategies/database_authenticatable.rb +20 -0
  75. data/lib/devise/strategies/rememberable.rb +55 -0
  76. data/lib/devise/strategies/token_authenticatable.rb +56 -0
  77. data/lib/devise/test_helpers.rb +130 -0
  78. data/lib/devise/version.rb +3 -0
  79. data/lib/generators/active_record/devise_generator.rb +75 -0
  80. data/lib/generators/active_record/templates/migration.rb +19 -0
  81. data/lib/generators/active_record/templates/migration_existing.rb +26 -0
  82. data/lib/generators/devise/devise_generator.rb +24 -0
  83. data/lib/generators/devise/install_generator.rb +24 -0
  84. data/lib/generators/devise/orm_helpers.rb +32 -0
  85. data/lib/generators/devise/views_generator.rb +110 -0
  86. data/lib/generators/mongoid/devise_generator.rb +57 -0
  87. data/lib/generators/templates/README +31 -0
  88. data/lib/generators/templates/devise.rb +216 -0
  89. data/lib/generators/templates/markerb/confirmation_instructions.markerb +5 -0
  90. data/lib/generators/templates/markerb/reset_password_instructions.markerb +8 -0
  91. data/lib/generators/templates/markerb/unlock_instructions.markerb +7 -0
  92. data/lib/generators/templates/simple_form_for/confirmations/new.html.erb +15 -0
  93. data/lib/generators/templates/simple_form_for/passwords/edit.html.erb +19 -0
  94. data/lib/generators/templates/simple_form_for/passwords/new.html.erb +15 -0
  95. data/lib/generators/templates/simple_form_for/registrations/edit.html.erb +22 -0
  96. data/lib/generators/templates/simple_form_for/registrations/new.html.erb +17 -0
  97. data/lib/generators/templates/simple_form_for/sessions/new.html.erb +15 -0
  98. data/lib/generators/templates/simple_form_for/unlocks/new.html.erb +15 -0
  99. data/test/controllers/custom_strategy_test.rb +62 -0
  100. data/test/controllers/helpers_test.rb +254 -0
  101. data/test/controllers/internal_helpers_test.rb +104 -0
  102. data/test/controllers/sessions_controller_test.rb +43 -0
  103. data/test/controllers/url_helpers_test.rb +59 -0
  104. data/test/delegator_test.rb +19 -0
  105. data/test/devise_test.rb +72 -0
  106. data/test/failure_app_test.rb +221 -0
  107. data/test/generators/active_record_generator_test.rb +69 -0
  108. data/test/generators/devise_generator_test.rb +39 -0
  109. data/test/generators/install_generator_test.rb +13 -0
  110. data/test/generators/mongoid_generator_test.rb +23 -0
  111. data/test/generators/views_generator_test.rb +52 -0
  112. data/test/helpers/devise_helper_test.rb +51 -0
  113. data/test/indifferent_hash.rb +33 -0
  114. data/test/integration/authenticatable_test.rb +587 -0
  115. data/test/integration/confirmable_test.rb +255 -0
  116. data/test/integration/database_authenticatable_test.rb +82 -0
  117. data/test/integration/http_authenticatable_test.rb +97 -0
  118. data/test/integration/lockable_test.rb +224 -0
  119. data/test/integration/omniauthable_test.rb +133 -0
  120. data/test/integration/recoverable_test.rb +300 -0
  121. data/test/integration/registerable_test.rb +324 -0
  122. data/test/integration/rememberable_test.rb +158 -0
  123. data/test/integration/timeoutable_test.rb +114 -0
  124. data/test/integration/token_authenticatable_test.rb +161 -0
  125. data/test/integration/trackable_test.rb +92 -0
  126. data/test/mailers/confirmation_instructions_test.rb +95 -0
  127. data/test/mailers/reset_password_instructions_test.rb +83 -0
  128. data/test/mailers/unlock_instructions_test.rb +77 -0
  129. data/test/mapping_test.rb +127 -0
  130. data/test/models/authenticatable_test.rb +7 -0
  131. data/test/models/confirmable_test.rb +377 -0
  132. data/test/models/database_authenticatable_test.rb +189 -0
  133. data/test/models/lockable_test.rb +263 -0
  134. data/test/models/omniauthable_test.rb +7 -0
  135. data/test/models/recoverable_test.rb +205 -0
  136. data/test/models/registerable_test.rb +7 -0
  137. data/test/models/rememberable_test.rb +174 -0
  138. data/test/models/serializable_test.rb +48 -0
  139. data/test/models/timeoutable_test.rb +46 -0
  140. data/test/models/token_authenticatable_test.rb +55 -0
  141. data/test/models/trackable_test.rb +13 -0
  142. data/test/models/validatable_test.rb +117 -0
  143. data/test/models_test.rb +179 -0
  144. data/test/omniauth/config_test.rb +57 -0
  145. data/test/omniauth/url_helpers_test.rb +58 -0
  146. data/test/orm/active_record.rb +9 -0
  147. data/test/orm/mongoid.rb +14 -0
  148. data/test/rails_app/Rakefile +10 -0
  149. data/test/rails_app/app/active_record/admin.rb +6 -0
  150. data/test/rails_app/app/active_record/mobile_user.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 +6 -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 +3 -0
  163. data/test/rails_app/app/mongoid/admin.rb +27 -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 +93 -0
  190. data/test/rails_app/db/migrate/20100401102949_create_tables.rb +88 -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_mobile_user.rb +13 -0
  194. data/test/rails_app/lib/shared_user.rb +26 -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 +248 -0
  201. data/test/support/assertions.rb +40 -0
  202. data/test/support/helpers.rb +97 -0
  203. data/test/support/integration.rb +90 -0
  204. data/test/support/locale/en.yml +4 -0
  205. data/test/support/webrat/integrations/rails.rb +24 -0
  206. data/test/test_helper.rb +27 -0
  207. data/test/test_helpers_test.rb +134 -0
  208. metadata +425 -0
@@ -0,0 +1,255 @@
1
+ require 'test_helper'
2
+
3
+ class ConfirmationTest < ActionController::IntegrationTest
4
+
5
+ def visit_user_confirmation_with_token(confirmation_token)
6
+ visit user_confirmation_path(:confirmation_token => confirmation_token)
7
+ end
8
+
9
+ def resend_confirmation
10
+ user = create_user(:confirm => false)
11
+ ActionMailer::Base.deliveries.clear
12
+
13
+ visit new_user_session_path
14
+ click_link "Didn't receive confirmation instructions?"
15
+
16
+ fill_in 'email', :with => user.email
17
+ click_button 'Resend confirmation instructions'
18
+ end
19
+
20
+ test 'user should be able to request a new confirmation' do
21
+ resend_confirmation
22
+
23
+ assert_current_url '/users/sign_in'
24
+ assert_contain 'You will receive an email with instructions about how to confirm your account in a few minutes'
25
+ assert_equal 1, ActionMailer::Base.deliveries.size
26
+ assert_equal ['please-change-me@config-initializers-devise.com'], ActionMailer::Base.deliveries.first.from
27
+ end
28
+
29
+ test 'user should receive a confirmation from a custom mailer' do
30
+ User.any_instance.stubs(:devise_mailer).returns(Users::Mailer)
31
+
32
+ resend_confirmation
33
+
34
+ assert_equal ['custom@example.com'], ActionMailer::Base.deliveries.first.from
35
+ end
36
+
37
+ test 'user with invalid confirmation token should not be able to confirm an account' do
38
+ visit_user_confirmation_with_token('invalid_confirmation')
39
+ assert_have_selector '#error_explanation'
40
+ assert_contain /Confirmation token(.*)invalid/
41
+ end
42
+
43
+ test 'user with valid confirmation token should be able to confirm an account' do
44
+ user = create_user(:confirm => false)
45
+ assert_not user.confirmed?
46
+ visit_user_confirmation_with_token(user.confirmation_token)
47
+
48
+ assert_contain 'Your account was successfully confirmed.'
49
+ assert_current_url '/'
50
+ assert user.reload.confirmed?
51
+ end
52
+
53
+ test 'user should be redirected to a custom path after confirmation' do
54
+ Devise::ConfirmationsController.any_instance.stubs(:after_confirmation_path_for).returns("/?custom=1")
55
+
56
+ user = create_user(:confirm => false)
57
+ visit_user_confirmation_with_token(user.confirmation_token)
58
+
59
+ assert_current_url "/?custom=1"
60
+ end
61
+
62
+ test 'already confirmed user should not be able to confirm the account again' do
63
+ user = create_user(:confirm => false)
64
+ user.confirmed_at = Time.now
65
+ user.save
66
+ visit_user_confirmation_with_token(user.confirmation_token)
67
+
68
+ assert_have_selector '#error_explanation'
69
+ assert_contain 'already confirmed'
70
+ end
71
+
72
+ test 'already confirmed user should not be able to confirm the account again neither request confirmation' do
73
+ user = create_user(:confirm => false)
74
+ user.confirmed_at = Time.now
75
+ user.save
76
+
77
+ visit_user_confirmation_with_token(user.confirmation_token)
78
+ assert_contain 'already confirmed'
79
+
80
+ fill_in 'email', :with => user.email
81
+ click_button 'Resend confirmation instructions'
82
+ assert_contain 'already confirmed'
83
+ end
84
+
85
+ test 'sign in user automatically after confirming its email' do
86
+ user = create_user(:confirm => false)
87
+ visit_user_confirmation_with_token(user.confirmation_token)
88
+
89
+ assert warden.authenticated?(:user)
90
+ end
91
+
92
+ test 'increases sign count when signed in through confirmation' do
93
+ user = create_user(:confirm => false)
94
+ visit_user_confirmation_with_token(user.confirmation_token)
95
+
96
+ user.reload
97
+ assert_equal 1, user.sign_in_count
98
+ end
99
+
100
+ test 'not confirmed user with setup to block without confirmation should not be able to sign in' do
101
+ swap Devise, :allow_unconfirmed_access_for => 0.days do
102
+ sign_in_as_user(:confirm => false)
103
+
104
+ assert_contain 'You have to confirm your account before continuing'
105
+ assert_not warden.authenticated?(:user)
106
+ end
107
+ end
108
+
109
+ test 'not confirmed user should not see confirmation message if invalid credentials are given' do
110
+ swap Devise, :allow_unconfirmed_access_for => 0.days do
111
+ sign_in_as_user(:confirm => false) do
112
+ fill_in 'password', :with => 'invalid'
113
+ end
114
+
115
+ assert_contain 'Invalid email or password'
116
+ assert_not warden.authenticated?(:user)
117
+ end
118
+ end
119
+
120
+ test 'not confirmed user but configured with some days to confirm should be able to sign in' do
121
+ swap Devise, :allow_unconfirmed_access_for => 1.day do
122
+ sign_in_as_user(:confirm => false)
123
+
124
+ assert_response :success
125
+ assert warden.authenticated?(:user)
126
+ end
127
+ end
128
+
129
+ test 'error message is configurable by resource name' do
130
+ store_translations :en, :devise => {
131
+ :failure => { :user => { :unconfirmed => "Not confirmed user" } }
132
+ } do
133
+ sign_in_as_user(:confirm => false)
134
+ assert_contain 'Not confirmed user'
135
+ end
136
+ end
137
+
138
+ test 'resent confirmation token with valid E-Mail in XML format should return valid response' do
139
+ user = create_user(:confirm => false)
140
+ post user_confirmation_path(:format => 'xml'), :user => { :email => user.email }
141
+ assert_response :success
142
+ assert_equal response.body, {}.to_xml
143
+ end
144
+
145
+ test 'resent confirmation token with invalid E-Mail in XML format should return invalid response' do
146
+ user = create_user(:confirm => false)
147
+ post user_confirmation_path(:format => 'xml'), :user => { :email => 'invalid.test@test.com' }
148
+ assert_response :unprocessable_entity
149
+ assert response.body.include? %(<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<errors>)
150
+ end
151
+
152
+ test 'confirm account with valid confirmation token in XML format should return valid response' do
153
+ user = create_user(:confirm => false)
154
+ get user_confirmation_path(:confirmation_token => user.confirmation_token, :format => 'xml')
155
+ assert_response :success
156
+ assert response.body.include? %(<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<user>)
157
+ end
158
+
159
+ test 'confirm account with invalid confirmation token in XML format should return invalid response' do
160
+ user = create_user(:confirm => false)
161
+ get user_confirmation_path(:confirmation_token => 'invalid_confirmation', :format => 'xml')
162
+ assert_response :unprocessable_entity
163
+ assert response.body.include? %(<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<errors>)
164
+ end
165
+
166
+ test 'request an account confirmation account with JSON, should return an empty JSON' do
167
+ user = create_user(:confirm => false)
168
+
169
+ post user_confirmation_path, :user => { :email => user.email }, :format => :json
170
+ assert_response :success
171
+ assert_equal response.body, {}.to_json
172
+ end
173
+
174
+ test "when in paranoid mode and with a valid e-mail, should not say that the e-mail is valid" do
175
+ swap Devise, :paranoid => true do
176
+ user = create_user(:confirm => false)
177
+ visit new_user_session_path
178
+
179
+ click_link "Didn't receive confirmation instructions?"
180
+ fill_in 'email', :with => user.email
181
+ click_button 'Resend confirmation instructions'
182
+
183
+ assert_contain "If your email address exists in our database, you will receive an email with instructions about how to confirm your account in a few minutes."
184
+ assert_current_url "/users/sign_in"
185
+ end
186
+ end
187
+
188
+ test "when in paranoid mode and with a invalid e-mail, should not say that the e-mail is invalid" do
189
+ swap Devise, :paranoid => true do
190
+ visit new_user_session_path
191
+
192
+ click_link "Didn't receive confirmation instructions?"
193
+ fill_in 'email', :with => "idonthavethisemail@gmail.com"
194
+ click_button 'Resend confirmation instructions'
195
+
196
+ assert_not_contain "1 error prohibited this user from being saved:"
197
+ assert_not_contain "Email not found"
198
+
199
+ assert_contain "If your email address exists in our database, you will receive an email with instructions about how to confirm your account in a few minutes."
200
+ assert_current_url "/users/sign_in"
201
+ end
202
+ end
203
+ end
204
+
205
+ class ConfirmationOnChangeTest < ActionController::IntegrationTest
206
+ def create_second_admin(options={})
207
+ @admin = nil
208
+ create_admin(options)
209
+ end
210
+
211
+ def visit_admin_confirmation_with_token(confirmation_token)
212
+ visit admin_confirmation_path(:confirmation_token => confirmation_token)
213
+ end
214
+
215
+ test 'admin should be able to request a new confirmation after email changed' do
216
+ admin = create_admin
217
+ admin.update_attributes(:email => 'new_test@example.com')
218
+
219
+ visit new_admin_session_path
220
+ click_link "Didn't receive confirmation instructions?"
221
+
222
+ fill_in 'email', :with => admin.unconfirmed_email
223
+ assert_difference "ActionMailer::Base.deliveries.size" do
224
+ click_button 'Resend confirmation instructions'
225
+ end
226
+
227
+ assert_current_url '/admin_area/sign_in'
228
+ assert_contain 'You will receive an email with instructions about how to confirm your account in a few minutes'
229
+ end
230
+
231
+ test 'admin with valid confirmation token should be able to confirm email after email changed' do
232
+ admin = create_admin
233
+ admin.update_attributes(:email => 'new_test@example.com')
234
+ assert_equal 'new_test@example.com', admin.unconfirmed_email
235
+ visit_admin_confirmation_with_token(admin.confirmation_token)
236
+
237
+ assert_contain 'Your account was successfully confirmed.'
238
+ assert_current_url '/admin_area/home'
239
+ assert admin.reload.confirmed?
240
+ assert_not admin.reload.pending_reconfirmation?
241
+ end
242
+
243
+ test 'admin email should be unique also within unconfirmed_email' do
244
+ admin = create_admin
245
+ admin.update_attributes(:email => 'new_admin_test@example.com')
246
+ assert_equal 'new_admin_test@example.com', admin.unconfirmed_email
247
+
248
+ create_second_admin(:email => "new_admin_test@example.com")
249
+
250
+ visit_admin_confirmation_with_token(admin.confirmation_token)
251
+ assert_have_selector '#error_explanation'
252
+ assert_contain /Email.*already.*taken/
253
+ assert admin.reload.pending_reconfirmation?
254
+ end
255
+ end
@@ -0,0 +1,82 @@
1
+ require 'test_helper'
2
+
3
+ class DatabaseAuthenticationTest < ActionController::IntegrationTest
4
+ test 'sign in with email of different case should succeed when email is in the list of case insensitive keys' do
5
+ create_user(:email => 'Foo@Bar.com')
6
+
7
+ sign_in_as_user do
8
+ fill_in 'email', :with => 'foo@bar.com'
9
+ end
10
+
11
+ assert warden.authenticated?(:user)
12
+ end
13
+
14
+ test 'sign in with email of different case should fail when email is NOT the list of case insensitive keys' do
15
+ swap Devise, :case_insensitive_keys => [] do
16
+ create_user(:email => 'Foo@Bar.com')
17
+
18
+ sign_in_as_user do
19
+ fill_in 'email', :with => 'foo@bar.com'
20
+ end
21
+
22
+ assert_not warden.authenticated?(:user)
23
+ end
24
+ end
25
+
26
+ test 'sign in with email including extra spaces should succeed when email is in the list of strip whitespace keys' do
27
+ create_user(:email => ' foo@bar.com ')
28
+
29
+ sign_in_as_user do
30
+ fill_in 'email', :with => 'foo@bar.com'
31
+ end
32
+
33
+ assert warden.authenticated?(:user)
34
+ end
35
+
36
+ test 'sign in with email including extra spaces should fail when email is NOT the list of strip whitespace keys' do
37
+ swap Devise, :strip_whitespace_keys => [] do
38
+ create_user(:email => 'foo@bar.com')
39
+
40
+ sign_in_as_user do
41
+ fill_in 'email', :with => ' foo@bar.com '
42
+ end
43
+
44
+ assert_not warden.authenticated?(:user)
45
+ end
46
+ end
47
+
48
+ test 'sign in should not authenticate if not using proper authentication keys' do
49
+ swap Devise, :authentication_keys => [:username] do
50
+ sign_in_as_user
51
+ assert_not warden.authenticated?(:user)
52
+ end
53
+ end
54
+
55
+ test 'sign in with invalid email should return to sign in form with error message' do
56
+ sign_in_as_admin do
57
+ fill_in 'email', :with => 'wrongemail@test.com'
58
+ end
59
+
60
+ assert_contain 'Invalid email or password'
61
+ assert_not warden.authenticated?(:admin)
62
+ end
63
+
64
+ test 'sign in with invalid pasword should return to sign in form with error message' do
65
+ sign_in_as_admin do
66
+ fill_in 'password', :with => 'abcdef'
67
+ end
68
+
69
+ assert_contain 'Invalid email or password'
70
+ assert_not warden.authenticated?(:admin)
71
+ end
72
+
73
+ test 'error message is configurable by resource name' do
74
+ store_translations :en, :devise => { :failure => { :admin => { :invalid => "Invalid credentials" } } } do
75
+ sign_in_as_admin do
76
+ fill_in 'password', :with => 'abcdef'
77
+ end
78
+
79
+ assert_contain 'Invalid credentials'
80
+ end
81
+ end
82
+ end
@@ -0,0 +1,97 @@
1
+ require 'test_helper'
2
+
3
+ class HttpAuthenticationTest < ActionController::IntegrationTest
4
+ test 'handles unverified requests gets rid of caches but continues signed in' do
5
+ swap UsersController, :allow_forgery_protection => true do
6
+ create_user
7
+ post exhibit_user_url(1), {}, "HTTP_AUTHORIZATION" => "Basic #{Base64.encode64("user@test.com:123456")}"
8
+ assert warden.authenticated?(:user)
9
+ assert_equal "User is authenticated", response.body
10
+ end
11
+ end
12
+
13
+ test 'sign in should authenticate with http' do
14
+ sign_in_as_new_user_with_http
15
+ assert_response 200
16
+ assert_match '<email>user@test.com</email>', response.body
17
+ assert warden.authenticated?(:user)
18
+
19
+ get users_path(:format => :xml)
20
+ assert_response 200
21
+ end
22
+
23
+ test 'sign in should authenticate with http but not emit a cookie if skipping session storage' do
24
+ swap Devise, :skip_session_storage => [:http_auth] do
25
+ sign_in_as_new_user_with_http
26
+ assert_response 200
27
+ assert_match '<email>user@test.com</email>', response.body
28
+ assert warden.authenticated?(:user)
29
+
30
+ get users_path(:format => :xml)
31
+ assert_response 401
32
+ end
33
+ end
34
+
35
+ test 'returns a custom response with www-authenticate header on failures' do
36
+ sign_in_as_new_user_with_http("unknown")
37
+ assert_equal 401, status
38
+ assert_equal 'Basic realm="Application"', headers["WWW-Authenticate"]
39
+ end
40
+
41
+ test 'uses the request format as response content type' do
42
+ sign_in_as_new_user_with_http("unknown")
43
+ assert_equal 401, status
44
+ assert_equal "application/xml; charset=utf-8", headers["Content-Type"]
45
+ assert_match "<error>Invalid email or password.</error>", response.body
46
+ end
47
+
48
+ test 'returns a custom response with www-authenticate and chosen realm' do
49
+ swap Devise, :http_authentication_realm => "MyApp" do
50
+ sign_in_as_new_user_with_http("unknown")
51
+ assert_equal 401, status
52
+ assert_equal 'Basic realm="MyApp"', headers["WWW-Authenticate"]
53
+ end
54
+ end
55
+
56
+ test 'sign in should authenticate with http even with specific authentication keys' do
57
+ swap Devise, :authentication_keys => [:username] do
58
+ sign_in_as_new_user_with_http("usertest")
59
+ assert_response :success
60
+ assert_match '<email>user@test.com</email>', response.body
61
+ assert warden.authenticated?(:user)
62
+ end
63
+ end
64
+
65
+ test 'test request with oauth2 header doesnt get mistaken for basic authentication' do
66
+ swap Devise, :http_authenticatable => true do
67
+ add_oauth2_header
68
+ assert_equal 401, status
69
+ assert_equal 'Basic realm="Application"', headers["WWW-Authenticate"]
70
+ end
71
+ end
72
+
73
+ test 'sign in should authenticate with really long token' do
74
+ token = "token_containing_so_many_characters_that_the_base64_encoding_will_wrap"
75
+ user = create_user
76
+ user.update_attribute :authentication_token, token
77
+ get users_path(:format => :xml), {}, "HTTP_AUTHORIZATION" => "Basic #{Base64.encode64("#{token}:x")}"
78
+ assert_response :success
79
+ assert_match "<email>user@test.com</email>", response.body
80
+ assert warden.authenticated?(:user)
81
+ end
82
+
83
+ private
84
+
85
+ def sign_in_as_new_user_with_http(username="user@test.com", password="123456")
86
+ user = create_user
87
+ get users_path(:format => :xml), {}, "HTTP_AUTHORIZATION" => "Basic #{Base64.encode64("#{username}:#{password}")}"
88
+ user
89
+ end
90
+
91
+ # Sign in with oauth2 token. This is just to test that it isn't misinterpreted as basic authentication
92
+ def add_oauth2_header
93
+ user = create_user
94
+ get users_path(:format => :xml), {}, "HTTP_AUTHORIZATION" => "OAuth #{Base64.encode64("#{user.email}:123456")}"
95
+ end
96
+
97
+ end
@@ -0,0 +1,224 @@
1
+ require 'test_helper'
2
+
3
+ class LockTest < ActionController::IntegrationTest
4
+
5
+ def visit_user_unlock_with_token(unlock_token)
6
+ visit user_unlock_path(:unlock_token => unlock_token)
7
+ end
8
+
9
+ def send_unlock_request
10
+ user = create_user(:locked => true)
11
+ ActionMailer::Base.deliveries.clear
12
+
13
+ visit new_user_session_path
14
+ click_link "Didn't receive unlock instructions?"
15
+
16
+ fill_in 'email', :with => user.email
17
+ click_button 'Resend unlock instructions'
18
+ end
19
+
20
+ test 'user should be able to request a new unlock token' do
21
+ send_unlock_request
22
+
23
+ assert_template 'sessions/new'
24
+ assert_contain 'You will receive an email with instructions about how to unlock your account in a few minutes'
25
+ assert_equal 1, ActionMailer::Base.deliveries.size
26
+ assert_equal ['please-change-me@config-initializers-devise.com'], ActionMailer::Base.deliveries.first.from
27
+ end
28
+
29
+ test 'user should receive the instructions from a custom mailer' do
30
+ User.any_instance.stubs(:devise_mailer).returns(Users::Mailer)
31
+
32
+ send_unlock_request
33
+
34
+ assert_equal ['custom@example.com'], ActionMailer::Base.deliveries.first.from
35
+ end
36
+
37
+ test 'unlocked user should not be able to request a unlock token' do
38
+ user = create_user(:locked => false)
39
+ ActionMailer::Base.deliveries.clear
40
+
41
+ visit new_user_session_path
42
+ click_link "Didn't receive unlock instructions?"
43
+
44
+ fill_in 'email', :with => user.email
45
+ click_button 'Resend unlock instructions'
46
+
47
+ assert_template 'unlocks/new'
48
+ assert_contain 'not locked'
49
+ assert_equal 0, ActionMailer::Base.deliveries.size
50
+ end
51
+
52
+ test 'unlocked pages should not be available if email strategy is disabled' do
53
+ visit "/admin_area/sign_in"
54
+
55
+ assert_raise Webrat::NotFoundError do
56
+ click_link "Didn't receive unlock instructions?"
57
+ end
58
+
59
+ assert_raise NameError do
60
+ visit new_admin_unlock_path
61
+ end
62
+
63
+ assert_raise ActionController::RoutingError do
64
+ visit "/admin_area/unlock/new"
65
+ end
66
+ end
67
+
68
+ test 'user with invalid unlock token should not be able to unlock an account' do
69
+ visit_user_unlock_with_token('invalid_token')
70
+
71
+ assert_response :success
72
+ assert_current_url '/users/unlock?unlock_token=invalid_token'
73
+ assert_have_selector '#error_explanation'
74
+ assert_contain /Unlock token(.*)invalid/
75
+ end
76
+
77
+ test "locked user should be able to unlock account" do
78
+ user = create_user(:locked => true)
79
+ assert user.access_locked?
80
+
81
+ visit_user_unlock_with_token(user.unlock_token)
82
+
83
+ assert_current_url "/users/sign_in"
84
+ assert_contain 'Your account has been unlocked successfully. Please sign in to continue.'
85
+
86
+ assert_not user.reload.access_locked?
87
+ end
88
+
89
+ test "redirect user to sign in page after unlocking its account" do
90
+ user = create_user(:locked => true)
91
+ visit_user_unlock_with_token(user.unlock_token)
92
+ assert_not warden.authenticated?(:user)
93
+ end
94
+
95
+ test "user should not send a new e-mail if already locked" do
96
+ user = create_user(:locked => true)
97
+ user.failed_attempts = User.maximum_attempts + 1
98
+ user.save!
99
+
100
+ ActionMailer::Base.deliveries.clear
101
+
102
+ sign_in_as_user(:password => "invalid")
103
+ assert_contain 'Your account is locked.'
104
+ assert ActionMailer::Base.deliveries.empty?
105
+ end
106
+
107
+ test 'error message is configurable by resource name' do
108
+ store_translations :en, :devise => {
109
+ :failure => {:user => {:locked => "You are locked!"}}
110
+ } do
111
+
112
+ user = create_user(:locked => true)
113
+ user.failed_attempts = User.maximum_attempts + 1
114
+ user.save!
115
+
116
+ sign_in_as_user(:password => "invalid")
117
+ assert_contain "You are locked!"
118
+ end
119
+ end
120
+
121
+ test "user should not be able to sign in when locked" do
122
+ store_translations :en, :devise => {
123
+ :failure => {:user => {:locked => "You are locked!"}}
124
+ } do
125
+
126
+ user = create_user(:locked => true)
127
+ user.failed_attempts = User.maximum_attempts + 1
128
+ user.save!
129
+
130
+ sign_in_as_user(:password => "123456")
131
+ assert_contain "You are locked!"
132
+ end
133
+ end
134
+
135
+ test 'user should be able to request a new unlock token via XML request' do
136
+ user = create_user(:locked => true)
137
+ ActionMailer::Base.deliveries.clear
138
+
139
+ post user_unlock_path(:format => 'xml'), :user => {:email => user.email}
140
+ assert_response :success
141
+ assert_equal response.body, {}.to_xml
142
+ assert_equal 1, ActionMailer::Base.deliveries.size
143
+ end
144
+
145
+ test 'unlocked user should not be able to request a unlock token via XML request' do
146
+ user = create_user(:locked => false)
147
+ ActionMailer::Base.deliveries.clear
148
+
149
+ post user_unlock_path(:format => 'xml'), :user => {:email => user.email}
150
+ assert_response :unprocessable_entity
151
+ assert response.body.include? %(<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<errors>)
152
+ assert_equal 0, ActionMailer::Base.deliveries.size
153
+ end
154
+
155
+ test 'user with valid unlock token should be able to unlock account via XML request' do
156
+ user = create_user(:locked => true)
157
+ assert user.access_locked?
158
+ get user_unlock_path(:format => 'xml', :unlock_token => user.unlock_token)
159
+ assert_response :success
160
+ assert response.body.include? %(<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<user>)
161
+ end
162
+
163
+
164
+ test 'user with invalid unlock token should not be able to unlock the account via XML request' do
165
+ get user_unlock_path(:format => 'xml', :unlock_token => 'invalid_token')
166
+ assert_response :unprocessable_entity
167
+ assert response.body.include? %(<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<errors>)
168
+ end
169
+
170
+ test "when using json to ask a unlock request, should not return the user" do
171
+ user = create_user(:locked => true)
172
+ post user_unlock_path(:format => "json", :user => {:email => user.email})
173
+ assert_response :success
174
+ assert_equal response.body, {}.to_json
175
+ end
176
+
177
+ test "in paranoid mode, when trying to unlock an user that exists it should not say that it exists if it is locked" do
178
+ swap Devise, :paranoid => true do
179
+ user = create_user(:locked => true)
180
+
181
+ visit new_user_session_path
182
+ click_link "Didn't receive unlock instructions?"
183
+
184
+ fill_in 'email', :with => user.email
185
+ click_button 'Resend unlock instructions'
186
+
187
+ assert_current_url "/users/sign_in"
188
+ assert_contain "If your account exists, you will receive an email with instructions about how to unlock it in a few minutes."
189
+ end
190
+ end
191
+
192
+ test "in paranoid mode, when trying to unlock an user that exists it should not say that it exists if it is not locked" do
193
+ swap Devise, :paranoid => true do
194
+ user = create_user(:locked => false)
195
+
196
+ visit new_user_session_path
197
+ click_link "Didn't receive unlock instructions?"
198
+
199
+ fill_in 'email', :with => user.email
200
+ click_button 'Resend unlock instructions'
201
+
202
+ assert_current_url "/users/sign_in"
203
+ assert_contain "If your account exists, you will receive an email with instructions about how to unlock it in a few minutes."
204
+ end
205
+ end
206
+
207
+ test "in paranoid mode, when trying to unlock an user that does not exists it should not say that it does not exists" do
208
+ swap Devise, :paranoid => true do
209
+ visit new_user_session_path
210
+ click_link "Didn't receive unlock instructions?"
211
+
212
+ fill_in 'email', :with => "arandomemail@hotmail.com"
213
+ click_button 'Resend unlock instructions'
214
+
215
+ assert_not_contain "1 error prohibited this user from being saved:"
216
+ assert_not_contain "Email not found"
217
+ assert_current_url "/users/sign_in"
218
+
219
+ assert_contain "If your account exists, you will receive an email with instructions about how to unlock it in a few minutes."
220
+
221
+ end
222
+ end
223
+
224
+ end