2019-07-07 11:18:12 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-11-03 12:29:30 +05:30
|
|
|
require 'spec_helper'
|
|
|
|
|
2021-09-04 01:27:46 +05:30
|
|
|
RSpec.describe Integrations::PipelinesEmail, :mailer do
|
2016-11-03 12:29:30 +05:30
|
|
|
let(:pipeline) do
|
2019-09-04 21:01:54 +05:30
|
|
|
create(:ci_pipeline, :failed,
|
|
|
|
project: project,
|
|
|
|
sha: project.commit('master').sha,
|
|
|
|
ref: project.default_branch
|
|
|
|
)
|
2016-11-03 12:29:30 +05:30
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
let(:project) { create(:project, :repository) }
|
2018-03-17 18:26:18 +05:30
|
|
|
let(:recipients) { 'test@gitlab.com' }
|
|
|
|
let(:receivers) { [recipients] }
|
2016-11-03 12:29:30 +05:30
|
|
|
|
|
|
|
let(:data) do
|
|
|
|
Gitlab::DataBuilder::Pipeline.build(pipeline)
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'Validations' do
|
2021-09-30 23:02:18 +05:30
|
|
|
context 'when integration is active' do
|
2016-11-03 12:29:30 +05:30
|
|
|
before do
|
|
|
|
subject.active = true
|
|
|
|
end
|
|
|
|
|
|
|
|
it { is_expected.to validate_presence_of(:recipients) }
|
|
|
|
end
|
|
|
|
|
2021-09-30 23:02:18 +05:30
|
|
|
context 'when integration is inactive' do
|
2016-11-03 12:29:30 +05:30
|
|
|
before do
|
|
|
|
subject.active = false
|
|
|
|
end
|
|
|
|
|
|
|
|
it { is_expected.not_to validate_presence_of(:recipients) }
|
|
|
|
end
|
2021-12-11 22:18:48 +05:30
|
|
|
|
|
|
|
describe 'validates number of recipients' do
|
|
|
|
before do
|
|
|
|
stub_const("#{described_class}::RECIPIENTS_LIMIT", 2)
|
|
|
|
end
|
|
|
|
|
|
|
|
subject(:integration) { described_class.new(project: project, recipients: recipients, active: true) }
|
|
|
|
|
|
|
|
context 'valid number of recipients' do
|
|
|
|
let(:recipients) { 'foo@bar.com, , ' }
|
|
|
|
|
|
|
|
it 'does not count empty emails' do
|
|
|
|
is_expected.to be_valid
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'invalid number of recipients' do
|
|
|
|
let(:recipients) { 'foo@bar.com bar@foo.com bob@gitlab.com' }
|
|
|
|
|
|
|
|
it { is_expected.not_to be_valid }
|
|
|
|
|
|
|
|
it 'adds an error message' do
|
|
|
|
integration.valid?
|
|
|
|
|
|
|
|
expect(integration.errors).to contain_exactly('Recipients can\'t exceed 2')
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when integration is not active' do
|
|
|
|
before do
|
|
|
|
integration.active = false
|
|
|
|
end
|
|
|
|
|
|
|
|
it { is_expected.to be_valid }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2016-11-03 12:29:30 +05:30
|
|
|
end
|
|
|
|
|
2019-12-04 20:38:33 +05:30
|
|
|
shared_examples 'sending email' do |branches_to_be_notified: nil|
|
2016-11-03 12:29:30 +05:30
|
|
|
before do
|
2018-03-17 18:26:18 +05:30
|
|
|
subject.recipients = recipients
|
2019-12-04 20:38:33 +05:30
|
|
|
subject.branches_to_be_notified = branches_to_be_notified if branches_to_be_notified
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2016-11-03 12:29:30 +05:30
|
|
|
perform_enqueued_jobs do
|
|
|
|
run
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'sends email' do
|
2021-11-11 11:23:49 +05:30
|
|
|
emails = receivers.map { |r| double(notification_email_or_default: r) }
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2021-12-11 22:18:48 +05:30
|
|
|
should_only_email(*emails)
|
2016-11-03 12:29:30 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-12-04 20:38:33 +05:30
|
|
|
shared_examples 'not sending email' do |branches_to_be_notified: nil|
|
2016-11-03 12:29:30 +05:30
|
|
|
before do
|
2018-03-17 18:26:18 +05:30
|
|
|
subject.recipients = recipients
|
2019-12-04 20:38:33 +05:30
|
|
|
subject.branches_to_be_notified = branches_to_be_notified if branches_to_be_notified
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2016-11-03 12:29:30 +05:30
|
|
|
perform_enqueued_jobs do
|
|
|
|
run
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not send email' do
|
2017-08-17 22:00:37 +05:30
|
|
|
should_not_email_anyone
|
2016-11-03 12:29:30 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#test' do
|
|
|
|
def run
|
|
|
|
subject.test(data)
|
|
|
|
end
|
|
|
|
|
2019-09-04 21:01:54 +05:30
|
|
|
context 'when pipeline is failed and on default branch' do
|
2016-11-03 12:29:30 +05:30
|
|
|
it_behaves_like 'sending email'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when pipeline is succeeded' do
|
|
|
|
before do
|
|
|
|
data[:object_attributes][:status] = 'success'
|
2020-11-24 15:15:51 +05:30
|
|
|
pipeline.update!(status: 'success')
|
2016-11-03 12:29:30 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'sending email'
|
|
|
|
end
|
2019-09-04 21:01:54 +05:30
|
|
|
|
2019-12-04 20:38:33 +05:30
|
|
|
context 'when the pipeline failed' do
|
|
|
|
context 'on default branch' do
|
|
|
|
before do
|
|
|
|
data[:object_attributes][:ref] = project.default_branch
|
2020-11-24 15:15:51 +05:30
|
|
|
pipeline.update!(ref: project.default_branch)
|
2019-12-04 20:38:33 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
context 'notifications are enabled only for default branch' do
|
|
|
|
it_behaves_like 'sending email', branches_to_be_notified: "default"
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'notifications are enabled only for protected branch' do
|
|
|
|
it_behaves_like 'sending email', branches_to_be_notified: "protected"
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'notifications are enabled only for default and protected branches ' do
|
|
|
|
it_behaves_like 'sending email', branches_to_be_notified: "default_and_protected"
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'notifications are enabled only for all branches' do
|
|
|
|
it_behaves_like 'sending email', branches_to_be_notified: "all"
|
|
|
|
end
|
2019-09-04 21:01:54 +05:30
|
|
|
end
|
|
|
|
|
2019-12-04 20:38:33 +05:30
|
|
|
context 'on a protected branch' do
|
2019-09-04 21:01:54 +05:30
|
|
|
before do
|
2019-12-04 20:38:33 +05:30
|
|
|
create(:protected_branch, project: project, name: 'a-protected-branch')
|
|
|
|
data[:object_attributes][:ref] = 'a-protected-branch'
|
2020-11-24 15:15:51 +05:30
|
|
|
pipeline.update!(ref: 'a-protected-branch')
|
2019-09-04 21:01:54 +05:30
|
|
|
end
|
|
|
|
|
2019-12-04 20:38:33 +05:30
|
|
|
context 'notifications are enabled only for default branch' do
|
|
|
|
it_behaves_like 'sending email', branches_to_be_notified: "default"
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'notifications are enabled only for protected branch' do
|
|
|
|
it_behaves_like 'sending email', branches_to_be_notified: "protected"
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'notifications are enabled only for default and protected branches ' do
|
|
|
|
it_behaves_like 'sending email', branches_to_be_notified: "default_and_protected"
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'notifications are enabled only for all branches' do
|
|
|
|
it_behaves_like 'sending email', branches_to_be_notified: "all"
|
|
|
|
end
|
2019-09-04 21:01:54 +05:30
|
|
|
end
|
|
|
|
|
2019-12-04 20:38:33 +05:30
|
|
|
context 'on a neither protected nor default branch' do
|
|
|
|
before do
|
|
|
|
data[:object_attributes][:ref] = 'a-random-branch'
|
2020-11-24 15:15:51 +05:30
|
|
|
pipeline.update!(ref: 'a-random-branch')
|
2019-12-04 20:38:33 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
context 'notifications are enabled only for default branch' do
|
|
|
|
it_behaves_like 'sending email', branches_to_be_notified: "default"
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'notifications are enabled only for protected branch' do
|
|
|
|
it_behaves_like 'sending email', branches_to_be_notified: "protected"
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'notifications are enabled only for default and protected branches ' do
|
|
|
|
it_behaves_like 'sending email', branches_to_be_notified: "default_and_protected"
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'notifications are enabled only for all branches' do
|
|
|
|
it_behaves_like 'sending email', branches_to_be_notified: "all"
|
|
|
|
end
|
2019-09-04 21:01:54 +05:30
|
|
|
end
|
|
|
|
end
|
2016-11-03 12:29:30 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
describe '#execute' do
|
2019-12-04 20:38:33 +05:30
|
|
|
before do
|
|
|
|
subject.project = project
|
|
|
|
end
|
|
|
|
|
2016-11-03 12:29:30 +05:30
|
|
|
def run
|
|
|
|
subject.execute(data)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with recipients' do
|
|
|
|
context 'with failed pipeline' do
|
|
|
|
it_behaves_like 'sending email'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with succeeded pipeline' do
|
|
|
|
before do
|
|
|
|
data[:object_attributes][:status] = 'success'
|
2020-11-24 15:15:51 +05:30
|
|
|
pipeline.update!(status: 'success')
|
2016-11-03 12:29:30 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'not sending email'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with notify_only_broken_pipelines on' do
|
|
|
|
before do
|
|
|
|
subject.notify_only_broken_pipelines = true
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with failed pipeline' do
|
|
|
|
it_behaves_like 'sending email'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with succeeded pipeline' do
|
|
|
|
before do
|
|
|
|
data[:object_attributes][:status] = 'success'
|
2020-11-24 15:15:51 +05:30
|
|
|
pipeline.update!(status: 'success')
|
2016-11-03 12:29:30 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'not sending email'
|
|
|
|
end
|
|
|
|
end
|
2019-09-04 21:01:54 +05:30
|
|
|
|
2019-12-04 20:38:33 +05:30
|
|
|
context 'when the pipeline failed' do
|
|
|
|
context 'on default branch' do
|
|
|
|
before do
|
|
|
|
data[:object_attributes][:ref] = project.default_branch
|
2020-11-24 15:15:51 +05:30
|
|
|
pipeline.update!(ref: project.default_branch)
|
2019-12-04 20:38:33 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
context 'notifications are enabled only for default branch' do
|
|
|
|
it_behaves_like 'sending email', branches_to_be_notified: "default"
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'notifications are enabled only for protected branch' do
|
|
|
|
it_behaves_like 'not sending email', branches_to_be_notified: "protected"
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'notifications are enabled only for default and protected branches ' do
|
|
|
|
it_behaves_like 'sending email', branches_to_be_notified: "default_and_protected"
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'notifications are enabled only for all branches' do
|
|
|
|
it_behaves_like 'sending email', branches_to_be_notified: "all"
|
|
|
|
end
|
2019-09-04 21:01:54 +05:30
|
|
|
end
|
|
|
|
|
2019-12-04 20:38:33 +05:30
|
|
|
context 'on a protected branch' do
|
2019-09-04 21:01:54 +05:30
|
|
|
before do
|
2019-12-04 20:38:33 +05:30
|
|
|
create(:protected_branch, project: project, name: 'a-protected-branch')
|
|
|
|
data[:object_attributes][:ref] = 'a-protected-branch'
|
2020-11-24 15:15:51 +05:30
|
|
|
pipeline.update!(ref: 'a-protected-branch')
|
2019-09-04 21:01:54 +05:30
|
|
|
end
|
|
|
|
|
2019-12-04 20:38:33 +05:30
|
|
|
context 'notifications are enabled only for default branch' do
|
|
|
|
it_behaves_like 'not sending email', branches_to_be_notified: "default"
|
|
|
|
end
|
2019-09-04 21:01:54 +05:30
|
|
|
|
2019-12-04 20:38:33 +05:30
|
|
|
context 'notifications are enabled only for protected branch' do
|
|
|
|
it_behaves_like 'sending email', branches_to_be_notified: "protected"
|
|
|
|
end
|
2019-09-04 21:01:54 +05:30
|
|
|
|
2019-12-04 20:38:33 +05:30
|
|
|
context 'notifications are enabled only for default and protected branches ' do
|
|
|
|
it_behaves_like 'sending email', branches_to_be_notified: "default_and_protected"
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'notifications are enabled only for all branches' do
|
|
|
|
it_behaves_like 'sending email', branches_to_be_notified: "all"
|
|
|
|
end
|
2019-09-04 21:01:54 +05:30
|
|
|
end
|
|
|
|
|
2019-12-04 20:38:33 +05:30
|
|
|
context 'on a neither protected nor default branch' do
|
2019-09-04 21:01:54 +05:30
|
|
|
before do
|
2019-12-04 20:38:33 +05:30
|
|
|
data[:object_attributes][:ref] = 'a-random-branch'
|
2020-11-24 15:15:51 +05:30
|
|
|
pipeline.update!(ref: 'a-random-branch')
|
2019-09-04 21:01:54 +05:30
|
|
|
end
|
|
|
|
|
2019-12-04 20:38:33 +05:30
|
|
|
context 'notifications are enabled only for default branch' do
|
|
|
|
it_behaves_like 'not sending email', branches_to_be_notified: "default"
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'notifications are enabled only for protected branch' do
|
|
|
|
it_behaves_like 'not sending email', branches_to_be_notified: "protected"
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'notifications are enabled only for default and protected branches ' do
|
|
|
|
it_behaves_like 'not sending email', branches_to_be_notified: "default_and_protected"
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'notifications are enabled only for all branches' do
|
|
|
|
it_behaves_like 'sending email', branches_to_be_notified: "all"
|
|
|
|
end
|
2019-09-04 21:01:54 +05:30
|
|
|
end
|
|
|
|
end
|
2016-11-03 12:29:30 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
context 'with empty recipients list' do
|
2018-03-17 18:26:18 +05:30
|
|
|
let(:recipients) { ' ,, ' }
|
2016-11-03 12:29:30 +05:30
|
|
|
|
|
|
|
context 'with failed pipeline' do
|
|
|
|
before do
|
|
|
|
data[:object_attributes][:status] = 'failed'
|
2020-11-24 15:15:51 +05:30
|
|
|
pipeline.update!(status: 'failed')
|
2016-11-03 12:29:30 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'not sending email'
|
|
|
|
end
|
|
|
|
end
|
2018-03-17 18:26:18 +05:30
|
|
|
|
|
|
|
context 'with recipients list separating with newlines' do
|
2020-03-13 15:44:24 +05:30
|
|
|
let(:recipients) { "\ntest@gitlab.com, \r\nexample@gitlab.com\rother@gitlab.com" }
|
|
|
|
let(:receivers) { %w[test@gitlab.com example@gitlab.com other@gitlab.com] }
|
2018-03-17 18:26:18 +05:30
|
|
|
|
|
|
|
context 'with failed pipeline' do
|
|
|
|
before do
|
|
|
|
data[:object_attributes][:status] = 'failed'
|
2020-11-24 15:15:51 +05:30
|
|
|
pipeline.update!(status: 'failed')
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'sending email'
|
|
|
|
end
|
|
|
|
end
|
2016-11-03 12:29:30 +05:30
|
|
|
end
|
|
|
|
end
|