2016-08-24 12:49:21 +05:30
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe Gitlab::CurrentSettings do
|
2017-08-17 22:00:37 +05:30
|
|
|
include StubENV
|
|
|
|
|
|
|
|
before do
|
|
|
|
stub_env('IN_MEMORY_APPLICATION_SETTINGS', 'false')
|
|
|
|
end
|
|
|
|
|
2016-08-24 12:49:21 +05:30
|
|
|
describe '#current_application_settings' do
|
2018-03-17 18:26:18 +05:30
|
|
|
it 'allows keys to be called directly' do
|
|
|
|
db_settings = create(:application_setting,
|
|
|
|
home_page_url: 'http://mydomain.com',
|
|
|
|
signup_enabled: false)
|
|
|
|
|
|
|
|
expect(described_class.home_page_url).to eq(db_settings.home_page_url)
|
|
|
|
expect(described_class.signup_enabled?).to be_falsey
|
|
|
|
expect(described_class.signup_enabled).to be_falsey
|
|
|
|
expect(described_class.metrics_sample_interval).to be(15)
|
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
context 'with DB available' do
|
|
|
|
before do
|
2018-03-17 18:26:18 +05:30
|
|
|
# For some reason, `allow(described_class).to receive(:connect_to_db?).and_return(true)` causes issues
|
|
|
|
# during the initialization phase of the test suite, so instead let's mock the internals of it
|
|
|
|
allow(ActiveRecord::Base.connection).to receive(:active?).and_return(true)
|
|
|
|
allow(ActiveRecord::Base.connection).to receive(:table_exists?).and_call_original
|
|
|
|
allow(ActiveRecord::Base.connection).to receive(:table_exists?).with('application_settings').and_return(true)
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
2016-08-24 12:49:21 +05:30
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
it 'attempts to use cached values first' do
|
2017-09-10 17:25:29 +05:30
|
|
|
expect(ApplicationSetting).to receive(:cached)
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
expect(described_class.current_application_settings).to be_a(ApplicationSetting)
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
2016-08-24 12:49:21 +05:30
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
it 'falls back to DB if Redis returns an empty value' do
|
2017-09-10 17:25:29 +05:30
|
|
|
expect(ApplicationSetting).to receive(:cached).and_return(nil)
|
2018-03-17 18:26:18 +05:30
|
|
|
expect(ApplicationSetting).to receive(:last).and_call_original.twice
|
2016-08-24 12:49:21 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
expect(described_class.current_application_settings).to be_a(ApplicationSetting)
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it 'falls back to DB if Redis fails' do
|
2017-09-10 17:25:29 +05:30
|
|
|
db_settings = ApplicationSetting.create!(ApplicationSetting.defaults)
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
expect(ApplicationSetting).to receive(:cached).and_raise(::Redis::BaseError)
|
|
|
|
expect(Rails.cache).to receive(:fetch).with(ApplicationSetting::CACHE_KEY).and_raise(Redis::BaseError)
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
expect(described_class.current_application_settings).to eq(db_settings)
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it 'creates default ApplicationSettings if none are present' do
|
|
|
|
expect(ApplicationSetting).to receive(:cached).and_raise(::Redis::BaseError)
|
|
|
|
expect(Rails.cache).to receive(:fetch).with(ApplicationSetting::CACHE_KEY).and_raise(Redis::BaseError)
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
settings = described_class.current_application_settings
|
2017-09-10 17:25:29 +05:30
|
|
|
|
|
|
|
expect(settings).to be_a(ApplicationSetting)
|
|
|
|
expect(settings).to be_persisted
|
|
|
|
expect(settings).to have_attributes(ApplicationSetting.defaults)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with migrations pending' do
|
|
|
|
before do
|
|
|
|
expect(ActiveRecord::Migrator).to receive(:needs_migration?).and_return(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns an in-memory ApplicationSetting object' do
|
2018-03-17 18:26:18 +05:30
|
|
|
settings = described_class.current_application_settings
|
2017-09-10 17:25:29 +05:30
|
|
|
|
|
|
|
expect(settings).to be_a(OpenStruct)
|
|
|
|
expect(settings.sign_in_enabled?).to eq(settings.sign_in_enabled)
|
|
|
|
expect(settings.sign_up_enabled?).to eq(settings.sign_up_enabled)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'uses the existing database settings and falls back to defaults' do
|
|
|
|
db_settings = create(:application_setting,
|
|
|
|
home_page_url: 'http://mydomain.com',
|
|
|
|
signup_enabled: false)
|
2018-03-17 18:26:18 +05:30
|
|
|
settings = described_class.current_application_settings
|
2017-09-10 17:25:29 +05:30
|
|
|
app_defaults = ApplicationSetting.last
|
|
|
|
|
|
|
|
expect(settings).to be_a(OpenStruct)
|
|
|
|
expect(settings.home_page_url).to eq(db_settings.home_page_url)
|
|
|
|
expect(settings.signup_enabled?).to be_falsey
|
|
|
|
expect(settings.signup_enabled).to be_falsey
|
|
|
|
|
|
|
|
# Check that unspecified values use the defaults
|
|
|
|
settings.reject! { |key, _| [:home_page_url, :signup_enabled].include? key }
|
|
|
|
settings.each { |key, _| expect(settings[key]).to eq(app_defaults[key]) }
|
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
2016-08-24 12:49:21 +05:30
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
context 'with DB unavailable' do
|
|
|
|
before do
|
2018-03-17 18:26:18 +05:30
|
|
|
# For some reason, `allow(described_class).to receive(:connect_to_db?).and_return(false)` causes issues
|
|
|
|
# during the initialization phase of the test suite, so instead let's mock the internals of it
|
|
|
|
allow(ActiveRecord::Base.connection).to receive(:active?).and_return(false)
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
2016-08-24 12:49:21 +05:30
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
it 'returns an in-memory ApplicationSetting object' do
|
|
|
|
expect(ApplicationSetting).not_to receive(:current)
|
|
|
|
expect(ApplicationSetting).not_to receive(:last)
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
expect(described_class.current_application_settings).to be_a(OpenStruct)
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
2016-08-24 12:49:21 +05:30
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
context 'when ENV["IN_MEMORY_APPLICATION_SETTINGS"] is true' do
|
|
|
|
before do
|
|
|
|
stub_env('IN_MEMORY_APPLICATION_SETTINGS', 'true')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns an in-memory ApplicationSetting object' do
|
|
|
|
expect(ApplicationSetting).not_to receive(:current)
|
|
|
|
expect(ApplicationSetting).not_to receive(:last)
|
2016-08-24 12:49:21 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
expect(described_class.current_application_settings).to be_a(ApplicationSetting)
|
|
|
|
expect(described_class.current_application_settings).not_to be_persisted
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
2016-08-24 12:49:21 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|