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