Commit 917effb7 authored by Grzegorz Bizon's avatar Grzegorz Bizon

Make sure that token `ensure_*` method always returns a token

parent 28ad40d9
...@@ -17,8 +17,12 @@ module TokenAuthenticatable ...@@ -17,8 +17,12 @@ module TokenAuthenticatable
end end
define_method("ensure_#{token_field}") do define_method("ensure_#{token_field}") do
write_attribute(token_field, generate_token_for(token_field)) if current_token = read_attribute(token_field)
read_attribute(token_field).blank? if current_token.blank?
write_attribute(token_field, generate_token_for(token_field))
else
current_token
end
end end
define_method("reset_#{token_field}!") do define_method("reset_#{token_field}!") do
......
...@@ -13,7 +13,7 @@ describe User, 'TokenAuthenticatable' do ...@@ -13,7 +13,7 @@ describe User, 'TokenAuthenticatable' do
let(:token_field) { :authentication_token } let(:token_field) { :authentication_token }
it_behaves_like 'TokenAuthenticatable' it_behaves_like 'TokenAuthenticatable'
describe 'ensured authentication token' do describe 'ensures authentication token' do
subject { create(:user).send(token_field) } subject { create(:user).send(token_field) }
it { is_expected.to be_a String } it { is_expected.to be_a String }
end end
...@@ -29,6 +29,13 @@ describe ApplicationSetting, 'TokenAuthenticatable' do ...@@ -29,6 +29,13 @@ describe ApplicationSetting, 'TokenAuthenticatable' do
context 'token is not generated yet' do context 'token is not generated yet' do
it { expect(token).to be nil } it { expect(token).to be nil }
describe 'ensured token' do
subject { described_class.new.send("ensure_#{token_field}") }
it { is_expected.to be_a String }
it { is_expected.to_not be_blank }
end
end end
context 'token is generated' do context 'token is generated' do
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment