cloudfoundry-devise 1.5.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (210) hide show
  1. data/.gitignore +12 -0
  2. data/.travis.yml +13 -0
  3. data/CHANGELOG.rdoc +755 -0
  4. data/Gemfile +35 -0
  5. data/MIT-LICENSE +20 -0
  6. data/README.rdoc +366 -0
  7. data/Rakefile +34 -0
  8. data/app/controllers/devise/confirmations_controller.rb +46 -0
  9. data/app/controllers/devise/omniauth_callbacks_controller.rb +26 -0
  10. data/app/controllers/devise/passwords_controller.rb +50 -0
  11. data/app/controllers/devise/registrations_controller.rb +114 -0
  12. data/app/controllers/devise/sessions_controller.rb +49 -0
  13. data/app/controllers/devise/unlocks_controller.rb +34 -0
  14. data/app/helpers/devise_helper.rb +25 -0
  15. data/app/mailers/devise/mailer.rb +15 -0
  16. data/app/views/devise/confirmations/new.html.erb +12 -0
  17. data/app/views/devise/mailer/confirmation_instructions.html.erb +5 -0
  18. data/app/views/devise/mailer/reset_password_instructions.html.erb +8 -0
  19. data/app/views/devise/mailer/unlock_instructions.html.erb +7 -0
  20. data/app/views/devise/passwords/edit.html.erb +16 -0
  21. data/app/views/devise/passwords/new.html.erb +12 -0
  22. data/app/views/devise/registrations/edit.html.erb +25 -0
  23. data/app/views/devise/registrations/new.html.erb +18 -0
  24. data/app/views/devise/sessions/new.html.erb +17 -0
  25. data/app/views/devise/shared/_links.erb +25 -0
  26. data/app/views/devise/unlocks/new.html.erb +12 -0
  27. data/cloudfoundry-devise.gemspec +25 -0
  28. data/config/locales/en.yml +59 -0
  29. data/lib/devise.rb +453 -0
  30. data/lib/devise/controllers/helpers.rb +260 -0
  31. data/lib/devise/controllers/internal_helpers.rb +161 -0
  32. data/lib/devise/controllers/rememberable.rb +52 -0
  33. data/lib/devise/controllers/scoped_views.rb +33 -0
  34. data/lib/devise/controllers/shared_helpers.rb +26 -0
  35. data/lib/devise/controllers/url_helpers.rb +53 -0
  36. data/lib/devise/delegator.rb +16 -0
  37. data/lib/devise/encryptors/authlogic_sha512.rb +19 -0
  38. data/lib/devise/encryptors/base.rb +20 -0
  39. data/lib/devise/encryptors/clearance_sha1.rb +17 -0
  40. data/lib/devise/encryptors/restful_authentication_sha1.rb +22 -0
  41. data/lib/devise/encryptors/sha1.rb +25 -0
  42. data/lib/devise/encryptors/sha512.rb +25 -0
  43. data/lib/devise/failure_app.rb +149 -0
  44. data/lib/devise/hooks/activatable.rb +11 -0
  45. data/lib/devise/hooks/forgetable.rb +9 -0
  46. data/lib/devise/hooks/rememberable.rb +6 -0
  47. data/lib/devise/hooks/timeoutable.rb +24 -0
  48. data/lib/devise/hooks/trackable.rb +9 -0
  49. data/lib/devise/mailers/helpers.rb +86 -0
  50. data/lib/devise/mapping.rb +175 -0
  51. data/lib/devise/models.rb +91 -0
  52. data/lib/devise/models/authenticatable.rb +181 -0
  53. data/lib/devise/models/confirmable.rb +220 -0
  54. data/lib/devise/models/database_authenticatable.rb +122 -0
  55. data/lib/devise/models/encryptable.rb +72 -0
  56. data/lib/devise/models/lockable.rb +169 -0
  57. data/lib/devise/models/omniauthable.rb +23 -0
  58. data/lib/devise/models/recoverable.rb +136 -0
  59. data/lib/devise/models/registerable.rb +21 -0
  60. data/lib/devise/models/rememberable.rb +114 -0
  61. data/lib/devise/models/serializable.rb +43 -0
  62. data/lib/devise/models/timeoutable.rb +45 -0
  63. data/lib/devise/models/token_authenticatable.rb +72 -0
  64. data/lib/devise/models/trackable.rb +30 -0
  65. data/lib/devise/models/validatable.rb +62 -0
  66. data/lib/devise/modules.rb +30 -0
  67. data/lib/devise/omniauth.rb +28 -0
  68. data/lib/devise/omniauth/config.rb +45 -0
  69. data/lib/devise/omniauth/url_helpers.rb +33 -0
  70. data/lib/devise/orm/active_record.rb +44 -0
  71. data/lib/devise/orm/mongoid.rb +31 -0
  72. data/lib/devise/param_filter.rb +41 -0
  73. data/lib/devise/path_checker.rb +18 -0
  74. data/lib/devise/rails.rb +73 -0
  75. data/lib/devise/rails/routes.rb +385 -0
  76. data/lib/devise/rails/warden_compat.rb +120 -0
  77. data/lib/devise/schema.rb +109 -0
  78. data/lib/devise/strategies/authenticatable.rb +155 -0
  79. data/lib/devise/strategies/base.rb +15 -0
  80. data/lib/devise/strategies/database_authenticatable.rb +21 -0
  81. data/lib/devise/strategies/rememberable.rb +53 -0
  82. data/lib/devise/strategies/token_authenticatable.rb +57 -0
  83. data/lib/devise/test_helpers.rb +90 -0
  84. data/lib/devise/version.rb +3 -0
  85. data/lib/generators/active_record/devise_generator.rb +71 -0
  86. data/lib/generators/active_record/templates/migration.rb +29 -0
  87. data/lib/generators/active_record/templates/migration_existing.rb +26 -0
  88. data/lib/generators/devise/devise_generator.rb +22 -0
  89. data/lib/generators/devise/install_generator.rb +24 -0
  90. data/lib/generators/devise/orm_helpers.rb +31 -0
  91. data/lib/generators/devise/views_generator.rb +98 -0
  92. data/lib/generators/mongoid/devise_generator.rb +60 -0
  93. data/lib/generators/templates/README +32 -0
  94. data/lib/generators/templates/devise.rb +215 -0
  95. data/lib/generators/templates/markerb/confirmation_instructions.markerb +5 -0
  96. data/lib/generators/templates/markerb/reset_password_instructions.markerb +8 -0
  97. data/lib/generators/templates/markerb/unlock_instructions.markerb +7 -0
  98. data/lib/generators/templates/simple_form_for/confirmations/new.html.erb +15 -0
  99. data/lib/generators/templates/simple_form_for/passwords/edit.html.erb +19 -0
  100. data/lib/generators/templates/simple_form_for/passwords/new.html.erb +15 -0
  101. data/lib/generators/templates/simple_form_for/registrations/edit.html.erb +22 -0
  102. data/lib/generators/templates/simple_form_for/registrations/new.html.erb +17 -0
  103. data/lib/generators/templates/simple_form_for/sessions/new.html.erb +15 -0
  104. data/lib/generators/templates/simple_form_for/unlocks/new.html.erb +15 -0
  105. data/test/controllers/helpers_test.rb +254 -0
  106. data/test/controllers/internal_helpers_test.rb +96 -0
  107. data/test/controllers/sessions_controller_test.rb +16 -0
  108. data/test/controllers/url_helpers_test.rb +59 -0
  109. data/test/delegator_test.rb +19 -0
  110. data/test/devise_test.rb +72 -0
  111. data/test/encryptors_test.rb +30 -0
  112. data/test/failure_app_test.rb +207 -0
  113. data/test/generators/active_record_generator_test.rb +47 -0
  114. data/test/generators/devise_generator_test.rb +39 -0
  115. data/test/generators/install_generator_test.rb +13 -0
  116. data/test/generators/mongoid_generator_test.rb +23 -0
  117. data/test/generators/views_generator_test.rb +52 -0
  118. data/test/helpers/devise_helper_test.rb +51 -0
  119. data/test/indifferent_hash.rb +33 -0
  120. data/test/integration/authenticatable_test.rb +590 -0
  121. data/test/integration/confirmable_test.rb +262 -0
  122. data/test/integration/database_authenticatable_test.rb +82 -0
  123. data/test/integration/http_authenticatable_test.rb +82 -0
  124. data/test/integration/lockable_test.rb +212 -0
  125. data/test/integration/omniauthable_test.rb +133 -0
  126. data/test/integration/recoverable_test.rb +287 -0
  127. data/test/integration/registerable_test.rb +335 -0
  128. data/test/integration/rememberable_test.rb +158 -0
  129. data/test/integration/timeoutable_test.rb +98 -0
  130. data/test/integration/token_authenticatable_test.rb +148 -0
  131. data/test/integration/trackable_test.rb +92 -0
  132. data/test/mailers/confirmation_instructions_test.rb +95 -0
  133. data/test/mailers/reset_password_instructions_test.rb +83 -0
  134. data/test/mailers/unlock_instructions_test.rb +77 -0
  135. data/test/mapping_test.rb +128 -0
  136. data/test/models/confirmable_test.rb +334 -0
  137. data/test/models/database_authenticatable_test.rb +167 -0
  138. data/test/models/encryptable_test.rb +67 -0
  139. data/test/models/lockable_test.rb +225 -0
  140. data/test/models/recoverable_test.rb +198 -0
  141. data/test/models/rememberable_test.rb +168 -0
  142. data/test/models/serializable_test.rb +38 -0
  143. data/test/models/timeoutable_test.rb +42 -0
  144. data/test/models/token_authenticatable_test.rb +49 -0
  145. data/test/models/trackable_test.rb +5 -0
  146. data/test/models/validatable_test.rb +113 -0
  147. data/test/models_test.rb +109 -0
  148. data/test/omniauth/config_test.rb +57 -0
  149. data/test/omniauth/url_helpers_test.rb +58 -0
  150. data/test/orm/active_record.rb +9 -0
  151. data/test/orm/mongoid.rb +14 -0
  152. data/test/rails_app/Rakefile +10 -0
  153. data/test/rails_app/app/active_record/admin.rb +6 -0
  154. data/test/rails_app/app/active_record/shim.rb +2 -0
  155. data/test/rails_app/app/active_record/user.rb +6 -0
  156. data/test/rails_app/app/controllers/admins/sessions_controller.rb +6 -0
  157. data/test/rails_app/app/controllers/admins_controller.rb +6 -0
  158. data/test/rails_app/app/controllers/application_controller.rb +8 -0
  159. data/test/rails_app/app/controllers/home_controller.rb +25 -0
  160. data/test/rails_app/app/controllers/publisher/registrations_controller.rb +2 -0
  161. data/test/rails_app/app/controllers/publisher/sessions_controller.rb +2 -0
  162. data/test/rails_app/app/controllers/users/omniauth_callbacks_controller.rb +14 -0
  163. data/test/rails_app/app/controllers/users_controller.rb +23 -0
  164. data/test/rails_app/app/helpers/application_helper.rb +3 -0
  165. data/test/rails_app/app/mailers/users/mailer.rb +3 -0
  166. data/test/rails_app/app/mongoid/admin.rb +24 -0
  167. data/test/rails_app/app/mongoid/shim.rb +24 -0
  168. data/test/rails_app/app/mongoid/user.rb +45 -0
  169. data/test/rails_app/app/views/admins/index.html.erb +1 -0
  170. data/test/rails_app/app/views/admins/sessions/new.html.erb +2 -0
  171. data/test/rails_app/app/views/home/admin_dashboard.html.erb +1 -0
  172. data/test/rails_app/app/views/home/index.html.erb +1 -0
  173. data/test/rails_app/app/views/home/join.html.erb +1 -0
  174. data/test/rails_app/app/views/home/private.html.erb +1 -0
  175. data/test/rails_app/app/views/home/user_dashboard.html.erb +1 -0
  176. data/test/rails_app/app/views/layouts/application.html.erb +24 -0
  177. data/test/rails_app/app/views/users/index.html.erb +1 -0
  178. data/test/rails_app/app/views/users/mailer/confirmation_instructions.erb +1 -0
  179. data/test/rails_app/app/views/users/sessions/new.html.erb +1 -0
  180. data/test/rails_app/config.ru +4 -0
  181. data/test/rails_app/config/application.rb +41 -0
  182. data/test/rails_app/config/boot.rb +8 -0
  183. data/test/rails_app/config/database.yml +18 -0
  184. data/test/rails_app/config/environment.rb +5 -0
  185. data/test/rails_app/config/environments/development.rb +18 -0
  186. data/test/rails_app/config/environments/production.rb +33 -0
  187. data/test/rails_app/config/environments/test.rb +33 -0
  188. data/test/rails_app/config/initializers/backtrace_silencers.rb +7 -0
  189. data/test/rails_app/config/initializers/devise.rb +197 -0
  190. data/test/rails_app/config/initializers/inflections.rb +2 -0
  191. data/test/rails_app/config/initializers/secret_token.rb +2 -0
  192. data/test/rails_app/config/routes.rb +87 -0
  193. data/test/rails_app/db/migrate/20100401102949_create_tables.rb +71 -0
  194. data/test/rails_app/db/schema.rb +52 -0
  195. data/test/rails_app/lib/shared_admin.rb +10 -0
  196. data/test/rails_app/lib/shared_user.rb +26 -0
  197. data/test/rails_app/public/404.html +26 -0
  198. data/test/rails_app/public/422.html +26 -0
  199. data/test/rails_app/public/500.html +26 -0
  200. data/test/rails_app/public/favicon.ico +0 -0
  201. data/test/rails_app/script/rails +10 -0
  202. data/test/routes_test.rb +240 -0
  203. data/test/support/assertions.rb +27 -0
  204. data/test/support/helpers.rb +109 -0
  205. data/test/support/integration.rb +88 -0
  206. data/test/support/locale/en.yml +4 -0
  207. data/test/support/webrat/integrations/rails.rb +24 -0
  208. data/test/test_helper.rb +27 -0
  209. data/test/test_helpers_test.rb +134 -0
  210. metadata +295 -0
