FEATURE: limit daily emails per user to 100 per day via site setting

- controlled via max_emails_per_day_per_user, 0 to disable
- when limit is reached user is warned
This commit is contained in:
Sam
2016-03-23 15:08:34 +11:00
parent 389801f244
commit c095304d6d
11 changed files with 124 additions and 7 deletions

View File

@@ -3,9 +3,34 @@ require "rails_helper"
describe Jobs::NotifyMailingListSubscribers do
context "with mailing list on" do
before { SiteSetting.stubs(:default_email_mailing_list_mode).returns(true) }
before { SiteSetting.default_email_mailing_list_mode = true }
let(:user) { Fabricate(:user) }
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
context "with a valid post" do
let!(:post) { Fabricate(:post, user: user) }

View File

@@ -193,6 +193,17 @@ describe Jobs::UserEmail do
Jobs::UserEmail.new.execute(type: :user_mentioned, user_id: user.id, notification_id: notification.id)
end
it "does not send notification if limit is reached" do
SiteSetting.max_emails_per_day_per_user = 2
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::UserEmail.new.execute(type: :user_mentioned, user_id: user.id, notification_id: notification.id, post_id: post.id)
expect(EmailLog.where(user_id: user.id, skipped: true).count).to eq(1)
end
it "doesn't send the mail if the user is using mailing list mode" do
Email::Sender.any_instance.expects(:send).never
user.user_option.update_column(:mailing_list_mode, true)
@@ -205,7 +216,7 @@ describe Jobs::UserEmail do
# When post does not have a topic
post = Fabricate(:post)
post.topic.destroy
Jobs::UserEmail.new.execute(type: :user_mentioned, user_id: user.id, notification_type: "posted", post_id: post)
Jobs::UserEmail.new.execute(type: :user_mentioned, user_id: user.id, notification_type: "posted", post_id: post.id)
end
it "doesn't send the email if the post has been user deleted" do