pipeline_email_service_spec.rb 3.6 KB
Newer Older
L
Lin Jen-Shin 已提交
1 2 3
require 'spec_helper'

describe PipelinesEmailService do
4 5
  include EmailHelpers

L
Lin Jen-Shin 已提交
6 7 8 9 10
  let(:pipeline) do
    create(:ci_pipeline, project: project, sha: project.commit('master').sha)
  end

  let(:project) { create(:project) }
L
Lin Jen-Shin 已提交
11 12
  let(:recipient) { 'test@gitlab.com' }

L
Lin Jen-Shin 已提交
13 14
  let(:data) do
    Gitlab::DataBuilder::Pipeline.build(pipeline)
L
Lin Jen-Shin 已提交
15 16
  end

L
Lin Jen-Shin 已提交
17
  before do
18
    reset_delivered_emails!
L
Lin Jen-Shin 已提交
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
  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
      project.team << [user, :developer]
    end

    it 'builds test data' do
      data = subject.test_data(project, user)

      expect(data[:object_kind]).to eq('pipeline')
    end
  end

L
Lin Jen-Shin 已提交
55
  shared_examples 'sending email' do
L
Lin Jen-Shin 已提交
56
    before do
L
Lin Jen-Shin 已提交
57 58 59
      perform_enqueued_jobs do
        run
      end
L
Lin Jen-Shin 已提交
60 61
    end

L
Lin Jen-Shin 已提交
62
    it 'sends email' do
63
      should_only_email(double(notification_email: recipient), kind: :bcc)
L
Lin Jen-Shin 已提交
64 65
    end
  end
L
Lin Jen-Shin 已提交
66

L
Lin Jen-Shin 已提交
67 68 69 70
  shared_examples 'not sending email' do
    before do
      perform_enqueued_jobs do
        run
L
Lin Jen-Shin 已提交
71 72 73
      end
    end

L
Lin Jen-Shin 已提交
74
    it 'does not send email' do
L
Lin Jen-Shin 已提交
75
      should_not_email_anyone
L
Lin Jen-Shin 已提交
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
    end
  end

  describe '#test' do
    def run
      subject.test(data)
    end

    before do
      subject.recipients = recipient
    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
L
Lin Jen-Shin 已提交
96 97 98 99

    context 'when pipeline is succeeded' do
      before do
        data[:object_attributes][:status] = 'success'
L
Lin Jen-Shin 已提交
100
        pipeline.update(status: 'success')
L
Lin Jen-Shin 已提交
101 102 103 104 105 106 107
      end

      it_behaves_like 'sending email'
    end
  end

  describe '#execute' do
L
Lin Jen-Shin 已提交
108 109 110 111
    def run
      subject.execute(data)
    end

L
Lin Jen-Shin 已提交
112 113 114 115 116
    context 'with recipients' do
      before do
        subject.recipients = recipient
      end

L
Lin Jen-Shin 已提交
117 118 119 120 121
      context 'with failed pipeline' do
        before do
          data[:object_attributes][:status] = 'failed'
          pipeline.update(status: 'failed')
        end
L
Lin Jen-Shin 已提交
122

L
Lin Jen-Shin 已提交
123
        it_behaves_like 'sending email'
L
Lin Jen-Shin 已提交
124 125
      end

L
Lin Jen-Shin 已提交
126 127 128 129 130
      context 'with succeeded pipeline' do
        before do
          data[:object_attributes][:status] = 'success'
          pipeline.update(status: 'success')
        end
L
Lin Jen-Shin 已提交
131

L
Lin Jen-Shin 已提交
132
        it_behaves_like 'not sending email'
L
Lin Jen-Shin 已提交
133 134 135 136 137 138 139
      end

      context 'with notify_only_broken_pipelines on' do
        before do
          subject.notify_only_broken_pipelines = true
        end

L
Lin Jen-Shin 已提交
140 141 142 143 144
        context 'with failed pipeline' do
          before do
            data[:object_attributes][:status] = 'failed'
            pipeline.update(status: 'failed')
          end
L
Lin Jen-Shin 已提交
145

L
Lin Jen-Shin 已提交
146 147 148 149 150 151 152 153
          it_behaves_like 'sending email'
        end

        context 'with succeeded pipeline' do
          before do
            data[:object_attributes][:status] = 'success'
            pipeline.update(status: 'success')
          end
L
Lin Jen-Shin 已提交
154

L
Lin Jen-Shin 已提交
155
          it_behaves_like 'not sending email'
L
Lin Jen-Shin 已提交
156 157 158 159
        end
      end
    end

L
Lin Jen-Shin 已提交
160 161 162 163
    context 'with empty recipients list' do
      before do
        subject.recipients = ' ,, '
      end
L
Lin Jen-Shin 已提交
164

L
Lin Jen-Shin 已提交
165 166 167 168 169
      context 'with failed pipeline' do
        before do
          data[:object_attributes][:status] = 'failed'
          pipeline.update(status: 'failed')
        end
L
Lin Jen-Shin 已提交
170

L
Lin Jen-Shin 已提交
171 172
        it_behaves_like 'not sending email'
      end
L
Lin Jen-Shin 已提交
173 174 175
    end
  end
end