devise_token_auth 1.1.4 → 1.2.1

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 (56) hide show
  1. checksums.yaml +4 -4
  2. data/app/controllers/devise_token_auth/application_controller.rb +17 -0
  3. data/app/controllers/devise_token_auth/concerns/resource_finder.rb +14 -1
  4. data/app/controllers/devise_token_auth/concerns/set_user_by_token.rb +45 -8
  5. data/app/controllers/devise_token_auth/confirmations_controller.rb +8 -4
  6. data/app/controllers/devise_token_auth/omniauth_callbacks_controller.rb +8 -4
  7. data/app/controllers/devise_token_auth/passwords_controller.rb +10 -2
  8. data/app/controllers/devise_token_auth/sessions_controller.rb +21 -3
  9. data/app/controllers/devise_token_auth/unlocks_controller.rb +6 -2
  10. data/app/models/devise_token_auth/concerns/active_record_support.rb +0 -2
  11. data/app/models/devise_token_auth/concerns/confirmable_support.rb +2 -1
  12. data/app/models/devise_token_auth/concerns/tokens_serialization.rb +16 -4
  13. data/app/models/devise_token_auth/concerns/user.rb +31 -15
  14. data/app/models/devise_token_auth/concerns/user_omniauth_callbacks.rb +12 -5
  15. data/app/validators/devise_token_auth_email_validator.rb +10 -2
  16. data/app/views/devise_token_auth/omniauth_external_window.html.erb +1 -1
  17. data/config/locales/en.yml +3 -0
  18. data/config/locales/ja.yml +12 -0
  19. data/lib/devise_token_auth/blacklist.rb +5 -1
  20. data/lib/devise_token_auth/controllers/helpers.rb +5 -9
  21. data/lib/devise_token_auth/engine.rb +11 -2
  22. data/lib/devise_token_auth/rails/routes.rb +17 -12
  23. data/lib/devise_token_auth/version.rb +1 -1
  24. data/lib/generators/devise_token_auth/install_generator.rb +1 -1
  25. data/lib/generators/devise_token_auth/templates/devise_token_auth.rb +3 -0
  26. data/lib/generators/devise_token_auth/templates/devise_token_auth_create_users.rb.erb +1 -1
  27. data/test/controllers/demo_mang_controller_test.rb +37 -8
  28. data/test/controllers/demo_user_controller_test.rb +37 -8
  29. data/test/controllers/devise_token_auth/confirmations_controller_test.rb +100 -19
  30. data/test/controllers/devise_token_auth/omniauth_callbacks_controller_test.rb +2 -2
  31. data/test/controllers/devise_token_auth/passwords_controller_test.rb +73 -21
  32. data/test/controllers/devise_token_auth/registrations_controller_test.rb +28 -15
  33. data/test/controllers/devise_token_auth/sessions_controller_test.rb +39 -10
  34. data/test/controllers/devise_token_auth/token_validations_controller_test.rb +41 -1
  35. data/test/controllers/devise_token_auth/unlocks_controller_test.rb +44 -5
  36. data/test/controllers/overrides/confirmations_controller_test.rb +1 -1
  37. data/test/dummy/app/views/layouts/application.html.erb +0 -2
  38. data/test/dummy/config/application.rb +0 -1
  39. data/test/dummy/config/environments/development.rb +0 -10
  40. data/test/dummy/config/environments/production.rb +0 -16
  41. data/test/dummy/db/schema.rb +5 -5
  42. data/test/dummy/tmp/generators/app/models/user.rb +11 -0
  43. data/test/dummy/tmp/generators/config/initializers/devise_token_auth.rb +60 -0
  44. data/test/dummy/tmp/generators/db/migrate/20220822003050_devise_token_auth_create_users.rb +49 -0
  45. data/test/lib/devise_token_auth/blacklist_test.rb +11 -3
  46. data/test/lib/devise_token_auth/rails/custom_routes_test.rb +29 -0
  47. data/test/lib/devise_token_auth/rails/routes_test.rb +87 -0
  48. data/test/lib/generators/devise_token_auth/install_generator_test.rb +1 -1
  49. data/test/lib/generators/devise_token_auth/install_generator_with_namespace_test.rb +1 -1
  50. data/test/models/concerns/tokens_serialization_test.rb +39 -5
  51. data/test/models/user_test.rb +22 -0
  52. data/test/test_helper.rb +35 -4
  53. metadata +16 -26
  54. data/test/dummy/config/initializers/assets.rb +0 -10
  55. data/test/dummy/tmp/generators/app/views/devise/mailer/confirmation_instructions.html.erb +0 -5
  56. data/test/dummy/tmp/generators/app/views/devise/mailer/reset_password_instructions.html.erb +0 -8
