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
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