Commit 15bb44fc authored by Lin Jen-Shin's avatar Lin Jen-Shin

test for real

parent c5e0305d
require 'spec_helper' require 'spec_helper'
describe PipelinesEmailService do describe PipelinesEmailService do
let(:data) do let(:pipeline) { create(:ci_pipeline) }
Gitlab::DataBuilder::Pipeline.build(create(:ci_pipeline))
end
let(:recipient) { 'test@gitlab.com' } let(:recipient) { 'test@gitlab.com' }
def expect_pipeline_service let(:data) do
expect_any_instance_of(Ci::SendPipelineNotificationService) Gitlab::DataBuilder::Pipeline.build(pipeline)
end end
def receive_execute before do
receive(:execute).with([recipient]) ActionMailer::Base.deliveries.clear
end end
describe 'Validations' do describe 'Validations' do
...@@ -57,24 +54,53 @@ describe PipelinesEmailService do ...@@ -57,24 +54,53 @@ describe PipelinesEmailService do
end end
end end
describe '#test' do shared_examples 'sending email' do
before do before do
subject.recipients = recipient perform_enqueued_jobs do
run
end
end end
shared_examples 'sending email' do
it 'sends email' do it 'sends email' do
expect_pipeline_service.to receive_execute sent_to = ActionMailer::Base.deliveries.flat_map(&:to)
expect(sent_to).to contain_exactly(recipient)
end
end
shared_examples 'not sending email' do
before do
perform_enqueued_jobs do
run
end
end
it 'does not send email' do
expect(ActionMailer::Base.deliveries).to be_empty
end
end
describe '#test' do
def run
subject.test(data) subject.test(data)
end end
before do
subject.recipients = recipient
end
context 'when pipeline is failed' do
before do
data[:object_attributes][:status] = 'failed'
pipeline.update(status: 'failed')
end end
it_behaves_like 'sending email' it_behaves_like 'sending email'
end
context 'when pipeline is succeeded' do context 'when pipeline is succeeded' do
before do before do
data[:object_attributes][:status] = 'success' data[:object_attributes][:status] = 'success'
pipeline.update(status: 'success')
end end
it_behaves_like 'sending email' it_behaves_like 'sending email'
...@@ -82,25 +108,31 @@ describe PipelinesEmailService do ...@@ -82,25 +108,31 @@ describe PipelinesEmailService do
end end
describe '#execute' do describe '#execute' do
def run
subject.execute(data)
end
context 'with recipients' do context 'with recipients' do
before do before do
subject.recipients = recipient subject.recipients = recipient
end end
it 'sends email for failed pipeline' do context 'with failed pipeline' do
before do
data[:object_attributes][:status] = 'failed' data[:object_attributes][:status] = 'failed'
pipeline.update(status: 'failed')
end
expect_pipeline_service.to receive_execute it_behaves_like 'sending email'
subject.execute(data)
end end
it 'does not send email for succeeded pipeline' do context 'with succeeded pipeline' do
before do
data[:object_attributes][:status] = 'success' data[:object_attributes][:status] = 'success'
pipeline.update(status: 'success')
end
expect_pipeline_service.not_to receive_execute it_behaves_like 'not sending email'
subject.execute(data)
end end
context 'with notify_only_broken_pipelines on' do context 'with notify_only_broken_pipelines on' do
...@@ -108,23 +140,39 @@ describe PipelinesEmailService do ...@@ -108,23 +140,39 @@ describe PipelinesEmailService do
subject.notify_only_broken_pipelines = true subject.notify_only_broken_pipelines = true
end end
it 'sends email for failed pipeline' do context 'with failed pipeline' do
before do
data[:object_attributes][:status] = 'failed' data[:object_attributes][:status] = 'failed'
pipeline.update(status: 'failed')
end
it_behaves_like 'sending email'
end
expect_pipeline_service.to receive_execute context 'with succeeded pipeline' do
before do
data[:object_attributes][:status] = 'success'
pipeline.update(status: 'success')
end
subject.execute(data) it_behaves_like 'not sending email'
end end
end end
end end
it 'does not send email when recipients list is empty' do context 'with empty recipients list' do
before do
subject.recipients = ' ,, ' subject.recipients = ' ,, '
data[:object_attributes][:status] = 'failed' end
expect_pipeline_service.not_to receive_execute context 'with failed pipeline' do
before do
data[:object_attributes][:status] = 'failed'
pipeline.update(status: 'failed')
end
subject.execute(data) it_behaves_like 'not sending email'
end
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