devise_token_auth_skycocker_fork 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (167) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +13 -0
  3. data/README.md +97 -0
  4. data/Rakefile +42 -0
  5. data/app/controllers/devise_token_auth/application_controller.rb +79 -0
  6. data/app/controllers/devise_token_auth/concerns/resource_finder.rb +44 -0
  7. data/app/controllers/devise_token_auth/concerns/set_user_by_token.rb +178 -0
  8. data/app/controllers/devise_token_auth/confirmations_controller.rb +39 -0
  9. data/app/controllers/devise_token_auth/omniauth_callbacks_controller.rb +239 -0
  10. data/app/controllers/devise_token_auth/passwords_controller.rb +185 -0
  11. data/app/controllers/devise_token_auth/registrations_controller.rb +198 -0
  12. data/app/controllers/devise_token_auth/sessions_controller.rb +131 -0
  13. data/app/controllers/devise_token_auth/token_validations_controller.rb +31 -0
  14. data/app/controllers/devise_token_auth/unlocks_controller.rb +89 -0
  15. data/app/models/devise_token_auth/concerns/active_record_support.rb +34 -0
  16. data/app/models/devise_token_auth/concerns/mongoid_support.rb +19 -0
  17. data/app/models/devise_token_auth/concerns/user.rb +262 -0
  18. data/app/models/devise_token_auth/concerns/user_omniauth_callbacks.rb +28 -0
  19. data/app/validators/devise_token_auth/email_validator.rb +23 -0
  20. data/app/views/devise/mailer/confirmation_instructions.html.erb +5 -0
  21. data/app/views/devise/mailer/reset_password_instructions.html.erb +8 -0
  22. data/app/views/devise/mailer/unlock_instructions.html.erb +7 -0
  23. data/app/views/devise_token_auth/omniauth_external_window.html.erb +38 -0
  24. data/config/locales/da-DK.yml +50 -0
  25. data/config/locales/de.yml +49 -0
  26. data/config/locales/en.yml +50 -0
  27. data/config/locales/es.yml +49 -0
  28. data/config/locales/fr.yml +49 -0
  29. data/config/locales/it.yml +46 -0
  30. data/config/locales/ja.yml +46 -0
  31. data/config/locales/nl.yml +30 -0
  32. data/config/locales/pl.yml +48 -0
  33. data/config/locales/pt-BR.yml +46 -0
  34. data/config/locales/pt.yml +48 -0
  35. data/config/locales/ro.yml +46 -0
  36. data/config/locales/ru.yml +50 -0
  37. data/config/locales/sq.yml +46 -0
  38. data/config/locales/sv.yml +50 -0
  39. data/config/locales/uk.yml +59 -0
  40. data/config/locales/vi.yml +50 -0
  41. data/config/locales/zh-CN.yml +46 -0
  42. data/config/locales/zh-HK.yml +48 -0
  43. data/config/locales/zh-TW.yml +48 -0
  44. data/lib/devise_token_auth.rb +13 -0
  45. data/lib/devise_token_auth/blacklist.rb +2 -0
  46. data/lib/devise_token_auth/controllers/helpers.rb +161 -0
  47. data/lib/devise_token_auth/controllers/url_helpers.rb +10 -0
  48. data/lib/devise_token_auth/engine.rb +90 -0
  49. data/lib/devise_token_auth/errors.rb +8 -0
  50. data/lib/devise_token_auth/rails/routes.rb +116 -0
  51. data/lib/devise_token_auth/url.rb +41 -0
  52. data/lib/devise_token_auth/version.rb +5 -0
  53. data/lib/generators/devise_token_auth/USAGE +31 -0
  54. data/lib/generators/devise_token_auth/install_generator.rb +91 -0
  55. data/lib/generators/devise_token_auth/install_generator_helpers.rb +98 -0
  56. data/lib/generators/devise_token_auth/install_mongoid_generator.rb +46 -0
  57. data/lib/generators/devise_token_auth/install_views_generator.rb +18 -0
  58. data/lib/generators/devise_token_auth/templates/devise_token_auth.rb +50 -0
  59. data/lib/generators/devise_token_auth/templates/devise_token_auth_create_users.rb.erb +56 -0
  60. data/lib/generators/devise_token_auth/templates/user.rb.erb +9 -0
  61. data/lib/generators/devise_token_auth/templates/user_mongoid.rb.erb +63 -0
  62. data/lib/tasks/devise_token_auth_tasks.rake +6 -0
  63. data/test/controllers/custom/custom_confirmations_controller_test.rb +25 -0
  64. data/test/controllers/custom/custom_omniauth_callbacks_controller_test.rb +33 -0
  65. data/test/controllers/custom/custom_passwords_controller_test.rb +79 -0
  66. data/test/controllers/custom/custom_registrations_controller_test.rb +63 -0
  67. data/test/controllers/custom/custom_sessions_controller_test.rb +39 -0
  68. data/test/controllers/custom/custom_token_validations_controller_test.rb +42 -0
  69. data/test/controllers/demo_group_controller_test.rb +151 -0
  70. data/test/controllers/demo_mang_controller_test.rb +284 -0
  71. data/test/controllers/demo_user_controller_test.rb +629 -0
  72. data/test/controllers/devise_token_auth/confirmations_controller_test.rb +127 -0
  73. data/test/controllers/devise_token_auth/omniauth_callbacks_controller_test.rb +376 -0
  74. data/test/controllers/devise_token_auth/passwords_controller_test.rb +639 -0
  75. data/test/controllers/devise_token_auth/registrations_controller_test.rb +880 -0
  76. data/test/controllers/devise_token_auth/sessions_controller_test.rb +541 -0
  77. data/test/controllers/devise_token_auth/token_validations_controller_test.rb +102 -0
  78. data/test/controllers/devise_token_auth/unlocks_controller_test.rb +196 -0
  79. data/test/controllers/overrides/confirmations_controller_test.rb +47 -0
  80. data/test/controllers/overrides/omniauth_callbacks_controller_test.rb +53 -0
  81. data/test/controllers/overrides/passwords_controller_test.rb +64 -0
  82. data/test/controllers/overrides/registrations_controller_test.rb +46 -0
  83. data/test/controllers/overrides/sessions_controller_test.rb +35 -0
  84. data/test/controllers/overrides/token_validations_controller_test.rb +43 -0
  85. data/test/dummy/README.rdoc +28 -0
  86. data/test/dummy/app/active_record/lockable_user.rb +7 -0
  87. data/test/dummy/app/active_record/mang.rb +5 -0
  88. data/test/dummy/app/active_record/only_email_user.rb +7 -0
  89. data/test/dummy/app/active_record/scoped_user.rb +9 -0
  90. data/test/dummy/app/active_record/unconfirmable_user.rb +9 -0
  91. data/test/dummy/app/active_record/unregisterable_user.rb +9 -0
  92. data/test/dummy/app/active_record/user.rb +6 -0
  93. data/test/dummy/app/controllers/application_controller.rb +18 -0
  94. data/test/dummy/app/controllers/auth_origin_controller.rb +7 -0
  95. data/test/dummy/app/controllers/custom/confirmations_controller.rb +13 -0
  96. data/test/dummy/app/controllers/custom/omniauth_callbacks_controller.rb +13 -0
  97. data/test/dummy/app/controllers/custom/passwords_controller.rb +39 -0
  98. data/test/dummy/app/controllers/custom/registrations_controller.rb +39 -0
  99. data/test/dummy/app/controllers/custom/sessions_controller.rb +29 -0
  100. data/test/dummy/app/controllers/custom/token_validations_controller.rb +19 -0
  101. data/test/dummy/app/controllers/demo_group_controller.rb +15 -0
  102. data/test/dummy/app/controllers/demo_mang_controller.rb +14 -0
  103. data/test/dummy/app/controllers/demo_user_controller.rb +27 -0
  104. data/test/dummy/app/controllers/overrides/confirmations_controller.rb +28 -0
  105. data/test/dummy/app/controllers/overrides/omniauth_callbacks_controller.rb +16 -0
  106. data/test/dummy/app/controllers/overrides/passwords_controller.rb +35 -0
  107. data/test/dummy/app/controllers/overrides/registrations_controller.rb +29 -0
  108. data/test/dummy/app/controllers/overrides/sessions_controller.rb +36 -0
  109. data/test/dummy/app/controllers/overrides/token_validations_controller.rb +23 -0
  110. data/test/dummy/app/helpers/application_helper.rb +1058 -0
  111. data/test/dummy/app/models/concerns/favorite_color.rb +19 -0
  112. data/test/dummy/app/mongoid/lockable_user.rb +38 -0
  113. data/test/dummy/app/mongoid/mang.rb +53 -0
  114. data/test/dummy/app/mongoid/only_email_user.rb +33 -0
  115. data/test/dummy/app/mongoid/scoped_user.rb +57 -0
  116. data/test/dummy/app/mongoid/unconfirmable_user.rb +51 -0
  117. data/test/dummy/app/mongoid/unregisterable_user.rb +54 -0
  118. data/test/dummy/app/mongoid/user.rb +56 -0
  119. data/test/dummy/app/views/layouts/application.html.erb +14 -0
  120. data/test/dummy/config.ru +18 -0
  121. data/test/dummy/config/application.rb +48 -0
  122. data/test/dummy/config/application.yml.bk +0 -0
  123. data/test/dummy/config/boot.rb +11 -0
  124. data/test/dummy/config/environment.rb +7 -0
  125. data/test/dummy/config/environments/development.rb +46 -0
  126. data/test/dummy/config/environments/production.rb +84 -0
  127. data/test/dummy/config/environments/test.rb +50 -0
  128. data/test/dummy/config/initializers/assets.rb +10 -0
  129. data/test/dummy/config/initializers/backtrace_silencers.rb +9 -0
  130. data/test/dummy/config/initializers/cookies_serializer.rb +5 -0
  131. data/test/dummy/config/initializers/devise.rb +17 -0
  132. data/test/dummy/config/initializers/devise_token_auth.rb +24 -0
  133. data/test/dummy/config/initializers/figaro.rb +3 -0
  134. data/test/dummy/config/initializers/filter_parameter_logging.rb +6 -0
  135. data/test/dummy/config/initializers/inflections.rb +18 -0
  136. data/test/dummy/config/initializers/mime_types.rb +6 -0
  137. data/test/dummy/config/initializers/omniauth.rb +10 -0
  138. data/test/dummy/config/initializers/session_store.rb +5 -0
  139. data/test/dummy/config/initializers/wrap_parameters.rb +16 -0
  140. data/test/dummy/config/routes.rb +55 -0
  141. data/test/dummy/config/spring.rb +3 -0
  142. data/test/dummy/db/migrate/20140715061447_devise_token_auth_create_users.rb +65 -0
  143. data/test/dummy/db/migrate/20140715061805_devise_token_auth_create_mangs.rb +64 -0
  144. data/test/dummy/db/migrate/20140829044006_add_operating_thetan_to_user.rb +8 -0
  145. data/test/dummy/db/migrate/20140916224624_add_favorite_color_to_mangs.rb +7 -0
  146. data/test/dummy/db/migrate/20141222035835_devise_token_auth_create_only_email_users.rb +62 -0
  147. data/test/dummy/db/migrate/20141222053502_devise_token_auth_create_unregisterable_users.rb +63 -0
  148. data/test/dummy/db/migrate/20150708104536_devise_token_auth_create_unconfirmable_users.rb +63 -0
  149. data/test/dummy/db/migrate/20160103235141_devise_token_auth_create_scoped_users.rb +63 -0
  150. data/test/dummy/db/migrate/20160629184441_devise_token_auth_create_lockable_users.rb +63 -0
  151. data/test/dummy/db/schema.rb +200 -0
  152. data/test/dummy/lib/migration_database_helper.rb +43 -0
  153. data/test/dummy/tmp/generators/app/models/user.rb +9 -0
  154. data/test/dummy/tmp/generators/config/initializers/devise_token_auth.rb +50 -0
  155. data/test/dummy/tmp/generators/db/migrate/20181030122248_devise_token_auth_create_users.rb +56 -0
  156. data/test/factories/users.rb +40 -0
  157. data/test/lib/devise_token_auth/blacklist_test.rb +11 -0
  158. data/test/lib/devise_token_auth/url_test.rb +26 -0
  159. data/test/lib/generators/devise_token_auth/install_generator_test.rb +217 -0
  160. data/test/lib/generators/devise_token_auth/install_generator_with_namespace_test.rb +222 -0
  161. data/test/lib/generators/devise_token_auth/install_views_generator_test.rb +25 -0
  162. data/test/models/concerns/mongoid_support_test.rb +31 -0
  163. data/test/models/only_email_user_test.rb +37 -0
  164. data/test/models/user_test.rb +140 -0
  165. data/test/support/controllers/routes.rb +43 -0
  166. data/test/test_helper.rb +103 -0
  167. metadata +443 -0
