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