Welcome to mirror list, hosted at ThFree Co, Russian Federation.

incoming_email_spec.rb « gitlab « lib « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 72d201eed7769bd32cc23b44be9e8f5ee792a769 (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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# frozen_string_literal: true

require "spec_helper"

RSpec.describe Gitlab::IncomingEmail do
  describe "self.enabled?" do
    context "when reply by email is enabled" do
      before do
        stub_incoming_email_setting(enabled: true)
      end

      it 'returns true' do
        expect(described_class.enabled?).to be(true)
      end
    end

    context "when reply by email is disabled" do
      before do
        stub_incoming_email_setting(enabled: false)
      end

      it "returns false" do
        expect(described_class.enabled?).to be(false)
      end
    end
  end

  describe 'self.supports_wildcard?' do
    context 'address contains the wildcard placeholder' do
      before do
        stub_incoming_email_setting(address: 'replies+%{key}@example.com')
      end

      it 'confirms that wildcard is supported' do
        expect(described_class.supports_wildcard?).to be(true)
      end
    end

    context "address doesn't contain the wildcard placeholder" do
      before do
        stub_incoming_email_setting(address: 'replies@example.com')
      end

      it 'returns that wildcard is not supported' do
        expect(described_class.supports_wildcard?).to be(false)
      end
    end

    context 'address is not set' do
      before do
        stub_incoming_email_setting(address: nil)
      end

      it 'returns that wildcard is not supported' do
        expect(described_class.supports_wildcard?).to be(false)
      end
    end
  end

  context 'self.unsubscribe_address' do
    before do
      stub_incoming_email_setting(address: 'replies+%{key}@example.com')
    end

    it 'returns the address with interpolated reply key and unsubscribe suffix' do
      expect(described_class.unsubscribe_address('key')).to eq("replies+key#{Gitlab::IncomingEmail::UNSUBSCRIBE_SUFFIX}@example.com")
    end
  end

  context "self.reply_address" do
    before do
      stub_incoming_email_setting(address: "replies+%{key}@example.com")
    end

    it "returns the address with an interpolated reply key" do
      expect(described_class.reply_address("key")).to eq("replies+key@example.com")
    end
  end

  context "self.key_from_address" do
    before do
      stub_incoming_email_setting(address: "replies+%{key}@example.com")
    end

    it "returns reply key" do
      expect(described_class.key_from_address("replies+key@example.com")).to eq("key")
    end

    it 'does not match emails with extra bits' do
      expect(described_class.key_from_address('somereplies+somekey@example.com.someotherdomain.com')).to be nil
    end

    context 'when a custom wildcard address is used' do
      let(:wildcard_address) { 'custom.address+%{key}@example.com' }

      it 'finds key if email matches address pattern' do
        key = described_class.key_from_address(
          'custom.address+foo@example.com', wildcard_address: wildcard_address
        )
        expect(key).to eq('foo')
      end
    end
  end

  context 'self.key_from_fallback_message_id' do
    it 'returns reply key' do
      expect(described_class.key_from_fallback_message_id('reply-key@localhost')).to eq('key')
    end
  end

  context 'self.scan_fallback_references' do
    let(:references) do
      '<issue_1@localhost>' \
        ' <reply-59d8df8370b7e95c5a49fbf86aeb2c93@localhost>' \
        ',<exchange@microsoft.com>'
    end

    it 'returns reply key' do
      expect(described_class.scan_fallback_references(references))
        .to eq(%w[issue_1@localhost
                  reply-59d8df8370b7e95c5a49fbf86aeb2c93@localhost
                  exchange@microsoft.com])
    end
  end
end