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

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

require 'spec_helper'

RSpec.describe Gitlab::Mailgun::WebhookProcessors::FailureLogger do
  describe '#execute', :freeze_time, :clean_gitlab_redis_rate_limiting do
    let(:base_payload) do
      {
        'id' => 'U2kZkAiuScqcMTq-8Atz-Q',
        'event' => 'failed',
        'recipient' => 'recipient@gitlab.com',
        'reason' => 'bounce',
        'delivery-status' => {
          'code' => '421',
          'message' => '4.4.2 mxfront9g.mail.example.com Error: timeout exceeded'
        }
      }
    end

    context 'on permanent failure' do
      let(:processor) { described_class.new(base_payload.merge({ 'severity' => 'permanent' })) }

      it 'logs the failure immediately' do
        expect(Gitlab::ErrorTracking::Logger).to receive(:error).with(
          event: 'email_delivery_failure',
          mailgun_event_id: base_payload['id'],
          recipient: base_payload['recipient'],
          failure_type: 'permanent',
          failure_reason: base_payload['reason'],
          failure_code: base_payload['delivery-status']['code'],
          failure_message: base_payload['delivery-status']['message']
        )

        processor.execute
      end
    end

    context 'on temporary failure' do
      let(:processor) { described_class.new(base_payload.merge({ 'severity' => 'temporary' })) }

      before do
        allow(Gitlab::ApplicationRateLimiter).to receive(:rate_limits)
          .and_return(temporary_email_failure: { threshold: 1, interval: 1.minute })
      end

      context 'when threshold is not exceeded' do
        it 'increments counter but does not log the failure' do
          expect(Gitlab::ApplicationRateLimiter).to receive(:throttled?).with(
            :temporary_email_failure, scope: 'recipient@gitlab.com'
          ).and_call_original
          expect(Gitlab::ErrorTracking::Logger).not_to receive(:error)

          processor.execute
        end
      end

      context 'when threshold is exceeded' do
        before do
          processor.execute
        end

        it 'increments counter and logs the failure' do
          expect(Gitlab::ApplicationRateLimiter).to receive(:throttled?).with(
            :temporary_email_failure, scope: 'recipient@gitlab.com'
          ).and_call_original
          expect(Gitlab::ErrorTracking::Logger).to receive(:error).with(
            event: 'email_delivery_failure',
            mailgun_event_id: base_payload['id'],
            recipient: base_payload['recipient'],
            failure_type: 'temporary',
            failure_reason: base_payload['reason'],
            failure_code: base_payload['delivery-status']['code'],
            failure_message: base_payload['delivery-status']['message']
          )

          processor.execute
        end
      end
    end

    context 'on other events' do
      let(:processor) { described_class.new(base_payload.merge({ 'event' => 'delivered' })) }

      it 'does nothing' do
        expect(Gitlab::ErrorTracking::Logger).not_to receive(:error)
        expect(Gitlab::ApplicationRateLimiter).not_to receive(:throttled?)

        processor.execute
      end
    end
  end
end