@@ -25,9 +25,13 @@ module DeviseTokenAuth
25
25
  :remove_tokens_after_password_reset,
26
26
  :default_callbacks,
27
27
  :headers_names,
28
+ :cookie_enabled,
29
+ :cookie_name,
30
+ :cookie_attributes,
28
31
  :bypass_sign_in,
29
32
  :send_confirmation_email,
30
- :require_client_password_reset_token
33
+ :require_client_password_reset_token,
34
+ :other_uid
31
35
 
32
36
  self.change_headers_on_each_request = true
33
37
  self.max_number_of_devices = 10
@@ -42,14 +46,19 @@ module DeviseTokenAuth
42
46
  self.enable_standard_devise_support = false
43
47
  self.remove_tokens_after_password_reset = false
44
48
  self.default_callbacks = true
45
- self.headers_names = { 'access-token': 'access-token',
49
+ self.headers_names = { 'authorization': 'Authorization',
50
+ 'access-token': 'access-token',
46
51
  'client': 'client',
47
52
  'expiry': 'expiry',
48
53
  'uid': 'uid',
49
54
  'token-type': 'token-type' }
55
+ self.cookie_enabled = false
56
+ self.cookie_name = 'auth_cookie'
57
+ self.cookie_attributes = {}
50
58
  self.bypass_sign_in = true
51
59
  self.send_confirmation_email = false
52
60
  self.require_client_password_reset_token = false
61
+ self.other_uid = nil
53
62
 
54
63
  def self.setup(&block)
55
64
  yield self
@@ -8,26 +8,31 @@ module ActionDispatch::Routing
8
8
  opts[:skip] ||= []
9
9
 
10
10
  # check for ctrl overrides, fall back to defaults
11
- sessions_ctrl = opts[:controllers][:sessions] || 'devise_token_auth/sessions'
12
- registrations_ctrl = opts[:controllers][:registrations] || 'devise_token_auth/registrations'
13
- passwords_ctrl = opts[:controllers][:passwords] || 'devise_token_auth/passwords'
14
- confirmations_ctrl = opts[:controllers][:confirmations] || 'devise_token_auth/confirmations'
15
- token_validations_ctrl = opts[:controllers][:token_validations] || 'devise_token_auth/token_validations'
16
- omniauth_ctrl = opts[:controllers][:omniauth_callbacks] || 'devise_token_auth/omniauth_callbacks'
17
- unlocks_ctrl = opts[:controllers][:unlocks] || 'devise_token_auth/unlocks'
11
+ sessions_ctrl = opts[:controllers].delete(:sessions) || 'devise_token_auth/sessions'
12
+ registrations_ctrl = opts[:controllers].delete(:registrations) || 'devise_token_auth/registrations'
13
+ passwords_ctrl = opts[:controllers].delete(:passwords) || 'devise_token_auth/passwords'
14
+ confirmations_ctrl = opts[:controllers].delete(:confirmations) || 'devise_token_auth/confirmations'
15
+ token_validations_ctrl = opts[:controllers].delete(:token_validations) || 'devise_token_auth/token_validations'
16
+ omniauth_ctrl = opts[:controllers].delete(:omniauth_callbacks) || 'devise_token_auth/omniauth_callbacks'
17
+ unlocks_ctrl = opts[:controllers].delete(:unlocks) || 'devise_token_auth/unlocks'
18
+
19
+ # check for resource override
20
+ route = opts[:as] || resource.pluralize.underscore.gsub('/', '_')
18
21
 
19
22
  # define devise controller mappings
20
- controllers = { sessions: sessions_ctrl,
23
+ controllers = opts[:controllers].merge(
24
+ sessions: sessions_ctrl,
21
25
  registrations: registrations_ctrl,
22
26
  passwords: passwords_ctrl,
23
- confirmations: confirmations_ctrl }
27
+ confirmations: confirmations_ctrl
28
+ )
24
29
 
25
30
  controllers[:unlocks] = unlocks_ctrl if unlocks_ctrl
26
31
 
27
32
  # remove any unwanted devise modules
28
33
  opts[:skip].each{ |item| controllers.delete(item) }
29
34
 
30
- devise_for resource.pluralize.underscore.gsub('/', '_').to_sym,
35
+ devise_for route.to_sym,
31
36
  class_name: resource,
32
37
  module: :devise,
33
38
  path: opts[:at].to_s,
@@ -68,7 +73,7 @@ module ActionDispatch::Routing
68
73
 
69
74
  # preserve the resource class thru oauth authentication by setting name of
70
75
  # resource as "resource_class" param
71
- match "#{full_path}/:provider", to: redirect{ |params, request|
76
+ match "#{full_path}/:provider", to: redirect(status: 307) { |params, request|
72
77
  # get the current querystring
73
78
  qs = CGI::parse(request.env['QUERY_STRING'])
74
79
 
@@ -94,7 +99,7 @@ module ActionDispatch::Routing
94
99
 
95
100
  # re-construct the path for omniauth
96
101
  "#{::OmniAuth.config.path_prefix}/#{params[:provider]}?#{redirect_params.to_param}"
97
- }, via: [:get]
102
+ }, via: [:get, :post]
98
103
  end
99
104
  end
100
105
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module DeviseTokenAuth
4
- VERSION = '1.1.4'.freeze
4
+ VERSION = '1.2.1'.freeze
5
5
  end
@@ -26,7 +26,7 @@ module DeviseTokenAuth
26
26
  inclusion = 'include DeviseTokenAuth::Concerns::User'
27
27
  unless parse_file_for_line(fname, inclusion)
28
28
 
29
- active_record_needle = (Rails::VERSION::MAJOR == 5) ? 'ApplicationRecord' : 'ActiveRecord::Base'
29
+ active_record_needle = (Rails::VERSION::MAJOR >= 5) ? 'ApplicationRecord' : 'ActiveRecord::Base'
30
30
  inject_into_file fname, after: "class #{user_class} < #{active_record_needle}\n" do <<-'RUBY'
31
31
  # Include default devise modules.
32
32
  devise :database_authenticatable, :registerable,
@@ -48,6 +48,9 @@ DeviseTokenAuth.setup do |config|
48
48
  # :'uid' => 'uid',
49
49
  # :'token-type' => 'token-type' }
50
50
 
51
+ # Makes it possible to use custom uid column
52
+ # config.other_uid = "foo"
53
+
51
54
  # By default, only Bearer Token authentication is implemented out of the box.
52
55
  # If, however, you wish to integrate with legacy Devise authentication, you can
53
56
  # do so by enabling this flag. NOTE: This feature is highly experimental!
@@ -44,6 +44,6 @@ class DeviseTokenAuthCreate<%= user_class.pluralize.gsub("::","") %> < ActiveRec
44
44
  add_index :<%= table_name %>, [:uid, :provider], unique: true
45
45
  add_index :<%= table_name %>, :reset_password_token, unique: true
46
46
  add_index :<%= table_name %>, :confirmation_token, unique: true
47
- # add_index :<%= table_name %>, :unlock_token, unique: true
47
+ # add_index :<%= table_name %>, :unlock_token, unique: true
48
48
  end
49
49
  end
@@ -235,7 +235,7 @@ class DemoMangControllerTest < ActionDispatch::IntegrationTest
235
235
  @resource.reload
236
236
  age_token(@resource, @client_id)
237
237
 
238
- # use expired auth header
238
+ # use previous auth header
239
239
  get '/demo/members_only_mang',
240
240
  params: {},
241
241
  headers: @auth_headers
@@ -244,38 +244,67 @@ class DemoMangControllerTest < ActionDispatch::IntegrationTest
244
244
  @second_user = assigns(:resource)
245
245
  @second_access_token = response.headers['access-token']
246
246
  @second_response_status = response.status
247
+
248
+ @resource.reload
249
+ age_token(@resource, @client_id)
250
+
251
+ # use expired auth headers
252
+ get '/demo/members_only_mang',
253
+ params: {},
254
+ headers: @auth_headers
255
+
256
+ @third_is_batch_request = assigns(:is_batch_request)
257
+ @third_user = assigns(:resource)
258
+ @third_access_token = response.headers['access-token']
259
+ @third_response_status = response.status
247
260
  end
248
261
 
249
262
  it 'should allow the first request through' do
250
263
  assert_equal 200, @first_response_status
251
264
  end
252
265
 
266
+ it 'should allow the second request through' do
267
+ assert_equal 200, @second_response_status
268
+ end
269
+
253
270
  it 'should not allow the second request through' do
254
- assert_equal 401, @second_response_status
271
+ assert_equal 401, @third_response_status
255
272
  end
256
273
 
257
274
  it 'should not treat first request as batch request' do
275
+ refute @first_is_batch_request
276
+ end
277
+
278
+ it 'should not treat second request as batch request' do
258
279
  refute @second_is_batch_request
259
280
  end
260
281
 
282
+ it 'should not treat third request as batch request' do
283
+ refute @third_is_batch_request
284
+ end
285
+
261
286
  it 'should return auth headers from the first request' do
262
287
  assert @first_access_token
263
288
  end
264
289
 
265
- it 'should not treat second request as batch request' do
266
- refute @second_is_batch_request
290
+ it 'should return auth headers from the second request' do
291
+ assert @second_access_token
267
292
  end
268
293
 
269
- it 'should not return auth headers from the second request' do
270
- refute @second_access_token
294
+ it 'should not return auth headers from the third request' do
295
+ refute @third_access_token
271
296
  end
272
297
 
273
298
  it 'should define user during first request' do
274
299
  assert @first_user
275
300
  end
276
301
 
277
- it 'should not define user during second request' do
278
- refute @second_user
302
+ it 'should define user during second request' do
303
+ assert @second_user
304
+ end
305
+
306
+ it 'should not define user during third request' do
307
+ refute @third_user
279
308
  end
280
309
  end
281
310
  end
@@ -265,7 +265,7 @@ class DemoUserControllerTest < ActionDispatch::IntegrationTest
265
265
  @resource.reload
266
266
  age_token(@resource, @client_id)
267
267
 
268
- # use expired auth header
268
+ # use previous auth header
269
269
  get '/demo/members_only',
270
270
  params: {},
271
271
  headers: @auth_headers
@@ -274,38 +274,67 @@ class DemoUserControllerTest < ActionDispatch::IntegrationTest
274
274
  @second_user = assigns(:resource)
275
275
  @second_access_token = response.headers['access-token']
276
276
  @second_response_status = response.status
277
+
278
+ @resource.reload
279
+ age_token(@resource, @client_id)
280
+
281
+ # use expired auth headers
282
+ get '/demo/members_only_mang',
283
+ params: {},
284
+ headers: @auth_headers
285
+
286
+ @third_is_batch_request = assigns(:is_batch_request)
287
+ @third_user = assigns(:resource)
288
+ @third_access_token = response.headers['access-token']
289
+ @third_response_status = response.status
277
290
  end
278
291
 
279
292
  it 'should allow the first request through' do
280
293
  assert_equal 200, @first_response_status
281
294
  end
282
295
 
296
+ it 'should allow the second request through' do
297
+ assert_equal 200, @second_response_status
298
+ end
299
+
283
300
  it 'should not allow the second request through' do
284
- assert_equal 401, @second_response_status
301
+ assert_equal 401, @third_response_status
285
302
  end
286
303
 
287
304
  it 'should not treat first request as batch request' do
305
+ refute @first_is_batch_request
306
+ end
307
+
308
+ it 'should not treat second request as batch request' do
288
309
  refute @second_is_batch_request
289
310
  end
290
311
 
312
+ it 'should not treat third request as batch request' do
313
+ refute @third_is_batch_request
314
+ end
315
+
291
316
  it 'should return auth headers from the first request' do
292
317
  assert @first_access_token
293
318
  end
294
319
 
295
- it 'should not treat second request as batch request' do
296
- refute @second_is_batch_request
320
+ it 'should return auth headers from the second request' do
321
+ assert @second_access_token
297
322
  end
298
323
 
299
- it 'should not return auth headers from the second request' do
300
- refute @second_access_token
324
+ it 'should not return auth headers from the third request' do
325
+ refute @third_access_token
301
326
  end
302
327
 
303
328
  it 'should define user during first request' do
304
329
  assert @first_user
305
330
  end
306
331
 
307
- it 'should not define user during second request' do
308
- refute @second_user
332
+ it 'should define user during second request' do
333
+ assert @second_user
334
+ end
335
+
336
+ it 'should not define user during third request' do
337
+ refute @third_user
309
338
  end
310
339
  end
311
340
  end
@@ -92,30 +92,111 @@ class DeviseTokenAuth::ConfirmationsControllerTest < ActionController::TestCase
92
92
  end
93
93
 
94
94
  describe 'resend confirmation' do
95
- before do
96
- post :create,
97
- params: { email: @new_user.email,
98
- redirect_url: @redirect_url },
99
- xhr: true
100
- @resource = assigns(:resource)
101
-
102
- @mail = ActionMailer::Base.deliveries.last
103
- @token, @client_config = token_and_client_config_from(@mail.body)
104
- end
105
-
106
- test 'user should not be confirmed' do
107
- assert_nil @resource.confirmed_at
95
+ describe 'without paranoid mode' do
96
+
97
+ describe 'on success' do
98
+ before do
99
+ post :create,
100
+ params: { email: @new_user.email,
101
+ redirect_url: @redirect_url },
102
+ xhr: true
103
+ @resource = assigns(:resource)
104
+ @data = JSON.parse(response.body)
105
+ @mail = ActionMailer::Base.deliveries.last
106
+ @token, @client_config = token_and_client_config_from(@mail.body)
107
+ end
108
+
109
+ test 'user should not be confirmed' do
110
+ assert_nil @resource.confirmed_at
111
+ end
112
+
113
+ test 'should generate raw token' do
114
+ assert @token
115
+ assert_equal @new_user.confirmation_token, @token
116
+ end
117
+
118
+ test 'user should receive confirmation email' do
119
+ assert_equal @resource.email, @mail['to'].to_s
120
+ end
121
+
122
+ test 'response should contain message' do
123
+ assert_equal @data['message'], I18n.t('devise_token_auth.confirmations.sended', email: @resource.email)
124
+ end
125
+ end
126
+
127
+ describe 'on failure' do
128
+ before do
129
+ post :create,
130
+ params: { email: 'chester@cheet.ah',
131
+ redirect_url: @redirect_url },
132
+ xhr: true
133
+ @data = JSON.parse(response.body)
134
+ end
135
+
136
+ test 'response should contain errors' do
137
+ assert_equal @data['errors'], [I18n.t('devise_token_auth.confirmations.user_not_found', email: 'chester@cheet.ah')]
138
+ end
139
+ end
108
140
  end
141
+ end
109
142
 
110
- test 'should generate raw token' do
111
- assert @token
112
- assert_equal @new_user.confirmation_token, @token
143
+ describe 'with paranoid mode' do
144
+ describe 'on success' do
145
+ before do
146
+ swap Devise, paranoid: true do
147
+ post :create,
148
+ params: { email: @new_user.email,
149
+ redirect_url: @redirect_url },
150
+ xhr: true
151
+ @resource = assigns(:resource)
152
+ @data = JSON.parse(response.body)
153
+ @mail = ActionMailer::Base.deliveries.last
154
+ @token, @client_config = token_and_client_config_from(@mail.body)
155
+ end
156
+ end
157
+
158
+ test 'user should not be confirmed' do
159
+ assert_nil @resource.confirmed_at
160
+ end
161
+
162
+ test 'should generate raw token' do
163
+ assert @token
164
+ assert_equal @new_user.confirmation_token, @token
165
+ end
166
+
167
+ test 'user should receive confirmation email' do
168
+ assert_equal @resource.email, @mail['to'].to_s
169
+ end
170
+
171
+ test 'response should contain message' do
172
+ assert_equal @data['message'], I18n.t('devise_token_auth.confirmations.sended_paranoid', email: @resource.email)
173
+ end
174
+
175
+ test 'response should return success status' do
176
+ assert_equal 200, response.status
177
+ end
113
178
  end
114
179
 
115
- test 'user should receive confirmation email' do
116
- assert_equal @resource.email, @mail['to'].to_s
180
+ describe 'on failure' do
181
+ before do
182
+ swap Devise, paranoid: true do
183
+ @email = 'chester@cheet.ah'
184
+ post :create,
185
+ params: { email: @email,
186
+ redirect_url: @redirect_url },
187
+ xhr: true
188
+ @data = JSON.parse(response.body)
189
+ end
190
+ end
191
+
192
+ test 'response should not contain errors' do
193
+ assert_equal @data['message'], I18n.t('devise_token_auth.confirmations.sended_paranoid', email: @email)
194
+ end
195
+
196
+ test 'response should return success status' do
197
+ assert_equal 200, response.status
198
+ end
117
199
  end
118
-
119
200
  end
120
201
  end
121
202
 
@@ -18,7 +18,7 @@ class OmniauthTest < ActionDispatch::IntegrationTest
18
18
 
19
19
  def get_parsed_data_json
20
20
  encoded_json_data = @response.body.match(/var data \= JSON.parse\(decodeURIComponent\(\'(.+)\'\)\)\;/)[1]
21
- JSON.parse(URI.unescape(encoded_json_data))
21
+ JSON.parse(CGI.unescape(encoded_json_data))
22
22
  end
23
23
 
24
24
  describe 'success callback' do
@@ -346,7 +346,7 @@ class OmniauthTest < ActionDispatch::IntegrationTest
346
346
  follow_all_redirects!
347
347
 
348
348
  data = get_parsed_data_json
349
- assert_equal "Redirect to &#39;#{@bad_redirect_url}&#39; not allowed.",
349
+ assert_equal "Redirect to '#{@bad_redirect_url}' not allowed.",
350
350
  data['error']
351
351
  end
352
352
 
@@ -85,37 +85,89 @@ class DeviseTokenAuth::PasswordsControllerTest < ActionController::TestCase
85
85
  end
86
86
 
87
87
  describe 'request password reset' do
88
- describe 'unknown user should return 404' do
89
- before do
90
- post :create,
91
- params: { email: 'chester@cheet.ah',
92
- redirect_url: @redirect_url }
93
- @data = JSON.parse(response.body)
94
- end
88
+ describe 'unknown user' do
89
+ describe 'without paranoid mode' do
90
+ before do
91
+ post :create,
92
+ params: { email: 'chester@cheet.ah',
93
+ redirect_url: @redirect_url }
94
+ @data = JSON.parse(response.body)
95
+ end
95
96
 
96
- test 'unknown user should return 404' do
97
- assert_equal 404, response.status
97
+ test 'unknown user should return 404' do
98
+ assert_equal 404, response.status
99
+ end
100
+
101
+ test 'errors should be returned' do
102
+ assert @data['errors']
103
+ assert_equal @data['errors'],
104
+ [I18n.t('devise_token_auth.passwords.user_not_found',
105
+ email: 'chester@cheet.ah')]
106
+ end
98
107
  end
99
108
 
100
- test 'errors should be returned' do
101
- assert @data['errors']
102
- assert_equal @data['errors'],
103
- [I18n.t('devise_token_auth.passwords.user_not_found',
104
- email: 'chester@cheet.ah')]
109
+ describe 'with paranoid mode' do
110
+ before do
111
+ swap Devise, paranoid: true do
112
+ post :create,
113
+ params: { email: 'chester@cheet.ah',
114
+ redirect_url: @redirect_url }
115
+ @data = JSON.parse(response.body)
116
+ end
117
+ end
118
+
119
+ test 'response should return success status' do
120
+ assert_equal 200, response.status
121
+ end
122
+
123
+ test 'response should contain message' do
124
+ assert_equal \
125
+ @data['message'],
126
+ I18n.t('devise_token_auth.passwords.sended_paranoid')
127
+ end
105
128
  end
106
129
  end
107
130
 
108
131
  describe 'successfully requested password reset' do
109
- before do
110
- post :create,
111
- params: { email: @resource.email,
112
- redirect_url: @redirect_url }
132
+ describe 'without paranoid mode' do
133
+ before do
134
+ post :create,
135
+ params: { email: @resource.email,
136
+ redirect_url: @redirect_url }
113
137
 
114
- @data = JSON.parse(response.body)
138
+ @data = JSON.parse(response.body)
139
+ end
140
+
141
+ test 'response should not contain extra data' do
142
+ assert_nil @data['data']
143
+ end
144
+
145
+ test 'response should contains message' do
146
+ assert_equal \
147
+ @data['message'],
148
+ I18n.t('devise_token_auth.passwords.sended', email: @resource.email)
149
+ end
115
150
  end
116
151
 
117
- test 'response should not contain extra data' do
118
- assert_nil @data['data']
152
+ describe 'with paranoid mode' do
153
+ before do
154
+ swap Devise, paranoid: true do
155
+ post :create,
156
+ params: { email: @resource.email,
157
+ redirect_url: @redirect_url }
158
+ @data = JSON.parse(response.body)
159
+ end
160
+ end
161
+
162
+ test 'response should return success status' do
163
+ assert_equal 200, response.status
164
+ end
165
+
166
+ test 'response should contain message' do
167
+ assert_equal \
168
+ @data['message'],
169
+ I18n.t('devise_token_auth.passwords.sended_paranoid')
170
+ end
119
171
  end
120
172
  end
121
173
 
@@ -10,6 +10,17 @@ require 'test_helper'
10
10
 
11
11
  class DeviseTokenAuth::RegistrationsControllerTest < ActionDispatch::IntegrationTest
12
12
  describe DeviseTokenAuth::RegistrationsController do
13
+
14
+ def mock_registration_params
15
+ {
16
+ email: Faker::Internet.email,
17
+ password: 'secret123',
18
+ password_confirmation: 'secret123',
19
+ confirm_success_url: Faker::Internet.url,
20
+ unpermitted_param: '(x_x)'
21
+ }
22
+ end
23
+
13
24
  describe 'Validate non-empty body' do
14
25
  before do
15
26
  # need to post empty data
@@ -41,13 +52,7 @@ class DeviseTokenAuth::RegistrationsControllerTest < ActionDispatch::Integration
41
52
  @mails_sent = ActionMailer::Base.deliveries.count
42
53
 
43
54
  post '/auth',
44
- params: {
45
- email: Faker::Internet.email,
46
- password: 'secret123',
47
- password_confirmation: 'secret123',
48
- confirm_success_url: Faker::Internet.url,
49
- unpermitted_param: '(x_x)'
50
- }
55
+ params: mock_registration_params
51
56
 
52
57
  @resource = assigns(:resource)
53
58
  @data = JSON.parse(response.body)
@@ -87,17 +92,10 @@ class DeviseTokenAuth::RegistrationsControllerTest < ActionDispatch::Integration
87
92
  before do
88
93
  @original_duration = Devise.allow_unconfirmed_access_for
89
94
  Devise.allow_unconfirmed_access_for = nil
90
- post '/auth',
91
- params: {
92
- email: Faker::Internet.email,
93
- password: 'secret123',
94
- password_confirmation: 'secret123',
95
- confirm_success_url: Faker::Internet.url,
96
- unpermitted_param: '(x_x)'
97
- }
98
95
  end
99
96
 
100
97
  test 'auth headers were returned in response' do
98
+ post '/auth', params: mock_registration_params
101
99
  assert response.headers['access-token']
102
100
  assert response.headers['token-type']
103
101
  assert response.headers['client']
@@ -105,6 +103,21 @@ class DeviseTokenAuth::RegistrationsControllerTest < ActionDispatch::Integration
105
103
  assert response.headers['uid']
106
104
  end
107
105
 
106
+ describe 'using auth cookie' do
107
+ before do
108
+ DeviseTokenAuth.cookie_enabled = true
109
+ end
110
+
111
+ test 'auth cookie was returned in response' do
112
+ post '/auth', params: mock_registration_params
113
+ assert response.cookies[DeviseTokenAuth.cookie_name]
114
+ end
115
+
116
+ after do
117
+ DeviseTokenAuth.cookie_enabled = false
118
+ end
119
+ end
120
+
108
121
  after do
109
122
  Devise.allow_unconfirmed_access_for = @original_duration
110
123
  end