current_settings_spec.rb 4.0 KB
Newer Older
1 2 3
require 'spec_helper'

describe Gitlab::CurrentSettings do
4 5 6 7 8 9
  include StubENV

  before do
    stub_env('IN_MEMORY_APPLICATION_SETTINGS', 'false')
  end

10
  describe '#current_application_settings' do
11 12
    context 'with DB available' do
      before do
13
        allow_any_instance_of(described_class).to receive(:connect_to_db?).and_return(true)
14
      end
15

16
      it 'attempts to use cached values first' do
17
        expect(ApplicationSetting).to receive(:cached)
18 19 20

        expect(current_application_settings).to be_a(ApplicationSetting)
      end
21

22
      it 'falls back to DB if Redis returns an empty value' do
23
        expect(ApplicationSetting).to receive(:cached).and_return(nil)
24
        expect(ApplicationSetting).to receive(:last).and_call_original.twice
25

26 27 28 29
        expect(current_application_settings).to be_a(ApplicationSetting)
      end

      it 'falls back to DB if Redis fails' do
30 31
        db_settings = ApplicationSetting.create!(ApplicationSetting.defaults)

32
        expect(ApplicationSetting).to receive(:cached).and_raise(::Redis::BaseError)
33
        expect(Rails.cache).to receive(:fetch).with(ApplicationSetting::CACHE_KEY).and_raise(Redis::BaseError)
34

35 36 37 38 39 40 41 42 43 44 45 46
        expect(current_application_settings).to eq(db_settings)
      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)

        settings = current_application_settings

        expect(settings).to be_a(ApplicationSetting)
        expect(settings).to be_persisted
        expect(settings).to have_attributes(ApplicationSetting.defaults)
47
      end
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78

      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
          settings = current_application_settings

          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)
          settings = current_application_settings
          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
      end
79 80
    end

81 82
    context 'with DB unavailable' do
      before do
83
        allow_any_instance_of(described_class).to receive(:connect_to_db?).and_return(false)
84
        allow_any_instance_of(described_class).to receive(:retrieve_settings_from_database_cache?).and_return(nil)
85
      end
86

87 88 89 90 91 92
      it 'returns an in-memory ApplicationSetting object' do
        expect(ApplicationSetting).not_to receive(:current)
        expect(ApplicationSetting).not_to receive(:last)

        expect(current_application_settings).to be_a(OpenStruct)
      end
93 94
    end

95 96 97 98 99 100 101 102
    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)
103

104 105 106
        expect(current_application_settings).to be_a(ApplicationSetting)
        expect(current_application_settings).not_to be_persisted
      end
107 108 109
    end
  end
end