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

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

  let(:project) { create(:project) }
L
Lin Jen-Shin 已提交
13
  let(:user) { create(:user) }
L
Lin Jen-Shin 已提交
14 15
  let(:pusher) { user }
  let(:watcher) { pusher }
L
Lin Jen-Shin 已提交
16 17

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

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

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

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

38 39 40 41 42
          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 已提交
43

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

49
        it_behaves_like 'sending emails'
L
Lin Jen-Shin 已提交
50

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

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

            it_behaves_like 'sending emails'
L
Lin Jen-Shin 已提交
62 63
          end

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

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

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

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

          it_behaves_like 'sending emails'
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

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

            context 'with failed pipeline notification on' do
              before do
                watcher.global_notification_setting.
                  update(level: 'custom', failed_pipeline: true)
              end

              it_behaves_like 'sending emails'
            end

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

              before do
                watcher.global_notification_setting.
                  update(level: 'custom', failed_pipeline: false)
              end

              it_behaves_like 'sending emails'
            end
          end
L
Lin Jen-Shin 已提交
106 107
        end
      end
L
Lin Jen-Shin 已提交
108 109
    end

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

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

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

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

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