devise_token_auth 1.0.0 → 1.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (134) hide show
  1. checksums.yaml +5 -5
  2. data/README.md +6 -3
  3. data/app/controllers/devise_token_auth/application_controller.rb +23 -3
  4. data/app/controllers/devise_token_auth/concerns/resource_finder.rb +24 -11
  5. data/app/controllers/devise_token_auth/concerns/set_user_by_token.rb +78 -57
  6. data/app/controllers/devise_token_auth/confirmations_controller.rb +69 -19
  7. data/app/controllers/devise_token_auth/omniauth_callbacks_controller.rb +89 -44
  8. data/app/controllers/devise_token_auth/passwords_controller.rb +55 -31
  9. data/app/controllers/devise_token_auth/registrations_controller.rb +33 -40
  10. data/app/controllers/devise_token_auth/sessions_controller.rb +36 -14
  11. data/app/controllers/devise_token_auth/unlocks_controller.rb +12 -7
  12. data/app/models/devise_token_auth/concerns/active_record_support.rb +14 -0
  13. data/app/models/devise_token_auth/concerns/confirmable_support.rb +28 -0
  14. data/app/models/devise_token_auth/concerns/mongoid_support.rb +19 -0
  15. data/app/models/devise_token_auth/concerns/tokens_serialization.rb +31 -0
  16. data/app/models/devise_token_auth/concerns/user.rb +79 -80
  17. data/app/models/devise_token_auth/concerns/user_omniauth_callbacks.rb +12 -5
  18. data/app/validators/{email_validator.rb → devise_token_auth_email_validator.rb} +11 -3
  19. data/app/views/devise_token_auth/omniauth_external_window.html.erb +1 -1
  20. data/config/locales/da-DK.yml +2 -0
  21. data/config/locales/de.yml +2 -0
  22. data/config/locales/en.yml +10 -0
  23. data/config/locales/es.yml +2 -0
  24. data/config/locales/fr.yml +2 -0
  25. data/config/locales/he.yml +52 -0
  26. data/config/locales/it.yml +2 -0
  27. data/config/locales/ja.yml +16 -2
  28. data/config/locales/ko.yml +51 -0
  29. data/config/locales/nl.yml +2 -0
  30. data/config/locales/pl.yml +6 -3
  31. data/config/locales/pt-BR.yml +2 -0
  32. data/config/locales/pt.yml +6 -3
  33. data/config/locales/ro.yml +2 -0
  34. data/config/locales/ru.yml +2 -0
  35. data/config/locales/sq.yml +2 -0
  36. data/config/locales/sv.yml +2 -0
  37. data/config/locales/uk.yml +2 -0
  38. data/config/locales/vi.yml +2 -0
  39. data/config/locales/zh-CN.yml +2 -0
  40. data/config/locales/zh-HK.yml +2 -0
  41. data/config/locales/zh-TW.yml +2 -0
  42. data/lib/devise_token_auth/blacklist.rb +6 -0
  43. data/lib/devise_token_auth/controllers/helpers.rb +5 -9
  44. data/lib/devise_token_auth/engine.rb +17 -2
  45. data/lib/devise_token_auth/rails/routes.rb +22 -16
  46. data/lib/devise_token_auth/token_factory.rb +126 -0
  47. data/lib/devise_token_auth/url.rb +3 -0
  48. data/lib/devise_token_auth/version.rb +1 -1
  49. data/lib/devise_token_auth.rb +6 -3
  50. data/lib/generators/devise_token_auth/USAGE +1 -1
  51. data/lib/generators/devise_token_auth/install_generator.rb +7 -91
  52. data/lib/generators/devise_token_auth/install_generator_helpers.rb +98 -0
  53. data/lib/generators/devise_token_auth/install_mongoid_generator.rb +46 -0
  54. data/lib/generators/devise_token_auth/templates/devise_token_auth.rb +21 -5
  55. data/lib/generators/devise_token_auth/templates/devise_token_auth_create_users.rb.erb +1 -8
  56. data/lib/generators/devise_token_auth/templates/user.rb.erb +2 -2
  57. data/lib/generators/devise_token_auth/templates/user_mongoid.rb.erb +56 -0
  58. data/test/controllers/custom/custom_confirmations_controller_test.rb +2 -2
  59. data/test/controllers/custom/custom_omniauth_callbacks_controller_test.rb +1 -1
  60. data/test/controllers/demo_mang_controller_test.rb +37 -8
  61. data/test/controllers/demo_user_controller_test.rb +39 -10
  62. data/test/controllers/devise_token_auth/confirmations_controller_test.rb +170 -22
  63. data/test/controllers/devise_token_auth/omniauth_callbacks_controller_test.rb +117 -53
  64. data/test/controllers/devise_token_auth/passwords_controller_test.rb +299 -122
  65. data/test/controllers/devise_token_auth/registrations_controller_test.rb +56 -16
  66. data/test/controllers/devise_token_auth/sessions_controller_test.rb +139 -75
  67. data/test/controllers/devise_token_auth/token_validations_controller_test.rb +43 -2
  68. data/test/controllers/devise_token_auth/unlocks_controller_test.rb +44 -5
  69. data/test/controllers/overrides/confirmations_controller_test.rb +1 -1
  70. data/test/controllers/overrides/omniauth_callbacks_controller_test.rb +1 -1
  71. data/test/dummy/app/active_record/confirmable_user.rb +11 -0
  72. data/test/dummy/app/{models → active_record}/scoped_user.rb +2 -2
  73. data/test/dummy/app/{models → active_record}/unconfirmable_user.rb +1 -2
  74. data/test/dummy/app/{models → active_record}/unregisterable_user.rb +3 -3
  75. data/test/dummy/app/active_record/user.rb +6 -0
  76. data/test/dummy/app/controllers/application_controller.rb +2 -6
  77. data/test/dummy/app/controllers/overrides/confirmations_controller.rb +5 -4
  78. data/test/dummy/app/controllers/overrides/passwords_controller.rb +5 -4
  79. data/test/dummy/app/controllers/overrides/registrations_controller.rb +1 -1
  80. data/test/dummy/app/controllers/overrides/sessions_controller.rb +2 -2
  81. data/test/dummy/app/models/{user.rb → concerns/favorite_color.rb} +7 -8
  82. data/test/dummy/app/mongoid/confirmable_user.rb +52 -0
  83. data/test/dummy/app/mongoid/lockable_user.rb +38 -0
  84. data/test/dummy/app/mongoid/mang.rb +46 -0
  85. data/test/dummy/app/mongoid/only_email_user.rb +33 -0
  86. data/test/dummy/app/mongoid/scoped_user.rb +50 -0
  87. data/test/dummy/app/mongoid/unconfirmable_user.rb +44 -0
  88. data/test/dummy/app/mongoid/unregisterable_user.rb +47 -0
  89. data/test/dummy/app/mongoid/user.rb +49 -0
  90. data/test/dummy/app/views/layouts/application.html.erb +0 -2
  91. data/test/dummy/config/application.rb +22 -1
  92. data/test/dummy/config/boot.rb +4 -0
  93. data/test/dummy/config/environments/development.rb +0 -10
  94. data/test/dummy/config/environments/production.rb +0 -16
  95. data/test/dummy/config/environments/test.rb +6 -2
  96. data/test/dummy/config/initializers/devise.rb +285 -0
  97. data/test/dummy/config/initializers/devise_token_auth.rb +35 -4
  98. data/test/dummy/config/initializers/figaro.rb +1 -1
  99. data/test/dummy/config/initializers/omniauth.rb +1 -0
  100. data/test/dummy/config/routes.rb +2 -0
  101. data/test/dummy/db/migrate/20140715061447_devise_token_auth_create_users.rb +0 -7
  102. data/test/dummy/db/migrate/20140715061805_devise_token_auth_create_mangs.rb +0 -7
  103. data/test/dummy/db/migrate/20141222035835_devise_token_auth_create_only_email_users.rb +0 -7
  104. data/test/dummy/db/migrate/20141222053502_devise_token_auth_create_unregisterable_users.rb +0 -7
  105. data/test/dummy/db/migrate/20150708104536_devise_token_auth_create_unconfirmable_users.rb +0 -7
  106. data/test/dummy/db/migrate/20160103235141_devise_token_auth_create_scoped_users.rb +0 -7
  107. data/test/dummy/db/migrate/20160629184441_devise_token_auth_create_lockable_users.rb +0 -7
  108. data/test/dummy/db/migrate/20190924101113_devise_token_auth_create_confirmable_users.rb +49 -0
  109. data/test/dummy/db/schema.rb +31 -33
  110. data/test/dummy/tmp/generators/app/models/user.rb +9 -0
  111. data/test/dummy/tmp/generators/config/initializers/devise_token_auth.rb +66 -0
  112. data/test/dummy/tmp/generators/db/migrate/20230415183419_devise_token_auth_create_users.rb +49 -0
  113. data/test/factories/users.rb +3 -2
  114. data/test/lib/devise_token_auth/blacklist_test.rb +19 -0
  115. data/test/lib/devise_token_auth/rails/custom_routes_test.rb +29 -0
  116. data/test/lib/devise_token_auth/rails/routes_test.rb +87 -0
  117. data/test/lib/devise_token_auth/token_factory_test.rb +191 -0
  118. data/test/lib/devise_token_auth/url_test.rb +2 -2
  119. data/test/lib/generators/devise_token_auth/install_generator_test.rb +51 -31
  120. data/test/lib/generators/devise_token_auth/install_generator_with_namespace_test.rb +51 -31
  121. data/test/models/concerns/mongoid_support_test.rb +31 -0
  122. data/test/models/concerns/tokens_serialization_test.rb +104 -0
  123. data/test/models/confirmable_user_test.rb +35 -0
  124. data/test/models/only_email_user_test.rb +0 -8
  125. data/test/models/user_test.rb +13 -23
  126. data/test/test_helper.rb +45 -4
  127. metadata +190 -97
  128. data/config/initializers/devise.rb +0 -198
  129. data/test/dummy/config/initializers/assets.rb +0 -10
  130. data/test/dummy/tmp/generators/app/views/devise/mailer/confirmation_instructions.html.erb +0 -5
  131. data/test/dummy/tmp/generators/app/views/devise/mailer/reset_password_instructions.html.erb +0 -8
  132. /data/test/dummy/app/{models → active_record}/lockable_user.rb +0 -0
  133. /data/test/dummy/app/{models → active_record}/mang.rb +0 -0
  134. /data/test/dummy/app/{models → active_record}/only_email_user.rb +0 -0
