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
|
2013-08-27 20:44:13 -04:00
|
|
|
Jobs::EnqueueDigestEmails.new.target_user_ids.include?(user_no_digests.id).should be_false
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-06-06 11:45:18 -04:00
|
|
|
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) }
|
2013-06-06 11:45:18 -04:00
|
|
|
When { SiteSetting.stubs(:must_approve_users?).returns(true) }
|
2013-08-27 20:44:13 -04:00
|
|
|
Then { expect(Jobs::EnqueueDigestEmails.new.target_user_ids.include?(unapproved_user.id)).to eq(false) }
|
2013-06-06 11:45:18 -04:00
|
|
|
|
|
|
|
# As a moderator
|
|
|
|
And { unapproved_user.update_column(:moderator, true) }
|
2013-08-27 20:44:13 -04:00
|
|
|
And { expect(Jobs::EnqueueDigestEmails.new.target_user_ids.include?(unapproved_user.id)).to eq(true) }
|
2013-06-06 11:45:18 -04:00
|
|
|
|
|
|
|
# As an admin
|
|
|
|
And { unapproved_user.update_attributes(admin: true, moderator: false) }
|
2013-08-27 20:44:13 -04:00
|
|
|
And { expect(Jobs::EnqueueDigestEmails.new.target_user_ids.include?(unapproved_user.id)).to eq(true) }
|
2013-06-06 11:45:18 -04:00
|
|
|
|
|
|
|
# As an approved user
|
|
|
|
And { unapproved_user.update_attributes(admin: false, moderator: false, approved: true ) }
|
2013-08-27 20:44:13 -04:00
|
|
|
And { expect(Jobs::EnqueueDigestEmails.new.target_user_ids.include?(unapproved_user.id)).to eq(true) }
|
2013-06-06 11:45:18 -04:00
|
|
|
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
|
2013-08-27 20:44:13 -04:00
|
|
|
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
|
2013-08-27 20:44:13 -04:00
|
|
|
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
|
2013-08-27 20:44:13 -04:00
|
|
|
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
|
2013-08-27 20:44:13 -04:00
|
|
|
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
|
2013-08-27 20:44:13 -04:00
|
|
|
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
|
|
|
|
|