loyal_devise 2.1.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (208) hide show
  1. data/.gitignore +10 -0
  2. data/.travis.yml +15 -0
  3. data/CHANGELOG.rdoc +881 -0
  4. data/CONTRIBUTING.md +12 -0
  5. data/Gemfile +31 -0
  6. data/Gemfile.lock +154 -0
  7. data/MIT-LICENSE +20 -0
  8. data/README.md +388 -0
  9. data/Rakefile +34 -0
  10. data/app/controllers/devise/confirmations_controller.rb +44 -0
  11. data/app/controllers/devise/omniauth_callbacks_controller.rb +31 -0
  12. data/app/controllers/devise/passwords_controller.rb +57 -0
  13. data/app/controllers/devise/registrations_controller.rb +120 -0
  14. data/app/controllers/devise/sessions_controller.rb +51 -0
  15. data/app/controllers/devise/unlocks_controller.rb +45 -0
  16. data/app/controllers/devise_controller.rb +193 -0
  17. data/app/helpers/devise_helper.rb +26 -0
  18. data/app/mailers/devise/mailer.rb +16 -0
  19. data/app/views/devise/_links.erb +3 -0
  20. data/app/views/devise/confirmations/new.html.erb +12 -0
  21. data/app/views/devise/mailer/confirmation_instructions.html.erb +5 -0
  22. data/app/views/devise/mailer/reset_password_instructions.html.erb +8 -0
  23. data/app/views/devise/mailer/unlock_instructions.html.erb +7 -0
  24. data/app/views/devise/passwords/edit.html.erb +16 -0
  25. data/app/views/devise/passwords/new.html.erb +12 -0
  26. data/app/views/devise/registrations/edit.html.erb +25 -0
  27. data/app/views/devise/registrations/new.html.erb +18 -0
  28. data/app/views/devise/sessions/new.html.erb +17 -0
  29. data/app/views/devise/shared/_links.erb +25 -0
  30. data/app/views/devise/unlocks/new.html.erb +12 -0
  31. data/config/locales/en.yml +59 -0
  32. data/devise.gemspec +26 -0
  33. data/gemfiles/Gemfile.rails-3.1.x +35 -0
  34. data/gemfiles/Gemfile.rails-3.1.x.lock +167 -0
  35. data/lib/devise/controllers/helpers.rb +273 -0
  36. data/lib/devise/controllers/rememberable.rb +53 -0
  37. data/lib/devise/controllers/scoped_views.rb +18 -0
  38. data/lib/devise/controllers/url_helpers.rb +68 -0
  39. data/lib/devise/delegator.rb +17 -0
  40. data/lib/devise/failure_app.rb +188 -0
  41. data/lib/devise/hooks/activatable.rb +12 -0
  42. data/lib/devise/hooks/forgetable.rb +10 -0
  43. data/lib/devise/hooks/lockable.rb +8 -0
  44. data/lib/devise/hooks/rememberable.rb +7 -0
  45. data/lib/devise/hooks/timeoutable.rb +26 -0
  46. data/lib/devise/hooks/trackable.rb +10 -0
  47. data/lib/devise/mailers/helpers.rb +92 -0
  48. data/lib/devise/mapping.rb +173 -0
  49. data/lib/devise/models/authenticatable.rb +269 -0
  50. data/lib/devise/models/confirmable.rb +271 -0
  51. data/lib/devise/models/database_authenticatable.rb +127 -0
  52. data/lib/devise/models/lockable.rb +194 -0
  53. data/lib/devise/models/omniauthable.rb +28 -0
  54. data/lib/devise/models/recoverable.rb +141 -0
  55. data/lib/devise/models/registerable.rb +26 -0
  56. data/lib/devise/models/rememberable.rb +126 -0
  57. data/lib/devise/models/timeoutable.rb +50 -0
  58. data/lib/devise/models/token_authenticatable.rb +90 -0
  59. data/lib/devise/models/trackable.rb +36 -0
  60. data/lib/devise/models/validatable.rb +67 -0
  61. data/lib/devise/models.rb +129 -0
  62. data/lib/devise/modules.rb +30 -0
  63. data/lib/devise/omniauth/config.rb +46 -0
  64. data/lib/devise/omniauth/url_helpers.rb +19 -0
  65. data/lib/devise/omniauth.rb +29 -0
  66. data/lib/devise/orm/active_record.rb +4 -0
  67. data/lib/devise/orm/mongoid.rb +4 -0
  68. data/lib/devise/param_filter.rb +42 -0
  69. data/lib/devise/rails/routes.rb +447 -0
  70. data/lib/devise/rails/warden_compat.rb +44 -0
  71. data/lib/devise/rails.rb +55 -0
  72. data/lib/devise/strategies/authenticatable.rb +177 -0
  73. data/lib/devise/strategies/base.rb +21 -0
  74. data/lib/devise/strategies/database_authenticatable.rb +21 -0
  75. data/lib/devise/strategies/rememberable.rb +56 -0
  76. data/lib/devise/strategies/token_authenticatable.rb +57 -0
  77. data/lib/devise/test_helpers.rb +132 -0
  78. data/lib/devise/time_inflector.rb +15 -0
  79. data/lib/devise/version.rb +4 -0
  80. data/lib/devise.rb +445 -0
  81. data/lib/generators/active_record/devise_generator.rb +80 -0
  82. data/lib/generators/active_record/templates/migration.rb +20 -0
  83. data/lib/generators/active_record/templates/migration_existing.rb +27 -0
  84. data/lib/generators/devise/devise_generator.rb +25 -0
  85. data/lib/generators/devise/install_generator.rb +25 -0
  86. data/lib/generators/devise/orm_helpers.rb +33 -0
  87. data/lib/generators/devise/views_generator.rb +117 -0
  88. data/lib/generators/mongoid/devise_generator.rb +58 -0
  89. data/lib/generators/templates/README +35 -0
  90. data/lib/generators/templates/devise.rb +241 -0
  91. data/lib/generators/templates/markerb/confirmation_instructions.markerb +5 -0
  92. data/lib/generators/templates/markerb/reset_password_instructions.markerb +8 -0
  93. data/lib/generators/templates/markerb/unlock_instructions.markerb +7 -0
  94. data/lib/generators/templates/simple_form_for/confirmations/new.html.erb +15 -0
  95. data/lib/generators/templates/simple_form_for/passwords/edit.html.erb +19 -0
  96. data/lib/generators/templates/simple_form_for/passwords/new.html.erb +15 -0
  97. data/lib/generators/templates/simple_form_for/registrations/edit.html.erb +22 -0
  98. data/lib/generators/templates/simple_form_for/registrations/new.html.erb +17 -0
  99. data/lib/generators/templates/simple_form_for/sessions/new.html.erb +15 -0
  100. data/lib/generators/templates/simple_form_for/unlocks/new.html.erb +15 -0
  101. data/test/controllers/custom_strategy_test.rb +63 -0
  102. data/test/controllers/helpers_test.rb +254 -0
  103. data/test/controllers/internal_helpers_test.rb +111 -0
  104. data/test/controllers/sessions_controller_test.rb +58 -0
  105. data/test/controllers/url_helpers_test.rb +60 -0
  106. data/test/delegator_test.rb +20 -0
  107. data/test/devise_test.rb +73 -0
  108. data/test/failure_app_test.rb +222 -0
  109. data/test/generators/active_record_generator_test.rb +76 -0
  110. data/test/generators/devise_generator_test.rb +40 -0
  111. data/test/generators/install_generator_test.rb +14 -0
  112. data/test/generators/mongoid_generator_test.rb +24 -0
  113. data/test/generators/views_generator_test.rb +53 -0
  114. data/test/helpers/devise_helper_test.rb +52 -0
  115. data/test/indifferent_hash.rb +34 -0
  116. data/test/integration/authenticatable_test.rb +634 -0
  117. data/test/integration/confirmable_test.rb +299 -0
  118. data/test/integration/database_authenticatable_test.rb +83 -0
  119. data/test/integration/http_authenticatable_test.rb +98 -0
  120. data/test/integration/lockable_test.rb +243 -0
  121. data/test/integration/omniauthable_test.rb +134 -0
  122. data/test/integration/recoverable_test.rb +307 -0
  123. data/test/integration/registerable_test.rb +346 -0
  124. data/test/integration/rememberable_test.rb +159 -0
  125. data/test/integration/timeoutable_test.rb +141 -0
  126. data/test/integration/token_authenticatable_test.rb +162 -0
  127. data/test/integration/trackable_test.rb +93 -0
  128. data/test/mailers/confirmation_instructions_test.rb +103 -0
  129. data/test/mailers/reset_password_instructions_test.rb +84 -0
  130. data/test/mailers/unlock_instructions_test.rb +78 -0
  131. data/test/mapping_test.rb +128 -0
  132. data/test/models/authenticatable_test.rb +8 -0
  133. data/test/models/confirmable_test.rb +392 -0
  134. data/test/models/database_authenticatable_test.rb +190 -0
  135. data/test/models/lockable_test.rb +274 -0
  136. data/test/models/omniauthable_test.rb +8 -0
  137. data/test/models/recoverable_test.rb +206 -0
  138. data/test/models/registerable_test.rb +8 -0
  139. data/test/models/rememberable_test.rb +175 -0
  140. data/test/models/serializable_test.rb +49 -0
  141. data/test/models/timeoutable_test.rb +47 -0
  142. data/test/models/token_authenticatable_test.rb +56 -0
  143. data/test/models/trackable_test.rb +14 -0
  144. data/test/models/validatable_test.rb +117 -0
  145. data/test/models_test.rb +180 -0
  146. data/test/omniauth/config_test.rb +58 -0
  147. data/test/omniauth/url_helpers_test.rb +52 -0
  148. data/test/orm/active_record.rb +10 -0
  149. data/test/orm/mongoid.rb +15 -0
  150. data/test/rails_app/Rakefile +10 -0
  151. data/test/rails_app/app/active_record/admin.rb +7 -0
  152. data/test/rails_app/app/active_record/shim.rb +3 -0
  153. data/test/rails_app/app/active_record/user.rb +7 -0
  154. data/test/rails_app/app/controllers/admins/sessions_controller.rb +7 -0
  155. data/test/rails_app/app/controllers/admins_controller.rb +12 -0
  156. data/test/rails_app/app/controllers/application_controller.rb +9 -0
  157. data/test/rails_app/app/controllers/home_controller.rb +26 -0
  158. data/test/rails_app/app/controllers/publisher/registrations_controller.rb +3 -0
  159. data/test/rails_app/app/controllers/publisher/sessions_controller.rb +3 -0
  160. data/test/rails_app/app/controllers/users/omniauth_callbacks_controller.rb +15 -0
  161. data/test/rails_app/app/controllers/users_controller.rb +24 -0
  162. data/test/rails_app/app/helpers/application_helper.rb +4 -0
  163. data/test/rails_app/app/mailers/users/mailer.rb +9 -0
  164. data/test/rails_app/app/mongoid/admin.rb +28 -0
  165. data/test/rails_app/app/mongoid/shim.rb +25 -0
  166. data/test/rails_app/app/mongoid/user.rb +43 -0
  167. data/test/rails_app/app/views/admins/index.html.erb +1 -0
  168. data/test/rails_app/app/views/admins/sessions/new.html.erb +2 -0
  169. data/test/rails_app/app/views/home/admin_dashboard.html.erb +1 -0
  170. data/test/rails_app/app/views/home/index.html.erb +1 -0
  171. data/test/rails_app/app/views/home/join.html.erb +1 -0
  172. data/test/rails_app/app/views/home/private.html.erb +1 -0
  173. data/test/rails_app/app/views/home/user_dashboard.html.erb +1 -0
  174. data/test/rails_app/app/views/layouts/application.html.erb +24 -0
  175. data/test/rails_app/app/views/users/index.html.erb +1 -0
  176. data/test/rails_app/app/views/users/mailer/confirmation_instructions.erb +1 -0
  177. data/test/rails_app/app/views/users/sessions/new.html.erb +1 -0
  178. data/test/rails_app/config/application.rb +42 -0
  179. data/test/rails_app/config/boot.rb +9 -0
  180. data/test/rails_app/config/database.yml +18 -0
  181. data/test/rails_app/config/environment.rb +6 -0
  182. data/test/rails_app/config/environments/development.rb +19 -0
  183. data/test/rails_app/config/environments/production.rb +34 -0
  184. data/test/rails_app/config/environments/test.rb +34 -0
  185. data/test/rails_app/config/initializers/backtrace_silencers.rb +8 -0
  186. data/test/rails_app/config/initializers/devise.rb +179 -0
  187. data/test/rails_app/config/initializers/inflections.rb +3 -0
  188. data/test/rails_app/config/initializers/secret_token.rb +3 -0
  189. data/test/rails_app/config/routes.rb +101 -0
  190. data/test/rails_app/config.ru +4 -0
  191. data/test/rails_app/db/migrate/20100401102949_create_tables.rb +75 -0
  192. data/test/rails_app/db/schema.rb +53 -0
  193. data/test/rails_app/lib/shared_admin.rb +15 -0
  194. data/test/rails_app/lib/shared_user.rb +27 -0
  195. data/test/rails_app/public/404.html +26 -0
  196. data/test/rails_app/public/422.html +26 -0
  197. data/test/rails_app/public/500.html +26 -0
  198. data/test/rails_app/public/favicon.ico +0 -0
  199. data/test/rails_app/script/rails +10 -0
  200. data/test/routes_test.rb +249 -0
  201. data/test/support/assertions.rb +41 -0
  202. data/test/support/helpers.rb +92 -0
  203. data/test/support/integration.rb +93 -0
  204. data/test/support/locale/en.yml +4 -0
  205. data/test/support/webrat/integrations/rails.rb +25 -0
  206. data/test/test_helper.rb +28 -0
  207. data/test/test_helpers_test.rb +152 -0
  208. metadata +407 -0
