devise_jwt_auth 0.1.0

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 (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,286 @@
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 DemoMangControllerTest < ActionDispatch::IntegrationTest
12
+ describe DemoMangController do
13
+ describe 'Token access' do
14
+ before do
15
+ @resource = create(:mang_user, :confirmed)
16
+ @auth_headers = @resource.create_named_token_pair
17
+
18
+ # @token = @auth_headers['access-token']
19
+ # @client_id = @auth_headers['client']
20
+ # @expiry = @auth_headers['expiry']
21
+ end
22
+
23
+ describe 'successful request' do
24
+ before do
25
+ # ensure that request is not treated as batch request
26
+ # age_token(@resource, @client_id)
27
+
28
+ get '/demo/members_only_mang',
29
+ params: {},
30
+ headers: @auth_headers
31
+
32
+ # @resp_token = response.headers['access-token']
33
+ # @resp_client_id = response.headers['client']
34
+ # @resp_expiry = response.headers['expiry']
35
+ # @resp_uid = response.headers['uid']
36
+ end
37
+
38
+ describe 'devise mappings' do
39
+ it 'should define current_mang' do
40
+ assert_equal @resource, @controller.current_mang
41
+ end
42
+
43
+ it 'should define mang_signed_in?' do
44
+ assert @controller.mang_signed_in?
45
+ end
46
+
47
+ it 'should not define current_user' do
48
+ refute_equal @resource, @controller.current_user
49
+ end
50
+
51
+ it 'should define render_authenticate_error' do
52
+ assert @controller.methods.include?(:render_authenticate_error)
53
+ end
54
+ end
55
+
56
+ it 'should return success status' do
57
+ assert_equal 200, response.status
58
+ end
59
+
60
+ =begin
61
+ it 'should receive new token after successful request' do
62
+ refute_equal @token, @resp_token
63
+ end
64
+
65
+ it 'should preserve the client id from the first request' do
66
+ assert_equal @client_id, @resp_client_id
67
+ end
68
+
69
+ it "should return the user's uid in the auth header" do
70
+ assert_equal @resource.uid, @resp_uid
71
+ end
72
+
73
+ it 'should not treat this request as a batch request' do
74
+ refute assigns(:is_batch_request)
75
+ end
76
+
77
+ describe 'subsequent requests' do
78
+ before do
79
+ @resource.reload
80
+ # ensure that request is not treated as batch request
81
+ # age_token(@resource, @client_id)
82
+
83
+ get '/demo/members_only_mang',
84
+ params: {},
85
+ headers: @auth_headers.merge('access-token' => @resp_token)
86
+ end
87
+
88
+ it 'should not treat this request as a batch request' do
89
+ refute assigns(:is_batch_request)
90
+ end
91
+
92
+ it 'should allow a new request to be made using new token' do
93
+ assert_equal 200, response.status
94
+ end
95
+ end
96
+ =end
97
+ end
98
+
99
+ describe 'failed request' do
100
+ before do
101
+ get '/demo/members_only_mang',
102
+ params: {},
103
+ headers: @auth_headers.merge('access-token' => 'bogus')
104
+ end
105
+
106
+ it 'should not return any auth headers' do
107
+ refute response.headers['access-token']
108
+ end
109
+
110
+ it 'should return error: unauthorized status' do
111
+ assert_equal 401, response.status
112
+ end
113
+ end
114
+
115
+ =begin
116
+ describe 'disable change_headers_on_each_request' do
117
+ before do
118
+ DeviseJwtAuth.change_headers_on_each_request = false
119
+ @resource.reload
120
+ # age_token(@resource, @client_id)
121
+
122
+ get '/demo/members_only_mang',
123
+ params: {},
124
+ headers: @auth_headers
125
+
126
+ @first_is_batch_request = assigns(:is_batch_request)
127
+ @first_user = assigns(:resource).dup
128
+ @first_access_token = response.headers['access-token']
129
+ @first_response_status = response.status
130
+
131
+ @resource.reload
132
+ # age_token(@resource, @client_id)
133
+
134
+ # use expired auth header
135
+ get '/demo/members_only_mang',
136
+ params: {},
137
+ headers: @auth_headers
138
+
139
+ @second_is_batch_request = assigns(:is_batch_request)
140
+ @second_user = assigns(:resource).dup
141
+ @second_access_token = response.headers['access-token']
142
+ @second_response_status = response.status
143
+ end
144
+
145
+ after do
146
+ DeviseJwtAuth.change_headers_on_each_request = true
147
+ end
148
+
149
+ it 'should allow the first request through' do
150
+ assert_equal 200, @first_response_status
151
+ end
152
+
153
+ it 'should allow the second request through' do
154
+ assert_equal 200, @second_response_status
155
+ end
156
+
157
+ it 'should return auth headers from the first request' do
158
+ assert @first_access_token
159
+ end
160
+
161
+ it 'should not treat either requests as batch requests' do
162
+ refute @first_is_batch_request
163
+ refute @second_is_batch_request
164
+ end
165
+
166
+ it 'should return auth headers from the second request' do
167
+ assert @second_access_token
168
+ end
169
+
170
+ it 'should define user during first request' do
171
+ assert @first_user
172
+ end
173
+
174
+ it 'should define user during second request' do
175
+ assert @second_user
176
+ end
177
+ end
178
+
179
+ describe 'batch requests' do
180
+ describe 'success' do
181
+ before do
182
+ # age_token(@resource, @client_id)
183
+
184
+ get '/demo/members_only_mang',
185
+ params: {},
186
+ headers: @auth_headers
187
+
188
+ @first_is_batch_request = assigns(:is_batch_request)
189
+ @first_user = assigns(:resource)
190
+ @first_access_token = response.headers['access-token']
191
+
192
+ get '/demo/members_only_mang',
193
+ params: {},
194
+ headers: @auth_headers
195
+
196
+ @second_is_batch_request = assigns(:is_batch_request)
197
+ @second_user = assigns(:resource)
198
+ @second_access_token = response.headers['access-token']
199
+ end
200
+
201
+ it 'should allow both requests through' do
202
+ assert_equal 200, response.status
203
+ end
204
+
205
+ it 'should not treat the first request as a batch request' do
206
+ refute @first_is_batch_request
207
+ end
208
+
209
+ it 'should treat the second request as a batch request' do
210
+ assert @second_is_batch_request
211
+ end
212
+
213
+ it 'should return access token for first (non-batch) request' do
214
+ assert @first_access_token
215
+ end
216
+
217
+ it 'should not return auth headers for second (batched) requests' do
218
+ assert_equal ' ', @second_access_token
219
+ end
220
+ end
221
+
222
+ describe 'time out' do
223
+ before do
224
+ @resource.reload
225
+ # age_token(@resource, @client_id)
226
+
227
+ get '/demo/members_only_mang',
228
+ params: {},
229
+ headers: @auth_headers
230
+
231
+ @first_is_batch_request = assigns(:is_batch_request)
232
+ @first_user = assigns(:resource).dup
233
+ @first_access_token = response.headers['access-token']
234
+ @first_response_status = response.status
235
+
236
+ @resource.reload
237
+ # age_token(@resource, @client_id)
238
+
239
+ # use expired auth header
240
+ get '/demo/members_only_mang',
241
+ params: {},
242
+ headers: @auth_headers
243
+
244
+ @second_is_batch_request = assigns(:is_batch_request)
245
+ @second_user = assigns(:resource)
246
+ @second_access_token = response.headers['access-token']
247
+ @second_response_status = response.status
248
+ end
249
+
250
+ it 'should allow the first request through' do
251
+ assert_equal 200, @first_response_status
252
+ end
253
+
254
+ it 'should not allow the second request through' do
255
+ assert_equal 401, @second_response_status
256
+ end
257
+
258
+ it 'should not treat first request as batch request' do
259
+ refute @second_is_batch_request
260
+ end
261
+
262
+ it 'should return auth headers from the first request' do
263
+ assert @first_access_token
264
+ end
265
+
266
+ it 'should not treat second request as batch request' do
267
+ refute @second_is_batch_request
268
+ end
269
+
270
+ it 'should not return auth headers from the second request' do
271
+ refute @second_access_token
272
+ end
273
+
274
+ it 'should define user during first request' do
275
+ assert @first_user
276
+ end
277
+
278
+ it 'should not define user during second request' do
279
+ refute @second_user
280
+ end
281
+ end
282
+ end
283
+ =end
284
+ end
285
+ end
286
+ end
@@ -0,0 +1,650 @@
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 DemoUserControllerTest < ActionDispatch::IntegrationTest
12
+ include Warden::Test::Helpers
13
+ describe DemoUserController do
14
+ describe 'Token access' do
15
+ before do
16
+ @resource = create(:user, :confirmed)
17
+ @auth_headers = @resource.create_named_token_pair
18
+
19
+ # @token = @auth_headers['access-token']
20
+ # @client_id = @auth_headers['client']
21
+ # @expiry = @auth_headers['expiry']
22
+ end
23
+
24
+ describe 'successful request' do
25
+ before do
26
+ # ensure that request is not treated as batch request
27
+ # age_token(@resource, @client_id)
28
+
29
+ get '/demo/members_only',
30
+ params: {},
31
+ headers: @auth_headers
32
+
33
+ # @resp_token = response.headers['access-token']
34
+ # @resp_client_id = response.headers['client']
35
+ # @resp_expiry = response.headers['expiry']
36
+ # @resp_uid = response.headers['uid']
37
+ end
38
+
39
+ describe 'devise mappings' do
40
+ it 'should define current_user' do
41
+ assert_equal @resource, @controller.current_user
42
+ end
43
+
44
+ it 'should define user_signed_in?' do
45
+ assert @controller.user_signed_in?
46
+ end
47
+
48
+ it 'should not define current_mang' do
49
+ refute_equal @resource, @controller.current_mang
50
+ end
51
+
52
+ it 'should define render_authenticate_error' do
53
+ assert @controller.methods.include?(:render_authenticate_error)
54
+ end
55
+ end
56
+
57
+ it 'should return success status' do
58
+ assert_equal 200, response.status
59
+ end
60
+
61
+ =begin
62
+ it 'should receive new token after successful request' do
63
+ refute_equal @token, @resp_token
64
+ end
65
+
66
+ it 'should preserve the client id from the first request' do
67
+ assert_equal @client_id, @resp_client_id
68
+ end
69
+
70
+ it "should return the user's uid in the auth header" do
71
+ assert_equal @resource.uid, @resp_uid
72
+ end
73
+
74
+ it 'should not treat this request as a batch request' do
75
+ refute assigns(:is_batch_request)
76
+ end
77
+
78
+ describe 'subsequent requests' do
79
+ before do
80
+ @resource.reload
81
+ # ensure that request is not treated as batch request
82
+ # age_token(@resource, @client_id)
83
+
84
+ get '/demo/members_only',
85
+ params: {},
86
+ headers: @auth_headers.merge('access-token' => @resp_token)
87
+ end
88
+
89
+ it 'should not treat this request as a batch request' do
90
+ refute assigns(:is_batch_request)
91
+ end
92
+
93
+ it 'should allow a new request to be made using new token' do
94
+ assert_equal 200, response.status
95
+ end
96
+ end
97
+ =end
98
+ end
99
+
100
+ describe 'failed request' do
101
+ before do
102
+ get '/demo/members_only',
103
+ params: {},
104
+ headers: @auth_headers.merge('access-token' => 'bogus')
105
+ end
106
+
107
+ it 'should not return any auth headers' do
108
+ refute response.headers['access-token']
109
+ end
110
+
111
+ it 'should return error: unauthorized status' do
112
+ assert_equal 401, response.status
113
+ end
114
+ end
115
+ =begin
116
+ describe 'disable change_headers_on_each_request' do
117
+ before do
118
+ DeviseJwtAuth.change_headers_on_each_request = false
119
+ @resource.reload
120
+ # age_token(@resource, @client_id)
121
+
122
+ get '/demo/members_only',
123
+ params: {},
124
+ headers: @auth_headers
125
+
126
+ @first_is_batch_request = assigns(:is_batch_request)
127
+ @first_user = assigns(:resource).dup
128
+ @first_access_token = response.headers['access-token']
129
+ @first_response_status = response.status
130
+
131
+ @resource.reload
132
+ # age_token(@resource, @client_id)
133
+
134
+ # use expired auth header
135
+ get '/demo/members_only',
136
+ params: {},
137
+ headers: @auth_headers
138
+
139
+ @second_is_batch_request = assigns(:is_batch_request)
140
+ @second_user = assigns(:resource).dup
141
+ @second_access_token = response.headers['access-token']
142
+ @second_response_status = response.status
143
+ end
144
+
145
+ after do
146
+ DeviseJwtAuth.change_headers_on_each_request = true
147
+ end
148
+
149
+ it 'should allow the first request through' do
150
+ assert_equal 200, @first_response_status
151
+ end
152
+
153
+ it 'should allow the second request through' do
154
+ assert_equal 200, @second_response_status
155
+ end
156
+
157
+ it 'should return auth headers from the first request' do
158
+ assert @first_access_token
159
+ end
160
+
161
+ it 'should not treat either requests as batch requests' do
162
+ refute @first_is_batch_request
163
+ refute @second_is_batch_request
164
+ end
165
+
166
+ it 'should return auth headers from the second request' do
167
+ assert @second_access_token
168
+ end
169
+
170
+ it 'should define user during first request' do
171
+ assert @first_user
172
+ end
173
+
174
+ it 'should define user during second request' do
175
+ assert @second_user
176
+ end
177
+ end
178
+
179
+ describe 'batch requests' do
180
+ describe 'success' do
181
+ before do
182
+ # age_token(@resource, @client_id)
183
+
184
+ get '/demo/members_only',
185
+ params: {},
186
+ headers: @auth_headers
187
+
188
+ @first_is_batch_request = assigns(:is_batch_request)
189
+ @first_user = assigns(:resource)
190
+ @first_access_token = response.headers['access-token']
191
+
192
+ get '/demo/members_only',
193
+ params: {},
194
+ headers: @auth_headers
195
+
196
+ @second_is_batch_request = assigns(:is_batch_request)
197
+ @second_user = assigns(:resource)
198
+ @second_access_token = response.headers['access-token']
199
+ end
200
+
201
+ it 'should allow both requests through' do
202
+ assert_equal 200, response.status
203
+ end
204
+
205
+ it 'should not treat the first request as a batch request' do
206
+ refute @first_is_batch_request
207
+ end
208
+
209
+ it 'should treat the second request as a batch request' do
210
+ assert @second_is_batch_request
211
+ end
212
+
213
+ it 'should return access token for first (non-batch) request' do
214
+ assert @first_access_token
215
+ end
216
+
217
+ it 'should not return auth headers for second (batched) requests' do
218
+ assert_equal ' ', @second_access_token
219
+ end
220
+ end
221
+
222
+ describe 'unbatch' do
223
+ before do
224
+ @resource.reload
225
+ # age_token(@resource, @client_id)
226
+
227
+ get '/demo/members_only',
228
+ params: {},
229
+ headers: @auth_headers
230
+
231
+ @first_is_batch_request = assigns(:is_batch_request)
232
+ @first_user = assigns(:resource).dup
233
+ @first_access_token = response.headers['access-token']
234
+ @first_response_status = response.status
235
+
236
+ get '/demo/members_only?unbatch=true',
237
+ params: {},
238
+ headers: @auth_headers
239
+
240
+ @second_is_batch_request = assigns(:is_batch_request)
241
+ @second_user = assigns(:resource)
242
+ @second_access_token = response.headers['access-token']
243
+ @second_response_status = response.status
244
+ end
245
+
246
+ it 'should NOT treat the second request as a batch request when "unbatch" param is set' do
247
+ refute @second_is_batch_request
248
+ end
249
+ end
250
+
251
+ describe 'time out' do
252
+ before do
253
+ @resource.reload
254
+ # age_token(@resource, @client_id)
255
+
256
+ get '/demo/members_only',
257
+ params: {},
258
+ headers: @auth_headers
259
+
260
+ @first_is_batch_request = assigns(:is_batch_request)
261
+ @first_user = assigns(:resource).dup
262
+ @first_access_token = response.headers['access-token']
263
+ @first_response_status = response.status
264
+
265
+ @resource.reload
266
+ # age_token(@resource, @client_id)
267
+
268
+ # use expired auth header
269
+ get '/demo/members_only',
270
+ params: {},
271
+ headers: @auth_headers
272
+
273
+ @second_is_batch_request = assigns(:is_batch_request)
274
+ @second_user = assigns(:resource)
275
+ @second_access_token = response.headers['access-token']
276
+ @second_response_status = response.status
277
+ end
278
+
279
+ it 'should allow the first request through' do
280
+ assert_equal 200, @first_response_status
281
+ end
282
+
283
+ it 'should not allow the second request through' do
284
+ assert_equal 401, @second_response_status
285
+ end
286
+
287
+ it 'should not treat first request as batch request' do
288
+ refute @second_is_batch_request
289
+ end
290
+
291
+ it 'should return auth headers from the first request' do
292
+ assert @first_access_token
293
+ end
294
+
295
+ it 'should not treat second request as batch request' do
296
+ refute @second_is_batch_request
297
+ end
298
+
299
+ it 'should not return auth headers from the second request' do
300
+ refute @second_access_token
301
+ end
302
+
303
+ it 'should define user during first request' do
304
+ assert @first_user
305
+ end
306
+
307
+ it 'should not define user during second request' do
308
+ refute @second_user
309
+ end
310
+ end
311
+ end
312
+ =end
313
+
314
+ describe 'successful password change' do
315
+ before do
316
+ # DeviseJwtAuth.remove_tokens_after_password_reset = true
317
+
318
+ # adding one more token to simulate another logged in device
319
+ @old_auth_headers = @auth_headers
320
+ @auth_headers = @resource.create_named_token_pair
321
+
322
+ # age_token(@resource, @client_id)
323
+ # assert @resource.tokens.count > 1
324
+
325
+ # password changed from new device
326
+ @resource.update(password: 'newsecret123',
327
+ password_confirmation: 'newsecret123')
328
+
329
+ get '/demo/members_only',
330
+ params: {},
331
+ headers: @auth_headers
332
+ end
333
+
334
+ after do
335
+ # DeviseJwtAuth.remove_tokens_after_password_reset = false
336
+ end
337
+
338
+ # it 'should have only one token' do
339
+ # assert_equal 1, @resource.tokens.count
340
+ # end
341
+
342
+ it 'new request should be successful' do
343
+ assert 200, response.status
344
+ end
345
+
346
+ describe 'another device should not be able to login' do
347
+ it 'should return forbidden status' do
348
+ get '/demo/members_only',
349
+ params: {},
350
+ headers: @old_auth_headers
351
+ assert 401, response.status
352
+ end
353
+ end
354
+ end
355
+
356
+ =begin
357
+ describe 'request including destroy of token' do
358
+ describe 'when change_headers_on_each_request is set to false' do
359
+ before do
360
+ DeviseJwtAuth.change_headers_on_each_request = false
361
+ # age_token(@resource, @client_id)
362
+
363
+ get '/demo/members_only_remove_token',
364
+ params: {},
365
+ headers: @auth_headers
366
+ end
367
+
368
+ after do
369
+ DeviseJwtAuth.change_headers_on_each_request = true
370
+ end
371
+
372
+ it 'should not return auth-headers' do
373
+ refute response.headers['access-token']
374
+ end
375
+ end
376
+
377
+ describe 'when change_headers_on_each_request is set to true' do
378
+ before do
379
+ # age_token(@resource, @client_id)
380
+ get '/demo/members_only_remove_token',
381
+ params: {},
382
+ headers: @auth_headers
383
+ end
384
+
385
+ it 'should not return auth-headers' do
386
+ refute response.headers['access-token']
387
+ end
388
+ end
389
+ end
390
+ =end
391
+
392
+ describe 'when access-token name has been changed' do
393
+ before do
394
+ # ensure that request is not treated as batch request
395
+ # DeviseJwtAuth.headers_names[:'access-token'] = 'new-access-token'
396
+ DeviseJwtAuth.access_token_name = 'new-access-token'
397
+
398
+
399
+ auth_headers_modified = @resource.create_named_token_pair
400
+
401
+ # client_id = auth_headers_modified['client']
402
+ # age_token(@resource, client_id)
403
+
404
+ get '/demo/members_only',
405
+ params: {},
406
+ headers: auth_headers_modified
407
+
408
+ # @resp_token = response.headers['new-access-token']
409
+ # TODO: do we want to send access-tokens with every response?
410
+ @data = JSON.parse(response.body)
411
+ end
412
+ =begin
413
+ it 'should have "new-access-token" in reponse' do
414
+ # assert @resp_token.present?
415
+ # assert @data['new-access-token']
416
+ end
417
+ =end
418
+ after do
419
+ # DeviseJwtAuth.headers_names[:'access-token'] = 'access-token'
420
+ DeviseJwtAuth.access_token_name = 'access-token'
421
+ end
422
+ end
423
+
424
+ =begin
425
+ describe 'maximum concurrent devices per user' do
426
+ before do
427
+ # Set the max_number_of_devices to a lower number
428
+ # to expedite tests! (Default is 10)
429
+ DeviseJwtAuth.max_number_of_devices = 5
430
+ end
431
+
432
+ it 'should limit the maximum number of concurrent devices' do
433
+ # increment the number of devices until the maximum is exceeded
434
+ 1.upto(DeviseJwtAuth.max_number_of_devices + 1).each do |n|
435
+
436
+ assert_equal(
437
+ [n, DeviseJwtAuth.max_number_of_devices].min,
438
+ @resource.reload.tokens.length
439
+ )
440
+
441
+ # Add a new device (and token) ahead of the next iteration
442
+ # @resource.create_new_auth_token
443
+ create_token_header(@resource)
444
+ end
445
+ end
446
+
447
+ it 'should drop the oldest token when the maximum number of devices is exceeded' do
448
+ # create the maximum number of tokens
449
+ 1.upto(DeviseJwtAuth.max_number_of_devices).each do
450
+ # @resource.create_new_auth_token
451
+ create_token_header(@resource)
452
+ end
453
+
454
+ # get the oldest token client_id
455
+ oldest_client_id, = @resource.reload.tokens.min_by do |cid, v|
456
+ v[:expiry] || v['expiry']
457
+ end # => [ 'CLIENT_ID', {token: ...} ]
458
+
459
+ # create another token, thereby dropping the oldest token
460
+ # @resource.create_new_auth_token
461
+ create_token_header(@resource)
462
+
463
+ assert_not_includes @resource.reload.tokens.keys, oldest_client_id
464
+ end
465
+
466
+ after do
467
+ DeviseJwtAuth.max_number_of_devices = 10
468
+ end
469
+ end
470
+ =end
471
+ end
472
+
473
+ describe 'bypass_sign_in' do
474
+ before do
475
+ @resource = create(:user)
476
+ @auth_headers = @resource.create_named_token_pair
477
+
478
+ # @token = @auth_headers['access-token']
479
+ # @client_id = @auth_headers['client']
480
+ # @expiry = @auth_headers['expiry']
481
+ end
482
+ describe 'is default value (true)' do
483
+ before do
484
+ # age_token(@resource, @client_id)
485
+
486
+ get '/demo/members_only', params: {}, headers: @auth_headers
487
+
488
+ # @access_token = response.headers['access-token']
489
+ @response_status = response.status
490
+ end
491
+
492
+ it 'should allow the request through' do
493
+ assert_equal 200, @response_status
494
+ end
495
+
496
+ # it 'should return auth headers' do
497
+ # assert @access_token
498
+ # end
499
+
500
+ it 'should set current user' do
501
+ assert_equal @controller.current_user, @resource
502
+ end
503
+ end
504
+ describe 'is false' do
505
+ before do
506
+ DeviseJwtAuth.bypass_sign_in = false
507
+ # age_token(@resource, @client_id)
508
+
509
+ get '/demo/members_only', params: {}, headers: @auth_headers
510
+
511
+ @access_token = response.headers['access-token']
512
+ @response_status = response.status
513
+
514
+ DeviseJwtAuth.bypass_sign_in = true
515
+ end
516
+
517
+ it 'should not allow the request through' do
518
+ refute_equal 200, @response_status
519
+ end
520
+
521
+ it 'should not return auth headers from the first request' do
522
+ assert_nil @access_token
523
+ end
524
+ end
525
+ end
526
+
527
+ describe 'enable_standard_devise_support' do
528
+ before do
529
+ @resource = create(:user, :confirmed)
530
+ @auth_headers = @resource.create_named_token_pair
531
+
532
+ DeviseJwtAuth.enable_standard_devise_support = true
533
+ end
534
+
535
+ describe 'Existing Warden authentication' do
536
+ before do
537
+ @resource = create(:user, :confirmed)
538
+ login_as(@resource, scope: :user)
539
+
540
+ # no auth headers sent, testing that warden authenticates correctly.
541
+ get '/demo/members_only',
542
+ params: {},
543
+ headers: nil
544
+
545
+ # @resp_token = response.headers['access-token']
546
+ # @resp_client_id = response.headers['client']
547
+ # @resp_expiry = response.headers['expiry']
548
+ # @resp_uid = response.headers['uid']
549
+ end
550
+
551
+ describe 'devise mappings' do
552
+ it 'should define current_user' do
553
+ assert_equal @resource, @controller.current_user
554
+ end
555
+
556
+ it 'should define user_signed_in?' do
557
+ assert @controller.user_signed_in?
558
+ end
559
+
560
+ it 'should not define current_mang' do
561
+ refute_equal @resource, @controller.current_mang
562
+ end
563
+
564
+ end
565
+
566
+ it 'should return success status' do
567
+ assert_equal 200, response.status
568
+ end
569
+
570
+ =begin
571
+ it 'should receive new token after successful request' do
572
+ assert @resp_token
573
+ end
574
+
575
+ it 'should set the token expiry in the auth header' do
576
+ assert @resp_expiry
577
+ end
578
+
579
+ it 'should return the client id in the auth header' do
580
+ assert @resp_client_id
581
+ end
582
+
583
+ it "should return the user's uid in the auth header" do
584
+ assert @resp_uid
585
+ end
586
+ =end
587
+ end
588
+
589
+ describe 'existing Warden authentication with ignored token data' do
590
+ before do
591
+ @resource = create(:user, :confirmed)
592
+ login_as(@resource, scope: :user)
593
+
594
+ get '/demo/members_only',
595
+ params: {},
596
+ headers: @auth_headers
597
+
598
+ # @resp_token = response.headers['access-token']
599
+ # @resp_client_id = response.headers['client']
600
+ # @resp_expiry = response.headers['expiry']
601
+ # @resp_uid = response.headers['uid']
602
+ end
603
+
604
+ describe 'devise mappings' do
605
+ it 'should define current_user' do
606
+ assert_equal @resource, @controller.current_user
607
+ end
608
+
609
+ it 'should define user_signed_in?' do
610
+ assert @controller.user_signed_in?
611
+ end
612
+
613
+ it 'should not define current_mang' do
614
+ refute_equal @resource, @controller.current_mang
615
+ end
616
+ end
617
+
618
+ it 'should return success status' do
619
+ assert_equal 200, response.status
620
+ end
621
+
622
+ =begin
623
+ it 'should receive new token after successful request' do
624
+ assert @resp_token
625
+ end
626
+
627
+ it 'should set the token expiry in the auth header' do
628
+ assert @resp_expiry
629
+ end
630
+
631
+ it 'should return the client id in the auth header' do
632
+ assert @resp_client_id
633
+ end
634
+
635
+ it "should not use the existing token's client" do
636
+ refute_equal @auth_headers['client'], @resp_client_id
637
+ end
638
+
639
+ it "should return the user's uid in the auth header" do
640
+ assert @resp_uid
641
+ end
642
+
643
+ it "should not return the token user's uid in the auth header" do
644
+ refute_equal @resp_uid, @auth_headers['uid']
645
+ end
646
+ =end
647
+ end
648
+ end
649
+ end
650
+ end