1# frozen_string_literal: true 2 3require 'spec_helper' 4require 'email_spec' 5 6RSpec.describe Emails::Pipelines do 7 include EmailSpec::Matchers 8 9 let_it_be(:project) { create(:project, :repository) } 10 11 shared_examples_for 'correct pipeline information' do 12 let(:expected_email_subject) do 13 "#{project.name} | " \ 14 "#{status} pipeline for #{pipeline.source_ref} | " \ 15 "#{pipeline.short_sha}" 16 end 17 18 it 'has a correct information' do 19 expect(subject).to have_subject expected_email_subject 20 expect(subject).to have_body_text pipeline.source_ref 21 expect(subject).to have_body_text status_text 22 end 23 24 context 'when pipeline on master branch has a merge request' do 25 let(:pipeline) { create(:ci_pipeline, ref: 'master', sha: sha, project: project) } 26 27 let!(:merge_request) do 28 create(:merge_request, source_branch: 'master', target_branch: 'feature', 29 source_project: project, target_project: project) 30 end 31 32 it 'has correct information that there is no merge request link' do 33 expect(subject).to have_subject expected_email_subject 34 expect(subject).to have_body_text pipeline.source_ref 35 expect(subject).to have_body_text status_text 36 end 37 end 38 39 context 'when pipeline for merge requests' do 40 let(:pipeline) { merge_request.all_pipelines.first } 41 42 let(:merge_request) do 43 create(:merge_request, :with_detached_merge_request_pipeline, 44 source_project: project, 45 target_project: project) 46 end 47 48 it 'has correct information that there is a merge request link' do 49 expect(subject).to have_subject expected_email_subject 50 expect(subject).to have_body_text merge_request.to_reference 51 expect(subject).to have_body_text pipeline.source_ref 52 expect(subject).not_to have_body_text pipeline.ref 53 end 54 end 55 56 context 'when branch pipeline is set to a merge request as a head pipeline' do 57 let(:pipeline) do 58 create(:ci_pipeline, project: project, ref: ref, sha: sha, 59 merge_requests_as_head_pipeline: [merge_request]) 60 end 61 62 let(:merge_request) do 63 create(:merge_request, source_project: project, target_project: project) 64 end 65 66 it 'has correct information that there is a merge request link' do 67 expect(subject).to have_subject expected_email_subject 68 expect(subject).to have_body_text merge_request.to_reference 69 expect(subject).to have_body_text pipeline.source_ref 70 end 71 end 72 end 73 74 shared_examples_for 'only accepts a single recipient' do 75 let(:recipient) { ['test@gitlab.com', 'test2@gitlab.com'] } 76 77 it 'raises an ArgumentError' do 78 expect { subject.deliver_now }.to raise_error(ArgumentError) 79 end 80 end 81 82 describe '#pipeline_success_email' do 83 subject { Notify.pipeline_success_email(pipeline, recipient) } 84 85 let(:pipeline) { create(:ci_pipeline, project: project, ref: ref, sha: sha) } 86 let(:recipient) { pipeline.user.try(:email) } 87 let(:ref) { 'master' } 88 let(:sha) { project.commit(ref).sha } 89 90 it_behaves_like 'correct pipeline information' do 91 let(:status) { 'Successful' } 92 let(:status_text) { "Pipeline ##{pipeline.id} has passed!" } 93 let(:email_subject_suffix) { 'A Nice Suffix' } 94 let(:expected_email_subject) do 95 "#{project.name} | " \ 96 "#{status} pipeline for #{pipeline.source_ref} | " \ 97 "#{pipeline.short_sha} | " \ 98 "#{email_subject_suffix}" 99 end 100 101 before do 102 stub_config_setting(email_subject_suffix: email_subject_suffix) 103 end 104 end 105 106 it_behaves_like 'only accepts a single recipient' 107 end 108 109 describe '#pipeline_failed_email' do 110 subject { Notify.pipeline_failed_email(pipeline, recipient) } 111 112 let(:pipeline) { create(:ci_pipeline, project: project, ref: ref, sha: sha) } 113 let(:recipient) { pipeline.user.try(:email) } 114 let(:ref) { 'master' } 115 let(:sha) { project.commit(ref).sha } 116 117 it_behaves_like 'correct pipeline information' do 118 let(:status) { 'Failed' } 119 let(:status_text) { "Pipeline ##{pipeline.id} has failed!" } 120 end 121 122 it_behaves_like 'only accepts a single recipient' 123 end 124 125 describe '#pipeline_fixed_email' do 126 subject { Notify.pipeline_fixed_email(pipeline, pipeline.user.try(:email)) } 127 128 let(:pipeline) { create(:ci_pipeline, project: project, ref: ref, sha: sha) } 129 let(:recipient) { pipeline.user.try(:email) } 130 let(:ref) { 'master' } 131 let(:sha) { project.commit(ref).sha } 132 133 it_behaves_like 'correct pipeline information' do 134 let(:status) { 'Fixed' } 135 let(:status_text) { "Pipeline has been fixed and ##{pipeline.id} has passed!" } 136 end 137 138 it_behaves_like 'only accepts a single recipient' 139 end 140end 141