builds_email_service_spec.rb 2.23 KB
Newer Older
1 2 3 4 5
require 'spec_helper'

describe BuildsEmailService do
  let(:build) { create(:ci_build) }
  let(:data) { Gitlab::BuildDataBuilder.build(build) }
6 7
  let!(:project) { create(:project, :public, ci_id: 1) }
  let(:service) { described_class.new(project: project, active: true) }
8

9
  describe '#execute' do
10
    it 'sends email' do
11 12 13 14 15 16
      service.recipients = 'test@gitlab.com'
      data[:build_status] = 'failed'
      expect(BuildEmailWorker).to receive(:perform_async)
      service.execute(data)
    end

17 18 19 20 21 22 23 24
    it 'does not send email with succeeded build and notify_only_broken_builds on' do
      expect(service).to receive(:notify_only_broken_builds).and_return(true)
      data[:build_status] = 'success'
      expect(BuildEmailWorker).not_to receive(:perform_async)
      service.execute(data)
    end

    it 'does not send email with failed build and build_allow_failure is true' do
25 26 27 28 29
      data[:build_status] = 'failed'
      data[:build_allow_failure] = true
      expect(BuildEmailWorker).not_to receive(:perform_async)
      service.execute(data)
    end
30 31 32 33 34 35 36 37 38 39 40 41 42

    it 'does not send email with unknown build status' do
      data[:build_status] = 'foo'
      expect(BuildEmailWorker).not_to receive(:perform_async)
      service.execute(data)
    end

    it 'does not send email when recipients list is empty' do
      service.recipients = ' ,, '
      data[:build_status] = 'failed'
      expect(BuildEmailWorker).not_to receive(:perform_async)
      service.execute(data)
    end
43
  end
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75

  describe 'validations' do

    context 'when pusher is not added' do
      before { service.add_pusher = false }

      it 'does not allow empty recipient input' do
        service.recipients = ''
        expect(service.valid?).to be false
      end

      it 'does allow non-empty recipient input' do
        service.recipients = 'test@example.com'
        expect(service.valid?).to be true
      end

    end

    context 'when pusher is added' do
      before { service.add_pusher = true }

      it 'does allow empty recipient input' do
        service.recipients = ''
        expect(service.valid?).to be true
      end

      it 'does allow non-empty recipient input' do
        service.recipients = 'test@example.com'
        expect(service.valid?).to be true
      end
    end
  end
76
end