2015-12-23 02:04:40 +05:30
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
shared_examples 'TokenAuthenticatable' do
|
|
|
|
describe 'dynamically defined methods' do
|
2016-01-14 18:37:52 +05:30
|
|
|
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-23 02:04:40 +05:30
|
|
|
it { expect(described_class).to respond_to("find_by_#{token_field}") }
|
|
|
|
it { is_expected.to respond_to("ensure_#{token_field}") }
|
2017-08-17 22:00:37 +05:30
|
|
|
it { is_expected.to respond_to("set_#{token_field}") }
|
2015-12-23 02:04:40 +05:30
|
|
|
it { is_expected.to respond_to("reset_#{token_field}!") }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe User, 'TokenAuthenticatable' do
|
|
|
|
let(:token_field) { :authentication_token }
|
|
|
|
it_behaves_like 'TokenAuthenticatable'
|
|
|
|
|
|
|
|
describe 'ensures authentication token' do
|
|
|
|
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
|
2016-01-14 18:37:52 +05:30
|
|
|
describe 'token field accessor' do
|
|
|
|
subject { described_class.new.send(token_field) }
|
2016-06-16 23:09:34 +05:30
|
|
|
it { is_expected.not_to be_blank }
|
2016-01-14 18:37:52 +05:30
|
|
|
end
|
2015-12-23 02:04:40 +05:30
|
|
|
|
|
|
|
describe 'ensured token' do
|
|
|
|
subject { described_class.new.send("ensure_#{token_field}") }
|
|
|
|
|
|
|
|
it { is_expected.to be_a String }
|
2016-06-16 23:09:34 +05:30
|
|
|
it { is_expected.not_to be_blank }
|
2015-12-23 02:04:40 +05:30
|
|
|
end
|
2016-01-14 18:37:52 +05:30
|
|
|
|
|
|
|
describe 'ensured! token' do
|
|
|
|
subject { described_class.new.send("ensure_#{token_field}!") }
|
|
|
|
|
2016-09-13 17:45:13 +05:30
|
|
|
it 'persists new token' do
|
2016-01-14 18:37:52 +05:30
|
|
|
expect(subject).to eq described_class.current[token_field]
|
|
|
|
end
|
|
|
|
end
|
2015-12-23 02:04:40 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
context 'token is generated' do
|
2017-09-10 17:25:29 +05:30
|
|
|
before do
|
|
|
|
subject.send("reset_#{token_field}!")
|
|
|
|
end
|
|
|
|
|
2016-06-16 23:09:34 +05:30
|
|
|
it 'persists a new token' do
|
2016-01-14 18:37:52 +05:30
|
|
|
expect(subject.send(:read_attribute, token_field)).to be_a String
|
|
|
|
end
|
2015-12-23 02:04:40 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
describe 'setting new token' do
|
|
|
|
subject { described_class.new.send("set_#{token_field}", '0123456789') }
|
|
|
|
|
|
|
|
it { is_expected.to eq '0123456789' }
|
|
|
|
end
|
|
|
|
|
2015-12-23 02:04:40 +05:30
|
|
|
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
|