application_setting_examples.rb 10.7 KB
Newer Older
1 2
# frozen_string_literal: true

3 4 5 6 7
RSpec.shared_examples 'string of domains' do |attribute|
  it 'sets single domain' do
    setting.method("#{attribute}_raw=").call('example.com')
    expect(setting.method(attribute).call).to eq(['example.com'])
  end
8

9 10 11 12
  it 'sets multiple domains with spaces' do
    setting.method("#{attribute}_raw=").call('example.com *.example.com')
    expect(setting.method(attribute).call).to eq(['example.com', '*.example.com'])
  end
13

14 15 16 17
  it 'sets multiple domains with newlines and a space' do
    setting.method("#{attribute}_raw=").call("example.com\n *.example.com")
    expect(setting.method(attribute).call).to eq(['example.com', '*.example.com'])
  end
18

19 20 21
  it 'sets multiple domains with commas' do
    setting.method("#{attribute}_raw=").call("example.com, *.example.com")
    expect(setting.method(attribute).call).to eq(['example.com', '*.example.com'])
22 23
  end

24 25 26 27
  it 'sets multiple domains with semicolon' do
    setting.method("#{attribute}_raw=").call("example.com; *.example.com")
    expect(setting.method(attribute).call).to contain_exactly('example.com', '*.example.com')
  end
28

29 30 31 32
  it 'sets multiple domains with mixture of everything' do
    setting.method("#{attribute}_raw=").call("example.com; *.example.com\n test.com\sblock.com   yes.com")
    expect(setting.method(attribute).call).to contain_exactly('example.com', '*.example.com', 'test.com', 'block.com', 'yes.com')
  end
33

34 35 36 37
  it 'removes duplicates' do
    setting.method("#{attribute}_raw=").call("example.com; example.com; 127.0.0.1; 127.0.0.1")
    expect(setting.method(attribute).call).to contain_exactly('example.com', '127.0.0.1')
  end
38

39 40 41 42
  it 'does not fail with garbage values' do
    setting.method("#{attribute}_raw=").call("example;34543:garbage:fdh5654;")
    expect(setting.method(attribute).call).to contain_exactly('example', '34543:garbage:fdh5654')
  end
43 44 45 46 47

  it 'does not raise error with nil' do
    setting.method("#{attribute}_raw=").call(nil)
    expect(setting.method(attribute).call).to eq([])
  end
48
end
49

50 51 52 53
RSpec.shared_examples 'application settings examples' do
  context 'restricted signup domains' do
    it_behaves_like 'string of domains', :domain_whitelist
  end
54

55 56
  context 'blacklisted signup domains' do
    it_behaves_like 'string of domains', :domain_blacklist
57 58 59 60 61 62 63

    it 'sets multiple domain with file' do
      setting.domain_blacklist_file = File.open(Rails.root.join('spec/fixtures/', 'domain_blacklist.txt'))
      expect(setting.domain_blacklist).to contain_exactly('example.com', 'test.com', 'foo.bar')
    end
  end

64 65
  context 'outbound_local_requests_whitelist' do
    it_behaves_like 'string of domains', :outbound_local_requests_whitelist
66 67 68 69 70 71 72 73 74 75 76 77 78

    it 'clears outbound_local_requests_whitelist_arrays memoization' do
      setting.outbound_local_requests_whitelist_raw = 'example.com'

      expect(setting.outbound_local_requests_whitelist_arrays).to contain_exactly(
        [], ['example.com']
      )

      setting.outbound_local_requests_whitelist_raw = 'gitlab.com'
      expect(setting.outbound_local_requests_whitelist_arrays).to contain_exactly(
        [], ['gitlab.com']
      )
    end
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
  end

  context 'outbound_local_requests_whitelist_arrays' do
    it 'separates the IPs and domains' do
      setting.outbound_local_requests_whitelist = [
        '192.168.1.1', '127.0.0.0/28', 'www.example.com', 'example.com',
        '::ffff:a00:2', '1:0:0:0:0:0:0:0/124', 'subdomain.example.com'
      ]

      ip_whitelist = [
        IPAddr.new('192.168.1.1'), IPAddr.new('127.0.0.0/8'),
        IPAddr.new('::ffff:a00:2'), IPAddr.new('1:0:0:0:0:0:0:0/124')
      ]
      domain_whitelist = ['www.example.com', 'example.com', 'subdomain.example.com']

94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
      expect(setting.outbound_local_requests_whitelist_arrays).to contain_exactly(
        ip_whitelist, domain_whitelist
      )
    end
  end

  context 'add_to_outbound_local_requests_whitelist' do
    it 'adds entry to outbound_local_requests_whitelist' do
      setting.outbound_local_requests_whitelist = ['example.com']

      setting.add_to_outbound_local_requests_whitelist(
        ['example.com', '127.0.0.1', 'gitlab.com']
      )

      expect(setting.outbound_local_requests_whitelist).to contain_exactly(
        'example.com',
        '127.0.0.1',
        'gitlab.com'
      )
    end

    it 'clears outbound_local_requests_whitelist_arrays memoization' do
      setting.outbound_local_requests_whitelist = ['example.com']

      expect(setting.outbound_local_requests_whitelist_arrays).to contain_exactly(
        [],
        ['example.com']
      )

      setting.add_to_outbound_local_requests_whitelist(
        ['example.com', 'gitlab.com']
      )

      expect(setting.outbound_local_requests_whitelist_arrays).to contain_exactly(
        [],
        ['example.com', 'gitlab.com']
      )
    end

    it 'does not raise error with nil' do
      setting.outbound_local_requests_whitelist = nil

      setting.add_to_outbound_local_requests_whitelist(['gitlab.com'])

      expect(setting.outbound_local_requests_whitelist).to contain_exactly('gitlab.com')
      expect(setting.outbound_local_requests_whitelist_arrays).to contain_exactly(
        [], ['gitlab.com']
      )
