hipchat_service_spec.rb 13.2 KB
Newer Older
1 2 3 4 5 6 7
# == Schema Information
#
# Table name: services
#
#  id                    :integer          not null, primary key
#  type                  :string(255)
#  title                 :string(255)
Stan Hu's avatar
Stan Hu committed
8
#  project_id            :integer
9 10 11 12
#  created_at            :datetime
#  updated_at            :datetime
#  active                :boolean          default(FALSE), not null
#  properties            :text
Stan Hu's avatar
Stan Hu committed
13
#  template              :boolean          default(FALSE)
14 15 16 17
#  push_events           :boolean          default(TRUE)
#  issues_events         :boolean          default(TRUE)
#  merge_requests_events :boolean          default(TRUE)
#  tag_push_events       :boolean          default(TRUE)
Stan Hu's avatar
Stan Hu committed
18
#  note_events           :boolean          default(TRUE), not null
19 20 21 22
#

require 'spec_helper'

Douwe Maan's avatar
Douwe Maan committed
23
describe HipchatService, models: true do
24 25 26 27 28
  describe "Associations" do
    it { is_expected.to belong_to :project }
    it { is_expected.to have_one :service_hook }
  end

29 30 31 32 33 34 35 36 37 38 39 40 41 42
  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

43 44 45 46 47 48
  describe "Execute" do
    let(:hipchat) { HipchatService.new }
    let(:user)    { create(:user, username: 'username') }
    let(:project) { create(:project, name: 'project') }
    let(:api_url) { 'https://hipchat.example.com/v2/room/123456/notification?auth_token=verySecret' }
    let(:project_name) { project.name_with_namespace.gsub(/\s/, '') }
49 50 51
    let(:token) { 'verySecret' }
    let(:server_url) { 'https://hipchat.example.com'}
    let(:push_sample_data) { Gitlab::PushDataBuilder.build_sample(project, user) }
52 53

    before(:each) do
54
      allow(hipchat).to receive_messages(
55 56 57
        project_id: project.id,
        project: project,
        room: 123456,
58 59
        server: server_url,
        token: token
60 61 62 63
      )
      WebMock.stub_request(:post, api_url)
    end

64
    it 'tests and return errors' do
65 66 67 68 69 70 71
      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

72
    it 'uses v1 if version is provided' do
73
      allow(hipchat).to receive(:api_version).and_return('v1')
Gabriel Mazetto's avatar
Gabriel Mazetto committed
74 75 76 77 78
      expect(HipChat::Client).to receive(:new).with(
        token,
        api_version: 'v1',
        server_url: server_url
      ).and_return(double(:hipchat_service).as_null_object)
79 80
      hipchat.execute(push_sample_data)
    end
81

82
    it 'uses v2 as the version when nothing is provided' do
83
      allow(hipchat).to receive(:api_version).and_return('')
Gabriel Mazetto's avatar
Gabriel Mazetto committed
84 85 86 87 88
      expect(HipChat::Client).to receive(:new).with(
        token,
        api_version: 'v2',
        server_url: server_url
      ).and_return(double(:hipchat_service).as_null_object)
89 90 91 92
      hipchat.execute(push_sample_data)
    end

    context 'push events' do
93
      it "calls Hipchat API for push events" do
94 95 96 97
        hipchat.execute(push_sample_data)

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

99
      it "creates a push message" do
100 101
        message = hipchat.send(:create_push_message, push_sample_data)

102
        push_sample_data[:object_attributes]
103 104 105 106 107 108 109 110
        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
111
      let(:push_sample_data) { Gitlab::PushDataBuilder.build(project, user, Gitlab::Git::BLANK_SHA, '1' * 40, 'refs/tags/test', []) }
112

113
      it "calls Hipchat API for tag push events" do
114 115 116 117 118
        hipchat.execute(push_sample_data)

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

119
      it "creates a tag push message" do
120 121
        message = hipchat.send(:create_push_message, push_sample_data)

122
        push_sample_data[:object_attributes]
123 124 125 126
        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
127 128 129 130 131 132 133
    end

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

134
      it "calls Hipchat API for issue events" do
135 136 137 138 139
        hipchat.execute(issues_sample_data)

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

140
      it "creates an issue message" do
141 142 143
        message = hipchat.send(:create_issue_message, issues_sample_data)

        obj_attr = issues_sample_data[:object_attributes]
144 145
        expect(message).to eq("#{user.name} opened " \
            "<a href=\"#{obj_attr[:url]}\">issue ##{obj_attr["iid"]}</a> in " \
146 147 148 149 150 151 152 153 154 155 156
            "<a href=\"#{project.web_url}\">#{project_name}</a>: " \
            "<b>Awesome issue</b>" \
            "<pre>please fix</pre>")
      end
    end

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

157
      it "calls Hipchat API for merge requests events" do
158 159 160 161 162
        hipchat.execute(merge_sample_data)

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

163
      it "creates a merge request message" do
164 165 166 167
        message = hipchat.send(:create_merge_request_message,
                               merge_sample_data)

        obj_attr = merge_sample_data[:object_attributes]
168
        expect(message).to eq("#{user.name} opened " \
169
            "<a href=\"#{obj_attr[:url]}\">merge request !#{obj_attr["iid"]}</a> in " \
170 171 172 173 174
            "<a href=\"#{project.web_url}\">#{project_name}</a>: " \
            "<b>Awesome merge request</b>" \
            "<pre>please fix</pre>")
      end
    end