@@ -0,0 +1,243 @@
1
+ # -*- encoding : utf-8 -*-
2
+ require 'test_helper'
3
+
4
+ class LockTest < ActionController::IntegrationTest
5
+
6
+ def visit_user_unlock_with_token(unlock_token)
7
+ visit user_unlock_path(:unlock_token => unlock_token)
8
+ end
9
+
10
+ def send_unlock_request
11
+ user = create_user(:locked => true)
12
+ ActionMailer::Base.deliveries.clear
13
+
14
+ visit new_user_session_path
15
+ click_link "Didn't receive unlock instructions?"
16
+
17
+ fill_in 'email', :with => user.email
18
+ click_button 'Resend unlock instructions'
19
+ end
20
+
21
+ test 'user should be able to request a new unlock token' do
22
+ send_unlock_request
23
+
24
+ assert_template 'sessions/new'
25
+ assert_contain 'You will receive an email with instructions about how to unlock your account in a few minutes'
26
+ assert_equal 1, ActionMailer::Base.deliveries.size
27
+ assert_equal ['please-change-me@config-initializers-devise.com'], ActionMailer::Base.deliveries.first.from
28
+ end
29
+
30
+ test 'user should receive the instructions from a custom mailer' do
31
+ User.any_instance.stubs(:devise_mailer).returns(Users::Mailer)
32
+
33
+ send_unlock_request
34
+
35
+ assert_equal ['custom@example.com'], ActionMailer::Base.deliveries.first.from
36
+ end
37
+
38
+ test 'unlocked user should not be able to request a unlock token' do
39
+ user = create_user(:locked => false)
40
+ ActionMailer::Base.deliveries.clear
41
+
42
+ visit new_user_session_path
43
+ click_link "Didn't receive unlock instructions?"
44
+
45
+ fill_in 'email', :with => user.email
46
+ click_button 'Resend unlock instructions'
47
+
48
+ assert_template 'unlocks/new'
49
+ assert_contain 'not locked'
50
+ assert_equal 0, ActionMailer::Base.deliveries.size
51
+ end
52
+
53
+ test 'unlocked pages should not be available if email strategy is disabled' do
54
+ visit "/admin_area/sign_in"
55
+
56
+ assert_raise Webrat::NotFoundError do
57
+ click_link "Didn't receive unlock instructions?"
58
+ end
59
+
60
+ assert_raise NameError do
61
+ visit new_admin_unlock_path
62
+ end
63
+
64
+ assert_raise ActionController::RoutingError do
65
+ visit "/admin_area/unlock/new"
66
+ end
67
+ end
68
+
69
+ test 'user with invalid unlock token should not be able to unlock an account' do
70
+ visit_user_unlock_with_token('invalid_token')
71
+
72
+ assert_response :success
73
+ assert_current_url '/users/unlock?unlock_token=invalid_token'
74
+ assert_have_selector '#error_explanation'
75
+ assert_contain /Unlock token(.*)invalid/
76
+ end
77
+
78
+ test "locked user should be able to unlock account" do
79
+ user = create_user(:locked => true)
80
+ assert user.access_locked?
81
+
82
+ visit_user_unlock_with_token(user.unlock_token)
83
+
84
+ assert_current_url "/users/sign_in"
85
+ assert_contain 'Your account has been unlocked successfully. Please sign in to continue.'
86
+
87
+ assert_not user.reload.access_locked?
88
+ end
89
+
90
+ test "redirect user to sign in page after unlocking its account" do
91
+ user = create_user(:locked => true)
92
+ visit_user_unlock_with_token(user.unlock_token)
93
+ assert_not warden.authenticated?(:user)
94
+ end
95
+
96
+ test "user should not send a new e-mail if already locked" do
97
+ user = create_user(:locked => true)
98
+ user.failed_attempts = User.maximum_attempts + 1
99
+ user.save!
100
+
101
+ ActionMailer::Base.deliveries.clear
102
+
103
+ sign_in_as_user(:password => "invalid")
104
+ assert_contain 'Your account is locked.'
105
+ assert ActionMailer::Base.deliveries.empty?
106
+ end
107
+
108
+ test 'error message is configurable by resource name' do
109
+ store_translations :en, :devise => {
110
+ :failure => {:user => {:locked => "You are locked!"}}
111
+ } do
112
+
113
+ user = create_user(:locked => true)
114
+ user.failed_attempts = User.maximum_attempts + 1
115
+ user.save!
116
+
117
+ sign_in_as_user(:password => "invalid")
118
+ assert_contain "You are locked!"
119
+ end
120
+ end
121
+
122
+ test "user should not be able to sign in when locked" do
123
+ store_translations :en, :devise => {
124
+ :failure => {:user => {:locked => "You are locked!"}}
125
+ } do
126
+
127
+ user = create_user(:locked => true)
128
+ user.failed_attempts = User.maximum_attempts + 1
129
+ user.save!
130
+
131
+ sign_in_as_user(:password => "123456")
132
+ assert_contain "You are locked!"
133
+ end
134
+ end
135
+
136
+ test 'user should be able to request a new unlock token via XML request' do
137
+ user = create_user(:locked => true)
138
+ ActionMailer::Base.deliveries.clear
139
+
140
+ post user_unlock_path(:format => 'xml'), :user => {:email => user.email}
141
+ assert_response :success
142
+ assert_equal response.body, {}.to_xml
143
+ assert_equal 1, ActionMailer::Base.deliveries.size
144
+ end
145
+
146
+ test 'unlocked user should not be able to request a unlock token via XML request' do
147
+ user = create_user(:locked => false)
148
+ ActionMailer::Base.deliveries.clear
149
+
150
+ post user_unlock_path(:format => 'xml'), :user => {:email => user.email}
151
+ assert_response :unprocessable_entity
152
+ assert response.body.include? %(<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<errors>)
153
+ assert_equal 0, ActionMailer::Base.deliveries.size
154
+ end
155
+
156
+ test 'user with valid unlock token should be able to unlock account via XML request' do
157
+ user = create_user(:locked => true)
158
+ assert user.access_locked?
159
+ get user_unlock_path(:format => 'xml', :unlock_token => user.unlock_token)
160
+ assert_response :success
161
+ assert response.body.include? %(<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<user>)
162
+ end
163
+
164
+
165
+ test 'user with invalid unlock token should not be able to unlock the account via XML request' do
166
+ get user_unlock_path(:format => 'xml', :unlock_token => 'invalid_token')
167
+ assert_response :unprocessable_entity
168
+ assert response.body.include? %(<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<errors>)
169
+ end
170
+
171
+ test "when using json to ask a unlock request, should not return the user" do
172
+ user = create_user(:locked => true)
173
+ post user_unlock_path(:format => "json", :user => {:email => user.email})
174
+ assert_response :success
175
+ assert_equal response.body, {}.to_json
176
+ end
177
+
178
+ test "in paranoid mode, when trying to unlock an user that exists it should not say that it exists if it is locked" do
179
+ swap Devise, :paranoid => true do
180
+ user = create_user(:locked => true)
181
+
182
+ visit new_user_session_path
183
+ click_link "Didn't receive unlock instructions?"
184
+
185
+ fill_in 'email', :with => user.email
186
+ click_button 'Resend unlock instructions'
187
+
188
+ assert_current_url "/users/sign_in"
189
+ assert_contain "If your account exists, you will receive an email with instructions about how to unlock it in a few minutes."
190
+ end
191
+ end
192
+
193
+ 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
194
+ swap Devise, :paranoid => true do
195
+ user = create_user(:locked => false)
196
+
197
+ visit new_user_session_path
198
+ click_link "Didn't receive unlock instructions?"
199
+
200
+ fill_in 'email', :with => user.email
201
+ click_button 'Resend unlock instructions'
202
+
203
+ assert_current_url "/users/sign_in"
204
+ assert_contain "If your account exists, you will receive an email with instructions about how to unlock it in a few minutes."
205
+ end
206
+ end
207
+
208
+ test "in paranoid mode, when trying to unlock an user that does not exists it should not say that it does not exists" do
209
+ swap Devise, :paranoid => true do
210
+ visit new_user_session_path
211
+ click_link "Didn't receive unlock instructions?"
212
+
213
+ fill_in 'email', :with => "arandomemail@hotmail.com"
214
+ click_button 'Resend unlock instructions'
215
+
216
+ assert_not_contain "1 error prohibited this user from being saved:"
217
+ assert_not_contain "Email not found"
218
+ assert_current_url "/users/sign_in"
219
+
220
+ assert_contain "If your account exists, you will receive an email with instructions about how to unlock it in a few minutes."
221
+
222
+ end
223
+ end
224
+
225
+ test "in paranoid mode, when locking a user that exists it should not say that the user was locked" do
226
+ swap Devise, :paranoid => true, :maximum_attempts => 1 do
227
+ user = create_user(:locked => false)
228
+
229
+ visit new_user_session_path
230
+ fill_in 'email', :with => user.email
231
+ fill_in 'password', :with => "abadpassword"
232
+ click_button 'Sign in'
233
+
234
+ fill_in 'email', :with => user.email
235
+ fill_in 'password', :with => "abadpassword"
236
+ click_button 'Sign in'
237
+
238
+ assert_current_url "/users/sign_in"
239
+ assert_not_contain "locked"
240
+ end
241
+ end
242
+
243
+ end
@@ -0,0 +1,134 @@
1
+ # -*- encoding : utf-8 -*-
2
+ require 'test_helper'
3
+
4
+
5
+ class OmniauthableIntegrationTest < ActionController::IntegrationTest
6
+ FACEBOOK_INFO = {
7
+ "id" => '12345',
8
+ "link" => 'http://facebook.com/josevalim',
9
+ "email" => 'user@example.com',
10
+ "first_name" => 'Jose',
11
+ "last_name" => 'Valim',
12
+ "website" => 'http://blog.plataformatec.com.br'
13
+ }
14
+
15
+ setup do
16
+ OmniAuth.config.test_mode = true
17
+ OmniAuth.config.mock_auth[:facebook] = {
18
+ "uid" => '12345',
19
+ "provider" => 'facebook',
20
+ "user_info" => {"nickname" => 'josevalim'},
21
+ "credentials" => {"token" => 'plataformatec'},
22
+ "extra" => {"user_hash" => FACEBOOK_INFO}
23
+ }
24
+ end
25
+
26
+ teardown do
27
+ OmniAuth.config.test_mode = false
28
+ end
29
+
30
+ def stub_action!(name)
31
+ Users::OmniauthCallbacksController.class_eval do
32
+ alias_method :__old_facebook, :facebook
33
+ alias_method :facebook, name
34
+ end
35
+ yield
36
+ ensure
37
+ Users::OmniauthCallbacksController.class_eval do
38
+ alias_method :facebook, :__old_facebook
39
+ end
40
+ end
41
+
42
+ test "can access omniauth.auth in the env hash" do
43
+ visit "/users/sign_in"
44
+ click_link "Sign in with Facebook"
45
+
46
+ json = ActiveSupport::JSON.decode(response.body)
47
+
48
+ assert_equal "12345", json["uid"]
49
+ assert_equal "facebook", json["provider"]
50
+ assert_equal "josevalim", json["user_info"]["nickname"]
51
+ assert_equal FACEBOOK_INFO, json["extra"]["user_hash"]
52
+ assert_equal "plataformatec", json["credentials"]["token"]
53
+ end
54
+
55
+ test "cleans up session on sign up" do
56
+ assert_no_difference "User.count" do
57
+ visit "/users/sign_in"
58
+ click_link "Sign in with Facebook"
59
+ end
60
+
61
+ assert session["devise.facebook_data"]
62
+
63
+ assert_difference "User.count" do
64
+ visit "/users/sign_up"
65
+ fill_in "Password", :with => "12345678"
66
+ fill_in "Password confirmation", :with => "12345678"
67
+ click_button "Sign up"
68
+ end
69
+
70
+ assert_current_url "/"
71
+ assert_contain "You have signed up successfully."
72
+ assert_contain "Hello User user@example.com"
73
+ assert_not session["devise.facebook_data"]
74
+ end
75
+
76
+ test "cleans up session on cancel" do
77
+ assert_no_difference "User.count" do
78
+ visit "/users/sign_in"
79
+ click_link "Sign in with Facebook"
80
+ end
81
+
82
+ assert session["devise.facebook_data"]
83
+ visit "/users/cancel"
84
+ assert !session["devise.facebook_data"]
85
+ end
86
+
87
+ test "cleans up session on sign in" do
88
+ assert_no_difference "User.count" do
89
+ visit "/users/sign_in"
90
+ click_link "Sign in with Facebook"
91
+ end
92
+
93
+ assert session["devise.facebook_data"]
94
+ user = sign_in_as_user
95
+ assert !session["devise.facebook_data"]
96
+ end
97
+
98
+ test "sign in and send remember token if configured" do
99
+ visit "/users/sign_in"
100
+ click_link "Sign in with Facebook"
101
+ assert_nil warden.cookies["remember_user_token"]
102
+
103
+ stub_action!(:sign_in_facebook) do
104
+ create_user
105
+ visit "/users/sign_in"
106
+ click_link "Sign in with Facebook"
107
+ assert warden.authenticated?(:user)
108
+ assert warden.cookies["remember_user_token"]
109
+ end
110
+ end
111
+
112
+ test "generates a proper link when SCRIPT_NAME is set" do
113
+ header 'SCRIPT_NAME', '/q'
114
+ visit "/users/sign_in"
115
+ assert_select "a", :href => "/q/users/auth/facebook"
116
+ end
117
+
118
+ test "handles callback error parameter according to the specification" do
119
+ OmniAuth.config.mock_auth[:facebook] = :access_denied
120
+ visit "/users/auth/facebook/callback?error=access_denied"
121
+ assert_current_url "/users/sign_in"
122
+ assert_contain 'Could not authenticate you from Facebook because "Access denied".'
123
+ end
124
+
125
+ test "handles other exceptions from omniauth" do
126
+ OmniAuth.config.mock_auth[:facebook] = :invalid_credentials
127
+
128
+ visit "/users/sign_in"
129
+ click_link "Sign in with Facebook"
130
+
131
+ assert_current_url "/users/sign_in"
132
+ assert_contain 'Could not authenticate you from Facebook because "Invalid credentials".'
133
+ end
134
+ end
@@ -0,0 +1,307 @@
1
+ # -*- encoding : utf-8 -*-
2
+ require 'test_helper'
3
+
4
+ class PasswordTest < ActionController::IntegrationTest
5
+
6
+ def visit_new_password_path
7
+ visit new_user_session_path
8
+ click_link 'Forgot your password?'
9
+ end
10
+
11
+ def request_forgot_password(&block)
12
+ visit_new_password_path
13
+ assert_response :success
14
+ assert_not warden.authenticated?(:user)
15
+
16
+ fill_in 'email', :with => 'user@test.com'
17
+ yield if block_given?
18
+ click_button 'Send me reset password instructions'
19
+ end
20
+
21
+ def reset_password(options={}, &block)
22
+ visit edit_user_password_path(:reset_password_token => options[:reset_password_token]) unless options[:visit] == false
23
+ assert_response :success
24
+
25
+ fill_in 'New password', :with => '987654321'
26
+ fill_in 'Confirm new password', :with => '987654321'
27
+ yield if block_given?
28
+ click_button 'Change my password'
29
+ end
30
+
31
+ test 'reset password with email of different case should succeed when email is in the list of case insensitive keys' do
32
+ create_user(:email => 'Foo@Bar.com')
33
+
34
+ request_forgot_password do
35
+ fill_in 'email', :with => 'foo@bar.com'
36
+ end
37
+
38
+ assert_current_url '/users/sign_in'
39
+ assert_contain 'You will receive an email with instructions about how to reset your password in a few minutes.'
40
+ end
41
+
42
+ test 'reset password with email should send an email from a custom mailer' do
43
+ create_user(:email => 'Foo@Bar.com')
44
+
45
+ User.any_instance.stubs(:devise_mailer).returns(Users::Mailer)
46
+ request_forgot_password do
47
+ fill_in 'email', :with => 'foo@bar.com'
48
+ end
49
+ assert_equal ['custom@example.com'], ActionMailer::Base.deliveries.last.from
50
+ end
51
+
52
+ test 'reset password with email of different case should fail when email is NOT the list of case insensitive keys' do
53
+ swap Devise, :case_insensitive_keys => [] do
54
+ create_user(:email => 'Foo@Bar.com')
55
+
56
+ request_forgot_password do
57
+ fill_in 'email', :with => 'foo@bar.com'
58
+ end
59
+
60
+ assert_response :success
61
+ assert_current_url '/users/password'
62
+ assert_have_selector "input[type=email][value='foo@bar.com']"
63
+ assert_contain 'not found'
64
+ end
65
+ end
66
+
67
+ test 'reset password with email with extra whitespace should succeed when email is in the list of strip whitespace keys' do
68
+ create_user(:email => 'foo@bar.com')
69
+
70
+ request_forgot_password do
71
+ fill_in 'email', :with => ' foo@bar.com '
72
+ end
73
+
74
+ assert_current_url '/users/sign_in'
75
+ assert_contain 'You will receive an email with instructions about how to reset your password in a few minutes.'
76
+ end
77
+
78
+ test 'reset password with email with extra whitespace should fail when email is NOT the list of strip whitespace keys' do
79
+ swap Devise, :strip_whitespace_keys => [] do
80
+ create_user(:email => 'foo@bar.com')
81
+
82
+ request_forgot_password do
83
+ fill_in 'email', :with => ' foo@bar.com '
84
+ end
85
+
86
+ assert_response :success
87
+ assert_current_url '/users/password'
88
+ assert_have_selector "input[type=email][value=' foo@bar.com ']"
89
+ assert_contain 'not found'
90
+ end
91
+ end
92
+
93
+ test 'authenticated user should not be able to visit forgot password page' do
94
+ sign_in_as_user
95
+ assert warden.authenticated?(:user)
96
+
97
+ get new_user_password_path
98
+
99
+ assert_response :redirect
100
+ assert_redirected_to root_path
101
+ end
102
+
103
+ test 'not authenticated user should be able to request a forgot password' do
104
+ create_user
105
+ request_forgot_password
106
+
107
+ assert_current_url '/users/sign_in'
108
+ assert_contain 'You will receive an email with instructions about how to reset your password in a few minutes.'
109
+ end
110
+
111
+ test 'not authenticated user with invalid email should receive an error message' do
112
+ request_forgot_password do
113
+ fill_in 'email', :with => 'invalid.test@test.com'
114
+ end
115
+
116
+ assert_response :success
117
+ assert_current_url '/users/password'
118
+ assert_have_selector "input[type=email][value='invalid.test@test.com']"
119
+ assert_contain 'not found'
120
+ end
121
+
122
+ test 'authenticated user should not be able to visit edit password page' do
123
+ sign_in_as_user
124
+ get edit_user_password_path
125
+ assert_response :redirect
126
+ assert_redirected_to root_path
127
+ assert warden.authenticated?(:user)
128
+ end
129
+
130
+ test 'not authenticated user without a reset password token should not be able to visit the page' do
131
+ get edit_user_password_path
132
+ assert_response :redirect
133
+ assert_redirected_to "/users/sign_in"
134
+ end
135
+
136
+ test 'not authenticated user with invalid reset password token should not be able to change his password' do
137
+ user = create_user
138
+ reset_password :reset_password_token => 'invalid_reset_password'
139
+
140
+ assert_response :success
141
+ assert_current_url '/users/password'
142
+ assert_have_selector '#error_explanation'
143
+ assert_contain /Reset password token(.*)invalid/
144
+ assert_not user.reload.valid_password?('987654321')
145
+ end
146
+
147
+ test 'not authenticated user with valid reset password token but invalid password should not be able to change his password' do
148
+ user = create_user
149
+ request_forgot_password
150
+ reset_password :reset_password_token => user.reload.reset_password_token do
151
+ fill_in 'Confirm new password', :with => 'other_password'
152
+ end
153
+
154
+ assert_response :success
155
+ assert_current_url '/users/password'
156
+ assert_have_selector '#error_explanation'
157
+ assert_contain 'Password doesn\'t match confirmation'
158
+ assert_not user.reload.valid_password?('987654321')
159
+ end
160
+
161
+ test 'not authenticated user with valid data should be able to change his password' do
162
+ user = create_user
163
+ request_forgot_password
164
+ reset_password :reset_password_token => user.reload.reset_password_token
165
+
166
+ assert_current_url '/'
167
+ assert_contain 'Your password was changed successfully. You are now signed in.'
168
+ assert user.reload.valid_password?('987654321')
169
+ end
170
+
171
+ test 'after entering invalid data user should still be able to change his password' do
172
+ user = create_user
173
+ request_forgot_password
174
+ reset_password :reset_password_token => user.reload.reset_password_token do
175
+ fill_in 'Confirm new password', :with => 'other_password'
176
+ end
177
+ assert_response :success
178
+ assert_have_selector '#error_explanation'
179
+ assert_not user.reload.valid_password?('987654321')
180
+
181
+ reset_password :reset_password_token => user.reload.reset_password_token, :visit => false
182
+ assert_contain 'Your password was changed successfully.'
183
+ assert user.reload.valid_password?('987654321')
184
+ end
185
+
186
+ test 'sign in user automatically after changing its password' do
187
+ user = create_user
188
+ request_forgot_password
189
+ reset_password :reset_password_token => user.reload.reset_password_token
190
+
191
+ assert warden.authenticated?(:user)
192
+ end
193
+
194
+ test 'does not sign in user automatically after changing its password if it\'s locked' do
195
+ user = create_user(:locked => true)
196
+ request_forgot_password
197
+ reset_password :reset_password_token => user.reload.reset_password_token
198
+
199
+ assert_contain 'Your password was changed successfully.'
200
+ assert_not_contain 'You are now signed in.'
201
+ assert_equal new_user_session_path, @request.path
202
+ assert !warden.authenticated?(:user)
203
+ end
204
+
205
+ test 'sign in user automatically and confirm after changing its password if it\'s not confirmed' do
206
+ user = create_user(:confirm => false)
207
+ request_forgot_password
208
+ reset_password :reset_password_token => user.reload.reset_password_token
209
+
210
+ assert warden.authenticated?(:user)
211
+ assert user.reload.confirmed?
212
+ end
213
+
214
+ test 'reset password request with valid E-Mail in XML format should return valid response' do
215
+ create_user
216
+ post user_password_path(:format => 'xml'), :user => {:email => "user@test.com"}
217
+ assert_response :success
218
+ assert_equal response.body, { }.to_xml
219
+ end
220
+
221
+ test 'reset password request with invalid E-Mail in XML format should return valid response' do
222
+ create_user
223
+ post user_password_path(:format => 'xml'), :user => {:email => "invalid.test@test.com"}
224
+ assert_response :unprocessable_entity
225
+ assert response.body.include? %(<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<errors>)
226
+ end
227
+
228
+ test 'reset password request with invalid E-Mail in XML format should return empty and valid response' do
229
+ swap Devise, :paranoid => true do
230
+ create_user
231
+ post user_password_path(:format => 'xml'), :user => {:email => "invalid@test.com"}
232
+ assert_response :success
233
+ assert_equal response.body, { }.to_xml
234
+ end
235
+ end
236
+
237
+ test 'change password with valid parameters in XML format should return valid response' do
238
+ user = create_user
239
+ request_forgot_password
240
+ put user_password_path(:format => 'xml'), :user => {:reset_password_token => user.reload.reset_password_token, :password => '987654321', :password_confirmation => '987654321'}
241
+ assert_response :success
242
+ assert warden.authenticated?(:user)
243
+ end
244
+
245
+ test 'change password with invalid token in XML format should return invalid response' do
246
+ user = create_user
247
+ request_forgot_password
248
+ put user_password_path(:format => 'xml'), :user => {:reset_password_token => 'invalid.token', :password => '987654321', :password_confirmation => '987654321'}
249
+ assert_response :unprocessable_entity
250
+ assert response.body.include? %(<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<errors>)
251
+ end
252
+
253
+ test 'change password with invalid new password in XML format should return invalid response' do
254
+ user = create_user
255
+ request_forgot_password
256
+ put user_password_path(:format => 'xml'), :user => {:reset_password_token => user.reload.reset_password_token, :password => '', :password_confirmation => '987654321'}
257
+ assert_response :unprocessable_entity
258
+ assert response.body.include? %(<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<errors>)
259
+ end
260
+
261
+ test "when using json requests to ask a confirmable request, should not return the object" do
262
+ user = create_user(:confirm => false)
263
+
264
+ post user_password_path(:format => :json), :user => { :email => user.email }
265
+
266
+ assert_response :success
267
+ assert_equal response.body, "{}"
268
+ end
269
+
270
+ test "when in paranoid mode and with an invalid e-mail, asking to reset a password should display a message that does not indicates that the e-mail does not exists in the database" do
271
+ swap Devise, :paranoid => true do
272
+ visit_new_password_path
273
+ fill_in "email", :with => "arandomemail@test.com"
274
+ click_button 'Send me reset password instructions'
275
+
276
+ assert_not_contain "1 error prohibited this user from being saved:"
277
+ assert_not_contain "Email not found"
278
+ assert_contain "If your email address exists in our database, you will receive a password recovery link at your email address in a few minutes."
279
+ assert_current_url "/users/sign_in"
280
+ end
281
+ end
282
+
283
+ test "when in paranoid mode and with a valid e-mail, asking to reset password should display a message that does not indicates that the email exists in the database and redirect to the failure route" do
284
+ swap Devise, :paranoid => true do
285
+ user = create_user
286
+ visit_new_password_path
287
+ fill_in 'email', :with => user.email
288
+ click_button 'Send me reset password instructions'
289
+
290
+ assert_contain "If your email address exists in our database, you will receive a password recovery link at your email address in a few minutes."
291
+ assert_current_url "/users/sign_in"
292
+ end
293
+ end
294
+
295
+ test "after recovering a password, should set failed attempts to 0" do
296
+ user = create_user
297
+ user.update_attribute(:failed_attempts, 10)
298
+
299
+ assert_equal 10, user.failed_attempts
300
+ request_forgot_password
301
+ reset_password :reset_password_token => user.reload.reset_password_token
302
+
303
+ assert warden.authenticated?(:user)
304
+ user.reload
305
+ assert_equal 0, user.failed_attempts
306
+ end
307
+ end