devise_jwt_auth 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (179) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +13 -0
  3. data/README.md +99 -0
  4. data/Rakefile +42 -0
  5. data/app/controllers/devise_jwt_auth/application_controller.rb +80 -0
  6. data/app/controllers/devise_jwt_auth/concerns/resource_finder.rb +44 -0
  7. data/app/controllers/devise_jwt_auth/concerns/set_user_by_jwt_token.rb +111 -0
  8. data/app/controllers/devise_jwt_auth/confirmations_controller.rb +88 -0
  9. data/app/controllers/devise_jwt_auth/omniauth_callbacks_controller.rb +291 -0
  10. data/app/controllers/devise_jwt_auth/passwords_controller.rb +217 -0
  11. data/app/controllers/devise_jwt_auth/refresh_token_controller.rb +41 -0
  12. data/app/controllers/devise_jwt_auth/registrations_controller.rb +203 -0
  13. data/app/controllers/devise_jwt_auth/sessions_controller.rb +131 -0
  14. data/app/controllers/devise_jwt_auth/unlocks_controller.rb +99 -0
  15. data/app/models/devise_jwt_auth/concerns/active_record_support.rb +16 -0
  16. data/app/models/devise_jwt_auth/concerns/confirmable_support.rb +27 -0
  17. data/app/models/devise_jwt_auth/concerns/mongoid_support.rb +19 -0
  18. data/app/models/devise_jwt_auth/concerns/tokens_serialization.rb +19 -0
  19. data/app/models/devise_jwt_auth/concerns/user.rb +117 -0
  20. data/app/models/devise_jwt_auth/concerns/user_omniauth_callbacks.rb +28 -0
  21. data/app/validators/devise_jwt_auth_email_validator.rb +23 -0
  22. data/app/views/devise/mailer/confirmation_instructions.html.erb +5 -0
  23. data/app/views/devise/mailer/reset_password_instructions.html.erb +8 -0
  24. data/app/views/devise/mailer/unlock_instructions.html.erb +7 -0
  25. data/app/views/devise_jwt_auth/omniauth_external_window.html.erb +38 -0
  26. data/config/locales/da-DK.yml +52 -0
  27. data/config/locales/de.yml +51 -0
  28. data/config/locales/en.yml +57 -0
  29. data/config/locales/es.yml +51 -0
  30. data/config/locales/fr.yml +51 -0
  31. data/config/locales/he.yml +52 -0
  32. data/config/locales/it.yml +48 -0
  33. data/config/locales/ja.yml +48 -0
  34. data/config/locales/ko.yml +51 -0
  35. data/config/locales/nl.yml +32 -0
  36. data/config/locales/pl.yml +50 -0
  37. data/config/locales/pt-BR.yml +48 -0
  38. data/config/locales/pt.yml +50 -0
  39. data/config/locales/ro.yml +48 -0
  40. data/config/locales/ru.yml +52 -0
  41. data/config/locales/sq.yml +48 -0
  42. data/config/locales/sv.yml +52 -0
  43. data/config/locales/uk.yml +61 -0
  44. data/config/locales/vi.yml +52 -0
  45. data/config/locales/zh-CN.yml +48 -0
  46. data/config/locales/zh-HK.yml +50 -0
  47. data/config/locales/zh-TW.yml +50 -0
  48. data/lib/devise_jwt_auth.rb +14 -0
  49. data/lib/devise_jwt_auth/blacklist.rb +2 -0
  50. data/lib/devise_jwt_auth/controllers/helpers.rb +161 -0
  51. data/lib/devise_jwt_auth/controllers/url_helpers.rb +10 -0
  52. data/lib/devise_jwt_auth/engine.rb +96 -0
  53. data/lib/devise_jwt_auth/errors.rb +8 -0
  54. data/lib/devise_jwt_auth/rails/routes.rb +118 -0
  55. data/lib/devise_jwt_auth/token_factory.rb +51 -0
  56. data/lib/devise_jwt_auth/url.rb +44 -0
  57. data/lib/devise_jwt_auth/version.rb +5 -0
  58. data/lib/generators/devise_jwt_auth/USAGE +31 -0
  59. data/lib/generators/devise_jwt_auth/install_generator.rb +91 -0
  60. data/lib/generators/devise_jwt_auth/install_generator_helpers.rb +98 -0
  61. data/lib/generators/devise_jwt_auth/install_mongoid_generator.rb +46 -0
  62. data/lib/generators/devise_jwt_auth/install_views_generator.rb +18 -0
  63. data/lib/generators/devise_jwt_auth/templates/devise_jwt_auth.rb +74 -0
  64. data/lib/generators/devise_jwt_auth/templates/devise_jwt_auth_create_users.rb.erb +51 -0
  65. data/lib/generators/devise_jwt_auth/templates/user.rb.erb +9 -0
  66. data/lib/generators/devise_jwt_auth/templates/user_mongoid.rb.erb +56 -0
  67. data/lib/tasks/devise_token_auth_tasks.rake +6 -0
  68. data/test/controllers/custom/custom_confirmations_controller_test.rb +25 -0
  69. data/test/controllers/custom/custom_omniauth_callbacks_controller_test.rb +33 -0
  70. data/test/controllers/custom/custom_passwords_controller_test.rb +79 -0
  71. data/test/controllers/custom/custom_refresh_token_controller_test.rb +36 -0
  72. data/test/controllers/custom/custom_registrations_controller_test.rb +59 -0
  73. data/test/controllers/custom/custom_sessions_controller_test.rb +39 -0
  74. data/test/controllers/demo_group_controller_test.rb +150 -0
  75. data/test/controllers/demo_mang_controller_test.rb +286 -0
  76. data/test/controllers/demo_user_controller_test.rb +650 -0
  77. data/test/controllers/devise_jwt_auth/confirmations_controller_test.rb +194 -0
  78. data/test/controllers/devise_jwt_auth/omniauth_callbacks_controller_test.rb +462 -0
  79. data/test/controllers/devise_jwt_auth/passwords_controller_test.rb +881 -0
  80. data/test/controllers/devise_jwt_auth/refresh_token_controller_test.rb +84 -0
  81. data/test/controllers/devise_jwt_auth/registrations_controller_test.rb +944 -0
  82. data/test/controllers/devise_jwt_auth/sessions_controller_test.rb +510 -0
  83. data/test/controllers/devise_jwt_auth/unlocks_controller_test.rb +197 -0
  84. data/test/controllers/overrides/confirmations_controller_test.rb +47 -0
  85. data/test/controllers/overrides/omniauth_callbacks_controller_test.rb +53 -0
  86. data/test/controllers/overrides/passwords_controller_test.rb +65 -0
  87. data/test/controllers/overrides/refresh_token_controller_test.rb +37 -0
  88. data/test/controllers/overrides/registrations_controller_test.rb +47 -0
  89. data/test/controllers/overrides/sessions_controller_test.rb +35 -0
  90. data/test/dummy/README.rdoc +28 -0
  91. data/test/dummy/app/active_record/confirmable_user.rb +11 -0
  92. data/test/dummy/app/active_record/lockable_user.rb +7 -0
  93. data/test/dummy/app/active_record/mang.rb +5 -0
  94. data/test/dummy/app/active_record/only_email_user.rb +7 -0
  95. data/test/dummy/app/active_record/scoped_user.rb +9 -0
  96. data/test/dummy/app/active_record/unconfirmable_user.rb +9 -0
  97. data/test/dummy/app/active_record/unregisterable_user.rb +9 -0
  98. data/test/dummy/app/active_record/user.rb +6 -0
  99. data/test/dummy/app/controllers/application_controller.rb +18 -0
  100. data/test/dummy/app/controllers/auth_origin_controller.rb +7 -0
  101. data/test/dummy/app/controllers/custom/confirmations_controller.rb +13 -0
  102. data/test/dummy/app/controllers/custom/omniauth_callbacks_controller.rb +13 -0
  103. data/test/dummy/app/controllers/custom/passwords_controller.rb +39 -0
  104. data/test/dummy/app/controllers/custom/refresh_token_controller.rb +20 -0
  105. data/test/dummy/app/controllers/custom/registrations_controller.rb +39 -0
  106. data/test/dummy/app/controllers/custom/sessions_controller.rb +29 -0
  107. data/test/dummy/app/controllers/demo_group_controller.rb +15 -0
  108. data/test/dummy/app/controllers/demo_mang_controller.rb +14 -0
  109. data/test/dummy/app/controllers/demo_user_controller.rb +27 -0
  110. data/test/dummy/app/controllers/overrides/confirmations_controller.rb +37 -0
  111. data/test/dummy/app/controllers/overrides/omniauth_callbacks_controller.rb +16 -0
  112. data/test/dummy/app/controllers/overrides/passwords_controller.rb +45 -0
  113. data/test/dummy/app/controllers/overrides/refresh_token_controller.rb +22 -0
  114. data/test/dummy/app/controllers/overrides/registrations_controller.rb +29 -0
  115. data/test/dummy/app/controllers/overrides/sessions_controller.rb +36 -0
  116. data/test/dummy/app/helpers/application_helper.rb +1058 -0
  117. data/test/dummy/app/models/concerns/favorite_color.rb +19 -0
  118. data/test/dummy/app/mongoid/confirmable_user.rb +52 -0
  119. data/test/dummy/app/mongoid/lockable_user.rb +38 -0
  120. data/test/dummy/app/mongoid/mang.rb +46 -0
  121. data/test/dummy/app/mongoid/only_email_user.rb +33 -0
  122. data/test/dummy/app/mongoid/scoped_user.rb +50 -0
  123. data/test/dummy/app/mongoid/unconfirmable_user.rb +44 -0
  124. data/test/dummy/app/mongoid/unregisterable_user.rb +47 -0
  125. data/test/dummy/app/mongoid/user.rb +49 -0
  126. data/test/dummy/app/views/layouts/application.html.erb +14 -0
  127. data/test/dummy/config.ru +18 -0
  128. data/test/dummy/config/application.rb +48 -0
  129. data/test/dummy/config/application.yml.bk +0 -0
  130. data/test/dummy/config/boot.rb +11 -0
  131. data/test/dummy/config/environment.rb +7 -0
  132. data/test/dummy/config/environments/development.rb +46 -0
  133. data/test/dummy/config/environments/production.rb +84 -0
  134. data/test/dummy/config/environments/test.rb +50 -0
  135. data/test/dummy/config/initializers/assets.rb +10 -0
  136. data/test/dummy/config/initializers/backtrace_silencers.rb +9 -0
  137. data/test/dummy/config/initializers/cookies_serializer.rb +5 -0
  138. data/test/dummy/config/initializers/devise.rb +290 -0
  139. data/test/dummy/config/initializers/devise_jwt_auth.rb +55 -0
  140. data/test/dummy/config/initializers/figaro.rb +3 -0
  141. data/test/dummy/config/initializers/filter_parameter_logging.rb +6 -0
  142. data/test/dummy/config/initializers/inflections.rb +18 -0
  143. data/test/dummy/config/initializers/mime_types.rb +6 -0
  144. data/test/dummy/config/initializers/omniauth.rb +11 -0
  145. data/test/dummy/config/initializers/session_store.rb +5 -0
  146. data/test/dummy/config/initializers/wrap_parameters.rb +16 -0
  147. data/test/dummy/config/routes.rb +57 -0
  148. data/test/dummy/config/spring.rb +3 -0
  149. data/test/dummy/db/migrate/20140715061447_devise_token_auth_create_users.rb +58 -0
  150. data/test/dummy/db/migrate/20140715061805_devise_token_auth_create_mangs.rb +57 -0
  151. data/test/dummy/db/migrate/20140829044006_add_operating_thetan_to_user.rb +8 -0
  152. data/test/dummy/db/migrate/20140916224624_add_favorite_color_to_mangs.rb +7 -0
  153. data/test/dummy/db/migrate/20141222035835_devise_token_auth_create_only_email_users.rb +55 -0
  154. data/test/dummy/db/migrate/20141222053502_devise_token_auth_create_unregisterable_users.rb +56 -0
  155. data/test/dummy/db/migrate/20150708104536_devise_token_auth_create_unconfirmable_users.rb +56 -0
  156. data/test/dummy/db/migrate/20160103235141_devise_token_auth_create_scoped_users.rb +56 -0
  157. data/test/dummy/db/migrate/20160629184441_devise_token_auth_create_lockable_users.rb +56 -0
  158. data/test/dummy/db/migrate/20190924101113_devise_token_auth_create_confirmable_users.rb +49 -0
  159. data/test/dummy/db/schema.rb +198 -0
  160. data/test/dummy/lib/migration_database_helper.rb +43 -0
  161. data/test/dummy/tmp/generators/app/models/user.rb +9 -0
  162. data/test/dummy/tmp/generators/config/initializers/devise_jwt_auth.rb +74 -0
  163. data/test/dummy/tmp/generators/config/routes.rb +4 -0
  164. data/test/dummy/tmp/generators/db/migrate/20200206224309_devise_jwt_auth_create_users.rb +51 -0
  165. data/test/factories/users.rb +41 -0
  166. data/test/lib/devise_jwt_auth/blacklist_test.rb +11 -0
  167. data/test/lib/devise_jwt_auth/token_factory_test.rb +115 -0
  168. data/test/lib/devise_jwt_auth/url_test.rb +26 -0
  169. data/test/lib/generators/devise_jwt_auth/install_generator_test.rb +219 -0
  170. data/test/lib/generators/devise_jwt_auth/install_generator_with_namespace_test.rb +224 -0
  171. data/test/lib/generators/devise_jwt_auth/install_views_generator_test.rb +25 -0
  172. data/test/models/concerns/mongoid_support_test.rb +31 -0
  173. data/test/models/concerns/tokens_serialization_test.rb +72 -0
  174. data/test/models/confirmable_user_test.rb +35 -0
  175. data/test/models/only_email_user_test.rb +29 -0
  176. data/test/models/user_test.rb +110 -0
  177. data/test/support/controllers/routes.rb +43 -0
  178. data/test/test_helper.rb +91 -0
  179. metadata +503 -0
