BigW Consortium Gitlab

hipchat_service_spec.rb 10.4 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 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 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 committed
18
#  note_events           :boolean          default(TRUE), not null
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
#

require 'spec_helper'

describe HipchatService do
  describe "Associations" do
    it { is_expected.to belong_to :project }
    it { is_expected.to have_one :service_hook }
  end

  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/, '') }
35 36 37
    let(:token) { 'verySecret' }
    let(:server_url) { 'https://hipchat.example.com'}
    let(:push_sample_data) { Gitlab::PushDataBuilder.build_sample(project, user) }
38 39

    before(:each) do
40
      allow(hipchat).to receive_messages(
41 42 43
        project_id: project.id,
        project: project,
        room: 123456,
44 45
        server: server_url,
        token: token
46 47 48 49
      )
      WebMock.stub_request(:post, api_url)
    end

50 51 52 53 54 55 56 57
    it 'should test and return errors' do
      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

58
    it 'should use v1 if version is provided' do
59
      allow(hipchat).to receive(:api_version).and_return('v1')
60 61 62 63 64 65 66 67
      expect(HipChat::Client).to receive(:new).
                                     with(token,
                                          api_version: 'v1',
                                          server_url: server_url).
                                     and_return(
                                         double(:hipchat_service).as_null_object)
      hipchat.execute(push_sample_data)
    end
68

69
    it 'should use v2 as the version when nothing is provided' do
70
      allow(hipchat).to receive(:api_version).and_return('')
71 72 73 74 75 76 77 78 79 80
      expect(HipChat::Client).to receive(:new).
                                     with(token,
                                          api_version: 'v2',
                                          server_url: server_url).
                                     and_return(
                                         double(:hipchat_service).as_null_object)
      hipchat.execute(push_sample_data)
    end

    context 'push events' do
81 82 83 84 85
      it "should call Hipchat API for push events" do
        hipchat.execute(push_sample_data)

        expect(WebMock).to have_requested(:post, api_url).once
      end
86 87 88 89 90 91 92 93 94 95 96 97 98

      it "should create a push message" do
        message = hipchat.send(:create_push_message, push_sample_data)

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

      it "should call Hipchat API for tag push events" do
        hipchat.execute(push_sample_data)

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

      it "should create a tag push message" do
        message = hipchat.send(:create_push_message, push_sample_data)

        obj_attr = push_sample_data[:object_attributes]
        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
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
    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') }

      it "should call Hipchat API for issue events" do
        hipchat.execute(issues_sample_data)

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

      it "should create an issue message" do
        message = hipchat.send(:create_issue_message, issues_sample_data)

        obj_attr = issues_sample_data[:object_attributes]
132 133
        expect(message).to eq("#{user.name} opened " \
            "<a href=\"#{obj_attr[:url]}\">issue ##{obj_attr["iid"]}</a> in " \
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
            "<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') }

      it "should call Hipchat API for merge requests events" do
        hipchat.execute(merge_sample_data)

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

      it "should create a merge request message" do
        message = hipchat.send(:create_merge_request_message,
                               merge_sample_data)

        obj_attr = merge_sample_data[:object_attributes]
156 157
        expect(message).to eq("#{user.name} opened " \
            "<a href=\"#{obj_attr[:url]}\">merge request ##{obj_attr["iid"]}</a> in " \
158 159 160 161 162
            "<a href=\"#{project.web_url}\">#{project_name}</a>: " \
            "<b>Awesome merge request</b>" \
            "<pre>please fix</pre>")
      end
    end
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186

    context "Note events" do
      let(:user) { create(:user) }
      let(:project) { create(:project, creator_id: user.id) }
      let(:issue)         { create(:issue, project: project) }
      let(:merge_request) { create(:merge_request, source_project: project, target_project: project) }
      let(:snippet)       { create(:project_snippet, project: project) }
      let(:commit_note) { create(:note_on_commit, author: user, project: project, commit_id: project.repository.commit.id, note: 'a comment on a commit') }
      let(:merge_request_note) { create(:note_on_merge_request, noteable_id: merge_request.id, note: "merge request note") }
      let(:issue_note) { create(:note_on_issue, noteable_id: issue.id, note: "issue note")}
      let(:snippet_note) { create(:note_on_project_snippet, noteable_id: snippet.id, note: "snippet note") }

      it "should call Hipchat API for commit comment events" do
        data = Gitlab::NoteDataBuilder.build(commit_note, user)
        hipchat.execute(data)

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

        message = hipchat.send(:create_message, data)

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

187
        expect(message).to eq("#{user.name} commented on " \
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
            "<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

      it "should call Hipchat API for merge request comment events" do
        data = Gitlab::NoteDataBuilder.build(merge_request_note, user)
        hipchat.execute(data)

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

        message = hipchat.send(:create_message, data)

        obj_attr = data[:object_attributes]
        merge_id = data[:merge_request]['iid']
        title = data[:merge_request]['title']

206
        expect(message).to eq("#{user.name} commented on " \
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
            "<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

      it "should call Hipchat API for issue comment events" do
        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']

223
        expect(message).to eq("#{user.name} commented on " \
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
            "<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

      it "should call Hipchat API for snippet comment events" do
        data = Gitlab::NoteDataBuilder.build(snippet_note, user)
        hipchat.execute(data)

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

        message = hipchat.send(:create_message, data)

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

242
        expect(message).to eq("#{user.name} commented on " \
243 244 245 246 247 248
            "<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
    end
249 250 251

    context "#message_options" do
      it "should be set to the defaults" do
252
        expect(hipchat.send(:message_options)).to eq({ notify: false, color: 'yellow' })
253 254 255
      end

      it "should set notfiy to true" do
256
        allow(hipchat).to receive(:notify).and_return('1')
257
        expect(hipchat.send(:message_options)).to eq({ notify: true, color: 'yellow' })
258 259 260
      end

      it "should set the color" do
261
        allow(hipchat).to receive(:color).and_return('red')
262
        expect(hipchat.send(:message_options)).to eq({ notify: false, color: 'red' })
263 264
      end
    end
265 266
  end
end