discourse/spec/components/jobs/enqueue_digest_emails_spec.rb

89 lines
2.8 KiB
Ruby
Raw Normal View History

2013-02-05 14:16:51 -05:00
require 'spec_helper'
require 'jobs'
describe Jobs::EnqueueDigestEmails do
describe '#target_users' do
2013-02-25 11:42:20 -05:00
2013-02-05 14:16:51 -05:00
context 'disabled digests' do
2013-07-11 18:47:06 -04:00
let!(:user_no_digests) { Fabricate(:active_user, email_digests: false, last_emailed_at: 8.days.ago, last_seen_at: 10.days.ago) }
2013-02-05 14:16:51 -05:00
it "doesn't return users with email disabled" do
Jobs::EnqueueDigestEmails.new.target_user_ids.include?(user_no_digests.id).should be_false
2013-02-05 14:16:51 -05:00
end
end
context 'unapproved users' do
2013-07-11 18:47:06 -04:00
Given!(:unapproved_user) { Fabricate(:active_user, approved: false, last_emailed_at: 8.days.ago, last_seen_at: 10.days.ago) }
When { SiteSetting.stubs(:must_approve_users?).returns(true) }
Then { expect(Jobs::EnqueueDigestEmails.new.target_user_ids.include?(unapproved_user.id)).to eq(false) }
# As a moderator
And { unapproved_user.update_column(:moderator, true) }
And { expect(Jobs::EnqueueDigestEmails.new.target_user_ids.include?(unapproved_user.id)).to eq(true) }
# As an admin
And { unapproved_user.update_attributes(admin: true, moderator: false) }
And { expect(Jobs::EnqueueDigestEmails.new.target_user_ids.include?(unapproved_user.id)).to eq(true) }
# As an approved user
And { unapproved_user.update_attributes(admin: false, moderator: false, approved: true ) }
And { expect(Jobs::EnqueueDigestEmails.new.target_user_ids.include?(unapproved_user.id)).to eq(true) }
end
2013-02-05 14:16:51 -05:00
context 'recently emailed' do
2013-07-11 18:47:06 -04:00
let!(:user_emailed_recently) { Fabricate(:active_user, last_emailed_at: 6.days.ago) }
2013-02-05 14:16:51 -05:00
it "doesn't return users who have been emailed recently" do
Jobs::EnqueueDigestEmails.new.target_user_ids.include?(user_emailed_recently.id).should be_false
2013-02-25 11:42:20 -05:00
end
2013-02-05 14:16:51 -05:00
end
2013-07-11 18:47:06 -04:00
context "inactive user" do
let!(:inactive_user) { Fabricate(:user) }
it "doesn't return users who have been emailed recently" do
Jobs::EnqueueDigestEmails.new.target_user_ids.include?(inactive_user.id).should be_false
2013-07-11 18:47:06 -04:00
end
end
2013-02-05 14:16:51 -05:00
context 'visited the site today' do
2013-07-11 18:47:06 -04:00
let!(:user_visited_today) { Fabricate(:active_user, last_seen_at: 6.days.ago) }
2013-02-05 14:16:51 -05:00
it "doesn't return users who have been emailed recently" do
Jobs::EnqueueDigestEmails.new.target_user_ids.include?(user_visited_today.id).should be_false
2013-02-25 11:42:20 -05:00
end
2013-02-05 14:16:51 -05:00
end
context 'regular users' do
2013-07-11 18:47:06 -04:00
let!(:user) { Fabricate(:active_user) }
2013-02-05 14:16:51 -05:00
it "returns the user" do
Jobs::EnqueueDigestEmails.new.target_user_ids.should == [user.id]
2013-02-05 14:16:51 -05:00
end
end
end
describe '#execute' do
let(:user) { Fabricate(:user) }
before do
Jobs::EnqueueDigestEmails.any_instance.expects(:target_user_ids).returns([user.id])
2013-02-05 14:16:51 -05:00
end
it "enqueues the digest email job" do
Jobs.expects(:enqueue).with(:user_email, type: :digest, user_id: user.id)
Jobs::EnqueueDigestEmails.new.execute({})
end
end
end