-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Max number of devices in new session #1115
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
a9b25dc
065e812
676c77a
04afcda
3bda3d5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -75,23 +75,46 @@ class DeviseTokenAuth::SessionsControllerTest < ActionController::TestCase | |
|
||
describe "with multiple clients and headers don't change in each request" do | ||
before do | ||
DeviseTokenAuth.max_number_of_devices = 1 | ||
# Set the max_number_of_devices to a lower number | ||
# to expedite tests! (Default is 10) | ||
DeviseTokenAuth.max_number_of_devices = 2 | ||
DeviseTokenAuth.change_headers_on_each_request = false | ||
@tokens = [] | ||
(1..3).each do |n| | ||
post :create, | ||
params: { | ||
email: @existing_user.email, | ||
password: 'secret123' | ||
} | ||
@tokens << @existing_user.reload.tokens | ||
|
||
@user_session_params = { | ||
email: @existing_user.email, | ||
password: 'secret123' | ||
} | ||
end | ||
|
||
test '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| | ||
initial_tokens = @existing_user.reload.tokens | ||
|
||
assert_equal( | ||
[n, DeviseTokenAuth.max_number_of_devices].min, | ||
@existing_user.reload.tokens.length | ||
) | ||
|
||
# Already have the max number of devices | ||
post :create, params: @user_session_params | ||
|
||
# A session for a new device maintains the max number of concurrent devices | ||
refute_equal initial_tokens, @existing_user.reload.tokens | ||
end | ||
end | ||
|
||
test 'should delete old tokens' do | ||
current_tokens = @existing_user.reload.tokens | ||
assert_equal 1, current_tokens.count | ||
assert_equal @tokens.pop.keys.first, current_tokens.keys.first | ||
test 'should drop old tokens when max number of devices is exceeded' do | ||
1.upto(DeviseTokenAuth.max_number_of_devices).each do |n| | ||
post :create, params: @user_session_params | ||
end | ||
|
||
oldest_token, _ = @existing_user.reload.tokens \ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. no need for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That is purely convention, although I used it incorrectly... https://github.com/bbatsov/ruby-style-guide#trailing-underscore-variables It should either be: oldest_token, = \
@existing_user.reload.tokens \
.min_by { |cid, v| v[:expiry] || v["expiry"] } # => [ <"client_id">, {...<token>...} ] or oldest_client_id, _oldest_token = \
@existing_user.reload.tokens \
.min_by { |cid, v| v[:expiry] || v["expiry"] } # => [ <"client_id">, {...<token>...} ] If you really truly think I should remove the On a different note, I've been itching to create an issue to get rubocop setup on this repo, so that stylistic issues like this are a non-issue. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You're right, thank you for the long explanation, and if you enable rubocop, there will be tons of issues, that's why we have Code Climate that analyzes only the differences. I will create an issue for it 😄 EDIT: the issue is #1124 |
||
.min_by { |cid, v| v[:expiry] || v["expiry"] } | ||
|
||
post :create, params: @user_session_params | ||
|
||
assert_not_includes @existing_user.reload.tokens.keys, oldest_token | ||
end | ||
|
||
after do | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
update_auth_header
should use this, but if you say so, we can remove itThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rather confusingly, there are two methods with that name.
the model concern:
devise_token_auth/app/models/devise_token_auth/concerns/user.rb
Lines 199 to 205 in 38d27cf
and the controller concern:
devise_token_auth/app/controllers/devise_token_auth/concerns/set_user_by_token.rb
Lines 96 to 154 in 38d27cf
I'll assume you mean the one in the controller concern (the same file as this change)...
Regardless, I'm not sure I understand what you mean. This is inside a conditional block for the use case of when
DeviseTokenAuth.enable_standard_devise_support = true
and the user has already authenticated with Devise in the session via Warden. There is no need for an auth token in that case, so why would we create one here?