hangouts_chat_service_spec.rb 6.63 KB
Newer Older
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
require 'spec_helper'

describe HangoutsChatService do
  describe 'Associations' do
    it { is_expected.to belong_to :project }
    it { is_expected.to have_one :service_hook }
  end

  describe 'Validations' do
    context 'when service is active' do
      before do
        subject.active = true
      end

      it { is_expected.to validate_presence_of(:webhook) }
      it_behaves_like 'issue tracker service URL attribute', :webhook
    end

    context 'when service is inactive' do
      before do
        subject.active = false
      end

      it { is_expected.not_to validate_presence_of(:webhook) }
    end
  end

  describe '#execute' do
    let(:user)    { create(:user) }
    let(:project) { create(:project, :repository) }
31
    let(:webhook_url) { 'https://example.gitlab.com/' }
32 33

    before do
34
      allow(subject).to receive_messages(
35 36 37 38 39 40 41 42 43
        project: project,
        project_id: project.id,
        service_hook: true,
        webhook: webhook_url
      )

      WebMock.stub_request(:post, webhook_url)
    end

44 45
    shared_examples 'Hangouts Chat service' do
      it 'calls Hangouts Chat API' do
46
        subject.execute(sample_data)
47

48 49 50 51
        expect(WebMock)
          .to have_requested(:post, webhook_url)
          .with { |req| req.body =~ /\A{"text":.+}\Z/ }
          .once
52 53 54
      end
    end

55
    context 'with push events' do
56
      let(:sample_data) do
57 58 59
        Gitlab::DataBuilder::Push.build_sample(project, user)
      end

60
      it_behaves_like 'Hangouts Chat service'
61 62 63 64

      it 'specifies the webhook when it is configured' do
        expect(HangoutsChat::Sender).to receive(:new).with(webhook_url).and_return(double(:hangouts_chat_service).as_null_object)

65 66 67
        subject.execute(sample_data)
      end

68 69
      context 'with not default branch' do
        let(:sample_data) do
70
          Gitlab::DataBuilder::Push.build(project, user, nil, nil, 'not-the-default-branch')
71 72 73 74
        end

        context 'when notify_only_default_branch enabled' do
          before do
75
            subject.notify_only_default_branch = true
76 77 78
          end

          it 'does not call the Hangouts Chat API' do
79
            result = subject.execute(sample_data)
80 81 82 83 84 85 86

            expect(result).to be_falsy
          end
        end

        context 'when notify_only_default_branch disabled' do
          before do
87
            subject.notify_only_default_branch = false
88 89 90 91
          end

          it_behaves_like 'Hangouts Chat service'
        end
92 93 94 95 96
      end
    end

    context 'with issue events' do
      let(:opts) { { title: 'Awesome issue', description: 'please fix' } }
97
      let(:sample_data) do
98 99 100 101 102
        service = Issues::CreateService.new(project, user, opts)
        issue = service.execute
        service.hook_data(issue, 'open')
      end

103
      it_behaves_like 'Hangouts Chat service'
104 105 106 107 108 109 110 111 112 113 114 115
    end

    context 'with merge events' do
      let(:opts) do
        {
          title: 'Awesome merge_request',
          description: 'please fix',
          source_branch: 'feature',
          target_branch: 'master'
        }
      end

116
      let(:sample_data) do
117 118 119 120 121 122 123 124 125
        service = MergeRequests::CreateService.new(project, user, opts)
        merge_request = service.execute
        service.hook_data(merge_request, 'open')
      end

      before do
        project.add_developer(user)
      end

126
      it_behaves_like 'Hangouts Chat service'
127 128 129 130 131 132 133 134 135 136 137 138
    end

    context 'with wiki page events' do
      let(:opts) do
        {
          title: 'Awesome wiki_page',
          content: 'Some text describing some thing or another',
          format: 'md',
          message: 'user created page: Awesome wiki_page'
        }
      end
      let(:wiki_page) { create(:wiki_page, wiki: project.wiki, attrs: opts) }
139
      let(:sample_data) { Gitlab::DataBuilder::WikiPage.build(wiki_page, user, 'create') }
140

141
      it_behaves_like 'Hangouts Chat service'
142 143
    end

144 145
    context 'with note events' do
      let(:sample_data) { Gitlab::DataBuilder::Note.build(note, user) }
146

147 148 149 150 151 152 153
      context 'with commit comment' do
        let(:note) do
          create(:note_on_commit, author: user,
                                  project: project,
                                  commit_id: project.repository.commit.id,
                                  note: 'a comment on a commit')
        end
154

155
        it_behaves_like 'Hangouts Chat service'
156 157
      end

158 159 160 161 162
      context 'with merge request comment' do
        let(:note) do
          create(:note_on_merge_request, project: project,
                                         note: 'merge request note')
        end
163

164
        it_behaves_like 'Hangouts Chat service'
165 166
      end

167 168 169 170
      context 'with issue comment' do
        let(:note) do
          create(:note_on_issue, project: project, note: 'issue note')
        end
171

172
        it_behaves_like 'Hangouts Chat service'
173 174
      end

175
      context 'with snippet comment' do
176 177 178 179
        let(:note) do
          create(:note_on_project_snippet, project: project,
                                           note: 'snippet note')
        end
180

181
        it_behaves_like 'Hangouts Chat service'
182 183 184
      end
    end

185 186 187 188 189
    context 'with pipeline events' do
      let(:pipeline) do
        create(:ci_pipeline,
               project: project, status: status,
               sha: project.commit.sha, ref: project.default_branch)
190
      end
191
      let(:sample_data) { Gitlab::DataBuilder::Pipeline.build(pipeline) }
192

193 194
      context 'with failed pipeline' do
        let(:status) { 'failed' }
195

196
        it_behaves_like 'Hangouts Chat service'
197 198
      end

199 200
      context 'with succeeded pipeline' do
        let(:status) { 'success' }
201

202 203
        context 'with default notify_only_broken_pipelines' do
          it 'does not call Hangouts Chat API' do
204
            result = subject.execute(sample_data)
205

206 207
            expect(result).to be_falsy
          end
208 209
        end

210 211
        context 'when notify_only_broken_pipelines is false' do
          before do
212
            subject.notify_only_broken_pipelines = false
213
          end
214

215 216
          it_behaves_like 'Hangouts Chat service'
        end
217 218
      end

219
      context 'with not default branch' do
220 221 222 223
        let(:pipeline) do
          create(:ci_pipeline, project: project, status: 'failed', ref: 'not-the-default-branch')
        end

224 225
        context 'when notify_only_default_branch enabled' do
          before do
226
            subject.notify_only_default_branch = true
227
          end
228

229
          it 'does not call the Hangouts Chat API' do
230
            result = subject.execute(sample_data)
231

232 233
            expect(result).to be_falsy
          end
234 235
        end

236 237
        context 'when notify_only_default_branch disabled' do
          before do
238
            subject.notify_only_default_branch = false
239
          end
240

241
          it_behaves_like 'Hangouts Chat service'
242 243 244 245 246
        end
      end
    end
  end
end