@@ -0,0 +1,639 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'test_helper'
4
+
5
+ # was the web request successful?
6
+ # was the user redirected to the right page?
7
+ # was the user successfully authenticated?
8
+ # was the correct object stored in the response?
9
+ # was the appropriate message delivered in the json payload?
10
+
11
+ class DeviseTokenAuth::PasswordsControllerTest < ActionController::TestCase
12
+ describe DeviseTokenAuth::PasswordsController do
13
+ describe 'Password reset' do
14
+ before do
15
+ @resource = create(:user, :confirmed)
16
+ @redirect_url = 'http://ng-token-auth.dev'
17
+ end
18
+
19
+ describe 'not email should return 401' do
20
+ before do
21
+ @auth_headers = @resource.create_new_auth_token
22
+ @new_password = Faker::Internet.password
23
+
24
+ post :create,
25
+ params: { redirect_url: @redirect_url }
26
+ @data = JSON.parse(response.body)
27
+ end
28
+
29
+ test 'response should fail' do
30
+ assert_equal 401, response.status
31
+ end
32
+
33
+ test 'error message should be returned' do
34
+ assert @data['errors']
35
+ assert_equal @data['errors'],
36
+ [I18n.t('devise_token_auth.passwords.missing_email')]
37
+ end
38
+ end
39
+
40
+ describe 'not redirect_url should return 401' do
41
+ before do
42
+ @auth_headers = @resource.create_new_auth_token
43
+ @new_password = Faker::Internet.password
44
+
45
+ post :create,
46
+ params: { email: 'chester@cheet.ah' }
47
+ @data = JSON.parse(response.body)
48
+ end
49
+
50
+ test 'response should fail' do
51
+ assert_equal 401, response.status
52
+ end
53
+
54
+ test 'error message should be returned' do
55
+ assert @data['errors']
56
+ assert_equal(
57
+ @data['errors'],
58
+ [I18n.t('devise_token_auth.passwords.missing_redirect_url')]
59
+ )
60
+ end
61
+ end
62
+
63
+ describe 'request password reset' do
64
+ describe 'unknown user should return 404' do
65
+ before do
66
+ post :create,
67
+ params: { email: 'chester@cheet.ah',
68
+ redirect_url: @redirect_url }
69
+ @data = JSON.parse(response.body)
70
+ end
71
+
72
+ test 'unknown user should return 404' do
73
+ assert_equal 404, response.status
74
+ end
75
+
76
+ test 'errors should be returned' do
77
+ assert @data['errors']
78
+ assert_equal @data['errors'],
79
+ [I18n.t('devise_token_auth.passwords.user_not_found',
80
+ email: 'chester@cheet.ah')]
81
+ end
82
+ end
83
+
84
+ describe 'successfully requested password reset' do
85
+ before do
86
+ post :create,
87
+ params: { email: @resource.email,
88
+ redirect_url: @redirect_url }
89
+
90
+ @data = JSON.parse(response.body)
91
+ end
92
+
93
+ test 'response should not contain extra data' do
94
+ assert_nil @data['data']
95
+ end
96
+ end
97
+
98
+ describe 'case-sensitive email' do
99
+ before do
100
+ post :create,
101
+ params: { email: @resource.email,
102
+ redirect_url: @redirect_url }
103
+
104
+ @mail = ActionMailer::Base.deliveries.last
105
+ @resource.reload
106
+ @data = JSON.parse(response.body)
107
+
108
+ @mail_config_name = CGI.unescape(@mail.body.match(/config=([^&]*)&/)[1])
109
+ @mail_redirect_url = CGI.unescape(@mail.body.match(/redirect_url=([^&]*)&/)[1])
110
+ @mail_reset_token = @mail.body.match(/reset_password_token=(.*)\"/)[1]
111
+ end
112
+
113
+ test 'response should return success status' do
114
+ assert_equal 200, response.status
115
+ end
116
+
117
+ test 'response should contains message' do
118
+ assert_equal \
119
+ @data['message'],
120
+ I18n.t('devise_token_auth.passwords.sended', email: @resource.email)
121
+ end
122
+
123
+ test 'action should send an email' do
124
+ assert @mail
125
+ end
126
+
127
+ test 'the email should be addressed to the user' do
128
+ assert_equal @mail.to.first, @resource.email
129
+ end
130
+
131
+ test 'the email body should contain a link with redirect url as a query param' do
132
+ assert_equal @redirect_url, @mail_redirect_url
133
+ end
134
+
135
+ test 'the client config name should fall back to "default"' do
136
+ assert_equal 'default', @mail_config_name
137
+ end
138
+
139
+ test 'the email body should contain a link with reset token as a query param' do
140
+ user = User.reset_password_by_token(reset_password_token: @mail_reset_token)
141
+
142
+ assert_equal user.id, @resource.id
143
+ end
144
+
145
+ describe 'password reset link failure' do
146
+ test 'response should return 404' do
147
+ assert_raises(ActionController::RoutingError) do
148
+ get :edit,
149
+ params: { reset_password_token: 'bogus',
150
+ redirect_url: @mail_redirect_url }
151
+ end
152
+ end
153
+ end
154
+
155
+ describe 'password reset link success' do
156
+ before do
157
+ get :edit,
158
+ params: { reset_password_token: @mail_reset_token,
159
+ redirect_url: @mail_redirect_url }
160
+
161
+ @resource.reload
162
+
163
+ raw_qs = response.location.split('?')[1]
164
+ @qs = Rack::Utils.parse_nested_query(raw_qs)
165
+
166
+ @access_token = @qs['access-token']
167
+ @client_id = @qs['client_id']
168
+ @client = @qs['client']
169
+ @expiry = @qs['expiry']
170
+ @reset_password = @qs['reset_password']
171
+ @token = @qs['token']
172
+ @uid = @qs['uid']
173
+ end
174
+
175
+ test 'response should have success redirect status' do
176
+ assert_equal 302, response.status
177
+ end
178
+
179
+ test 'response should contain auth params' do
180
+ assert @access_token
181
+ assert @client
182
+ assert @client_id
183
+ assert @expiry
184
+ assert @reset_password
185
+ assert @token
186
+ assert @uid
187
+ end
188
+
189
+ test 'response auth params should be valid' do
190
+ assert @resource.valid_token?(@token, @client_id)
191
+ assert @resource.valid_token?(@access_token, @client)
192
+ end
193
+ end
194
+ end
195
+
196
+ describe 'case-insensitive email' do
197
+ before do
198
+ @resource_class = User
199
+ @request_params = {
200
+ email: @resource.email.upcase,
201
+ redirect_url: @redirect_url
202
+ }
203
+ end
204
+
205
+ test 'response should return success status if configured' do
206
+ @resource_class.case_insensitive_keys = [:email]
207
+ post :create, params: @request_params
208
+ assert_equal 200, response.status
209
+ end
210
+
211
+ test 'response should return failure status if not configured' do
212
+ @resource_class.case_insensitive_keys = []
213
+ post :create, params: @request_params
214
+ assert_equal 404, response.status
215
+ end
216
+ end
217
+
218
+ describe 'Cheking reset_password_token' do
219
+ before do
220
+ post :create, params: {
221
+ email: @resource.email,
222
+ redirect_url: @redirect_url
223
+ }
224
+
225
+ @mail = ActionMailer::Base.deliveries.last
226
+ @mail_redirect_url = CGI.unescape(@mail.body.match(/redirect_url=([^&]*)&/)[1])
227
+ @mail_reset_token = @mail.body.match(/reset_password_token=(.*)\"/)[1]
228
+
229
+ @resource.reload
230
+ end
231
+
232
+ describe 'reset_password_token is valid' do
233
+
234
+ test 'mail_reset_token should be the same as reset_password_token' do
235
+ assert_equal Devise.token_generator.digest(self, :reset_password_token, @mail_reset_token), @resource.reset_password_token
236
+ end
237
+
238
+ test 'reset_password_token should not be rewritten by origin mail_reset_token' do
239
+ get :edit, params: {
240
+ reset_password_token: @mail_reset_token,
241
+ redirect_url: @mail_redirect_url
242
+ }
243
+ @resource.reload
244
+
245
+ assert_equal Devise.token_generator.digest(self, :reset_password_token, @mail_reset_token), @resource.reset_password_token
246
+ end
247
+
248
+ test 'response should return success status' do
249
+ get :edit, params: {
250
+ reset_password_token: @mail_reset_token,
251
+ redirect_url: @mail_redirect_url
252
+ }
253
+
254
+ assert_equal 302, response.status
255
+ end
256
+
257
+ test 'reset_password_sent_at should be valid' do
258
+ assert_equal @resource.reset_password_period_valid?, true
259
+
260
+ get :edit, params: {
261
+ reset_password_token: @mail_reset_token,
262
+ redirect_url: @mail_redirect_url
263
+ }
264
+
265
+ @resource.reload
266
+ assert_equal Devise.token_generator.digest(self, :reset_password_token, @mail_reset_token), @resource.reset_password_token
267
+ end
268
+
269
+ test 'reset_password_sent_at should be expired' do
270
+ assert_equal @resource.reset_password_period_valid?, true
271
+
272
+ @resource.update reset_password_sent_at: @resource.reset_password_sent_at - Devise.reset_password_within - 1.seconds
273
+ assert_equal @resource.reset_password_period_valid?, false
274
+
275
+ assert_raises(ActionController::RoutingError) {
276
+ get :edit, params: {
277
+ reset_password_token: @mail_reset_token,
278
+ redirect_url: @mail_redirect_url
279
+ }
280
+ }
281
+ end
282
+ end
283
+
284
+ describe 'reset_password_token is not valid' do
285
+ test 'response should return error status' do
286
+ @resource.update reset_password_token: 'koskoskoskos'
287
+
288
+ assert_not_equal Devise.token_generator.digest(self, :reset_password_token, @mail_reset_token), @resource.reset_password_token
289
+
290
+ assert_raises(ActionController::RoutingError) {
291
+ get :edit, params: {
292
+ reset_password_token: @mail_reset_token,
293
+ redirect_url: @mail_redirect_url
294
+ }
295
+ }
296
+ end
297
+ end
298
+ end
299
+ end
300
+
301
+ describe 'Using default_password_reset_url' do
302
+ before do
303
+ @resource = create(:user, :confirmed)
304
+ @redirect_url = 'http://ng-token-auth.dev'
305
+
306
+ DeviseTokenAuth.default_password_reset_url = @redirect_url
307
+
308
+ post :create,
309
+ params: { email: @resource.email,
310
+ redirect_url: @redirect_url }
311
+
312
+ @mail = ActionMailer::Base.deliveries.last
313
+ @resource.reload
314
+
315
+ @sent_redirect_url = CGI.unescape(@mail.body.match(/redirect_url=([^&]*)&/)[1])
316
+ end
317
+
318
+ teardown do
319
+ DeviseTokenAuth.default_password_reset_url = nil
320
+ end
321
+
322
+ test 'response should return success status' do
323
+ assert_equal 200, response.status
324
+ end
325
+
326
+ test 'action should send an email' do
327
+ assert @mail
328
+ end
329
+
330
+ test 'the email body should contain a link with redirect url as a query param' do
331
+ assert_equal @redirect_url, @sent_redirect_url
332
+ end
333
+ end
334
+
335
+ describe 'Using redirect_whitelist' do
336
+ before do
337
+ @resource = create(:user, :confirmed)
338
+ @good_redirect_url = Faker::Internet.url
339
+ @bad_redirect_url = Faker::Internet.url
340
+ DeviseTokenAuth.redirect_whitelist = [@good_redirect_url]
341
+ end
342
+
343
+ teardown do
344
+ DeviseTokenAuth.redirect_whitelist = nil
345
+ end
346
+
347
+ test 'request to whitelisted redirect should be successful' do
348
+ post :create,
349
+ params: { email: @resource.email,
350
+ redirect_url: @good_redirect_url }
351
+
352
+ assert_equal 200, response.status
353
+ end
354
+
355
+ test 'request to non-whitelisted redirect should fail' do
356
+ post :create,
357
+ params: { email: @resource.email,
358
+ redirect_url: @bad_redirect_url }
359
+
360
+ assert_equal 422, response.status
361
+ end
362
+ test 'request to non-whitelisted redirect should return error message' do
363
+ post :create,
364
+ params: { email: @resource.email,
365
+ redirect_url: @bad_redirect_url }
366
+
367
+ @data = JSON.parse(response.body)
368
+ assert @data['errors']
369
+ assert_equal @data['errors'],
370
+ [I18n.t('devise_token_auth.passwords.not_allowed_redirect_url',
371
+ redirect_url: @bad_redirect_url)]
372
+ end
373
+ end
374
+
375
+ describe 'change password with current password required' do
376
+ before do
377
+ DeviseTokenAuth.check_current_password_before_update = :password
378
+ end
379
+
380
+ after do
381
+ DeviseTokenAuth.check_current_password_before_update = false
382
+ end
383
+
384
+ describe 'success' do
385
+ before do
386
+ @auth_headers = @resource.create_new_auth_token
387
+ request.headers.merge!(@auth_headers)
388
+ @new_password = Faker::Internet.password
389
+ @resource.update password: 'secret123', password_confirmation: 'secret123'
390
+
391
+ put :update,
392
+ params: { password: @new_password,
393
+ password_confirmation: @new_password,
394
+ current_password: 'secret123' }
395
+
396
+ @data = JSON.parse(response.body)
397
+ @resource.reload
398
+ end
399
+
400
+ test 'request should be successful' do
401
+ assert_equal 200, response.status
402
+ end
403
+ end
404
+
405
+ describe 'success with after password reset' do
406
+ before do
407
+ # create a new password reset request
408
+ post :create, params: { email: @resource.email,
409
+ redirect_url: @redirect_url }
410
+
411
+ @mail = ActionMailer::Base.deliveries.last
412
+ @mail_redirect_url = CGI.unescape(@mail.body.match(/redirect_url=([^&]*)&/)[1])
413
+ @mail_reset_token = @mail.body.match(/reset_password_token=(.*)\"/)[1]
414
+
415
+ # confirm via password reset email link
416
+ get :edit, params: { reset_password_token: @mail_reset_token,
417
+ redirect_url: @mail_redirect_url }
418
+
419
+ @resource.reload
420
+ @allow_password_change_after_reset = @resource.allow_password_change
421
+
422
+ @auth_headers = @resource.create_new_auth_token
423
+ request.headers.merge!(@auth_headers)
424
+ @new_password = Faker::Internet.password
425
+
426
+ put :update, params: { password: @new_password,
427
+ password_confirmation: @new_password }
428
+
429
+ @data = JSON.parse(response.body)
430
+ @resource.reload
431
+ @allow_password_change = @resource.allow_password_change
432
+ @resource.reload
433
+ end
434
+
435
+ test 'request should be successful' do
436
+ assert_equal 200, response.status
437
+ end
438
+
439
+ test 'changes allow_password_change to true on reset' do
440
+ assert_equal true, @allow_password_change_after_reset
441
+ end
442
+
443
+ test 'sets allow_password_change false' do
444
+ assert_equal false, @allow_password_change
445
+ end
446
+ end
447
+
448
+ describe 'current password mismatch error' do
449
+ before do
450
+ @auth_headers = @resource.create_new_auth_token
451
+ request.headers.merge!(@auth_headers)
452
+ @new_password = Faker::Internet.password
453
+
454
+ put :update, params: { password: @new_password,
455
+ password_confirmation: @new_password,
456
+ current_password: 'not_very_secret321' }
457
+ end
458
+
459
+ test 'response should fail unauthorized' do
460
+ assert_equal 422, response.status
461
+ end
462
+ end
463
+ end
464
+
465
+ describe 'change password' do
466
+ describe 'success' do
467
+ before do
468
+ @auth_headers = @resource.create_new_auth_token
469
+ request.headers.merge!(@auth_headers)
470
+ @new_password = Faker::Internet.password
471
+
472
+ put :update, params: { password: @new_password,
473
+ password_confirmation: @new_password }
474
+
475
+ @data = JSON.parse(response.body)
476
+ @resource.reload
477
+ end
478
+
479
+ test 'request should be successful' do
480
+ assert_equal 200, response.status
481
+ end
482
+
483
+ test 'request should return success message' do
484
+ assert @data['message']
485
+ assert_equal @data['message'],
486
+ I18n.t('devise_token_auth.passwords.successfully_updated')
487
+ end
488
+
489
+ test 'new password should authenticate user' do
490
+ assert @resource.valid_password?(@new_password)
491
+ end
492
+
493
+ test 'reset_password_token should be removed' do
494
+ assert_nil @resource.reset_password_token
495
+ end
496
+ end
497
+
498
+ describe 'password mismatch error' do
499
+ before do
500
+ @auth_headers = @resource.create_new_auth_token
501
+ request.headers.merge!(@auth_headers)
502
+ @new_password = Faker::Internet.password
503
+
504
+ put :update, params: { password: 'chong',
505
+ password_confirmation: 'bong' }
506
+ end
507
+
508
+ test 'response should fail' do
509
+ assert_equal 422, response.status
510
+ end
511
+ end
512
+
513
+ describe 'unauthorized user' do
514
+ before do
515
+ @auth_headers = @resource.create_new_auth_token
516
+ @new_password = Faker::Internet.password
517
+
518
+ put :update, params: { password: @new_password,
519
+ password_confirmation: @new_password }
520
+ end
521
+
522
+ test 'response should fail' do
523
+ assert_equal 401, response.status
524
+ end
525
+ end
526
+ end
527
+ end
528
+
529
+ describe 'Alternate user class' do
530
+ setup do
531
+ @request.env['devise.mapping'] = Devise.mappings[:mang]
532
+ end
533
+
534
+ teardown do
535
+ @request.env['devise.mapping'] = Devise.mappings[:user]
536
+ end
537
+
538
+ before do
539
+ @resource = create(:mang_user, :confirmed)
540
+ @redirect_url = 'http://ng-token-auth.dev'
541
+
542
+ post :create, params: { email: @resource.email,
543
+ redirect_url: @redirect_url }
544
+
545
+ @mail = ActionMailer::Base.deliveries.last
546
+ @resource.reload
547
+
548
+ @mail_config_name = CGI.unescape(@mail.body.match(/config=([^&]*)&/)[1])
549
+ @mail_redirect_url = CGI.unescape(@mail.body.match(/redirect_url=([^&]*)&/)[1])
550
+ @mail_reset_token = @mail.body.match(/reset_password_token=(.*)\"/)[1]
551
+ end
552
+
553
+ test 'response should return success status' do
554
+ assert_equal 200, response.status
555
+ end
556
+
557
+ test 'the email body should contain a link with reset token as a query param' do
558
+ user = Mang.reset_password_by_token(reset_password_token: @mail_reset_token)
559
+
560
+ assert_equal user.id, @resource.id
561
+ end
562
+ end
563
+
564
+ describe 'unconfirmed user' do
565
+ before do
566
+ @resource = create(:user)
567
+ @redirect_url = 'http://ng-token-auth.dev'
568
+
569
+ post :create, params: { email: @resource.email,
570
+ redirect_url: @redirect_url }
571
+
572
+ @mail = ActionMailer::Base.deliveries.last
573
+ @resource.reload
574
+
575
+ @mail_config_name = CGI.unescape(@mail.body.match(/config=([^&]*)&/)[1])
576
+ @mail_redirect_url = CGI.unescape(@mail.body.match(/redirect_url=([^&]*)&/)[1])
577
+ @mail_reset_token = @mail.body.match(/reset_password_token=(.*)\"/)[1]
578
+
579
+ get :edit, params: { reset_password_token: @mail_reset_token,
580
+ redirect_url: @mail_redirect_url }
581
+
582
+ @resource.reload
583
+ end
584
+ end
585
+
586
+ describe 'unconfirmable user' do
587
+ setup do
588
+ @request.env['devise.mapping'] = Devise.mappings[:unconfirmable_user]
589
+ end
590
+
591
+ teardown do
592
+ @request.env['devise.mapping'] = Devise.mappings[:user]
593
+ end
594
+
595
+ before do
596
+ @resource = unconfirmable_users(:user)
597
+ @redirect_url = 'http://ng-token-auth.dev'
598
+
599
+ post :create, params: { email: @resource.email,
600
+ redirect_url: @redirect_url }
601
+
602
+ @mail = ActionMailer::Base.deliveries.last
603
+ @resource.reload
604
+
605
+ @mail_config_name = CGI.unescape(@mail.body.match(/config=([^&]*)&/)[1])
606
+ @mail_redirect_url = CGI.unescape(@mail.body.match(/redirect_url=([^&]*)&/)[1])
607
+ @mail_reset_token = @mail.body.match(/reset_password_token=(.*)\"/)[1]
608
+
609
+ get :edit, params: { reset_password_token: @mail_reset_token,
610
+ redirect_url: @mail_redirect_url }
611
+
612
+ @resource.reload
613
+ end
614
+ end
615
+
616
+ describe 'alternate user type' do
617
+ before do
618
+ @resource = create(:user, :confirmed)
619
+ @redirect_url = 'http://ng-token-auth.dev'
620
+ @config_name = 'altUser'
621
+
622
+ post :create, params: { email: @resource.email,
623
+ redirect_url: @redirect_url,
624
+ config_name: @config_name }
625
+
626
+ @mail = ActionMailer::Base.deliveries.last
627
+ @resource.reload
628
+
629
+ @mail_config_name = CGI.unescape(@mail.body.match(/config=([^&]*)&/)[1])
630
+ @mail_redirect_url = CGI.unescape(@mail.body.match(/redirect_url=([^&]*)&/)[1])
631
+ @mail_reset_token = @mail.body.match(/reset_password_token=(.*)\"/)[1]
632
+ end
633
+
634
+ test 'config_name param is included in the confirmation email link' do
635
+ assert_equal @config_name, @mail_config_name
636
+ end
637
+ end
638
+ end
639
+ end