@@ -407,6 +407,53 @@ class DemoUserControllerTest < ActionDispatch::IntegrationTest
407
407
DeviseTokenAuth . headers_names [ :'access-token' ] = 'access-token'
408
408
end
409
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
+
417
+ # @max_devices = DeviseTokenAuth.max_number_of_devices
418
+ end
419
+
420
+ it 'should limit the maximum number of concurrent devices' do
421
+ # increment the number of devices until the maximum is exceeded
422
+ 1 . upto ( DeviseTokenAuth . max_number_of_devices + 1 ) . each do |n |
423
+ # initial_tokens = @resource.reload.tokens
424
+
425
+ assert_equal (
426
+ [ n , DeviseTokenAuth . max_number_of_devices ] . min ,
427
+ @resource . reload . tokens . length
428
+ )
429
+
430
+ # Add a new device (and token) ahead of the next iteration
431
+ @resource . create_new_auth_token
432
+
433
+ # refute_equal initial_tokens, @resource.reload.tokens
434
+ end
435
+ end
436
+
437
+ it 'should drop the oldest token when the maximum number of devices is exceeded' do
438
+ # create the maximum number of tokens
439
+ 1 . upto ( DeviseTokenAuth . max_number_of_devices ) . each do
440
+ @resource . create_new_auth_token
441
+ end
442
+
443
+ # get the oldest token
444
+ oldest_token , _ = @resource . reload . tokens \
445
+ . min_by { |cid , v | v [ :expiry ] || v [ "expiry" ] }
446
+
447
+ # create another token, thereby dropping the oldest token
448
+ @resource . create_new_auth_token
449
+
450
+ assert_not_includes @resource . reload . tokens . keys , oldest_token
451
+ end
452
+
453
+ after do
454
+ DeviseTokenAuth . max_number_of_devices = 10
455
+ end
456
+ end
410
457
end
411
458
412
459
describe 'bypass_sign_in' do
@@ -503,17 +550,8 @@ class DemoUserControllerTest < ActionDispatch::IntegrationTest
503
550
refute_equal @resource , @controller . current_mang
504
551
end
505
552
506
- it 'should increase the number of tokens by a factor of 2 up to 11' do
507
- @first_token = @resource . tokens . keys . first
508
553
509
- DeviseTokenAuth . max_number_of_devices = 11
510
- ( 1 ..10 ) . each do |n |
511
- assert_equal [ 11 , 2 * n ] . min , @resource . reload . tokens . keys . length
512
- get '/demo/members_only' , params : { } , headers : nil
513
- end
514
554
515
- assert_not_includes @resource . reload . tokens . keys , @first_token
516
- end
517
555
end
518
556
519
557
it 'should return success status' do
0 commit comments