175 176 177 178 179

    context "Note events" do
      let(:user) { create(:user) }
      let(:project) { create(:project, creator_id: user.id) }

180 181 182 183 184 185
      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
186

187
        it "calls Hipchat API for commit comment events" do
188 189
          data = Gitlab::NoteDataBuilder.build(commit_note, user)
          hipchat.execute(data)
190

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

193
          message = hipchat.send(:create_message, data)
194

195 196 197
          obj_attr = data[:object_attributes]
          commit_id = Commit.truncate_sha(data[:commit][:id])
          title = hipchat.send(:format_title, data[:commit][:message])
198

199 200 201 202 203 204 205
          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}" \
              "<pre>a comment on a commit</pre>")
        end
      end
206

207 208 209 210 211
      context 'when merge request comment event triggered' do
        let(:merge_request) do
          create(:merge_request, source_project: project,
                                 target_project: project)
        end
212

213
        let(:merge_request_note) do
Grzegorz Bizon's avatar
Grzegorz Bizon committed
214
          create(:note_on_merge_request, noteable: merge_request,
215 216 217
                                         project: project,
                                         note: "merge request note")
        end
218

219
        it "calls Hipchat API for merge request comment events" do
220 221
          data = Gitlab::NoteDataBuilder.build(merge_request_note, user)
          hipchat.execute(data)
222

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

225
          message = hipchat.send(:create_message, data)
226

227 228 229
          obj_attr = data[:object_attributes]
          merge_id = data[:merge_request]['iid']
          title = data[:merge_request]['title']
230

231 232 233 234 235 236
          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>" \
              "<pre>merge request note</pre>")
        end
237 238
      end

239 240 241
      context 'when issue comment event triggered' do
        let(:issue) { create(:issue, project: project) }
        let(:issue_note) do
Grzegorz Bizon's avatar
Grzegorz Bizon committed
242
          create(:note_on_issue, noteable: issue, project: project,
243 244 245
                                 note: "issue note")
        end

246
        it "calls Hipchat API for issue comment events" do
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
          data = Gitlab::NoteDataBuilder.build(issue_note, user)
          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>" \
              "<pre>issue note</pre>")
        end
      end
263

264 265 266
      context 'when snippet comment event triggered' do
        let(:snippet) { create(:project_snippet, project: project) }
        let(:snippet_note) do
Grzegorz Bizon's avatar
Grzegorz Bizon committed
267
          create(:note_on_project_snippet, noteable: snippet,
268 269 270
                                           project: project,
                                           note: "snippet note")
        end
271

272
        it "calls Hipchat API for snippet comment events" do
273 274
          data = Gitlab::NoteDataBuilder.build(snippet_note, user)
          hipchat.execute(data)
275

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

278 279 280 281 282 283 284 285 286 287 288 289
          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>" \
              "<pre>snippet note</pre>")
        end
290 291
      end
    end
292

293 294 295 296 297 298 299
    context 'build events' do
      let(:build) { create(:ci_build) }
      let(:data) { Gitlab::BuildDataBuilder.build(build) }

      context 'for failed' do
        before { build.drop }

300
        it "calls Hipchat API" do
301 302 303 304 305
          hipchat.execute(data)

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

306
        it "creates a build message" do
307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
          message = hipchat.send(:create_build_message, data)

          project_url = project.web_url
          project_name = project.name_with_namespace.gsub(/\s/, '')
          sha = data[:sha]
          ref = data[:ref]
          ref_type = data[:tag] ? 'tag' : 'branch'
          duration = data[:commit][:duration]

          expect(message).to eq("<a href=\"#{project_url}\">#{project_name}</a>: " \
            "Commit <a href=\"#{project_url}/commit/#{sha}/builds\">#{Commit.truncate_sha(sha)}</a> " \
            "of <a href=\"#{project_url}/commits/#{ref}\">#{ref}</a> #{ref_type} " \
            "by #{data[:commit][:author_name]} failed in #{duration} second(s)")
        end
      end

      context 'for succeeded' do
        before do
          build.success
        end

328
        it "calls Hipchat API" do
329 330 331 332 333
          hipchat.notify_only_broken_builds = false
          hipchat.execute(data)
          expect(WebMock).to have_requested(:post, api_url).once
        end

334
        it "notifies only broken" do
335 336
          hipchat.notify_only_broken_builds = true
          hipchat.execute(data)
337
          expect(WebMock).not_to have_requested(:post, api_url).once
338 339 340 341
        end
      end
    end

342
    context "#message_options" do
343 344
      it "is set to the defaults" do
        expect(hipchat.__send__(:message_options)).to eq({ notify: false, color: 'yellow' })
345 346
      end

347
      it "sets notify to true" do
348
        allow(hipchat).to receive(:notify).and_return('1')
349 350

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

353
      it "sets the color" do
354
        allow(hipchat).to receive(:color).and_return('red')
355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372

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

      context 'with a successful build' do
        it 'uses the green color' do
          build_data = { object_kind: 'build', commit: { status: 'success' } }

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

      context 'with a failed build' do
        it 'uses the red color' do
          build_data = { object_kind: 'build', commit: { status: 'failed' } }

          expect(hipchat.__send__(:message_options, build_data)).to eq({ notify: false, color: 'red' })
        end
373 374
      end
    end
375 376
  end
end