BigW Consortium Gitlab

hipchat_service_spec.rb 12.8 KB
Newer Older
1 2
require 'spec_helper'

Douwe Maan committed
3
describe HipchatService, models: true do
4 5 6 7 8
  describe "Associations" do
    it { is_expected.to belong_to :project }
    it { is_expected.to have_one :service_hook }
  end

9 10 11 12 13 14 15 16 17 18 19 20 21 22
  describe 'Validations' do
    context 'when service is active' do
      before { subject.active = true }

      it { is_expected.to validate_presence_of(:token) }
    end

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

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

23 24
  describe "Execute" do
    let(:hipchat) { HipchatService.new }
25 26
    let(:user)    { create(:user) }
    let(:project) { create(:project, :repository) }
27 28
    let(:api_url) { 'https://hipchat.example.com/v2/room/123456/notification?auth_token=verySecret' }
    let(:project_name) { project.name_with_namespace.gsub(/\s/, '') }
29 30
    let(:token) { 'verySecret' }
    let(:server_url) { 'https://hipchat.example.com'}
31
    let(:push_sample_data) do
32
      Gitlab::DataBuilder::Push.build_sample(project, user)
33
    end
34 35

    before(:each) do
36
      allow(hipchat).to receive_messages(
37 38 39
        project_id: project.id,
        project: project,
        room: 123456,
40 41
        server: server_url,
        token: token
42 43 44 45
      )
      WebMock.stub_request(:post, api_url)
    end

46
    it 'tests and return errors' do
47 48 49 50 51 52 53
      allow(hipchat).to receive(:execute).and_raise(StandardError, 'no such room')
      result = hipchat.test(push_sample_data)

      expect(result[:success]).to be_falsey
      expect(result[:result].to_s).to eq('no such room')
    end

54
    it 'uses v1 if version is provided' do
55
      allow(hipchat).to receive(:api_version).and_return('v1')
56 57 58 59 60
      expect(HipChat::Client).to receive(:new).with(
        token,
        api_version: 'v1',
        server_url: server_url
      ).and_return(double(:hipchat_service).as_null_object)
61 62
      hipchat.execute(push_sample_data)
    end
63

64
    it 'uses v2 as the version when nothing is provided' do
65
      allow(hipchat).to receive(:api_version).and_return('')
66 67 68 69 70
      expect(HipChat::Client).to receive(:new).with(
        token,
        api_version: 'v2',
        server_url: server_url
      ).and_return(double(:hipchat_service).as_null_object)
71 72 73 74
      hipchat.execute(push_sample_data)
    end

    context 'push events' do
75
      it "calls Hipchat API for push events" do
76 77 78 79
        hipchat.execute(push_sample_data)

        expect(WebMock).to have_requested(:post, api_url).once
      end
80

81
      it "creates a push message" do
82 83
        message = hipchat.send(:create_push_message, push_sample_data)

84
        push_sample_data[:object_attributes]
85 86 87 88 89 90 91 92
        branch = push_sample_data[:ref].gsub('refs/heads/', '')
        expect(message).to include("#{user.name} pushed to branch " \
            "<a href=\"#{project.web_url}/commits/#{branch}\">#{branch}</a> of " \
            "<a href=\"#{project.web_url}\">#{project_name}</a>")
      end
    end

    context 'tag_push events' do
93
      let(:push_sample_data) do
94
        Gitlab::DataBuilder::Push.build(
95 96 97 98 99 100 101
          project,
          user,
          Gitlab::Git::BLANK_SHA,
          '1' * 40,
          'refs/tags/test',
          [])
      end
102

103
      it "calls Hipchat API for tag push events" do
104 105 106 107 108
        hipchat.execute(push_sample_data)

        expect(WebMock).to have_requested(:post, api_url).once
      end

109
      it "creates a tag push message" do
110 111
        message = hipchat.send(:create_push_message, push_sample_data)

112
        push_sample_data[:object_attributes]
113 114 115 116
        expect(message).to eq("#{user.name} pushed new tag " \
            "<a href=\"#{project.web_url}/commits/test\">test</a> to " \
            "<a href=\"#{project.web_url}\">#{project_name}</a>\n")
      end
117 118 119
    end

    context 'issue events' do
120
      let(:issue) { create(:issue, title: 'Awesome issue', description: '**please** fix') }
121 122 123
      let(:issue_service) { Issues::CreateService.new(project, user) }
      let(:issues_sample_data) { issue_service.hook_data(issue, 'open') }

124
      it "calls Hipchat API for issue events" do
125 126 127 128 129
        hipchat.execute(issues_sample_data)

        expect(WebMock).to have_requested(:post, api_url).once
      end

130
      it "creates an issue message" do
