devise_token_auth 0.1.42 → 0.1.43.beta1

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of devise_token_auth might be problematic. Click here for more details.

Files changed (69) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +39 -38
  3. data/app/controllers/devise_token_auth/application_controller.rb +15 -0
  4. data/app/controllers/devise_token_auth/concerns/resource_finder.rb +38 -0
  5. data/app/controllers/devise_token_auth/concerns/set_user_by_token.rb +21 -19
  6. data/app/controllers/devise_token_auth/confirmations_controller.rb +12 -7
  7. data/app/controllers/devise_token_auth/omniauth_callbacks_controller.rb +3 -7
  8. data/app/controllers/devise_token_auth/passwords_controller.rb +16 -26
  9. data/app/controllers/devise_token_auth/registrations_controller.rb +6 -2
  10. data/app/controllers/devise_token_auth/sessions_controller.rb +3 -14
  11. data/app/controllers/devise_token_auth/unlocks_controller.rb +105 -0
  12. data/app/models/devise_token_auth/concerns/user.rb +29 -12
  13. data/app/models/devise_token_auth/concerns/user_omniauth_callbacks.rb +5 -8
  14. data/app/views/devise/mailer/unlock_instructions.html.erb +1 -1
  15. data/app/views/devise_token_auth/omniauth_external_window.html.erb +1 -1
  16. data/config/initializers/devise.rb +1 -1
  17. data/config/locales/da-DK.yml +50 -0
  18. data/config/locales/en.yml +4 -0
  19. data/lib/devise_token_auth/controllers/helpers.rb +30 -20
  20. data/lib/devise_token_auth/rails/routes.rb +1 -1
  21. data/lib/devise_token_auth/url.rb +1 -1
  22. data/lib/devise_token_auth/version.rb +1 -1
  23. data/lib/generators/devise_token_auth/templates/devise_token_auth_create_users.rb.erb +2 -1
  24. data/lib/generators/devise_token_auth/templates/user.rb +1 -1
  25. data/test/controllers/custom/custom_confirmations_controller_test.rb +5 -10
  26. data/test/controllers/custom/custom_omniauth_callbacks_controller_test.rb +15 -16
  27. data/test/controllers/custom/custom_passwords_controller_test.rb +28 -34
  28. data/test/controllers/custom/custom_registrations_controller_test.rb +23 -21
  29. data/test/controllers/custom/custom_sessions_controller_test.rb +16 -18
  30. data/test/controllers/custom/custom_token_validations_controller_test.rb +13 -11
  31. data/test/controllers/demo_group_controller_test.rb +19 -5
  32. data/test/controllers/demo_mang_controller_test.rb +37 -16
  33. data/test/controllers/demo_user_controller_test.rb +70 -38
  34. data/test/controllers/devise_token_auth/confirmations_controller_test.rb +40 -22
  35. data/test/controllers/devise_token_auth/omniauth_callbacks_controller_test.rb +108 -82
  36. data/test/controllers/devise_token_auth/passwords_controller_test.rb +127 -145
  37. data/test/controllers/devise_token_auth/registrations_controller_test.rb +258 -274
  38. data/test/controllers/devise_token_auth/sessions_controller_test.rb +112 -117
  39. data/test/controllers/devise_token_auth/token_validations_controller_test.rb +14 -15
  40. data/test/controllers/devise_token_auth/unlocks_controller_test.rb +194 -0
  41. data/test/controllers/overrides/confirmations_controller_test.rb +8 -9
  42. data/test/controllers/overrides/omniauth_callbacks_controller_test.rb +16 -12
  43. data/test/controllers/overrides/passwords_controller_test.rb +19 -15
  44. data/test/controllers/overrides/registrations_controller_test.rb +7 -7
  45. data/test/controllers/overrides/sessions_controller_test.rb +6 -6
  46. data/test/controllers/overrides/token_validations_controller_test.rb +7 -4
  47. data/test/dummy/app/controllers/auth_origin_controller.rb +2 -2
  48. data/test/dummy/app/controllers/custom/omniauth_callbacks_controller.rb +0 -2
  49. data/test/dummy/app/controllers/overrides/confirmations_controller.rb +11 -7
  50. data/test/dummy/app/controllers/overrides/passwords_controller.rb +10 -8
  51. data/test/dummy/app/controllers/overrides/sessions_controller.rb +1 -1
  52. data/test/dummy/db/migrate/20140715061447_devise_token_auth_create_users.rb +2 -2
  53. data/test/dummy/db/migrate/20140715061805_devise_token_auth_create_mangs.rb +2 -2
  54. data/test/dummy/db/migrate/20140829044006_add_operating_thetan_to_user.rb +1 -1
  55. data/test/dummy/db/migrate/20140916224624_add_favorite_color_to_mangs.rb +1 -1
  56. data/test/dummy/db/migrate/20140928231203_devise_token_auth_create_evil_users.rb +2 -1
  57. data/test/dummy/db/migrate/20141222035835_devise_token_auth_create_only_email_users.rb +1 -1
  58. data/test/dummy/db/migrate/20141222053502_devise_token_auth_create_unregisterable_users.rb +2 -1
  59. data/test/dummy/db/migrate/20150409095712_devise_token_auth_create_nice_users.rb +2 -1
  60. data/test/dummy/db/migrate/20150708104536_devise_token_auth_create_unconfirmable_users.rb +2 -1
  61. data/test/dummy/db/migrate/20160103235141_devise_token_auth_create_scoped_users.rb +2 -1
  62. data/test/dummy/db/migrate/20160629184441_devise_token_auth_create_lockable_users.rb +2 -1
  63. data/test/dummy/db/schema.rb +157 -162
  64. data/test/dummy/tmp/generators/app/models/user.rb +1 -1
  65. data/test/dummy/tmp/generators/db/migrate/{20170517171822_devise_token_auth_create_users.rb → 20171014052631_devise_token_auth_create_users.rb} +2 -1
  66. data/test/lib/generators/devise_token_auth/install_generator_test.rb +5 -1
  67. data/test/models/user_test.rb +35 -1
  68. data/test/test_helper.rb +17 -11
  69. metadata +85 -80
