BigW Consortium Gitlab

builds_email_service_spec.rb 2.79 KB
Newer Older
1 2 3
require 'spec_helper'

describe BuildsEmailService do
4
  let(:data) do
5
    Gitlab::DataBuilder::Build.build(create(:ci_build))
6
  end
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26

  describe 'Validations' do
    context 'when service is active' do
      before { subject.active = true }

      it { is_expected.to validate_presence_of(:recipients) }

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

        it { is_expected.not_to validate_presence_of(:recipients) }
      end
    end

    context 'when service is inactive' do
      before { subject.active = false }

      it { is_expected.not_to validate_presence_of(:recipients) }
    end
  end
27

28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
  describe '#test_data' do
    let(:build)   { create(:ci_build) }
    let(:project) { build.project }
    let(:user)    { create(:user) }

    before { project.team << [user, :developer] }

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

      expect(data[:object_kind]).to eq("build")
    end
  end

  describe '#test' do
    it 'sends email' do
44
      data = Gitlab::DataBuilder::Build.build(create(:ci_build))
45 46 47 48 49 50 51 52 53
      subject.recipients = 'test@gitlab.com'

      expect(BuildEmailWorker).to receive(:perform_async)

      subject.test(data)
    end

    context 'notify only failed builds is true' do
      it 'sends email' do
54
        data = Gitlab::DataBuilder::Build.build(create(:ci_build))
55 56 57 58 59 60 61 62 63 64 65
        data[:build_status] = "success"
        subject.recipients = 'test@gitlab.com'

        expect(subject).not_to receive(:notify_only_broken_builds)
        expect(BuildEmailWorker).to receive(:perform_async)

        subject.test(data)
      end
    end
  end

66
  describe '#execute' do
67
    it 'sends email' do
68
      subject.recipients = 'test@gitlab.com'
69
      data[:build_status] = 'failed'
70

71
      expect(BuildEmailWorker).to receive(:perform_async)
72 73

      subject.execute(data)
74 75
    end

76
    it 'does not send email with succeeded build and notify_only_broken_builds on' do
77
      expect(subject).to receive(:notify_only_broken_builds).and_return(true)
78
      data[:build_status] = 'success'
79

80
      expect(BuildEmailWorker).not_to receive(:perform_async)
81 82

      subject.execute(data)
83 84 85
    end

    it 'does not send email with failed build and build_allow_failure is true' do
86 87
      data[:build_status] = 'failed'
      data[:build_allow_failure] = true
88

89
      expect(BuildEmailWorker).not_to receive(:perform_async)
90 91

      subject.execute(data)
92
    end
93 94 95 96 97

    it 'does not send email with unknown build status' do
      data[:build_status] = 'foo'

      expect(BuildEmailWorker).not_to receive(:perform_async)
98

99
      subject.execute(data)
100 101
    end

102 103 104
    it 'does not send email when recipients list is empty' do
      subject.recipients = ' ,, '
      data[:build_status] = 'failed'
105

106
      expect(BuildEmailWorker).not_to receive(:perform_async)
107

108
      subject.execute(data)
109 110
    end
  end
111
end