devise_token_auth_skycocker_fork 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (167) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +13 -0
  3. data/README.md +97 -0
  4. data/Rakefile +42 -0
  5. data/app/controllers/devise_token_auth/application_controller.rb +79 -0
  6. data/app/controllers/devise_token_auth/concerns/resource_finder.rb +44 -0
  7. data/app/controllers/devise_token_auth/concerns/set_user_by_token.rb +178 -0
  8. data/app/controllers/devise_token_auth/confirmations_controller.rb +39 -0
  9. data/app/controllers/devise_token_auth/omniauth_callbacks_controller.rb +239 -0
  10. data/app/controllers/devise_token_auth/passwords_controller.rb +185 -0
  11. data/app/controllers/devise_token_auth/registrations_controller.rb +198 -0
  12. data/app/controllers/devise_token_auth/sessions_controller.rb +131 -0
  13. data/app/controllers/devise_token_auth/token_validations_controller.rb +31 -0
  14. data/app/controllers/devise_token_auth/unlocks_controller.rb +89 -0
  15. data/app/models/devise_token_auth/concerns/active_record_support.rb +34 -0
  16. data/app/models/devise_token_auth/concerns/mongoid_support.rb +19 -0
  17. data/app/models/devise_token_auth/concerns/user.rb +262 -0
  18. data/app/models/devise_token_auth/concerns/user_omniauth_callbacks.rb +28 -0
  19. data/app/validators/devise_token_auth/email_validator.rb +23 -0
  20. data/app/views/devise/mailer/confirmation_instructions.html.erb +5 -0
  21. data/app/views/devise/mailer/reset_password_instructions.html.erb +8 -0
  22. data/app/views/devise/mailer/unlock_instructions.html.erb +7 -0
  23. data/app/views/devise_token_auth/omniauth_external_window.html.erb +38 -0
  24. data/config/locales/da-DK.yml +50 -0
  25. data/config/locales/de.yml +49 -0
  26. data/config/locales/en.yml +50 -0
  27. data/config/locales/es.yml +49 -0
  28. data/config/locales/fr.yml +49 -0
  29. data/config/locales/it.yml +46 -0
  30. data/config/locales/ja.yml +46 -0
  31. data/config/locales/nl.yml +30 -0
  32. data/config/locales/pl.yml +48 -0
  33. data/config/locales/pt-BR.yml +46 -0
  34. data/config/locales/pt.yml +48 -0
  35. data/config/locales/ro.yml +46 -0
  36. data/config/locales/ru.yml +50 -0
  37. data/config/locales/sq.yml +46 -0
  38. data/config/locales/sv.yml +50 -0
  39. data/config/locales/uk.yml +59 -0
  40. data/config/locales/vi.yml +50 -0
  41. data/config/locales/zh-CN.yml +46 -0
  42. data/config/locales/zh-HK.yml +48 -0
  43. data/config/locales/zh-TW.yml +48 -0
  44. data/lib/devise_token_auth.rb +13 -0
  45. data/lib/devise_token_auth/blacklist.rb +2 -0
  46. data/lib/devise_token_auth/controllers/helpers.rb +161 -0
  47. data/lib/devise_token_auth/controllers/url_helpers.rb +10 -0
  48. data/lib/devise_token_auth/engine.rb +90 -0
  49. data/lib/devise_token_auth/errors.rb +8 -0
  50. data/lib/devise_token_auth/rails/routes.rb +116 -0
  51. data/lib/devise_token_auth/url.rb +41 -0
  52. data/lib/devise_token_auth/version.rb +5 -0
  53. data/lib/generators/devise_token_auth/USAGE +31 -0
  54. data/lib/generators/devise_token_auth/install_generator.rb +91 -0
  55. data/lib/generators/devise_token_auth/install_generator_helpers.rb +98 -0
  56. data/lib/generators/devise_token_auth/install_mongoid_generator.rb +46 -0
  57. data/lib/generators/devise_token_auth/install_views_generator.rb +18 -0
  58. data/lib/generators/devise_token_auth/templates/devise_token_auth.rb +50 -0
  59. data/lib/generators/devise_token_auth/templates/devise_token_auth_create_users.rb.erb +56 -0
  60. data/lib/generators/devise_token_auth/templates/user.rb.erb +9 -0
  61. data/lib/generators/devise_token_auth/templates/user_mongoid.rb.erb +63 -0
  62. data/lib/tasks/devise_token_auth_tasks.rake +6 -0
  63. data/test/controllers/custom/custom_confirmations_controller_test.rb +25 -0
  64. data/test/controllers/custom/custom_omniauth_callbacks_controller_test.rb +33 -0
  65. data/test/controllers/custom/custom_passwords_controller_test.rb +79 -0
  66. data/test/controllers/custom/custom_registrations_controller_test.rb +63 -0
  67. data/test/controllers/custom/custom_sessions_controller_test.rb +39 -0
  68. data/test/controllers/custom/custom_token_validations_controller_test.rb +42 -0
  69. data/test/controllers/demo_group_controller_test.rb +151 -0
  70. data/test/controllers/demo_mang_controller_test.rb +284 -0
  71. data/test/controllers/demo_user_controller_test.rb +629 -0
  72. data/test/controllers/devise_token_auth/confirmations_controller_test.rb +127 -0
  73. data/test/controllers/devise_token_auth/omniauth_callbacks_controller_test.rb +376 -0
  74. data/test/controllers/devise_token_auth/passwords_controller_test.rb +639 -0
  75. data/test/controllers/devise_token_auth/registrations_controller_test.rb +880 -0
  76. data/test/controllers/devise_token_auth/sessions_controller_test.rb +541 -0
  77. data/test/controllers/devise_token_auth/token_validations_controller_test.rb +102 -0
  78. data/test/controllers/devise_token_auth/unlocks_controller_test.rb +196 -0
  79. data/test/controllers/overrides/confirmations_controller_test.rb +47 -0
  80. data/test/controllers/overrides/omniauth_callbacks_controller_test.rb +53 -0
  81. data/test/controllers/overrides/passwords_controller_test.rb +64 -0
  82. data/test/controllers/overrides/registrations_controller_test.rb +46 -0
  83. data/test/controllers/overrides/sessions_controller_test.rb +35 -0
  84. data/test/controllers/overrides/token_validations_controller_test.rb +43 -0
  85. data/test/dummy/README.rdoc +28 -0
  86. data/test/dummy/app/active_record/lockable_user.rb +7 -0
  87. data/test/dummy/app/active_record/mang.rb +5 -0
  88. data/test/dummy/app/active_record/only_email_user.rb +7 -0
  89. data/test/dummy/app/active_record/scoped_user.rb +9 -0
  90. data/test/dummy/app/active_record/unconfirmable_user.rb +9 -0
  91. data/test/dummy/app/active_record/unregisterable_user.rb +9 -0
  92. data/test/dummy/app/active_record/user.rb +6 -0
  93. data/test/dummy/app/controllers/application_controller.rb +18 -0
  94. data/test/dummy/app/controllers/auth_origin_controller.rb +7 -0
  95. data/test/dummy/app/controllers/custom/confirmations_controller.rb +13 -0
  96. data/test/dummy/app/controllers/custom/omniauth_callbacks_controller.rb +13 -0
  97. data/test/dummy/app/controllers/custom/passwords_controller.rb +39 -0
  98. data/test/dummy/app/controllers/custom/registrations_controller.rb +39 -0
  99. data/test/dummy/app/controllers/custom/sessions_controller.rb +29 -0
  100. data/test/dummy/app/controllers/custom/token_validations_controller.rb +19 -0
  101. data/test/dummy/app/controllers/demo_group_controller.rb +15 -0
  102. data/test/dummy/app/controllers/demo_mang_controller.rb +14 -0
  103. data/test/dummy/app/controllers/demo_user_controller.rb +27 -0
  104. data/test/dummy/app/controllers/overrides/confirmations_controller.rb +28 -0
  105. data/test/dummy/app/controllers/overrides/omniauth_callbacks_controller.rb +16 -0
  106. data/test/dummy/app/controllers/overrides/passwords_controller.rb +35 -0
  107. data/test/dummy/app/controllers/overrides/registrations_controller.rb +29 -0
  108. data/test/dummy/app/controllers/overrides/sessions_controller.rb +36 -0
  109. data/test/dummy/app/controllers/overrides/token_validations_controller.rb +23 -0
  110. data/test/dummy/app/helpers/application_helper.rb +1058 -0
  111. data/test/dummy/app/models/concerns/favorite_color.rb +19 -0
  112. data/test/dummy/app/mongoid/lockable_user.rb +38 -0
  113. data/test/dummy/app/mongoid/mang.rb +53 -0
  114. data/test/dummy/app/mongoid/only_email_user.rb +33 -0
  115. data/test/dummy/app/mongoid/scoped_user.rb +57 -0
  116. data/test/dummy/app/mongoid/unconfirmable_user.rb +51 -0
  117. data/test/dummy/app/mongoid/unregisterable_user.rb +54 -0
  118. data/test/dummy/app/mongoid/user.rb +56 -0
  119. data/test/dummy/app/views/layouts/application.html.erb +14 -0
  120. data/test/dummy/config.ru +18 -0
  121. data/test/dummy/config/application.rb +48 -0
  122. data/test/dummy/config/application.yml.bk +0 -0
  123. data/test/dummy/config/boot.rb +11 -0
  124. data/test/dummy/config/environment.rb +7 -0
  125. data/test/dummy/config/environments/development.rb +46 -0
  126. data/test/dummy/config/environments/production.rb +84 -0
  127. data/test/dummy/config/environments/test.rb +50 -0
  128. data/test/dummy/config/initializers/assets.rb +10 -0
  129. data/test/dummy/config/initializers/backtrace_silencers.rb +9 -0
  130. data/test/dummy/config/initializers/cookies_serializer.rb +5 -0
  131. data/test/dummy/config/initializers/devise.rb +17 -0
  132. data/test/dummy/config/initializers/devise_token_auth.rb +24 -0
  133. data/test/dummy/config/initializers/figaro.rb +3 -0
  134. data/test/dummy/config/initializers/filter_parameter_logging.rb +6 -0
  135. data/test/dummy/config/initializers/inflections.rb +18 -0
  136. data/test/dummy/config/initializers/mime_types.rb +6 -0
  137. data/test/dummy/config/initializers/omniauth.rb +10 -0
  138. data/test/dummy/config/initializers/session_store.rb +5 -0
  139. data/test/dummy/config/initializers/wrap_parameters.rb +16 -0
  140. data/test/dummy/config/routes.rb +55 -0
  141. data/test/dummy/config/spring.rb +3 -0
  142. data/test/dummy/db/migrate/20140715061447_devise_token_auth_create_users.rb +65 -0
  143. data/test/dummy/db/migrate/20140715061805_devise_token_auth_create_mangs.rb +64 -0
  144. data/test/dummy/db/migrate/20140829044006_add_operating_thetan_to_user.rb +8 -0
  145. data/test/dummy/db/migrate/20140916224624_add_favorite_color_to_mangs.rb +7 -0
  146. data/test/dummy/db/migrate/20141222035835_devise_token_auth_create_only_email_users.rb +62 -0
  147. data/test/dummy/db/migrate/20141222053502_devise_token_auth_create_unregisterable_users.rb +63 -0
  148. data/test/dummy/db/migrate/20150708104536_devise_token_auth_create_unconfirmable_users.rb +63 -0
  149. data/test/dummy/db/migrate/20160103235141_devise_token_auth_create_scoped_users.rb +63 -0
  150. data/test/dummy/db/migrate/20160629184441_devise_token_auth_create_lockable_users.rb +63 -0
  151. data/test/dummy/db/schema.rb +200 -0
  152. data/test/dummy/lib/migration_database_helper.rb +43 -0
  153. data/test/dummy/tmp/generators/app/models/user.rb +9 -0
  154. data/test/dummy/tmp/generators/config/initializers/devise_token_auth.rb +50 -0
  155. data/test/dummy/tmp/generators/db/migrate/20181030122248_devise_token_auth_create_users.rb +56 -0
  156. data/test/factories/users.rb +40 -0
  157. data/test/lib/devise_token_auth/blacklist_test.rb +11 -0
  158. data/test/lib/devise_token_auth/url_test.rb +26 -0
  159. data/test/lib/generators/devise_token_auth/install_generator_test.rb +217 -0
  160. data/test/lib/generators/devise_token_auth/install_generator_with_namespace_test.rb +222 -0
  161. data/test/lib/generators/devise_token_auth/install_views_generator_test.rb +25 -0
  162. data/test/models/concerns/mongoid_support_test.rb +31 -0
  163. data/test/models/only_email_user_test.rb +37 -0
  164. data/test/models/user_test.rb +140 -0
  165. data/test/support/controllers/routes.rb +43 -0
  166. data/test/test_helper.rb +103 -0
  167. metadata +443 -0