@@ -13,24 +13,30 @@ class OmniauthTest < ActionDispatch::IntegrationTest
13
13
  end
14
14
 
15
15
  before do
16
- @redirect_url = "http://ng-token-auth.dev/"
16
+ @redirect_url = 'http://ng-token-auth.dev/'
17
+ end
18
+
19
+ def get_parsed_data_json
20
+ encoded_json_data = @response.body.match(/var data \= JSON.parse\(decodeURIComponent\(\'(.+)\'\)\)\;/)[1]
21
+ JSON.parse(URI.unescape(encoded_json_data))
17
22
  end
18
23
 
19
24
  describe 'success callback' do
20
25
  setup do
21
- OmniAuth.config.mock_auth[:facebook] = OmniAuth::AuthHash.new({
22
- :provider => 'facebook',
23
- :uid => '123545',
24
- :info => {
26
+ OmniAuth.config.mock_auth[:facebook] = OmniAuth::AuthHash.new(
27
+ provider: 'facebook',
28
+ uid: '123545',
29
+ info: {
25
30
  name: 'chong',
26
31
  email: 'chongbong@aol.com'
27
32
  }
28
- })
33
+ )
29
34
  end
30
35
 
31
36
  test 'request should pass correct redirect_url' do
32
37
  get_success
33
- assert_equal @redirect_url, controller.send(:omniauth_params)['auth_origin_url']
38
+ assert_equal @redirect_url,
39
+ controller.send(:omniauth_params)['auth_origin_url']
34
40
  end
35
41
 
36
42
  test 'user should have been created' do
@@ -45,12 +51,14 @@ class OmniauthTest < ActionDispatch::IntegrationTest
45
51
 
46
52
  test 'user should be assigned token' do
47
53
  get_success
54
+
48
55
  client_id = controller.auth_params[:client_id]
49
56
  token = controller.auth_params[:auth_token]
50
57
  expiry = controller.auth_params[:expiry]
51
58
 
52
59
  # the expiry should have been set
53
- assert_equal expiry, @resource.tokens[client_id][:expiry]
60
+ assert_equal expiry, @resource.tokens[client_id]['expiry']
61
+
54
62
  # the token sent down to the client should now be valid
55
63
  assert @resource.valid_token?(token, client_id)
56
64
  end
@@ -68,7 +76,8 @@ class OmniauthTest < ActionDispatch::IntegrationTest
68
76
 
69
77
  test 'should be redirected via valid url' do
70
78
  get_success
71
- assert_equal 'http://www.example.com/auth/facebook/callback', request.original_url
79
+ assert_equal 'http://www.example.com/auth/facebook/callback',
80
+ request.original_url
72
81
  end
73
82
 
74
83
  describe 'with default user model' do
@@ -86,17 +95,23 @@ class OmniauthTest < ActionDispatch::IntegrationTest
86
95
 
87
96
  describe 'with alternate user model' do
88
97
  before do
89
- get_via_redirect '/mangs/facebook', {
90
- auth_origin_url: @redirect_url,
91
- omniauth_window_type: 'newWindow'
92
- }
98
+ get '/mangs/facebook',
99
+ params: {
100
+ auth_origin_url: @redirect_url,
101
+ omniauth_window_type: 'newWindow'
102
+ }
103
+
104
+ follow_all_redirects!
105
+
93
106
  assert_equal 200, response.status
94
107
  @resource = assigns(:resource)
95
108
  end
109
+
96
110
  test 'request should determine the correct resource_class' do
97
111
  assert_equal 'Mang', controller.send(:omniauth_params)['resource_class']
98
112
  end
99
- test 'user should be of the correct class' do
113
+
114
+ test 'user should be of the correct class' do
100
115
  assert_equal Mang, @resource.class
101
116
  end
102
117
  end
@@ -104,13 +119,14 @@ class OmniauthTest < ActionDispatch::IntegrationTest
104
119
  describe 'pass additional params' do
105
120
  before do
106
121
  @fav_color = 'alizarin crimson'
107
- @unpermitted_param = "M. Bison"
108
- get_via_redirect '/auth/facebook', {
109
- auth_origin_url: @redirect_url,
110
- favorite_color: @fav_color,
111
- name: @unpermitted_param,
112
- omniauth_window_type: 'newWindow'
113
- }
122
+ @unpermitted_param = 'M. Bison'
123
+ get '/auth/facebook',
124
+ params: { auth_origin_url: @redirect_url,
125
+ favorite_color: @fav_color,
126
+ name: @unpermitted_param,
127
+ omniauth_window_type: 'newWindow' }
128
+
129
+ follow_all_redirects!
114
130
 
115
131
  @resource = assigns(:resource)
116
132
  end
@@ -128,7 +144,7 @@ class OmniauthTest < ActionDispatch::IntegrationTest
128
144
  end
129
145
  end
130
146
 
131
- describe "oauth registration attr" do
147
+ describe 'oauth registration attr' do
132
148
  after do
133
149
  User.any_instance.unstub(:new_record?)
134
150
  end
@@ -139,11 +155,11 @@ class OmniauthTest < ActionDispatch::IntegrationTest
139
155
  end
140
156
 
141
157
  test 'response contains oauth_registration attr' do
158
+ get '/auth/facebook',
159
+ params: { auth_origin_url: @redirect_url,
160
+ omniauth_window_type: 'newWindow' }
142
161
 
143
- get_via_redirect '/auth/facebook', {
144
- auth_origin_url: @redirect_url,
145
- omniauth_window_type: 'newWindow'
146
- }
162
+ follow_all_redirects!
147
163
 
148
164
  assert_equal true, controller.auth_params[:oauth_registration]
149
165
  end
@@ -155,25 +171,24 @@ class OmniauthTest < ActionDispatch::IntegrationTest
155
171
  end
156
172
 
157
173
  test 'response does not contain oauth_registration attr' do
174
+ get '/auth/facebook',
175
+ params: { auth_origin_url: @redirect_url,
176
+ omniauth_window_type: 'newWindow' }
158
177
 
159
- get_via_redirect '/auth/facebook', {
160
- auth_origin_url: @redirect_url,
161
- omniauth_window_type: 'newWindow'
162
- }
178
+ follow_all_redirects!
163
179
 
164
180
  assert_equal false, controller.auth_params.key?(:oauth_registration)
165
181
  end
166
-
167
182
  end
168
-
169
183
  end
170
184
 
171
185
  describe 'using namespaces' do
172
186
  before do
173
- get_via_redirect '/api/v1/auth/facebook', {
174
- auth_origin_url: @redirect_url,
175
- omniauth_window_type: 'newWindow'
176
- }
187
+ get '/api/v1/auth/facebook',
188
+ params: { auth_origin_url: @redirect_url,
189
+ omniauth_window_type: 'newWindow' }
190
+
191
+ follow_all_redirects!
177
192
 
178
193
  @resource = assigns(:resource)
179
194
  end
@@ -196,7 +211,6 @@ class OmniauthTest < ActionDispatch::IntegrationTest
196
211
  get_success(omniauth_window_type: 'inAppBrowser')
197
212
  assert_expected_data_in_new_window
198
213
  end
199
-
200
214
  end
201
215
 
202
216
  describe 'with omniauth_window_type=newWindow' do
@@ -207,19 +221,20 @@ class OmniauthTest < ActionDispatch::IntegrationTest
207
221
  end
208
222
 
209
223
  def assert_expected_data_in_new_window
210
- data_json = @response.body.match(/var data \= (.+)\;/)[1]
211
- data = ActiveSupport::JSON.decode(data_json)
224
+ data = get_parsed_data_json
212
225
  expected_data = @resource.as_json.merge(controller.auth_params.as_json)
213
226
  expected_data = ActiveSupport::JSON.decode(expected_data.to_json)
214
- assert_equal(expected_data.merge("message" => "deliverCredentials"), data)
227
+ assert_equal(expected_data.merge('message' => 'deliverCredentials'), data)
215
228
  end
216
229
 
217
230
  describe 'with omniauth_window_type=sameWindow' do
218
231
  test 'redirects to auth_origin_url with all expected query params' do
219
- get_via_redirect '/auth/facebook', {
220
- auth_origin_url: '/auth_origin',
221
- omniauth_window_type: 'sameWindow'
222
- }
232
+ get '/auth/facebook',
233
+ params: { auth_origin_url: '/auth_origin',
234
+ omniauth_window_type: 'sameWindow' }
235
+
236
+ follow_all_redirects!
237
+
223
238
  assert_equal 200, response.status
224
239
 
225
240
  # We have been forwarded to a url with all the expected
@@ -228,21 +243,26 @@ class OmniauthTest < ActionDispatch::IntegrationTest
228
243
  # Assert that a uid was passed along. We have to assume
229
244
  # that the rest of the values were as well, as we don't
230
245
  # have access to @resource in this test anymore
231
- assert(uid = controller.params['uid'], "No uid found")
246
+ assert(controller.params['uid'], 'No uid found')
232
247
 
233
248
  # check that all the auth stuff is there
234
- [:auth_token, :client_id, :uid, :expiry, :config].each do |key|
249
+ %i[auth_token client_id uid expiry config].each do |key|
235
250
  assert(controller.params.key?(key), "No value for #{key.inspect}")
236
251
  end
237
252
  end
238
253
  end
239
254
 
240
255
  def get_success(params = {})
241
- get_via_redirect '/auth/facebook', {
242
- auth_origin_url: @redirect_url,
243
- omniauth_window_type: 'newWindow'
244
- }.merge(params)
256
+ get '/auth/facebook',
257
+ params: {
258
+ auth_origin_url: @redirect_url,
259
+ omniauth_window_type: 'newWindow'
260
+ }.merge(params)
261
+
262
+ follow_all_redirects!
263
+
245
264
  assert_equal 200, response.status
265
+
246
266
  @resource = assigns(:resource)
247
267
  end
248
268
  end
@@ -250,38 +270,40 @@ class OmniauthTest < ActionDispatch::IntegrationTest
250
270
  describe 'failure callback' do
251
271
  setup do
252
272
  OmniAuth.config.mock_auth[:facebook] = :invalid_credentials
253
- OmniAuth.config.on_failure = Proc.new { |env|
273
+ OmniAuth.config.on_failure = proc { |env|
254
274
  OmniAuth::FailureEndpoint.new(env).redirect_to_failure
255
275
  }
256
276
  end
257
277
 
258
278
  test 'renders expected data' do
259
- get_via_redirect '/auth/facebook', {
260
- auth_origin_url: @redirect_url,
261
- omniauth_window_type: 'newWindow'
262
- }
279
+ get '/auth/facebook',
280
+ params: { auth_origin_url: @redirect_url,
281
+ omniauth_window_type: 'newWindow' }
282
+
283
+ follow_all_redirects!
284
+
263
285
  assert_equal 200, response.status
264
286
 
265
- data_json = @response.body.match(/var data \= (.+)\;/)[1]
266
- data = ActiveSupport::JSON.decode(data_json)
287
+ data = get_parsed_data_json
267
288
 
268
- assert_equal({"error"=>"invalid_credentials", "message"=>"authFailure"}, data)
289
+ assert_equal({ 'error' => 'invalid_credentials', 'message' => 'authFailure' }, data)
269
290
  end
270
291
 
271
292
  test 'renders something with no auth_origin_url' do
272
- get_via_redirect '/auth/facebook'
293
+ get '/auth/facebook'
294
+ follow_all_redirects!
273
295
  assert_equal 200, response.status
274
- assert_select "body", "invalid_credentials"
296
+ assert_select 'body', 'invalid_credentials'
275
297
  end
276
298
  end
277
299
 
278
300
  describe 'User with only :database_authenticatable and :registerable included' do
279
301
  test 'OnlyEmailUser should not be able to use OAuth' do
280
- assert_raises(ActionController::RoutingError) {
281
- get_via_redirect '/only_email_auth/facebook', {
282
- auth_origin_url: @redirect_url
283
- }
284
- }
302
+ assert_raises(ActionController::RoutingError) do
303
+ get '/only_email_auth/facebook',
304
+ params: { auth_origin_url: @redirect_url }
305
+ follow_all_redirects!
306
+ end
285
307
  end
286
308
  end
287
309
 
@@ -306,36 +328,40 @@ class OmniauthTest < ActionDispatch::IntegrationTest
306
328
  end
307
329
 
308
330
  test 'request using non-whitelisted redirect fail' do
309
- get_via_redirect '/auth/facebook',
310
- auth_origin_url: @bad_redirect_url,
311
- omniauth_window_type: 'newWindow'
331
+ get '/auth/facebook',
332
+ params: { auth_origin_url: @bad_redirect_url,
333
+ omniauth_window_type: 'newWindow' }
312
334
 
313
- data_json = @response.body.match(/var data \= (.+)\;/)[1]
314
- data = ActiveSupport::JSON.decode(data_json)
315
- assert_equal "Redirect to '#{@bad_redirect_url}' not allowed.",
335
+ follow_all_redirects!
336
+
337
+ data = get_parsed_data_json
338
+ assert_equal "Redirect to &#39;#{@bad_redirect_url}&#39; not allowed.",
316
339
  data['error']
317
340
  end
318
341
 
319
342
  test 'request to whitelisted redirect should succeed' do
320
- get_via_redirect '/auth/facebook',
321
- auth_origin_url: @good_redirect_url,
322
- omniauth_window_type: 'newWindow'
343
+ get '/auth/facebook',
344
+ params: {
345
+ auth_origin_url: @good_redirect_url,
346
+ omniauth_window_type: 'newWindow'
347
+ }
323
348
 
324
- data_json = @response.body.match(/var data \= (.+)\;/)[1]
325
- data = ActiveSupport::JSON.decode(data_json)
349
+ follow_all_redirects!
350
+
351
+ data = get_parsed_data_json
326
352
  assert_equal @user_email, data['email']
327
353
  end
328
354
 
329
355
  test 'should support wildcards' do
330
356
  DeviseTokenAuth.redirect_whitelist = ["#{@good_redirect_url[0..8]}*"]
331
- get_via_redirect '/auth/facebook',
332
- auth_origin_url: @good_redirect_url,
333
- omniauth_window_type: 'newWindow'
357
+ get '/auth/facebook',
358
+ params: { auth_origin_url: @good_redirect_url,
359
+ omniauth_window_type: 'newWindow' }
360
+
361
+ follow_all_redirects!
334
362
 
335
- data_json = @response.body.match(/var data \= (.+)\;/)[1]
336
- data = ActiveSupport::JSON.decode(data_json)
363
+ data = get_parsed_data_json
337
364
  assert_equal @user_email, data['email']
338
365
  end
339
-
340
366
  end
341
367
  end
@@ -8,7 +8,7 @@ require 'test_helper'
8
8
 
9
9
  class DeviseTokenAuth::PasswordsControllerTest < ActionController::TestCase
10
10
  describe DeviseTokenAuth::PasswordsController do
11
- describe "Password reset" do
11
+ describe 'Password reset' do
12
12
  before do
13
13
  @resource = users(:confirmed_email_user)
14
14
  @redirect_url = 'http://ng-token-auth.dev'
@@ -19,18 +19,19 @@ class DeviseTokenAuth::PasswordsControllerTest < ActionController::TestCase
19
19
  @auth_headers = @resource.create_new_auth_token
20
20
  @new_password = Faker::Internet.password
21
21
 
22
- xhr :post, :create, {
23
- redirect_url: @redirect_url
24
- }
22
+ post :create,
23
+ params: { redirect_url: @redirect_url }
25
24
  @data = JSON.parse(response.body)
26
25
  end
27
26
 
28
27
  test 'response should fail' do
29
28
  assert_equal 401, response.status
30
29
  end
30
+
31
31
  test 'error message should be returned' do
32
- assert @data["errors"]
33
- assert_equal @data["errors"], [I18n.t("devise_token_auth.passwords.missing_email")]
32
+ assert @data['errors']
33
+ assert_equal @data['errors'],
34
+ [I18n.t('devise_token_auth.passwords.missing_email')]
34
35
  end
35
36
  end
36
37
 
@@ -39,62 +40,62 @@ class DeviseTokenAuth::PasswordsControllerTest < ActionController::TestCase
39
40
  @auth_headers = @resource.create_new_auth_token
40
41
  @new_password = Faker::Internet.password
41
42
 
42
- xhr :post, :create, {
43
- email: 'chester@cheet.ah',
44
- }
43
+ post :create,
44
+ params: { email: 'chester@cheet.ah' }
45
45
  @data = JSON.parse(response.body)
46
46
  end
47
47
 
48
48
  test 'response should fail' do
49
49
  assert_equal 401, response.status
50
50
  end
51
+
51
52
  test 'error message should be returned' do
52
- assert @data["errors"]
53
- assert_equal @data["errors"], [I18n.t("devise_token_auth.passwords.missing_redirect_url")]
53
+ assert @data['errors']
54
+ assert_equal @data['errors'],
55
+ [I18n.t('devise_token_auth.passwords.missing_redirect_url')]
54
56
  end
55
57
  end
56
58
 
57
59
  describe 'request password reset' do
58
60
  describe 'unknown user should return 404' do
59
61
  before do
60
- xhr :post, :create, {
61
- email: 'chester@cheet.ah',
62
- redirect_url: @redirect_url
63
- }
62
+ post :create,
63
+ params: { email: 'chester@cheet.ah',
64
+ redirect_url: @redirect_url }
64
65
  @data = JSON.parse(response.body)
65
66
  end
67
+
66
68
  test 'unknown user should return 404' do
67
69
  assert_equal 404, response.status
68
70
  end
69
71
 
70
72
  test 'errors should be returned' do
71
- assert @data["errors"]
72
- assert_equal @data["errors"], [I18n.t("devise_token_auth.passwords.user_not_found", email: 'chester@cheet.ah')]
73
+ assert @data['errors']
74
+ assert_equal @data['errors'],
75
+ [I18n.t('devise_token_auth.passwords.user_not_found',
76
+ email: 'chester@cheet.ah')]
73
77
  end
74
78
  end
75
79
 
76
80
  describe 'successfully requested password reset' do
77
81
  before do
78
- xhr :post, :create, {
79
- email: @resource.email,
80
- redirect_url: @redirect_url
81
- }
82
+ post :create,
83
+ params: { email: @resource.email,
84
+ redirect_url: @redirect_url }
82
85
 
83
86
  @data = JSON.parse(response.body)
84
87
  end
85
88
 
86
89
  test 'response should not contain extra data' do
87
- assert_nil @data["data"]
90
+ assert_nil @data['data']
88
91
  end
89
92
  end
90
93
 
91
-
92
94
  describe 'case-sensitive email' do
93
95
  before do
94
- xhr :post, :create, {
95
- email: @resource.email,
96
- redirect_url: @redirect_url
97
- }
96
+ post :create,
97
+ params: { email: @resource.email,
98
+ redirect_url: @redirect_url }
98
99
 
99
100
  @mail = ActionMailer::Base.deliveries.last
100
101
  @resource.reload
@@ -110,7 +111,7 @@ class DeviseTokenAuth::PasswordsControllerTest < ActionController::TestCase
110
111
  end
111
112
 
112
113
  test 'response should contains message' do
113
- assert_equal @data["message"], I18n.t("devise_token_auth.passwords.sended", email: @resource.email)
114
+ assert_equal @data['message'], I18n.t('devise_token_auth.passwords.sended', email: @resource.email)
114
115
  end
115
116
 
116
117
  test 'action should send an email' do
@@ -130,41 +131,39 @@ class DeviseTokenAuth::PasswordsControllerTest < ActionController::TestCase
130
131
  end
131
132
 
132
133
  test 'the email body should contain a link with reset token as a query param' do
133
- user = User.reset_password_by_token({
134
- reset_password_token: @mail_reset_token
135
- })
134
+ user = User.reset_password_by_token(reset_password_token: @mail_reset_token)
136
135
 
137
136
  assert_equal user.id, @resource.id
138
137
  end
139
138
 
140
139
  describe 'password reset link failure' do
141
140
  test 'response should return 404' do
142
- assert_raises(ActionController::RoutingError) {
143
- xhr :get, :edit, {
144
- reset_password_token: "bogus",
145
- redirect_url: @mail_redirect_url
146
- }
147
- }
141
+ assert_raises(ActionController::RoutingError) do
142
+ get :edit,
143
+ params: { reset_password_token: 'bogus',
144
+ redirect_url: @mail_redirect_url }
145
+ end
148
146
  end
149
147
  end
150
148
 
151
149
  describe 'password reset link success' do
152
150
  before do
153
- xhr :get, :edit, {
154
- reset_password_token: @mail_reset_token,
155
- redirect_url: @mail_redirect_url
156
- }
151
+ get :edit,
152
+ params: { reset_password_token: @mail_reset_token,
153
+ redirect_url: @mail_redirect_url }
157
154
 
158
155
  @resource.reload
159
156
 
160
157
  raw_qs = response.location.split('?')[1]
161
158
  @qs = Rack::Utils.parse_nested_query(raw_qs)
162
159
 
163
- @client_id = @qs["client_id"]
164
- @expiry = @qs["expiry"]
165
- @reset_password = @qs["reset_password"]
166
- @token = @qs["token"]
167
- @uid = @qs["uid"]
160
+ @access_token = @qs['access-token']
161
+ @client_id = @qs['client_id']
162
+ @client = @qs['client']
163
+ @expiry = @qs['expiry']
164
+ @reset_password = @qs['reset_password']
165
+ @token = @qs['token']
166
+ @uid = @qs['uid']
168
167
  end
169
168
 
170
169
  test 'respones should have success redirect status' do
@@ -172,6 +171,8 @@ class DeviseTokenAuth::PasswordsControllerTest < ActionController::TestCase
172
171
  end
173
172
 
174
173
  test 'response should contain auth params' do
174
+ assert @access_token
175
+ assert @client
175
176
  assert @client_id
176
177
  assert @expiry
177
178
  assert @reset_password
@@ -181,9 +182,9 @@ class DeviseTokenAuth::PasswordsControllerTest < ActionController::TestCase
181
182
 
182
183
  test 'response auth params should be valid' do
183
184
  assert @resource.valid_token?(@token, @client_id)
185
+ assert @resource.valid_token?(@access_token, @client)
184
186
  end
185
187
  end
186
-
187
188
  end
188
189
 
189
190
  describe 'case-insensitive email' do
@@ -197,13 +198,13 @@ class DeviseTokenAuth::PasswordsControllerTest < ActionController::TestCase
197
198
 
198
199
  test 'response should return success status if configured' do
199
200
  @resource_class.case_insensitive_keys = [:email]
200
- xhr :post, :create, @request_params
201
+ post :create, params: @request_params
201
202
  assert_equal 200, response.status
202
203
  end
203
204
 
204
205
  test 'response should return failure status if not configured' do
205
206
  @resource_class.case_insensitive_keys = []
206
- xhr :post, :create, @request_params
207
+ post :create, params: @request_params
207
208
  assert_equal 404, response.status
208
209
  end
209
210
  end
@@ -216,10 +217,9 @@ class DeviseTokenAuth::PasswordsControllerTest < ActionController::TestCase
216
217
 
217
218
  DeviseTokenAuth.default_password_reset_url = @redirect_url
218
219
 
219
- xhr :post, :create, {
220
- email: @resource.email,
221
- redirect_url: @redirect_url
222
- }
220
+ post :create,
221
+ params: { email: @resource.email,
222
+ redirect_url: @redirect_url }
223
223
 
224
224
  @mail = ActionMailer::Base.deliveries.last
225
225
  @resource.reload
@@ -256,36 +256,35 @@ class DeviseTokenAuth::PasswordsControllerTest < ActionController::TestCase
256
256
  DeviseTokenAuth.redirect_whitelist = nil
257
257
  end
258
258
 
259
- test "request to whitelisted redirect should be successful" do
260
- xhr :post, :create, {
261
- email: @resource.email,
262
- redirect_url: @good_redirect_url
263
- }
259
+ test 'request to whitelisted redirect should be successful' do
260
+ post :create,
261
+ params: { email: @resource.email,
262
+ redirect_url: @good_redirect_url }
264
263
 
265
264
  assert_equal 200, response.status
266
265
  end
267
266
 
268
- test "request to non-whitelisted redirect should fail" do
269
- xhr :post, :create, {
270
- email: @resource.email,
271
- redirect_url: @bad_redirect_url
272
- }
267
+ test 'request to non-whitelisted redirect should fail' do
268
+ post :create,
269
+ params: { email: @resource.email,
270
+ redirect_url: @bad_redirect_url }
273
271
 
274
272
  assert_equal 422, response.status
275
273
  end
276
- test "request to non-whitelisted redirect should return error message" do
277
- xhr :post, :create, {
278
- email: @resource.email,
279
- redirect_url: @bad_redirect_url
280
- }
274
+ test 'request to non-whitelisted redirect should return error message' do
275
+ post :create,
276
+ params: { email: @resource.email,
277
+ redirect_url: @bad_redirect_url }
281
278
 
282
279
  @data = JSON.parse(response.body)
283
- assert @data["errors"]
284
- assert_equal @data["errors"], [I18n.t("devise_token_auth.passwords.not_allowed_redirect_url", redirect_url: @bad_redirect_url)]
280
+ assert @data['errors']
281
+ assert_equal @data['errors'],
282
+ [I18n.t('devise_token_auth.passwords.not_allowed_redirect_url',
283
+ redirect_url: @bad_redirect_url)]
285
284
  end
286
285
  end
287
286
 
288
- describe "change password with current password required" do
287
+ describe 'change password with current password required' do
289
288
  before do
290
289
  DeviseTokenAuth.check_current_password_before_update = :password
291
290
  end
@@ -301,56 +300,59 @@ class DeviseTokenAuth::PasswordsControllerTest < ActionController::TestCase
301
300
  @new_password = Faker::Internet.password
302
301
  @resource.update password: 'secret123', password_confirmation: 'secret123'
303
302
 
304
- xhr :put, :update, {
305
- password: @new_password,
306
- password_confirmation: @new_password,
307
- current_password: 'secret123'
308
- }
303
+ put :update,
304
+ params: { password: @new_password,
305
+ password_confirmation: @new_password,
306
+ current_password: 'secret123' }
309
307
 
310
308
  @data = JSON.parse(response.body)
311
309
  @resource.reload
312
310
  end
313
311
 
314
- test "request should be successful" do
312
+ test 'request should be successful' do
315
313
  assert_equal 200, response.status
316
314
  end
317
315
  end
318
316
 
319
317
  describe 'success with after password reset' do
320
318
  before do
321
- xhr :post, :create, {
322
- email: @resource.email,
323
- redirect_url: @redirect_url
324
- }
319
+ # create a new password reset request
320
+ post :create, params: { email: @resource.email,
321
+ redirect_url: @redirect_url }
325
322
 
326
323
  @mail = ActionMailer::Base.deliveries.last
327
324
  @mail_redirect_url = CGI.unescape(@mail.body.match(/redirect_url=([^&]*)&/)[1])
328
325
  @mail_reset_token = @mail.body.match(/reset_password_token=(.*)\"/)[1]
329
326
 
330
- xhr :get, :edit, {
331
- reset_password_token: @mail_reset_token,
332
- redirect_url: @mail_redirect_url
333
- }
327
+ # confirm via password reset email link
328
+ get :edit, params: { reset_password_token: @mail_reset_token,
329
+ redirect_url: @mail_redirect_url }
330
+
331
+ @resource.reload
332
+ @allow_password_change_after_reset = @resource.allow_password_change
334
333
 
335
334
  @auth_headers = @resource.create_new_auth_token
336
335
  request.headers.merge!(@auth_headers)
337
336
  @new_password = Faker::Internet.password
338
337
 
339
- xhr :put, :update, {
340
- password: @new_password,
341
- password_confirmation: @new_password
342
- }
338
+ put :update, params: { password: @new_password,
339
+ password_confirmation: @new_password }
343
340
 
344
341
  @data = JSON.parse(response.body)
342
+ @resource.reload
345
343
  @allow_password_change = @resource.allow_password_change
346
344
  @resource.reload
347
345
  end
348
346
 
349
- test "request should be successful" do
347
+ test 'request should be successful' do
350
348
  assert_equal 200, response.status
351
349
  end
352
350
 
353
- test "sets allow_password_change false" do
351
+ test 'changes allow_password_change to true on reset' do
352
+ assert_equal true, @allow_password_change_after_reset
353
+ end
354
+
355
+ test 'sets allow_password_change false' do
354
356
  assert_equal false, @allow_password_change
355
357
  end
356
358
  end
@@ -361,11 +363,9 @@ class DeviseTokenAuth::PasswordsControllerTest < ActionController::TestCase
361
363
  request.headers.merge!(@auth_headers)
362
364
  @new_password = Faker::Internet.password
363
365
 
364
- xhr :put, :update, {
365
- password: @new_password,
366
- password_confirmation: @new_password,
367
- current_password: 'not_very_secret321'
368
- }
366
+ put :update, params: { password: @new_password,
367
+ password_confirmation: @new_password,
368
+ current_password: 'not_very_secret321' }
369
369
  end
370
370
 
371
371
  test 'response should fail unauthorized' do
@@ -374,32 +374,31 @@ class DeviseTokenAuth::PasswordsControllerTest < ActionController::TestCase
374
374
  end
375
375
  end
376
376
 
377
- describe "change password" do
377
+ describe 'change password' do
378
378
  describe 'success' do
379
379
  before do
380
380
  @auth_headers = @resource.create_new_auth_token
381
381
  request.headers.merge!(@auth_headers)
382
382
  @new_password = Faker::Internet.password
383
383
 
384
- xhr :put, :update, {
385
- password: @new_password,
386
- password_confirmation: @new_password
387
- }
384
+ put :update, params: { password: @new_password,
385
+ password_confirmation: @new_password }
388
386
 
389
387
  @data = JSON.parse(response.body)
390
388
  @resource.reload
391
389
  end
392
390
 
393
- test "request should be successful" do
391
+ test 'request should be successful' do
394
392
  assert_equal 200, response.status
395
393
  end
396
394
 
397
- test "request should return success message" do
398
- assert @data["message"]
399
- assert_equal @data["message"], I18n.t("devise_token_auth.passwords.successfully_updated")
395
+ test 'request should return success message' do
396
+ assert @data['message']
397
+ assert_equal @data['message'],
398
+ I18n.t('devise_token_auth.passwords.successfully_updated')
400
399
  end
401
400
 
402
- test "new password should authenticate user" do
401
+ test 'new password should authenticate user' do
403
402
  assert @resource.valid_password?(@new_password)
404
403
  end
405
404
  end
@@ -410,10 +409,8 @@ class DeviseTokenAuth::PasswordsControllerTest < ActionController::TestCase
410
409
  request.headers.merge!(@auth_headers)
411
410
  @new_password = Faker::Internet.password
412
411
 
413
- xhr :put, :update, {
414
- password: 'chong',
415
- password_confirmation: 'bong'
416
- }
412
+ put :update, params: { password: 'chong',
413
+ password_confirmation: 'bong' }
417
414
  end
418
415
 
419
416
  test 'response should fail' do
@@ -426,10 +423,8 @@ class DeviseTokenAuth::PasswordsControllerTest < ActionController::TestCase
426
423
  @auth_headers = @resource.create_new_auth_token
427
424
  @new_password = Faker::Internet.password
428
425
 
429
- xhr :put, :update, {
430
- password: @new_password,
431
- password_confirmation: @new_password
432
- }
426
+ put :update, params: { password: @new_password,
427
+ password_confirmation: @new_password }
433
428
  end
434
429
 
435
430
  test 'response should fail' do
@@ -439,7 +434,7 @@ class DeviseTokenAuth::PasswordsControllerTest < ActionController::TestCase
439
434
  end
440
435
  end
441
436
 
442
- describe "Alternate user class" do
437
+ describe 'Alternate user class' do
443
438
  setup do
444
439
  @request.env['devise.mapping'] = Devise.mappings[:mang]
445
440
  end
@@ -452,10 +447,8 @@ class DeviseTokenAuth::PasswordsControllerTest < ActionController::TestCase
452
447
  @resource = mangs(:confirmed_email_user)
453
448
  @redirect_url = 'http://ng-token-auth.dev'
454
449
 
455
- xhr :post, :create, {
456
- email: @resource.email,
457
- redirect_url: @redirect_url
458
- }
450
+ post :create, params: { email: @resource.email,
451
+ redirect_url: @redirect_url }
459
452
 
460
453
  @mail = ActionMailer::Base.deliveries.last
461
454
  @resource.reload
@@ -470,9 +463,7 @@ class DeviseTokenAuth::PasswordsControllerTest < ActionController::TestCase
470
463
  end
471
464
 
472
465
  test 'the email body should contain a link with reset token as a query param' do
473
- user = Mang.reset_password_by_token({
474
- reset_password_token: @mail_reset_token
475
- })
466
+ user = Mang.reset_password_by_token(reset_password_token: @mail_reset_token)
476
467
 
477
468
  assert_equal user.id, @resource.id
478
469
  end
@@ -483,10 +474,8 @@ class DeviseTokenAuth::PasswordsControllerTest < ActionController::TestCase
483
474
  @resource = users(:unconfirmed_email_user)
484
475
  @redirect_url = 'http://ng-token-auth.dev'
485
476
 
486
- xhr :post, :create, {
487
- email: @resource.email,
488
- redirect_url: @redirect_url
489
- }
477
+ post :create, params: { email: @resource.email,
478
+ redirect_url: @redirect_url }
490
479
 
491
480
  @mail = ActionMailer::Base.deliveries.last
492
481
  @resource.reload
@@ -495,14 +484,13 @@ class DeviseTokenAuth::PasswordsControllerTest < ActionController::TestCase
495
484
  @mail_redirect_url = CGI.unescape(@mail.body.match(/redirect_url=([^&]*)&/)[1])
496
485
  @mail_reset_token = @mail.body.match(/reset_password_token=(.*)\"/)[1]
497
486
 
498
- xhr :get, :edit, {
499
- reset_password_token: @mail_reset_token,
500
- redirect_url: @mail_redirect_url
501
- }
487
+ get :edit, params: { reset_password_token: @mail_reset_token,
488
+ redirect_url: @mail_redirect_url }
502
489
 
503
490
  @resource.reload
504
491
  end
505
492
  end
493
+
506
494
  describe 'unconfirmable user' do
507
495
  setup do
508
496
  @request.env['devise.mapping'] = Devise.mappings[:unconfirmable_user]
@@ -516,10 +504,8 @@ class DeviseTokenAuth::PasswordsControllerTest < ActionController::TestCase
516
504
  @resource = unconfirmable_users(:user)
517
505
  @redirect_url = 'http://ng-token-auth.dev'
518
506
 
519
- xhr :post, :create, {
520
- email: @resource.email,
521
- redirect_url: @redirect_url
522
- }
507
+ post :create, params: { email: @resource.email,
508
+ redirect_url: @redirect_url }
523
509
 
524
510
  @mail = ActionMailer::Base.deliveries.last
525
511
  @resource.reload
@@ -528,10 +514,8 @@ class DeviseTokenAuth::PasswordsControllerTest < ActionController::TestCase
528
514
  @mail_redirect_url = CGI.unescape(@mail.body.match(/redirect_url=([^&]*)&/)[1])
529
515
  @mail_reset_token = @mail.body.match(/reset_password_token=(.*)\"/)[1]
530
516
 
531
- xhr :get, :edit, {
532
- reset_password_token: @mail_reset_token,
533
- redirect_url: @mail_redirect_url
534
- }
517
+ get :edit, params: { reset_password_token: @mail_reset_token,
518
+ redirect_url: @mail_redirect_url }
535
519
 
536
520
  @resource.reload
537
521
  end
@@ -539,15 +523,13 @@ class DeviseTokenAuth::PasswordsControllerTest < ActionController::TestCase
539
523
 
540
524
  describe 'alternate user type' do
541
525
  before do
542
- @resource = users(:confirmed_email_user)
526
+ @resource = users(:confirmed_email_user)
543
527
  @redirect_url = 'http://ng-token-auth.dev'
544
- @config_name = "altUser"
528
+ @config_name = 'altUser'
545
529
 
546
- xhr :post, :create, {
547
- email: @resource.email,
548
- redirect_url: @redirect_url,
549
- config_name: @config_name
550
- }
530
+ post :create, params: { email: @resource.email,
531
+ redirect_url: @redirect_url,
532
+ config_name: @config_name }
551
533
 
552
534
  @mail = ActionMailer::Base.deliveries.last
553
535
  @resource.reload