@@ -11,7 +11,7 @@ require 'test_helper'
11
11
  class DeviseTokenAuth::ConfirmationsControllerTest < ActionController::TestCase
12
12
  describe DeviseTokenAuth::ConfirmationsController do
13
13
  def token_and_client_config_from(body)
14
- token = body.match(/confirmation_token=([^&]*)&/)[1]
14
+ token = body.match(/confirmation_token=([^&]*)[&"]/)[1]
15
15
  client_config = body.match(/config=([^&]*)&/)[1]
16
16
  [token, client_config]
17
17
  end
@@ -23,6 +23,7 @@ class DeviseTokenAuth::ConfirmationsControllerTest < ActionController::TestCase
23
23
  @new_user.send_confirmation_instructions(redirect_url: @redirect_url)
24
24
  mail = ActionMailer::Base.deliveries.last
25
25
  @token, @client_config = token_and_client_config_from(mail.body)
26
+ @token_params = %w[access-token client client_id config expiry token uid]
26
27
  end
27
28
 
28
29
  test 'should generate raw token' do
@@ -38,43 +39,190 @@ class DeviseTokenAuth::ConfirmationsControllerTest < ActionController::TestCase
38
39
  end
39
40
 
40
41
  describe 'success' do
41
- before do
42
- get :show,
43
- params: { confirmation_token: @token,
44
- redirect_url: @redirect_url },
45
- xhr: true
46
- @resource = assigns(:resource)
47
- end
42
+ describe 'when authenticated' do
43
+ before do
44
+ sign_in(@new_user)
45
+ get :show,
46
+ params: { confirmation_token: @token,
47
+ redirect_url: @redirect_url },
48
+ xhr: true
49
+ @resource = assigns(:resource)
50
+ end
48
51
 
49
- test 'user should now be confirmed' do
50
- assert @resource.confirmed?
51
- end
52
+ test 'user should now be confirmed' do
53
+ assert @resource.confirmed?
54
+ end
52
55
 
53
- test 'should redirect to success url' do
54
- assert_redirected_to(/^#{@redirect_url}/)
56
+ test 'should save the authentication token' do
57
+ assert @resource.reload.tokens.present?
58
+ end
59
+
60
+ test 'should redirect to success url' do
61
+ assert_redirected_to(/^#{@redirect_url}/)
62
+ end
63
+
64
+ test 'redirect url includes token params' do
65
+ assert @token_params.all? { |param| response.body.include?(param) }
66
+ assert response.body.include?('account_confirmation_success')
67
+ end
55
68
  end
56
69
 
57
- test 'the sign_in_count should be 1' do
58
- assert @resource.sign_in_count == 1
70
+ describe 'when unauthenticated' do
71
+ before do
72
+ sign_out(@new_user)
73
+ get :show,
74
+ params: { confirmation_token: @token,
75
+ redirect_url: @redirect_url },
76
+ xhr: true
77
+ @resource = assigns(:resource)
78
+ end
79
+
80
+ test 'user should now be confirmed' do
81
+ assert @resource.confirmed?
82
+ end
83
+
84
+ test 'should redirect to success url' do
85
+ assert_redirected_to(/^#{@redirect_url}/)
86
+ end
87
+
88
+ test 'redirect url does not include token params' do
89
+ refute @token_params.any? { |param| response.body.include?(param) }
90
+ assert response.body.include?('account_confirmation_success')
91
+ end
59
92
  end
60
93
 
61
- test 'User shoud have the signed in info filled' do
62
- assert @resource.current_sign_in_at?
94
+ describe 'resend confirmation' do
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
140
+ end
63
141
  end
64
142
 
65
- test 'User shoud have the Last checkin filled' do
66
- assert @resource.last_sign_in_at?
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
178
+ end
179
+
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
199
+ end
67
200
  end
68
201
  end
69
202
 
70
203
  describe 'failure' do
71
204
  test 'user should not be confirmed' do
72
- assert_raises(ActionController::RoutingError) do
73
- get :show, params: { confirmation_token: 'bogus' }
74
- end
205
+ get :show,
206
+ params: { confirmation_token: 'bogus',
207
+ redirect_url: @redirect_url }
208
+
209
+ assert_redirected_to(/^#{@redirect_url}/)
210
+
75
211
  @resource = assigns(:resource)
76
212
  refute @resource.confirmed?
77
213
  end
214
+
215
+ test 'request resend confirmation without email' do
216
+ post :create, params: { email: nil }, xhr: true
217
+
218
+ assert_equal 401, response.status
219
+ end
220
+
221
+ test 'user should not be found on resend confirmation request' do
222
+ post :create, params: { email: 'bogus' }, xhr: true
223
+
224
+ assert_equal 404, response.status
225
+ end
78
226
  end
79
227
  end
80
228
 
@@ -13,12 +13,12 @@ class OmniauthTest < ActionDispatch::IntegrationTest
13
13
  end
14
14
 
15
15
  before do
16
- @redirect_url = 'http://ng-token-auth.dev/'
16
+ @redirect_url = 'https://ng-token-auth.dev/'
17
17
  end
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
@@ -98,7 +98,7 @@ class OmniauthTest < ActionDispatch::IntegrationTest
98
98
 
99
99
  describe 'with alternate user model' do
100
100
  before do
101
- get '/mangs/facebook',
101
+ post '/mangs/facebook',
102
102
  params: {
103
103
  auth_origin_url: @redirect_url,
104
104
  omniauth_window_type: 'newWindow'
@@ -123,7 +123,7 @@ class OmniauthTest < ActionDispatch::IntegrationTest
123
123
  before do
124
124
  @fav_color = 'alizarin crimson'
125
125
  @unpermitted_param = 'M. Bison'
126
- get '/auth/facebook',
126
+ post '/auth/facebook',
127
127
  params: { auth_origin_url: @redirect_url,
128
128
  favorite_color: @fav_color,
129
129
  name: @unpermitted_param,
@@ -155,10 +155,12 @@ class OmniauthTest < ActionDispatch::IntegrationTest
155
155
  describe 'with new user' do
156
156
  before do
157
157
  User.any_instance.expects(:new_record?).returns(true).at_least_once
158
+ # https://docs.mongodb.com/mongoid/master/tutorials/mongoid-documents/#notes-on-persistence
159
+ User.any_instance.expects(:save!).returns(true)
158
160
  end
159
161
 
160
162
  test 'response contains oauth_registration attr' do
161
- get '/auth/facebook',
163
+ post '/auth/facebook',
162
164
  params: { auth_origin_url: @redirect_url,
163
165
  omniauth_window_type: 'newWindow' }
164
166
 
@@ -174,7 +176,7 @@ class OmniauthTest < ActionDispatch::IntegrationTest
174
176
  end
175
177
 
176
178
  test 'response does not contain oauth_registration attr' do
177
- get '/auth/facebook',
179
+ post '/auth/facebook',
178
180
  params: { auth_origin_url: @redirect_url,
179
181
  omniauth_window_type: 'newWindow' }
180
182
 
@@ -187,7 +189,7 @@ class OmniauthTest < ActionDispatch::IntegrationTest
187
189
 
188
190
  describe 'using namespaces' do
189
191
  before do
190
- get '/api/v1/auth/facebook',
192
+ post '/api/v1/auth/facebook',
191
193
  params: { auth_origin_url: @redirect_url,
192
194
  omniauth_window_type: 'newWindow' }
193
195
 
@@ -232,7 +234,7 @@ class OmniauthTest < ActionDispatch::IntegrationTest
232
234
 
233
235
  describe 'with omniauth_window_type=sameWindow' do
234
236
  test 'redirects to auth_origin_url with all expected query params' do
235
- get '/auth/facebook',
237
+ post '/auth/facebook',
236
238
  params: { auth_origin_url: '/auth_origin',
237
239
  omniauth_window_type: 'sameWindow' }
238
240
 
@@ -256,7 +258,7 @@ class OmniauthTest < ActionDispatch::IntegrationTest
256
258
  end
257
259
 
258
260
  def get_success(params = {})
259
- get '/auth/facebook',
261
+ post '/auth/facebook',
260
262
  params: {
261
263
  auth_origin_url: @redirect_url,
262
264
  omniauth_window_type: 'newWindow'
@@ -280,7 +282,7 @@ class OmniauthTest < ActionDispatch::IntegrationTest
280
282
 
281
283
  test 'renders expected data' do
282
284
  silence_omniauth do
283
- get '/auth/facebook',
285
+ post '/auth/facebook',
284
286
  params: { auth_origin_url: @redirect_url,
285
287
  omniauth_window_type: 'newWindow' }
286
288
 
@@ -296,7 +298,7 @@ class OmniauthTest < ActionDispatch::IntegrationTest
296
298
 
297
299
  test 'renders something with no auth_origin_url' do
298
300
  silence_omniauth do
299
- get '/auth/facebook'
301
+ post '/auth/facebook'
300
302
  follow_all_redirects!
301
303
  end
302
304
  assert_equal 200, response.status
@@ -315,60 +317,122 @@ class OmniauthTest < ActionDispatch::IntegrationTest
315
317
  end
316
318
 
317
319
  describe 'Using redirect_whitelist' do
318
- before do
319
- @user_email = 'slemp.diggler@sillybandz.gov'
320
- OmniAuth.config.mock_auth[:facebook] = OmniAuth::AuthHash.new(
321
- provider: 'facebook',
322
- uid: '123545',
323
- info: {
324
- name: 'chong',
325
- email: @user_email
326
- }
327
- )
328
- @good_redirect_url = Faker::Internet.url
329
- @bad_redirect_url = Faker::Internet.url
330
- DeviseTokenAuth.redirect_whitelist = [@good_redirect_url]
331
- end
332
320
 
333
- teardown do
334
- DeviseTokenAuth.redirect_whitelist = nil
335
- end
321
+ describe "newWindow" do
322
+ before do
323
+ @user_email = 'slemp.diggler@sillybandz.gov'
324
+ OmniAuth.config.mock_auth[:facebook] = OmniAuth::AuthHash.new(
325
+ provider: 'facebook',
326
+ uid: '123545',
327
+ info: {
328
+ name: 'chong',
329
+ email: @user_email
330
+ }
331
+ )
332
+ @good_redirect_url = Faker::Internet.url
333
+ @bad_redirect_url = Faker::Internet.url
334
+ DeviseTokenAuth.redirect_whitelist = [@good_redirect_url]
335
+ end
336
336
 
337
- test 'request using non-whitelisted redirect fail' do
338
- get '/auth/facebook',
339
- params: { auth_origin_url: @bad_redirect_url,
340
- omniauth_window_type: 'newWindow' }
337
+ teardown do
338
+ DeviseTokenAuth.redirect_whitelist = nil
339
+ end
341
340
 
342
- follow_all_redirects!
341
+ test 'request using non-whitelisted redirect fail' do
342
+ post '/auth/facebook',
343
+ params: { auth_origin_url: @bad_redirect_url,
344
+ omniauth_window_type: 'newWindow' }
343
345
 
344
- data = get_parsed_data_json
345
- assert_equal "Redirect to &#39;#{@bad_redirect_url}&#39; not allowed.",
346
- data['error']
346
+ follow_all_redirects!
347
+
348
+ data = get_parsed_data_json
349
+ assert_equal "Redirect to '#{@bad_redirect_url}' not allowed.",
350
+ data['error']
351
+ end
352
+
353
+ test 'request to whitelisted redirect should succeed' do
354
+ post '/auth/facebook',
355
+ params: {
356
+ auth_origin_url: @good_redirect_url,
357
+ omniauth_window_type: 'newWindow'
358
+ }
359
+
360
+ follow_all_redirects!
361
+
362
+ data = get_parsed_data_json
363
+ assert_equal @user_email, data['email']
364
+ end
365
+
366
+ test 'should support wildcards' do
367
+ DeviseTokenAuth.redirect_whitelist = ["#{@good_redirect_url[0..8]}*"]
368
+ post '/auth/facebook',
369
+ params: { auth_origin_url: @good_redirect_url,
370
+ omniauth_window_type: 'newWindow' }
371
+
372
+ follow_all_redirects!
373
+
374
+ data = get_parsed_data_json
375
+ assert_equal @user_email, data['email']
376
+ end
347
377
  end
348
378
 
349
- test 'request to whitelisted redirect should succeed' do
350
- get '/auth/facebook',
351
- params: {
352
- auth_origin_url: @good_redirect_url,
353
- omniauth_window_type: 'newWindow'
379
+ describe "sameWindow" do
380
+ before do
381
+ @user_email = 'slemp.diggler@sillybandz.gov'
382
+ OmniAuth.config.mock_auth[:facebook] = OmniAuth::AuthHash.new(
383
+ provider: 'facebook',
384
+ uid: '123545',
385
+ info: {
386
+ name: 'chong',
387
+ email: @user_email
354
388
  }
389
+ )
390
+ @good_redirect_url = '/auth_origin'
391
+ @bad_redirect_url = Faker::Internet.url
392
+ DeviseTokenAuth.redirect_whitelist = [@good_redirect_url]
393
+ end
355
394
 
356
- follow_all_redirects!
395
+ teardown do
396
+ DeviseTokenAuth.redirect_whitelist = nil
397
+ end
357
398
 
358
- data = get_parsed_data_json
359
- assert_equal @user_email, data['email']
360
- end
399
+ test 'request using non-whitelisted redirect fail' do
400
+ post '/auth/facebook',
401
+ params: { auth_origin_url: @bad_redirect_url,
402
+ omniauth_window_type: 'sameWindow' }
361
403
 
362
- test 'should support wildcards' do
363
- DeviseTokenAuth.redirect_whitelist = ["#{@good_redirect_url[0..8]}*"]
364
- get '/auth/facebook',
365
- params: { auth_origin_url: @good_redirect_url,
366
- omniauth_window_type: 'newWindow' }
404
+ follow_all_redirects!
367
405
 
368
- follow_all_redirects!
406
+ assert_equal 200, response.status
407
+ assert_equal true, response.body.include?("Redirect to '#{@bad_redirect_url}' not allowed")
408
+ end
369
409
 
370
- data = get_parsed_data_json
371
- assert_equal @user_email, data['email']
410
+ test 'request to whitelisted redirect should succeed' do
411
+ post '/auth/facebook',
412
+ params: {
413
+ auth_origin_url: '/auth_origin',
414
+ omniauth_window_type: 'sameWindow'
415
+ }
416
+
417
+ follow_all_redirects!
418
+
419
+ assert_equal 200, response.status
420
+ assert_equal false, response.body.include?("Redirect to '#{@good_redirect_url}' not allowed")
421
+ end
422
+
423
+ test 'should support wildcards' do
424
+ DeviseTokenAuth.redirect_whitelist = ["#{@good_redirect_url[0..8]}*"]
425
+ post '/auth/facebook',
426
+ params: {
427
+ auth_origin_url: '/auth_origin',
428
+ omniauth_window_type: 'sameWindow'
429
+ }
430
+
431
+ follow_all_redirects!
432
+
433
+ assert_equal 200, response.status
434
+ assert_equal false, response.body.include?("Redirect to '#{@good_redirect_url}' not allowed")
435
+ end
372
436
  end
373
437
  end
374
438
  end