2015-12-22 15:15:06 -05:00
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe BuildsEmailService do
|
2016-08-04 11:44:27 -04:00
|
|
|
let(:data) do
|
2016-08-12 04:09:29 -04:00
|
|
|
Gitlab::DataBuilder::Build.build(create(:ci_build))
|
2016-08-04 11:44:27 -04:00
|
|
|
end
|
2016-04-21 11:13:14 -04:00
|
|
|
|
|
|
|
describe 'Validations' do
|
|
|
|
context 'when service is active' do
|
|
|
|
before { subject.active = true }
|
|
|
|
|
|
|
|
it { is_expected.to validate_presence_of(:recipients) }
|
|
|
|
|
|
|
|
context 'when pusher is added' do
|
|
|
|
before { subject.add_pusher = true }
|
|
|
|
|
|
|
|
it { is_expected.not_to validate_presence_of(:recipients) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when service is inactive' do
|
|
|
|
before { subject.active = false }
|
|
|
|
|
|
|
|
it { is_expected.not_to validate_presence_of(:recipients) }
|
|
|
|
end
|
|
|
|
end
|
2015-12-22 15:15:06 -05:00
|
|
|
|
2016-07-12 17:19:47 -04:00
|
|
|
describe '#test_data' do
|
|
|
|
let(:build) { create(:ci_build) }
|
|
|
|
let(:project) { build.project }
|
|
|
|
let(:user) { create(:user) }
|
|
|
|
|
|
|
|
before { project.team << [user, :developer] }
|
|
|
|
|
|
|
|
it 'builds test data' do
|
|
|
|
data = subject.test_data(project)
|
|
|
|
|
|
|
|
expect(data[:object_kind]).to eq("build")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#test' do
|
|
|
|
it 'sends email' do
|
2016-08-12 04:09:29 -04:00
|
|
|
data = Gitlab::DataBuilder::Build.build(create(:ci_build))
|
2016-07-12 17:19:47 -04:00
|
|
|
subject.recipients = 'test@gitlab.com'
|
|
|
|
|
|
|
|
expect(BuildEmailWorker).to receive(:perform_async)
|
|
|
|
|
|
|
|
subject.test(data)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'notify only failed builds is true' do
|
|
|
|
it 'sends email' do
|
2016-08-12 04:09:29 -04:00
|
|
|
data = Gitlab::DataBuilder::Build.build(create(:ci_build))
|
2016-07-12 17:19:47 -04:00
|
|
|
data[:build_status] = "success"
|
|
|
|
subject.recipients = 'test@gitlab.com'
|
|
|
|
|
|
|
|
expect(subject).not_to receive(:notify_only_broken_builds)
|
|
|
|
expect(BuildEmailWorker).to receive(:perform_async)
|
|
|
|
|
|
|
|
subject.test(data)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-04-14 04:24:09 -04:00
|
|
|
describe '#execute' do
|
2016-04-05 12:15:13 -04:00
|
|
|
it 'sends email' do
|
2016-04-21 11:13:14 -04:00
|
|
|
subject.recipients = 'test@gitlab.com'
|
2015-12-22 15:15:06 -05:00
|
|
|
data[:build_status] = 'failed'
|
2016-04-21 11:13:14 -04:00
|
|
|
|
2015-12-22 15:15:06 -05:00
|
|
|
expect(BuildEmailWorker).to receive(:perform_async)
|
2016-04-21 11:13:14 -04:00
|
|
|
|
|
|
|
subject.execute(data)
|
2015-12-22 15:15:06 -05:00
|
|
|
end
|
|
|
|
|
2016-04-05 12:15:13 -04:00
|
|
|
it 'does not send email with succeeded build and notify_only_broken_builds on' do
|
2016-04-21 11:13:14 -04:00
|
|
|
expect(subject).to receive(:notify_only_broken_builds).and_return(true)
|
2016-04-05 12:15:13 -04:00
|
|
|
data[:build_status] = 'success'
|
2016-04-21 11:13:14 -04:00
|
|
|
|
2016-04-05 12:15:13 -04:00
|
|
|
expect(BuildEmailWorker).not_to receive(:perform_async)
|
2016-04-21 11:13:14 -04:00
|
|
|
|
|
|
|
subject.execute(data)
|
2016-04-05 12:15:13 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not send email with failed build and build_allow_failure is true' do
|
2015-12-22 15:15:06 -05:00
|
|
|
data[:build_status] = 'failed'
|
|
|
|
data[:build_allow_failure] = true
|
2016-04-21 11:13:14 -04:00
|
|
|
|
2015-12-22 15:15:06 -05:00
|
|
|
expect(BuildEmailWorker).not_to receive(:perform_async)
|
2016-04-21 11:13:14 -04:00
|
|
|
|
|
|
|
subject.execute(data)
|
2015-12-22 15:15:06 -05:00
|
|
|
end
|
2016-04-05 12:15:13 -04:00
|
|
|
|
|
|
|
it 'does not send email with unknown build status' do
|
|
|
|
data[:build_status] = 'foo'
|
|
|
|
|
|
|
|
expect(BuildEmailWorker).not_to receive(:perform_async)
|
2016-04-14 04:24:09 -04:00
|
|
|
|
2016-04-21 11:13:14 -04:00
|
|
|
subject.execute(data)
|
2016-04-14 04:24:09 -04:00
|
|
|
end
|
|
|
|
|
2016-04-21 11:13:14 -04:00
|
|
|
it 'does not send email when recipients list is empty' do
|
|
|
|
subject.recipients = ' ,, '
|
|
|
|
data[:build_status] = 'failed'
|
2016-04-14 04:24:09 -04:00
|
|
|
|
2016-04-21 11:13:14 -04:00
|
|
|
expect(BuildEmailWorker).not_to receive(:perform_async)
|
2016-04-14 04:24:09 -04:00
|
|
|
|
2016-04-21 11:13:14 -04:00
|
|
|
subject.execute(data)
|
2016-04-14 04:24:09 -04:00
|
|
|
end
|
|
|
|
end
|
2015-12-22 15:15:06 -05:00
|
|
|
end
|