email_mirror: Ignore missed message email if the user isn't active.

This commit is contained in:
Mateusz Mandera 2019-09-21 02:00:00 +02:00 committed by Tim Abbott
parent 21459ed193
commit 3271235200
2 changed files with 70 additions and 1 deletions

View File

@ -26,6 +26,8 @@ from zerver.models import Stream, Recipient, \
get_user_profile_by_id, get_display_recipient, get_personal_recipient, \
Message, Realm, UserProfile, get_system_bot, get_user, get_stream_by_id_in_realm
from zproject.backends import is_user_active
logger = logging.getLogger(__name__)
def redact_email_address(error_message: str) -> str:
@ -180,6 +182,9 @@ def send_to_missed_message_address(address: str, message: message.Message) -> No
user_profile_id, recipient_id, subject_b = result # type: (bytes, bytes, bytes)
user_profile = get_user_profile_by_id(user_profile_id)
if not is_user_active(user_profile):
logger.warning("Sending user is not active. Ignoring this missed message email.")
return
recipient = Recipient.objects.get(id=recipient_id)
body = construct_zulip_body(message, user_profile.realm)

View File

@ -21,7 +21,7 @@ from zerver.models import (
Recipient,
)
from zerver.lib.actions import ensure_stream
from zerver.lib.actions import ensure_stream, do_deactivate_realm, do_deactivate_user
from zerver.lib.email_mirror import (
process_message, process_missed_message,
@ -672,6 +672,70 @@ class TestMissedMessageEmailMessages(ZulipTestCase):
self.assertEqual(message.recipient.type, Recipient.STREAM)
self.assertEqual(message.recipient.id, usermessage.message.recipient.id)
def test_missed_message_email_response_from_deactivated_user(self) -> None:
self.subscribe(self.example_user("hamlet"), "Denmark")
self.subscribe(self.example_user("othello"), "Denmark")
email = self.example_email('hamlet')
self.login(email)
result = self.client_post("/json/messages", {"type": "stream",
"topic": "test topic",
"content": "test_receive_missed_stream_message_email_messages",
"client": "test suite",
"to": "Denmark"})
self.assert_json_success(result)
user_profile = self.example_user('othello')
message = most_recent_message(user_profile)
mm_address = create_missed_message_address(user_profile, message)
do_deactivate_user(user_profile)
incoming_valid_message = MIMEText('TestMissedMessageEmailMessages Body')
incoming_valid_message['Subject'] = 'TestMissedMessageEmailMessages Subject'
incoming_valid_message['From'] = self.example_email('othello')
incoming_valid_message['To'] = mm_address
incoming_valid_message['Reply-to'] = self.example_email('othello')
initial_last_message = self.get_last_message()
process_message(incoming_valid_message)
# Since othello is deactivated, his message shouldn't be posted:
self.assertEqual(initial_last_message, self.get_last_message())
def test_missed_message_email_response_from_deactivated_realm(self) -> None:
self.subscribe(self.example_user("hamlet"), "Denmark")
self.subscribe(self.example_user("othello"), "Denmark")
email = self.example_email('hamlet')
self.login(email)
result = self.client_post("/json/messages", {"type": "stream",
"topic": "test topic",
"content": "test_receive_missed_stream_message_email_messages",
"client": "test suite",
"to": "Denmark"})
self.assert_json_success(result)
user_profile = self.example_user('othello')
message = most_recent_message(user_profile)
mm_address = create_missed_message_address(user_profile, message)
do_deactivate_realm(user_profile.realm)
incoming_valid_message = MIMEText('TestMissedMessageEmailMessages Body')
incoming_valid_message['Subject'] = 'TestMissedMessageEmailMessages Subject'
incoming_valid_message['From'] = self.example_email('othello')
incoming_valid_message['To'] = mm_address
incoming_valid_message['Reply-to'] = self.example_email('othello')
initial_last_message = self.get_last_message()
process_message(incoming_valid_message)
# Since othello's realm is deactivated, his message shouldn't be posted:
self.assertEqual(initial_last_message, self.get_last_message())
class TestEmptyGatewaySetting(ZulipTestCase):
def test_missed_message(self) -> None:
email = self.example_email('othello')