summaryrefslogtreecommitdiff
path: root/spec/models/project_services/chat_notification_service_spec.rb
diff options
context:
space:
mode:
Diffstat (limited to 'spec/models/project_services/chat_notification_service_spec.rb')
-rw-r--r--spec/models/project_services/chat_notification_service_spec.rb150
1 files changed, 137 insertions, 13 deletions
diff --git a/spec/models/project_services/chat_notification_service_spec.rb b/spec/models/project_services/chat_notification_service_spec.rb
index 476d99364b6..62f97873a06 100644
--- a/spec/models/project_services/chat_notification_service_spec.rb
+++ b/spec/models/project_services/chat_notification_service_spec.rb
@@ -11,6 +11,10 @@ RSpec.describe ChatNotificationService do
it { is_expected.to validate_presence_of :webhook }
end
+ describe 'validations' do
+ it { is_expected.to validate_inclusion_of(:labels_to_be_notified_behavior).in_array(%w[match_any match_all]).allow_blank }
+ end
+
describe '#can_test?' do
context 'with empty repository' do
it 'returns true' do
@@ -32,8 +36,9 @@ RSpec.describe ChatNotificationService do
describe '#execute' do
subject(:chat_service) { described_class.new }
+ let_it_be(:project) { create(:project, :repository) }
+
let(:user) { create(:user) }
- let(:project) { create(:project, :repository) }
let(:webhook_url) { 'https://example.gitlab.com/' }
let(:data) { Gitlab::DataBuilder::Push.build_sample(subject.project, user) }
@@ -76,9 +81,12 @@ RSpec.describe ChatNotificationService do
end
context 'when the data object has a label' do
- let(:label) { create(:label, project: project, name: 'Bug')}
- let(:issue) { create(:labeled_issue, project: project, labels: [label]) }
- let(:note) { create(:note, noteable: issue, project: project)}
+ 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) }
+
let(:data) { Gitlab::DataBuilder::Note.build(note, user) }
it 'notifies the chat service' do
@@ -87,23 +95,139 @@ RSpec.describe ChatNotificationService do
chat_service.execute(data)
end
- context 'and the chat_service has a label filter that does not matches the label' do
- subject(:chat_service) { described_class.new(labels_to_be_notified: '~some random label') }
+ shared_examples 'notifies the chat service' do
+ specify do
+ expect(chat_service).to receive(:notify).with(any_args)
+
+ chat_service.execute(data)
+ end
+ end
- it 'does not notify the chat service' do
- expect(chat_service).not_to receive(:notify)
+ shared_examples 'does not notify the chat service' do
+ specify do
+ expect(chat_service).not_to receive(:notify).with(any_args)
chat_service.execute(data)
end
end
- context 'and the chat_service has a label filter that matches the label' do
- subject(:chat_service) { described_class.new(labels_to_be_notified: '~Backend, ~Bug') }
+ context 'when labels_to_be_notified_behavior is not defined' do
+ subject(:chat_service) { described_class.new(labels_to_be_notified: label_filter) }
- it 'notifies the chat service' do
- expect(chat_service).to receive(:notify).with(any_args)
+ context 'no matching labels' do
+ let(:label_filter) { '~some random label' }
- chat_service.execute(data)
+ it_behaves_like 'does not notify the chat service'
+ end
+
+ context 'only one label matches' do
+ let(:label_filter) { '~some random label, ~Bug' }
+
+ it_behaves_like 'notifies the chat service'
+ end
+ end
+
+ context 'when labels_to_be_notified_behavior is blank' do
+ subject(:chat_service) { described_class.new(labels_to_be_notified: label_filter, labels_to_be_notified_behavior: '') }
+
+ context 'no matching labels' do
+ let(:label_filter) { '~some random label' }
+
+ it_behaves_like 'does not notify the chat service'
+ end
+
+ context 'only one label matches' do
+ let(:label_filter) { '~some random label, ~Bug' }
+
+ it_behaves_like 'notifies the chat service'
+ end
+ end
+
+ context 'when labels_to_be_notified_behavior is match_any' do
+ subject(:chat_service) do
+ 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 }
+
+ it_behaves_like 'notifies the chat service'
+ end
+
+ context 'no matching labels' do
+ let(:label_filter) { '~some random label' }
+
+ it_behaves_like 'does not notify the chat service'
+ end
+
+ context 'only one label matches' do
+ let(:label_filter) { '~some random label, ~Bug' }
+
+ it_behaves_like 'notifies the chat service'
+ end
+ end
+
+ context 'when labels_to_be_notified_behavior is match_all' do
+ subject(:chat_service) do
+ 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 }
+
+ it_behaves_like 'notifies the chat service'
+ end
+
+ context 'no matching labels' do
+ let(:label_filter) { '~some random label' }
+
+ it_behaves_like 'does not notify the chat service'
+ end
+
+ context 'only one label matches' do
+ let(:label_filter) { '~some random label, ~Bug' }
+
+ it_behaves_like 'does not notify the chat service'
+ end
+
+ context 'labels matches exactly' do
+ let(:label_filter) { '~Bug, ~Backend, ~Community contribution' }
+
+ it_behaves_like 'notifies the chat service'
+ end
+
+ context 'labels matches but object has more' do
+ let(:label_filter) { '~Bug, ~Backend' }
+
+ it_behaves_like 'notifies the chat service'
+ 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
+
+ it_behaves_like 'does not notify the chat service'
end
end
end