From d5d3c03598df712550acf0c6463a61c6e7dcc19e Mon Sep 17 00:00:00 2001 From: GitLab Bot Date: Fri, 31 Jan 2020 21:08:52 +0000 Subject: Add latest changes from gitlab-org/gitlab@master --- spec/lib/gitlab/mail_room/mail_room_spec.rb | 134 ++++++++++++---------------- 1 file changed, 59 insertions(+), 75 deletions(-) (limited to 'spec/lib/gitlab/mail_room/mail_room_spec.rb') diff --git a/spec/lib/gitlab/mail_room/mail_room_spec.rb b/spec/lib/gitlab/mail_room/mail_room_spec.rb index 43218fc6e0d..5d41ee06263 100644 --- a/spec/lib/gitlab/mail_room/mail_room_spec.rb +++ b/spec/lib/gitlab/mail_room/mail_room_spec.rb @@ -4,9 +4,10 @@ require 'spec_helper' describe Gitlab::MailRoom do let(:default_port) { 143 } - let(:default_config) do + let(:yml_config) do { - enabled: false, + enabled: true, + address: 'address@example.com', port: default_port, ssl: false, start_tls: false, @@ -16,71 +17,73 @@ describe Gitlab::MailRoom do } end - shared_examples_for 'only truthy if both enabled and address are truthy' do |target_proc| - context 'with both enabled and address as truthy values' do - it 'is truthy' do - stub_config(enabled: true, address: 'localhost') + let(:custom_config) { {} } + let(:incoming_email_config) { yml_config.merge(custom_config) } + let(:service_desk_email_config) { yml_config.merge(custom_config) } - expect(target_proc.call).to be_truthy - end - end - - context 'with address only as truthy' do - it 'is falsey' do - stub_config(enabled: false, address: 'localhost') - - expect(target_proc.call).to be_falsey - end - end + let(:configs) do + { + incoming_email: incoming_email_config, + service_desk_email: service_desk_email_config + } + end - context 'with enabled only as truthy' do - it 'is falsey' do - stub_config(enabled: true, address: nil) + before do + described_class.instance_variable_set(:@enabled_configs, nil) + end - expect(target_proc.call).to be_falsey - end + describe '#enabled_configs' do + before do + allow(described_class).to receive(:load_yaml).and_return(configs) end - context 'with neither address nor enabled as truthy' do - it 'is falsey' do - stub_config(enabled: false, address: nil) - - expect(target_proc.call).to be_falsey + context 'when both email and address is set' do + it 'returns email configs' do + expect(described_class.enabled_configs.size).to eq(2) end end - end - - before do - described_class.reset_config! - allow(File).to receive(:exist?).and_return true - end - describe '#config' do - context 'if the yml file cannot be found' do + context 'when the yml file cannot be found' do before do - allow(File).to receive(:exist?).and_return false + allow(described_class).to receive(:config_file).and_return('not_existing_file') end - it 'returns an empty hash' do - expect(described_class.config).to be_empty + it 'returns an empty list' do + expect(described_class.enabled_configs).to be_empty end end - before do - allow(described_class).to receive(:load_from_yaml).and_return(default_config) + context 'when email is disabled' do + let(:custom_config) { { enabled: false } } + + it 'returns an empty list' do + expect(described_class.enabled_configs).to be_empty + end end - it 'sets up config properly' do - expected_result = default_config + context 'when email is enabled but address is not set' do + let(:custom_config) { { enabled: true, address: '' } } - expect(described_class.config).to match expected_result + it 'returns an empty list' do + expect(described_class.enabled_configs).to be_empty + end end context 'when a config value is missing from the yml file' do + let(:yml_config) { {} } + let(:custom_config) { { enabled: true, address: 'address@example.com' } } + it 'overwrites missing values with the default' do - stub_config(port: nil) + expect(described_class.enabled_configs.first[:port]).to eq(Gitlab::MailRoom::DEFAULT_CONFIG[:port]) + end + end + + context 'when only incoming_email config is present' do + let(:configs) { { incoming_email: incoming_email_config } } - expect(described_class.config[:port]).to eq default_port + it 'returns only encoming_email' do + expect(described_class.enabled_configs.size).to eq(1) + expect(described_class.enabled_configs.first[:worker]).to eq('EmailReceiverWorker') end end @@ -91,50 +94,31 @@ describe Gitlab::MailRoom do allow(Gitlab::Redis::Queues).to receive(:new).and_return(fake_redis_queues) end - target_proc = proc { described_class.config[:redis_url] } + it 'sets redis config' do + config = described_class.enabled_configs.first - it_behaves_like 'only truthy if both enabled and address are truthy', target_proc + expect(config[:redis_url]).to eq('localhost') + expect(config[:sentinels]).to eq('yes, them') + end end describe 'setting up the log path' do context 'if the log path is a relative path' do - it 'expands the log path to an absolute value' do - stub_config(log_path: 'tiny_log.log') + let(:custom_config) { { log_path: 'tiny_log.log' } } - new_path = Pathname.new(described_class.config[:log_path]) + it 'expands the log path to an absolute value' do + new_path = Pathname.new(described_class.enabled_configs.first[:log_path]) expect(new_path.absolute?).to be_truthy end end context 'if the log path is absolute path' do - it 'leaves the path as-is' do - new_path = '/dev/null' - stub_config(log_path: new_path) + let(:custom_config) { { log_path: '/dev/null' } } - expect(described_class.config[:log_path]).to eq new_path + it 'leaves the path as-is' do + expect(described_class.enabled_configs.first[:log_path]).to eq '/dev/null' end end end end - - describe '#enabled?' do - target_proc = proc { described_class.enabled? } - - it_behaves_like 'only truthy if both enabled and address are truthy', target_proc - end - - describe '#reset_config?' do - it 'resets config' do - described_class.instance_variable_set(:@config, { some_stuff: 'hooray' }) - - described_class.reset_config! - - expect(described_class.instance_variable_get(:@config)).to be_nil - end - end - - def stub_config(override_values) - modified_config = default_config.merge(override_values) - allow(described_class).to receive(:load_from_yaml).and_return(modified_config) - end end -- cgit v1.2.1