summaryrefslogtreecommitdiff
path: root/spec/services/notification_recipients/builder/new_note_spec.rb
blob: f88e8b2dfb0789c771b5008a21514db0443a75c5 (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
# frozen_string_literal: true

require 'spec_helper'

describe NotificationRecipients::Builder::NewNote do
  describe '#notification_recipients' do
    let_it_be(:group)   { create(:group, :public) }
    let_it_be(:project) { create(:project, :public, group: group) }
    let_it_be(:issue) { create(:issue, project: project) }

    let_it_be(:other_user)              { create(:user) }
    let_it_be(:participant)             { create(:user) }
    let_it_be(:non_member_participant)  { create(:user) }
    let_it_be(:group_watcher)           { create(:user) }
    let_it_be(:project_watcher)         { create(:user) }
    let_it_be(:guest_project_watcher)   { create(:user) }
    let_it_be(:subscriber)              { create(:user) }
    let_it_be(:unsubscribed_user)       { create(:user) }
    let_it_be(:non_member_subscriber)   { create(:user) }

    let_it_be(:notification_setting_project_w) { create(:notification_setting, source: project, user: project_watcher, level: 2) }
    let_it_be(:notification_setting_guest_w) { create(:notification_setting, source: project, user: guest_project_watcher, level: 2) }
    let_it_be(:notification_setting_group_w) { create(:notification_setting, source: group, user: group_watcher, level: 2) }
    let_it_be(:subscriptions) do
      [
        create(:subscription, project: project, user: subscriber, subscribable: issue, subscribed: true),
        create(:subscription, project: project, user: unsubscribed_user, subscribable: issue, subscribed: false),
        create(:subscription, project: project, user: non_member_subscriber, subscribable: issue, subscribed: true)
      ]
    end

    subject { described_class.new(note) }

    before do
      project.add_developer(participant)
      project.add_developer(project_watcher)
      project.add_guest(guest_project_watcher)
      project.add_developer(subscriber)
      group.add_developer(group_watcher)

      expect(issue).to receive(:participants).and_return([participant, non_member_participant])
    end

    context 'for public notes' do
      let_it_be(:note) { create(:note, noteable: issue, project: project) }

      it 'adds all participants, watchers and subscribers' do
        expect(subject.notification_recipients.map(&:user)).to contain_exactly(
          participant, non_member_participant, project_watcher, group_watcher, guest_project_watcher, subscriber, non_member_subscriber
        )
      end
    end

    context 'for confidential notes' do
      let_it_be(:note) { create(:note, :confidential, noteable: issue, project: project) }

      it 'adds all participants, watchers and subscribers that are project memebrs' do
        expect(subject.notification_recipients.map(&:user)).to contain_exactly(
          participant, project_watcher, group_watcher, subscriber
        )
      end
    end
  end
end