2019-09-27 11:06:16 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
require 'email_spec'
|
|
|
|
|
2020-06-24 14:09:03 -04:00
|
|
|
RSpec.describe Emails::Pipelines do
|
2019-09-27 11:06:16 -04:00
|
|
|
include EmailSpec::Matchers
|
|
|
|
|
2020-02-18 10:08:51 -05:00
|
|
|
let_it_be(:project) { create(:project, :repository) }
|
2019-09-27 11:06:16 -04:00
|
|
|
|
|
|
|
shared_examples_for 'correct pipeline information' do
|
2021-08-18 20:11:16 -04:00
|
|
|
let(:expected_email_subject) do
|
|
|
|
"#{project.name} | " \
|
|
|
|
"#{status} pipeline for #{pipeline.source_ref} | " \
|
|
|
|
"#{pipeline.short_sha}"
|
|
|
|
end
|
2019-09-27 11:06:16 -04:00
|
|
|
|
2021-08-18 20:11:16 -04:00
|
|
|
it 'has a correct information' do
|
|
|
|
expect(subject).to have_subject expected_email_subject
|
2019-09-27 11:06:16 -04:00
|
|
|
expect(subject).to have_body_text pipeline.source_ref
|
|
|
|
expect(subject).to have_body_text status_text
|
|
|
|
end
|
|
|
|
|
2020-02-17 07:09:20 -05:00
|
|
|
context 'when pipeline on master branch has a merge request' do
|
|
|
|
let(:pipeline) { create(:ci_pipeline, ref: 'master', sha: sha, project: project) }
|
|
|
|
|
|
|
|
let!(:merge_request) do
|
|
|
|
create(:merge_request, source_branch: 'master', target_branch: 'feature',
|
2022-08-26 02:11:44 -04:00
|
|
|
source_project: project, target_project: project)
|
2020-02-17 07:09:20 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'has correct information that there is no merge request link' do
|
2021-08-18 20:11:16 -04:00
|
|
|
expect(subject).to have_subject expected_email_subject
|
2020-02-17 07:09:20 -05:00
|
|
|
expect(subject).to have_body_text pipeline.source_ref
|
|
|
|
expect(subject).to have_body_text status_text
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-09-27 11:06:16 -04:00
|
|
|
context 'when pipeline for merge requests' do
|
|
|
|
let(:pipeline) { merge_request.all_pipelines.first }
|
|
|
|
|
|
|
|
let(:merge_request) do
|
|
|
|
create(:merge_request, :with_detached_merge_request_pipeline,
|
|
|
|
source_project: project,
|
|
|
|
target_project: project)
|
|
|
|
end
|
|
|
|
|
2020-02-17 07:09:20 -05:00
|
|
|
it 'has correct information that there is a merge request link' do
|
2021-08-18 20:11:16 -04:00
|
|
|
expect(subject).to have_subject expected_email_subject
|
2019-09-27 11:06:16 -04:00
|
|
|
expect(subject).to have_body_text merge_request.to_reference
|
|
|
|
expect(subject).to have_body_text pipeline.source_ref
|
|
|
|
expect(subject).not_to have_body_text pipeline.ref
|
|
|
|
end
|
|
|
|
end
|
2020-02-17 07:09:20 -05:00
|
|
|
|
|
|
|
context 'when branch pipeline is set to a merge request as a head pipeline' do
|
|
|
|
let(:pipeline) do
|
|
|
|
create(:ci_pipeline, project: project, ref: ref, sha: sha,
|
2022-08-26 02:11:44 -04:00
|
|
|
merge_requests_as_head_pipeline: [merge_request])
|
2020-02-17 07:09:20 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
let(:merge_request) do
|
|
|
|
create(:merge_request, source_project: project, target_project: project)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'has correct information that there is a merge request link' do
|
2021-08-18 20:11:16 -04:00
|
|
|
expect(subject).to have_subject expected_email_subject
|
2020-02-17 07:09:20 -05:00
|
|
|
expect(subject).to have_body_text merge_request.to_reference
|
|
|
|
expect(subject).to have_body_text pipeline.source_ref
|
|
|
|
end
|
|
|
|
end
|
2019-09-27 11:06:16 -04:00
|
|
|
end
|
|
|
|
|
2021-11-03 08:10:26 -04:00
|
|
|
shared_examples_for 'only accepts a single recipient' do
|
|
|
|
let(:recipient) { ['test@gitlab.com', 'test2@gitlab.com'] }
|
|
|
|
|
|
|
|
it 'raises an ArgumentError' do
|
|
|
|
expect { subject.deliver_now }.to raise_error(ArgumentError)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-09-27 11:06:16 -04:00
|
|
|
describe '#pipeline_success_email' do
|
2021-11-03 08:10:26 -04:00
|
|
|
subject { Notify.pipeline_success_email(pipeline, recipient) }
|
2019-09-27 11:06:16 -04:00
|
|
|
|
|
|
|
let(:pipeline) { create(:ci_pipeline, project: project, ref: ref, sha: sha) }
|
2021-11-03 08:10:26 -04:00
|
|
|
let(:recipient) { pipeline.user.try(:email) }
|
2019-09-27 11:06:16 -04:00
|
|
|
let(:ref) { 'master' }
|
|
|
|
let(:sha) { project.commit(ref).sha }
|
|
|
|
|
|
|
|
it_behaves_like 'correct pipeline information' do
|
2021-03-09 16:09:15 -05:00
|
|
|
let(:status) { 'Successful' }
|
2021-01-08 07:10:35 -05:00
|
|
|
let(:status_text) { "Pipeline ##{pipeline.id} has passed!" }
|
2021-08-18 20:11:16 -04:00
|
|
|
let(:email_subject_suffix) { 'A Nice Suffix' }
|
|
|
|
let(:expected_email_subject) do
|
|
|
|
"#{project.name} | " \
|
|
|
|
"#{status} pipeline for #{pipeline.source_ref} | " \
|
|
|
|
"#{pipeline.short_sha} | " \
|
|
|
|
"#{email_subject_suffix}"
|
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
|
|
|
stub_config_setting(email_subject_suffix: email_subject_suffix)
|
|
|
|
end
|
2019-09-27 11:06:16 -04:00
|
|
|
end
|
2021-11-03 08:10:26 -04:00
|
|
|
|
|
|
|
it_behaves_like 'only accepts a single recipient'
|
2019-09-27 11:06:16 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
describe '#pipeline_failed_email' do
|
2021-11-03 08:10:26 -04:00
|
|
|
subject { Notify.pipeline_failed_email(pipeline, recipient) }
|
2019-09-27 11:06:16 -04:00
|
|
|
|
|
|
|
let(:pipeline) { create(:ci_pipeline, project: project, ref: ref, sha: sha) }
|
2021-11-03 08:10:26 -04:00
|
|
|
let(:recipient) { pipeline.user.try(:email) }
|
2019-09-27 11:06:16 -04:00
|
|
|
let(:ref) { 'master' }
|
|
|
|
let(:sha) { project.commit(ref).sha }
|
|
|
|
|
|
|
|
it_behaves_like 'correct pipeline information' do
|
2021-01-08 07:10:35 -05:00
|
|
|
let(:status) { 'Failed' }
|
|
|
|
let(:status_text) { "Pipeline ##{pipeline.id} has failed!" }
|
2019-09-27 11:06:16 -04:00
|
|
|
end
|
2021-11-03 08:10:26 -04:00
|
|
|
|
|
|
|
it_behaves_like 'only accepts a single recipient'
|
2019-09-27 11:06:16 -04:00
|
|
|
end
|
2020-02-27 13:09:21 -05:00
|
|
|
|
|
|
|
describe '#pipeline_fixed_email' do
|
|
|
|
subject { Notify.pipeline_fixed_email(pipeline, pipeline.user.try(:email)) }
|
|
|
|
|
|
|
|
let(:pipeline) { create(:ci_pipeline, project: project, ref: ref, sha: sha) }
|
2021-11-03 08:10:26 -04:00
|
|
|
let(:recipient) { pipeline.user.try(:email) }
|
2020-02-27 13:09:21 -05:00
|
|
|
let(:ref) { 'master' }
|
|
|
|
let(:sha) { project.commit(ref).sha }
|
|
|
|
|
|
|
|
it_behaves_like 'correct pipeline information' do
|
2021-01-08 07:10:35 -05:00
|
|
|
let(:status) { 'Fixed' }
|
|
|
|
let(:status_text) { "Pipeline has been fixed and ##{pipeline.id} has passed!" }
|
2020-02-27 13:09:21 -05:00
|
|
|
end
|
2021-11-03 08:10:26 -04:00
|
|
|
|
|
|
|
it_behaves_like 'only accepts a single recipient'
|
2020-02-27 13:09:21 -05:00
|
|
|
end
|
2019-09-27 11:06:16 -04:00
|
|
|
end
|