pipeline_notification_worker_spec.rb 3.7 KB
Newer Older
L
Lin Jen-Shin 已提交
1 2
require 'spec_helper'

3
describe PipelineNotificationWorker do
4 5
  include EmailHelpers

L
Lin Jen-Shin 已提交
6
  let(:pipeline) do
L
Lin Jen-Shin 已提交
7 8 9
    create(:ci_pipeline,
           project: project,
           sha: project.commit('master').sha,
10
           user: pusher,
L
Lin Jen-Shin 已提交
11
           status: status)
L
Lin Jen-Shin 已提交
12 13
  end

Z
Z.J. van de Weg 已提交
14
  let(:project) { create(:project, public_builds: false) }
L
Lin Jen-Shin 已提交
15
  let(:user) { create(:user) }
L
Lin Jen-Shin 已提交
16 17
  let(:pusher) { user }
  let(:watcher) { pusher }
L
Lin Jen-Shin 已提交
18 19

  describe '#execute' do
L
Lin Jen-Shin 已提交
20 21
    before do
      reset_delivered_emails!
22
      pipeline.project.team << [pusher, Gitlab::Access::DEVELOPER]
L
Lin Jen-Shin 已提交
23 24
    end

25 26 27 28
    context 'when watcher has developer access' do
      before do
        pipeline.project.team << [watcher, Gitlab::Access::DEVELOPER]
      end
L
Lin Jen-Shin 已提交
29

30 31 32 33 34
      shared_examples 'sending emails' do
        it 'sends emails' do
          perform_enqueued_jobs do
            subject.perform(pipeline.id)
          end
L
Lin Jen-Shin 已提交
35

36 37 38
          emails = ActionMailer::Base.deliveries
          actual = emails.flat_map(&:bcc).sort
          expected_receivers = receivers.map(&:email).uniq.sort
L
Lin Jen-Shin 已提交
39

40 41 42 43 44
          expect(actual).to eq(expected_receivers)
          expect(emails.size).to eq(1)
          expect(emails.last.subject).to include(email_subject)
        end
      end
L
Lin Jen-Shin 已提交
45

46 47 48 49
      context 'with success pipeline' do
        let(:status) { 'success' }
        let(:email_subject) { "Pipeline ##{pipeline.id} has succeeded" }
        let(:receivers) { [pusher, watcher] }
L
Lin Jen-Shin 已提交
50

51
        it_behaves_like 'sending emails'
L
Lin Jen-Shin 已提交
52

53 54
        context 'with pipeline from someone else' do
          let(:pusher) { create(:user) }
L
Lin Jen-Shin 已提交
55 56
          let(:watcher) { user }

57 58
          context 'with success pipeline notification on' do
            before do
59 60
              watcher.global_notification_setting
                .update(level: 'custom', success_pipeline: true)
61 62 63
            end

            it_behaves_like 'sending emails'
L
Lin Jen-Shin 已提交
64 65
          end

66 67 68 69
          context 'with success pipeline notification off' do
            let(:receivers) { [pusher] }

            before do
70 71
              watcher.global_notification_setting
                .update(level: 'custom', success_pipeline: false)
72
            end
L
Lin Jen-Shin 已提交
73

74
            it_behaves_like 'sending emails'
L
Lin Jen-Shin 已提交
75
          end
76 77 78 79 80
        end

        context 'with failed pipeline' do
          let(:status) { 'failed' }
          let(:email_subject) { "Pipeline ##{pipeline.id} has failed" }
L
Lin Jen-Shin 已提交
81 82

          it_behaves_like 'sending emails'
83 84 85 86 87 88 89

          context 'with pipeline from someone else' do
            let(:pusher) { create(:user) }
            let(:watcher) { user }

            context 'with failed pipeline notification on' do
              before do
90 91
                watcher.global_notification_setting
                  .update(level: 'custom', failed_pipeline: true)
92 93 94 95 96 97 98 99 100
              end

              it_behaves_like 'sending emails'
            end

            context 'with failed pipeline notification off' do
              let(:receivers) { [pusher] }

              before do
101 102
                watcher.global_notification_setting
                  .update(level: 'custom', failed_pipeline: false)
103 104 105 106 107
              end

              it_behaves_like 'sending emails'
            end
          end
L
Lin Jen-Shin 已提交
108 109
        end
      end
L
Lin Jen-Shin 已提交
110 111
    end

112
    context 'when watcher has no read_build access' do
L
Lin Jen-Shin 已提交
113
      let(:status) { 'failed' }
L
Lin Jen-Shin 已提交
114
      let(:email_subject) { "Pipeline ##{pipeline.id} has failed" }
115
      let(:watcher) { create(:user) }
L
Lin Jen-Shin 已提交
116

117 118
      before do
        pipeline.project.team << [watcher, Gitlab::Access::GUEST]
L
Lin Jen-Shin 已提交
119

120 121
        watcher.global_notification_setting
          .update(level: 'custom', failed_pipeline: true)
L
Lin Jen-Shin 已提交
122

123 124
        perform_enqueued_jobs do
          subject.perform(pipeline.id)
L
Lin Jen-Shin 已提交
125
        end
126
      end
L
Lin Jen-Shin 已提交
127

128 129
      it 'does not send emails' do
        should_only_email(pusher, kind: :bcc)
L
Lin Jen-Shin 已提交
130
      end
L
Lin Jen-Shin 已提交
131 132 133
    end
  end
end