forked from discourse/discourse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenqueue_digest_emails_spec.rb
220 lines (178 loc) · 7.24 KB
/
enqueue_digest_emails_spec.rb
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# frozen_string_literal: true
RSpec.describe Jobs::EnqueueDigestEmails do
describe "#target_users" do
context "with disabled digests" do
before { SiteSetting.default_email_digest_frequency = 0 }
let!(:user_no_digests) do
Fabricate(:active_user, last_emailed_at: 8.days.ago, last_seen_at: 10.days.ago)
end
it "doesn't return users with email disabled" do
expect(Jobs::EnqueueDigestEmails.new.target_user_ids.include?(user_no_digests.id)).to eq(
false,
)
end
end
context "with unapproved users" do
before { SiteSetting.must_approve_users = true }
let!(:unapproved_user) do
Fabricate(
:active_user,
approved: false,
last_emailed_at: 8.days.ago,
last_seen_at: 10.days.ago,
)
end
it "should enqueue the right digest emails" do
expect(Jobs::EnqueueDigestEmails.new.target_user_ids.include?(unapproved_user.id)).to eq(
false,
)
# As a moderator
unapproved_user.update_column(:moderator, true)
expect(Jobs::EnqueueDigestEmails.new.target_user_ids.include?(unapproved_user.id)).to eq(
true,
)
# As an admin
unapproved_user.update(admin: true, moderator: false)
expect(Jobs::EnqueueDigestEmails.new.target_user_ids.include?(unapproved_user.id)).to eq(
true,
)
# As an approved user
unapproved_user.update(admin: false, moderator: false, approved: true)
expect(Jobs::EnqueueDigestEmails.new.target_user_ids.include?(unapproved_user.id)).to eq(
true,
)
end
end
context "with staged users" do
let!(:staged_user) do
Fabricate(:active_user, staged: true, last_emailed_at: 1.year.ago, last_seen_at: 1.year.ago)
end
it "doesn't return staged users" do
expect(Jobs::EnqueueDigestEmails.new.target_user_ids.include?(staged_user.id)).to eq(false)
end
end
context "when recently emailed" do
let!(:user_emailed_recently) { Fabricate(:active_user, last_emailed_at: 6.days.ago) }
it "doesn't return users who have been emailed recently" do
expect(
Jobs::EnqueueDigestEmails.new.target_user_ids.include?(user_emailed_recently.id),
).to eq(false)
end
end
context "with inactive user" do
let!(:inactive_user) { Fabricate(:user, active: false) }
it "doesn't return users who have been emailed recently" do
expect(Jobs::EnqueueDigestEmails.new.target_user_ids.include?(inactive_user.id)).to eq(
false,
)
end
end
context "with suspended user" do
let!(:suspended_user) do
Fabricate(:user, suspended_till: 1.week.from_now, suspended_at: 1.day.ago)
end
it "doesn't return users who are suspended" do
expect(Jobs::EnqueueDigestEmails.new.target_user_ids.include?(suspended_user.id)).to eq(
false,
)
end
end
context "when visited the site this week" do
let(:user_visited_this_week) { Fabricate(:active_user, last_seen_at: 6.days.ago) }
it "doesn't return users who have been emailed recently" do
user = user_visited_this_week
expect(Jobs::EnqueueDigestEmails.new.target_user_ids.include?(user.id)).to eq(false)
end
end
context "when visited the site a year ago" do
let!(:user_visited_a_year_ago) { Fabricate(:active_user, last_seen_at: 370.days.ago) }
it "doesn't return the user who have not visited the site for more than 365 days" do
expect(
Jobs::EnqueueDigestEmails.new.target_user_ids.include?(user_visited_a_year_ago.id),
).to eq(false)
end
end
context "with regular users" do
let!(:user) do
Fabricate(
:active_user,
last_seen_at: (SiteSetting.suppress_digest_email_after_days - 1).days.ago,
)
end
it "returns the user" do
expect(Jobs::EnqueueDigestEmails.new.target_user_ids).to eq([user.id])
end
end
context "with too many bounces" do
let!(:bounce_user) { Fabricate(:active_user, last_seen_at: 6.month.ago) }
it "doesn't return users with too many bounces" do
bounce_user.user_stat.update(bounce_score: SiteSetting.bounce_score_threshold + 1)
expect(Jobs::EnqueueDigestEmails.new.target_user_ids.include?(bounce_user.id)).to eq(false)
end
end
context "with no primary email" do
let!(:user) { Fabricate(:active_user, last_seen_at: 2.months.ago) }
it "doesn't return users with no primary emails" do
UserEmail.where(user: user).delete_all
expect(Jobs::EnqueueDigestEmails.new.target_user_ids.include?(user.id)).to eq(false)
end
end
end
describe "#execute" do
let(:user) { Fabricate(:user) }
it "limits jobs enqueued per max_digests_enqueued_per_30_mins_per_site" do
user1 = Fabricate(:user, last_seen_at: 2.months.ago, last_emailed_at: 2.months.ago)
user2 = Fabricate(:user, last_seen_at: 2.months.ago, last_emailed_at: 2.months.ago)
user1.user_stat.update(digest_attempted_at: 2.week.ago)
user2.user_stat.update(digest_attempted_at: 3.weeks.ago)
global_setting :max_digests_enqueued_per_30_mins_per_site, 1
expect_enqueued_with(job: :user_email, args: { type: :digest, user_id: user2.id }) do
expect { Jobs::EnqueueDigestEmails.new.execute(nil) }.to change(
Jobs::UserEmail.jobs,
:size,
).by(1)
end
# The job didn't actually run, so fake the user_stat update
user2.user_stat.update(digest_attempted_at: Time.zone.now)
expect_enqueued_with(job: :user_email, args: { type: :digest, user_id: user1.id }) do
expect { Jobs::EnqueueDigestEmails.new.execute(nil) }.to change(
Jobs::UserEmail.jobs,
:size,
).by(1)
end
user1.user_stat.update(digest_attempted_at: Time.zone.now)
expect_not_enqueued_with(job: :user_email) { Jobs::EnqueueDigestEmails.new.execute(nil) }
end
context "when digest emails are enabled" do
before { Jobs::EnqueueDigestEmails.any_instance.expects(:target_user_ids).returns([user.id]) }
it "enqueues the digest email job" do
SiteSetting.disable_digest_emails = false
expect_enqueued_with(job: :user_email, args: { type: :digest, user_id: user.id }) do
Jobs::EnqueueDigestEmails.new.execute({})
end
end
end
context "with private email" do
before do
Jobs::EnqueueDigestEmails.any_instance.expects(:target_user_ids).never
SiteSetting.private_email = true
end
it "doesn't return users with email disabled" do
expect_not_enqueued_with(job: :user_email, args: { type: :digest, user_id: user.id }) do
Jobs::EnqueueDigestEmails.new.execute({})
end
end
end
context "when digest emails are disabled" do
before do
Jobs::EnqueueDigestEmails.any_instance.expects(:target_user_ids).never
SiteSetting.disable_digest_emails = true
end
it "does not enqueue the digest email job" do
expect_not_enqueued_with(job: :user_email, args: { type: :digest, user_id: user.id }) do
Jobs::EnqueueDigestEmails.new.execute({})
end
end
end
end
end