2019-12-16 07:07:43 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 14:09:03 -04:00
|
|
|
RSpec.describe Gitlab::MailRoom do
|
2019-12-16 07:07:43 -05:00
|
|
|
let(:default_port) { 143 }
|
2020-01-31 16:08:52 -05:00
|
|
|
let(:yml_config) do
|
2019-12-16 07:07:43 -05:00
|
|
|
{
|
2020-01-31 16:08:52 -05:00
|
|
|
enabled: true,
|
|
|
|
address: 'address@example.com',
|
2019-12-16 07:07:43 -05:00
|
|
|
port: default_port,
|
|
|
|
ssl: false,
|
|
|
|
start_tls: false,
|
|
|
|
mailbox: 'inbox',
|
|
|
|
idle_timeout: 60,
|
2020-05-12 20:07:50 -04:00
|
|
|
log_path: Rails.root.join('log', 'mail_room_json.log').to_s,
|
|
|
|
expunge_deleted: false
|
2019-12-16 07:07:43 -05:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2020-01-31 16:08:52 -05:00
|
|
|
let(:custom_config) { {} }
|
|
|
|
let(:incoming_email_config) { yml_config.merge(custom_config) }
|
|
|
|
let(:service_desk_email_config) { yml_config.merge(custom_config) }
|
2020-01-20 22:08:37 -05:00
|
|
|
|
2020-01-31 16:08:52 -05:00
|
|
|
let(:configs) do
|
|
|
|
{
|
|
|
|
incoming_email: incoming_email_config,
|
|
|
|
service_desk_email: service_desk_email_config
|
|
|
|
}
|
|
|
|
end
|
2020-01-20 22:08:37 -05:00
|
|
|
|
2020-01-31 16:08:52 -05:00
|
|
|
before do
|
|
|
|
described_class.instance_variable_set(:@enabled_configs, nil)
|
|
|
|
end
|
2020-01-20 22:08:37 -05:00
|
|
|
|
2020-01-31 16:08:52 -05:00
|
|
|
describe '#enabled_configs' do
|
|
|
|
before do
|
|
|
|
allow(described_class).to receive(:load_yaml).and_return(configs)
|
2020-01-20 22:08:37 -05:00
|
|
|
end
|
|
|
|
|
2020-01-31 16:08:52 -05:00
|
|
|
context 'when both email and address is set' do
|
|
|
|
it 'returns email configs' do
|
|
|
|
expect(described_class.enabled_configs.size).to eq(2)
|
2020-01-20 22:08:37 -05:00
|
|
|
end
|
|
|
|
end
|
2019-12-16 07:07:43 -05:00
|
|
|
|
2020-01-31 16:08:52 -05:00
|
|
|
context 'when the yml file cannot be found' do
|
2019-12-16 07:07:43 -05:00
|
|
|
before do
|
2020-01-31 16:08:52 -05:00
|
|
|
allow(described_class).to receive(:config_file).and_return('not_existing_file')
|
2019-12-16 07:07:43 -05:00
|
|
|
end
|
|
|
|
|
2020-01-31 16:08:52 -05:00
|
|
|
it 'returns an empty list' do
|
|
|
|
expect(described_class.enabled_configs).to be_empty
|
2019-12-16 07:07:43 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-01-31 16:08:52 -05:00
|
|
|
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
|
2019-12-16 07:07:43 -05:00
|
|
|
end
|
|
|
|
|
2020-01-31 16:08:52 -05:00
|
|
|
context 'when email is enabled but address is not set' do
|
|
|
|
let(:custom_config) { { enabled: true, address: '' } }
|
2019-12-16 07:07:43 -05:00
|
|
|
|
2020-01-31 16:08:52 -05:00
|
|
|
it 'returns an empty list' do
|
|
|
|
expect(described_class.enabled_configs).to be_empty
|
|
|
|
end
|
2019-12-16 07:07:43 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when a config value is missing from the yml file' do
|
2020-01-31 16:08:52 -05:00
|
|
|
let(:yml_config) { {} }
|
|
|
|
let(:custom_config) { { enabled: true, address: 'address@example.com' } }
|
|
|
|
|
2019-12-16 07:07:43 -05:00
|
|
|
it 'overwrites missing values with the default' do
|
2020-01-31 16:08:52 -05:00
|
|
|
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 } }
|
2019-12-16 07:07:43 -05:00
|
|
|
|
2020-01-31 16:08:52 -05:00
|
|
|
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')
|
2019-12-16 07:07:43 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'setting up redis settings' do
|
|
|
|
let(:fake_redis_queues) { double(url: "localhost", sentinels: "yes, them", sentinels?: true) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
allow(Gitlab::Redis::Queues).to receive(:new).and_return(fake_redis_queues)
|
|
|
|
end
|
|
|
|
|
2020-01-31 16:08:52 -05:00
|
|
|
it 'sets redis config' do
|
|
|
|
config = described_class.enabled_configs.first
|
2019-12-16 07:07:43 -05:00
|
|
|
|
2020-01-31 16:08:52 -05:00
|
|
|
expect(config[:redis_url]).to eq('localhost')
|
|
|
|
expect(config[:sentinels]).to eq('yes, them')
|
|
|
|
end
|
2019-12-16 07:07:43 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
describe 'setting up the log path' do
|
|
|
|
context 'if the log path is a relative path' do
|
2020-01-31 16:08:52 -05:00
|
|
|
let(:custom_config) { { log_path: 'tiny_log.log' } }
|
2019-12-16 07:07:43 -05:00
|
|
|
|
2020-01-31 16:08:52 -05:00
|
|
|
it 'expands the log path to an absolute value' do
|
|
|
|
new_path = Pathname.new(described_class.enabled_configs.first[:log_path])
|
2019-12-16 07:07:43 -05:00
|
|
|
expect(new_path.absolute?).to be_truthy
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'if the log path is absolute path' do
|
2020-01-31 16:08:52 -05:00
|
|
|
let(:custom_config) { { log_path: '/dev/null' } }
|
2019-12-16 07:07:43 -05:00
|
|
|
|
2020-01-31 16:08:52 -05:00
|
|
|
it 'leaves the path as-is' do
|
|
|
|
expect(described_class.enabled_configs.first[:log_path]).to eq '/dev/null'
|
2019-12-16 07:07:43 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|