summaryrefslogtreecommitdiff
path: root/spec/support/shared_examples/services/notification_service_shared_examples.rb
blob: 43fe6789145ec5d35bf04c9909e9168dfd439a64 (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
# frozen_string_literal: true

# Note that we actually update the attribute on the target_project/group, rather than
# using `allow`.  This is because there are some specs where, based on how the notification
# is done, using an `allow` doesn't change the correct object.
RSpec.shared_examples 'project emails are disabled' do |check_delivery_jobs_queue: false|
  let(:target_project) { notification_target.is_a?(Project) ? notification_target : notification_target.project }

  before do
    reset_delivered_emails!
    target_project.clear_memoization(:emails_disabled)
  end

  it 'sends no emails with project emails disabled' do
    target_project.update_attribute(:emails_disabled, true)

    notification_trigger

    if check_delivery_jobs_queue
      # Only check enqueud jobs, not delivered emails
      expect_no_delivery_jobs
    else
      # Deprecated: Check actual delivered emails
      should_not_email_anyone
    end
  end

  it 'sends emails to someone' do
    target_project.update_attribute(:emails_disabled, false)

    notification_trigger

    if check_delivery_jobs_queue
      # Only check enqueud jobs, not delivered emails
      expect_any_delivery_jobs
    else
      # Deprecated: Check actual delivered emails
      should_email_anyone
    end
  end
end

RSpec.shared_examples 'group emails are disabled' do
  let(:target_group) { notification_target.is_a?(Group) ? notification_target : notification_target.project.group }

  before do
    reset_delivered_emails!
    target_group.clear_memoization(:emails_disabled)
  end

  it 'sends no emails with group emails disabled' do
    target_group.update_attribute(:emails_disabled, true)

    notification_trigger

    should_not_email_anyone
  end

  it 'sends emails to someone' do
    target_group.update_attribute(:emails_disabled, false)

    notification_trigger

    should_email_anyone
  end
end

RSpec.shared_examples 'sends notification only to a maximum of ten, most recently active group owners' do
  let(:owners) { create_list(:user, 12, :with_sign_ins) }

  before do
    owners.each do |owner|
      group.add_owner(owner)
    end

    reset_delivered_emails!
  end

  context 'limit notification emails' do
    it 'sends notification only to a maximum of ten, most recently active group owners' do
      ten_most_recently_active_group_owners = owners.sort_by(&:last_sign_in_at).last(10)

      notification_trigger

      should_only_email(*ten_most_recently_active_group_owners)
    end
  end
end

RSpec.shared_examples 'sends notification only to a maximum of ten, most recently active project maintainers' do
  let(:maintainers) { create_list(:user, 12, :with_sign_ins) }

  before do
    maintainers.each do |maintainer|
      project.add_maintainer(maintainer)
    end

    reset_delivered_emails!
  end

  context 'limit notification emails' do
    it 'sends notification only to a maximum of ten, most recently active project maintainers' do
      ten_most_recently_active_project_maintainers = maintainers.sort_by(&:last_sign_in_at).last(10)

      notification_trigger

      should_only_email(*ten_most_recently_active_project_maintainers)
    end
  end
end