142
    end
143 144 145 146 147 148

    it 'does not raise error with nil' do
      setting.outbound_local_requests_whitelist = nil

      expect(setting.outbound_local_requests_whitelist_arrays).to contain_exactly([], [])
    end
149 150
  end

151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
  describe 'usage ping settings' do
    context 'when the usage ping is disabled in gitlab.yml' do
      before do
        allow(Settings.gitlab).to receive(:usage_ping_enabled).and_return(false)
      end

      it 'does not allow the usage ping to be configured' do
        expect(setting.usage_ping_can_be_configured?).to be_falsey
      end

      context 'when the usage ping is disabled in the DB' do
        before do
          setting.usage_ping_enabled = false
        end

        it 'returns false for usage_ping_enabled' do
          expect(setting.usage_ping_enabled).to be_falsey
        end
      end

      context 'when the usage ping is enabled in the DB' do
        before do
          setting.usage_ping_enabled = true
        end

        it 'returns false for usage_ping_enabled' do
          expect(setting.usage_ping_enabled).to be_falsey
        end
      end
    end

    context 'when the usage ping is enabled in gitlab.yml' do
      before do
        allow(Settings.gitlab).to receive(:usage_ping_enabled).and_return(true)
      end

      it 'allows the usage ping to be configured' do
        expect(setting.usage_ping_can_be_configured?).to be_truthy
      end

      context 'when the usage ping is disabled in the DB' do
        before do
          setting.usage_ping_enabled = false
        end

        it 'returns false for usage_ping_enabled' do
          expect(setting.usage_ping_enabled).to be_falsey
        end
      end

      context 'when the usage ping is enabled in the DB' do
        before do
          setting.usage_ping_enabled = true
        end

        it 'returns true for usage_ping_enabled' do
          expect(setting.usage_ping_enabled).to be_truthy
        end
      end
    end
  end

  describe '#allowed_key_types' do
    it 'includes all key types by default' do
      expect(setting.allowed_key_types).to contain_exactly(*described_class::SUPPORTED_KEY_TYPES)
    end

    it 'excludes disabled key types' do
      expect(setting.allowed_key_types).to include(:ed25519)

      setting.ed25519_key_restriction = described_class::FORBIDDEN_KEY_VALUE

      expect(setting.allowed_key_types).not_to include(:ed25519)
    end
  end

  describe '#key_restriction_for' do
    it 'returns the restriction value for recognised types' do
      setting.rsa_key_restriction = 1024

      expect(setting.key_restriction_for(:rsa)).to eq(1024)
    end

    it 'allows types to be passed as a string' do
      setting.rsa_key_restriction = 1024

      expect(setting.key_restriction_for('rsa')).to eq(1024)
    end

    it 'returns forbidden for unrecognised type' do
      expect(setting.key_restriction_for(:foo)).to eq(described_class::FORBIDDEN_KEY_VALUE)
    end
  end

  describe '#allow_signup?' do
    it 'returns true' do
      expect(setting.allow_signup?).to be_truthy
    end

    it 'returns false if signup is disabled' do
      allow(setting).to receive(:signup_enabled?).and_return(false)

      expect(setting.allow_signup?).to be_falsey
    end

    it 'returns false if password authentication is disabled for the web interface' do
      allow(setting).to receive(:password_authentication_enabled_for_web?).and_return(false)

      expect(setting.allow_signup?).to be_falsey
    end
  end

  describe '#pick_repository_storage' do
    it 'uses Array#sample to pick a random storage' do
      array = double('array', sample: 'random')
      expect(setting).to receive(:repository_storages).and_return(array)

      expect(setting.pick_repository_storage).to eq('random')
    end
  end

  describe '#user_default_internal_regex_enabled?' do
    using RSpec::Parameterized::TableSyntax

    where(:user_default_external, :user_default_internal_regex, :result) do
      false | nil                        | false
      false | ''                         | false
      false | '^(?:(?!\.ext@).)*$\r?\n?' | false
      true  | ''                         | false
      true  | nil                        | false
      true  | '^(?:(?!\.ext@).)*$\r?\n?' | true
    end

    with_them do
      before do
        setting.user_default_external = user_default_external
        setting.user_default_internal_regex = user_default_internal_regex
      end

      subject { setting.user_default_internal_regex_enabled? }

      it { is_expected.to eq(result) }
    end
  end

  describe '#archive_builds_older_than' do
    subject { setting.archive_builds_older_than }

    context 'when the archive_builds_in_seconds is set' do
      before do
        setting.archive_builds_in_seconds = 3600
      end

      it { is_expected.to be_within(1.minute).of(1.hour.ago) }
    end

    context 'when the archive_builds_in_seconds is set' do
      before do
        setting.archive_builds_in_seconds = nil
      end

      it { is_expected.to be_nil }
    end
  end

  describe '#commit_email_hostname' do
    context 'when the value is provided' do
      before do
        setting.commit_email_hostname = 'localhost'
      end

      it 'returns the provided value' do
        expect(setting.commit_email_hostname).to eq('localhost')
      end
    end

    context 'when the value is not provided' do
      it 'returns the default from the class' do
        expect(setting.commit_email_hostname)
          .to eq(described_class.default_commit_email_hostname)
      end
    end
  end

  it 'predicate method changes when value is updated' do
    setting.password_authentication_enabled_for_web = false

    expect(setting.password_authentication_enabled_for_web?).to be_falsey
  end
end