current_settings_spec.rb 2.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 13 14
    context 'with DB available' do
      before do
        allow_any_instance_of(Gitlab::CurrentSettings).to receive(:connect_to_db?).and_return(true)
      end
15

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

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

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

26 27 28 29 30 31 32 33 34
        expect(current_application_settings).to be_a(ApplicationSetting)
      end

      it 'falls back to DB if Redis fails' do
        expect(ApplicationSetting).to receive(:current).and_raise(::Redis::BaseError)
        expect(ApplicationSetting).to receive(:last).and_call_original

        expect(current_application_settings).to be_a(ApplicationSetting)
      end
35 36
    end

37 38 39 40
    context 'with DB unavailable' do
      before do
        allow_any_instance_of(Gitlab::CurrentSettings).to receive(:connect_to_db?).and_return(false)
      end
41

42 43 44 45 46 47
      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
48 49
    end

50 51 52 53 54 55 56 57
    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)
58

59 60 61
        expect(current_application_settings).to be_a(ApplicationSetting)
        expect(current_application_settings).not_to be_persisted
      end
62 63 64
    end
  end
end