summaryrefslogtreecommitdiff
path: root/spec/requests/members/mailgun/permanent_failure_spec.rb
blob: e47aedf8e9411bf3380b3de67d0fc2701f275daf (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
126
127
128
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe 'receive a permanent failure' do
  describe 'POST /members/mailgun/permanent_failures', :aggregate_failures do
    let_it_be(:member) { create(:project_member, :invited) }

    let(:raw_invite_token) { member.raw_invite_token }
    let(:mailgun_events) { true }
    let(:mailgun_signing_key) { 'abc123' }

    subject(:post_request) { post members_mailgun_permanent_failures_path(standard_params) }

    before do
      stub_application_setting(mailgun_events_enabled: mailgun_events, mailgun_signing_key: mailgun_signing_key)
    end

    it 'marks the member invite email success as false' do
      expect { post_request }.to change { member.reload.invite_email_success }.from(true).to(false)

      expect(response).to have_gitlab_http_status(:ok)
    end

    context 'when the change to a member is not made' do
      context 'with incorrect signing key' do
        context 'with incorrect signing key' do
          let(:mailgun_signing_key) { '_foobar_' }

          it 'does not change member status and responds as not_found' do
            expect { post_request }.not_to change { member.reload.invite_email_success }

            expect(response).to have_gitlab_http_status(:not_found)
          end
        end

        context 'with nil signing key' do
          let(:mailgun_signing_key) { nil }

          it 'does not change member status and responds as not_found' do
            expect { post_request }.not_to change { member.reload.invite_email_success }

            expect(response).to have_gitlab_http_status(:not_found)
          end
        end
      end

      context 'when the feature is not enabled' do
        let(:mailgun_events) { false }

        it 'does not change member status and responds as expected' do
          expect { post_request }.not_to change { member.reload.invite_email_success }

          expect(response).to have_gitlab_http_status(:not_acceptable)
        end
      end

      context 'when it is not an invite email' do
        before do
          stub_const('::Members::Mailgun::INVITE_EMAIL_TAG', '_foobar_')
        end

        it 'does not change member status and responds as expected' do
          expect { post_request }.not_to change { member.reload.invite_email_success }

          expect(response).to have_gitlab_http_status(:not_acceptable)
        end
      end
    end

    def standard_params
      {
        "signature": {
          "timestamp": "1625056677",
          "token": "eb944d0ace7227667a1b97d2d07276ae51d2b849ed2cfa68f3",
          "signature": "9790cc6686eb70f0b1f869180d906870cdfd496d27fee81da0aa86b9e539e790"
        },
        "event-data": {
          "severity": "permanent",
          "tags": ["invite_email"],
          "timestamp": 1521233195.375624,
          "storage": {
            "url": "_anything_",
            "key": "_anything_"
          },
          "log-level": "error",
          "id": "_anything_",
          "campaigns": [],
          "reason": "suppress-bounce",
          "user-variables": {
            "invite_token": raw_invite_token
          },
          "flags": {
            "is-routed": false,
            "is-authenticated": true,
            "is-system-test": false,
            "is-test-mode": false
          },
          "recipient-domain": "example.com",
          "envelope": {
            "sender": "bob@mg.gitlab.com",
            "transport": "smtp",
            "targets": "alice@example.com"
          },
          "message": {
            "headers": {
              "to": "Alice <alice@example.com>",
              "message-id": "20130503192659.13651.20287@mg.gitlab.com",
              "from": "Bob <bob@mg.gitlab.com>",
              "subject": "Test permanent_fail webhook"
            },
            "attachments": [],
            "size": 111
          },
          "recipient": "alice@example.com",
          "event": "failed",
          "delivery-status": {
            "attempt-no": 1,
            "message": "",
            "code": 605,
            "description": "Not delivering to previously bounced address",
            "session-seconds": 0
          }
        }
      }
    end
  end
end