debian-mirror-gitlab/spec/support/helpers/stub_configuration.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

194 lines
6.5 KiB
Ruby
Raw Normal View History

2019-10-12 21:52:04 +05:30
# frozen_string_literal: true
2018-10-15 14:42:47 +05:30
require 'active_support/hash_with_indifferent_access'
2018-12-05 23:21:45 +05:30
require 'active_support/dependencies'
2019-09-04 21:01:54 +05:30
# check gets rid of already initialized constant warnings when using spring
require_dependency 'gitlab' unless defined?(Gitlab)
2018-10-15 14:42:47 +05:30
2015-09-11 14:41:01 +05:30
module StubConfiguration
def stub_application_setting(messages)
add_predicates(messages)
# Stubbing both of these because we're not yet consistent with how we access
# current application settings
2017-09-10 17:25:29 +05:30
allow_any_instance_of(ApplicationSetting).to receive_messages(to_settings(messages))
allow(Gitlab::CurrentSettings.current_application_settings)
.to receive_messages(to_settings(messages))
2018-03-17 18:26:18 +05:30
# 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)
2017-09-10 17:25:29 +05:30
end
2023-03-04 22:38:38 +05:30
# For enums with `_prefix: true`, this allows us to stub the application setting properly
def stub_application_setting_enum(setting, value)
stub_application_setting(setting.to_sym => value)
ApplicationSetting.send(setting.pluralize.to_sym).each_key do |key|
stub_application_setting("#{setting}_#{key}".to_sym => key == value)
end
Gitlab::CurrentSettings.send(setting)
end
2017-09-10 17:25:29 +05:30
def stub_not_protect_default_branch
stub_application_setting(
default_branch_protection: Gitlab::Access::PROTECTION_NONE)
2015-09-11 14:41:01 +05:30
end
def stub_config_setting(messages)
2017-09-10 17:25:29 +05:30
allow(Gitlab.config.gitlab).to receive_messages(to_settings(messages))
2015-09-11 14:41:01 +05:30
end
2019-10-12 21:52:04 +05:30
def stub_config(messages)
allow(Gitlab.config).to receive_messages(to_settings(messages))
end
2020-10-24 23:57:45 +05:30
def stub_default_url_options(host: "localhost", protocol: "http", script_name: nil)
url_options = { host: host, protocol: protocol, script_name: script_name }
2019-02-15 15:39:39 +05:30
allow(Rails.application.routes).to receive(:default_url_options).and_return(url_options)
end
2023-01-13 00:05:48 +05:30
def stub_dependency_proxy_setting(messages)
allow(Gitlab.config.dependency_proxy).to receive_messages(to_settings(messages))
end
2015-09-11 14:41:01 +05:30
def stub_gravatar_setting(messages)
2017-09-10 17:25:29 +05:30
allow(Gitlab.config.gravatar).to receive_messages(to_settings(messages))
2015-09-11 14:41:01 +05:30
end
2015-09-25 12:07:36 +05:30
def stub_incoming_email_setting(messages)
2017-09-10 17:25:29 +05:30
allow(Gitlab.config.incoming_email).to receive_messages(to_settings(messages))
2015-09-25 12:07:36 +05:30
end
2017-08-17 22:00:37 +05:30
def stub_mattermost_setting(messages)
2017-09-10 17:25:29 +05:30
allow(Gitlab.config.mattermost).to receive_messages(to_settings(messages))
end
def stub_omniauth_setting(messages)
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))
end
2018-03-17 18:26:18 +05:30
def stub_lfs_setting(messages)
allow(Gitlab.config.lfs).to receive_messages(to_settings(messages))
end
2019-03-02 22:35:43 +05:30
def stub_external_diffs_setting(messages)
allow(Gitlab.config.external_diffs).to receive_messages(to_settings(messages))
end
2018-05-09 12:01:36 +05:30
def stub_artifacts_setting(messages)
allow(Gitlab.config.artifacts).to receive_messages(to_settings(messages))
end
2019-09-30 21:07:59 +05:30
def stub_pages_setting(messages)
allow(Gitlab.config.pages).to receive_messages(to_settings(messages))
end
2017-09-10 17:25:29 +05:30
def stub_storage_settings(messages)
2018-03-17 18:26:18 +05:30
messages.deep_stringify_keys!
2017-09-10 17:25:29 +05:30
# Default storage is always required
messages['default'] ||= Gitlab.config.repositories.storages.default
2018-05-09 12:01:36 +05:30
messages.each do |storage_name, storage_hash|
if !storage_hash.key?('path') || storage_hash['path'] == Gitlab::GitalyClient::StorageSettings::Deprecated
2022-11-25 23:54:43 +05:30
storage_hash['path'] = Gitlab::GitalyClient::StorageSettings.allow_disk_access { TestEnv.repos_path }
2018-05-09 12:01:36 +05:30
end
messages[storage_name] = Gitlab::GitalyClient::StorageSettings.new(storage_hash.to_h)
2017-09-10 17:25:29 +05:30
end
allow(Gitlab.config.repositories).to receive(:storages).and_return(Settingslogic.new(messages))
2017-08-17 22:00:37 +05:30
end
2022-05-07 20:08:51 +05:30
def stub_sentry_settings(enabled: true)
allow(Gitlab.config.sentry).to receive(:enabled) { enabled }
allow(Gitlab::CurrentSettings).to receive(:sentry_enabled?) { enabled }
dsn = 'dummy://b44a0828b72421a6d8e99efd68d44fa8@example.com/42'
allow(Gitlab.config.sentry).to receive(:dsn) { dsn }
allow(Gitlab::CurrentSettings).to receive(:sentry_dsn) { dsn }
clientside_dsn = 'dummy://b44a0828b72421a6d8e99efd68d44fa8@example.com/43'
allow(Gitlab.config.sentry).to receive(:clientside_dsn) { clientside_dsn }
allow(Gitlab::CurrentSettings)
.to receive(:sentry_clientside_dsn) { clientside_dsn }
2019-09-30 21:07:59 +05:30
end
2022-10-11 01:57:18 +05:30
def stub_microsoft_graph_mailer_setting(messages)
allow(Gitlab.config.microsoft_graph_mailer).to receive_messages(to_settings(messages))
end
2018-11-20 20:47:30 +05:30
def stub_kerberos_setting(messages)
allow(Gitlab.config.kerberos).to receive_messages(to_settings(messages))
end
2019-07-07 11:18:12 +05:30
def stub_gitlab_shell_setting(messages)
allow(Gitlab.config.gitlab_shell).to receive_messages(to_settings(messages))
end
2019-09-04 21:01:54 +05:30
def stub_asset_proxy_setting(messages)
allow(Gitlab.config.asset_proxy).to receive_messages(to_settings(messages))
end
2019-09-30 21:07:59 +05:30
def stub_rack_attack_setting(messages)
allow(Gitlab.config.rack_attack).to receive(:git_basic_auth).and_return(messages)
allow(Gitlab.config.rack_attack.git_basic_auth).to receive_messages(to_settings(messages))
2020-07-28 23:09:34 +05:30
end
def stub_service_desk_email_setting(messages)
allow(::Gitlab.config.service_desk_email).to receive_messages(to_settings(messages))
end
def stub_packages_setting(messages)
allow(::Gitlab.config.packages).to receive_messages(to_settings(messages))
2019-09-30 21:07:59 +05:30
end
2021-03-11 19:13:27 +05:30
def stub_maintenance_mode_setting(value)
allow(Gitlab::CurrentSettings).to receive(:current_application_settings?).and_return(true)
stub_application_setting(maintenance_mode: value)
end
2015-09-11 14:41:01 +05:30
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
2017-09-10 17:25:29 +05:30
# 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
2015-09-11 14:41:01 +05:30
end
2019-07-31 22:56:46 +05:30
require_relative '../../../ee/spec/support/helpers/ee/stub_configuration' if
Dir.exist?("#{__dir__}/../../../ee")
2019-12-04 20:38:33 +05:30
2021-06-08 01:23:25 +05:30
StubConfiguration.prepend_mod_with('StubConfiguration')