mail_service_spec.rb 6.18 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'

Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
17
describe Ci::MailService do
18
  describe "Associations" do
Dmitriy Zaporozhets's avatar
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
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
31
    let(:mail)   { Ci::MailService.new }
Kamil Trzcinski's avatar
Kamil Trzcinski committed
32
    let(:user)   { User.new(notification_email: 'git@example.com')}
33 34

    describe 'failed build' do
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
35
      let(:project) { FactoryGirl.create(:ci_project, email_add_pusher: true) }
Kamil Trzcinski's avatar
WIP  
Kamil Trzcinski committed
36 37
      let(:gl_project) { FactoryGirl.create(:empty_project, gitlab_ci_project: project) }
      let(:commit) { FactoryGirl.create(:ci_commit, gl_project: gl_project) }
Kamil Trzcinski's avatar
Kamil Trzcinski committed
38
      let(:build) { FactoryGirl.create(:ci_build, status: 'failed', commit: commit, user: user) }
39 40

      before do
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
41
        allow(mail).to receive_messages(
42 43 44 45 46
          project: project
        )
      end

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

      def should_email(email)
52 53
        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)
54 55 56 57
      end
    end

    describe 'successfull build' do
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
58
      let(:project) { FactoryGirl.create(:ci_project, email_add_pusher: true, email_only_broken_builds: false) }
Kamil Trzcinski's avatar
WIP  
Kamil Trzcinski committed
59 60
      let(:gl_project) { FactoryGirl.create(:empty_project, gitlab_ci_project: project) }
      let(:commit) { FactoryGirl.create(:ci_commit, gl_project: gl_project) }
61
      let(:build) { FactoryGirl.create(:ci_build, status: 'success', commit: commit, user: user) }
62 63

      before do
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
64
        allow(mail).to receive_messages(
65 66 67 68 69
          project: project
        )
      end

      it do
70
        should_email("git@example.com")
71
        mail.execute(build)
72 73 74
      end

      def should_email(email)
75 76
        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)
77 78 79 80
      end
    end

    describe 'successfull build and project has email_recipients' do
Valery Sizov's avatar
Valery Sizov committed
81
      let(:project) do
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
82
        FactoryGirl.create(:ci_project,
83 84 85
                           email_add_pusher: true,
                           email_only_broken_builds: false,
                           email_recipients: "jeroen@example.com")
Valery Sizov's avatar
Valery Sizov committed
86
      end
Kamil Trzcinski's avatar
WIP  
Kamil Trzcinski committed
87 88
      let(:gl_project) { FactoryGirl.create(:empty_project, gitlab_ci_project: project) }
      let(:commit) { FactoryGirl.create(:ci_commit, gl_project: gl_project) }
89
      let(:build) { FactoryGirl.create(:ci_build, status: 'success', commit: commit, user: user) }
90 91

      before do
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
92
        allow(mail).to receive_messages(
93 94 95 96 97
          project: project
        )
      end

      it do
98 99
        should_email("git@example.com")
        should_email("jeroen@example.com")
100
        mail.execute(build)
101 102 103
      end

      def should_email(email)
104 105
        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)
106 107 108 109
      end
    end

    describe 'successful build and notify only broken builds' do
Valery Sizov's avatar
Valery Sizov committed
110
      let(:project) do
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
111
        FactoryGirl.create(:ci_project,
112 113 114
                           email_add_pusher: true,
                           email_only_broken_builds: true,
                           email_recipients: "jeroen@example.com")
Valery Sizov's avatar
Valery Sizov committed
115
      end
Kamil Trzcinski's avatar
WIP  
Kamil Trzcinski committed
116 117
      let(:gl_project) { FactoryGirl.create(:empty_project, gitlab_ci_project: project) }
      let(:commit) { FactoryGirl.create(:ci_commit, gl_project: gl_project) }
118
      let(:build) { FactoryGirl.create(:ci_build, status: 'success', commit: commit, user: user) }
119 120

      before do
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
121
        allow(mail).to receive_messages(
122 123 124 125 126
          project: project
        )
      end

      it do
127 128
        should_email(commit.git_author_email)
        should_email("jeroen@example.com")
129
        mail.execute(build) if mail.can_execute?(build)
130 131 132
      end

      def should_email(email)
133 134
        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)
135 136 137 138
      end
    end

    describe 'successful build and can test service' do
Valery Sizov's avatar
Valery Sizov committed
139
      let(:project) do
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
140
        FactoryGirl.create(:ci_project,
141 142 143
                           email_add_pusher: true,
                           email_only_broken_builds: false,
                           email_recipients: "jeroen@example.com")
Valery Sizov's avatar
Valery Sizov committed
144
      end
Kamil Trzcinski's avatar
WIP  
Kamil Trzcinski committed
145 146
      let(:gl_project) { FactoryGirl.create(:empty_project, gitlab_ci_project: project) }
      let(:commit) { FactoryGirl.create(:ci_commit, gl_project: gl_project) }
147
      let(:build) { FactoryGirl.create(:ci_build, status: 'success', commit: commit, user: user) }
148 149

      before do
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
150
        allow(mail).to receive_messages(
151 152 153 154 155 156
          project: project
        )
        build
      end

      it do
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
157
        expect(mail.can_test?).to eq(true)
158 159 160 161
      end
    end

    describe 'retried build should not receive email' do
Valery Sizov's avatar
Valery Sizov committed
162
      let(:project) do
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
163
        FactoryGirl.create(:ci_project,
164 165 166
                           email_add_pusher: true,
                           email_only_broken_builds: true,
                           email_recipients: "jeroen@example.com")
Valery Sizov's avatar
Valery Sizov committed
167
      end
Kamil Trzcinski's avatar
WIP  
Kamil Trzcinski committed
168 169
      let(:gl_project) { FactoryGirl.create(:empty_project, gitlab_ci_project: project) }
      let(:commit) { FactoryGirl.create(:ci_commit, gl_project: gl_project) }
Kamil Trzcinski's avatar
Kamil Trzcinski committed
170
      let(:build) { FactoryGirl.create(:ci_build, status: 'failed', commit: commit, user: user) }
171 172

      before do
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
173
        allow(mail).to receive_messages(
174 175 176 177 178
          project: project
        )
      end

      it do
Valery Sizov's avatar
Valery Sizov committed
179
        Ci::Build.retry(build)
180 181
        should_email(commit.git_author_email)
        should_email("jeroen@example.com")
182
        mail.execute(build) if mail.can_execute?(build)
183 184 185
      end

      def should_email(email)
186 187
        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)
188 189 190 191
      end
    end
  end
end