2015-10-11 05:41:23 -04:00
|
|
|
require "rails_helper"
|
2014-11-24 11:40:21 -05:00
|
|
|
|
|
|
|
describe Jobs::NotifyMailingListSubscribers do
|
|
|
|
|
|
|
|
context "with mailing list on" do
|
2016-03-23 00:08:34 -04:00
|
|
|
before { SiteSetting.default_email_mailing_list_mode = true }
|
2016-02-03 13:27:58 -05:00
|
|
|
let(:user) { Fabricate(:user) }
|
2014-11-24 11:40:21 -05:00
|
|
|
|
2016-03-23 00:08:34 -04:00
|
|
|
context "SiteSetting.max_emails_per_day_per_user" do
|
|
|
|
|
|
|
|
it 'stops sending mail once limit is reached' do
|
|
|
|
SiteSetting.max_emails_per_day_per_user = 2
|
|
|
|
post = Fabricate(:post)
|
|
|
|
|
|
|
|
user.email_logs.create(email_type: 'blah', to_address: user.email, user_id: user.id)
|
|
|
|
user.email_logs.create(email_type: 'blah', to_address: user.email, user_id: user.id)
|
|
|
|
|
|
|
|
Jobs::NotifyMailingListSubscribers.new.execute(post_id: post.id)
|
|
|
|
expect(EmailLog.where(user_id: user.id, skipped: true).count).to eq(1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "totally skipped if mailing list mode disabled" do
|
|
|
|
|
|
|
|
it "sends no email to the user" do
|
|
|
|
SiteSetting.disable_mailing_list_mode = true
|
|
|
|
|
|
|
|
post = Fabricate(:post)
|
|
|
|
Jobs::NotifyMailingListSubscribers.new.execute(post_id: post.id)
|
|
|
|
expect(EmailLog.count).to eq(0)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-02-03 13:27:58 -05:00
|
|
|
context "with a valid post" do
|
|
|
|
let!(:post) { Fabricate(:post, user: user) }
|
2015-08-21 14:39:21 -04:00
|
|
|
|
2016-02-03 13:27:58 -05:00
|
|
|
it "sends the email to the user" do
|
|
|
|
UserNotifications.expects(:mailing_list_notify).with(user, post).once
|
|
|
|
Jobs::NotifyMailingListSubscribers.new.execute(post_id: post.id)
|
2014-11-24 11:40:21 -05:00
|
|
|
end
|
2016-02-03 13:27:58 -05:00
|
|
|
end
|
2014-11-24 11:40:21 -05:00
|
|
|
|
2016-02-03 13:27:58 -05:00
|
|
|
context "with a deleted post" do
|
|
|
|
let!(:post) { Fabricate(:post, user: user, deleted_at: Time.now) }
|
2014-11-24 11:40:21 -05:00
|
|
|
|
2016-02-03 13:27:58 -05:00
|
|
|
it "doesn't send the email to the user" do
|
|
|
|
UserNotifications.expects(:mailing_list_notify).with(user, post).never
|
|
|
|
Jobs::NotifyMailingListSubscribers.new.execute(post_id: post.id)
|
2014-11-24 11:40:21 -05:00
|
|
|
end
|
2016-02-03 13:27:58 -05:00
|
|
|
end
|
2014-11-24 11:40:21 -05:00
|
|
|
|
2016-02-03 13:27:58 -05:00
|
|
|
context "with a user_deleted post" do
|
|
|
|
let!(:post) { Fabricate(:post, user: user, user_deleted: true) }
|
2014-11-24 11:40:21 -05:00
|
|
|
|
2016-02-03 13:27:58 -05:00
|
|
|
it "doesn't send the email to the user" do
|
|
|
|
UserNotifications.expects(:mailing_list_notify).with(user, post).never
|
|
|
|
Jobs::NotifyMailingListSubscribers.new.execute(post_id: post.id)
|
2014-11-24 11:40:21 -05:00
|
|
|
end
|
2016-02-03 13:27:58 -05:00
|
|
|
end
|
2014-11-24 11:40:21 -05:00
|
|
|
|
2016-02-03 13:27:58 -05:00
|
|
|
context "with a deleted topic" do
|
|
|
|
let!(:post) { Fabricate(:post, user: user) }
|
2015-08-21 14:39:21 -04:00
|
|
|
|
2016-02-03 13:27:58 -05:00
|
|
|
before do
|
|
|
|
post.topic.update_column(:deleted_at, Time.now)
|
2014-11-24 11:40:21 -05:00
|
|
|
end
|
|
|
|
|
2016-02-03 13:27:58 -05:00
|
|
|
it "doesn't send the email to the user" do
|
|
|
|
UserNotifications.expects(:mailing_list_notify).with(user, post).never
|
|
|
|
Jobs::NotifyMailingListSubscribers.new.execute(post_id: post.id)
|
|
|
|
end
|
2015-08-21 14:39:21 -04:00
|
|
|
end
|
|
|
|
|
2016-02-03 13:27:58 -05:00
|
|
|
context "to an anonymous user" do
|
2015-08-21 14:39:21 -04:00
|
|
|
let(:user) { Fabricate(:anonymous) }
|
|
|
|
let!(:post) { Fabricate(:post, user: user) }
|
|
|
|
|
2014-11-24 11:40:21 -05:00
|
|
|
it "doesn't send the email to the user" do
|
|
|
|
UserNotifications.expects(:mailing_list_notify).with(user, post).never
|
|
|
|
Jobs::NotifyMailingListSubscribers.new.execute(post_id: post.id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
context "with mailing list off" do
|
2015-08-21 14:39:21 -04:00
|
|
|
before { SiteSetting.stubs(:default_email_mailing_list_mode).returns(false) }
|
|
|
|
|
|
|
|
let(:user) { Fabricate(:user) }
|
2014-11-24 11:40:21 -05:00
|
|
|
let!(:post) { Fabricate(:post, user: user) }
|
|
|
|
|
|
|
|
it "doesn't send the email to the user" do
|
|
|
|
UserNotifications.expects(:mailing_list_notify).never
|
|
|
|
Jobs::NotifyMailingListSubscribers.new.execute(post_id: post.id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|