pipeline_email_service_spec.rb 3.61 KB
Newer Older
1 2 3
require 'spec_helper'

describe PipelinesEmailService do
4 5
  include EmailHelpers

6 7 8 9 10
  let(:pipeline) do
    create(:ci_pipeline, project: project, sha: project.commit('master').sha)
  end

  let(:project) { create(:project) }
11 12
  let(:recipient) { 'test@gitlab.com' }

Lin Jen-Shin's avatar
Lin Jen-Shin committed
13 14
  let(:data) do
    Gitlab::DataBuilder::Pipeline.build(pipeline)
15 16
  end

Lin Jen-Shin's avatar
Lin Jen-Shin committed
17
  before do
18
    reset_delivered_emails!
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

Lin Jen-Shin's avatar
Lin Jen-Shin committed
55
  shared_examples 'sending email' do
56
    before do
Lin Jen-Shin's avatar
Lin Jen-Shin committed
57 58 59
      perform_enqueued_jobs do
        run
      end
60 61
    end

Lin Jen-Shin's avatar
Lin Jen-Shin committed
62
    it 'sends email' do
63
      should_only_email(double(notification_email: recipient), kind: :bcc)
Lin Jen-Shin's avatar
Lin Jen-Shin committed
64 65
    end
  end
66

Lin Jen-Shin's avatar
Lin Jen-Shin committed
67 68 69 70
  shared_examples 'not sending email' do
    before do
      perform_enqueued_jobs do
        run
71 72 73
      end
    end

Lin Jen-Shin's avatar
Lin Jen-Shin committed
74
    it 'does not send email' do
Lin Jen-Shin's avatar
Lin Jen-Shin committed
75
      should_not_email_anyone
Lin Jen-Shin's avatar
Lin Jen-Shin committed
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
96 97 98 99

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

      it_behaves_like 'sending email'
    end
  end

  describe '#execute' do
Lin Jen-Shin's avatar
Lin Jen-Shin committed
108 109 110 111
    def run
      subject.execute(data)
    end

112 113 114 115 116
    context 'with recipients' do
      before do
        subject.recipients = recipient
      end

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

Lin Jen-Shin's avatar
Lin Jen-Shin committed
123
        it_behaves_like 'sending email'
124 125
      end

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

Lin Jen-Shin's avatar
Lin Jen-Shin committed
132
        it_behaves_like 'not sending email'
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

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

Lin Jen-Shin's avatar
Lin Jen-Shin committed
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
154

Lin Jen-Shin's avatar
Lin Jen-Shin committed
155
          it_behaves_like 'not sending email'
156 157 158 159
        end
      end
    end

Lin Jen-Shin's avatar
Lin Jen-Shin committed
160 161 162 163
    context 'with empty recipients list' do
      before do
        subject.recipients = ' ,, '
      end
164

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

Lin Jen-Shin's avatar
Lin Jen-Shin committed
171 172
        it_behaves_like 'not sending email'
      end
173 174 175
    end
  end
end