@@ -0,0 +1,262 @@
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, :confirm_within => 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, :confirm_within => 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, :confirm_within => 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 e-mail exists on 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 e-mail exists on 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 < ConfirmationTest
206
+
207
+ def create_second_user(options={})
208
+ @user = nil
209
+ create_user(options)
210
+ end
211
+
212
+ def setup
213
+ add_unconfirmed_email_column
214
+ Devise.reconfirmable = true
215
+ end
216
+
217
+ def teardown
218
+ remove_unconfirmed_email_column
219
+ Devise.reconfirmable = false
220
+ end
221
+
222
+ test 'user should be able to request a new confirmation after email changed' do
223
+ user = create_user(:confirm => true)
224
+ user.update_attributes(:email => 'new_test@example.com')
225
+
226
+ visit new_user_session_path
227
+ click_link "Didn't receive confirmation instructions?"
228
+
229
+ fill_in 'email', :with => user.unconfirmed_email
230
+ assert_difference "ActionMailer::Base.deliveries.size" do
231
+ click_button 'Resend confirmation instructions'
232
+ end
233
+
234
+ assert_current_url '/users/sign_in'
235
+ assert_contain 'You will receive an email with instructions about how to confirm your account in a few minutes'
236
+ end
237
+
238
+ test 'user with valid confirmation token should be able to confirm email after email changed' do
239
+ user = create_user(:confirm => true)
240
+ user.update_attributes(:email => 'new_test@example.com')
241
+ assert 'new_test@example.com', user.unconfirmed_email
242
+ visit_user_confirmation_with_token(user.confirmation_token)
243
+
244
+ assert_contain 'Your account was successfully confirmed.'
245
+ assert_current_url '/'
246
+ assert user.reload.confirmed?
247
+ assert_not user.reload.pending_reconfirmation?
248
+ end
249
+
250
+ test 'user email should be unique also within unconfirmed_email' do
251
+ user = create_user(:confirm => true)
252
+ user.update_attributes(:email => 'new_test@example.com')
253
+ assert 'new_test@example.com', user.unconfirmed_email
254
+
255
+ create_second_user(:email => "new_test@example.com")
256
+
257
+ visit_user_confirmation_with_token(user.confirmation_token)
258
+ assert_have_selector '#error_explanation'
259
+ assert_contain /Email.*already.*taken/
260
+ assert user.reload.pending_reconfirmation?
261
+ end
262
+ 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,82 @@
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 #{ActiveSupport::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 :success
16
+ assert_match '<email>user@test.com</email>', response.body
17
+ assert warden.authenticated?(:user)
18
+ end
19
+
20
+ test 'returns a custom response with www-authenticate header on failures' do
21
+ sign_in_as_new_user_with_http("unknown")
22
+ assert_equal 401, status
23
+ assert_equal 'Basic realm="Application"', headers["WWW-Authenticate"]
24
+ end
25
+
26
+ test 'uses the request format as response content type' do
27
+ sign_in_as_new_user_with_http("unknown")
28
+ assert_equal 401, status
29
+ assert_equal "application/xml; charset=utf-8", headers["Content-Type"]
30
+ assert_match "<error>Invalid email or password.</error>", response.body
31
+ end
32
+
33
+ test 'returns a custom response with www-authenticate and chosen realm' do
34
+ swap Devise, :http_authentication_realm => "MyApp" do
35
+ sign_in_as_new_user_with_http("unknown")
36
+ assert_equal 401, status
37
+ assert_equal 'Basic realm="MyApp"', headers["WWW-Authenticate"]
38
+ end
39
+ end
40
+
41
+ test 'sign in should authenticate with http even with specific authentication keys' do
42
+ swap Devise, :authentication_keys => [:username] do
43
+ sign_in_as_new_user_with_http("usertest")
44
+ assert_response :success
45
+ assert_match '<email>user@test.com</email>', response.body
46
+ assert warden.authenticated?(:user)
47
+ end
48
+ end
49
+
50
+ test 'test request with oauth2 header doesnt get mistaken for basic authentication' do
51
+ swap Devise, :http_authenticatable => true do
52
+ add_oauth2_header
53
+ assert_equal 401, status
54
+ assert_equal 'Basic realm="Application"', headers["WWW-Authenticate"]
55
+ end
56
+ end
57
+
58
+ test 'sign in should authenticate with really long token' do
59
+ token = "token_containing_so_many_characters_that_the_base64_encoding_will_wrap"
60
+ user = create_user
61
+ user.update_attribute :authentication_token, token
62
+ get users_path(:format => :xml), {}, "HTTP_AUTHORIZATION" => "Basic #{ActiveSupport::Base64.encode64("#{token}:x")}"
63
+ assert_response :success
64
+ assert_match "<email>user@test.com</email>", response.body
65
+ assert warden.authenticated?(:user)
66
+ end
67
+
68
+ private
69
+
70
+ def sign_in_as_new_user_with_http(username="user@test.com", password="123456")
71
+ user = create_user
72
+ get users_path(:format => :xml), {}, "HTTP_AUTHORIZATION" => "Basic #{ActiveSupport::Base64.encode64("#{username}:#{password}")}"
73
+ user
74
+ end
75
+
76
+ # Sign in with oauth2 token. This is just to test that it isn't misinterpreted as basic authentication
77
+ def add_oauth2_header
78
+ user = create_user
79
+ get users_path(:format => :xml), {}, "HTTP_AUTHORIZATION" => "OAuth #{ActiveSupport::Base64.encode64("#{user.email}:123456")}"
80
+ end
81
+
82
+ end
@@ -0,0 +1,212 @@
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 '/'
84
+ assert_contain 'Your account was successfully unlocked.'
85
+
86
+ assert_not user.reload.access_locked?
87
+ end
88
+
89
+ test "sign in user automatically after unlocking its account" do
90
+ user = create_user(:locked => true)
91
+ visit_user_unlock_with_token(user.unlock_token)
92
+ assert warden.authenticated?(:user)
93
+ end
94
+
95
+ test "user should not be able to sign in when locked" do
96
+ user = sign_in_as_user(:locked => true)
97
+ assert_template 'sessions/new'
98
+ assert_contain 'Your account is locked.'
99
+ assert_not warden.authenticated?(:user)
100
+ end
101
+
102
+ test "user should not send a new e-mail if already locked" do
103
+ user = create_user(:locked => true)
104
+ user.failed_attempts = User.maximum_attempts + 1
105
+ user.save!
106
+
107
+ ActionMailer::Base.deliveries.clear
108
+
109
+ sign_in_as_user(:password => "invalid")
110
+ assert_contain 'Your account is locked.'
111
+ assert ActionMailer::Base.deliveries.empty?
112
+ end
113
+
114
+ test 'error message is configurable by resource name' do
115
+ store_translations :en, :devise => {
116
+ :failure => { :user => { :locked => "You are locked!" } }
117
+ } do
118
+ user = sign_in_as_user(:locked => true)
119
+ assert_contain 'You are locked!'
120
+ end
121
+ end
122
+
123
+ test 'user should be able to request a new unlock token via XML request' do
124
+ user = create_user(:locked => true)
125
+ ActionMailer::Base.deliveries.clear
126
+
127
+ post user_unlock_path(:format => 'xml'), :user => {:email => user.email}
128
+ assert_response :success
129
+ assert_equal response.body, {}.to_xml
130
+ assert_equal 1, ActionMailer::Base.deliveries.size
131
+ end
132
+
133
+ test 'unlocked user should not be able to request a unlock token via XML request' do
134
+ user = create_user(:locked => false)
135
+ ActionMailer::Base.deliveries.clear
136
+
137
+ post user_unlock_path(:format => 'xml'), :user => {:email => user.email}
138
+ assert_response :unprocessable_entity
139
+ assert response.body.include? %(<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<errors>)
140
+ assert_equal 0, ActionMailer::Base.deliveries.size
141
+ end
142
+
143
+ test 'user with valid unlock token should be able to unlock account via XML request' do
144
+ user = create_user(:locked => true)
145
+ assert user.access_locked?
146
+ get user_unlock_path(:format => 'xml', :unlock_token => user.unlock_token)
147
+ assert_response :success
148
+ assert response.body.include? %(<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<user>)
149
+ end
150
+
151
+
152
+ test 'user with invalid unlock token should not be able to unlock the account via XML request' do
153
+ get user_unlock_path(:format => 'xml', :unlock_token => 'invalid_token')
154
+ assert_response :unprocessable_entity
155
+ assert response.body.include? %(<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<errors>)
156
+ end
157
+
158
+ test "when using json to ask a unlock request, should not return the user" do
159
+ user = create_user(:locked => true)
160
+ post user_unlock_path(:format => "json", :user => {:email => user.email})
161
+ assert_response :success
162
+ assert_equal response.body, {}.to_json
163
+ end
164
+
165
+ test "in paranoid mode, when trying to unlock an user that exists it should not say that it exists if it is locked" do
166
+ swap Devise, :paranoid => true do
167
+ user = create_user(:locked => true)
168
+
169
+ visit new_user_session_path
170
+ click_link "Didn't receive unlock instructions?"
171
+
172
+ fill_in 'email', :with => user.email
173
+ click_button 'Resend unlock instructions'
174
+
175
+ assert_current_url "/users/sign_in"
176
+ assert_contain "If your account exists, you will receive an email with instructions about how to unlock it in a few minutes."
177
+ end
178
+ end
179
+
180
+ 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
181
+ swap Devise, :paranoid => true do
182
+ user = create_user(:locked => false)
183
+
184
+ visit new_user_session_path
185
+ click_link "Didn't receive unlock instructions?"
186
+
187
+ fill_in 'email', :with => user.email
188
+ click_button 'Resend unlock instructions'
189
+
190
+ assert_current_url "/users/sign_in"
191
+ assert_contain "If your account exists, you will receive an email with instructions about how to unlock it in a few minutes."
192
+ end
193
+ end
194
+
195
+ test "in paranoid mode, when trying to unlock an user that does not exists it should not say that it does not exists" do
196
+ swap Devise, :paranoid => true do
197
+ visit new_user_session_path
198
+ click_link "Didn't receive unlock instructions?"
199
+
200
+ fill_in 'email', :with => "arandomemail@hotmail.com"
201
+ click_button 'Resend unlock instructions'
202
+
203
+ assert_not_contain "1 error prohibited this user from being saved:"
204
+ assert_not_contain "Email not found"
205
+ assert_current_url "/users/sign_in"
206
+
207
+ assert_contain "If your account exists, you will receive an email with instructions about how to unlock it in a few minutes."
208
+
209
+ end
210
+ end
211
+
212
+ end