BigW Consortium Gitlab

stub_configuration.rb 2.63 KB
Newer Older
1 2
module StubConfiguration
  def stub_application_setting(messages)
3 4 5 6
    add_predicates(messages)

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

12 13 14 15 16
  def stub_not_protect_default_branch
    stub_application_setting(
      default_branch_protection: Gitlab::Access::PROTECTION_NONE)
  end

17
  def stub_config_setting(messages)
18
    allow(Gitlab.config.gitlab).to receive_messages(to_settings(messages))
19 20 21
  end

  def stub_gravatar_setting(messages)
22
    allow(Gitlab.config.gravatar).to receive_messages(to_settings(messages))
23
  end
24

25
  def stub_incoming_email_setting(messages)
26
    allow(Gitlab.config.incoming_email).to receive_messages(to_settings(messages))
27 28
  end

29
  def stub_mattermost_setting(messages)
30
    allow(Gitlab.config.mattermost).to receive_messages(to_settings(messages))
31 32
  end

33
  def stub_omniauth_setting(messages)
34 35 36 37 38
    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))
39 40
  end

41
  def stub_storage_settings(messages)
42 43
    # Default storage is always required
    messages['default'] ||= Gitlab.config.repositories.storages.default
44
    messages.each do |storage_name, storage_settings|
45
      storage_settings['path'] ||= TestEnv.repos_path
46 47 48 49 50 51
      storage_settings['failure_count_threshold'] ||= 10
      storage_settings['failure_wait_time'] ||= 30
      storage_settings['failure_reset_time'] ||= 1800
      storage_settings['storage_timeout'] ||= 5
    end

52
    allow(Gitlab.config.repositories).to receive(:storages).and_return(Settingslogic.new(messages))
53 54
  end

55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
  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
75 76 77 78 79 80 81 82 83 84 85

  # 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
86
end