Skip to content

Move create_token in user concern out of included #1415

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions app/models/devise_token_auth/concerns/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,19 +88,19 @@ def send_unlock_instructions(opts = {})
send_devise_notification(:unlock_instructions, raw, opts)
raw
end
end

def create_token(client: nil, lifespan: nil, cost: nil, **token_extras)
token = DeviseTokenAuth::TokenFactory.create(client: client, lifespan: lifespan, cost: cost)
def create_token(client: nil, lifespan: nil, cost: nil, **token_extras)
token = DeviseTokenAuth::TokenFactory.create(client: client, lifespan: lifespan, cost: cost)

tokens[token.client] = {
token: token.token_hash,
expiry: token.expiry
}.merge!(token_extras)
tokens[token.client] = {
token: token.token_hash,
expiry: token.expiry
}.merge!(token_extras)

clean_old_tokens
clean_old_tokens

token
end
token
end

def valid_token?(token, client = 'default')
Expand Down
53 changes: 53 additions & 0 deletions test/models/user_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -126,5 +126,58 @@ class UserTest < ActiveSupport::TestCase
assert @resource.save
end
end

describe 'create_token method' do
before do
@resource = create(:user, :confirmed)
end

test 'creates token with default parameters' do
token = @resource.create_token

assert_not_nil token
assert_not_nil token.client
assert_not_nil token.token
assert_not_nil token.token_hash
assert_not_nil token.expiry
assert @resource.tokens[token.client].present?
assert_equal @resource.tokens[token.client]['token'], token.token_hash
assert_equal @resource.tokens[token.client]['expiry'], token.expiry
end

test 'creates token with custom client' do
custom_client = 'custom_client_id'
token = @resource.create_token(client: custom_client)

assert_equal custom_client, token.client
assert @resource.tokens[custom_client].present?
end

test 'creates token with custom lifespan' do
custom_lifespan = 1.hour
token = @resource.create_token(lifespan: custom_lifespan)

expected_expiry = (Time.zone.now + custom_lifespan).to_i
assert_in_delta expected_expiry, token.expiry, 2
end

test 'creates token with token extras' do
extras = { custom_field: 'custom_value' }
token = @resource.create_token(**extras)

assert_equal 'custom_value', @resource.tokens[token.client]['custom_field']
end

test 'cleans old tokens when creating new token' do
# Create maximum number of tokens plus one more
max_tokens = DeviseTokenAuth.max_number_of_devices
(max_tokens + 1).times do |i|
@resource.create_token(client: "client_#{i}")
end

# Should not exceed max number of tokens
assert_operator @resource.tokens.length, :<=, max_tokens
end
end
end
end
Loading