2019-03-30 07:23:56 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-12-06 17:59:03 +00:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 18:09:03 +00:00
|
|
|
RSpec.describe ChatNotificationService do
|
2017-04-18 09:50:04 +00:00
|
|
|
describe 'Associations' do
|
2016-12-06 17:59:03 +00:00
|
|
|
before do
|
|
|
|
allow(subject).to receive(:activated?).and_return(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it { is_expected.to validate_presence_of :webhook }
|
|
|
|
end
|
2017-04-18 09:50:04 +00:00
|
|
|
|
|
|
|
describe '#can_test?' do
|
|
|
|
context 'with empty repository' do
|
2017-04-24 15:23:51 +00:00
|
|
|
it 'returns true' do
|
2017-08-02 19:55:11 +00:00
|
|
|
subject.project = create(:project, :empty_repo)
|
2017-04-18 09:50:04 +00:00
|
|
|
|
2017-04-24 15:23:51 +00:00
|
|
|
expect(subject.can_test?).to be true
|
2017-04-18 09:50:04 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with repository' do
|
|
|
|
it 'returns true' do
|
2017-08-01 18:51:52 +00:00
|
|
|
subject.project = create(:project, :repository)
|
2017-04-18 09:50:04 +00:00
|
|
|
|
|
|
|
expect(subject.can_test?).to be true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2018-08-30 09:57:39 +00:00
|
|
|
|
|
|
|
describe '#execute' do
|
2019-09-27 11:35:37 +00:00
|
|
|
subject(:chat_service) { described_class.new }
|
|
|
|
|
2018-08-30 09:57:39 +00:00
|
|
|
let(:user) { create(:user) }
|
|
|
|
let(:project) { create(:project, :repository) }
|
|
|
|
let(:webhook_url) { 'https://example.gitlab.com/' }
|
2020-02-17 18:09:00 +00:00
|
|
|
let(:data) { Gitlab::DataBuilder::Push.build_sample(subject.project, user) }
|
2018-08-30 09:57:39 +00:00
|
|
|
|
|
|
|
before do
|
|
|
|
allow(chat_service).to receive_messages(
|
|
|
|
project: project,
|
|
|
|
project_id: project.id,
|
|
|
|
service_hook: true,
|
|
|
|
webhook: webhook_url
|
|
|
|
)
|
|
|
|
|
|
|
|
WebMock.stub_request(:post, webhook_url)
|
|
|
|
|
|
|
|
subject.active = true
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with a repository' do
|
|
|
|
it 'returns true' do
|
2019-09-27 11:35:37 +00:00
|
|
|
expect(chat_service).to receive(:notify).and_return(true)
|
2018-08-30 09:57:39 +00:00
|
|
|
expect(chat_service.execute(data)).to be true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with an empty repository' do
|
|
|
|
it 'returns true' do
|
|
|
|
subject.project = create(:project, :empty_repo)
|
|
|
|
|
2019-09-27 11:35:37 +00:00
|
|
|
expect(chat_service).to receive(:notify).and_return(true)
|
2018-08-30 09:57:39 +00:00
|
|
|
expect(chat_service.execute(data)).to be true
|
|
|
|
end
|
|
|
|
end
|
2020-02-17 18:09:00 +00:00
|
|
|
|
|
|
|
context 'with a project with name containing spaces' do
|
|
|
|
it 'does not remove spaces' do
|
|
|
|
allow(project).to receive(:full_name).and_return('Project Name')
|
|
|
|
|
|
|
|
expect(chat_service).to receive(:get_message).with(any_args, hash_including(project_name: 'Project Name'))
|
|
|
|
chat_service.execute(data)
|
|
|
|
end
|
|
|
|
end
|
2020-02-25 21:09:23 +00:00
|
|
|
|
2020-04-08 15:09:29 +00:00
|
|
|
context 'with "channel" property' do
|
|
|
|
before do
|
|
|
|
allow(chat_service).to receive(:channel).and_return(channel)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'empty string' do
|
|
|
|
let(:channel) { '' }
|
|
|
|
|
|
|
|
it 'does not include the channel' do
|
|
|
|
expect(chat_service).to receive(:notify).with(any_args, hash_excluding(:channel)).and_return(true)
|
|
|
|
expect(chat_service.execute(data)).to be(true)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'empty spaces' do
|
|
|
|
let(:channel) { ' ' }
|
|
|
|
|
|
|
|
it 'does not include the channel' do
|
|
|
|
expect(chat_service).to receive(:notify).with(any_args, hash_excluding(:channel)).and_return(true)
|
|
|
|
expect(chat_service.execute(data)).to be(true)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-02-25 21:09:23 +00:00
|
|
|
shared_examples 'with channel specified' do |channel, expected_channels|
|
|
|
|
before do
|
|
|
|
allow(chat_service).to receive(:push_channel).and_return(channel)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'notifies all channels' do
|
|
|
|
expect(chat_service).to receive(:notify).with(any_args, hash_including(channel: expected_channels)).and_return(true)
|
|
|
|
expect(chat_service.execute(data)).to be(true)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with single channel specified' do
|
|
|
|
it_behaves_like 'with channel specified', 'slack-integration', ['slack-integration']
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with multiple channel names specified' do
|
|
|
|
it_behaves_like 'with channel specified', 'slack-integration,#slack-test', ['slack-integration', '#slack-test']
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with multiple channel names with spaces specified' do
|
|
|
|
it_behaves_like 'with channel specified', 'slack-integration, #slack-test, @UDLP91W0A', ['slack-integration', '#slack-test', '@UDLP91W0A']
|
|
|
|
end
|
2018-08-30 09:57:39 +00:00
|
|
|
end
|
2016-12-06 17:59:03 +00:00
|
|
|
end
|