2020-04-29 11:09:58 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 14:09:03 -04:00
|
|
|
RSpec.describe Ci::InstanceVariable do
|
2020-04-29 11:09:58 -04:00
|
|
|
subject { build(:ci_instance_variable) }
|
|
|
|
|
|
|
|
it_behaves_like "CI variable"
|
|
|
|
|
|
|
|
it { is_expected.to include_module(Ci::Maskable) }
|
|
|
|
it { is_expected.to validate_uniqueness_of(:key).with_message(/\(\w+\) has already been taken/) }
|
2020-07-24 17:09:16 -04:00
|
|
|
it { is_expected.to validate_length_of(:value).is_at_most(10_000).with_message(/The value of the provided variable exceeds the 10000 character limit/) }
|
2020-04-29 11:09:58 -04:00
|
|
|
|
2020-06-09 11:08:05 -04:00
|
|
|
it_behaves_like 'includes Limitable concern' do
|
|
|
|
subject { build(:ci_instance_variable) }
|
|
|
|
end
|
|
|
|
|
2020-07-24 17:09:16 -04:00
|
|
|
describe '#value' do
|
|
|
|
context 'without application limit' do
|
|
|
|
# Ensures breakage if encryption algorithm changes
|
|
|
|
let(:variable) { build(:ci_instance_variable, key: 'too_long', value: value) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
allow(variable).to receive(:valid?).and_return(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when value is over the limit' do
|
|
|
|
let(:value) { SecureRandom.alphanumeric(10_002) }
|
|
|
|
|
|
|
|
it 'raises a database level error' do
|
2020-09-07 11:09:04 -04:00
|
|
|
expect { variable.save! }.to raise_error(ActiveRecord::StatementInvalid)
|
2020-07-24 17:09:16 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when value is under the limit' do
|
|
|
|
let(:value) { SecureRandom.alphanumeric(10_000) }
|
|
|
|
|
|
|
|
it 'does not raise database level error' do
|
2020-09-07 11:09:04 -04:00
|
|
|
expect { variable.save! }.not_to raise_error
|
2020-07-24 17:09:16 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-04-29 11:09:58 -04:00
|
|
|
describe '.unprotected' do
|
|
|
|
subject { described_class.unprotected }
|
|
|
|
|
|
|
|
context 'when variable is protected' do
|
|
|
|
before do
|
|
|
|
create(:ci_instance_variable, :protected)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns nothing' do
|
|
|
|
is_expected.to be_empty
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when variable is not protected' do
|
|
|
|
let(:variable) { create(:ci_instance_variable, protected: false) }
|
|
|
|
|
|
|
|
it 'returns the variable' do
|
|
|
|
is_expected.to contain_exactly(variable)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2020-05-18 05:08:12 -04:00
|
|
|
|
|
|
|
describe '.all_cached', :use_clean_rails_memory_store_caching do
|
|
|
|
let_it_be(:unprotected_variable) { create(:ci_instance_variable, protected: false) }
|
|
|
|
let_it_be(:protected_variable) { create(:ci_instance_variable, protected: true) }
|
|
|
|
|
|
|
|
it { expect(described_class.all_cached).to contain_exactly(protected_variable, unprotected_variable) }
|
|
|
|
|
|
|
|
it 'memoizes the result' do
|
2020-05-25 08:08:23 -04:00
|
|
|
expect(described_class).to receive(:unscoped).once.and_call_original
|
2020-05-18 05:08:12 -04:00
|
|
|
|
|
|
|
2.times do
|
|
|
|
expect(described_class.all_cached).to contain_exactly(protected_variable, unprotected_variable)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'removes scopes' do
|
|
|
|
expect(described_class.unprotected.all_cached).to contain_exactly(protected_variable, unprotected_variable)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'resets the cache when records are deleted' do
|
|
|
|
expect(described_class.all_cached).to contain_exactly(protected_variable, unprotected_variable)
|
|
|
|
|
2020-09-07 11:09:04 -04:00
|
|
|
protected_variable.destroy!
|
2020-05-18 05:08:12 -04:00
|
|
|
|
|
|
|
expect(described_class.all_cached).to contain_exactly(unprotected_variable)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'resets the cache when records are inserted' do
|
|
|
|
expect(described_class.all_cached).to contain_exactly(protected_variable, unprotected_variable)
|
|
|
|
|
|
|
|
variable = create(:ci_instance_variable, protected: true)
|
|
|
|
|
|
|
|
expect(described_class.all_cached).to contain_exactly(protected_variable, unprotected_variable, variable)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '.unprotected_cached', :use_clean_rails_memory_store_caching do
|
|
|
|
let_it_be(:unprotected_variable) { create(:ci_instance_variable, protected: false) }
|
|
|
|
let_it_be(:protected_variable) { create(:ci_instance_variable, protected: true) }
|
|
|
|
|
|
|
|
it { expect(described_class.unprotected_cached).to contain_exactly(unprotected_variable) }
|
|
|
|
|
|
|
|
it 'memoizes the result' do
|
2020-05-25 08:08:23 -04:00
|
|
|
expect(described_class).to receive(:unscoped).once.and_call_original
|
2020-05-18 05:08:12 -04:00
|
|
|
|
|
|
|
2.times do
|
|
|
|
expect(described_class.unprotected_cached).to contain_exactly(unprotected_variable)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2020-04-29 11:09:58 -04:00
|
|
|
end
|