discourse/spec/jobs/enqueue_digest_emails_spec.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

150 lines
4.9 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
RSpec.describe Jobs::EnqueueDigestEmails do
let(:job) { described_class.new }
2013-02-06 03:16:51 +08:00
describe "#execute" do
fab!(:user)
2013-02-06 03:16:51 +08:00
context "when all emails are disabled" do
before { SiteSetting.disable_emails = "yes" }
2016-12-13 09:59:38 +08:00
it "does not enqueue the digest email job" do
Jobs::EnqueueDigestEmails.any_instance.expects(:target_user_ids).never
expect_not_enqueued_with(job: :user_email, args: { type: :digest, user_id: user.id }) do
job.execute({})
end
2016-12-13 09:59:38 +08:00
end
end
context "when digest emails are disabled" do
before { SiteSetting.disable_digest_emails = true }
2015-11-07 02:19:13 +08:00
it "does not enqueue the digest email job" do
Jobs::EnqueueDigestEmails.any_instance.expects(:target_user_ids).never
2013-02-06 03:16:51 +08:00
expect_not_enqueued_with(job: :user_email, args: { type: :digest, user_id: user.id }) do
job.execute({})
end
2013-02-26 00:42:20 +08:00
end
2013-02-06 03:16:51 +08:00
end
context "when emails are private" do
before { SiteSetting.private_email = true }
2013-07-12 06:47:06 +08:00
it "does not enqueue the digest email job" do
Jobs::EnqueueDigestEmails.any_instance.expects(:target_user_ids).never
expect_not_enqueued_with(job: :user_email, args: { type: :digest, user_id: user.id }) do
job.execute({})
end
end
end
end
describe "#target_user_ids" do
fab!(:user) { Fabricate(:active_user, last_seen_at: 10.days.ago) }
fab!(:bot) { Fabricate(:bot, last_seen_at: 10.days.ago) }
fab!(:anon) { Fabricate(:anonymous, last_seen_at: 10.days.ago) }
2013-02-06 03:16:51 +08:00
it "never returns bots" do
expect(job.target_user_ids).not_to include(bot.id)
end
it "never returns anonymous users" do
expect(job.target_user_ids).not_to include(anon.id)
end
2013-02-06 03:16:51 +08:00
it "never returns inactive users" do
user.update!(active: false)
expect(job.target_user_ids).not_to include(user.id)
2013-02-06 03:16:51 +08:00
end
it "never returns staged users" do
user.update!(staged: true)
expect(job.target_user_ids).not_to include(user.id)
end
it "never returns suspended users" do
user.update!(suspended_till: 1.day.from_now)
expect(job.target_user_ids).not_to include(user.id)
end
2013-02-06 03:16:51 +08:00
it "never returns users who have not opted in to digest emails" do
user.user_option.update!(email_digests: false)
expect(job.target_user_ids).not_to include(user.id)
end
it "never returns users who have set a digest frequency to 'never" do
user.user_option.update!(digest_after_minutes: 0)
expect(job.target_user_ids).not_to include(user.id)
end
it "never returns users who have not set a digest frequency and it's disabled globally" do
SiteSetting.default_email_digest_frequency = 0
user.user_option.update!(digest_after_minutes: nil)
expect(job.target_user_ids).not_to include(user.id)
end
it "never returns users who have a bounce score above the threshold" do
user.user_stat.update!(bounce_score: SiteSetting.bounce_score_threshold + 1)
expect(job.target_user_ids).not_to include(user.id)
end
it "never returns users who doesn't have a primary email" do
user.user_emails.update_all(primary: false)
expect(job.target_user_ids).not_to include(user.id)
end
it "never returns users who have received a digest email too recently" do
user.user_stat.update!(digest_attempted_at: 1.minute.ago)
expect(job.target_user_ids).not_to include(user.id)
end
it "never returns users who have been seen too recently" do
user.update!(last_seen_at: 1.minute.ago)
expect(job.target_user_ids).not_to include(user.id)
end
it "never returns users who have been seen too long ago" do
user.update!(last_seen_at: (SiteSetting.suppress_digest_email_after_days + 1).days.ago)
expect(job.target_user_ids).not_to include(user.id)
end
context "when the site requires user approval" do
before { SiteSetting.must_approve_users = true }
2015-01-27 12:46:21 +08:00
it "never returns users who have not been approved" do
user.update!(approved: false)
expect(job.target_user_ids).not_to include(user.id)
end
2020-07-24 17:16:52 +08:00
it "returns users who have been approved" do
user.update!(approved: true)
expect(job.target_user_ids).to include(user.id)
2015-01-27 12:46:21 +08:00
end
2013-02-06 03:16:51 +08:00
it "always returns moderators" do
user.update!(approved: false, moderator: true)
expect(job.target_user_ids).to include(user.id)
end
2020-07-24 17:16:52 +08:00
it "always returns admins" do
user.update!(approved: false, admin: true)
expect(job.target_user_ids).to include(user.id)
end
end
it "limits the number of users returned" do
global_setting :max_digests_enqueued_per_30_mins_per_site, 1
2.times { Fabricate(:active_user, last_seen_at: 10.days.ago) }
expect(job.target_user_ids.size).to eq(1)
end
2015-01-27 12:46:21 +08:00
it "returns the user ids of users who want to receive digest emails" do
expect(job.target_user_ids).to eq([user.id])
2013-02-06 03:16:51 +08:00
end
end
end