2019-03-30 07:23:56 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-12-10 11:44:40 +00:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 18:09:03 +00:00
|
|
|
RSpec.shared_examples 'TokenAuthenticatable' do
|
2015-12-10 11:44:40 +00:00
|
|
|
describe 'dynamically defined methods' do
|
|
|
|
it { expect(described_class).to respond_to("find_by_#{token_field}") }
|
|
|
|
it { is_expected.to respond_to("ensure_#{token_field}") }
|
2016-10-02 22:30:44 +00:00
|
|
|
it { is_expected.to respond_to("set_#{token_field}") }
|
2015-12-10 11:44:40 +00:00
|
|
|
it { is_expected.to respond_to("reset_#{token_field}!") }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-06-24 18:09:03 +00:00
|
|
|
RSpec.describe User, 'TokenAuthenticatable' do
|
2018-05-31 14:01:04 +00:00
|
|
|
let(:token_field) { :feed_token }
|
2019-12-19 00:08:01 +00:00
|
|
|
|
2015-12-10 11:44:40 +00:00
|
|
|
it_behaves_like 'TokenAuthenticatable'
|
|
|
|
|
2015-12-11 13:37:54 +00:00
|
|
|
describe 'ensures authentication token' do
|
2015-12-10 11:44:40 +00:00
|
|
|
subject { create(:user).send(token_field) }
|
2019-10-18 11:11:44 +00:00
|
|
|
|
2015-12-10 11:44:40 +00:00
|
|
|
it { is_expected.to be_a String }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-06-24 18:09:03 +00:00
|
|
|
RSpec.describe ApplicationSetting, 'TokenAuthenticatable' do
|
2015-12-10 11:44:40 +00:00
|
|
|
let(:token_field) { :runners_registration_token }
|
2018-11-15 14:17:32 +00:00
|
|
|
let(:settings) { described_class.new }
|
|
|
|
|
2015-12-10 11:44:40 +00:00
|
|
|
it_behaves_like 'TokenAuthenticatable'
|
|
|
|
|
|
|
|
describe 'generating new token' do
|
|
|
|
context 'token is not generated yet' do
|
2015-12-23 09:47:18 +00:00
|
|
|
describe 'token field accessor' do
|
2018-11-15 14:17:32 +00:00
|
|
|
subject { settings.send(token_field) }
|
|
|
|
|
2016-05-23 23:37:59 +00:00
|
|
|
it { is_expected.not_to be_blank }
|
2015-12-23 09:47:18 +00:00
|
|
|
end
|
2015-12-11 13:37:54 +00:00
|
|
|
|
2018-11-15 14:17:32 +00:00
|
|
|
describe "ensure_runners_registration_token" do
|
|
|
|
subject { settings.send("ensure_#{token_field}") }
|
2015-12-11 13:37:54 +00:00
|
|
|
|
|
|
|
it { is_expected.to be_a String }
|
2016-05-23 23:37:59 +00:00
|
|
|
it { is_expected.not_to be_blank }
|
2018-11-15 14:17:32 +00:00
|
|
|
|
|
|
|
it 'does not persist token' do
|
|
|
|
expect(settings).not_to be_persisted
|
|
|
|
end
|
2015-12-23 09:47:18 +00:00
|
|
|
end
|
|
|
|
|
2018-11-15 14:17:32 +00:00
|
|
|
describe 'ensure_runners_registration_token!' do
|
|
|
|
subject { settings.send("ensure_#{token_field}!") }
|
|
|
|
|
|
|
|
it 'persists new token as an encrypted string' do
|
|
|
|
expect(subject).to eq settings.reload.runners_registration_token
|
|
|
|
expect(settings.read_attribute('runners_registration_token_encrypted'))
|
2021-02-02 00:09:14 +00:00
|
|
|
.to eq Gitlab::CryptoHelper.aes256_gcm_encrypt(subject, nonce: Gitlab::CryptoHelper::AES256_GCM_IV_STATIC)
|
2018-11-15 14:17:32 +00:00
|
|
|
expect(settings).to be_persisted
|
|
|
|
end
|
2015-12-23 08:29:04 +00:00
|
|
|
|
2018-11-15 14:17:32 +00:00
|
|
|
it 'does not persist token in a clear text' do
|
|
|
|
expect(subject).not_to eq settings.reload
|
|
|
|
.read_attribute('runners_registration_token_encrypted')
|
2015-12-23 08:29:04 +00:00
|
|
|
end
|
2015-12-11 13:37:54 +00:00
|
|
|
end
|
2015-12-10 11:44:40 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'token is generated' do
|
2017-06-14 18:18:56 +00:00
|
|
|
before do
|
2018-11-15 14:17:32 +00:00
|
|
|
settings.send("reset_#{token_field}!")
|
2017-06-14 18:18:56 +00:00
|
|
|
end
|
|
|
|
|
2016-05-23 18:16:35 +00:00
|
|
|
it 'persists a new token' do
|
2018-11-15 14:17:32 +00:00
|
|
|
expect(settings.runners_registration_token).to be_a String
|
2015-12-23 09:47:18 +00:00
|
|
|
end
|
2015-12-10 11:44:40 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-10-02 22:30:44 +00:00
|
|
|
describe 'setting new token' do
|
2018-11-15 14:17:32 +00:00
|
|
|
subject { settings.send("set_#{token_field}", '0123456789') }
|
2016-10-02 22:30:44 +00:00
|
|
|
|
|
|
|
it { is_expected.to eq '0123456789' }
|
|
|
|
end
|
|
|
|
|
2015-12-10 11:44:40 +00:00
|
|
|
describe 'multiple token fields' do
|
2018-10-29 16:06:45 +00:00
|
|
|
before(:all) do
|
2015-12-10 11:44:40 +00:00
|
|
|
described_class.send(:add_authentication_token_field, :yet_another_token)
|
|
|
|
end
|
|
|
|
|
2018-10-29 16:06:45 +00:00
|
|
|
it { is_expected.to respond_to(:ensure_runners_registration_token) }
|
|
|
|
it { is_expected.to respond_to(:ensure_yet_another_token) }
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'setting same token field multiple times' do
|
|
|
|
subject { described_class.send(:add_authentication_token_field, :runners_registration_token) }
|
|
|
|
|
|
|
|
it 'raises error' do
|
|
|
|
expect {subject}.to raise_error(ArgumentError)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-06-24 18:09:03 +00:00
|
|
|
RSpec.describe PersonalAccessToken, 'TokenAuthenticatable' do
|
2019-02-20 11:39:37 +00:00
|
|
|
shared_examples 'changes personal access token' do
|
|
|
|
it 'sets new token' do
|
|
|
|
subject
|
|
|
|
|
2020-12-15 15:09:59 +00:00
|
|
|
expect(personal_access_token.token).to eq("#{PersonalAccessToken.token_prefix}#{token_value}")
|
|
|
|
expect(personal_access_token.token_digest).to eq(Gitlab::CryptoHelper.sha256("#{PersonalAccessToken.token_prefix}#{token_value}"))
|
2019-02-20 11:39:37 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
shared_examples 'does not change personal access token' do
|
|
|
|
it 'sets new token' do
|
|
|
|
subject
|
|
|
|
|
|
|
|
expect(personal_access_token.token).to be(nil)
|
|
|
|
expect(personal_access_token.token_digest).to eq(token_digest)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-10-29 16:06:45 +00:00
|
|
|
let(:token_value) { 'token' }
|
2019-02-20 11:39:37 +00:00
|
|
|
let(:token_digest) { Gitlab::CryptoHelper.sha256(token_value) }
|
2018-10-29 16:06:45 +00:00
|
|
|
let(:user) { create(:user) }
|
|
|
|
let(:personal_access_token) do
|
2019-02-20 11:39:37 +00:00
|
|
|
described_class.new(name: 'test-pat-01',
|
2018-10-29 16:06:45 +00:00
|
|
|
user_id: user.id,
|
|
|
|
scopes: [:api],
|
|
|
|
token_digest: token_digest)
|
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
|
|
|
allow(Devise).to receive(:friendly_token).and_return(token_value)
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '.find_by_token' do
|
|
|
|
subject { PersonalAccessToken.find_by_token(token_value) }
|
|
|
|
|
2019-02-20 11:39:37 +00:00
|
|
|
it 'finds the token' do
|
2020-09-22 18:09:54 +00:00
|
|
|
personal_access_token.save!
|
2018-10-29 16:06:45 +00:00
|
|
|
|
2019-02-20 11:39:37 +00:00
|
|
|
expect(subject).to eq(personal_access_token)
|
2018-10-29 16:06:45 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#set_token' do
|
|
|
|
let(:new_token_value) { 'new-token' }
|
|
|
|
|
2019-02-20 11:39:37 +00:00
|
|
|
subject { personal_access_token.set_token(new_token_value) }
|
2018-10-29 16:06:45 +00:00
|
|
|
|
2019-02-20 11:39:37 +00:00
|
|
|
it 'sets new token' do
|
|
|
|
subject
|
2018-10-29 16:06:45 +00:00
|
|
|
|
2019-02-20 11:39:37 +00:00
|
|
|
expect(personal_access_token.token).to eq(new_token_value)
|
|
|
|
expect(personal_access_token.token_digest).to eq(Gitlab::CryptoHelper.sha256(new_token_value))
|
2018-10-29 16:06:45 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#ensure_token' do
|
|
|
|
subject { personal_access_token.ensure_token }
|
|
|
|
|
2019-02-20 11:39:37 +00:00
|
|
|
context 'token_digest does not exist' do
|
2018-10-29 16:06:45 +00:00
|
|
|
let(:token_digest) { nil }
|
|
|
|
|
2019-02-20 11:39:37 +00:00
|
|
|
it_behaves_like 'changes personal access token'
|
2018-10-29 16:06:45 +00:00
|
|
|
end
|
|
|
|
|
2019-02-20 11:39:37 +00:00
|
|
|
context 'token_digest already generated' do
|
|
|
|
let(:token_digest) { 's3cr3t' }
|
2018-10-29 16:06:45 +00:00
|
|
|
|
2019-02-20 11:39:37 +00:00
|
|
|
it_behaves_like 'does not change personal access token'
|
2018-10-29 16:06:45 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#ensure_token!' do
|
|
|
|
subject { personal_access_token.ensure_token! }
|
|
|
|
|
2019-02-20 11:39:37 +00:00
|
|
|
context 'token_digest does not exist' do
|
2018-10-29 16:06:45 +00:00
|
|
|
let(:token_digest) { nil }
|
|
|
|
|
2019-02-20 11:39:37 +00:00
|
|
|
it_behaves_like 'changes personal access token'
|
2018-10-29 16:06:45 +00:00
|
|
|
end
|
|
|
|
|
2019-02-20 11:39:37 +00:00
|
|
|
context 'token_digest already generated' do
|
|
|
|
let(:token_digest) { 's3cr3t' }
|
2018-10-29 16:06:45 +00:00
|
|
|
|
2019-02-20 11:39:37 +00:00
|
|
|
it_behaves_like 'does not change personal access token'
|
2018-10-29 16:06:45 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#reset_token!' do
|
|
|
|
subject { personal_access_token.reset_token! }
|
|
|
|
|
2019-02-20 11:39:37 +00:00
|
|
|
context 'token_digest does not exist' do
|
2018-10-29 16:06:45 +00:00
|
|
|
let(:token_digest) { nil }
|
|
|
|
|
2019-02-20 11:39:37 +00:00
|
|
|
it_behaves_like 'changes personal access token'
|
2018-10-29 16:06:45 +00:00
|
|
|
end
|
|
|
|
|
2019-02-20 11:39:37 +00:00
|
|
|
context 'token_digest already generated' do
|
|
|
|
let(:token_digest) { 's3cr3t' }
|
2018-10-29 16:06:45 +00:00
|
|
|
|
2019-02-20 11:39:37 +00:00
|
|
|
it_behaves_like 'changes personal access token'
|
2015-12-10 11:44:40 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2018-12-06 09:42:18 +00:00
|
|
|
|
2020-06-24 18:09:03 +00:00
|
|
|
RSpec.describe Ci::Build, 'TokenAuthenticatable' do
|
2018-12-06 09:42:18 +00:00
|
|
|
let(:token_field) { :token }
|
|
|
|
let(:build) { FactoryBot.build(:ci_build) }
|
|
|
|
|
|
|
|
it_behaves_like 'TokenAuthenticatable'
|
|
|
|
|
|
|
|
describe 'generating new token' do
|
|
|
|
context 'token is not generated yet' do
|
|
|
|
describe 'token field accessor' do
|
|
|
|
it 'makes it possible to access token' do
|
|
|
|
expect(build.token).to be_nil
|
|
|
|
|
|
|
|
build.save!
|
|
|
|
|
|
|
|
expect(build.token).to be_present
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "ensure_token" do
|
|
|
|
subject { build.ensure_token }
|
|
|
|
|
|
|
|
it { is_expected.to be_a String }
|
|
|
|
it { is_expected.not_to be_blank }
|
|
|
|
|
|
|
|
it 'does not persist token' do
|
|
|
|
expect(build).not_to be_persisted
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'ensure_token!' do
|
|
|
|
it 'persists a new token' do
|
|
|
|
expect(build.ensure_token!).to eq build.reload.token
|
|
|
|
expect(build).to be_persisted
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'persists new token as an encrypted string' do
|
|
|
|
build.ensure_token!
|
|
|
|
|
2021-02-02 00:09:14 +00:00
|
|
|
encrypted = Gitlab::CryptoHelper.aes256_gcm_encrypt(build.token, nonce: Gitlab::CryptoHelper::AES256_GCM_IV_STATIC)
|
2018-12-06 09:42:18 +00:00
|
|
|
|
|
|
|
expect(build.read_attribute('token_encrypted')).to eq encrypted
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not persist a token in a clear text' do
|
|
|
|
build.ensure_token!
|
|
|
|
|
|
|
|
expect(build.read_attribute('token')).to be_nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#reset_token!' do
|
|
|
|
it 'persists a new token' do
|
|
|
|
build.save!
|
|
|
|
|
|
|
|
build.token.yield_self do |previous_token|
|
|
|
|
build.reset_token!
|
|
|
|
|
|
|
|
expect(build.token).not_to eq previous_token
|
|
|
|
expect(build.token).to be_a String
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'setting a new token' do
|
|
|
|
subject { build.set_token('0123456789') }
|
|
|
|
|
|
|
|
it 'returns the token' do
|
|
|
|
expect(subject).to eq '0123456789'
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'writes a new encrypted token' do
|
|
|
|
expect(build.read_attribute('token_encrypted')).to be_nil
|
|
|
|
expect(subject).to eq '0123456789'
|
|
|
|
expect(build.read_attribute('token_encrypted')).to be_present
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not write a new cleartext token' do
|
|
|
|
expect(build.read_attribute('token')).to be_nil
|
|
|
|
expect(subject).to eq '0123456789'
|
|
|
|
expect(build.read_attribute('token')).to be_nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|