@@ -0,0 +1,194 @@
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 DeviseJwtAuth::ConfirmationsControllerTest < ActionController::TestCase
12
+ describe DeviseJwtAuth::ConfirmationsController do
13
+ def token_and_client_config_from(body)
14
+ token = body.match(/confirmation_token=([^&]*)&/)[1]
15
+ client_config = body.match(/config=([^&]*)&/)[1]
16
+ [token, client_config]
17
+ end
18
+
19
+ describe 'Confirmation' do
20
+ before do
21
+ @redirect_url = Faker::Internet.url
22
+ @new_user = create(:user)
23
+ @new_user.send_confirmation_instructions(redirect_url: @redirect_url)
24
+ mail = ActionMailer::Base.deliveries.last
25
+ @token, @client_config = token_and_client_config_from(mail.body)
26
+ @token_params = %w[access-token]
27
+ end
28
+
29
+ test 'should generate raw token' do
30
+ assert @token
31
+ end
32
+
33
+ test "should include config name as 'default' in confirmation link" do
34
+ assert_equal 'default', @client_config
35
+ end
36
+
37
+ test 'should store token hash in user' do
38
+ assert @new_user.confirmation_token
39
+ end
40
+
41
+ describe 'success' do
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
+ # TODO: parse response and look for access and refresh tokens
51
+ end
52
+
53
+ test 'user should now be confirmed' do
54
+ assert @resource.confirmed?
55
+ end
56
+
57
+ test 'should redirect to success url' do
58
+ assert_redirected_to(/^#{@redirect_url}/)
59
+ end
60
+
61
+ test 'redirect url includes token params' do
62
+ assert @token_params.all? { |param| response.body.include?(param) }
63
+ assert response.body.include?('account_confirmation_success')
64
+ end
65
+ end
66
+
67
+ describe 'when unauthenticated' do
68
+ before do
69
+ sign_out(@new_user)
70
+ get :show,
71
+ params: { confirmation_token: @token,
72
+ redirect_url: @redirect_url },
73
+ xhr: true
74
+ @resource = assigns(:resource)
75
+ # TODO: parse resp and look for access and refresh tokens
76
+ end
77
+
78
+ test 'user should now be confirmed' do
79
+ assert @resource.confirmed?
80
+ end
81
+
82
+ test 'should redirect to success url' do
83
+ assert_redirected_to(/^#{@redirect_url}/)
84
+ end
85
+
86
+ test 'redirect url does not include token params' do
87
+ refute @token_params.any? { |param| response.body.include?(param) }
88
+ assert response.body.include?('account_confirmation_success')
89
+ end
90
+ end
91
+
92
+ describe 'resend confirmation' do
93
+ before do
94
+ post :create,
95
+ params: { email: @new_user.email,
96
+ redirect_url: @redirect_url },
97
+ xhr: true
98
+ @resource = assigns(:resource)
99
+
100
+ @mail = ActionMailer::Base.deliveries.last
101
+ @token, @client_config = token_and_client_config_from(@mail.body)
102
+ # TODO: parse response and look for access and refresh tokens.
103
+ end
104
+
105
+ test 'user should not be confirmed' do
106
+ assert_nil @resource.confirmed_at
107
+ end
108
+
109
+ test 'should generate raw token' do
110
+ assert @token
111
+ assert_equal @new_user.confirmation_token, @token
112
+ end
113
+
114
+ test 'user should receive confirmation email' do
115
+ assert_equal @resource.email, @mail['to'].to_s
116
+ end
117
+ end
118
+ end
119
+
120
+ describe 'failure' do
121
+ test 'user should not be confirmed' do
122
+ assert_raises(ActionController::RoutingError) do
123
+ get :show, params: { confirmation_token: 'bogus' }
124
+ end
125
+
126
+ @resource = assigns(:resource)
127
+ refute @resource.confirmed?
128
+ # TODO: parse response for access token and assert that it
129
+ # did not get returned.
130
+ end
131
+
132
+ test 'request resend confirmation without email' do
133
+ post :create, params: { email: nil }, xhr: true
134
+
135
+ assert_equal 401, response.status
136
+ end
137
+
138
+ test 'user should not be found on resend confirmation request' do
139
+ post :create, params: { email: 'bogus' }, xhr: true
140
+
141
+ assert_equal 404, response.status
142
+ end
143
+ end
144
+ end
145
+
146
+ # test with non-standard user class
147
+ describe 'Alternate user model' do
148
+ setup do
149
+ @request.env['devise.mapping'] = Devise.mappings[:mang]
150
+ end
151
+
152
+ teardown do
153
+ @request.env['devise.mapping'] = Devise.mappings[:user]
154
+ end
155
+
156
+ before do
157
+ @config_name = 'altUser'
158
+ @new_user = create(:mang_user)
159
+
160
+ @new_user.send_confirmation_instructions(client_config: @config_name)
161
+
162
+ mail = ActionMailer::Base.deliveries.last
163
+ @token, @client_config = token_and_client_config_from(mail.body)
164
+ end
165
+
166
+ test 'should generate raw token' do
167
+ assert @token
168
+ end
169
+
170
+ test 'should include config name in confirmation link' do
171
+ assert_equal @config_name, @client_config
172
+ end
173
+
174
+ test 'should store token hash in user' do
175
+ assert @new_user.confirmation_token
176
+ end
177
+
178
+ describe 'success' do
179
+ before do
180
+ @redirect_url = Faker::Internet.url
181
+ get :show, params: { confirmation_token: @token,
182
+ redirect_url: @redirect_url }
183
+ @resource = assigns(:resource)
184
+
185
+ # TODO: parse reponse and look for access and refresh tokens
186
+ end
187
+
188
+ test 'user should now be confirmed' do
189
+ assert @resource.confirmed?
190
+ end
191
+ end
192
+ end
193
+ end
194
+ end
@@ -0,0 +1,462 @@
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 OmniauthTest < ActionDispatch::IntegrationTest
12
+ setup do
13
+ OmniAuth.config.test_mode = true
14
+ end
15
+
16
+ before do
17
+ @redirect_url = 'http://ng-token-auth.dev/'
18
+ end
19
+
20
+ def get_parsed_data_json
21
+ encoded_json_data = @response.body.match(/var data \= JSON.parse\(decodeURIComponent\(\'(.+)\'\)\)\;/)[1]
22
+ JSON.parse(URI.unescape(encoded_json_data))
23
+ end
24
+
25
+ describe 'success callback' do
26
+ setup do
27
+ OmniAuth.config.mock_auth[:facebook] = OmniAuth::AuthHash.new(
28
+ provider: 'facebook',
29
+ uid: '123545',
30
+ info: {
31
+ name: 'chong',
32
+ email: 'chongbong@aol.com'
33
+ }
34
+ )
35
+ end
36
+
37
+ test 'request should pass correct redirect_url' do
38
+ get_success
39
+ assert_equal @redirect_url,
40
+ controller.send(:omniauth_params)['auth_origin_url']
41
+ end
42
+
43
+ test 'user should have been created' do
44
+ get_success
45
+ assert @resource
46
+ end
47
+
48
+ test 'user should be assigned info from provider' do
49
+ get_success
50
+ assert_equal 'chongbong@aol.com', @resource.email
51
+ end
52
+
53
+ test 'user should be assigned token' do
54
+ get_success
55
+
56
+ # TODO: Test for an access or refresh token here.
57
+ end
58
+
59
+ test 'session vars have been cleared' do
60
+ get_success
61
+ refute request.session['dta.omniauth.auth']
62
+ refute request.session['dta.omniauth.params']
63
+ end
64
+
65
+ test 'sign_in was called' do
66
+ DeviseJwtAuth::OmniauthCallbacksController.any_instance\
67
+ .expects(:sign_in).with(
68
+ :user, instance_of(User), has_entries(store: false, bypass: false)
69
+ )
70
+ get_success
71
+ end
72
+
73
+ test 'should be redirected via valid url' do
74
+ get_success
75
+ assert_equal 'http://www.example.com/auth/facebook/callback',
76
+ request.original_url
77
+ end
78
+
79
+ describe 'with default user model' do
80
+ before do
81
+ get_success
82
+ end
83
+
84
+ test 'request should determine the correct resource_class' do
85
+ assert_equal 'User', controller.send(:omniauth_params)['resource_class']
86
+ end
87
+
88
+ test 'user should be of the correct class' do
89
+ assert_equal User, @resource.class
90
+ end
91
+ end
92
+
93
+ describe 'with alternate user model' do
94
+ before do
95
+ get '/mangs/facebook',
96
+ params: {
97
+ auth_origin_url: @redirect_url,
98
+ omniauth_window_type: 'newWindow'
99
+ }
100
+
101
+ follow_all_redirects!
102
+
103
+ assert_equal 200, response.status
104
+ @resource = assigns(:resource)
105
+
106
+ # TODO: parse response? look for access for refresh tokens?
107
+ end
108
+
109
+ test 'request should determine the correct resource_class' do
110
+ assert_equal 'Mang', controller.send(:omniauth_params)['resource_class']
111
+ end
112
+
113
+ test 'user should be of the correct class' do
114
+ assert_equal Mang, @resource.class
115
+ end
116
+ end
117
+
118
+ describe 'pass additional params' do
119
+ before do
120
+ @fav_color = 'alizarin crimson'
121
+ @unpermitted_param = 'M. Bison'
122
+ get '/auth/facebook',
123
+ params: { auth_origin_url: @redirect_url,
124
+ favorite_color: @fav_color,
125
+ name: @unpermitted_param,
126
+ omniauth_window_type: 'newWindow' }
127
+
128
+ follow_all_redirects!
129
+
130
+ @resource = assigns(:resource)
131
+
132
+ # TODO: parse response and look for access and refresh tokens?
133
+ end
134
+
135
+ test 'status shows success' do
136
+ assert_equal 200, response.status
137
+ end
138
+
139
+ test 'additional attribute was passed' do
140
+ assert_equal @fav_color, @resource.favorite_color
141
+ end
142
+
143
+ test 'non-whitelisted attributes are ignored' do
144
+ refute_equal @unpermitted_param, @resource.name
145
+ end
146
+ end
147
+
148
+ describe 'oauth registration attr' do
149
+ after do
150
+ User.any_instance.unstub(:new_record?)
151
+ end
152
+
153
+ describe 'with new user' do
154
+ before do
155
+ User.any_instance.expects(:new_record?).returns(true).at_least_once
156
+ # https://docs.mongodb.com/mongoid/master/tutorials/mongoid-documents/#notes-on-persistence
157
+ User.any_instance.expects(:save!).returns(true)
158
+ end
159
+
160
+ test 'response contains oauth_registration attr' do
161
+ get '/auth/facebook',
162
+ params: { auth_origin_url: @redirect_url,
163
+ omniauth_window_type: 'newWindow' }
164
+
165
+ follow_all_redirects!
166
+
167
+ assert_equal true, controller.auth_params[:oauth_registration]
168
+
169
+ # TODO: parse response and look for access and refresh tokens?
170
+ end
171
+ end
172
+
173
+ describe 'with existing user' do
174
+ before do
175
+ User.any_instance.expects(:new_record?).returns(false).at_least_once
176
+ end
177
+
178
+ test 'response does not contain oauth_registration attr' do
179
+ get '/auth/facebook',
180
+ params: { auth_origin_url: @redirect_url,
181
+ omniauth_window_type: 'newWindow' }
182
+
183
+ follow_all_redirects!
184
+
185
+ assert_equal false, controller.auth_params.key?(:oauth_registration)
186
+
187
+ # TODO: parse response and look for access or refresh tokens?
188
+ end
189
+ end
190
+ end
191
+
192
+ describe 'using namespaces' do
193
+ before do
194
+ get '/api/v1/auth/facebook',
195
+ params: { auth_origin_url: @redirect_url,
196
+ omniauth_window_type: 'newWindow' }
197
+
198
+ follow_all_redirects!
199
+
200
+ @resource = assigns(:resource)
201
+
202
+ # TODO: parse response and look for access or refresh tokens?
203
+ end
204
+
205
+ test 'request is successful' do
206
+ assert_equal 200, response.status
207
+ end
208
+
209
+ test 'user should have been created' do
210
+ assert @resource
211
+ end
212
+
213
+ test 'user should be of the correct class' do
214
+ assert_equal User, @resource.class
215
+ end
216
+ end
217
+
218
+ describe 'with omniauth_window_type=inAppBrowser' do
219
+ test 'response contains all expected data' do
220
+ get_success(omniauth_window_type: 'inAppBrowser')
221
+ assert_expected_data_in_new_window
222
+ end
223
+ end
224
+
225
+ describe 'with omniauth_window_type=newWindow' do
226
+ test 'response contains all expected data' do
227
+ get_success(omniauth_window_type: 'newWindow')
228
+ assert_expected_data_in_new_window
229
+ end
230
+ end
231
+
232
+ def assert_expected_data_in_new_window
233
+ data = get_parsed_data_json
234
+ expected_data = @resource.as_json.merge(controller.auth_params.as_json)
235
+ expected_data = ActiveSupport::JSON.decode(expected_data.to_json)
236
+ assert_equal(expected_data.merge('message' => 'deliverCredentials'), data)
237
+ end
238
+
239
+ describe 'with omniauth_window_type=sameWindow' do
240
+ test 'redirects to auth_origin_url with all expected query params' do
241
+ get '/auth/facebook',
242
+ params: { auth_origin_url: '/auth_origin',
243
+ omniauth_window_type: 'sameWindow' }
244
+
245
+ follow_all_redirects!
246
+
247
+ assert_equal 200, response.status
248
+ # TODO: parse reponse? look for access or refresh tokens?
249
+
250
+ # We have been forwarded to a url with all the expected
251
+ # data in the query params.
252
+
253
+ # Assert that a uid was passed along. We have to assume
254
+ # that the rest of the values were as well, as we don't
255
+ # have access to @resource in this test anymore
256
+ assert(controller.params['uid'], 'No uid found')
257
+
258
+ # check that all the auth stuff is there
259
+ # %i[auth_token client_id uid expiry config].each do |key|
260
+ # assert(controller.params.key?(key), "No value for #{key.inspect}")
261
+ # end
262
+ # TODO: remove uid from this list?
263
+ %i[uid config].each do |key|
264
+ assert(controller.params.key?(key), "No value for #{key.inspect}")
265
+ end
266
+ end
267
+ end
268
+
269
+ def get_success(params = {})
270
+ get '/auth/facebook',
271
+ params: {
272
+ auth_origin_url: @redirect_url,
273
+ omniauth_window_type: 'newWindow'
274
+ }.merge(params)
275
+
276
+ follow_all_redirects!
277
+
278
+ assert_equal 200, response.status
279
+
280
+ @resource = assigns(:resource)
281
+
282
+ # TODO: parse response and look for access or refresh tokens?
283
+ end
284
+ end
285
+
286
+ describe 'failure callback' do
287
+ setup do
288
+ OmniAuth.config.mock_auth[:facebook] = :invalid_credentials
289
+ OmniAuth.config.on_failure = proc { |env|
290
+ OmniAuth::FailureEndpoint.new(env).redirect_to_failure
291
+ }
292
+ end
293
+
294
+ test 'renders expected data' do
295
+ silence_omniauth do
296
+ get '/auth/facebook',
297
+ params: { auth_origin_url: @redirect_url,
298
+ omniauth_window_type: 'newWindow' }
299
+
300
+ follow_all_redirects!
301
+ end
302
+
303
+ assert_equal 200, response.status
304
+
305
+ data = get_parsed_data_json
306
+ # TODO: Check data that it doesnt contain an access or refresh token
307
+ assert_equal({ 'error' => 'invalid_credentials', 'message' => 'authFailure' }, data)
308
+ end
309
+
310
+ test 'renders something with no auth_origin_url' do
311
+ silence_omniauth do
312
+ get '/auth/facebook'
313
+ follow_all_redirects!
314
+ end
315
+ assert_equal 200, response.status
316
+ assert_select 'body', 'invalid_credentials'
317
+ # TODO: test that no access or refresh tokens were sent.
318
+ end
319
+ end
320
+
321
+ describe 'User with only :database_authenticatable and :registerable included' do
322
+ test 'OnlyEmailUser should not be able to use OAuth' do
323
+ assert_raises(ActionController::RoutingError) do
324
+ get '/only_email_auth/facebook',
325
+ params: { auth_origin_url: @redirect_url }
326
+ follow_all_redirects!
327
+ end
328
+
329
+ # TODO: parse response and assert that no access or refresh tokens were sent.
330
+ end
331
+ end
332
+
333
+ describe 'Using redirect_whitelist' do
334
+
335
+ describe "newWindow" do
336
+ before do
337
+ @user_email = 'slemp.diggler@sillybandz.gov'
338
+ OmniAuth.config.mock_auth[:facebook] = OmniAuth::AuthHash.new(
339
+ provider: 'facebook',
340
+ uid: '123545',
341
+ info: {
342
+ name: 'chong',
343
+ email: @user_email
344
+ }
345
+ )
346
+ @good_redirect_url = Faker::Internet.url
347
+ @bad_redirect_url = Faker::Internet.url
348
+ DeviseJwtAuth.redirect_whitelist = [@good_redirect_url]
349
+ end
350
+
351
+ teardown do
352
+ DeviseJwtAuth.redirect_whitelist = nil
353
+ end
354
+
355
+ test 'request using non-whitelisted redirect fail' do
356
+ get '/auth/facebook',
357
+ params: { auth_origin_url: @bad_redirect_url,
358
+ omniauth_window_type: 'newWindow' }
359
+
360
+ follow_all_redirects!
361
+
362
+ data = get_parsed_data_json
363
+ assert_equal "Redirect to &#39;#{@bad_redirect_url}&#39; not allowed.",
364
+ data['error']
365
+ # TODO: parse data and assert no access or refresh tokens were sent.
366
+ end
367
+
368
+ test 'request to whitelisted redirect should succeed' do
369
+ get '/auth/facebook',
370
+ params: {
371
+ auth_origin_url: @good_redirect_url,
372
+ omniauth_window_type: 'newWindow'
373
+ }
374
+
375
+ follow_all_redirects!
376
+
377
+ data = get_parsed_data_json
378
+ assert_equal @user_email, data['email']
379
+ # TODO: parse data and assert that an access token exists
380
+ # and that a refresh token cookie exists.
381
+ end
382
+
383
+ test 'should support wildcards' do
384
+ DeviseJwtAuth.redirect_whitelist = ["#{@good_redirect_url[0..8]}*"]
385
+ get '/auth/facebook',
386
+ params: { auth_origin_url: @good_redirect_url,
387
+ omniauth_window_type: 'newWindow' }
388
+
389
+ follow_all_redirects!
390
+
391
+ data = get_parsed_data_json
392
+ assert_equal @user_email, data['email']
393
+ # TODO: parse data and assert that an access token exists
394
+ # and that a refresh token cookie exists.
395
+ end
396
+ end
397
+
398
+ describe "sameWindow" do
399
+ before do
400
+ @user_email = 'slemp.diggler@sillybandz.gov'
401
+ OmniAuth.config.mock_auth[:facebook] = OmniAuth::AuthHash.new(
402
+ provider: 'facebook',
403
+ uid: '123545',
404
+ info: {
405
+ name: 'chong',
406
+ email: @user_email
407
+ }
408
+ )
409
+ @good_redirect_url = '/auth_origin'
410
+ @bad_redirect_url = Faker::Internet.url
411
+ DeviseJwtAuth.redirect_whitelist = [@good_redirect_url]
412
+ end
413
+
414
+ teardown do
415
+ DeviseJwtAuth.redirect_whitelist = nil
416
+ end
417
+
418
+ test 'request using non-whitelisted redirect fail' do
419
+ get '/auth/facebook',
420
+ params: { auth_origin_url: @bad_redirect_url,
421
+ omniauth_window_type: 'sameWindow' }
422
+
423
+ follow_all_redirects!
424
+
425
+ assert_equal 200, response.status
426
+ assert_equal true, response.body.include?("Redirect to '#{@bad_redirect_url}' not allowed")
427
+ # TODO: parse response and assert no access or refresh tokens exist
428
+ end
429
+
430
+ test 'request to whitelisted redirect should succeed' do
431
+ get '/auth/facebook',
432
+ params: {
433
+ auth_origin_url: '/auth_origin',
434
+ omniauth_window_type: 'sameWindow'
435
+ }
436
+
437
+ follow_all_redirects!
438
+
439
+ assert_equal 200, response.status
440
+ assert_equal false, response.body.include?("Redirect to '#{@good_redirect_url}' not allowed")
441
+ # TODO: parse data and assert that an access token exists
442
+ # and that a refresh token cookie exists.
443
+ end
444
+
445
+ test 'should support wildcards' do
446
+ DeviseJwtAuth.redirect_whitelist = ["#{@good_redirect_url[0..8]}*"]
447
+ get '/auth/facebook',
448
+ params: {
449
+ auth_origin_url: '/auth_origin',
450
+ omniauth_window_type: 'sameWindow'
451
+ }
452
+
453
+ follow_all_redirects!
454
+
455
+ assert_equal 200, response.status
456
+ assert_equal false, response.body.include?("Redirect to '#{@good_redirect_url}' not allowed")
457
+ # TODO: parse data and assert that an access token exists
458
+ # and that a refresh token cookie exists.
459
+ end
460
+ end
461
+ end
462
+ end