2019-04-30 08:27:42 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-07-28 10:27:38 +08:00
|
|
|
RSpec.describe Jobs::EnqueueDigestEmails do
|
2013-02-06 03:16:51 +08:00
|
|
|
describe "#target_users" do
|
2022-07-28 00:14:14 +08:00
|
|
|
context "with disabled digests" do
|
2017-04-25 03:26:06 +08:00
|
|
|
before { SiteSetting.default_email_digest_frequency = 0 }
|
2015-08-22 02:39:21 +08:00
|
|
|
let!(:user_no_digests) do
|
|
|
|
Fabricate(:active_user, last_emailed_at: 8.days.ago, last_seen_at: 10.days.ago)
|
2023-01-09 19:18:21 +08:00
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
|
|
|
|
it "doesn't return users with email disabled" do
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(Jobs::EnqueueDigestEmails.new.target_user_ids.include?(user_no_digests.id)).to eq(
|
|
|
|
false,
|
|
|
|
)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-28 00:14:14 +08:00
|
|
|
context "with unapproved users" do
|
2016-12-13 09:59:38 +08:00
|
|
|
before { SiteSetting.must_approve_users = true }
|
|
|
|
|
2019-04-17 02:42:47 +08:00
|
|
|
let!(:unapproved_user) do
|
|
|
|
Fabricate(
|
|
|
|
:active_user,
|
|
|
|
approved: false,
|
|
|
|
last_emailed_at: 8.days.ago,
|
|
|
|
last_seen_at: 10.days.ago,
|
|
|
|
)
|
2023-01-09 19:18:21 +08:00
|
|
|
end
|
2019-04-17 02:42:47 +08:00
|
|
|
|
2016-12-13 09:59:38 +08:00
|
|
|
it "should enqueue the right digest emails" do
|
|
|
|
expect(Jobs::EnqueueDigestEmails.new.target_user_ids.include?(unapproved_user.id)).to eq(
|
|
|
|
false,
|
|
|
|
)
|
2013-06-06 23:45:18 +08:00
|
|
|
|
2016-12-13 09:59:38 +08:00
|
|
|
# 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
|
2019-04-29 15:32:25 +08:00
|
|
|
unapproved_user.update(admin: true, moderator: false)
|
2016-12-13 09:59:38 +08:00
|
|
|
expect(Jobs::EnqueueDigestEmails.new.target_user_ids.include?(unapproved_user.id)).to eq(
|
|
|
|
true,
|
|
|
|
)
|
|
|
|
|
|
|
|
# As an approved user
|
2019-04-29 15:32:25 +08:00
|
|
|
unapproved_user.update(admin: false, moderator: false, approved: true)
|
2016-12-13 09:59:38 +08:00
|
|
|
expect(Jobs::EnqueueDigestEmails.new.target_user_ids.include?(unapproved_user.id)).to eq(
|
|
|
|
true,
|
|
|
|
)
|
|
|
|
end
|
2013-06-06 23:45:18 +08:00
|
|
|
end
|
|
|
|
|
2022-07-28 00:14:14 +08:00
|
|
|
context "with staged users" do
|
2015-11-07 02:19:13 +08:00
|
|
|
let!(:staged_user) do
|
|
|
|
Fabricate(:active_user, staged: true, last_emailed_at: 1.year.ago, last_seen_at: 1.year.ago)
|
2023-01-09 19:18:21 +08:00
|
|
|
end
|
2015-11-07 02:19:13 +08:00
|
|
|
|
|
|
|
it "doesn't return staged users" do
|
|
|
|
expect(Jobs::EnqueueDigestEmails.new.target_user_ids.include?(staged_user.id)).to eq(false)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-28 00:14:14 +08:00
|
|
|
context "when recently emailed" do
|
2013-07-12 06:47:06 +08:00
|
|
|
let!(:user_emailed_recently) { Fabricate(:active_user, last_emailed_at: 6.days.ago) }
|
2013-02-06 03:16:51 +08:00
|
|
|
|
|
|
|
it "doesn't return users who have been emailed recently" do
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(
|
|
|
|
Jobs::EnqueueDigestEmails.new.target_user_ids.include?(user_emailed_recently.id),
|
|
|
|
).to eq(false)
|
2013-02-26 00:42:20 +08:00
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2022-07-28 00:14:14 +08:00
|
|
|
context "with inactive user" do
|
2014-08-14 01:30:25 +08:00
|
|
|
let!(:inactive_user) { Fabricate(:user, active: false) }
|
2013-07-12 06:47:06 +08:00
|
|
|
|
|
|
|
it "doesn't return users who have been emailed recently" do
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(Jobs::EnqueueDigestEmails.new.target_user_ids.include?(inactive_user.id)).to eq(
|
|
|
|
false,
|
|
|
|
)
|
2013-07-12 06:47:06 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-28 00:14:14 +08:00
|
|
|
context "with suspended user" do
|
2014-12-30 04:16:08 +08:00
|
|
|
let!(:suspended_user) do
|
|
|
|
Fabricate(:user, suspended_till: 1.week.from_now, suspended_at: 1.day.ago)
|
2023-01-09 19:18:21 +08:00
|
|
|
end
|
2014-12-30 04:16:08 +08:00
|
|
|
|
|
|
|
it "doesn't return users who are suspended" do
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(Jobs::EnqueueDigestEmails.new.target_user_ids.include?(suspended_user.id)).to eq(
|
|
|
|
false,
|
|
|
|
)
|
2014-12-30 04:16:08 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-28 00:14:14 +08:00
|
|
|
context "when visited the site this week" do
|
2013-10-01 15:04:02 +08:00
|
|
|
let(:user_visited_this_week) { Fabricate(:active_user, last_seen_at: 6.days.ago) }
|
2013-02-06 03:16:51 +08:00
|
|
|
|
|
|
|
it "doesn't return users who have been emailed recently" do
|
2013-10-01 15:04:02 +08:00
|
|
|
user = user_visited_this_week
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(Jobs::EnqueueDigestEmails.new.target_user_ids.include?(user.id)).to eq(false)
|
2013-02-26 00:42:20 +08:00
|
|
|
end
|
2015-01-27 00:07:10 +08:00
|
|
|
end
|
|
|
|
|
2022-07-28 00:14:14 +08:00
|
|
|
context "when visited the site a year ago" do
|
2015-01-27 00:07:10 +08:00
|
|
|
let!(:user_visited_a_year_ago) { Fabricate(:active_user, last_seen_at: 370.days.ago) }
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2015-01-27 00:07:10 +08:00
|
|
|
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)
|
2013-10-01 15:04:02 +08:00
|
|
|
end
|
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2022-07-28 00:14:14 +08:00
|
|
|
context "with regular users" do
|
2021-11-23 01:37:32 +08:00
|
|
|
let!(:user) do
|
|
|
|
Fabricate(
|
|
|
|
:active_user,
|
|
|
|
last_seen_at: (SiteSetting.suppress_digest_email_after_days - 1).days.ago,
|
|
|
|
)
|
2023-01-09 19:18:21 +08:00
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
|
|
|
|
it "returns the user" do
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(Jobs::EnqueueDigestEmails.new.target_user_ids).to eq([user.id])
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-28 00:14:14 +08:00
|
|
|
context "with too many bounces" do
|
2017-03-09 02:19:11 +08:00
|
|
|
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
|
|
|
|
|
2022-07-28 00:14:14 +08:00
|
|
|
context "with no primary email" do
|
2020-05-27 23:09:40 +08:00
|
|
|
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
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
describe "#execute" do
|
|
|
|
let(:user) { Fabricate(:user) }
|
|
|
|
|
2020-10-07 14:30:15 +08:00
|
|
|
it "limits jobs enqueued per max_digests_enqueued_per_30_mins_per_site" do
|
2020-10-07 22:30:38 +08:00
|
|
|
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)
|
2020-10-07 14:30:15 +08:00
|
|
|
|
|
|
|
global_setting :max_digests_enqueued_per_30_mins_per_site, 1
|
|
|
|
|
2020-10-07 22:30:38 +08:00
|
|
|
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) }
|
2020-10-07 14:30:15 +08:00
|
|
|
end
|
|
|
|
|
2022-07-28 00:14:14 +08:00
|
|
|
context "when digest emails are enabled" do
|
2015-01-27 12:46:21 +08:00
|
|
|
before { Jobs::EnqueueDigestEmails.any_instance.expects(:target_user_ids).returns([user.id]) }
|
|
|
|
|
|
|
|
it "enqueues the digest email job" do
|
2017-07-07 14:09:14 +08:00
|
|
|
SiteSetting.disable_digest_emails = false
|
2020-07-24 17:16:52 +08:00
|
|
|
|
|
|
|
expect_enqueued_with(job: :user_email, args: { type: :digest, user_id: user.id }) do
|
|
|
|
Jobs::EnqueueDigestEmails.new.execute({})
|
|
|
|
end
|
2015-01-27 12:46:21 +08:00
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2022-07-28 00:14:14 +08:00
|
|
|
context "with private email" do
|
2017-04-25 03:26:06 +08:00
|
|
|
before do
|
|
|
|
Jobs::EnqueueDigestEmails.any_instance.expects(:target_user_ids).never
|
|
|
|
SiteSetting.private_email = true
|
|
|
|
end
|
2020-07-24 17:16:52 +08:00
|
|
|
|
2017-04-25 03:26:06 +08:00
|
|
|
it "doesn't return users with email disabled" do
|
2020-07-24 17:16:52 +08:00
|
|
|
expect_not_enqueued_with(job: :user_email, args: { type: :digest, user_id: user.id }) do
|
|
|
|
Jobs::EnqueueDigestEmails.new.execute({})
|
|
|
|
end
|
2017-04-25 03:26:06 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-28 00:14:14 +08:00
|
|
|
context "when digest emails are disabled" do
|
2015-01-27 12:46:21 +08:00
|
|
|
before do
|
|
|
|
Jobs::EnqueueDigestEmails.any_instance.expects(:target_user_ids).never
|
2017-04-25 03:26:06 +08:00
|
|
|
SiteSetting.disable_digest_emails = true
|
2015-01-27 12:46:21 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "does not enqueue the digest email job" do
|
2020-07-24 17:16:52 +08:00
|
|
|
expect_not_enqueued_with(job: :user_email, args: { type: :digest, user_id: user.id }) do
|
|
|
|
Jobs::EnqueueDigestEmails.new.execute({})
|
|
|
|
end
|
2015-01-27 12:46:21 +08:00
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|