2019-03-30 03:23:56 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-12-06 12:59:03 -05:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2021-05-31 23:10:06 -04:00
|
|
|
RSpec.describe Integrations::BaseChatNotification do
|
2022-04-22 08:08:38 -04:00
|
|
|
describe 'validations' do
|
2016-12-06 12:59:03 -05:00
|
|
|
before do
|
|
|
|
allow(subject).to receive(:activated?).and_return(true)
|
2022-04-22 08:08:38 -04:00
|
|
|
allow(subject).to receive(:default_channel_placeholder).and_return('placeholder')
|
|
|
|
allow(subject).to receive(:webhook_placeholder).and_return('placeholder')
|
2016-12-06 12:59:03 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it { is_expected.to validate_presence_of :webhook }
|
2021-05-10 14:10:41 -04:00
|
|
|
it { is_expected.to validate_inclusion_of(:labels_to_be_notified_behavior).in_array(%w[match_any match_all]).allow_blank }
|
2021-04-28 08:10:09 -04:00
|
|
|
end
|
|
|
|
|
2018-08-30 05:57:39 -04:00
|
|
|
describe '#execute' do
|
2021-06-30 14:07:05 -04:00
|
|
|
subject(:chat_integration) { described_class.new }
|
2019-09-27 07:35:37 -04:00
|
|
|
|
2021-04-28 08:10:09 -04:00
|
|
|
let_it_be(:project) { create(:project, :repository) }
|
|
|
|
|
2018-08-30 05:57:39 -04:00
|
|
|
let(:user) { create(:user) }
|
|
|
|
let(:webhook_url) { 'https://example.gitlab.com/' }
|
2020-02-17 13:09:00 -05:00
|
|
|
let(:data) { Gitlab::DataBuilder::Push.build_sample(subject.project, user) }
|
2018-08-30 05:57:39 -04:00
|
|
|
|
|
|
|
before do
|
2021-06-30 14:07:05 -04:00
|
|
|
allow(chat_integration).to receive_messages(
|
2018-08-30 05:57:39 -04:00
|
|
|
project: project,
|
|
|
|
project_id: project.id,
|
|
|
|
webhook: webhook_url
|
|
|
|
)
|
|
|
|
|
|
|
|
WebMock.stub_request(:post, webhook_url)
|
|
|
|
|
|
|
|
subject.active = true
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with a repository' do
|
|
|
|
it 'returns true' do
|
2021-06-30 14:07:05 -04:00
|
|
|
expect(chat_integration).to receive(:notify).and_return(true)
|
|
|
|
expect(chat_integration.execute(data)).to be true
|
2018-08-30 05:57:39 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with an empty repository' do
|
|
|
|
it 'returns true' do
|
|
|
|
subject.project = create(:project, :empty_repo)
|
|
|
|
|
2021-06-30 14:07:05 -04:00
|
|
|
expect(chat_integration).to receive(:notify).and_return(true)
|
|
|
|
expect(chat_integration.execute(data)).to be true
|
2018-08-30 05:57:39 -04:00
|
|
|
end
|
|
|
|
end
|
2020-02-17 13:09:00 -05: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')
|
|
|
|
|
2021-06-30 14:07:05 -04:00
|
|
|
expect(chat_integration).to receive(:get_message).with(any_args, hash_including(project_name: 'Project Name'))
|
|
|
|
chat_integration.execute(data)
|
2020-02-17 13:09:00 -05:00
|
|
|
end
|
|
|
|
end
|
2020-02-25 16:09:23 -05:00
|
|
|
|
2021-02-04 16:09:06 -05:00
|
|
|
context 'when the data object has a label' do
|
2021-04-28 08:10:09 -04:00
|
|
|
let_it_be(:label) { create(:label, name: 'Bug') }
|
|
|
|
let_it_be(:label_2) { create(:label, name: 'Community contribution') }
|
|
|
|
let_it_be(:label_3) { create(:label, name: 'Backend') }
|
|
|
|
let_it_be(:issue) { create(:labeled_issue, project: project, labels: [label, label_2, label_3]) }
|
|
|
|
let_it_be(:note) { create(:note, noteable: issue, project: project) }
|
|
|
|
|
2021-02-04 16:09:06 -05:00
|
|
|
let(:data) { Gitlab::DataBuilder::Note.build(note, user) }
|
|
|
|
|
2021-06-30 14:07:05 -04:00
|
|
|
shared_examples 'notifies the chat integration' do
|
2021-04-28 08:10:09 -04:00
|
|
|
specify do
|
2021-06-30 14:07:05 -04:00
|
|
|
expect(chat_integration).to receive(:notify).with(any_args)
|
2021-04-28 08:10:09 -04:00
|
|
|
|
2021-06-30 14:07:05 -04:00
|
|
|
chat_integration.execute(data)
|
2021-04-28 08:10:09 -04:00
|
|
|
end
|
|
|
|
end
|
2021-02-04 16:09:06 -05:00
|
|
|
|
2021-06-30 14:07:05 -04:00
|
|
|
shared_examples 'does not notify the chat integration' do
|
2021-04-28 08:10:09 -04:00
|
|
|
specify do
|
2021-06-30 14:07:05 -04:00
|
|
|
expect(chat_integration).not_to receive(:notify).with(any_args)
|
2021-02-04 16:09:06 -05:00
|
|
|
|
2021-06-30 14:07:05 -04:00
|
|
|
chat_integration.execute(data)
|
2021-02-04 16:09:06 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-06-30 14:07:05 -04:00
|
|
|
it_behaves_like 'notifies the chat integration'
|
2021-05-27 02:10:47 -04:00
|
|
|
|
|
|
|
context 'with label filter' do
|
2021-06-30 14:07:05 -04:00
|
|
|
subject(:chat_integration) { described_class.new(labels_to_be_notified: '~Bug') }
|
2021-05-27 02:10:47 -04:00
|
|
|
|
2021-06-30 14:07:05 -04:00
|
|
|
it_behaves_like 'notifies the chat integration'
|
2021-05-27 02:10:47 -04:00
|
|
|
|
|
|
|
context 'MergeRequest events' do
|
|
|
|
let(:data) { create(:merge_request, labels: [label]).to_hook_data(user) }
|
|
|
|
|
2021-06-30 14:07:05 -04:00
|
|
|
it_behaves_like 'notifies the chat integration'
|
2021-05-27 02:10:47 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'Issue events' do
|
|
|
|
let(:data) { issue.to_hook_data(user) }
|
|
|
|
|
2021-06-30 14:07:05 -04:00
|
|
|
it_behaves_like 'notifies the chat integration'
|
2021-05-27 02:10:47 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-04-28 08:10:09 -04:00
|
|
|
context 'when labels_to_be_notified_behavior is not defined' do
|
2021-06-30 14:07:05 -04:00
|
|
|
subject(:chat_integration) { described_class.new(labels_to_be_notified: label_filter) }
|
2021-02-04 16:09:06 -05:00
|
|
|
|
2021-04-28 08:10:09 -04:00
|
|
|
context 'no matching labels' do
|
|
|
|
let(:label_filter) { '~some random label' }
|
2021-02-04 16:09:06 -05:00
|
|
|
|
2021-06-30 14:07:05 -04:00
|
|
|
it_behaves_like 'does not notify the chat integration'
|
2021-04-28 08:10:09 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'only one label matches' do
|
|
|
|
let(:label_filter) { '~some random label, ~Bug' }
|
|
|
|
|
2021-06-30 14:07:05 -04:00
|
|
|
it_behaves_like 'notifies the chat integration'
|
2021-04-28 08:10:09 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-05-10 14:10:41 -04:00
|
|
|
context 'when labels_to_be_notified_behavior is blank' do
|
2021-06-30 14:07:05 -04:00
|
|
|
subject(:chat_integration) { described_class.new(labels_to_be_notified: label_filter, labels_to_be_notified_behavior: '') }
|
2021-05-10 14:10:41 -04:00
|
|
|
|
|
|
|
context 'no matching labels' do
|
|
|
|
let(:label_filter) { '~some random label' }
|
|
|
|
|
2021-06-30 14:07:05 -04:00
|
|
|
it_behaves_like 'does not notify the chat integration'
|
2021-05-10 14:10:41 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'only one label matches' do
|
|
|
|
let(:label_filter) { '~some random label, ~Bug' }
|
|
|
|
|
2021-06-30 14:07:05 -04:00
|
|
|
it_behaves_like 'notifies the chat integration'
|
2021-05-10 14:10:41 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-04-28 08:10:09 -04:00
|
|
|
context 'when labels_to_be_notified_behavior is match_any' do
|
2021-06-30 14:07:05 -04:00
|
|
|
subject(:chat_integration) do
|
2021-04-28 08:10:09 -04:00
|
|
|
described_class.new(
|
|
|
|
labels_to_be_notified: label_filter,
|
|
|
|
labels_to_be_notified_behavior: 'match_any'
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'no label filter' do
|
|
|
|
let(:label_filter) { nil }
|
|
|
|
|
2021-06-30 14:07:05 -04:00
|
|
|
it_behaves_like 'notifies the chat integration'
|
2021-04-28 08:10:09 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'no matching labels' do
|
|
|
|
let(:label_filter) { '~some random label' }
|
|
|
|
|
2021-06-30 14:07:05 -04:00
|
|
|
it_behaves_like 'does not notify the chat integration'
|
2021-04-28 08:10:09 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'only one label matches' do
|
|
|
|
let(:label_filter) { '~some random label, ~Bug' }
|
|
|
|
|
2021-06-30 14:07:05 -04:00
|
|
|
it_behaves_like 'notifies the chat integration'
|
2021-04-28 08:10:09 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when labels_to_be_notified_behavior is match_all' do
|
2021-06-30 14:07:05 -04:00
|
|
|
subject(:chat_integration) do
|
2021-04-28 08:10:09 -04:00
|
|
|
described_class.new(
|
|
|
|
labels_to_be_notified: label_filter,
|
|
|
|
labels_to_be_notified_behavior: 'match_all'
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'no label filter' do
|
|
|
|
let(:label_filter) { nil }
|
|
|
|
|
2021-06-30 14:07:05 -04:00
|
|
|
it_behaves_like 'notifies the chat integration'
|
2021-04-28 08:10:09 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'no matching labels' do
|
|
|
|
let(:label_filter) { '~some random label' }
|
|
|
|
|
2021-06-30 14:07:05 -04:00
|
|
|
it_behaves_like 'does not notify the chat integration'
|
2021-04-28 08:10:09 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'only one label matches' do
|
|
|
|
let(:label_filter) { '~some random label, ~Bug' }
|
|
|
|
|
2021-06-30 14:07:05 -04:00
|
|
|
it_behaves_like 'does not notify the chat integration'
|
2021-04-28 08:10:09 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'labels matches exactly' do
|
|
|
|
let(:label_filter) { '~Bug, ~Backend, ~Community contribution' }
|
|
|
|
|
2021-06-30 14:07:05 -04:00
|
|
|
it_behaves_like 'notifies the chat integration'
|
2021-04-28 08:10:09 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'labels matches but object has more' do
|
|
|
|
let(:label_filter) { '~Bug, ~Backend' }
|
|
|
|
|
2021-06-30 14:07:05 -04:00
|
|
|
it_behaves_like 'notifies the chat integration'
|
2021-04-28 08:10:09 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'labels are distributed on multiple objects' do
|
|
|
|
let(:label_filter) { '~Bug, ~Backend' }
|
|
|
|
let(:data) do
|
|
|
|
Gitlab::DataBuilder::Note.build(note, user).merge({
|
|
|
|
issue: {
|
|
|
|
labels: [
|
|
|
|
{ title: 'Bug' }
|
|
|
|
]
|
|
|
|
},
|
|
|
|
merge_request: {
|
|
|
|
labels: [
|
|
|
|
{
|
|
|
|
title: 'Backend'
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
})
|
|
|
|
end
|
|
|
|
|
2021-06-30 14:07:05 -04:00
|
|
|
it_behaves_like 'does not notify the chat integration'
|
2021-02-04 16:09:06 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-04-08 11:09:29 -04:00
|
|
|
context 'with "channel" property' do
|
|
|
|
before do
|
2021-06-30 14:07:05 -04:00
|
|
|
allow(chat_integration).to receive(:channel).and_return(channel)
|
2020-04-08 11:09:29 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'empty string' do
|
|
|
|
let(:channel) { '' }
|
|
|
|
|
|
|
|
it 'does not include the channel' do
|
2021-06-30 14:07:05 -04:00
|
|
|
expect(chat_integration).to receive(:notify).with(any_args, hash_excluding(:channel)).and_return(true)
|
|
|
|
expect(chat_integration.execute(data)).to be(true)
|
2020-04-08 11:09:29 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'empty spaces' do
|
|
|
|
let(:channel) { ' ' }
|
|
|
|
|
|
|
|
it 'does not include the channel' do
|
2021-06-30 14:07:05 -04:00
|
|
|
expect(chat_integration).to receive(:notify).with(any_args, hash_excluding(:channel)).and_return(true)
|
|
|
|
expect(chat_integration.execute(data)).to be(true)
|
2020-04-08 11:09:29 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-02-25 16:09:23 -05:00
|
|
|
shared_examples 'with channel specified' do |channel, expected_channels|
|
|
|
|
before do
|
2021-06-30 14:07:05 -04:00
|
|
|
allow(chat_integration).to receive(:push_channel).and_return(channel)
|
2020-02-25 16:09:23 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'notifies all channels' do
|
2021-06-30 14:07:05 -04:00
|
|
|
expect(chat_integration).to receive(:notify).with(any_args, hash_including(channel: expected_channels)).and_return(true)
|
|
|
|
expect(chat_integration.execute(data)).to be(true)
|
2020-02-25 16:09:23 -05:00
|
|
|
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 05:57:39 -04:00
|
|
|
end
|
2022-04-22 08:08:38 -04:00
|
|
|
|
|
|
|
describe '#default_channel_placeholder' do
|
|
|
|
it 'raises an error' do
|
|
|
|
expect { subject.default_channel_placeholder }.to raise_error(NotImplementedError)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#webhook_placeholder' do
|
|
|
|
it 'raises an error' do
|
|
|
|
expect { subject.webhook_placeholder }.to raise_error(NotImplementedError)
|
|
|
|
end
|
|
|
|
end
|
2016-12-06 12:59:03 -05:00
|
|
|
end
|