gitlab-org--gitlab-foss/spec/models/project_services/pipeline_email_service_spec.rb

176 lines
3.6 KiB
Ruby
Raw Normal View History

2016-09-13 13:10:58 -04:00
require 'spec_helper'
describe PipelinesEmailService do
include EmailHelpers
2016-09-23 07:11:16 -04:00
let(:pipeline) do
create(:ci_pipeline, project: project, sha: project.commit('master').sha)
end
let(:project) { create(:project, :repository) }
2016-09-13 13:10:58 -04:00
let(:recipient) { 'test@gitlab.com' }
2016-09-14 08:38:20 -04:00
let(:data) do
Gitlab::DataBuilder::Pipeline.build(pipeline)
2016-09-13 13:10:58 -04:00
end
2016-09-14 08:38:20 -04:00
before do
reset_delivered_emails!
2016-09-13 13:10:58 -04:00
end
describe 'Validations' do
context 'when service is active' do
before do
subject.active = true
end
it { is_expected.to validate_presence_of(:recipients) }
end
context 'when service is inactive' do
before do
subject.active = false
end
it { is_expected.not_to validate_presence_of(:recipients) }
end
end
describe '#test_data' do
let(:build) { create(:ci_build) }
let(:project) { build.project }
let(:user) { create(:user) }
before do
project.team << [user, :developer]
end
it 'builds test data' do
data = subject.test_data(project, user)
expect(data[:object_kind]).to eq('pipeline')
end
end
2016-09-14 08:38:20 -04:00
shared_examples 'sending email' do
2016-09-13 13:10:58 -04:00
before do
2016-09-14 08:38:20 -04:00
perform_enqueued_jobs do
run
end
2016-09-13 13:10:58 -04:00
end
2016-09-14 08:38:20 -04:00
it 'sends email' do
should_only_email(double(notification_email: recipient), kind: :bcc)
2016-09-14 08:38:20 -04:00
end
end
2016-09-13 13:10:58 -04:00
2016-09-14 08:38:20 -04:00
shared_examples 'not sending email' do
before do
perform_enqueued_jobs do
run
2016-09-13 13:10:58 -04:00
end
end
2016-09-14 08:38:20 -04:00
it 'does not send email' do
2016-10-18 08:53:28 -04:00
should_not_email_anyone
2016-09-14 08:38:20 -04:00
end
end
describe '#test' do
def run
subject.test(data)
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
it_behaves_like 'sending email'
end
2016-09-13 13:10:58 -04:00
context 'when pipeline is succeeded' do
before do
data[:object_attributes][:status] = 'success'
2016-09-14 08:38:20 -04:00
pipeline.update(status: 'success')
2016-09-13 13:10:58 -04:00
end
it_behaves_like 'sending email'
end
end
describe '#execute' do
2016-09-14 08:38:20 -04:00
def run
subject.execute(data)
end
2016-09-13 13:10:58 -04:00
context 'with recipients' do
before do
subject.recipients = recipient
end
2016-09-14 08:38:20 -04:00
context 'with failed pipeline' do
before do
data[:object_attributes][:status] = 'failed'
pipeline.update(status: 'failed')
end
2016-09-13 13:10:58 -04:00
2016-09-14 08:38:20 -04:00
it_behaves_like 'sending email'
2016-09-13 13:10:58 -04:00
end
2016-09-14 08:38:20 -04:00
context 'with succeeded pipeline' do
before do
data[:object_attributes][:status] = 'success'
pipeline.update(status: 'success')
end
2016-09-13 13:10:58 -04:00
2016-09-14 08:38:20 -04:00
it_behaves_like 'not sending email'
2016-09-13 13:10:58 -04:00
end
context 'with notify_only_broken_pipelines on' do
before do
subject.notify_only_broken_pipelines = true
end
2016-09-14 08:38:20 -04:00
context 'with failed pipeline' do
before do
data[:object_attributes][:status] = 'failed'
pipeline.update(status: 'failed')
end
2016-09-13 13:10:58 -04:00
2016-09-14 08:38:20 -04:00
it_behaves_like 'sending email'
end
context 'with succeeded pipeline' do
before do
data[:object_attributes][:status] = 'success'
pipeline.update(status: 'success')
end
2016-09-13 13:10:58 -04:00
2016-09-14 08:38:20 -04:00
it_behaves_like 'not sending email'
2016-09-13 13:10:58 -04:00
end
end
end
2016-09-14 08:38:20 -04:00
context 'with empty recipients list' do
before do
subject.recipients = ' ,, '
end
2016-09-13 13:10:58 -04:00
2016-09-14 08:38:20 -04:00
context 'with failed pipeline' do
before do
data[:object_attributes][:status] = 'failed'
pipeline.update(status: 'failed')
end
2016-09-13 13:10:58 -04:00
2016-09-14 08:38:20 -04:00
it_behaves_like 'not sending email'
end
2016-09-13 13:10:58 -04:00
end
end
end