2019-04-15 06:17:05 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-09-12 06:29:50 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-03 23:08:05 -04:00
|
|
|
RSpec.describe Settings do
|
2018-09-12 06:29:50 -04:00
|
|
|
describe 'omniauth' do
|
|
|
|
it 'defaults to enabled' do
|
|
|
|
expect(described_class.omniauth.enabled).to be true
|
|
|
|
end
|
|
|
|
end
|
2018-11-22 09:35:49 -05:00
|
|
|
|
2020-02-19 04:08:59 -05:00
|
|
|
describe '.load_dynamic_cron_schedules!' do
|
|
|
|
it 'generates a valid cron schedule' do
|
|
|
|
expect(Fugit::Cron.parse(described_class.load_dynamic_cron_schedules!)).to be_a(Fugit::Cron)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-11-22 09:35:49 -05:00
|
|
|
describe '.attr_encrypted_db_key_base_truncated' do
|
|
|
|
it 'is a string with maximum 32 bytes size' do
|
|
|
|
expect(described_class.attr_encrypted_db_key_base_truncated.bytesize)
|
|
|
|
.to be <= 32
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '.attr_encrypted_db_key_base_12' do
|
|
|
|
context 'when db key base secret is less than 12 bytes' do
|
|
|
|
before do
|
|
|
|
allow(described_class)
|
|
|
|
.to receive(:attr_encrypted_db_key_base)
|
|
|
|
.and_return('a' * 10)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'expands db key base secret to 12 bytes' do
|
|
|
|
expect(described_class.attr_encrypted_db_key_base_12)
|
2018-11-28 06:00:17 -05:00
|
|
|
.to eq(('a' * 10) + ('0' * 2))
|
2018-11-22 09:35:49 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when key has multiple multi-byte UTF chars exceeding 12 bytes' do
|
|
|
|
before do
|
|
|
|
allow(described_class)
|
|
|
|
.to receive(:attr_encrypted_db_key_base)
|
|
|
|
.and_return('❤' * 18)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not use more than 32 bytes' do
|
|
|
|
db_key_base = described_class.attr_encrypted_db_key_base_12
|
|
|
|
|
2018-11-23 04:03:43 -05:00
|
|
|
expect(db_key_base).to eq('❤' * 4)
|
2018-11-22 09:35:49 -05:00
|
|
|
expect(db_key_base.bytesize).to eq 12
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '.attr_encrypted_db_key_base_32' do
|
|
|
|
context 'when db key base secret is less than 32 bytes' do
|
|
|
|
before do
|
|
|
|
allow(described_class)
|
|
|
|
.to receive(:attr_encrypted_db_key_base)
|
|
|
|
.and_return('a' * 10)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'expands db key base secret to 32 bytes' do
|
2018-11-28 06:00:17 -05:00
|
|
|
expanded_key_base = ('a' * 10) + ('0' * 22)
|
2018-11-22 09:35:49 -05:00
|
|
|
|
|
|
|
expect(expanded_key_base.bytesize).to eq 32
|
|
|
|
expect(described_class.attr_encrypted_db_key_base_32)
|
|
|
|
.to eq expanded_key_base
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when db key base secret is 32 bytes' do
|
|
|
|
before do
|
|
|
|
allow(described_class)
|
|
|
|
.to receive(:attr_encrypted_db_key_base)
|
|
|
|
.and_return('a' * 32)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns original value' do
|
|
|
|
expect(described_class.attr_encrypted_db_key_base_32)
|
|
|
|
.to eq 'a' * 32
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when db key base contains multi-byte UTF character' do
|
|
|
|
before do
|
|
|
|
allow(described_class)
|
|
|
|
.to receive(:attr_encrypted_db_key_base)
|
|
|
|
.and_return('❤' * 6)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not use more than 32 bytes' do
|
|
|
|
db_key_base = described_class.attr_encrypted_db_key_base_32
|
|
|
|
|
2018-11-28 06:00:17 -05:00
|
|
|
expect(db_key_base).to eq '❤❤❤❤❤❤' + ('0' * 14)
|
2018-11-22 09:35:49 -05:00
|
|
|
expect(db_key_base.bytesize).to eq 32
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when db key base multi-byte UTF chars exceeding 32 bytes' do
|
|
|
|
before do
|
|
|
|
allow(described_class)
|
|
|
|
.to receive(:attr_encrypted_db_key_base)
|
|
|
|
.and_return('❤' * 18)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not use more than 32 bytes' do
|
|
|
|
db_key_base = described_class.attr_encrypted_db_key_base_32
|
|
|
|
|
2018-11-28 06:00:17 -05:00
|
|
|
expect(db_key_base).to eq(('❤' * 10) + ('0' * 2))
|
2018-11-22 09:35:49 -05:00
|
|
|
expect(db_key_base.bytesize).to eq 32
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2020-07-08 11:09:24 -04:00
|
|
|
|
2021-07-08 02:09:14 -04:00
|
|
|
describe '.cron_for_service_ping' do
|
2020-07-08 11:09:24 -04:00
|
|
|
it 'returns correct crontab for some manually calculated example' do
|
|
|
|
allow(Gitlab::CurrentSettings)
|
|
|
|
.to receive(:uuid) { 'd9e2f4e8-db1f-4e51-b03d-f427e1965c4a'}
|
|
|
|
|
2021-07-08 02:09:14 -04:00
|
|
|
expect(described_class.send(:cron_for_service_ping)).to eq('21 18 * * 4')
|
2020-07-08 11:09:24 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns min, hour, day in the valid range' do
|
|
|
|
allow(Gitlab::CurrentSettings)
|
|
|
|
.to receive(:uuid) { SecureRandom.uuid }
|
|
|
|
|
|
|
|
10.times do
|
2021-07-08 02:09:14 -04:00
|
|
|
cron = described_class.send(:cron_for_service_ping).split(/\s/)
|
2020-07-08 11:09:24 -04:00
|
|
|
|
|
|
|
expect(cron[0].to_i).to be_between(0, 59)
|
|
|
|
expect(cron[1].to_i).to be_between(0, 23)
|
|
|
|
expect(cron[4].to_i).to be_between(0, 6)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2020-11-30 16:09:16 -05:00
|
|
|
|
|
|
|
describe '.encrypted' do
|
|
|
|
before do
|
|
|
|
allow(Gitlab::Application.secrets).to receive(:encryped_settings_key_base).and_return(SecureRandom.hex(64))
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'defaults to using the encrypted_settings_key_base for the key' do
|
|
|
|
expect(Gitlab::EncryptedConfiguration).to receive(:new).with(hash_including(base_key: Gitlab::Application.secrets.encrypted_settings_key_base))
|
|
|
|
Settings.encrypted('tmp/tests/test.enc')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns empty encrypted config when a key has not been set' do
|
|
|
|
allow(Gitlab::Application.secrets).to receive(:encrypted_settings_key_base).and_return(nil)
|
|
|
|
expect(Settings.encrypted('tmp/tests/test.enc').read).to be_empty
|
|
|
|
end
|
|
|
|
end
|
2018-09-12 06:29:50 -04:00
|
|
|
end
|