Commit 2009279a authored by Balasankar "Balu" C's avatar Balasankar "Balu" C

Make specs DRY

Signed-off-by: default avatarBalasankar "Balu" C <balasankar@gitlab.com>
parent 399eb9f1
...@@ -3,5 +3,5 @@ ...@@ -3,5 +3,5 @@
require 'spec_helper' require 'spec_helper'
describe MattermostService do describe MattermostService do
it_behaves_like "slack or mattermost notifications" it_behaves_like "slack or mattermost notifications", "Mattermost"
end end
...@@ -226,9 +226,10 @@ describe MicrosoftTeamsService do ...@@ -226,9 +226,10 @@ describe MicrosoftTeamsService do
) )
end end
shared_examples 'call Microsoft Teams API' do shared_examples 'call Microsoft Teams API' do |branches_to_be_notified: nil|
before do before do
WebMock.stub_request(:post, webhook_url) WebMock.stub_request(:post, webhook_url)
chat_service.branches_to_be_notified = branches_to_be_notified if branches_to_be_notified
end end
it 'calls Microsoft Teams API for pipeline events' do it 'calls Microsoft Teams API for pipeline events' do
...@@ -245,6 +246,18 @@ describe MicrosoftTeamsService do ...@@ -245,6 +246,18 @@ describe MicrosoftTeamsService do
end end
end end
shared_examples 'does not call Microsoft Teams API' do |branches_to_be_notified: nil|
before do
chat_service.branches_to_be_notified = branches_to_be_notified if branches_to_be_notified
end
it 'does not call Microsoft Teams API for pipeline events' do
data = Gitlab::DataBuilder::Pipeline.build(pipeline)
result = chat_service.execute(data)
expect(result).to be_falsy
end
end
context 'with failed pipeline' do context 'with failed pipeline' do
let(:status) { 'failed' } let(:status) { 'failed' }
...@@ -278,40 +291,19 @@ describe MicrosoftTeamsService do ...@@ -278,40 +291,19 @@ describe MicrosoftTeamsService do
end end
context 'only notify for the default branch' do context 'only notify for the default branch' do
before do it_behaves_like 'call Microsoft Teams API', branches_to_be_notified: "default"
chat_service.branches_to_be_notified = "default"
end
it_behaves_like 'call Microsoft Teams API'
end end
context 'notify for only protected branches' do context 'notify for only protected branches' do
before do it_behaves_like 'does not call Microsoft Teams API', branches_to_be_notified: "protected"
chat_service.branches_to_be_notified = "protected"
end
it 'does not call the Microsoft Teams API for pipeline events' do
data = Gitlab::DataBuilder::Pipeline.build(pipeline)
result = chat_service.execute(data)
expect(result).to be_falsy
end
end end
context 'notify for only default and protected branches' do context 'notify for only default and protected branches' do
before do it_behaves_like 'call Microsoft Teams API', branches_to_be_notified: "default_and_protected"
chat_service.branches_to_be_notified = "default_and_protected"
end
it_behaves_like 'call Microsoft Teams API'
end end
context 'notify for all branches' do context 'notify for all branches' do
before do it_behaves_like 'call Microsoft Teams API', branches_to_be_notified: "all"
chat_service.branches_to_be_notified = "all"
end
it_behaves_like 'call Microsoft Teams API'
end end
end end
...@@ -325,40 +317,19 @@ describe MicrosoftTeamsService do ...@@ -325,40 +317,19 @@ describe MicrosoftTeamsService do
end end
context 'only notify for the default branch' do context 'only notify for the default branch' do
before do it_behaves_like 'does not call Microsoft Teams API', branches_to_be_notified: "default"
chat_service.branches_to_be_notified = "default"
end
it 'does not call the Microsoft Teams API for pipeline events' do
data = Gitlab::DataBuilder::Pipeline.build(pipeline)
result = chat_service.execute(data)
expect(result).to be_falsy
end
end end
context 'notify for only protected branches' do context 'notify for only protected branches' do
before do it_behaves_like 'call Microsoft Teams API', branches_to_be_notified: "protected"
chat_service.branches_to_be_notified = "protected"
end
it_behaves_like 'call Microsoft Teams API'
end end
context 'notify for only default and protected branches' do context 'notify for only default and protected branches' do
before do it_behaves_like 'call Microsoft Teams API', branches_to_be_notified: "default_and_protected"
chat_service.branches_to_be_notified = "default_and_protected"
end
it_behaves_like 'call Microsoft Teams API'
end end
context 'notify for all branches' do context 'notify for all branches' do
before do it_behaves_like 'call Microsoft Teams API', branches_to_be_notified: "all"
chat_service.branches_to_be_notified = "all"
end
it_behaves_like 'call Microsoft Teams API'
end end
end end
...@@ -368,50 +339,19 @@ describe MicrosoftTeamsService do ...@@ -368,50 +339,19 @@ describe MicrosoftTeamsService do
end end
context 'only notify for the default branch' do context 'only notify for the default branch' do
before do it_behaves_like 'does not call Microsoft Teams API', branches_to_be_notified: "default"
chat_service.branches_to_be_notified = "default"
end
it 'does not call the Microsoft Teams API for pipeline events' do
data = Gitlab::DataBuilder::Pipeline.build(pipeline)
result = chat_service.execute(data)
expect(result).to be_falsy
end
end end
context 'notify for only protected branches' do context 'notify for only protected branches' do
before do it_behaves_like 'does not call Microsoft Teams API', branches_to_be_notified: "protected"
chat_service.branches_to_be_notified = "protected"
end
it 'does not call the Microsoft Teams API for pipeline events' do
data = Gitlab::DataBuilder::Pipeline.build(pipeline)
result = chat_service.execute(data)
expect(result).to be_falsy
end
end end
context 'notify for only default and protected branches' do context 'notify for only default and protected branches' do
before do it_behaves_like 'does not call Microsoft Teams API', branches_to_be_notified: "default_and_protected"
chat_service.branches_to_be_notified = "default_and_protected"
end
it 'does not call the Microsoft Teams API for pipeline events' do
data = Gitlab::DataBuilder::Pipeline.build(pipeline)
result = chat_service.execute(data)
expect(result).to be_falsy
end
end end
context 'notify for all branches' do context 'notify for all branches' do
before do it_behaves_like 'call Microsoft Teams API', branches_to_be_notified: "all"
chat_service.branches_to_be_notified = "all"
end
it_behaves_like 'call Microsoft Teams API'
end end
end end
end end
......
...@@ -53,9 +53,10 @@ describe PipelinesEmailService, :mailer do ...@@ -53,9 +53,10 @@ describe PipelinesEmailService, :mailer do
end end
end end
shared_examples 'sending email' do shared_examples 'sending email' do |branches_to_be_notified: nil|
before do before do
subject.recipients = recipients subject.recipients = recipients
subject.branches_to_be_notified = branches_to_be_notified if branches_to_be_notified
perform_enqueued_jobs do perform_enqueued_jobs do
run run
...@@ -69,9 +70,10 @@ describe PipelinesEmailService, :mailer do ...@@ -69,9 +70,10 @@ describe PipelinesEmailService, :mailer do
end end
end end
shared_examples 'not sending email' do shared_examples 'not sending email' do |branches_to_be_notified: nil|
before do before do
subject.recipients = recipients subject.recipients = recipients
subject.branches_to_be_notified = branches_to_be_notified if branches_to_be_notified
perform_enqueued_jobs do perform_enqueued_jobs do
run run
...@@ -109,35 +111,19 @@ describe PipelinesEmailService, :mailer do ...@@ -109,35 +111,19 @@ describe PipelinesEmailService, :mailer do
end end
context 'notifications are enabled only for default branch' do context 'notifications are enabled only for default branch' do
before do it_behaves_like 'sending email', branches_to_be_notified: "default"
subject.branches_to_be_notified = "default"
end
it_behaves_like 'sending email'
end end
context 'notifications are enabled only for protected branch' do context 'notifications are enabled only for protected branch' do
before do it_behaves_like 'sending email', branches_to_be_notified: "protected"
subject.branches_to_be_notified = "protected"
end
it_behaves_like 'sending email'
end end
context 'notifications are enabled only for default and protected branches ' do context 'notifications are enabled only for default and protected branches ' do
before do it_behaves_like 'sending email', branches_to_be_notified: "default_and_protected"
subject.branches_to_be_notified = "default_and_protected"
end
it_behaves_like 'sending email'
end end
context 'notifications are enabled only for all branches' do context 'notifications are enabled only for all branches' do
before do it_behaves_like 'sending email', branches_to_be_notified: "all"
subject.branches_to_be_notified = "all"
end
it_behaves_like 'sending email'
end end
end end
...@@ -149,35 +135,19 @@ describe PipelinesEmailService, :mailer do ...@@ -149,35 +135,19 @@ describe PipelinesEmailService, :mailer do
end end
context 'notifications are enabled only for default branch' do context 'notifications are enabled only for default branch' do
before do it_behaves_like 'sending email', branches_to_be_notified: "default"
subject.branches_to_be_notified = "default"
end
it_behaves_like 'sending email'
end end
context 'notifications are enabled only for protected branch' do context 'notifications are enabled only for protected branch' do
before do it_behaves_like 'sending email', branches_to_be_notified: "protected"
subject.branches_to_be_notified = "protected"
end
it_behaves_like 'sending email'
end end
context 'notifications are enabled only for default and protected branches ' do context 'notifications are enabled only for default and protected branches ' do
before do it_behaves_like 'sending email', branches_to_be_notified: "default_and_protected"
subject.branches_to_be_notified = "default_and_protected"
end
it_behaves_like 'sending email'
end end
context 'notifications are enabled only for all branches' do context 'notifications are enabled only for all branches' do
before do it_behaves_like 'sending email', branches_to_be_notified: "all"
subject.branches_to_be_notified = "all"
end
it_behaves_like 'sending email'
end end
end end
...@@ -188,35 +158,19 @@ describe PipelinesEmailService, :mailer do ...@@ -188,35 +158,19 @@ describe PipelinesEmailService, :mailer do
end end
context 'notifications are enabled only for default branch' do context 'notifications are enabled only for default branch' do
before do it_behaves_like 'sending email', branches_to_be_notified: "default"
subject.branches_to_be_notified = "default"
end
it_behaves_like 'sending email'
end end
context 'notifications are enabled only for protected branch' do context 'notifications are enabled only for protected branch' do
before do it_behaves_like 'sending email', branches_to_be_notified: "protected"
subject.branches_to_be_notified = "protected"
end
it_behaves_like 'sending email'
end end
context 'notifications are enabled only for default and protected branches ' do context 'notifications are enabled only for default and protected branches ' do
before do it_behaves_like 'sending email', branches_to_be_notified: "default_and_protected"
subject.branches_to_be_notified = "default_and_protected"
end
it_behaves_like 'sending email'
end end
context 'notifications are enabled only for all branches' do context 'notifications are enabled only for all branches' do
before do it_behaves_like 'sending email', branches_to_be_notified: "all"
subject.branches_to_be_notified = "all"
end
it_behaves_like 'sending email'
end end
end end
end end
...@@ -265,42 +219,26 @@ describe PipelinesEmailService, :mailer do ...@@ -265,42 +219,26 @@ describe PipelinesEmailService, :mailer do
end end
context 'when the pipeline failed' do context 'when the pipeline failed' do
context 'on a default branch' do context 'on default branch' do
before do before do
data[:object_attributes][:ref] = project.default_branch data[:object_attributes][:ref] = project.default_branch
pipeline.update(ref: project.default_branch) pipeline.update(ref: project.default_branch)
end end
context 'notifications are enabled only for default branch' do context 'notifications are enabled only for default branch' do
before do it_behaves_like 'sending email', branches_to_be_notified: "default"
subject.branches_to_be_notified = "default"
end
it_behaves_like 'sending email'
end end
context 'notifications are enabled only for protected branch' do context 'notifications are enabled only for protected branch' do
before do it_behaves_like 'not sending email', branches_to_be_notified: "protected"
subject.branches_to_be_notified = "protected"
end
it_behaves_like 'not sending email'
end end
context 'notifications are enabled only for default and protected branches ' do context 'notifications are enabled only for default and protected branches ' do
before do it_behaves_like 'sending email', branches_to_be_notified: "default_and_protected"
subject.branches_to_be_notified = "default_and_protected"
end
it_behaves_like 'sending email'
end end
context 'notifications are enabled only for all branches' do context 'notifications are enabled only for all branches' do
before do it_behaves_like 'sending email', branches_to_be_notified: "all"
subject.branches_to_be_notified = "all"
end
it_behaves_like 'sending email'
end end
end end
...@@ -312,35 +250,19 @@ describe PipelinesEmailService, :mailer do ...@@ -312,35 +250,19 @@ describe PipelinesEmailService, :mailer do
end end
context 'notifications are enabled only for default branch' do context 'notifications are enabled only for default branch' do
before do it_behaves_like 'not sending email', branches_to_be_notified: "default"
subject.branches_to_be_notified = "default"
end
it_behaves_like 'not sending email'
end end
context 'notifications are enabled only for protected branch' do context 'notifications are enabled only for protected branch' do
before do it_behaves_like 'sending email', branches_to_be_notified: "protected"
subject.branches_to_be_notified = "protected"
end
it_behaves_like 'sending email'
end end
context 'notifications are enabled only for default and protected branches ' do context 'notifications are enabled only for default and protected branches ' do
before do it_behaves_like 'sending email', branches_to_be_notified: "default_and_protected"
subject.branches_to_be_notified = "default_and_protected"
end
it_behaves_like 'sending email'
end end
context 'notifications are enabled only for all branches' do context 'notifications are enabled only for all branches' do
before do it_behaves_like 'sending email', branches_to_be_notified: "all"
subject.branches_to_be_notified = "all"
end
it_behaves_like 'sending email'
end end
end end
...@@ -351,35 +273,19 @@ describe PipelinesEmailService, :mailer do ...@@ -351,35 +273,19 @@ describe PipelinesEmailService, :mailer do
end end
context 'notifications are enabled only for default branch' do context 'notifications are enabled only for default branch' do
before do it_behaves_like 'not sending email', branches_to_be_notified: "default"
subject.branches_to_be_notified = "default"
end
it_behaves_like 'not sending email'
end end
context 'notifications are enabled only for protected branch' do context 'notifications are enabled only for protected branch' do
before do it_behaves_like 'not sending email', branches_to_be_notified: "protected"
subject.branches_to_be_notified = "protected"
end
it_behaves_like 'not sending email'
end end
context 'notifications are enabled only for default and protected branches ' do context 'notifications are enabled only for default and protected branches ' do
before do it_behaves_like 'not sending email', branches_to_be_notified: "default_and_protected"
subject.branches_to_be_notified = "default_and_protected"
end
it_behaves_like 'not sending email'
end end
context 'notifications are enabled only for all branches' do context 'notifications are enabled only for all branches' do
before do it_behaves_like 'sending email', branches_to_be_notified: "all"
subject.branches_to_be_notified = "all"
end
it_behaves_like 'sending email'
end end
end end
end end
......
...@@ -3,5 +3,5 @@ ...@@ -3,5 +3,5 @@
require 'spec_helper' require 'spec_helper'
describe SlackService do describe SlackService do
it_behaves_like "slack or mattermost notifications" it_behaves_like "slack or mattermost notifications", "Slack"
end end
...@@ -49,7 +49,11 @@ shared_examples_for "chat service" do |service_name| ...@@ -49,7 +49,11 @@ shared_examples_for "chat service" do |service_name|
WebMock.stub_request(:post, webhook_url) WebMock.stub_request(:post, webhook_url)
end end
shared_examples "#{service_name} service" do shared_examples "triggered #{service_name} service" do |branches_to_be_notified: nil|
before do
subject.branches_to_be_notified = branches_to_be_notified if branches_to_be_notified
end
it "calls #{service_name} API" do it "calls #{service_name} API" do
subject.execute(sample_data) subject.execute(sample_data)
...@@ -57,12 +61,24 @@ shared_examples_for "chat service" do |service_name| ...@@ -57,12 +61,24 @@ shared_examples_for "chat service" do |service_name|
end end
end end
shared_examples "untriggered #{service_name} service" do |branches_to_be_notified: nil|
before do
subject.branches_to_be_notified = branches_to_be_notified if branches_to_be_notified
end
it "does not call #{service_name} API" do
result = subject.execute(sample_data)
expect(result).to be_falsy
end
end
context "with push events" do context "with push events" do
let(:sample_data) do let(:sample_data) do
Gitlab::DataBuilder::Push.build_sample(project, user) Gitlab::DataBuilder::Push.build_sample(project, user)
end end
it_behaves_like "#{service_name} service" it_behaves_like "triggered #{service_name} service"
it "specifies the webhook when it is configured" do it "specifies the webhook when it is configured" do
expect(client).to receive(:new).with(client_arguments).and_return(double(:chat_service).as_null_object) expect(client).to receive(:new).with(client_arguments).and_return(double(:chat_service).as_null_object)
...@@ -76,39 +92,19 @@ shared_examples_for "chat service" do |service_name| ...@@ -76,39 +92,19 @@ shared_examples_for "chat service" do |service_name|
end end
context "when only default branch are to be notified" do context "when only default branch are to be notified" do
before do it_behaves_like "triggered #{service_name} service", branches_to_be_notified: "default"
subject.branches_to_be_notified = "default"
end
it_behaves_like "#{service_name} service"
end end
context "when only protected branches are to be notified" do context "when only protected branches are to be notified" do
before do it_behaves_like "untriggered #{service_name} service", branches_to_be_notified: "protected"
subject.branches_to_be_notified = "protected"
end
it "does not call the Discord Webhooks API" do
result = subject.execute(sample_data)
expect(result).to be_falsy
end
end end
context "when default and protected branches are to be notified" do context "when default and protected branches are to be notified" do
before do it_behaves_like "triggered #{service_name} service", branches_to_be_notified: "default_and_protected"
subject.branches_to_be_notified = "default_and_protected"
end
it_behaves_like "#{service_name} service"
end end
context "when all branches are to be notified" do context "when all branches are to be notified" do
before do it_behaves_like "triggered #{service_name} service", branches_to_be_notified: "all"
subject.branches_to_be_notified = "all"
end
it_behaves_like "#{service_name} service"
end end
end end
...@@ -122,39 +118,19 @@ shared_examples_for "chat service" do |service_name| ...@@ -122,39 +118,19 @@ shared_examples_for "chat service" do |service_name|
end end
context "when only default branch are to be notified" do context "when only default branch are to be notified" do
before do it_behaves_like "untriggered #{service_name} service", branches_to_be_notified: "default"
subject.branches_to_be_notified = "default"
end
it "does not call the Discord Webhooks API" do
result = subject.execute(sample_data)
expect(result).to be_falsy
end
end end
context "when only protected branches are to be notified" do context "when only protected branches are to be notified" do
before do it_behaves_like "triggered #{service_name} service", branches_to_be_notified: "protected"
subject.branches_to_be_notified = "protected"
end
it_behaves_like "#{service_name} service"
end end
context "when default and protected branches are to be notified" do context "when default and protected branches are to be notified" do
before do it_behaves_like "triggered #{service_name} service", branches_to_be_notified: "default_and_protected"
subject.branches_to_be_notified = "default_and_protected"
end
it_behaves_like "#{service_name} service"
end end
context "when all branches are to be notified" do context "when all branches are to be notified" do
before do it_behaves_like "triggered #{service_name} service", branches_to_be_notified: "all"
subject.branches_to_be_notified = "all"
end
it_behaves_like "#{service_name} service"
end end
end end
...@@ -164,47 +140,19 @@ shared_examples_for "chat service" do |service_name| ...@@ -164,47 +140,19 @@ shared_examples_for "chat service" do |service_name|
end end
context "when only default branch are to be notified" do context "when only default branch are to be notified" do
before do it_behaves_like "untriggered #{service_name} service", branches_to_be_notified: "default"
subject.branches_to_be_notified = "default"
end
it "does not call the Discord Webhooks API" do
result = subject.execute(sample_data)
expect(result).to be_falsy
end
end end
context "when only protected branches are to be notified" do context "when only protected branches are to be notified" do
before do it_behaves_like "untriggered #{service_name} service", branches_to_be_notified: "protected"
subject.branches_to_be_notified = "protected"
end
it "does not call the Discord Webhooks API" do
result = subject.execute(sample_data)
expect(result).to be_falsy
end
end end
context "when default and protected branches are to be notified" do context "when default and protected branches are to be notified" do
before do it_behaves_like "untriggered #{service_name} service", branches_to_be_notified: "default_and_protected"
subject.branches_to_be_notified = "default_and_protected"
end
it "does not call the Discord Webhooks API" do
result = subject.execute(sample_data)
expect(result).to be_falsy
end
end end
context "when all branches are to be notified" do context "when all branches are to be notified" do
before do it_behaves_like "triggered #{service_name} service", branches_to_be_notified: "all"
subject.branches_to_be_notified = "all"
end
it_behaves_like "#{service_name} service"
end end
end end
end end
...@@ -217,7 +165,7 @@ shared_examples_for "chat service" do |service_name| ...@@ -217,7 +165,7 @@ shared_examples_for "chat service" do |service_name|
service.hook_data(issue, "open") service.hook_data(issue, "open")
end end
it_behaves_like "#{service_name} service" it_behaves_like "triggered #{service_name} service"
end end
context "with merge events" do context "with merge events" do
...@@ -240,7 +188,7 @@ shared_examples_for "chat service" do |service_name| ...@@ -240,7 +188,7 @@ shared_examples_for "chat service" do |service_name|
project.add_developer(user) project.add_developer(user)
end end
it_behaves_like "#{service_name} service" it_behaves_like "triggered #{service_name} service"
end end
context "with wiki page events" do context "with wiki page events" do
...@@ -255,7 +203,7 @@ shared_examples_for "chat service" do |service_name| ...@@ -255,7 +203,7 @@ shared_examples_for "chat service" do |service_name|
let(:wiki_page) { create(:wiki_page, wiki: project.wiki, attrs: opts) } let(:wiki_page) { create(:wiki_page, wiki: project.wiki, attrs: opts) }
let(:sample_data) { Gitlab::DataBuilder::WikiPage.build(wiki_page, user, "create") } let(:sample_data) { Gitlab::DataBuilder::WikiPage.build(wiki_page, user, "create") }
it_behaves_like "#{service_name} service" it_behaves_like "triggered #{service_name} service"
end end
context "with note events" do context "with note events" do
...@@ -270,7 +218,7 @@ shared_examples_for "chat service" do |service_name| ...@@ -270,7 +218,7 @@ shared_examples_for "chat service" do |service_name|
note: "a comment on a commit") note: "a comment on a commit")
end end
it_behaves_like "#{service_name} service" it_behaves_like "triggered #{service_name} service"
end end
context "with merge request comment" do context "with merge request comment" do
...@@ -278,7 +226,7 @@ shared_examples_for "chat service" do |service_name| ...@@ -278,7 +226,7 @@ shared_examples_for "chat service" do |service_name|
create(:note_on_merge_request, project: project, note: "merge request note") create(:note_on_merge_request, project: project, note: "merge request note")
end end
it_behaves_like "#{service_name} service" it_behaves_like "triggered #{service_name} service"
end end
context "with issue comment" do context "with issue comment" do
...@@ -286,7 +234,7 @@ shared_examples_for "chat service" do |service_name| ...@@ -286,7 +234,7 @@ shared_examples_for "chat service" do |service_name|
create(:note_on_issue, project: project, note: "issue note") create(:note_on_issue, project: project, note: "issue note")
end end
it_behaves_like "#{service_name} service" it_behaves_like "triggered #{service_name} service"
end end
context "with snippet comment" do context "with snippet comment" do
...@@ -294,7 +242,7 @@ shared_examples_for "chat service" do |service_name| ...@@ -294,7 +242,7 @@ shared_examples_for "chat service" do |service_name|
create(:note_on_project_snippet, project: project, note: "snippet note") create(:note_on_project_snippet, project: project, note: "snippet note")
end end
it_behaves_like "#{service_name} service" it_behaves_like "triggered #{service_name} service"
end end
end end
...@@ -309,14 +257,14 @@ shared_examples_for "chat service" do |service_name| ...@@ -309,14 +257,14 @@ shared_examples_for "chat service" do |service_name|
context "with failed pipeline" do context "with failed pipeline" do
let(:status) { "failed" } let(:status) { "failed" }
it_behaves_like "#{service_name} service" it_behaves_like "triggered #{service_name} service"
end end
context "with succeeded pipeline" do context "with succeeded pipeline" do
let(:status) { "success" } let(:status) { "success" }
context "with default notify_only_broken_pipelines" do context "with default notify_only_broken_pipelines" do
it "does not call Discord Webhooks API" do it "does not call #{service_name} API" do
result = subject.execute(sample_data) result = subject.execute(sample_data)
expect(result).to be_falsy expect(result).to be_falsy
...@@ -328,105 +276,77 @@ shared_examples_for "chat service" do |service_name| ...@@ -328,105 +276,77 @@ shared_examples_for "chat service" do |service_name|
subject.notify_only_broken_pipelines = false subject.notify_only_broken_pipelines = false
end end
it_behaves_like "#{service_name} service" it_behaves_like "triggered #{service_name} service"
end end
end end
context "with protected branch" do context "with default branch" do
before do let(:sample_data) do
create(:protected_branch, project: project, name: 'a-protected-branch') Gitlab::DataBuilder::Push.build(project: project, user: user, ref: project.default_branch)
end end
let(:pipeline) do context "when only default branch are to be notified" do
create(:ci_pipeline, :failed, project: project, it_behaves_like "triggered #{service_name} service", branches_to_be_notified: "default"
sha: project.commit.sha, ref: 'a-protected-branch')
end end
context "when only default branch are to be notified" do context "when only protected branches are to be notified" do
before do it_behaves_like "untriggered #{service_name} service", branches_to_be_notified: "protected"
subject.branches_to_be_notified = "default"
end end
it "does not call the Discord Webhooks API" do context "when default and protected branches are to be notified" do
result = subject.execute(sample_data) it_behaves_like "triggered #{service_name} service", branches_to_be_notified: "default_and_protected"
end
expect(result).to be_falsy context "when all branches are to be notified" do
it_behaves_like "triggered #{service_name} service", branches_to_be_notified: "all"
end end
end end
context "when only protected branches are to be notified" do context "with protected branch" do
before do before do
subject.branches_to_be_notified = "protected" create(:protected_branch, project: project, name: "a-protected-branch")
end end
it_behaves_like "#{service_name} service" let(:sample_data) do
Gitlab::DataBuilder::Push.build(project: project, user: user, ref: "a-protected-branch")
end end
context "when default and protected branches are to be notified" do context "when only default branch are to be notified" do
before do it_behaves_like "untriggered #{service_name} service", branches_to_be_notified: "default"
subject.branches_to_be_notified = "default_and_protected"
end end
it_behaves_like "#{service_name} service" context "when only protected branches are to be notified" do
it_behaves_like "triggered #{service_name} service", branches_to_be_notified: "protected"
end end
context "when all branches are to be notified" do context "when default and protected branches are to be notified" do
before do it_behaves_like "triggered #{service_name} service", branches_to_be_notified: "default_and_protected"
subject.branches_to_be_notified = "all"
end end
it_behaves_like "#{service_name} service" context "when all branches are to be notified" do
it_behaves_like "triggered #{service_name} service", branches_to_be_notified: "all"
end end
end end
context "with neither default nor protected branch" do context "with neither default nor protected branch" do
let(:pipeline) do let(:sample_data) do
create(:ci_pipeline, :failed, project: project, Gitlab::DataBuilder::Push.build(project: project, user: user, ref: "a-random-branch")
sha: project.commit.sha, ref: "a-random-branch")
end end
context "when only default branch are to be notified" do context "when only default branch are to be notified" do
before do it_behaves_like "untriggered #{service_name} service", branches_to_be_notified: "default"
subject.branches_to_be_notified = "default"
end
it "does not call the Discord Webhooks API" do
result = subject.execute(sample_data)
expect(result).to be_falsy
end
end end
context "when only protected branches are to be notified" do context "when only protected branches are to be notified" do
before do it_behaves_like "untriggered #{service_name} service", branches_to_be_notified: "protected"
subject.branches_to_be_notified = "protected"
end
it "does not call the Discord Webhooks API" do
result = subject.execute(sample_data)
expect(result).to be_falsy
end
end end
context "when default and protected branches are to be notified" do context "when default and protected branches are to be notified" do
before do it_behaves_like "untriggered #{service_name} service", branches_to_be_notified: "default_and_protected"
subject.branches_to_be_notified = "default_and_protected"
end
it "does not call the Discord Webhooks API" do
result = subject.execute(sample_data)
expect(result).to be_falsy
end
end end
context "when all branches are to be notified" do context "when all branches are to be notified" do
before do it_behaves_like "triggered #{service_name} service", branches_to_be_notified: "all"
subject.branches_to_be_notified = "all"
end
it_behaves_like "#{service_name} service"
end end
end end
end end
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
Dir[Rails.root.join("app/models/project_services/chat_message/*.rb")].each { |f| require f } Dir[Rails.root.join("app/models/project_services/chat_message/*.rb")].each { |f| require f }
RSpec.shared_examples 'slack or mattermost notifications' do RSpec.shared_examples 'slack or mattermost notifications' do |service_name|
let(:chat_service) { described_class.new } let(:chat_service) { described_class.new }
let(:webhook_url) { 'https://example.gitlab.com/' } let(:webhook_url) { 'https://example.gitlab.com/' }
...@@ -35,6 +35,28 @@ RSpec.shared_examples 'slack or mattermost notifications' do ...@@ -35,6 +35,28 @@ RSpec.shared_examples 'slack or mattermost notifications' do
end end
end end
shared_examples "triggered #{service_name} service" do |event_type: nil, branches_to_be_notified: nil|
before do
chat_service.branches_to_be_notified = branches_to_be_notified if branches_to_be_notified
end
it "notifies about #{event_type} events" do
chat_service.execute(data)
expect(WebMock).to have_requested(:post, webhook_url)
end
end
shared_examples "untriggered #{service_name} service" do |event_type: nil, branches_to_be_notified: nil|
before do
chat_service.branches_to_be_notified = branches_to_be_notified if branches_to_be_notified
end
it "notifies about #{event_type} events" do
chat_service.execute(data)
expect(WebMock).not_to have_requested(:post, webhook_url)
end
end
describe "#execute" do describe "#execute" do
let(:user) { create(:user) } let(:user) { create(:user) }
let(:project) { create(:project, :repository, :wiki_repo) } let(:project) { create(:project, :repository, :wiki_repo) }
...@@ -42,7 +64,7 @@ RSpec.shared_examples 'slack or mattermost notifications' do ...@@ -42,7 +64,7 @@ RSpec.shared_examples 'slack or mattermost notifications' do
let(:channel) { 'slack_channel' } let(:channel) { 'slack_channel' }
let(:issue_service_options) { { title: 'Awesome issue', description: 'please fix' } } let(:issue_service_options) { { title: 'Awesome issue', description: 'please fix' } }
let(:push_sample_data) do let(:data) do
Gitlab::DataBuilder::Push.build_sample(project, user) Gitlab::DataBuilder::Push.build_sample(project, user)
end end
...@@ -84,31 +106,31 @@ RSpec.shared_examples 'slack or mattermost notifications' do ...@@ -84,31 +106,31 @@ RSpec.shared_examples 'slack or mattermost notifications' do
@wiki_page_sample_data = Gitlab::DataBuilder::WikiPage.build(@wiki_page, user, 'create') @wiki_page_sample_data = Gitlab::DataBuilder::WikiPage.build(@wiki_page, user, 'create')
end end
it "calls Slack/Mattermost API for push events" do it "calls #{service_name} API for push events" do
chat_service.execute(push_sample_data) chat_service.execute(data)
expect(WebMock).to have_requested(:post, webhook_url).once expect(WebMock).to have_requested(:post, webhook_url).once
end end
it "calls Slack/Mattermost API for issue events" do it "calls #{service_name} API for issue events" do
chat_service.execute(@issues_sample_data) chat_service.execute(@issues_sample_data)
expect(WebMock).to have_requested(:post, webhook_url).once expect(WebMock).to have_requested(:post, webhook_url).once
end end
it "calls Slack/Mattermost API for merge requests events" do it "calls #{service_name} API for merge requests events" do
chat_service.execute(@merge_sample_data) chat_service.execute(@merge_sample_data)
expect(WebMock).to have_requested(:post, webhook_url).once expect(WebMock).to have_requested(:post, webhook_url).once
end end
it "calls Slack/Mattermost API for wiki page events" do it "calls #{service_name} API for wiki page events" do
chat_service.execute(@wiki_page_sample_data) chat_service.execute(@wiki_page_sample_data)
expect(WebMock).to have_requested(:post, webhook_url).once expect(WebMock).to have_requested(:post, webhook_url).once
end end
it "calls Slack/Mattermost API for deployment events" do it "calls #{service_name} API for deployment events" do
deployment_event_data = { object_kind: 'deployment' } deployment_event_data = { object_kind: 'deployment' }
chat_service.execute(deployment_event_data) chat_service.execute(deployment_event_data)
...@@ -125,7 +147,7 @@ RSpec.shared_examples 'slack or mattermost notifications' do ...@@ -125,7 +147,7 @@ RSpec.shared_examples 'slack or mattermost notifications' do
double(:slack_service).as_null_object double(:slack_service).as_null_object
) )
chat_service.execute(push_sample_data) chat_service.execute(data)
end end
it 'uses the channel as an option when it is configured' do it 'uses the channel as an option when it is configured' do
...@@ -135,7 +157,7 @@ RSpec.shared_examples 'slack or mattermost notifications' do ...@@ -135,7 +157,7 @@ RSpec.shared_examples 'slack or mattermost notifications' do
.and_return( .and_return(
double(:slack_service).as_null_object double(:slack_service).as_null_object
) )
chat_service.execute(push_sample_data) chat_service.execute(data)
end end
context "event channels" do context "event channels" do
...@@ -148,7 +170,7 @@ RSpec.shared_examples 'slack or mattermost notifications' do ...@@ -148,7 +170,7 @@ RSpec.shared_examples 'slack or mattermost notifications' do
double(:slack_service).as_null_object double(:slack_service).as_null_object
) )
chat_service.execute(push_sample_data) chat_service.execute(data)
end end
it "uses the right channel for merge request event" do it "uses the right channel for merge request event" do
...@@ -270,85 +292,40 @@ RSpec.shared_examples 'slack or mattermost notifications' do ...@@ -270,85 +292,40 @@ RSpec.shared_examples 'slack or mattermost notifications' do
end end
context 'on default branch' do context 'on default branch' do
it 'still notifies about pushed tags' do let(:data) do
ref = "#{Gitlab::Git::TAG_REF_PREFIX}test" Gitlab::DataBuilder::Push.build(
push_sample_data = Gitlab::DataBuilder::Push.build(project: project, user: user, ref: ref)
chat_service.execute(push_sample_data)
expect(WebMock).to have_requested(:post, webhook_url).once
end
context 'notification enabled only for default branch' do
before do
chat_service.branches_to_be_notified = "default"
end
it 'notifies about push events' do
push_sample_data = Gitlab::DataBuilder::Push.build(
project: project, project: project,
user: user, user: user,
ref: project.default_branch ref: project.default_branch
) )
chat_service.execute(push_sample_data)
expect(WebMock).to have_requested(:post, webhook_url)
end
end
context 'notification enabled only for protected branches' do
before do
chat_service.branches_to_be_notified = "protected"
end end
it 'does not notify about push events' do context 'pushing tags' do
push_sample_data = Gitlab::DataBuilder::Push.build( let(:data) do
Gitlab::DataBuilder::Push.build(
project: project, project: project,
user: user, user: user,
ref: project.default_branch ref: "#{Gitlab::Git::TAG_REF_PREFIX}test"
) )
chat_service.execute(push_sample_data)
expect(WebMock).not_to have_requested(:post, webhook_url).once
end
end end
context 'notification enabled only for default and protected branches' do it_behaves_like "triggered #{service_name} service", event_type: "push"
before do
chat_service.branches_to_be_notified = "default_and_protected"
end end
it 'notifies about push events' do context 'notification enabled only for default branch' do
push_sample_data = Gitlab::DataBuilder::Push.build( it_behaves_like "triggered #{service_name} service", event_type: "push", branches_to_be_notified: "default"
project: project,
user: user,
ref: project.default_branch
)
chat_service.execute(push_sample_data)
expect(WebMock).to have_requested(:post, webhook_url).once
end
end end
context 'notification enabled for all branches' do context 'notification enabled only for protected branches' do
before do it_behaves_like "untriggered #{service_name} service", event_type: "push", branches_to_be_notified: "protected"
chat_service.branches_to_be_notified = "all"
end end
it 'notifies about push events' do context 'notification enabled only for default and protected branches' do
push_sample_data = Gitlab::DataBuilder::Push.build( it_behaves_like "triggered #{service_name} service", event_type: "push", branches_to_be_notified: "default_and_protected"
project: project,
user: user,
ref: project.default_branch
)
chat_service.execute(push_sample_data)
expect(WebMock).to have_requested(:post, webhook_url).once
end end
context 'notification enabled for all branches' do
it_behaves_like "triggered #{service_name} service", event_type: "push", branches_to_be_notified: "all"
end end
end end
...@@ -357,179 +334,89 @@ RSpec.shared_examples 'slack or mattermost notifications' do ...@@ -357,179 +334,89 @@ RSpec.shared_examples 'slack or mattermost notifications' do
create(:protected_branch, project: project, name: 'a-protected-branch') create(:protected_branch, project: project, name: 'a-protected-branch')
end end
it 'still notifies about pushed tags' do let(:data) do
ref = "#{Gitlab::Git::TAG_REF_PREFIX}test" Gitlab::DataBuilder::Push.build(
push_sample_data = Gitlab::DataBuilder::Push.build(project: project, user: user, ref: ref)
chat_service.execute(push_sample_data)
expect(WebMock).to have_requested(:post, webhook_url).once
end
context 'notification enabled only for default branch' do
before do
chat_service.branches_to_be_notified = "default"
end
it 'does not notify about push events' do
push_sample_data = Gitlab::DataBuilder::Push.build(
project: project, project: project,
user: user, user: user,
ref: 'a-protected-branch' ref: 'a-protected-branch'
) )
chat_service.execute(push_sample_data)
expect(WebMock).not_to have_requested(:post, webhook_url)
end
end end
context 'notification enabled only for protected branches' do context 'pushing tags' do
before do let(:data) do
chat_service.branches_to_be_notified = "protected" Gitlab::DataBuilder::Push.build(
end
it 'notifies about push events' do
push_sample_data = Gitlab::DataBuilder::Push.build(
project: project, project: project,
user: user, user: user,
ref: 'a-protected-branch' ref: "#{Gitlab::Git::TAG_REF_PREFIX}test"
) )
chat_service.execute(push_sample_data)
expect(WebMock).to have_requested(:post, webhook_url).once
end
end end
context 'notification enabled only for default and protected branches' do it_behaves_like "triggered #{service_name} service", event_type: "push"
before do
chat_service.branches_to_be_notified = "default_and_protected"
end end
it 'notifies about push events' do context 'notification enabled only for default branch' do
push_sample_data = Gitlab::DataBuilder::Push.build( it_behaves_like "untriggered #{service_name} service", event_type: "push", branches_to_be_notified: "default"
project: project,
user: user,
ref: 'a-protected-branch'
)
chat_service.execute(push_sample_data)
expect(WebMock).to have_requested(:post, webhook_url).once
end
end end
context 'notification enabled for all branches' do context 'notification enabled only for protected branches' do
before do it_behaves_like "triggered #{service_name} service", event_type: "push", branches_to_be_notified: "protected"
chat_service.branches_to_be_notified = "all"
end end
it 'notifies about push events' do context 'notification enabled only for default and protected branches' do
push_sample_data = Gitlab::DataBuilder::Push.build( it_behaves_like "triggered #{service_name} service", event_type: "push", branches_to_be_notified: "default_and_protected"
project: project,
user: user,
ref: 'a-protected-branch'
)
chat_service.execute(push_sample_data)
expect(WebMock).to have_requested(:post, webhook_url).once
end
end
end end
context 'on a neither protected nor default branch' do context 'notification enabled for all branches' do
it 'still notifies about pushed tags' do it_behaves_like "triggered #{service_name} service", event_type: "push", branches_to_be_notified: "all"
ref = "#{Gitlab::Git::TAG_REF_PREFIX}test"
push_sample_data = Gitlab::DataBuilder::Push.build(project: project, user: user, ref: ref)
chat_service.execute(push_sample_data)
expect(WebMock).to have_requested(:post, webhook_url).once
end end
context 'notification enabled only for default branch' do
before do
chat_service.branches_to_be_notified = "default"
end end
it 'does not notify about push events' do context 'on a neither protected nor default branch' do
push_sample_data = Gitlab::DataBuilder::Push.build( let(:data) do
Gitlab::DataBuilder::Push.build(
project: project, project: project,
user: user, user: user,
ref: 'a-random-branch' ref: 'a-random-branch'
) )
chat_service.execute(push_sample_data)
expect(WebMock).not_to have_requested(:post, webhook_url)
end
end end
context 'notification enabled only for protected branches' do context 'pushing tags' do
before do let(:data) do
chat_service.branches_to_be_notified = "protected" Gitlab::DataBuilder::Push.build(
end
it 'does not notify about push events' do
push_sample_data = Gitlab::DataBuilder::Push.build(
project: project, project: project,
user: user, user: user,
ref: 'a-random-branch' ref: "#{Gitlab::Git::TAG_REF_PREFIX}test"
) )
chat_service.execute(push_sample_data)
expect(WebMock).not_to have_requested(:post, webhook_url).once
end
end end
context 'notification enabled only for default and protected branches' do it_behaves_like "triggered #{service_name} service", event_type: "push"
before do
chat_service.branches_to_be_notified = "default_and_protected"
end end
it 'does not notify about push events' do context 'notification enabled only for default branch' do
push_sample_data = Gitlab::DataBuilder::Push.build( it_behaves_like "untriggered #{service_name} service", event_type: "push", branches_to_be_notified: "default"
project: project,
user: user,
ref: 'a-random-branch'
)
chat_service.execute(push_sample_data)
expect(WebMock).not_to have_requested(:post, webhook_url).once
end
end end
context 'notification enabled for all branches' do context 'notification enabled only for protected branches' do
before do it_behaves_like "untriggered #{service_name} service", event_type: "push", branches_to_be_notified: "protected"
chat_service.branches_to_be_notified = "all"
end end
it 'notifies about push events' do context 'notification enabled only for default and protected branches' do
push_sample_data = Gitlab::DataBuilder::Push.build( it_behaves_like "untriggered #{service_name} service", event_type: "push", branches_to_be_notified: "default_and_protected"
project: project,
user: user,
ref: 'a-random-branch'
)
chat_service.execute(push_sample_data)
expect(WebMock).to have_requested(:post, webhook_url).once
end end
context 'notification enabled for all branches' do
it_behaves_like "triggered #{service_name} service", event_type: "push", branches_to_be_notified: "all"
end end
end end
end end
describe "Note events" do describe 'Note events' do
let(:user) { create(:user) } let(:user) { create(:user) }
let(:project) { create(:project, :repository, creator: user) } let(:project) { create(:project, :repository, creator: user) }
before do before do
allow(chat_service).to receive_messages( allow(chat_service).to receive_messages(
project: project, project: project,
project_id: project.id,
service_hook: true, service_hook: true,
webhook: webhook_url webhook: webhook_url
) )
...@@ -545,61 +432,56 @@ RSpec.shared_examples 'slack or mattermost notifications' do ...@@ -545,61 +432,56 @@ RSpec.shared_examples 'slack or mattermost notifications' do
note: 'a comment on a commit') note: 'a comment on a commit')
end end
it "calls Slack/Mattermost API for commit comment events" do let(:data) do
data = Gitlab::DataBuilder::Note.build(commit_note, user) Gitlab::DataBuilder::Note.build(commit_note, user)
chat_service.execute(data)
expect(WebMock).to have_requested(:post, webhook_url).once
end end
it_behaves_like "triggered #{service_name} service", event_type: "commit comment"
end end
context 'when merge request comment event executed' do context 'when merge request comment event executed' do
let(:merge_request_note) do let(:merge_request_note) do
create(:note_on_merge_request, project: project, create(:note_on_merge_request, project: project,
note: "merge request note") note: 'a comment on a merge request')
end end
it "calls Slack API for merge request comment events" do let(:data) do
data = Gitlab::DataBuilder::Note.build(merge_request_note, user) Gitlab::DataBuilder::Note.build(merge_request_note, user)
chat_service.execute(data)
expect(WebMock).to have_requested(:post, webhook_url).once
end end
it_behaves_like "triggered #{service_name} service", event_type: "merge request comment"
end end
context 'when issue comment event executed' do context 'when issue comment event executed' do
let(:issue_note) do let(:issue_note) do
create(:note_on_issue, project: project, note: "issue note") create(:note_on_issue, project: project,
note: 'a comment on an issue')
end end
let(:data) { Gitlab::DataBuilder::Note.build(issue_note, user) } let(:data) do
Gitlab::DataBuilder::Note.build(issue_note, user)
it "calls Slack API for issue comment events" do
chat_service.execute(data)
expect(WebMock).to have_requested(:post, webhook_url).once
end end
it_behaves_like "triggered #{service_name} service", event_type: "issue comment"
end end
context 'when snippet comment event executed' do context 'when snippet comment event executed' do
let(:snippet_note) do let(:snippet_note) do
create(:note_on_project_snippet, project: project, create(:note_on_project_snippet, project: project,
note: "snippet note") note: 'a comment on a snippet')
end end
it "calls Slack API for snippet comment events" do let(:data) do
data = Gitlab::DataBuilder::Note.build(snippet_note, user) Gitlab::DataBuilder::Note.build(snippet_note, user)
chat_service.execute(data)
expect(WebMock).to have_requested(:post, webhook_url).once
end end
it_behaves_like "triggered #{service_name} service", event_type: "snippet comment"
end end
end end
describe 'Pipeline events' do describe 'Pipeline events' do
let(:user) { create(:user) } let(:user) { create(:user) }
let(:project) { create(:project, :repository) } let(:project) { create(:project, :repository, creator: user) }
let(:pipeline) do let(:pipeline) do
create(:ci_pipeline, create(:ci_pipeline,
project: project, status: status, project: project, status: status,
...@@ -612,37 +494,16 @@ RSpec.shared_examples 'slack or mattermost notifications' do ...@@ -612,37 +494,16 @@ RSpec.shared_examples 'slack or mattermost notifications' do
service_hook: true, service_hook: true,
webhook: webhook_url webhook: webhook_url
) )
end
shared_examples 'call Slack/Mattermost API' do
before do
WebMock.stub_request(:post, webhook_url) WebMock.stub_request(:post, webhook_url)
end end
it 'calls Slack/Mattermost API for pipeline events' do
data = Gitlab::DataBuilder::Pipeline.build(pipeline)
chat_service.execute(data)
expect(WebMock).to have_requested(:post, webhook_url).once
end
end
context 'with failed pipeline' do
let(:status) { 'failed' }
it_behaves_like 'call Slack/Mattermost API'
end
context 'with succeeded pipeline' do context 'with succeeded pipeline' do
let(:status) { 'success' } let(:status) { 'success' }
let(:data) { Gitlab::DataBuilder::Pipeline.build(pipeline) }
context 'with default to notify_only_broken_pipelines' do context 'with default to notify_only_broken_pipelines' do
it 'does not call Slack/Mattermost API for pipeline events' do it_behaves_like "untriggered #{service_name} service", event_type: "pipeline"
data = Gitlab::DataBuilder::Pipeline.build(pipeline)
result = chat_service.execute(data)
expect(result).to be_falsy
end
end end
context 'with setting notify_only_broken_pipelines to false' do context 'with setting notify_only_broken_pipelines to false' do
...@@ -650,50 +511,34 @@ RSpec.shared_examples 'slack or mattermost notifications' do ...@@ -650,50 +511,34 @@ RSpec.shared_examples 'slack or mattermost notifications' do
chat_service.notify_only_broken_pipelines = false chat_service.notify_only_broken_pipelines = false
end end
it_behaves_like 'call Slack/Mattermost API' it_behaves_like "triggered #{service_name} service", event_type: "pipeline"
end end
end end
context 'on a default branch' do context 'with failed pipeline' do
context 'on default branch' do
let(:pipeline) do let(:pipeline) do
create(:ci_pipeline, :failed, project: project, sha: project.commit.sha, ref: project.default_branch) create(:ci_pipeline,
end project: project, status: :failed,
sha: project.commit.sha, ref: project.default_branch)
context 'notification enabled only for default branch' do
before do
chat_service.branches_to_be_notified = 'default'
end end
it_behaves_like 'call Slack/Mattermost API' let(:data) { Gitlab::DataBuilder::Pipeline.build(pipeline) }
end
context 'notification enabled only for protected branch' do context 'notification enabled only for default branch' do
before do it_behaves_like "triggered #{service_name} service", event_type: "pipeline", branches_to_be_notified: "default"
chat_service.branches_to_be_notified = 'protected'
end end
it 'does not call the Slack/Mattermost API for pipeline events' do context 'notification enabled only for protected branches' do
data = Gitlab::DataBuilder::Pipeline.build(pipeline) it_behaves_like "untriggered #{service_name} service", event_type: "pipeline", branches_to_be_notified: "protected"
result = chat_service.execute(data)
expect(result).to be_falsy
end
end end
context 'notification enabled only for default and protected branches' do context 'notification enabled only for default and protected branches' do
before do it_behaves_like "triggered #{service_name} service", event_type: "pipeline", branches_to_be_notified: "default_and_protected"
chat_service.branches_to_be_notified = 'default_and_protected'
end
it_behaves_like 'call Slack/Mattermost API'
end end
context 'notification enabled for all branches' do context 'notification enabled for all branches' do
before do it_behaves_like "triggered #{service_name} service", event_type: "pipeline", branches_to_be_notified: "all"
chat_service.branches_to_be_notified = 'all'
end
it_behaves_like 'call Slack/Mattermost API'
end end
end end
...@@ -703,97 +548,54 @@ RSpec.shared_examples 'slack or mattermost notifications' do ...@@ -703,97 +548,54 @@ RSpec.shared_examples 'slack or mattermost notifications' do
end end
let(:pipeline) do let(:pipeline) do
create(:ci_pipeline, :failed, project: project, sha: project.commit.sha, ref: 'a-protected-branch') create(:ci_pipeline,
end project: project, status: :failed,
sha: project.commit.sha, ref: 'a-protected-branch')
context 'notification enabled only for default branch' do
before do
chat_service.branches_to_be_notified = 'default'
end end
it 'does not call the Slack/Mattermost API for pipeline events' do let(:data) { Gitlab::DataBuilder::Pipeline.build(pipeline) }
data = Gitlab::DataBuilder::Pipeline.build(pipeline)
result = chat_service.execute(data)
expect(result).to be_falsy
end
end
context 'notification enabled only for protected branch' do context 'notification enabled only for default branch' do
before do it_behaves_like "untriggered #{service_name} service", event_type: "pipeline", branches_to_be_notified: "default"
chat_service.branches_to_be_notified = 'protected'
end end
it_behaves_like 'call Slack/Mattermost API' context 'notification enabled only for protected branches' do
it_behaves_like "triggered #{service_name} service", event_type: "pipeline", branches_to_be_notified: "protected"
end end
context 'notification enabled only for default and protected branches' do context 'notification enabled only for default and protected branches' do
before do it_behaves_like "triggered #{service_name} service", event_type: "pipeline", branches_to_be_notified: "default_and_protected"
chat_service.branches_to_be_notified = 'default_and_protected'
end
it_behaves_like 'call Slack/Mattermost API'
end end
context 'notification enabled for all branches' do context 'notification enabled for all branches' do
before do it_behaves_like "triggered #{service_name} service", event_type: "pipeline", branches_to_be_notified: "all"
chat_service.branches_to_be_notified = 'all'
end
it_behaves_like 'call Slack/Mattermost API'
end end
end end
context 'on a neithuer protected nor default branch' do context 'on a neither protected nor default branch' do
let(:pipeline) do let(:pipeline) do
create(:ci_pipeline, :failed, project: project, sha: project.commit.sha, ref: 'a-random-branch') create(:ci_pipeline,
end project: project, status: :failed,
sha: project.commit.sha, ref: 'a-random-branch')
context 'notification enabled only for default branch' do
before do
chat_service.branches_to_be_notified = 'default'
end end
it 'does not call the Slack/Mattermost API for pipeline events' do let(:data) { Gitlab::DataBuilder::Pipeline.build(pipeline) }
data = Gitlab::DataBuilder::Pipeline.build(pipeline)
result = chat_service.execute(data)
expect(result).to be_falsy context 'notification enabled only for default branch' do
end it_behaves_like "untriggered #{service_name} service", event_type: "pipeline", branches_to_be_notified: "default"
end
context 'notification enabled only for protected branch' do
before do
chat_service.branches_to_be_notified = 'protected'
end end
it 'does not call the Slack/Mattermost API for pipeline events' do context 'notification enabled only for protected branches' do
data = Gitlab::DataBuilder::Pipeline.build(pipeline) it_behaves_like "untriggered #{service_name} service", event_type: "pipeline", branches_to_be_notified: "protected"
result = chat_service.execute(data)
expect(result).to be_falsy
end
end end
context 'notification enabled only for default and protected branches' do context 'notification enabled only for default and protected branches' do
before do it_behaves_like "untriggered #{service_name} service", event_type: "pipeline", branches_to_be_notified: "default_and_protected"
chat_service.branches_to_be_notified = 'default_and_protected'
end
it 'does not call the Slack/Mattermost API for pipeline events' do
data = Gitlab::DataBuilder::Pipeline.build(pipeline)
result = chat_service.execute(data)
expect(result).to be_falsy
end
end end
context 'notification enabled for all branches' do context 'notification enabled for all branches' do
before do it_behaves_like "triggered #{service_name} service", event_type: "pipeline", branches_to_be_notified: "all"
chat_service.branches_to_be_notified = 'all'
end end
it_behaves_like 'call Slack/Mattermost API'
end end
end end
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment