stub_configuration.rb 3.17 KB
Newer Older
1 2 3
require 'active_support/core_ext/hash/transform_values'
require 'active_support/hash_with_indifferent_access'

4 5
module StubConfiguration
  def stub_application_setting(messages)
6 7 8 9
    add_predicates(messages)

    # Stubbing both of these because we're not yet consistent with how we access
    # current application settings
10
    allow_any_instance_of(ApplicationSetting).to receive_messages(to_settings(messages))
11
    allow(Gitlab::CurrentSettings.current_application_settings)
12
      .to receive_messages(to_settings(messages))
13 14 15

    # Ensure that we don't use the Markdown cache when stubbing these values
    allow_any_instance_of(ApplicationSetting).to receive(:cached_html_up_to_date?).and_return(false)
16 17
  end

18 19 20 21 22
  def stub_not_protect_default_branch
    stub_application_setting(
      default_branch_protection: Gitlab::Access::PROTECTION_NONE)
  end

23
  def stub_config_setting(messages)
24
    allow(Gitlab.config.gitlab).to receive_messages(to_settings(messages))
25 26 27
  end

  def stub_gravatar_setting(messages)
28
    allow(Gitlab.config.gravatar).to receive_messages(to_settings(messages))
29
  end
30

31
  def stub_incoming_email_setting(messages)
32
    allow(Gitlab.config.incoming_email).to receive_messages(to_settings(messages))
33 34
  end

35
  def stub_mattermost_setting(messages)
36
    allow(Gitlab.config.mattermost).to receive_messages(to_settings(messages))
37 38
  end

39
  def stub_omniauth_setting(messages)
40 41 42 43 44
    allow(Gitlab.config.omniauth).to receive_messages(to_settings(messages))
  end

  def stub_backup_setting(messages)
    allow(Gitlab.config.backup).to receive_messages(to_settings(messages))
45 46
  end

47 48 49 50
  def stub_lfs_setting(messages)
    allow(Gitlab.config.lfs).to receive_messages(to_settings(messages))
  end

51 52 53 54
  def stub_artifacts_setting(messages)
    allow(Gitlab.config.artifacts).to receive_messages(to_settings(messages))
  end

55
  def stub_storage_settings(messages)
56 57
    messages.deep_stringify_keys!

58 59
    # Default storage is always required
    messages['default'] ||= Gitlab.config.repositories.storages.default
60 61 62 63 64 65
    messages.each do |storage_name, storage_hash|
      if !storage_hash.key?('path') || storage_hash['path'] == Gitlab::GitalyClient::StorageSettings::Deprecated
        storage_hash['path'] = TestEnv.repos_path
      end

      messages[storage_name] = Gitlab::GitalyClient::StorageSettings.new(storage_hash.to_h)
66 67
    end

68
    allow(Gitlab.config.repositories).to receive(:storages).and_return(Settingslogic.new(messages))
69 70
  end

71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
  private

  # Modifies stubbed messages to also stub possible predicate versions
  #
  # Examples:
  #
  #   add_predicates(foo: true)
  #   # => {foo: true, foo?: true}
  #
  #   add_predicates(signup_enabled?: false)
  #   # => {signup_enabled? false}
  def add_predicates(messages)
    # Only modify keys that aren't already predicates
    keys = messages.keys.map(&:to_s).reject { |k| k.end_with?('?') }

    keys.each do |key|
      predicate = key + '?'
      messages[predicate.to_sym] = messages[key.to_sym]
    end
  end
91 92 93 94 95 96 97 98 99 100 101

  # Support nested hashes by converting all values into Settingslogic objects
  def to_settings(hash)
    hash.transform_values do |value|
      if value.is_a? Hash
        Settingslogic.new(value.deep_stringify_keys)
      else
        value
      end
    end
  end
102
end