summaryrefslogtreecommitdiff
path: root/spec/models/mail_service_spec.rb
blob: adc015364c530f98de208197a7c34fcce13e2722 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# == 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'

describe MailService do
  describe "Associations" do
    it { should belong_to :project }
  end

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

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

    describe 'failed build' do
      let(:project) { FactoryGirl.create(:project, email_add_pusher: true) }
      let(:commit) { FactoryGirl.create(:commit, project: project) }
      let(:build) { FactoryGirl.create(:build, status: :failed, commit: commit) }

      before do
        allow(mail).to receive_messages(project: project)
      end

      it do
        should_email("git@example.com")
        mail.execute(build)
      end

      def should_email(email)
        Notify.should_receive(:build_fail_email).with(build.id, email)
        Notify.should_not_receive(:build_success_email).with(build.id, email)
      end
    end

    describe 'successfull build' do
      let(:project) { FactoryGirl.create(:project, email_add_pusher: true, email_only_broken_builds: false) }
      let(:commit) { FactoryGirl.create(:commit, project: project) }
      let(:build) { FactoryGirl.create(:build, status: :success, commit: commit) }

      before do
        allow(mail).to receive_messages(project: project)
      end

      it do
        should_email("git@example.com")
        mail.execute(build)
      end

      def should_email(email)
        Notify.should_receive(:build_success_email).with(build.id, email)
        Notify.should_not_receive(:build_fail_email).with(build.id, email)
      end
    end

    describe 'successfull build and project has email_recipients' do
      let(:project) {
        FactoryGirl.create(:project,
                           email_add_pusher: true,
                           email_only_broken_builds: false,
                           email_recipients: "jeroen@example.com")
      }
      let(:commit) { FactoryGirl.create(:commit, project: project) }
      let(:build) { FactoryGirl.create(:build, status: :success, commit: commit) }

      before do
        allow(mail).to receive_messages(project: project)
      end

      it do
        should_email("git@example.com")
        should_email("jeroen@example.com")
        mail.execute(build)
      end

      def should_email(email)
        Notify.should_receive(:build_success_email).with(build.id, email)
        Notify.should_not_receive(:build_fail_email).with(build.id, email)
      end
    end

    describe 'successful build and notify only broken builds' do
      let(:project) {
        FactoryGirl.create(:project,
                           email_add_pusher: true,
                           email_only_broken_builds: true,
                           email_recipients: "jeroen@example.com")
      }
      let(:commit) { FactoryGirl.create(:commit, project: project) }
      let(:build) { FactoryGirl.create(:build, status: :success, commit: commit) }

      before do
        allow(mail).to receive_messages(project: project)
      end

      it do
        should_email(commit.git_author_email)
        should_email("jeroen@example.com")
        mail.execute(build) if mail.can_execute?(build)
      end

      def should_email(email)
        Notify.should_not_receive(:build_success_email).with(build.id, email)
        Notify.should_not_receive(:build_fail_email).with(build.id, email)
      end
    end

    describe 'successful build and can test service' do
      let(:project) {
        FactoryGirl.create(:project,
                           email_add_pusher: true,
                           email_only_broken_builds: false,
                           email_recipients: "jeroen@example.com")
      }
      let(:commit) { FactoryGirl.create(:commit, project: project) }
      let(:build) { FactoryGirl.create(:build, status: :success, commit: commit) }

      before do
        allow(mail).to receive_messages(project: project)
        build
      end

      it do
        mail.can_test?.should eq true
      end
    end

    describe 'retried build should not receive email' do
      let(:project) {
        FactoryGirl.create(:project,
                           email_add_pusher: true,
                           email_only_broken_builds: true,
                           email_recipients: "jeroen@example.com")
      }
      let(:commit) { FactoryGirl.create(:commit, project: project) }
      let(:build) { FactoryGirl.create(:build, status: :failed, commit: commit) }

      before do
        allow(mail).to receive_messages(project: project)
      end

      it do
        Build.retry(build)
        should_email(commit.git_author_email)
        should_email("jeroen@example.com")
        mail.execute(build) if mail.can_execute?(build)
      end

      def should_email(email)
        Notify.should_not_receive(:build_success_email).with(build.id, email)
        Notify.should_not_receive(:build_fail_email).with(build.id, email)
      end
    end
  end
end