2019-07-25 01:21:37 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-06-26 02:29:28 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 14:09:03 -04:00
|
|
|
RSpec.describe Gitlab::CurrentSettings do
|
2017-01-21 19:11:19 -05:00
|
|
|
before do
|
|
|
|
stub_env('IN_MEMORY_APPLICATION_SETTINGS', 'false')
|
|
|
|
end
|
|
|
|
|
2018-06-07 11:23:38 -04:00
|
|
|
shared_context 'with settings in cache' do
|
|
|
|
before do
|
|
|
|
create(:application_setting)
|
|
|
|
described_class.current_application_settings # warm the cache
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-08-07 14:40:36 -04:00
|
|
|
describe '.expire_current_application_settings', :use_clean_rails_memory_store_caching, :request_store do
|
|
|
|
include_context 'with settings in cache'
|
|
|
|
|
|
|
|
it 'expires the cache' do
|
|
|
|
described_class.expire_current_application_settings
|
|
|
|
|
|
|
|
expect(ActiveRecord::QueryRecorder.new { described_class.current_application_settings }.count).not_to eq(0)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-05-25 11:10:33 -04:00
|
|
|
describe '.signup_limited?' do
|
|
|
|
subject { described_class.signup_limited? }
|
|
|
|
|
|
|
|
context 'when there are allowed domains' do
|
|
|
|
before do
|
|
|
|
create(:application_setting, domain_allowlist: ['www.gitlab.com'])
|
|
|
|
end
|
|
|
|
|
|
|
|
it { is_expected.to be_truthy }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when there are email restrictions' do
|
|
|
|
before do
|
|
|
|
create(:application_setting, email_restrictions_enabled: true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it { is_expected.to be_truthy }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the admin has to approve signups' do
|
|
|
|
before do
|
|
|
|
create(:application_setting, require_admin_approval_after_user_signup: true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it { is_expected.to be_truthy }
|
|
|
|
end
|
|
|
|
|
2021-12-06 22:12:22 -05:00
|
|
|
context 'when new users are set to external' do
|
|
|
|
before do
|
|
|
|
create(:application_setting, user_default_external: true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it { is_expected.to be_truthy }
|
|
|
|
end
|
|
|
|
|
2021-05-25 11:10:33 -04:00
|
|
|
context 'when there are no restrictions' do
|
|
|
|
before do
|
2021-12-06 22:12:22 -05:00
|
|
|
create(:application_setting, domain_allowlist: [], email_restrictions_enabled: false, require_admin_approval_after_user_signup: false, user_default_external: false)
|
2021-05-25 11:10:33 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it { is_expected.to be_falsey }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-02-22 04:10:46 -05:00
|
|
|
describe '.signup_disabled?' do
|
|
|
|
subject { described_class.signup_disabled? }
|
|
|
|
|
|
|
|
context 'when signup is enabled' do
|
|
|
|
before do
|
|
|
|
create(:application_setting, signup_enabled: true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it { is_expected.to be_falsey }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when signup is disabled' do
|
|
|
|
before do
|
|
|
|
create(:application_setting, signup_enabled: false)
|
|
|
|
end
|
|
|
|
|
|
|
|
it { is_expected.to be_truthy }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-05-16 11:10:48 -04:00
|
|
|
describe '#current_application_settings', :use_clean_rails_memory_store_caching do
|
2018-02-02 13:39:55 -05:00
|
|
|
it 'allows keys to be called directly' do
|
2019-02-19 07:58:05 -05:00
|
|
|
db_settings = create(:application_setting,
|
|
|
|
home_page_url: 'http://mydomain.com',
|
|
|
|
signup_enabled: false)
|
2018-02-02 13:39:55 -05:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2018-05-16 11:10:48 -04:00
|
|
|
context 'when ENV["IN_MEMORY_APPLICATION_SETTINGS"] is true' do
|
2017-01-21 19:11:19 -05:00
|
|
|
before do
|
2018-05-16 11:10:48 -04:00
|
|
|
stub_env('IN_MEMORY_APPLICATION_SETTINGS', 'true')
|
2017-01-21 19:11:19 -05:00
|
|
|
end
|
2016-06-26 02:29:28 -04:00
|
|
|
|
2018-05-16 11:10:48 -04:00
|
|
|
it 'returns an in-memory ApplicationSetting object' do
|
|
|
|
expect(ApplicationSetting).not_to receive(:current)
|
2017-01-21 19:11:19 -05:00
|
|
|
|
2018-02-02 13:39:55 -05:00
|
|
|
expect(described_class.current_application_settings).to be_a(ApplicationSetting)
|
2018-05-16 11:10:48 -04:00
|
|
|
expect(described_class.current_application_settings).not_to be_persisted
|
2017-01-21 19:11:19 -05:00
|
|
|
end
|
2018-05-16 11:10:48 -04:00
|
|
|
end
|
2017-01-14 00:18:40 -05:00
|
|
|
|
2020-04-15 17:09:18 -04:00
|
|
|
context 'in a Rake task with DB unavailable' do
|
|
|
|
before do
|
|
|
|
allow(Gitlab::Runtime).to receive(:rake?).and_return(true)
|
|
|
|
# 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-01-21 19:11:19 -05:00
|
|
|
end
|
|
|
|
|
2018-06-07 11:23:38 -04:00
|
|
|
context 'and no settings in cache' do
|
|
|
|
before do
|
|
|
|
expect(ApplicationSetting).not_to receive(:current)
|
|
|
|
end
|
2017-07-10 08:29:16 -04:00
|
|
|
|
2018-12-14 09:45:14 -05:00
|
|
|
it 'returns a FakeApplicationSettings object' do
|
2018-06-07 11:23:38 -04:00
|
|
|
expect(described_class.current_application_settings).to be_a(Gitlab::FakeApplicationSettings)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not issue any query' do
|
|
|
|
expect(ActiveRecord::QueryRecorder.new { described_class.current_application_settings }.count).to eq(0)
|
|
|
|
end
|
2018-05-16 11:10:48 -04:00
|
|
|
end
|
|
|
|
end
|
2017-01-21 19:11:19 -05:00
|
|
|
|
2018-05-16 11:10:48 -04:00
|
|
|
context 'with DB available' do
|
|
|
|
# This method returns the ::ApplicationSetting.defaults hash
|
|
|
|
# but with respect of custom attribute accessors of ApplicationSetting model
|
|
|
|
def settings_from_defaults
|
|
|
|
ar_wrapped_defaults = ::ApplicationSetting.build_from_defaults.attributes
|
|
|
|
ar_wrapped_defaults.slice(*::ApplicationSetting.defaults.keys)
|
2017-07-10 08:29:16 -04:00
|
|
|
end
|
|
|
|
|
2018-06-07 11:23:38 -04:00
|
|
|
context 'and settings in cache' do
|
|
|
|
include_context 'with settings in cache'
|
2017-07-10 08:29:16 -04:00
|
|
|
|
2018-06-07 11:23:38 -04:00
|
|
|
it 'fetches the settings from cache' do
|
|
|
|
# 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
|
|
|
|
expect(ActiveRecord::Base.connection).not_to receive(:active?)
|
|
|
|
expect(ActiveRecord::Base.connection).not_to receive(:cached_table_exists?)
|
2019-06-25 13:54:42 -04:00
|
|
|
expect_any_instance_of(ActiveRecord::MigrationContext).not_to receive(:needs_migration?)
|
2018-06-07 11:23:38 -04:00
|
|
|
expect(ActiveRecord::QueryRecorder.new { described_class.current_application_settings }.count).to eq(0)
|
|
|
|
end
|
2017-01-21 19:11:19 -05:00
|
|
|
end
|
2017-06-17 10:35:30 -04:00
|
|
|
|
2018-06-07 11:23:38 -04:00
|
|
|
context 'and no settings in cache' do
|
2017-06-17 10:35:30 -04:00
|
|
|
before do
|
2018-06-07 11:23:38 -04:00
|
|
|
allow(ActiveRecord::Base.connection).to receive(:active?).and_return(true)
|
|
|
|
allow(ActiveRecord::Base.connection).to receive(:cached_table_exists?).with('application_settings').and_return(true)
|
2017-06-17 10:35:30 -04:00
|
|
|
end
|
|
|
|
|
2018-12-12 06:59:06 -05:00
|
|
|
context 'with RequestStore enabled', :request_store do
|
|
|
|
it 'fetches the settings from DB only once' do
|
|
|
|
described_class.current_application_settings # warm the cache
|
|
|
|
|
|
|
|
expect(ActiveRecord::QueryRecorder.new { described_class.current_application_settings }.count).to eq(0)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-06-07 11:23:38 -04:00
|
|
|
it 'creates default ApplicationSettings if none are present' do
|
2018-02-02 13:39:55 -05:00
|
|
|
settings = described_class.current_application_settings
|
2017-06-17 10:35:30 -04:00
|
|
|
|
2018-06-07 11:23:38 -04:00
|
|
|
expect(settings).to be_a(ApplicationSetting)
|
|
|
|
expect(settings).to be_persisted
|
|
|
|
expect(settings).to have_attributes(settings_from_defaults)
|
2017-06-17 10:35:30 -04:00
|
|
|
end
|
|
|
|
|
2020-09-01 11:10:39 -04:00
|
|
|
context 'when ApplicationSettings does not have a primary key' do
|
|
|
|
before do
|
|
|
|
allow(ActiveRecord::Base.connection).to receive(:primary_key).with('application_settings').and_return(nil)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'raises an exception if ApplicationSettings does not have a primary key' do
|
|
|
|
expect { described_class.current_application_settings }.to raise_error(/table is missing a primary key constraint/)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-12-12 06:59:06 -05:00
|
|
|
context 'with pending migrations' do
|
2020-02-17 13:09:00 -05:00
|
|
|
let(:current_settings) { described_class.current_application_settings }
|
|
|
|
|
2018-06-07 11:23:38 -04:00
|
|
|
before do
|
2020-02-17 13:09:00 -05:00
|
|
|
allow(Gitlab::Runtime).to receive(:rake?).and_return(false)
|
2018-06-07 11:23:38 -04:00
|
|
|
end
|
|
|
|
|
2018-12-12 06:59:06 -05:00
|
|
|
shared_examples 'a non-persisted ApplicationSetting object' do
|
|
|
|
it 'uses the default value from ApplicationSetting.defaults' do
|
|
|
|
expect(current_settings.signup_enabled).to eq(ApplicationSetting.defaults[:signup_enabled])
|
|
|
|
end
|
2018-06-07 11:23:38 -04:00
|
|
|
|
2018-12-12 06:59:06 -05:00
|
|
|
it 'uses the default value from custom ApplicationSetting accessors' do
|
|
|
|
expect(current_settings.commit_email_hostname).to eq(ApplicationSetting.default_commit_email_hostname)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'responds to predicate methods' do
|
|
|
|
expect(current_settings.signup_enabled?).to eq(current_settings.signup_enabled)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-02-17 13:09:00 -05:00
|
|
|
context 'in a Rake task' do
|
|
|
|
before do
|
|
|
|
allow(Gitlab::Runtime).to receive(:rake?).and_return(true)
|
|
|
|
expect_any_instance_of(ActiveRecord::MigrationContext).to receive(:needs_migration?).and_return(true)
|
|
|
|
end
|
2018-12-12 06:59:06 -05:00
|
|
|
|
|
|
|
it_behaves_like 'a non-persisted ApplicationSetting object'
|
|
|
|
|
2020-02-17 13:09:00 -05:00
|
|
|
it 'returns a FakeApplicationSettings object' do
|
|
|
|
expect(current_settings).to be_a(Gitlab::FakeApplicationSettings)
|
2018-12-12 06:59:06 -05:00
|
|
|
end
|
2019-03-26 10:33:56 -04:00
|
|
|
|
|
|
|
context 'when a new column is used before being migrated' do
|
|
|
|
before do
|
|
|
|
allow(ApplicationSetting).to receive(:defaults).and_return({ foo: 'bar' })
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'uses the default value if present' do
|
|
|
|
expect(current_settings.foo).to eq('bar')
|
|
|
|
end
|
|
|
|
end
|
2018-06-07 11:23:38 -04:00
|
|
|
end
|
2020-02-17 13:09:00 -05:00
|
|
|
|
|
|
|
context 'with no ApplicationSetting DB record' do
|
|
|
|
it_behaves_like 'a non-persisted ApplicationSetting object'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with an existing ApplicationSetting DB record' do
|
|
|
|
let!(:db_settings) { ApplicationSetting.build_from_defaults(home_page_url: 'http://mydomain.com').save! && ApplicationSetting.last }
|
|
|
|
|
|
|
|
it_behaves_like 'a non-persisted ApplicationSetting object'
|
|
|
|
|
|
|
|
it 'uses the value from the DB attribute if present and not overridden by an accessor' do
|
|
|
|
expect(current_settings.home_page_url).to eq(db_settings.home_page_url)
|
|
|
|
end
|
|
|
|
end
|
2017-06-17 10:35:30 -04:00
|
|
|
end
|
2016-06-29 17:28:41 -04:00
|
|
|
|
2018-06-07 11:23:38 -04:00
|
|
|
context 'when ApplicationSettings.current is present' do
|
|
|
|
it 'returns the existing application settings' do
|
|
|
|
expect(ApplicationSetting).to receive(:current).and_return(:current_settings)
|
2017-01-21 19:11:19 -05:00
|
|
|
|
2018-06-07 11:23:38 -04:00
|
|
|
expect(described_class.current_application_settings).to eq(:current_settings)
|
|
|
|
end
|
2018-05-16 11:10:48 -04:00
|
|
|
end
|
2017-01-21 19:11:19 -05:00
|
|
|
end
|
2016-06-26 02:29:28 -04:00
|
|
|
end
|
|
|
|
end
|
2021-02-01 19:09:14 -05:00
|
|
|
|
|
|
|
describe '#current_application_settings?', :use_clean_rails_memory_store_caching do
|
|
|
|
before do
|
|
|
|
allow(Gitlab::CurrentSettings).to receive(:current_application_settings?).and_call_original
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns true when settings exist' do
|
|
|
|
create(:application_setting,
|
|
|
|
home_page_url: 'http://mydomain.com',
|
|
|
|
signup_enabled: false)
|
|
|
|
|
|
|
|
expect(described_class.current_application_settings?).to eq(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns false when settings do not exist' do
|
|
|
|
expect(described_class.current_application_settings?).to eq(false)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with cache', :request_store do
|
|
|
|
include_context 'with settings in cache'
|
|
|
|
|
|
|
|
it 'returns an in-memory ApplicationSetting object' do
|
|
|
|
expect(ApplicationSetting).not_to receive(:current)
|
|
|
|
|
|
|
|
expect(described_class.current_application_settings?).to eq(true)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2016-06-26 02:29:28 -04:00
|
|
|
end
|