af-devise 2.1.2

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