131 132 133
        message = hipchat.send(:create_issue_message, issues_sample_data)

        obj_attr = issues_sample_data[:object_attributes]
134 135
        expect(message).to eq("#{user.name} opened " \
            "<a href=\"#{obj_attr[:url]}\">issue ##{obj_attr["iid"]}</a> in " \
136 137
            "<a href=\"#{project.web_url}\">#{project_name}</a>: " \
            "<b>Awesome issue</b>" \
138
            "<pre><strong>please</strong> fix</pre>")
139 140 141 142
      end
    end

    context 'merge request events' do
143
      let(:merge_request) { create(:merge_request, description: '**please** fix', title: 'Awesome merge request', target_project: project, source_project: project) }
144 145 146
      let(:merge_service) { MergeRequests::CreateService.new(project, user) }
      let(:merge_sample_data) { merge_service.hook_data(merge_request, 'open') }

147
      it "calls Hipchat API for merge requests events" do
148 149 150 151 152
        hipchat.execute(merge_sample_data)

        expect(WebMock).to have_requested(:post, api_url).once
      end

153
      it "creates a merge request message" do
154 155 156 157
        message = hipchat.send(:create_merge_request_message,
                               merge_sample_data)

        obj_attr = merge_sample_data[:object_attributes]
158
        expect(message).to eq("#{user.name} opened " \
159
            "<a href=\"#{obj_attr[:url]}\">merge request !#{obj_attr["iid"]}</a> in " \
160 161
            "<a href=\"#{project.web_url}\">#{project_name}</a>: " \
            "<b>Awesome merge request</b>" \
162
            "<pre><strong>please</strong> fix</pre>")
163 164
      end
    end
165 166 167

    context "Note events" do
      let(:user) { create(:user) }
168
      let(:project) { create(:project, :repository, creator: user) }
169

170 171 172 173 174 175
      context 'when commit comment event triggered' do
        let(:commit_note) do
          create(:note_on_commit, author: user, project: project,
                                  commit_id: project.repository.commit.id,
                                  note: 'a comment on a commit')
        end
176

177
        it "calls Hipchat API for commit comment events" do
178
          data = Gitlab::DataBuilder::Note.build(commit_note, user)
179
          hipchat.execute(data)
180

181
          expect(WebMock).to have_requested(:post, api_url).once
182

183
          message = hipchat.send(:create_message, data)
184

185 186 187
          obj_attr = data[:object_attributes]
          commit_id = Commit.truncate_sha(data[:commit][:id])
          title = hipchat.send(:format_title, data[:commit][:message])
188

189 190 191 192
          expect(message).to eq("#{user.name} commented on " \
              "<a href=\"#{obj_attr[:url]}\">commit #{commit_id}</a> in " \
              "<a href=\"#{project.web_url}\">#{project_name}</a>: " \
              "#{title}" \
193
              "<pre>a comment on a commit</pre>")
194 195
        end
      end
196

197 198 199 200 201
      context 'when merge request comment event triggered' do
        let(:merge_request) do
          create(:merge_request, source_project: project,
                                 target_project: project)
        end
202

203
        let(:merge_request_note) do
Grzegorz Bizon committed
204
          create(:note_on_merge_request, noteable: merge_request,
205
                                         project: project,
206
                                         note: "merge request **note**")
207
        end
208

209
        it "calls Hipchat API for merge request comment events" do
210
          data = Gitlab::DataBuilder::Note.build(merge_request_note, user)
211
          hipchat.execute(data)
212

213
          expect(WebMock).to have_requested(:post, api_url).once
214

215
          message = hipchat.send(:create_message, data)
216

217 218 219
          obj_attr = data[:object_attributes]
          merge_id = data[:merge_request]['iid']
          title = data[:merge_request]['title']
220

221 222 223 224
          expect(message).to eq("#{user.name} commented on " \
              "<a href=\"#{obj_attr[:url]}\">merge request !#{merge_id}</a> in " \
              "<a href=\"#{project.web_url}\">#{project_name}</a>: " \
              "<b>#{title}</b>" \
225
              "<pre>merge request <strong>note</strong></pre>")
226
        end
227 228
      end

229 230 231
      context 'when issue comment event triggered' do
        let(:issue) { create(:issue, project: project) }
        let(:issue_note) do
Grzegorz Bizon committed
232
          create(:note_on_issue, noteable: issue, project: project,
233
                                 note: "issue **note**")
234 235
        end

236
        it "calls Hipchat API for issue comment events" do
237
          data = Gitlab::DataBuilder::Note.build(issue_note, user)
