BigW Consortium Gitlab

mail_service_spec.rb 5.47 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
# == Schema Information
#
# Table name: services
#
#  id         :integer          not null, primary key
#  type       :string(255)
#  title      :string(255)
#  project_id :integer          not null
#  created_at :datetime
#  updated_at :datetime
#  active     :boolean          default(FALSE), not null
#  properties :text
#

require 'spec_helper'

17
describe Ci::MailService do
18
  describe "Associations" do
Dmitriy Zaporozhets committed
19
    it { is_expected.to belong_to :project }
20 21 22 23 24 25 26 27 28 29 30
  end

  describe "Validations" do
    context "active" do
      before do
        subject.active = true
      end
    end
  end

  describe 'Sends email for' do
31
    let(:mail)   { Ci::MailService.new }
32 33

    describe 'failed build' do
34 35 36
      let(:project) { FactoryGirl.create(:ci_project, email_add_pusher: true) }
      let(:commit) { FactoryGirl.create(:ci_commit, project: project) }
      let(:build) { FactoryGirl.create(:ci_build, status: :failed, commit: commit) }
37 38

      before do
Dmitriy Zaporozhets committed
39
        allow(mail).to receive_messages(
40 41 42 43 44
          project: project
        )
      end

      it do
45
        should_email("git@example.com")
46
        mail.execute(build)
47 48 49
      end

      def should_email(email)
50 51
        expect(Ci::Notify).to receive(:build_fail_email).with(build.id, email)
        expect(Ci::Notify).not_to receive(:build_success_email).with(build.id, email)
52 53 54 55
      end
    end

    describe 'successfull build' do
56 57 58
      let(:project) { FactoryGirl.create(:ci_project, email_add_pusher: true, email_only_broken_builds: false) }
      let(:commit) { FactoryGirl.create(:ci_commit, project: project) }
      let(:build) { FactoryGirl.create(:ci_build, status: :success, commit: commit) }
59 60

      before do
Dmitriy Zaporozhets committed
61
        allow(mail).to receive_messages(
62 63 64 65 66
          project: project
        )
      end

      it do
67
        should_email("git@example.com")
68
        mail.execute(build)
69 70 71
      end

      def should_email(email)
72 73
        expect(Ci::Notify).to receive(:build_success_email).with(build.id, email)
        expect(Ci::Notify).not_to receive(:build_fail_email).with(build.id, email)
74 75 76 77
      end
    end

    describe 'successfull build and project has email_recipients' do
Valery Sizov committed
78
      let(:project) do
79
        FactoryGirl.create(:ci_project,
80 81 82
                           email_add_pusher: true,
                           email_only_broken_builds: false,
                           email_recipients: "jeroen@example.com")
Valery Sizov committed
83
      end
84 85
      let(:commit) { FactoryGirl.create(:ci_commit, project: project) }
      let(:build) { FactoryGirl.create(:ci_build, status: :success, commit: commit) }
86 87

      before do
Dmitriy Zaporozhets committed
88
        allow(mail).to receive_messages(
89 90 91 92 93
          project: project
        )
      end

      it do
94 95
        should_email("git@example.com")
        should_email("jeroen@example.com")
96
        mail.execute(build)
97 98 99
      end

      def should_email(email)
100 101
        expect(Ci::Notify).to receive(:build_success_email).with(build.id, email)
        expect(Ci::Notify).not_to receive(:build_fail_email).with(build.id, email)
102 103 104 105
      end
    end

    describe 'successful build and notify only broken builds' do
Valery Sizov committed
106
      let(:project) do
107
        FactoryGirl.create(:ci_project,
108 109 110
                           email_add_pusher: true,
                           email_only_broken_builds: true,
                           email_recipients: "jeroen@example.com")
Valery Sizov committed
111
      end
112 113
      let(:commit) { FactoryGirl.create(:ci_commit, project: project) }
      let(:build) { FactoryGirl.create(:ci_build, status: :success, commit: commit) }
114 115

      before do
Dmitriy Zaporozhets committed
116
        allow(mail).to receive_messages(
117 118 119 120 121
          project: project
        )
      end

      it do
122 123
        should_email(commit.git_author_email)
        should_email("jeroen@example.com")
124
        mail.execute(build) if mail.can_execute?(build)
125 126 127
      end

      def should_email(email)
128 129
        expect(Ci::Notify).not_to receive(:build_success_email).with(build.id, email)
        expect(Ci::Notify).not_to receive(:build_fail_email).with(build.id, email)
130 131 132 133
      end
    end

    describe 'successful build and can test service' do
Valery Sizov committed
134
      let(:project) do
135
        FactoryGirl.create(:ci_project,
136 137 138
                           email_add_pusher: true,
                           email_only_broken_builds: false,
                           email_recipients: "jeroen@example.com")
Valery Sizov committed
139
      end
140 141
      let(:commit) { FactoryGirl.create(:ci_commit, project: project) }
      let(:build) { FactoryGirl.create(:ci_build, status: :success, commit: commit) }
142 143

      before do
Dmitriy Zaporozhets committed
144
        allow(mail).to receive_messages(
145 146 147 148 149 150
          project: project
        )
        build
      end

      it do
Dmitriy Zaporozhets committed
151
        expect(mail.can_test?).to eq(true)
152 153 154 155
      end
    end

    describe 'retried build should not receive email' do
Valery Sizov committed
156
      let(:project) do
157
        FactoryGirl.create(:ci_project,
158 159 160
                           email_add_pusher: true,
                           email_only_broken_builds: true,
                           email_recipients: "jeroen@example.com")
Valery Sizov committed
161
      end
162 163
      let(:commit) { FactoryGirl.create(:ci_commit, project: project) }
      let(:build) { FactoryGirl.create(:ci_build, status: :failed, commit: commit) }
164 165

      before do
Dmitriy Zaporozhets committed
166
        allow(mail).to receive_messages(
167 168 169 170 171
          project: project
        )
      end

      it do
Valery Sizov committed
172
        Ci::Build.retry(build)
173 174
        should_email(commit.git_author_email)
        should_email("jeroen@example.com")
175
        mail.execute(build) if mail.can_execute?(build)
176 177 178
      end

      def should_email(email)
179 180
        expect(Ci::Notify).not_to receive(:build_success_email).with(build.id, email)
        expect(Ci::Notify).not_to receive(:build_fail_email).with(build.id, email)
181 182 183 184
      end
    end
  end
end