@@ -0,0 +1,284 @@
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
+
17
+ @auth_headers = @resource.create_new_auth_token
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_mang',
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_mang' do
41
+ assert_equal @resource, @controller.current_mang
42
+ end
43
+
44
+ it 'should define mang_signed_in?' do
45
+ assert @controller.mang_signed_in?
46
+ end
47
+
48
+ it 'should not define current_user' do
49
+ refute_equal @resource, @controller.current_user
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
+ 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
+
98
+ describe 'failed request' do
99
+ before do
100
+ get '/demo/members_only_mang',
101
+ params: {},
102
+ headers: @auth_headers.merge('access-token' => 'bogus')
103
+ end
104
+
105
+ it 'should not return any auth headers' do
106
+ refute response.headers['access-token']
107
+ end
108
+
109
+ it 'should return error: unauthorized status' do
110
+ assert_equal 401, response.status
111
+ end
112
+ end
113
+
114
+ describe 'disable change_headers_on_each_request' do
115
+ before do
116
+ DeviseTokenAuth.change_headers_on_each_request = false
117
+ @resource.reload
118
+ age_token(@resource, @client_id)
119
+
120
+ get '/demo/members_only_mang',
121
+ params: {},
122
+ headers: @auth_headers
123
+
124
+ @first_is_batch_request = assigns(:is_batch_request)
125
+ @first_user = assigns(:resource).dup
126
+ @first_access_token = response.headers['access-token']
127
+ @first_response_status = response.status
128
+
129
+ @resource.reload
130
+ age_token(@resource, @client_id)
131
+
132
+ # use expired auth header
133
+ get '/demo/members_only_mang',
134
+ params: {},
135
+ headers: @auth_headers
136
+
137
+ @second_is_batch_request = assigns(:is_batch_request)
138
+ @second_user = assigns(:resource).dup
139
+ @second_access_token = response.headers['access-token']
140
+ @second_response_status = response.status
141
+ end
142
+
143
+ after do
144
+ DeviseTokenAuth.change_headers_on_each_request = true
145
+ end
146
+
147
+ it 'should allow the first request through' do
148
+ assert_equal 200, @first_response_status
149
+ end
150
+
151
+ it 'should allow the second request through' do
152
+ assert_equal 200, @second_response_status
153
+ end
154
+
155
+ it 'should return auth headers from the first request' do
156
+ assert @first_access_token
157
+ end
158
+
159
+ it 'should not treat either requests as batch requests' do
160
+ refute @first_is_batch_request
161
+ refute @second_is_batch_request
162
+ end
163
+
164
+ it 'should return auth headers from the second request' do
165
+ assert @second_access_token
166
+ end
167
+
168
+ it 'should define user during first request' do
169
+ assert @first_user
170
+ end
171
+
172
+ it 'should define user during second request' do
173
+ assert @second_user
174
+ end
175
+ end
176
+
177
+ describe 'batch requests' do
178
+ describe 'success' do
179
+ before do
180
+ age_token(@resource, @client_id)
181
+ # request.headers.merge!(@auth_headers)
182
+
183
+ get '/demo/members_only_mang',
184
+ params: {},
185
+ headers: @auth_headers
186
+
187
+ @first_is_batch_request = assigns(:is_batch_request)
188
+ @first_user = assigns(:resource)
189
+ @first_access_token = response.headers['access-token']
190
+
191
+ get '/demo/members_only_mang',
192
+ params: {},
193
+ headers: @auth_headers
194
+
195
+ @second_is_batch_request = assigns(:is_batch_request)
196
+ @second_user = assigns(:resource)
197
+ @second_access_token = response.headers['access-token']
198
+ end
199
+
200
+ it 'should allow both requests through' do
201
+ assert_equal 200, response.status
202
+ end
203
+
204
+ it 'should not treat the first request as a batch request' do
205
+ refute @first_is_batch_request
206
+ end
207
+
208
+ it 'should treat the second request as a batch request' do
209
+ assert @second_is_batch_request
210
+ end
211
+
212
+ it 'should return access token for first (non-batch) request' do
213
+ assert @first_access_token
214
+ end
215
+
216
+ it 'should not return auth headers for second (batched) requests' do
217
+ assert_equal ' ', @second_access_token
218
+ end
219
+ end
220
+
221
+ describe 'time out' do
222
+ before do
223
+ @resource.reload
224
+ age_token(@resource, @client_id)
225
+
226
+ get '/demo/members_only_mang',
227
+ params: {},
228
+ headers: @auth_headers
229
+
230
+ @first_is_batch_request = assigns(:is_batch_request)
231
+ @first_user = assigns(:resource).dup
232
+ @first_access_token = response.headers['access-token']
233
+ @first_response_status = response.status
234
+
235
+ @resource.reload
236
+ age_token(@resource, @client_id)
237
+
238
+ # use expired auth header
239
+ get '/demo/members_only_mang',
240
+ params: {},
241
+ headers: @auth_headers
242
+
243
+ @second_is_batch_request = assigns(:is_batch_request)
244
+ @second_user = assigns(:resource)
245
+ @second_access_token = response.headers['access-token']
246
+ @second_response_status = response.status
247
+ end
248
+
249
+ it 'should allow the first request through' do
250
+ assert_equal 200, @first_response_status
251
+ end
252
+
253
+ it 'should not allow the second request through' do
254
+ assert_equal 401, @second_response_status
255
+ end
256
+
257
+ it 'should not treat first request as batch request' do
258
+ refute @second_is_batch_request
259
+ end
260
+
261
+ it 'should return auth headers from the first request' do
262
+ assert @first_access_token
263
+ end
264
+
265
+ it 'should not treat second request as batch request' do
266
+ refute @second_is_batch_request
267
+ end
268
+
269
+ it 'should not return auth headers from the second request' do
270
+ refute @second_access_token
271
+ end
272
+
273
+ it 'should define user during first request' do
274
+ assert @first_user
275
+ end
276
+
277
+ it 'should not define user during second request' do
278
+ refute @second_user
279
+ end
280
+ end
281
+ end
282
+ end
283
+ end
284
+ end
@@ -0,0 +1,629 @@
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
+
18
+ @auth_headers = @resource.create_new_auth_token
19
+
20
+ @token = @auth_headers['access-token']
21
+ @client_id = @auth_headers['client']
22
+ @expiry = @auth_headers['expiry']
23
+ end
24
+
25
+ describe 'successful request' do
26
+ before do
27
+ # ensure that request is not treated as batch request
28
+ age_token(@resource, @client_id)
29
+
30
+ get '/demo/members_only',
31
+ params: {},
32
+ headers: @auth_headers
33
+
34
+ @resp_token = response.headers['access-token']
35
+ @resp_client_id = response.headers['client']
36
+ @resp_expiry = response.headers['expiry']
37
+ @resp_uid = response.headers['uid']
38
+ end
39
+
40
+ describe 'devise mappings' do
41
+ it 'should define current_user' do
42
+ assert_equal @resource, @controller.current_user
43
+ end
44
+
45
+ it 'should define user_signed_in?' do
46
+ assert @controller.user_signed_in?
47
+ end
48
+
49
+ it 'should not define current_mang' do
50
+ refute_equal @resource, @controller.current_mang
51
+ end
52
+
53
+ it 'should define render_authenticate_error' do
54
+ assert @controller.methods.include?(:render_authenticate_error)
55
+ end
56
+ end
57
+
58
+ it 'should return success status' do
59
+ assert_equal 200, response.status
60
+ end
61
+
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
+
99
+ describe 'failed request' do
100
+ before do
101
+ get '/demo/members_only',
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
+ describe 'disable change_headers_on_each_request' do
116
+ before do
117
+ DeviseTokenAuth.change_headers_on_each_request = false
118
+ @resource.reload
119
+ age_token(@resource, @client_id)
120
+
121
+ get '/demo/members_only',
122
+ params: {},
123
+ headers: @auth_headers
124
+
125
+ @first_is_batch_request = assigns(:is_batch_request)
126
+ @first_user = assigns(:resource).dup
127
+ @first_access_token = response.headers['access-token']
128
+ @first_response_status = response.status
129
+
130
+ @resource.reload
131
+ age_token(@resource, @client_id)
132
+
133
+ # use expired auth header
134
+ get '/demo/members_only',
135
+ params: {},
136
+ headers: @auth_headers
137
+
138
+ @second_is_batch_request = assigns(:is_batch_request)
139
+ @second_user = assigns(:resource).dup
140
+ @second_access_token = response.headers['access-token']
141
+ @second_response_status = response.status
142
+ end
143
+
144
+ after do
145
+ DeviseTokenAuth.change_headers_on_each_request = true
146
+ end
147
+
148
+ it 'should allow the first request through' do
149
+ assert_equal 200, @first_response_status
150
+ end
151
+
152
+ it 'should allow the second request through' do
153
+ assert_equal 200, @second_response_status
154
+ end
155
+
156
+ it 'should return auth headers from the first request' do
157
+ assert @first_access_token
158
+ end
159
+
160
+ it 'should not treat either requests as batch requests' do
161
+ refute @first_is_batch_request
162
+ refute @second_is_batch_request
163
+ end
164
+
165
+ it 'should return auth headers from the second request' do
166
+ assert @second_access_token
167
+ end
168
+
169
+ it 'should define user during first request' do
170
+ assert @first_user
171
+ end
172
+
173
+ it 'should define user during second request' do
174
+ assert @second_user
175
+ end
176
+ end
177
+
178
+ describe 'batch requests' do
179
+ describe 'success' do
180
+ before do
181
+ age_token(@resource, @client_id)
182
+ # request.headers.merge!(@auth_headers)
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
+
313
+ describe 'successful password change' do
314
+ before do
315
+ DeviseTokenAuth.remove_tokens_after_password_reset = true
316
+
317
+ # adding one more token to simulate another logged in device
318
+ @old_auth_headers = @auth_headers
319
+ @auth_headers = @resource.create_new_auth_token
320
+ age_token(@resource, @client_id)
321
+ assert @resource.tokens.count > 1
322
+
323
+ # password changed from new device
324
+ @resource.update_attributes(password: 'newsecret123',
325
+ password_confirmation: 'newsecret123')
326
+
327
+ get '/demo/members_only',
328
+ params: {},
329
+ headers: @auth_headers
330
+ end
331
+
332
+ after do
333
+ DeviseTokenAuth.remove_tokens_after_password_reset = false
334
+ end
335
+
336
+ it 'should have only one token' do
337
+ assert_equal 1, @resource.tokens.count
338
+ end
339
+
340
+ it 'new request should be successful' do
341
+ assert 200, response.status
342
+ end
343
+
344
+ describe 'another device should not be able to login' do
345
+ it 'should return forbidden status' do
346
+ get '/demo/members_only',
347
+ params: {},
348
+ headers: @old_auth_headers
349
+ assert 401, response.status
350
+ end
351
+ end
352
+ end
353
+
354
+ describe 'request including destroy of token' do
355
+ describe 'when change_headers_on_each_request is set to false' do
356
+ before do
357
+ DeviseTokenAuth.change_headers_on_each_request = false
358
+ age_token(@resource, @client_id)
359
+
360
+ get '/demo/members_only_remove_token',
361
+ params: {},
362
+ headers: @auth_headers
363
+ end
364
+
365
+ after do
366
+ DeviseTokenAuth.change_headers_on_each_request = true
367
+ end
368
+
369
+ it 'should not return auth-headers' do
370
+ refute response.headers['access-token']
371
+ end
372
+ end
373
+
374
+ describe 'when change_headers_on_each_request is set to true' do
375
+ before do
376
+ age_token(@resource, @client_id)
377
+ get '/demo/members_only_remove_token',
378
+ params: {},
379
+ headers: @auth_headers
380
+ end
381
+
382
+ it 'should not return auth-headers' do
383
+ refute response.headers['access-token']
384
+ end
385
+ end
386
+ end
387
+
388
+ describe 'when access-token name has been changed' do
389
+ before do
390
+ # ensure that request is not treated as batch request
391
+ DeviseTokenAuth.headers_names[:'access-token'] = 'new-access-token'
392
+ auth_headers_modified = @resource.create_new_auth_token
393
+ client_id = auth_headers_modified['client']
394
+ age_token(@resource, client_id)
395
+
396
+ get '/demo/members_only',
397
+ params: {},
398
+ headers: auth_headers_modified
399
+ @resp_token = response.headers['new-access-token']
400
+ end
401
+
402
+ it 'should have "new-access-token" header' do
403
+ assert @resp_token.present?
404
+ end
405
+
406
+ after do
407
+ DeviseTokenAuth.headers_names[:'access-token'] = 'access-token'
408
+ end
409
+ end
410
+
411
+ describe 'maximum concurrent devices per user' do
412
+ before do
413
+ # Set the max_number_of_devices to a lower number
414
+ # to expedite tests! (Default is 10)
415
+ DeviseTokenAuth.max_number_of_devices = 5
416
+ end
417
+
418
+ it 'should limit the maximum number of concurrent devices' do
419
+ # increment the number of devices until the maximum is exceeded
420
+ 1.upto(DeviseTokenAuth.max_number_of_devices + 1).each do |n|
421
+
422
+ assert_equal(
423
+ [n, DeviseTokenAuth.max_number_of_devices].min,
424
+ @resource.reload.tokens.length
425
+ )
426
+
427
+ # Add a new device (and token) ahead of the next iteration
428
+ @resource.create_new_auth_token
429
+
430
+ end
431
+ end
432
+
433
+ it 'should drop the oldest token when the maximum number of devices is exceeded' do
434
+ # create the maximum number of tokens
435
+ 1.upto(DeviseTokenAuth.max_number_of_devices).each do
436
+ @resource.create_new_auth_token
437
+ end
438
+
439
+ # get the oldest token client_id
440
+ oldest_client_id, = @resource.reload.tokens.min_by do |cid, v|
441
+ v[:expiry] || v['expiry']
442
+ end # => [ 'CLIENT_ID', {token: ...} ]
443
+
444
+ # create another token, thereby dropping the oldest token
445
+ @resource.create_new_auth_token
446
+
447
+ assert_not_includes @resource.reload.tokens.keys, oldest_client_id
448
+ end
449
+
450
+ after do
451
+ DeviseTokenAuth.max_number_of_devices = 10
452
+ end
453
+ end
454
+ end
455
+
456
+ describe 'bypass_sign_in' do
457
+ before do
458
+ @resource = create(:user)
459
+
460
+ @auth_headers = @resource.create_new_auth_token
461
+
462
+ @token = @auth_headers['access-token']
463
+ @client_id = @auth_headers['client']
464
+ @expiry = @auth_headers['expiry']
465
+ end
466
+ describe 'is default value (true)' do
467
+ before do
468
+ age_token(@resource, @client_id)
469
+
470
+ get '/demo/members_only', params: {}, headers: @auth_headers
471
+
472
+ @access_token = response.headers['access-token']
473
+ @response_status = response.status
474
+ end
475
+
476
+ it 'should allow the request through' do
477
+ assert_equal 200, @response_status
478
+ end
479
+
480
+ it 'should return auth headers' do
481
+ assert @access_token
482
+ end
483
+
484
+ it 'should set current user' do
485
+ assert_equal @controller.current_user, @resource
486
+ end
487
+ end
488
+ describe 'is false' do
489
+ before do
490
+ DeviseTokenAuth.bypass_sign_in = false
491
+ age_token(@resource, @client_id)
492
+
493
+ get '/demo/members_only', params: {}, headers: @auth_headers
494
+
495
+ @access_token = response.headers['access-token']
496
+ @response_status = response.status
497
+
498
+ DeviseTokenAuth.bypass_sign_in = true
499
+ end
500
+
501
+ it 'should not allow the request through' do
502
+ refute_equal 200, @response_status
503
+ end
504
+
505
+ it 'should not return auth headers from the first request' do
506
+ assert_nil @access_token
507
+ end
508
+ end
509
+ end
510
+
511
+ describe 'enable_standard_devise_support' do
512
+ before do
513
+ @resource = create(:user, :confirmed)
514
+ @auth_headers = @resource.create_new_auth_token
515
+ DeviseTokenAuth.enable_standard_devise_support = true
516
+ end
517
+
518
+ describe 'Existing Warden authentication' do
519
+ before do
520
+ @resource = create(:user, :confirmed)
521
+ login_as(@resource, scope: :user)
522
+
523
+ # no auth headers sent, testing that warden authenticates correctly.
524
+ get '/demo/members_only',
525
+ params: {},
526
+ headers: nil
527
+
528
+ @resp_token = response.headers['access-token']
529
+ @resp_client_id = response.headers['client']
530
+ @resp_expiry = response.headers['expiry']
531
+ @resp_uid = response.headers['uid']
532
+ end
533
+
534
+ describe 'devise mappings' do
535
+ it 'should define current_user' do
536
+ assert_equal @resource, @controller.current_user
537
+ end
538
+
539
+ it 'should define user_signed_in?' do
540
+ assert @controller.user_signed_in?
541
+ end
542
+
543
+ it 'should not define current_mang' do
544
+ refute_equal @resource, @controller.current_mang
545
+ end
546
+
547
+ end
548
+
549
+ it 'should return success status' do
550
+ assert_equal 200, response.status
551
+ end
552
+
553
+ it 'should receive new token after successful request' do
554
+ assert @resp_token
555
+ end
556
+
557
+ it 'should set the token expiry in the auth header' do
558
+ assert @resp_expiry
559
+ end
560
+
561
+ it 'should return the client id in the auth header' do
562
+ assert @resp_client_id
563
+ end
564
+
565
+ it "should return the user's uid in the auth header" do
566
+ assert @resp_uid
567
+ end
568
+ end
569
+
570
+ describe 'existing Warden authentication with ignored token data' do
571
+ before do
572
+ @resource = create(:user, :confirmed)
573
+ login_as(@resource, scope: :user)
574
+
575
+ get '/demo/members_only',
576
+ params: {},
577
+ headers: @auth_headers
578
+
579
+ @resp_token = response.headers['access-token']
580
+ @resp_client_id = response.headers['client']
581
+ @resp_expiry = response.headers['expiry']
582
+ @resp_uid = response.headers['uid']
583
+ end
584
+
585
+ describe 'devise mappings' do
586
+ it 'should define current_user' do
587
+ assert_equal @resource, @controller.current_user
588
+ end
589
+
590
+ it 'should define user_signed_in?' do
591
+ assert @controller.user_signed_in?
592
+ end
593
+
594
+ it 'should not define current_mang' do
595
+ refute_equal @resource, @controller.current_mang
596
+ end
597
+ end
598
+
599
+ it 'should return success status' do
600
+ assert_equal 200, response.status
601
+ end
602
+
603
+ it 'should receive new token after successful request' do
604
+ assert @resp_token
605
+ end
606
+
607
+ it 'should set the token expiry in the auth header' do
608
+ assert @resp_expiry
609
+ end
610
+
611
+ it 'should return the client id in the auth header' do
612
+ assert @resp_client_id
613
+ end
614
+
615
+ it "should not use the existing token's client" do
616
+ refute_equal @auth_headers['client'], @resp_client_id
617
+ end
618
+
619
+ it "should return the user's uid in the auth header" do
620
+ assert @resp_uid
621
+ end
622
+
623
+ it "should not return the token user's uid in the auth header" do
624
+ refute_equal @resp_uid, @auth_headers['uid']
625
+ end
626
+ end
627
+ end
628
+ end
629
+ end