summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/current_settings_spec.rb
blob: a566f24f6a6a31d69769ae016bb2344d7804bf7d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
require 'spec_helper'

describe Gitlab::CurrentSettings do
  include StubENV

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

  describe '#current_application_settings' do
    context 'with DB available' do
      before do
        allow_any_instance_of(described_class).to receive(:connect_to_db?).and_return(true)
      end

      it 'attempts to use cached values first' do
        expect(ApplicationSetting).to receive(:cached)

        expect(current_application_settings).to be_a(ApplicationSetting)
      end

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

        expect(current_application_settings).to be_a(ApplicationSetting)
      end

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

        expect(current_application_settings).to be_a(ApplicationSetting)
      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
          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
    end

    context 'with DB unavailable' do
      before do
        allow_any_instance_of(described_class).to receive(:connect_to_db?).and_return(false)
        allow_any_instance_of(described_class).to receive(:retrieve_settings_from_database_cache?).and_return(nil)
      end

      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
    end

    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)

        expect(current_application_settings).to be_a(ApplicationSetting)
        expect(current_application_settings).not_to be_persisted
      end
    end
  end
end