summaryrefslogtreecommitdiff
path: root/spec/workers/pipeline_notification_worker_spec.rb
blob: 5a7ce2e08c482fc9be21530a0c7fb68ed6bbdc02 (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
require 'spec_helper'

describe PipelineNotificationWorker do
  include EmailHelpers

  let(:pipeline) do
    create(:ci_pipeline,
           project: project,
           sha: project.commit('master').sha,
           user: pusher,
           status: status)
  end

  let(:project) { create(:project, :repository, public_builds: false) }
  let(:user) { create(:user) }
  let(:pusher) { user }
  let(:watcher) { pusher }

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

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

      shared_examples 'sending emails' do
        it 'sends emails' do
          perform_enqueued_jobs do
            subject.perform(pipeline.id)
          end

          emails = ActionMailer::Base.deliveries
          actual = emails.flat_map(&:bcc).sort
          expected_receivers = receivers.map(&:email).uniq.sort

          expect(actual).to eq(expected_receivers)
          expect(emails.size).to eq(1)
          expect(emails.last.subject).to include(email_subject)
        end
      end

      context 'with success pipeline' do
        let(:status) { 'success' }
        let(:email_subject) { "Pipeline ##{pipeline.id} has succeeded" }
        let(:receivers) { [pusher, watcher] }

        it_behaves_like 'sending emails'

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

          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'
          end

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

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

            it_behaves_like 'sending emails'
          end
        end

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

          it_behaves_like 'sending emails'

          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
        end
      end
    end

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

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

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

        perform_enqueued_jobs do
          subject.perform(pipeline.id)
        end
      end

      it 'does not send emails' do
        should_only_email(pusher, kind: :bcc)
      end
    end
  end
end