238 239 240 241 242 243 244 245 246 247 248 249
          hipchat.execute(data)

          message = hipchat.send(:create_message, data)

          obj_attr = data[:object_attributes]
          issue_id = data[:issue]['iid']
          title = data[:issue]['title']

          expect(message).to eq("#{user.name} commented on " \
              "<a href=\"#{obj_attr[:url]}\">issue ##{issue_id}</a> in " \
              "<a href=\"#{project.web_url}\">#{project_name}</a>: " \
              "<b>#{title}</b>" \
250
              "<pre>issue <strong>note</strong></pre>")
251 252
        end
      end
253

254 255 256
      context 'when snippet comment event triggered' do
        let(:snippet) { create(:project_snippet, project: project) }
        let(:snippet_note) do
Grzegorz Bizon committed
257
          create(:note_on_project_snippet, noteable: snippet,
258 259 260
                                           project: project,
                                           note: "snippet note")
        end
261

262
        it "calls Hipchat API for snippet comment events" do
263
          data = Gitlab::DataBuilder::Note.build(snippet_note, user)
264
          hipchat.execute(data)
265

266
          expect(WebMock).to have_requested(:post, api_url).once
267

268 269 270 271 272 273 274 275 276 277
          message = hipchat.send(:create_message, data)

          obj_attr = data[:object_attributes]
          snippet_id = data[:snippet]['id']
          title = data[:snippet]['title']

          expect(message).to eq("#{user.name} commented on " \
              "<a href=\"#{obj_attr[:url]}\">snippet ##{snippet_id}</a> in " \
              "<a href=\"#{project.web_url}\">#{project_name}</a>: " \
              "<b>#{title}</b>" \
278
              "<pre>snippet note</pre>")
279
        end
280 281
      end
    end
282

283 284 285
    context 'pipeline events' do
      let(:pipeline) { create(:ci_empty_pipeline, user: create(:user)) }
      let(:data) { Gitlab::DataBuilder::Pipeline.build(pipeline) }
286 287

      context 'for failed' do
288 289 290
        before do
          pipeline.drop
        end
291

292
        it "calls Hipchat API" do
293 294 295 296 297
          hipchat.execute(data)

          expect(WebMock).to have_requested(:post, api_url).once
        end

298
        it "creates a build message" do
299
          message = hipchat.__send__(:create_pipeline_message, data)
300 301 302

          project_url = project.web_url
          project_name = project.name_with_namespace.gsub(/\s/, '')
303 304 305 306 307
          pipeline_attributes = data[:object_attributes]
          ref = pipeline_attributes[:ref]
          ref_type = pipeline_attributes[:tag] ? 'tag' : 'branch'
          duration = pipeline_attributes[:duration]
          user_name = data[:user][:name]
308 309

          expect(message).to eq("<a href=\"#{project_url}\">#{project_name}</a>: " \
310
            "Pipeline <a href=\"#{project_url}/pipelines/#{pipeline.id}\">##{pipeline.id}</a> " \
311
            "of <a href=\"#{project_url}/commits/#{ref}\">#{ref}</a> #{ref_type} " \
312
            "by #{user_name} failed in #{duration} second(s)")
313 314 315 316 317
        end
      end

      context 'for succeeded' do
        before do
318
          pipeline.succeed
319 320
        end

321
        it "calls Hipchat API" do
322
          hipchat.notify_only_broken_pipelines = false
323 324 325 326
          hipchat.execute(data)
          expect(WebMock).to have_requested(:post, api_url).once
        end

327
        it "notifies only broken" do
328
          hipchat.notify_only_broken_pipelines = true
329
          hipchat.execute(data)
330
          expect(WebMock).not_to have_requested(:post, api_url).once
331 332 333 334
        end
      end
    end

335
    context "#message_options" do
336 337
      it "is set to the defaults" do
        expect(hipchat.__send__(:message_options)).to eq({ notify: false, color: 'yellow' })
338 339
      end

340
      it "sets notify to true" do
341
        allow(hipchat).to receive(:notify).and_return('1')
342 343

        expect(hipchat.__send__(:message_options)).to eq({ notify: true, color: 'yellow' })
344 345
      end

346
      it "sets the color" do
347
        allow(hipchat).to receive(:color).and_return('red')
348 349 350 351 352 353

        expect(hipchat.__send__(:message_options)).to eq({ notify: false, color: 'red' })
      end

      context 'with a successful build' do
        it 'uses the green color' do
354 355
          data = { object_kind: 'pipeline',
                   object_attributes: { status: 'success' } }
356

357
          expect(hipchat.__send__(:message_options, data)).to eq({ notify: false, color: 'green' })
358 359 360 361 362
        end
      end

      context 'with a failed build' do
        it 'uses the red color' do
363 364
          data = { object_kind: 'pipeline',
                   object_attributes: { status: 'failed' } }
365

366
          expect(hipchat.__send__(:message_options, data)).to eq({ notify: false, color: 'red' })
367
        end
368 369
      end
    end
370 371
  end
end