BigW Consortium Gitlab

pipeline_notification_worker_spec.rb 3.66 KB
Newer Older
1 2
require 'spec_helper'

3
describe PipelineNotificationWorker do
4 5
  include EmailHelpers

6
  let(:pipeline) do
7 8 9
    create(:ci_pipeline,
           project: project,
           sha: project.commit('master').sha,
10
           user: pusher,
11
           status: status)
12 13
  end

14
  let(:project) { create(:project, public_builds: false) }
15
  let(:user) { create(:user) }
16 17
  let(:pusher) { user }
  let(:watcher) { pusher }
18 19

  describe '#execute' do
20 21
    before do
      reset_delivered_emails!
22
      pipeline.project.team << [pusher, Gitlab::Access::DEVELOPER]
23 24
    end

25 26 27 28
    context 'when watcher has developer access' do
      before do
        pipeline.project.team << [watcher, Gitlab::Access::DEVELOPER]
      end
29

30 31 32 33 34
      shared_examples 'sending emails' do
        it 'sends emails' do
          perform_enqueued_jobs do
            subject.perform(pipeline.id)
          end
35

36 37 38
          emails = ActionMailer::Base.deliveries
          actual = emails.flat_map(&:bcc).sort
          expected_receivers = receivers.map(&:email).uniq.sort
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
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] }
50

51
        it_behaves_like 'sending emails'
52

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

57 58 59 60 61 62 63
          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'
64 65
          end

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

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

74
            it_behaves_like 'sending emails'
75
          end
76 77 78 79 80
        end

        context 'with failed pipeline' do
          let(:status) { 'failed' }
          let(:email_subject) { "Pipeline ##{pipeline.id} has failed" }
81 82

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

          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
108 109
        end
      end
110 111
    end

112
    context 'when watcher has no read_build access' do
113
      let(:status) { 'failed' }
114
      let(:email_subject) { "Pipeline ##{pipeline.id} has failed" }
115
      let(:watcher) { create(:user) }
116

117 118
      before do
        pipeline.project.team << [watcher, Gitlab::Access::GUEST]
119

120 121
        watcher.global_notification_setting.
          update(level: 'custom', failed_pipeline: true)
122

123 124
        perform_enqueued_jobs do
          subject.perform(pipeline.id)
125
        end
126
      end
127

128 129
      it 'does not send emails' do
        should_only_email(pusher, kind: :bcc)
130
      end
131 132 133
    end
  end
end