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

service_desk_receiver_spec.rb « email « gitlab « lib « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 4b67020471a036d42aad3e1067ee147638dcd2fc (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::Email::ServiceDeskReceiver do
  let(:email) { fixture_file('emails/service_desk_custom_address.eml') }
  let(:receiver) { described_class.new(email) }

  context 'when the email contains a valid email address' do
    shared_examples 'received successfully' do
      it 'finds the service desk key' do
        expect { receiver.execute }.not_to raise_error
      end
    end

    before do
      stub_service_desk_email_setting(enabled: true, address: 'support+%{key}@example.com')

      handler = double(execute: true, metrics_event: true, metrics_params: true)
      expected_params = [
        an_instance_of(Mail::Message), nil,
        { service_desk_key: 'project_slug-project_key' }
      ]

      expect(Gitlab::Email::Handler::ServiceDeskHandler)
        .to receive(:new).with(*expected_params).and_return(handler)
    end

    context 'when in a To header' do
      it_behaves_like 'received successfully'
    end

    context 'when the email contains a valid email address in a header' do
      context 'when in a Delivered-To header' do
        let(:email) { fixture_file('emails/service_desk_custom_address_reply.eml') }

        it_behaves_like 'received successfully'
      end

      context 'when in a Envelope-To header' do
        let(:email) { fixture_file('emails/service_desk_custom_address_envelope_to.eml') }

        it_behaves_like 'received successfully'
      end

      context 'when in a X-Envelope-To header' do
        let(:email) { fixture_file('emails/service_desk_custom_address_x_envelope_to.eml') }

        it_behaves_like 'received successfully'
      end

      context 'when in a Cc header' do
        let(:email) do
          <<~EMAIL
          From: from@example.com
          To: to@example.com
          Cc: support+project_slug-project_key@example.com
          Subject: Issue titile

          Issue description
          EMAIL
        end

        it_behaves_like 'received successfully'
      end
    end
  end

  context 'when the email contains no key in the To header and contains reference header with no key' do
    let(:email) { fixture_file('emails/service_desk_reference_headers.eml') }

    before do
      stub_service_desk_email_setting(enabled: true, address: 'support+%{key}@example.com')
    end

    it 'sends a rejection email' do
      expect { receiver.execute }.to raise_error(Gitlab::Email::UnknownIncomingEmail)
    end
  end

  context 'when the email does not contain a valid email address' do
    before do
      stub_service_desk_email_setting(enabled: true, address: 'other_support+%{key}@example.com')
    end

    it 'raises an error' do
      expect { receiver.execute }.to raise_error(Gitlab::Email::UnknownIncomingEmail)
    end
  end
end