FIX: don't apply max emails per day per user to forgot password

This commit is contained in:
Arpit Jalan 2017-05-03 13:33:43 +05:30
parent f5f4c36795
commit cdce060a38
4 changed files with 27 additions and 14 deletions

View File

@ -121,7 +121,7 @@ module Jobs
email_args[:email_token] = email_token if email_token.present? email_args[:email_token] = email_token if email_token.present?
email_args[:new_email] = user.email if type.to_s == "notify_old_email" email_args[:new_email] = user.email if type.to_s == "notify_old_email"
if EmailLog.reached_max_emails?(user) if EmailLog.reached_max_emails?(user, type.to_s)
return skip_message(I18n.t('email_log.exceeded_emails_limit')) return skip_message(I18n.t('email_log.exceeded_emails_limit'))
end end

View File

@ -28,12 +28,12 @@ class EmailLog < ActiveRecord::Base
end end
end end
def self.reached_max_emails?(user) def self.reached_max_emails?(user, email_type=nil)
return false if SiteSetting.max_emails_per_day_per_user == 0 return false if SiteSetting.max_emails_per_day_per_user == 0 || email_type == 'forgot_password'
count = sent.where('created_at > ?', 1.day.ago) count = sent.where('created_at > ?', 1.day.ago)
.where(user_id: user.id) .where(user_id: user.id)
.count .count
count >= SiteSetting.max_emails_per_day_per_user count >= SiteSetting.max_emails_per_day_per_user
end end

View File

@ -222,15 +222,22 @@ describe Jobs::UserEmail do
Jobs::UserEmail.new.execute(type: :user_mentioned, user_id: user.id, notification_id: notification.id) Jobs::UserEmail.new.execute(type: :user_mentioned, user_id: user.id, notification_id: notification.id)
end end
it "does not send notification if limit is reached" do context 'max_emails_per_day_per_user limit is reached' do
SiteSetting.max_emails_per_day_per_user = 2 before 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)
end
user.email_logs.create(email_type: 'blah', to_address: user.email, user_id: user.id) it "does not send notification if limit is reached" do
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
Jobs::UserEmail.new.execute(type: :user_mentioned, user_id: user.id, notification_id: notification.id, post_id: post.id) it "sends forgot password email" do
Jobs::UserEmail.new.execute(type: :forgot_password, 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) expect(EmailLog.where(user_id: user.id, skipped: true).count).to eq(0)
end
end end
it "does not send notification if bounce threshold is reached" do it "does not send notification if bounce threshold is reached" do

View File

@ -53,18 +53,24 @@ describe EmailLog do
end end
describe '#reached_max_emails?' do describe '#reached_max_emails?' do
it "tracks when max emails are reached" do before do
SiteSetting.max_emails_per_day_per_user = 2 SiteSetting.max_emails_per_day_per_user = 2
user.email_logs.create(email_type: 'blah', to_address: user.email, user_id: user.id, skipped: true) user.email_logs.create(email_type: 'blah', to_address: user.email, user_id: user.id, skipped: true)
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)
user.email_logs.create(email_type: 'blah', to_address: user.email, user_id: user.id, created_at: 3.days.ago) user.email_logs.create(email_type: 'blah', to_address: user.email, user_id: user.id, created_at: 3.days.ago)
end
it "tracks when max emails are reached" do
expect(EmailLog.reached_max_emails?(user)).to eq(false) expect(EmailLog.reached_max_emails?(user)).to eq(false)
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)
expect(EmailLog.reached_max_emails?(user)).to eq(true) expect(EmailLog.reached_max_emails?(user)).to eq(true)
end end
it "returns false for forgot_password email" do
user.email_logs.create(email_type: 'blah', to_address: user.email, user_id: user.id)
expect(EmailLog.reached_max_emails?(user, 'forgot_password')).to eq(false)
end
end end
describe '#count_per_day' do describe '#count_per_day' do