summaryrefslogtreecommitdiff
path: root/spec/models/integrations/base_chat_notification_spec.rb
blob: 1527ffd727839587977472e7f441dc420d074178 (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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Integrations::BaseChatNotification, feature_category: :integrations do
  describe 'default values' do
    it { expect(subject.category).to eq(:chat) }
  end

  describe 'validations' do
    before do
      allow(subject).to receive(:activated?).and_return(true)
      allow(subject).to receive(:default_channel_placeholder).and_return('placeholder')
      allow(subject).to receive(:webhook_help).and_return('help')
    end

    it { is_expected.to validate_presence_of :webhook }
    it { is_expected.to validate_inclusion_of(:labels_to_be_notified_behavior).in_array(%w[match_any match_all]).allow_blank }
  end

  describe '#execute' do
    subject(:chat_integration) { described_class.new }

    let_it_be(:project) { create(:project, :repository) }

    let(:user) { build_stubbed(:user) }
    let(:webhook_url) { 'https://example.gitlab.com/' }
    let(:data) { Gitlab::DataBuilder::Push.build_sample(subject.project, user) }

    before do
      allow(chat_integration).to receive_messages(
        project: project,
        project_id: project.id,
        webhook: webhook_url
      )

      WebMock.stub_request(:post, webhook_url) if webhook_url.present?

      subject.active = true
    end

    context 'with a repository' do
      it 'returns true' do
        expect(chat_integration).to receive(:notify).and_return(true)
        expect(chat_integration.execute(data)).to be true
      end
    end

    context 'with an empty repository' do
      it 'returns true' do
        subject.project = build_stubbed(:project, :empty_repo)

        expect(chat_integration).to receive(:notify).and_return(true)
        expect(chat_integration.execute(data)).to be true
      end
    end

    context 'when webhook is blank' do
      let(:webhook_url) { '' }

      it 'returns false' do
        expect(chat_integration).not_to receive(:notify)
        expect(chat_integration.execute(data)).to be false
      end

      context 'when webhook is not required' do
        it 'returns true' do
          allow(chat_integration).to receive(:requires_webhook?).and_return(false)

          expect(chat_integration).to receive(:notify).and_return(true)
          expect(chat_integration.execute(data)).to be true
        end
      end
    end

    context 'when event is not supported' do
      it 'returns false' do
        allow(chat_integration).to receive(:supported_events).and_return(['foo'])

        expect(chat_integration).not_to receive(:notify)
        expect(chat_integration.execute(data)).to be false
      end
    end

    context 'with a project with name containing spaces' do
      it 'does not remove spaces' do
        allow(project).to receive(:full_name).and_return('Project Name')

        expect(chat_integration).to receive(:get_message).with(any_args, hash_including(project_name: 'Project Name'))
        chat_integration.execute(data)
      end
    end

    context 'when the data object has a label' do
      let_it_be(:label) { create(:label, project: project, name: 'Bug') }
      let_it_be(:label_2) { create(:label, project: project, name: 'Community contribution') }
      let_it_be(:label_3) { create(:label, project: project, name: 'Backend') }
      let_it_be(:issue) { create(:labeled_issue, project: project, labels: [label, label_2, label_3]) }
      let_it_be(:note) { create(:note, noteable: issue, project: project) }

      let(:data) { Gitlab::DataBuilder::Note.build(note, user) }

      shared_examples 'notifies the chat integration' do
        specify do
          expect(chat_integration).to receive(:notify).with(any_args)

          chat_integration.execute(data)
        end
      end

      shared_examples 'does not notify the chat integration' do
        specify do
          expect(chat_integration).not_to receive(:notify).with(any_args)

          chat_integration.execute(data)
        end
      end

      it_behaves_like 'notifies the chat integration'

      context 'with label filter' do
        subject(:chat_integration) { described_class.new(labels_to_be_notified: '~Bug') }

        it_behaves_like 'notifies the chat integration'

        context 'MergeRequest events' do
          let(:data) { build_stubbed(:merge_request, source_project: project, labels: [label]).to_hook_data(user) }

          it_behaves_like 'notifies the chat integration'
        end

        context 'Issue events' do
          let(:data) { issue.to_hook_data(user) }

          it_behaves_like 'notifies the chat integration'
        end

        context 'Incident events' do
          let(:data) { issue.to_hook_data(user).merge!({ object_kind: 'incident' }) }

          it_behaves_like 'notifies the chat integration'
        end
      end

      context 'when labels_to_be_notified_behavior is not defined' do
        subject(:chat_integration) { described_class.new(labels_to_be_notified: label_filter) }

        context 'no matching labels' do
          let(:label_filter) { '~some random label' }

          it_behaves_like 'does not notify the chat integration'
        end

        context 'only one label matches' do
          let(:label_filter) { '~some random label, ~Bug' }

          it_behaves_like 'notifies the chat integration'
        end
      end

      context 'when labels_to_be_notified_behavior is blank' do
        subject(:chat_integration) { described_class.new(labels_to_be_notified: label_filter, labels_to_be_notified_behavior: '') }

        context 'no matching labels' do
          let(:label_filter) { '~some random label' }

          it_behaves_like 'does not notify the chat integration'
        end

        context 'only one label matches' do
          let(:label_filter) { '~some random label, ~Bug' }

          it_behaves_like 'notifies the chat integration'
        end
      end

      context 'when labels_to_be_notified_behavior is match_any' do
        subject(:chat_integration) do
          described_class.new(
            labels_to_be_notified: label_filter,
            labels_to_be_notified_behavior: 'match_any'
          )
        end

        context 'no label filter' do
          let(:label_filter) { nil }

          it_behaves_like 'notifies the chat integration'
        end

        context 'no matching labels' do
          let(:label_filter) { '~some random label' }

          it_behaves_like 'does not notify the chat integration'
        end

        context 'only one label matches' do
          let(:label_filter) { '~some random label, ~Bug' }

          it_behaves_like 'notifies the chat integration'
        end
      end

      context 'when labels_to_be_notified_behavior is match_all' do
        subject(:chat_integration) do
          described_class.new(
            labels_to_be_notified: label_filter,
            labels_to_be_notified_behavior: 'match_all'
          )
        end

        context 'no label filter' do
          let(:label_filter) { nil }

          it_behaves_like 'notifies the chat integration'
        end

        context 'no matching labels' do
          let(:label_filter) { '~some random label' }

          it_behaves_like 'does not notify the chat integration'
        end

        context 'only one label matches' do
          let(:label_filter) { '~some random label, ~Bug' }

          it_behaves_like 'does not notify the chat integration'
        end

        context 'labels matches exactly' do
          let(:label_filter) { '~Bug, ~Backend, ~Community contribution' }

          it_behaves_like 'notifies the chat integration'
        end

        context 'labels matches but object has more' do
          let(:label_filter) { '~Bug, ~Backend' }

          it_behaves_like 'notifies the chat integration'
        end

        context 'labels are distributed on multiple objects' do
          let(:label_filter) { '~Bug, ~Backend' }
          let(:data) do
            Gitlab::DataBuilder::Note.build(note, user).merge({
              issue: {
                labels: [
                  { title: 'Bug' }
                ]
              },
              merge_request: {
                labels: [
                  {
                    title: 'Backend'
                  }
                ]
              }
            })
          end

          it_behaves_like 'does not notify the chat integration'
        end
      end
    end

    context 'with "channel" property' do
      before do
        allow(chat_integration).to receive(:channel).and_return(channel)
      end

      context 'empty string' do
        let(:channel) { '' }

        it 'does not include the channel' do
          expect(chat_integration).to receive(:notify).with(any_args, hash_excluding(:channel)).and_return(true)
          expect(chat_integration.execute(data)).to be(true)
        end
      end

      context 'empty spaces' do
        let(:channel) { '  ' }

        it 'does not include the channel' do
          expect(chat_integration).to receive(:notify).with(any_args, hash_excluding(:channel)).and_return(true)
          expect(chat_integration.execute(data)).to be(true)
        end
      end
    end

    shared_examples 'with channel specified' do |channel, expected_channels|
      before do
        allow(chat_integration).to receive(:push_channel).and_return(channel)
      end

      it 'notifies all channels' do
        expect(chat_integration).to receive(:notify).with(any_args, hash_including(channel: expected_channels)).and_return(true)
        expect(chat_integration.execute(data)).to be(true)
      end
    end

    context 'with single channel specified' do
      it_behaves_like 'with channel specified', 'slack-integration', ['slack-integration']
    end

    context 'with multiple channel names specified' do
      it_behaves_like 'with channel specified', 'slack-integration,#slack-test', ['slack-integration', '#slack-test']
    end

    context 'with multiple channel names with spaces specified' do
      it_behaves_like 'with channel specified', 'slack-integration, #slack-test, @UDLP91W0A', ['slack-integration', '#slack-test', '@UDLP91W0A']
    end
  end

  describe '#default_channel_placeholder' do
    it 'raises an error' do
      expect { subject.default_channel_placeholder }.to raise_error(NotImplementedError)
    end
  end

  describe '#webhook_help' do
    it 'raises an error' do
      expect { subject.webhook_help }.to raise_error(NotImplementedError)
    end
  end

  describe '#event_channel_name' do
    it 'returns the channel field name for the given event' do
      expect(subject.event_channel_name(:event)).to eq('event_channel')
    end
  end

  describe '#event_channel_value' do
    it 'returns the channel field value for the given event' do
      subject.push_channel = '#pushes'

      expect(subject.event_channel_value(:push)).to eq('#pushes')
    end

    it 'raises an error for unsupported events' do
      expect { subject.event_channel_value(:foo) }.to raise_error(NoMethodError)
    end
  end
end