summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/mail_room/mail_room_spec.rb
blob: cb3e214d38b42e9c068b2621be4a39975a957934 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# frozen_string_literal: true

require 'spec_helper'

describe Gitlab::MailRoom do
  let(:default_port) { 143 }
  let(:default_config) do
    {
      enabled: false,
      port: default_port,
      ssl: false,
      start_tls: false,
      mailbox: 'inbox',
      idle_timeout: 60,
      log_path: Rails.root.join('log', 'mail_room_json.log').to_s
    }
  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
      before do
        allow(File).to receive(:exist?).and_return false
      end

      it 'returns an empty hash' do
        expect(described_class.config).to be_empty
      end
    end

    before do
      allow(described_class).to receive(:load_from_yaml).and_return(default_config)
    end

    it 'sets up config properly' do
      expected_result = default_config

      expect(described_class.config).to match expected_result
    end

    context 'when a config value is missing from the yml file' do
      it 'overwrites missing values with the default' do
        stub_config(port: nil)

        expect(described_class.config[:port]).to eq default_port
      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

      target_proc = proc { described_class.config[:redis_url] }

      it_behaves_like 'only truthy if both enabled and address are truthy', target_proc
    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')

          new_path = Pathname.new(described_class.config[: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)

          expect(described_class.config[:log_path]).to eq new_path
        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