2015-12-10 06:44:40 -05:00
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
shared_examples 'TokenAuthenticatable' do
|
|
|
|
describe 'dynamically defined methods' do
|
2015-12-23 04:47:18 -05:00
|
|
|
it { expect(described_class).to be_private_method_defined(:generate_token) }
|
|
|
|
it { expect(described_class).to be_private_method_defined(:write_new_token) }
|
2015-12-10 06:44:40 -05:00
|
|
|
it { expect(described_class).to respond_to("find_by_#{token_field}") }
|
|
|
|
it { is_expected.to respond_to("ensure_#{token_field}") }
|
2016-10-02 18:30:44 -04:00
|
|
|
it { is_expected.to respond_to("set_#{token_field}") }
|
2015-12-10 06:44:40 -05:00
|
|
|
it { is_expected.to respond_to("reset_#{token_field}!") }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe User, 'TokenAuthenticatable' do
|
|
|
|
let(:token_field) { :authentication_token }
|
|
|
|
it_behaves_like 'TokenAuthenticatable'
|
|
|
|
|
2015-12-11 08:37:54 -05:00
|
|
|
describe 'ensures authentication token' do
|
2015-12-10 06:44:40 -05:00
|
|
|
subject { create(:user).send(token_field) }
|
|
|
|
it { is_expected.to be_a String }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe ApplicationSetting, 'TokenAuthenticatable' do
|
|
|
|
let(:token_field) { :runners_registration_token }
|
|
|
|
it_behaves_like 'TokenAuthenticatable'
|
|
|
|
|
|
|
|
describe 'generating new token' do
|
|
|
|
context 'token is not generated yet' do
|
2015-12-23 04:47:18 -05:00
|
|
|
describe 'token field accessor' do
|
|
|
|
subject { described_class.new.send(token_field) }
|
2016-05-23 19:37:59 -04:00
|
|
|
it { is_expected.not_to be_blank }
|
2015-12-23 04:47:18 -05:00
|
|
|
end
|
2015-12-11 08:37:54 -05:00
|
|
|
|
|
|
|
describe 'ensured token' do
|
|
|
|
subject { described_class.new.send("ensure_#{token_field}") }
|
|
|
|
|
|
|
|
it { is_expected.to be_a String }
|
2016-05-23 19:37:59 -04:00
|
|
|
it { is_expected.not_to be_blank }
|
2015-12-23 04:47:18 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
describe 'ensured! token' do
|
|
|
|
subject { described_class.new.send("ensure_#{token_field}!") }
|
2015-12-23 03:29:04 -05:00
|
|
|
|
2016-08-01 11:00:44 -04:00
|
|
|
it 'persists new token' do
|
2015-12-23 03:29:04 -05:00
|
|
|
expect(subject).to eq described_class.current[token_field]
|
|
|
|
end
|
2015-12-11 08:37:54 -05:00
|
|
|
end
|
2015-12-10 06:44:40 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'token is generated' do
|
|
|
|
before { subject.send("reset_#{token_field}!") }
|
2016-05-23 14:16:35 -04:00
|
|
|
it 'persists a new token' do
|
2015-12-23 04:47:18 -05:00
|
|
|
expect(subject.send(:read_attribute, token_field)).to be_a String
|
|
|
|
end
|
2015-12-10 06:44:40 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-10-02 18:30:44 -04:00
|
|
|
describe 'setting new token' do
|
|
|
|
subject { described_class.new.send("set_#{token_field}", '0123456789') }
|
|
|
|
|
|
|
|
it { is_expected.to eq '0123456789' }
|
|
|
|
end
|
|
|
|
|
2015-12-10 06:44:40 -05:00
|
|
|
describe 'multiple token fields' do
|
|
|
|
before do
|
|
|
|
described_class.send(:add_authentication_token_field, :yet_another_token)
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '.token_fields' do
|
|
|
|
subject { described_class.authentication_token_fields }
|
|
|
|
it { is_expected.to include(:runners_registration_token, :yet_another_token) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|