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

chat_notification_service_spec.rb « project_services « models « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 476d99364b6d33f1075195f33a503ed47b07a069 (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe ChatNotificationService do
  describe 'Associations' do
    before do
      allow(subject).to receive(:activated?).and_return(true)
    end

    it { is_expected.to validate_presence_of :webhook }
  end

  describe '#can_test?' do
    context 'with empty repository' do
      it 'returns true' do
        subject.project = create(:project, :empty_repo)

        expect(subject.can_test?).to be true
      end
    end

    context 'with repository' do
      it 'returns true' do
        subject.project = create(:project, :repository)

        expect(subject.can_test?).to be true
      end
    end
  end

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

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

    before do
      allow(chat_service).to receive_messages(
        project: project,
        project_id: project.id,
        service_hook: true,
        webhook: webhook_url
      )

      WebMock.stub_request(:post, webhook_url)

      subject.active = true
    end

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

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

        expect(chat_service).to receive(:notify).and_return(true)
        expect(chat_service.execute(data)).to be true
      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_service).to receive(:get_message).with(any_args, hash_including(project_name: 'Project Name'))
        chat_service.execute(data)
      end
    end

    context 'when the data object has a label' do
      let(:label) { create(:label, project: project, name: 'Bug')}
      let(:issue) { create(:labeled_issue, project: project, labels: [label]) }
      let(:note) { create(:note, noteable: issue, project: project)}
      let(:data) { Gitlab::DataBuilder::Note.build(note, user) }

      it 'notifies the chat service' do
        expect(chat_service).to receive(:notify).with(any_args)

        chat_service.execute(data)
      end

      context 'and the chat_service has a label filter that does not matches the label' do
        subject(:chat_service) { described_class.new(labels_to_be_notified: '~some random label') }

        it 'does not notify the chat service' do
          expect(chat_service).not_to receive(:notify)

          chat_service.execute(data)
        end
      end

      context 'and the chat_service has a label filter that matches the label' do
        subject(:chat_service) { described_class.new(labels_to_be_notified: '~Backend, ~Bug') }

        it 'notifies the chat service' do
          expect(chat_service).to receive(:notify).with(any_args)

          chat_service.execute(data)
        end
      end
    end

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

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

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

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

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

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

      it 'notifies all channels' do
        expect(chat_service).to receive(:notify).with(any_args, hash_including(channel: expected_channels)).and_return(true)
        expect(chat_service.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
end