2016-09-13 13:10:58 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2017-08-02 12:43:51 -04:00
|
|
|
describe PipelinesEmailService, :mailer do
|
2016-09-23 07:11:16 -04:00
|
|
|
let(:pipeline) do
|
|
|
|
create(:ci_pipeline, project: project, sha: project.commit('master').sha)
|
|
|
|
end
|
|
|
|
|
2017-01-26 17:44:58 -05:00
|
|
|
let(:project) { create(:project, :repository) }
|
2017-09-13 08:24:19 -04:00
|
|
|
let(:recipients) { 'test@gitlab.com' }
|
|
|
|
let(:receivers) { [recipients] }
|
2016-09-13 13:10:58 -04:00
|
|
|
|
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
|
|
|
|
|
|
|
|
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
|
2017-12-22 03:18:28 -05:00
|
|
|
project.add_developer(user)
|
2016-09-13 13:10:58 -04:00
|
|
|
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
|
2017-09-13 08:24:19 -04:00
|
|
|
subject.recipients = recipients
|
|
|
|
|
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
|
2017-09-13 08:24:19 -04:00
|
|
|
emails = receivers.map { |r| double(notification_email: r) }
|
|
|
|
|
|
|
|
should_only_email(*emails, 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
|
2017-09-13 08:24:19 -04:00
|
|
|
subject.recipients = recipients
|
|
|
|
|
2016-09-14 08:38:20 -04:00
|
|
|
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
|
|
|
|
|
|
|
|
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
|
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
|
2017-09-13 08:24:19 -04:00
|
|
|
let(:recipients) { ' ,, ' }
|
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
|
2017-09-13 08:24:19 -04:00
|
|
|
|
|
|
|
context 'with recipients list separating with newlines' do
|
|
|
|
let(:recipients) { "\ntest@gitlab.com, \r\nexample@gitlab.com" }
|
|
|
|
let(:receivers) { %w[test@gitlab.com example@gitlab.com] }
|
|
|
|
|
|
|
|
context 'with failed pipeline' do
|
|
|
|
before do
|
|
|
|
data[:object_attributes][:status] = 'failed'
|
|
|
|
pipeline.update(status: 'failed')
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'sending email'
|
|
|
|
end
|
|
|
|
end
|
2016-09-13 13:10:58 -04:00
|
|
|
end
|
|
|
|
end
|