From 4e5228dc9eb0325babfd0c688a737bdde3a8d8e4 Mon Sep 17 00:00:00 2001 From: Arpit Jalan Date: Mon, 31 Aug 2015 11:24:37 +0530 Subject: [PATCH] FIX: don't send notification emails to deactivated user --- app/models/user_email_observer.rb | 4 +-- spec/models/user_email_observer_spec.rb | 42 +++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 2 deletions(-) diff --git a/app/models/user_email_observer.rb b/app/models/user_email_observer.rb index 3f24c6c5684..0da32b73f0f 100644 --- a/app/models/user_email_observer.rb +++ b/app/models/user_email_observer.rb @@ -39,7 +39,7 @@ class UserEmailObserver < ActiveRecord::Observer private def enqueue(type) - return unless notification.user.email_direct? + return unless (notification.user.email_direct? && notification.user.active?) Jobs.enqueue_in(delay, :user_email, @@ -49,7 +49,7 @@ class UserEmailObserver < ActiveRecord::Observer end def enqueue_private(type) - return unless notification.user.email_private_messages? + return unless (notification.user.email_private_messages? && notification.user.active?) Jobs.enqueue_in(delay, :user_email, diff --git a/spec/models/user_email_observer_spec.rb b/spec/models/user_email_observer_spec.rb index ece539e7d2e..74dec64b5e3 100644 --- a/spec/models/user_email_observer_spec.rb +++ b/spec/models/user_email_observer_spec.rb @@ -24,6 +24,12 @@ describe UserEmailObserver do UserEmailObserver.send(:new).after_commit(notification) end + it "doesn't enqueue an email if the user account is deactivated" do + user.active = false + Jobs.expects(:enqueue_in).with(SiteSetting.email_time_window_mins.minutes, :user_email, has_entry(type: :user_mentioned)).never + UserEmailObserver.send(:new).after_commit(notification) + end + end context 'posted' do @@ -42,6 +48,12 @@ describe UserEmailObserver do UserEmailObserver.send(:new).after_commit(notification) end + it "doesn't enqueue an email if the user account is deactivated" do + user.active = false + Jobs.expects(:enqueue_in).with(SiteSetting.email_time_window_mins.minutes, :user_email, has_entry(type: :user_posted)).never + UserEmailObserver.send(:new).after_commit(notification) + end + end context 'user_replied' do @@ -60,6 +72,12 @@ describe UserEmailObserver do UserEmailObserver.send(:new).after_commit(notification) end + it "doesn't enqueue an email if the user account is deactivated" do + user.active = false + Jobs.expects(:enqueue_in).with(SiteSetting.email_time_window_mins.minutes, :user_email, has_entry(type: :user_replied)).never + UserEmailObserver.send(:new).after_commit(notification) + end + end context 'user_quoted' do @@ -78,6 +96,12 @@ describe UserEmailObserver do UserEmailObserver.send(:new).after_commit(notification) end + it "doesn't enqueue an email if the user account is deactivated" do + user.active = false + Jobs.expects(:enqueue_in).with(SiteSetting.email_time_window_mins.minutes, :user_email, has_entry(type: :user_quoted)).never + UserEmailObserver.send(:new).after_commit(notification) + end + end context 'email_user_invited_to_private_message' do @@ -96,6 +120,12 @@ describe UserEmailObserver do UserEmailObserver.send(:new).after_commit(notification) end + it "doesn't enqueue an email if the user account is deactivated" do + user.active = false + Jobs.expects(:enqueue_in).with(SiteSetting.email_time_window_mins.minutes, :user_email, has_entry(type: :user_invited_to_private_message)).never + UserEmailObserver.send(:new).after_commit(notification) + end + end context 'private_message' do @@ -114,6 +144,12 @@ describe UserEmailObserver do UserEmailObserver.send(:new).after_commit(notification) end + it "doesn't enqueue an email if the user account is deactivated" do + user.active = false + Jobs.expects(:enqueue_in).with(SiteSetting.email_time_window_mins.minutes, :user_email, has_entry(type: :user_private_message)).never + UserEmailObserver.send(:new).after_commit(notification) + end + end context 'user_invited_to_topic' do @@ -132,6 +168,12 @@ describe UserEmailObserver do UserEmailObserver.send(:new).after_commit(notification) end + it "doesn't enqueue an email if the user account is deactivated" do + user.active = false + Jobs.expects(:enqueue_in).with(SiteSetting.email_time_window_mins.minutes, :user_email, has_entry(type: :user_invited_to_topic)).never + UserEmailObserver.send(:new).after_commit(notification) + end + end end