debian-mirror-gitlab/spec/models/project_services/hipchat_service_spec.rb

410 lines
14 KiB
Ruby
Raw Normal View History

2019-07-07 11:18:12 +05:30
# frozen_string_literal: true
2015-04-26 12:48:37 +05:30
require 'spec_helper'
2017-09-10 17:25:29 +05:30
describe HipchatService do
2015-04-26 12:48:37 +05:30
describe "Associations" do
it { is_expected.to belong_to :project }
it { is_expected.to have_one :service_hook }
end
2016-06-02 11:05:42 +05:30
describe 'Validations' do
context 'when service is active' do
2017-09-10 17:25:29 +05:30
before do
subject.active = true
end
2016-06-02 11:05:42 +05:30
it { is_expected.to validate_presence_of(:token) }
end
context 'when service is inactive' do
2017-09-10 17:25:29 +05:30
before do
subject.active = false
end
2016-06-02 11:05:42 +05:30
it { is_expected.not_to validate_presence_of(:token) }
end
end
2015-04-26 12:48:37 +05:30
describe "Execute" do
2017-09-10 17:25:29 +05:30
let(:hipchat) { described_class.new }
2017-08-17 22:00:37 +05:30
let(:user) { create(:user) }
let(:project) { create(:project, :repository) }
2015-04-26 12:48:37 +05:30
let(:api_url) { 'https://hipchat.example.com/v2/room/123456/notification?auth_token=verySecret' }
2018-03-27 19:54:05 +05:30
let(:project_name) { project.full_name.gsub(/\s/, '') }
2015-09-11 14:41:01 +05:30
let(:token) { 'verySecret' }
let(:server_url) { 'https://hipchat.example.com'}
2016-09-13 17:45:13 +05:30
let(:push_sample_data) do
Gitlab::DataBuilder::Push.build_sample(project, user)
end
2015-04-26 12:48:37 +05:30
2018-03-17 18:26:18 +05:30
before do
2015-09-11 14:41:01 +05:30
allow(hipchat).to receive_messages(
2015-04-26 12:48:37 +05:30
project_id: project.id,
project: project,
room: 123456,
2015-09-11 14:41:01 +05:30
server: server_url,
token: token
2015-04-26 12:48:37 +05:30
)
WebMock.stub_request(:post, api_url)
end
2016-09-13 17:45:13 +05:30
it 'tests and return errors' do
2015-09-11 14:41:01 +05:30
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
2016-09-13 17:45:13 +05:30
it 'uses v1 if version is provided' do
2015-09-11 14:41:01 +05:30
allow(hipchat).to receive(:api_version).and_return('v1')
2015-12-23 02:04:40 +05:30
expect(HipChat::Client).to receive(:new).with(
token,
api_version: 'v1',
server_url: server_url
).and_return(double(:hipchat_service).as_null_object)
2015-09-11 14:41:01 +05:30
hipchat.execute(push_sample_data)
end
2016-09-13 17:45:13 +05:30
it 'uses v2 as the version when nothing is provided' do
2015-09-11 14:41:01 +05:30
allow(hipchat).to receive(:api_version).and_return('')
2015-12-23 02:04:40 +05:30
expect(HipChat::Client).to receive(:new).with(
token,
api_version: 'v2',
server_url: server_url
).and_return(double(:hipchat_service).as_null_object)
2015-09-11 14:41:01 +05:30
hipchat.execute(push_sample_data)
end
2015-04-26 12:48:37 +05:30
2015-09-11 14:41:01 +05:30
context 'push events' do
2016-09-13 17:45:13 +05:30
it "calls Hipchat API for push events" do
2015-04-26 12:48:37 +05:30
hipchat.execute(push_sample_data)
expect(WebMock).to have_requested(:post, api_url).once
end
2016-09-13 17:45:13 +05:30
it "creates a push message" do
2015-04-26 12:48:37 +05:30
message = hipchat.send(:create_push_message, push_sample_data)
2015-10-24 18:46:33 +05:30
push_sample_data[:object_attributes]
2015-04-26 12:48:37 +05:30
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
2016-09-13 17:45:13 +05:30
let(:push_sample_data) do
Gitlab::DataBuilder::Push.build(
2019-07-31 22:56:46 +05:30
project: project,
user: user,
oldrev: Gitlab::Git::BLANK_SHA,
newrev: '1' * 40,
ref: 'refs/tags/test')
2016-09-13 17:45:13 +05:30
end
2015-04-26 12:48:37 +05:30
2016-09-13 17:45:13 +05:30
it "calls Hipchat API for tag push events" do
2015-04-26 12:48:37 +05:30
hipchat.execute(push_sample_data)
expect(WebMock).to have_requested(:post, api_url).once
end
2016-09-13 17:45:13 +05:30
it "creates a tag push message" do
2015-04-26 12:48:37 +05:30
message = hipchat.send(:create_push_message, push_sample_data)
2015-10-24 18:46:33 +05:30
push_sample_data[:object_attributes]
2015-04-26 12:48:37 +05:30
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
end
context 'issue events' do
2017-08-17 22:00:37 +05:30
let(:issue) { create(:issue, title: 'Awesome issue', description: '**please** fix') }
2015-04-26 12:48:37 +05:30
let(:issue_service) { Issues::CreateService.new(project, user) }
let(:issues_sample_data) { issue_service.hook_data(issue, 'open') }
2016-09-13 17:45:13 +05:30
it "calls Hipchat API for issue events" do
2015-04-26 12:48:37 +05:30
hipchat.execute(issues_sample_data)
expect(WebMock).to have_requested(:post, api_url).once
end
2016-09-13 17:45:13 +05:30
it "creates an issue message" do
2015-04-26 12:48:37 +05:30
message = hipchat.send(:create_issue_message, issues_sample_data)
obj_attr = issues_sample_data[:object_attributes]
expect(message).to eq("#{user.name} opened " \
"<a href=\"#{obj_attr[:url]}\">issue ##{obj_attr["iid"]}</a> in " \
"<a href=\"#{project.web_url}\">#{project_name}</a>: " \
"<b>Awesome issue</b>" \
2017-08-17 22:00:37 +05:30
"<pre><strong>please</strong> fix</pre>")
2015-04-26 12:48:37 +05:30
end
end
context 'merge request events' do
2017-08-17 22:00:37 +05:30
let(:merge_request) { create(:merge_request, description: '**please** fix', title: 'Awesome merge request', target_project: project, source_project: project) }
2015-04-26 12:48:37 +05:30
let(:merge_service) { MergeRequests::CreateService.new(project, user) }
let(:merge_sample_data) { merge_service.hook_data(merge_request, 'open') }
2016-09-13 17:45:13 +05:30
it "calls Hipchat API for merge requests events" do
2015-04-26 12:48:37 +05:30
hipchat.execute(merge_sample_data)
expect(WebMock).to have_requested(:post, api_url).once
end
2016-09-13 17:45:13 +05:30
it "creates a merge request message" do
2015-04-26 12:48:37 +05:30
message = hipchat.send(:create_merge_request_message,
merge_sample_data)
obj_attr = merge_sample_data[:object_attributes]
expect(message).to eq("#{user.name} opened " \
2016-06-02 11:05:42 +05:30
"<a href=\"#{obj_attr[:url]}\">merge request !#{obj_attr["iid"]}</a> in " \
2015-04-26 12:48:37 +05:30
"<a href=\"#{project.web_url}\">#{project_name}</a>: " \
"<b>Awesome merge request</b>" \
2017-08-17 22:00:37 +05:30
"<pre><strong>please</strong> fix</pre>")
2015-04-26 12:48:37 +05:30
end
end
context "Note events" do
let(:user) { create(:user) }
2017-08-17 22:00:37 +05:30
let(:project) { create(:project, :repository, creator: user) }
2015-04-26 12:48:37 +05:30
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
2016-09-13 17:45:13 +05:30
it "calls Hipchat API for commit comment events" do
data = Gitlab::DataBuilder::Note.build(commit_note, user)
hipchat.execute(data)
2015-04-26 12:48:37 +05:30
expect(WebMock).to have_requested(:post, api_url).once
2015-04-26 12:48:37 +05:30
message = hipchat.send(:create_message, data)
2015-04-26 12:48:37 +05:30
obj_attr = data[:object_attributes]
commit_id = Commit.truncate_sha(data[:commit][:id])
title = hipchat.send(:format_title, data[:commit][:message])
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
2015-04-26 12:48:37 +05:30
end
context 'when merge request comment event triggered' do
let(:merge_request) do
create(:merge_request, source_project: project,
target_project: project)
end
2015-04-26 12:48:37 +05:30
let(:merge_request_note) do
create(:note_on_merge_request, noteable: merge_request,
project: project,
2017-08-17 22:00:37 +05:30
note: "merge request **note**")
end
2015-04-26 12:48:37 +05:30
2016-09-13 17:45:13 +05:30
it "calls Hipchat API for merge request comment events" do
data = Gitlab::DataBuilder::Note.build(merge_request_note, user)
hipchat.execute(data)
2015-04-26 12:48:37 +05:30
expect(WebMock).to have_requested(:post, api_url).once
2015-04-26 12:48:37 +05:30
message = hipchat.send(:create_message, data)
obj_attr = data[:object_attributes]
merge_id = data[:merge_request]['iid']
title = data[:merge_request]['title']
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>" \
2017-08-17 22:00:37 +05:30
"<pre>merge request <strong>note</strong></pre>")
end
2015-04-26 12:48:37 +05:30
end
context 'when issue comment event triggered' do
let(:issue) { create(:issue, project: project) }
let(:issue_note) do
create(:note_on_issue, noteable: issue, project: project,
2017-08-17 22:00:37 +05:30
note: "issue **note**")
end
2015-04-26 12:48:37 +05:30
2016-09-13 17:45:13 +05:30
it "calls Hipchat API for issue comment events" do
data = Gitlab::DataBuilder::Note.build(issue_note, user)
hipchat.execute(data)
2015-04-26 12:48:37 +05:30
message = hipchat.send(:create_message, data)
2015-04-26 12:48:37 +05:30
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>" \
2017-08-17 22:00:37 +05:30
"<pre>issue <strong>note</strong></pre>")
end
2018-04-05 14:03:07 +05:30
context 'with confidential issue' do
before do
issue.update!(confidential: true)
end
it 'calls Hipchat API with issue comment' do
data = Gitlab::DataBuilder::Note.build(issue_note, user)
hipchat.execute(data)
message = hipchat.send(:create_message, data)
expect(message).to include("<pre>issue <strong>note</strong></pre>")
end
end
2015-04-26 12:48:37 +05:30
end
context 'when snippet comment event triggered' do
let(:snippet) { create(:project_snippet, project: project) }
let(:snippet_note) do
create(:note_on_project_snippet, noteable: snippet,
project: project,
note: "snippet note")
end
2015-04-26 12:48:37 +05:30
2016-09-13 17:45:13 +05:30
it "calls Hipchat API for snippet comment events" do
data = Gitlab::DataBuilder::Note.build(snippet_note, user)
hipchat.execute(data)
2015-04-26 12:48:37 +05:30
expect(WebMock).to have_requested(:post, api_url).once
2015-04-26 12:48:37 +05:30
message = hipchat.send(:create_message, data)
2015-04-26 12:48:37 +05:30
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
2015-04-26 12:48:37 +05:30
end
end
2015-09-11 14:41:01 +05:30
2017-08-17 22:00:37 +05:30
context 'pipeline events' do
2019-09-04 21:01:54 +05:30
let(:pipeline) { create(:ci_empty_pipeline, user: project.owner) }
2017-08-17 22:00:37 +05:30
let(:data) { Gitlab::DataBuilder::Pipeline.build(pipeline) }
2015-12-23 02:04:40 +05:30
context 'for failed' do
2017-08-17 22:00:37 +05:30
before do
pipeline.drop
end
2015-12-23 02:04:40 +05:30
2016-09-13 17:45:13 +05:30
it "calls Hipchat API" do
2015-12-23 02:04:40 +05:30
hipchat.execute(data)
expect(WebMock).to have_requested(:post, api_url).once
end
2016-09-13 17:45:13 +05:30
it "creates a build message" do
2017-08-17 22:00:37 +05:30
message = hipchat.__send__(:create_pipeline_message, data)
2015-12-23 02:04:40 +05:30
project_url = project.web_url
2018-03-27 19:54:05 +05:30
project_name = project.full_name.gsub(/\s/, '')
2017-08-17 22:00:37 +05:30
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]
2015-12-23 02:04:40 +05:30
expect(message).to eq("<a href=\"#{project_url}\">#{project_name}</a>: " \
2017-08-17 22:00:37 +05:30
"Pipeline <a href=\"#{project_url}/pipelines/#{pipeline.id}\">##{pipeline.id}</a> " \
2015-12-23 02:04:40 +05:30
"of <a href=\"#{project_url}/commits/#{ref}\">#{ref}</a> #{ref_type} " \
2017-08-17 22:00:37 +05:30
"by #{user_name} failed in #{duration} second(s)")
2015-12-23 02:04:40 +05:30
end
end
context 'for succeeded' do
before do
2017-08-17 22:00:37 +05:30
pipeline.succeed
2015-12-23 02:04:40 +05:30
end
2016-09-13 17:45:13 +05:30
it "calls Hipchat API" do
2017-08-17 22:00:37 +05:30
hipchat.notify_only_broken_pipelines = false
2015-12-23 02:04:40 +05:30
hipchat.execute(data)
expect(WebMock).to have_requested(:post, api_url).once
end
2016-09-13 17:45:13 +05:30
it "notifies only broken" do
2017-08-17 22:00:37 +05:30
hipchat.notify_only_broken_pipelines = true
2015-12-23 02:04:40 +05:30
hipchat.execute(data)
expect(WebMock).not_to have_requested(:post, api_url).once
2015-12-23 02:04:40 +05:30
end
end
end
2020-03-09 13:42:32 +05:30
describe "#message_options" do
2016-09-13 17:45:13 +05:30
it "is set to the defaults" do
expect(hipchat.__send__(:message_options)).to eq({ notify: false, color: 'yellow' })
2015-09-11 14:41:01 +05:30
end
2016-09-13 17:45:13 +05:30
it "sets notify to true" do
2015-09-11 14:41:01 +05:30
allow(hipchat).to receive(:notify).and_return('1')
2016-09-13 17:45:13 +05:30
expect(hipchat.__send__(:message_options)).to eq({ notify: true, color: 'yellow' })
2015-09-11 14:41:01 +05:30
end
2016-09-13 17:45:13 +05:30
it "sets the color" do
2015-09-11 14:41:01 +05:30
allow(hipchat).to receive(:color).and_return('red')
2016-09-13 17:45:13 +05:30
expect(hipchat.__send__(:message_options)).to eq({ notify: false, color: 'red' })
end
context 'with a successful build' do
it 'uses the green color' do
2017-08-17 22:00:37 +05:30
data = { object_kind: 'pipeline',
object_attributes: { status: 'success' } }
2016-09-13 17:45:13 +05:30
2017-08-17 22:00:37 +05:30
expect(hipchat.__send__(:message_options, data)).to eq({ notify: false, color: 'green' })
2016-09-13 17:45:13 +05:30
end
end
context 'with a failed build' do
it 'uses the red color' do
2017-08-17 22:00:37 +05:30
data = { object_kind: 'pipeline',
object_attributes: { status: 'failed' } }
2016-09-13 17:45:13 +05:30
2017-08-17 22:00:37 +05:30
expect(hipchat.__send__(:message_options, data)).to eq({ notify: false, color: 'red' })
2016-09-13 17:45:13 +05:30
end
2015-09-11 14:41:01 +05:30
end
end
2015-04-26 12:48:37 +05:30
end
2018-11-18 11:00:15 +05:30
context 'with UrlBlocker' do
let(:user) { create(:user) }
let(:project) { create(:project, :repository) }
2019-07-07 11:18:12 +05:30
let(:hipchat) { create(:hipchat_service, project: project, properties: { room: 'test' }) }
2018-11-18 11:00:15 +05:30
let(:push_sample_data) { Gitlab::DataBuilder::Push.build_sample(project, user) }
describe '#execute' do
before do
hipchat.server = 'http://localhost:9123'
end
it 'raises UrlBlocker for localhost' do
expect(Gitlab::UrlBlocker).to receive(:validate!).and_call_original
expect { hipchat.execute(push_sample_data) }.to raise_error(Gitlab::HTTP::BlockedUrlError)
end
end
end
2015-04-26 12:48:37 +05:30
end