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

93 lines
2.6 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'
2020-07-28 23:09:34 +05:30
RSpec.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
2021-04-01 16:36:13 +05:30
it 'does nothing' do
expect { hipchat.execute(push_sample_data) }.not_to raise_error
2015-12-23 02:04:40 +05:30
end
2020-03-13 15:44:24 +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
end