2020-06-05 23:35:52 +02:00
|
|
|
import base64
|
2020-06-05 23:26:35 +02:00
|
|
|
import email.policy
|
2020-06-11 00:54:34 +02:00
|
|
|
import os
|
2017-04-18 17:28:55 +02:00
|
|
|
import subprocess
|
2020-06-11 00:54:34 +02:00
|
|
|
from email import message_from_string
|
2020-06-05 23:26:35 +02:00
|
|
|
from email.message import EmailMessage, MIMEPart
|
2020-06-11 00:54:34 +02:00
|
|
|
from typing import Any, Callable, Dict, Mapping, Optional
|
|
|
|
from unittest import mock
|
2017-04-18 17:28:55 +02:00
|
|
|
|
2020-08-07 01:09:47 +02:00
|
|
|
import orjson
|
2020-06-11 00:54:34 +02:00
|
|
|
from django.conf import settings
|
2017-04-18 17:28:55 +02:00
|
|
|
from django.http import HttpResponse
|
2015-10-29 16:09:09 +01:00
|
|
|
|
2020-06-11 00:54:34 +02:00
|
|
|
from zerver.lib.actions import do_deactivate_realm, do_deactivate_user, ensure_stream
|
2015-10-29 16:09:09 +01:00
|
|
|
from zerver.lib.email_mirror import (
|
2020-06-11 00:54:34 +02:00
|
|
|
ZulipEmailForwardError,
|
2015-10-31 16:31:17 +01:00
|
|
|
create_missed_message_address,
|
2020-06-11 00:54:34 +02:00
|
|
|
filter_footer,
|
2016-09-22 18:41:10 +02:00
|
|
|
get_missed_message_token_from_address,
|
2019-03-09 22:35:45 +01:00
|
|
|
is_forwarded,
|
2019-03-22 17:05:26 +01:00
|
|
|
is_missed_message_address,
|
2019-03-22 16:33:57 +01:00
|
|
|
log_and_report,
|
2020-06-11 00:54:34 +02:00
|
|
|
process_message,
|
|
|
|
process_missed_message,
|
2019-03-22 16:33:57 +01:00
|
|
|
redact_email_address,
|
2020-06-11 00:54:34 +02:00
|
|
|
strip_from_subject,
|
2015-10-29 16:09:09 +01:00
|
|
|
)
|
2019-03-21 10:24:56 +01:00
|
|
|
from zerver.lib.email_mirror_helpers import (
|
|
|
|
decode_email_address,
|
|
|
|
encode_email_address,
|
|
|
|
get_email_gateway_message_string_from_address,
|
|
|
|
)
|
2019-03-15 18:51:39 +01:00
|
|
|
from zerver.lib.email_notifications import convert_html_to_markdown
|
2017-06-26 19:43:32 +02:00
|
|
|
from zerver.lib.send_email import FromAddress
|
2020-06-11 00:54:34 +02:00
|
|
|
from zerver.lib.test_classes import ZulipTestCase
|
2020-08-14 10:03:36 +02:00
|
|
|
from zerver.lib.test_helpers import mock_queue_publish, most_recent_message, most_recent_usermessage
|
2020-06-11 00:54:34 +02:00
|
|
|
from zerver.models import (
|
|
|
|
MissedMessageEmailAddress,
|
|
|
|
Recipient,
|
|
|
|
UserProfile,
|
|
|
|
get_display_recipient,
|
|
|
|
get_realm,
|
|
|
|
get_stream,
|
|
|
|
get_system_bot,
|
|
|
|
)
|
2019-12-26 13:46:55 +01:00
|
|
|
from zerver.worker.queue_processors import MirrorWorker
|
2015-10-31 16:31:17 +01:00
|
|
|
|
2015-10-29 16:09:09 +01:00
|
|
|
|
2018-08-13 18:28:24 +02:00
|
|
|
class TestEncodeDecode(ZulipTestCase):
|
2019-05-26 18:07:21 +02:00
|
|
|
def _assert_options(self, options: Dict[str, bool], show_sender: bool=False,
|
2020-01-15 16:28:46 +01:00
|
|
|
include_footer: bool=False, include_quotes: bool=False,
|
|
|
|
prefer_text: bool=True) -> None:
|
2019-05-26 16:25:23 +02:00
|
|
|
self.assertEqual(show_sender, ('show_sender' in options) and options['show_sender'])
|
2019-06-06 12:14:12 +02:00
|
|
|
self.assertEqual(include_footer, ('include_footer' in options) and options['include_footer'])
|
2019-07-14 03:51:53 +02:00
|
|
|
self.assertEqual(include_quotes, ('include_quotes' in options) and options['include_quotes'])
|
2020-01-15 16:28:46 +01:00
|
|
|
self.assertEqual(prefer_text, options.get('prefer_text', True))
|
2019-05-26 16:25:23 +02:00
|
|
|
|
2018-08-13 18:28:24 +02:00
|
|
|
def test_encode_decode(self) -> None:
|
|
|
|
realm = get_realm('zulip')
|
|
|
|
stream_name = 'dev. help'
|
|
|
|
stream = ensure_stream(realm, stream_name)
|
|
|
|
email_address = encode_email_address(stream)
|
2020-06-09 00:25:09 +02:00
|
|
|
self.assertEqual(email_address, f"dev-help.{stream.email_token}@testserver")
|
2019-09-05 11:27:59 +02:00
|
|
|
|
|
|
|
# The default form of the email address (with an option - "include-footer"):
|
|
|
|
token, options = decode_email_address(
|
python: Use trailing commas consistently.
Automatically generated by the following script, based on the output
of lint with flake8-comma:
import re
import sys
last_filename = None
last_row = None
lines = []
for msg in sys.stdin:
m = re.match(
r"\x1b\[35mflake8 \|\x1b\[0m \x1b\[1;31m(.+):(\d+):(\d+): (\w+)", msg
)
if m:
filename, row_str, col_str, err = m.groups()
row, col = int(row_str), int(col_str)
if filename == last_filename:
assert last_row != row
else:
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
with open(filename) as f:
lines = f.readlines()
last_filename = filename
last_row = row
line = lines[row - 1]
if err in ["C812", "C815"]:
lines[row - 1] = line[: col - 1] + "," + line[col - 1 :]
elif err in ["C819"]:
assert line[col - 2] == ","
lines[row - 1] = line[: col - 2] + line[col - 1 :].lstrip(" ")
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-10 05:23:40 +02:00
|
|
|
f"dev-help.{stream.email_token}.include-footer@testserver",
|
2019-09-05 11:27:59 +02:00
|
|
|
)
|
|
|
|
self._assert_options(options, include_footer=True)
|
|
|
|
self.assertEqual(token, stream.email_token)
|
|
|
|
|
|
|
|
# Using + instead of . as the separator is also supported for backwards compatibility,
|
|
|
|
# since that was the original form of addresses that we used:
|
|
|
|
token, options = decode_email_address(
|
python: Use trailing commas consistently.
Automatically generated by the following script, based on the output
of lint with flake8-comma:
import re
import sys
last_filename = None
last_row = None
lines = []
for msg in sys.stdin:
m = re.match(
r"\x1b\[35mflake8 \|\x1b\[0m \x1b\[1;31m(.+):(\d+):(\d+): (\w+)", msg
)
if m:
filename, row_str, col_str, err = m.groups()
row, col = int(row_str), int(col_str)
if filename == last_filename:
assert last_row != row
else:
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
with open(filename) as f:
lines = f.readlines()
last_filename = filename
last_row = row
line = lines[row - 1]
if err in ["C812", "C815"]:
lines[row - 1] = line[: col - 1] + "," + line[col - 1 :]
elif err in ["C819"]:
assert line[col - 2] == ","
lines[row - 1] = line[: col - 2] + line[col - 1 :].lstrip(" ")
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-10 05:23:40 +02:00
|
|
|
f"dev-help+{stream.email_token}+include-footer@testserver",
|
2019-09-05 11:27:59 +02:00
|
|
|
)
|
|
|
|
self._assert_options(options, include_footer=True)
|
|
|
|
self.assertEqual(token, stream.email_token)
|
|
|
|
|
2019-05-26 16:25:23 +02:00
|
|
|
token, options = decode_email_address(email_address)
|
2019-05-26 18:07:21 +02:00
|
|
|
self._assert_options(options)
|
2018-08-13 18:28:24 +02:00
|
|
|
self.assertEqual(token, stream.email_token)
|
|
|
|
|
2019-09-05 11:27:59 +02:00
|
|
|
# We also handle mixing + and . but it shouldn't be recommended to users.
|
|
|
|
email_address_all_options = "dev-help.{}+include-footer.show-sender+include-quotes@testserver"
|
|
|
|
email_address_all_options = email_address_all_options.format(stream.email_token)
|
2019-05-26 18:07:21 +02:00
|
|
|
token, options = decode_email_address(email_address_all_options)
|
2019-07-14 03:51:53 +02:00
|
|
|
self._assert_options(options, show_sender=True, include_footer=True, include_quotes=True)
|
2018-08-13 18:28:24 +02:00
|
|
|
self.assertEqual(token, stream.email_token)
|
|
|
|
|
|
|
|
email_address = email_address.replace('@testserver', '@zulip.org')
|
2019-05-26 18:07:21 +02:00
|
|
|
email_address_all_options = email_address_all_options.replace('@testserver', '@zulip.org')
|
2019-03-21 11:28:14 +01:00
|
|
|
with self.assertRaises(ZulipEmailForwardError):
|
|
|
|
decode_email_address(email_address)
|
|
|
|
|
|
|
|
with self.assertRaises(ZulipEmailForwardError):
|
2019-05-26 18:07:21 +02:00
|
|
|
decode_email_address(email_address_all_options)
|
2018-08-13 18:28:24 +02:00
|
|
|
|
|
|
|
with self.settings(EMAIL_GATEWAY_EXTRA_PATTERN_HACK='@zulip.org'):
|
2019-05-26 16:25:23 +02:00
|
|
|
token, options = decode_email_address(email_address)
|
2019-05-26 18:07:21 +02:00
|
|
|
self._assert_options(options)
|
2019-02-08 14:13:33 +01:00
|
|
|
self.assertEqual(token, stream.email_token)
|
|
|
|
|
2019-05-26 18:07:21 +02:00
|
|
|
token, options = decode_email_address(email_address_all_options)
|
2019-07-14 03:51:53 +02:00
|
|
|
self._assert_options(options, show_sender=True, include_footer=True, include_quotes=True)
|
2018-08-13 18:28:24 +02:00
|
|
|
self.assertEqual(token, stream.email_token)
|
|
|
|
|
2019-03-21 11:28:14 +01:00
|
|
|
with self.assertRaises(ZulipEmailForwardError):
|
|
|
|
decode_email_address('bogus')
|
2018-08-13 18:28:24 +02:00
|
|
|
|
2019-03-17 13:13:23 +01:00
|
|
|
# Test stream name encoding changes introduced due to
|
|
|
|
# https://github.com/zulip/zulip/issues/9840
|
|
|
|
def test_encode_decode_nonlatin_alphabet_stream_name(self) -> None:
|
|
|
|
realm = get_realm('zulip')
|
|
|
|
stream_name = 'Тестовы some ascii letters'
|
|
|
|
stream = ensure_stream(realm, stream_name)
|
|
|
|
email_address = encode_email_address(stream)
|
|
|
|
|
|
|
|
msg_string = get_email_gateway_message_string_from_address(email_address)
|
|
|
|
parts = msg_string.split('+')
|
|
|
|
# Stream name should be completely stripped to '', so msg_string
|
|
|
|
# should only have the email_token in it.
|
|
|
|
self.assertEqual(len(parts), 1)
|
|
|
|
|
|
|
|
# Correctly decode the resulting address that doesn't have the stream name:
|
2019-03-21 11:28:14 +01:00
|
|
|
token, show_sender = decode_email_address(email_address)
|
2019-03-17 13:13:23 +01:00
|
|
|
self.assertFalse(show_sender)
|
|
|
|
self.assertEqual(token, stream.email_token)
|
|
|
|
|
|
|
|
asciiable_stream_name = "ąężć"
|
|
|
|
stream = ensure_stream(realm, asciiable_stream_name)
|
|
|
|
email_address = encode_email_address(stream)
|
2019-07-14 03:03:47 +02:00
|
|
|
self.assertTrue(email_address.startswith("aezc."))
|
2019-03-17 13:13:23 +01:00
|
|
|
|
2019-03-17 10:36:16 +01:00
|
|
|
def test_decode_ignores_stream_name(self) -> None:
|
|
|
|
stream = get_stream("Denmark", get_realm("zulip"))
|
|
|
|
stream_to_address = encode_email_address(stream)
|
2019-03-17 13:13:23 +01:00
|
|
|
stream_to_address = stream_to_address.replace("denmark", "Some_name")
|
2019-03-17 10:36:16 +01:00
|
|
|
|
|
|
|
# get the email_token:
|
2019-03-21 11:28:14 +01:00
|
|
|
token = decode_email_address(stream_to_address)[0]
|
2019-03-17 10:36:16 +01:00
|
|
|
self.assertEqual(token, stream.email_token)
|
|
|
|
|
2019-09-05 11:34:05 +02:00
|
|
|
def test_encode_with_show_sender(self) -> None:
|
|
|
|
stream = get_stream("Denmark", get_realm("zulip"))
|
|
|
|
stream_to_address = encode_email_address(stream, show_sender=True)
|
|
|
|
|
|
|
|
token, options = decode_email_address(stream_to_address)
|
|
|
|
self._assert_options(options, show_sender=True)
|
|
|
|
self.assertEqual(token, stream.email_token)
|
|
|
|
|
2020-01-15 16:28:46 +01:00
|
|
|
def test_decode_prefer_text_options(self) -> None:
|
|
|
|
stream = get_stream("Denmark", get_realm("zulip"))
|
2020-06-09 00:25:09 +02:00
|
|
|
address_prefer_text = f"Denmark.{stream.email_token}.prefer-text@testserver"
|
|
|
|
address_prefer_html = f"Denmark.{stream.email_token}.prefer-html@testserver"
|
2020-01-15 16:28:46 +01:00
|
|
|
|
|
|
|
token, options = decode_email_address(address_prefer_text)
|
|
|
|
self._assert_options(options, prefer_text=True)
|
|
|
|
|
|
|
|
token, options = decode_email_address(address_prefer_html)
|
|
|
|
self._assert_options(options, prefer_text=False)
|
|
|
|
|
2019-03-22 17:05:26 +01:00
|
|
|
class TestGetMissedMessageToken(ZulipTestCase):
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_get_missed_message_token(self) -> None:
|
2019-03-22 17:05:26 +01:00
|
|
|
with self.settings(EMAIL_GATEWAY_PATTERN="%s@example.com"):
|
|
|
|
address = 'mm' + ('x' * 32) + '@example.com'
|
|
|
|
self.assertTrue(is_missed_message_address(address))
|
|
|
|
token = get_missed_message_token_from_address(address)
|
2019-12-26 13:46:55 +01:00
|
|
|
self.assertEqual(token, 'mm' + 'x' * 32)
|
2019-03-22 17:05:26 +01:00
|
|
|
|
|
|
|
# This next section was a bug at one point--we'd treat ordinary
|
|
|
|
# user addresses that happened to begin with "mm" as being
|
|
|
|
# the special mm+32chars tokens.
|
|
|
|
address = 'mmathers@example.com'
|
|
|
|
self.assertFalse(is_missed_message_address(address))
|
|
|
|
with self.assertRaises(ZulipEmailForwardError):
|
|
|
|
get_missed_message_token_from_address(address)
|
|
|
|
|
|
|
|
# Now test the case where we our address does not match the
|
|
|
|
# EMAIL_GATEWAY_PATTERN.
|
|
|
|
# This used to crash in an ugly way; we want to throw a proper
|
|
|
|
# exception.
|
|
|
|
address = 'alice@not-the-domain-we-were-expecting.com'
|
|
|
|
self.assertFalse(is_missed_message_address(address))
|
|
|
|
with self.assertRaises(ZulipEmailForwardError):
|
|
|
|
get_missed_message_token_from_address(address)
|
2016-09-22 18:55:18 +02:00
|
|
|
|
2019-03-21 16:35:31 +01:00
|
|
|
class TestFilterFooter(ZulipTestCase):
|
|
|
|
def test_filter_footer(self) -> None:
|
|
|
|
text = """Test message
|
|
|
|
--
|
|
|
|
Footer"""
|
|
|
|
result = filter_footer(text)
|
|
|
|
self.assertEqual(result, "Test message")
|
|
|
|
|
|
|
|
def test_filter_footer_many_parts(self) -> None:
|
|
|
|
text = """Test message
|
|
|
|
--
|
|
|
|
Part1
|
|
|
|
--
|
|
|
|
Part2"""
|
|
|
|
result = filter_footer(text)
|
|
|
|
# Multiple possible footers, don't strip
|
|
|
|
self.assertEqual(result, text)
|
|
|
|
|
2016-08-23 02:08:42 +02:00
|
|
|
class TestStreamEmailMessagesSuccess(ZulipTestCase):
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_receive_stream_email_messages_success(self) -> None:
|
2015-10-29 16:09:09 +01:00
|
|
|
|
|
|
|
# build dummy messages for stream
|
|
|
|
# test valid incoming stream message is processed properly
|
2017-05-07 17:21:26 +02:00
|
|
|
user_profile = self.example_user('hamlet')
|
2020-03-06 18:40:46 +01:00
|
|
|
self.login_user(user_profile)
|
2017-08-25 06:01:29 +02:00
|
|
|
self.subscribe(user_profile, "Denmark")
|
2015-10-29 16:09:09 +01:00
|
|
|
stream = get_stream("Denmark", user_profile.realm)
|
|
|
|
|
|
|
|
stream_to_address = encode_email_address(stream)
|
|
|
|
|
2020-06-05 23:26:35 +02:00
|
|
|
incoming_valid_message = EmailMessage()
|
|
|
|
incoming_valid_message.set_content('TestStreamEmailMessages Body')
|
2015-10-29 16:09:09 +01:00
|
|
|
|
|
|
|
incoming_valid_message['Subject'] = 'TestStreamEmailMessages Subject'
|
2017-05-24 05:08:49 +02:00
|
|
|
incoming_valid_message['From'] = self.example_email('hamlet')
|
2015-10-29 16:09:09 +01:00
|
|
|
incoming_valid_message['To'] = stream_to_address
|
2017-05-24 05:08:49 +02:00
|
|
|
incoming_valid_message['Reply-to'] = self.example_email('othello')
|
2015-10-29 16:09:09 +01:00
|
|
|
|
|
|
|
process_message(incoming_valid_message)
|
|
|
|
|
|
|
|
# Hamlet is subscribed to this stream so should see the email message from Othello.
|
|
|
|
message = most_recent_message(user_profile)
|
|
|
|
|
|
|
|
self.assertEqual(message.content, "TestStreamEmailMessages Body")
|
|
|
|
self.assertEqual(get_display_recipient(message.recipient), stream.name)
|
2016-07-14 17:48:11 +02:00
|
|
|
self.assertEqual(message.topic_name(), incoming_valid_message['Subject'])
|
2015-10-29 16:09:09 +01:00
|
|
|
|
2020-07-22 21:11:25 +02:00
|
|
|
# Test receiving an email with the address on an UnstructuredHeader
|
|
|
|
# (e.g. Envelope-To) instead of an AddressHeader (e.g. To).
|
|
|
|
# https://github.com/zulip/zulip/issues/15864
|
|
|
|
def test_receive_stream_email_messages_other_header_success(self) -> None:
|
|
|
|
user_profile = self.example_user('hamlet')
|
|
|
|
self.login_user(user_profile)
|
|
|
|
self.subscribe(user_profile, "Denmark")
|
|
|
|
stream = get_stream("Denmark", user_profile.realm)
|
|
|
|
|
|
|
|
stream_to_address = encode_email_address(stream)
|
|
|
|
|
|
|
|
incoming_valid_message = EmailMessage()
|
|
|
|
incoming_valid_message.set_content('TestStreamEmailMessages Body')
|
|
|
|
|
|
|
|
incoming_valid_message['Subject'] = 'TestStreamEmailMessages Subject'
|
|
|
|
incoming_valid_message['From'] = self.example_email('hamlet')
|
|
|
|
# Simulate a mailing list
|
|
|
|
incoming_valid_message['To'] = "foo-mailinglist@example.com"
|
|
|
|
incoming_valid_message['Envelope-To'] = stream_to_address
|
|
|
|
incoming_valid_message['Reply-to'] = self.example_email('othello')
|
|
|
|
|
|
|
|
process_message(incoming_valid_message)
|
|
|
|
|
|
|
|
# Hamlet is subscribed to this stream so should see the email message from Othello.
|
|
|
|
message = most_recent_message(user_profile)
|
|
|
|
|
|
|
|
self.assertEqual(message.content, "TestStreamEmailMessages Body")
|
|
|
|
self.assertEqual(get_display_recipient(message.recipient), stream.name)
|
|
|
|
self.assertEqual(message.topic_name(), incoming_valid_message['Subject'])
|
|
|
|
|
2018-04-26 19:17:29 +02:00
|
|
|
def test_receive_stream_email_messages_blank_subject_success(self) -> None:
|
|
|
|
user_profile = self.example_user('hamlet')
|
2020-03-06 18:40:46 +01:00
|
|
|
self.login_user(user_profile)
|
2018-04-26 19:17:29 +02:00
|
|
|
self.subscribe(user_profile, "Denmark")
|
|
|
|
stream = get_stream("Denmark", user_profile.realm)
|
|
|
|
|
|
|
|
stream_to_address = encode_email_address(stream)
|
|
|
|
|
2020-06-05 23:26:35 +02:00
|
|
|
incoming_valid_message = EmailMessage()
|
|
|
|
incoming_valid_message.set_content('TestStreamEmailMessages Body')
|
2018-04-26 19:17:29 +02:00
|
|
|
|
|
|
|
incoming_valid_message['Subject'] = ''
|
|
|
|
incoming_valid_message['From'] = self.example_email('hamlet')
|
|
|
|
incoming_valid_message['To'] = stream_to_address
|
|
|
|
incoming_valid_message['Reply-to'] = self.example_email('othello')
|
|
|
|
|
|
|
|
process_message(incoming_valid_message)
|
|
|
|
|
|
|
|
# Hamlet is subscribed to this stream so should see the email message from Othello.
|
|
|
|
message = most_recent_message(user_profile)
|
|
|
|
|
|
|
|
self.assertEqual(message.content, "TestStreamEmailMessages Body")
|
|
|
|
self.assertEqual(get_display_recipient(message.recipient), stream.name)
|
|
|
|
self.assertEqual(message.topic_name(), "(no topic)")
|
|
|
|
|
2018-04-27 22:53:18 +02:00
|
|
|
def test_receive_private_stream_email_messages_success(self) -> None:
|
|
|
|
user_profile = self.example_user('hamlet')
|
2020-03-06 18:40:46 +01:00
|
|
|
self.login_user(user_profile)
|
2018-04-27 22:53:18 +02:00
|
|
|
self.make_stream("private_stream", invite_only=True)
|
|
|
|
self.subscribe(user_profile, "private_stream")
|
|
|
|
stream = get_stream("private_stream", user_profile.realm)
|
|
|
|
|
|
|
|
stream_to_address = encode_email_address(stream)
|
|
|
|
|
2020-06-05 23:26:35 +02:00
|
|
|
incoming_valid_message = EmailMessage()
|
|
|
|
incoming_valid_message.set_content('TestStreamEmailMessages Body')
|
2018-04-27 22:53:18 +02:00
|
|
|
|
|
|
|
incoming_valid_message['Subject'] = 'TestStreamEmailMessages Subject'
|
|
|
|
incoming_valid_message['From'] = self.example_email('hamlet')
|
|
|
|
incoming_valid_message['To'] = stream_to_address
|
|
|
|
incoming_valid_message['Reply-to'] = self.example_email('othello')
|
|
|
|
|
|
|
|
process_message(incoming_valid_message)
|
|
|
|
|
|
|
|
# Hamlet is subscribed to this stream so should see the email message from Othello.
|
|
|
|
message = most_recent_message(user_profile)
|
|
|
|
|
|
|
|
self.assertEqual(message.content, "TestStreamEmailMessages Body")
|
|
|
|
self.assertEqual(get_display_recipient(message.recipient), stream.name)
|
|
|
|
self.assertEqual(message.topic_name(), incoming_valid_message['Subject'])
|
|
|
|
|
2019-01-03 15:53:27 +01:00
|
|
|
def test_receive_stream_email_multiple_recipient_success(self) -> None:
|
|
|
|
user_profile = self.example_user('hamlet')
|
2020-03-06 18:40:46 +01:00
|
|
|
self.login_user(user_profile)
|
2019-01-03 15:53:27 +01:00
|
|
|
self.subscribe(user_profile, "Denmark")
|
|
|
|
stream = get_stream("Denmark", user_profile.realm)
|
|
|
|
|
|
|
|
# stream address is angle-addr within multiple addresses
|
|
|
|
stream_to_addresses = ["A.N. Other <another@example.org>",
|
2020-06-09 00:25:09 +02:00
|
|
|
f"Denmark <{encode_email_address(stream)}>"]
|
2019-01-03 15:53:27 +01:00
|
|
|
|
2020-06-05 23:26:35 +02:00
|
|
|
incoming_valid_message = EmailMessage()
|
|
|
|
incoming_valid_message.set_content('TestStreamEmailMessages Body')
|
2019-01-03 15:53:27 +01:00
|
|
|
|
|
|
|
incoming_valid_message['Subject'] = 'TestStreamEmailMessages Subject'
|
|
|
|
incoming_valid_message['From'] = self.example_email('hamlet')
|
|
|
|
incoming_valid_message['To'] = ", ".join(stream_to_addresses)
|
|
|
|
incoming_valid_message['Reply-to'] = self.example_email('othello')
|
|
|
|
|
|
|
|
process_message(incoming_valid_message)
|
|
|
|
|
|
|
|
# Hamlet is subscribed to this stream so should see the email message from Othello.
|
|
|
|
message = most_recent_message(user_profile)
|
|
|
|
|
|
|
|
self.assertEqual(message.content, "TestStreamEmailMessages Body")
|
|
|
|
self.assertEqual(get_display_recipient(message.recipient), stream.name)
|
|
|
|
self.assertEqual(message.topic_name(), incoming_valid_message['Subject'])
|
|
|
|
|
2019-02-08 14:13:33 +01:00
|
|
|
def test_receive_stream_email_show_sender_success(self) -> None:
|
|
|
|
user_profile = self.example_user('hamlet')
|
2020-03-06 18:40:46 +01:00
|
|
|
self.login_user(user_profile)
|
2019-02-08 14:13:33 +01:00
|
|
|
self.subscribe(user_profile, "Denmark")
|
|
|
|
stream = get_stream("Denmark", user_profile.realm)
|
|
|
|
|
|
|
|
stream_to_address = encode_email_address(stream)
|
|
|
|
parts = stream_to_address.split('@')
|
|
|
|
parts[0] += "+show-sender"
|
|
|
|
stream_to_address = '@'.join(parts)
|
|
|
|
|
2020-06-05 23:26:35 +02:00
|
|
|
incoming_valid_message = EmailMessage()
|
|
|
|
incoming_valid_message.set_content('TestStreamEmailMessages Body')
|
2019-02-08 14:13:33 +01:00
|
|
|
incoming_valid_message['Subject'] = 'TestStreamEmailMessages Subject'
|
|
|
|
incoming_valid_message['From'] = self.example_email('hamlet')
|
|
|
|
incoming_valid_message['To'] = stream_to_address
|
|
|
|
incoming_valid_message['Reply-to'] = self.example_email('othello')
|
|
|
|
|
|
|
|
process_message(incoming_valid_message)
|
|
|
|
message = most_recent_message(user_profile)
|
|
|
|
|
2020-06-10 06:41:04 +02:00
|
|
|
self.assertEqual(message.content, "From: {}\n{}".format(self.example_email('hamlet'),
|
|
|
|
"TestStreamEmailMessages Body"))
|
2019-02-08 14:13:33 +01:00
|
|
|
self.assertEqual(get_display_recipient(message.recipient), stream.name)
|
|
|
|
self.assertEqual(message.topic_name(), incoming_valid_message['Subject'])
|
2019-01-03 15:53:27 +01:00
|
|
|
|
2020-01-29 12:32:56 +01:00
|
|
|
def test_receive_stream_email_show_sender_utf8_encoded_sender(self) -> None:
|
|
|
|
user_profile = self.example_user('hamlet')
|
2020-03-06 18:40:46 +01:00
|
|
|
self.login_user(user_profile)
|
2020-01-29 12:32:56 +01:00
|
|
|
self.subscribe(user_profile, "Denmark")
|
|
|
|
stream = get_stream("Denmark", user_profile.realm)
|
|
|
|
|
|
|
|
stream_to_address = encode_email_address(stream)
|
|
|
|
parts = stream_to_address.split('@')
|
|
|
|
parts[0] += "+show-sender"
|
|
|
|
stream_to_address = '@'.join(parts)
|
|
|
|
|
2020-06-05 23:26:35 +02:00
|
|
|
incoming_valid_message = EmailMessage()
|
|
|
|
incoming_valid_message.set_content('TestStreamEmailMessages Body')
|
2020-01-29 12:32:56 +01:00
|
|
|
incoming_valid_message['Subject'] = 'TestStreamEmailMessages Subject'
|
2020-06-05 23:26:35 +02:00
|
|
|
incoming_valid_message['From'] = 'Test =?utf-8?b?VXNlcsOzxIXEmQ==?= <=?utf-8?q?hamlet=5F=C4=99?=@zulip.com>'
|
2020-01-29 12:32:56 +01:00
|
|
|
incoming_valid_message['To'] = stream_to_address
|
|
|
|
incoming_valid_message['Reply-to'] = self.example_email('othello')
|
|
|
|
|
|
|
|
process_message(incoming_valid_message)
|
|
|
|
message = most_recent_message(user_profile)
|
|
|
|
|
2020-06-10 06:41:04 +02:00
|
|
|
self.assertEqual(message.content, "From: {}\n{}".format('Test Useróąę <hamlet_ę@zulip.com>',
|
|
|
|
"TestStreamEmailMessages Body"))
|
2020-01-29 12:32:56 +01:00
|
|
|
self.assertEqual(get_display_recipient(message.recipient), stream.name)
|
|
|
|
self.assertEqual(message.topic_name(), incoming_valid_message['Subject'])
|
|
|
|
|
2019-06-06 12:14:12 +02:00
|
|
|
def test_receive_stream_email_include_footer_success(self) -> None:
|
2019-05-26 18:07:21 +02:00
|
|
|
user_profile = self.example_user('hamlet')
|
2020-03-06 18:40:46 +01:00
|
|
|
self.login_user(user_profile)
|
2019-05-26 18:07:21 +02:00
|
|
|
self.subscribe(user_profile, "Denmark")
|
|
|
|
stream = get_stream("Denmark", user_profile.realm)
|
|
|
|
|
|
|
|
stream_to_address = encode_email_address(stream)
|
|
|
|
parts = stream_to_address.split('@')
|
2019-06-06 12:14:12 +02:00
|
|
|
parts[0] += "+include-footer"
|
2019-05-26 18:07:21 +02:00
|
|
|
stream_to_address = '@'.join(parts)
|
|
|
|
|
|
|
|
text = """Test message
|
|
|
|
--
|
|
|
|
Footer"""
|
|
|
|
|
2020-06-05 23:26:35 +02:00
|
|
|
incoming_valid_message = EmailMessage()
|
|
|
|
incoming_valid_message.set_content(text)
|
2019-05-26 18:07:21 +02:00
|
|
|
incoming_valid_message['Subject'] = 'TestStreamEmailMessages Subject'
|
|
|
|
incoming_valid_message['From'] = self.example_email('hamlet')
|
2019-05-26 18:28:39 +02:00
|
|
|
incoming_valid_message['To'] = stream_to_address
|
|
|
|
incoming_valid_message['Reply-to'] = self.example_email('othello')
|
|
|
|
|
|
|
|
process_message(incoming_valid_message)
|
|
|
|
message = most_recent_message(user_profile)
|
|
|
|
|
|
|
|
self.assertEqual(message.content, text)
|
|
|
|
self.assertEqual(get_display_recipient(message.recipient), stream.name)
|
|
|
|
self.assertEqual(message.topic_name(), incoming_valid_message['Subject'])
|
|
|
|
|
2019-07-14 03:51:53 +02:00
|
|
|
def test_receive_stream_email_include_quotes_success(self) -> None:
|
2019-05-26 18:28:39 +02:00
|
|
|
user_profile = self.example_user('hamlet')
|
2020-03-06 18:40:46 +01:00
|
|
|
self.login_user(user_profile)
|
2019-05-26 18:28:39 +02:00
|
|
|
self.subscribe(user_profile, "Denmark")
|
|
|
|
stream = get_stream("Denmark", user_profile.realm)
|
|
|
|
|
|
|
|
stream_to_address = encode_email_address(stream)
|
|
|
|
parts = stream_to_address.split('@')
|
2019-07-14 03:51:53 +02:00
|
|
|
parts[0] += "+include-quotes"
|
2019-05-26 18:28:39 +02:00
|
|
|
stream_to_address = '@'.join(parts)
|
|
|
|
|
|
|
|
text = """Reply
|
|
|
|
|
|
|
|
-----Original Message-----
|
|
|
|
|
|
|
|
Quote"""
|
|
|
|
|
2020-06-05 23:26:35 +02:00
|
|
|
incoming_valid_message = EmailMessage()
|
|
|
|
incoming_valid_message.set_content(text)
|
2019-05-26 18:28:39 +02:00
|
|
|
incoming_valid_message['Subject'] = 'TestStreamEmailMessages Subject'
|
|
|
|
incoming_valid_message['From'] = self.example_email('hamlet')
|
2019-05-26 18:07:21 +02:00
|
|
|
incoming_valid_message['To'] = stream_to_address
|
|
|
|
incoming_valid_message['Reply-to'] = self.example_email('othello')
|
|
|
|
|
|
|
|
process_message(incoming_valid_message)
|
|
|
|
message = most_recent_message(user_profile)
|
|
|
|
|
|
|
|
self.assertEqual(message.content, text)
|
|
|
|
self.assertEqual(get_display_recipient(message.recipient), stream.name)
|
|
|
|
self.assertEqual(message.topic_name(), incoming_valid_message['Subject'])
|
|
|
|
|
2019-03-22 09:22:52 +01:00
|
|
|
class TestEmailMirrorMessagesWithAttachments(ZulipTestCase):
|
|
|
|
def test_message_with_valid_attachment(self) -> None:
|
|
|
|
user_profile = self.example_user('hamlet')
|
2020-03-06 18:40:46 +01:00
|
|
|
self.login_user(user_profile)
|
2019-03-22 09:22:52 +01:00
|
|
|
self.subscribe(user_profile, "Denmark")
|
|
|
|
stream = get_stream("Denmark", user_profile.realm)
|
|
|
|
stream_to_address = encode_email_address(stream)
|
|
|
|
|
2020-06-05 23:26:35 +02:00
|
|
|
incoming_valid_message = EmailMessage()
|
|
|
|
incoming_valid_message.set_content("Test body")
|
2019-03-22 09:22:52 +01:00
|
|
|
with open(os.path.join(settings.DEPLOY_ROOT, "static/images/default-avatar.png"), 'rb') as f:
|
|
|
|
image_bytes = f.read()
|
|
|
|
|
2020-06-05 23:26:35 +02:00
|
|
|
incoming_valid_message.add_attachment(
|
|
|
|
image_bytes,
|
|
|
|
maintype="image",
|
|
|
|
subtype="png",
|
|
|
|
filename="image.png",
|
|
|
|
)
|
2019-03-22 09:22:52 +01:00
|
|
|
|
|
|
|
incoming_valid_message['Subject'] = 'TestStreamEmailMessages Subject'
|
|
|
|
incoming_valid_message['From'] = self.example_email('hamlet')
|
|
|
|
incoming_valid_message['To'] = stream_to_address
|
|
|
|
incoming_valid_message['Reply-to'] = self.example_email('othello')
|
|
|
|
|
|
|
|
with mock.patch('zerver.lib.email_mirror.upload_message_file',
|
|
|
|
return_value='https://test_url') as upload_message_file:
|
|
|
|
process_message(incoming_valid_message)
|
|
|
|
upload_message_file.assert_called_with('image.png', len(image_bytes),
|
|
|
|
'image/png', image_bytes,
|
|
|
|
get_system_bot(settings.EMAIL_GATEWAY_BOT),
|
|
|
|
target_realm=user_profile.realm)
|
2020-01-14 16:33:48 +01:00
|
|
|
|
|
|
|
message = most_recent_message(user_profile)
|
|
|
|
self.assertEqual(message.content, "Test body\n[image.png](https://test_url)")
|
|
|
|
|
2020-01-30 15:37:33 +01:00
|
|
|
def test_message_with_attachment_utf8_filename(self) -> None:
|
|
|
|
user_profile = self.example_user('hamlet')
|
2020-03-06 18:40:46 +01:00
|
|
|
self.login_user(user_profile)
|
2020-01-30 15:37:33 +01:00
|
|
|
self.subscribe(user_profile, "Denmark")
|
|
|
|
stream = get_stream("Denmark", user_profile.realm)
|
|
|
|
stream_to_address = encode_email_address(stream)
|
|
|
|
|
2020-06-05 23:26:35 +02:00
|
|
|
incoming_valid_message = EmailMessage()
|
|
|
|
incoming_valid_message.set_content("Test body")
|
2020-01-30 15:37:33 +01:00
|
|
|
with open(os.path.join(settings.DEPLOY_ROOT, "static/images/default-avatar.png"), 'rb') as f:
|
|
|
|
image_bytes = f.read()
|
|
|
|
|
|
|
|
utf8_filename = "image_ąęó.png"
|
2020-06-05 23:26:35 +02:00
|
|
|
incoming_valid_message.add_attachment(
|
|
|
|
image_bytes,
|
|
|
|
maintype="image",
|
|
|
|
subtype="png",
|
|
|
|
filename=utf8_filename,
|
|
|
|
)
|
2020-01-30 15:37:33 +01:00
|
|
|
|
|
|
|
incoming_valid_message['Subject'] = 'TestStreamEmailMessages Subject'
|
|
|
|
incoming_valid_message['From'] = self.example_email('hamlet')
|
|
|
|
incoming_valid_message['To'] = stream_to_address
|
|
|
|
incoming_valid_message['Reply-to'] = self.example_email('othello')
|
|
|
|
|
|
|
|
with mock.patch('zerver.lib.email_mirror.upload_message_file',
|
|
|
|
return_value='https://test_url') as upload_message_file:
|
|
|
|
process_message(incoming_valid_message)
|
|
|
|
upload_message_file.assert_called_with(utf8_filename, len(image_bytes),
|
|
|
|
'image/png', image_bytes,
|
|
|
|
get_system_bot(settings.EMAIL_GATEWAY_BOT),
|
|
|
|
target_realm=user_profile.realm)
|
|
|
|
|
|
|
|
message = most_recent_message(user_profile)
|
2020-06-10 06:41:04 +02:00
|
|
|
self.assertEqual(message.content, f"Test body\n[{utf8_filename}](https://test_url)")
|
2020-01-30 15:37:33 +01:00
|
|
|
|
2020-01-14 16:33:48 +01:00
|
|
|
def test_message_with_valid_nested_attachment(self) -> None:
|
|
|
|
user_profile = self.example_user('hamlet')
|
2020-03-06 18:40:46 +01:00
|
|
|
self.login_user(user_profile)
|
2020-01-14 16:33:48 +01:00
|
|
|
self.subscribe(user_profile, "Denmark")
|
|
|
|
stream = get_stream("Denmark", user_profile.realm)
|
|
|
|
stream_to_address = encode_email_address(stream)
|
|
|
|
|
2020-06-05 23:26:35 +02:00
|
|
|
incoming_valid_message = EmailMessage()
|
|
|
|
incoming_valid_message.set_content("Test body")
|
2020-01-14 16:33:48 +01:00
|
|
|
|
2020-06-05 23:26:35 +02:00
|
|
|
nested_multipart = EmailMessage()
|
|
|
|
nested_multipart.set_content("Nested text that should get skipped.")
|
2020-01-14 16:33:48 +01:00
|
|
|
with open(os.path.join(settings.DEPLOY_ROOT, "static/images/default-avatar.png"), 'rb') as f:
|
|
|
|
image_bytes = f.read()
|
|
|
|
|
2020-06-05 23:26:35 +02:00
|
|
|
nested_multipart.add_attachment(
|
|
|
|
image_bytes,
|
|
|
|
maintype="image",
|
|
|
|
subtype="png",
|
|
|
|
filename="image.png",
|
|
|
|
)
|
|
|
|
incoming_valid_message.add_attachment(nested_multipart)
|
2020-01-14 16:33:48 +01:00
|
|
|
|
|
|
|
incoming_valid_message['Subject'] = 'Subject'
|
|
|
|
incoming_valid_message['From'] = self.example_email('hamlet')
|
|
|
|
incoming_valid_message['To'] = stream_to_address
|
|
|
|
incoming_valid_message['Reply-to'] = self.example_email('othello')
|
|
|
|
|
|
|
|
with mock.patch('zerver.lib.email_mirror.upload_message_file',
|
|
|
|
return_value='https://test_url') as upload_message_file:
|
|
|
|
process_message(incoming_valid_message)
|
|
|
|
upload_message_file.assert_called_with('image.png', len(image_bytes),
|
|
|
|
'image/png', image_bytes,
|
|
|
|
get_system_bot(settings.EMAIL_GATEWAY_BOT),
|
|
|
|
target_realm=user_profile.realm)
|
2019-03-22 09:22:52 +01:00
|
|
|
|
|
|
|
message = most_recent_message(user_profile)
|
2020-01-14 13:05:14 +01:00
|
|
|
self.assertEqual(message.content, "Test body\n[image.png](https://test_url)")
|
2019-03-22 09:22:52 +01:00
|
|
|
|
|
|
|
def test_message_with_invalid_attachment(self) -> None:
|
|
|
|
user_profile = self.example_user('hamlet')
|
2020-03-06 18:40:46 +01:00
|
|
|
self.login_user(user_profile)
|
2019-03-22 09:22:52 +01:00
|
|
|
self.subscribe(user_profile, "Denmark")
|
|
|
|
stream = get_stream("Denmark", user_profile.realm)
|
|
|
|
stream_to_address = encode_email_address(stream)
|
|
|
|
|
2020-06-05 23:26:35 +02:00
|
|
|
incoming_valid_message = EmailMessage()
|
|
|
|
incoming_valid_message.set_content("Test body")
|
2019-03-22 09:22:52 +01:00
|
|
|
# Create an invalid attachment:
|
2020-06-05 23:26:35 +02:00
|
|
|
attachment_msg = MIMEPart()
|
2019-03-22 09:22:52 +01:00
|
|
|
attachment_msg.add_header('Content-Disposition', 'attachment', filename="some_attachment")
|
2020-06-05 23:26:35 +02:00
|
|
|
incoming_valid_message.add_attachment(attachment_msg)
|
2019-03-22 09:22:52 +01:00
|
|
|
|
|
|
|
incoming_valid_message['Subject'] = 'TestStreamEmailMessages Subject'
|
|
|
|
incoming_valid_message['From'] = self.example_email('hamlet')
|
|
|
|
incoming_valid_message['To'] = stream_to_address
|
|
|
|
incoming_valid_message['Reply-to'] = self.example_email('othello')
|
|
|
|
|
|
|
|
with mock.patch('zerver.lib.email_mirror.logger.warning') as mock_warn:
|
|
|
|
process_message(incoming_valid_message)
|
2020-05-02 08:44:14 +02:00
|
|
|
mock_warn.assert_called_with(
|
|
|
|
"Payload is not bytes (invalid attachment %s in message from %s).",
|
|
|
|
'some_attachment', self.example_email('hamlet'),
|
|
|
|
)
|
2019-03-22 09:22:52 +01:00
|
|
|
|
2020-01-15 16:28:46 +01:00
|
|
|
def test_receive_plaintext_and_html_prefer_text_html_options(self) -> None:
|
|
|
|
user_profile = self.example_user('hamlet')
|
2020-03-06 18:40:46 +01:00
|
|
|
self.login_user(user_profile)
|
2020-01-15 16:28:46 +01:00
|
|
|
self.subscribe(user_profile, "Denmark")
|
|
|
|
stream = get_stream("Denmark", user_profile.realm)
|
2020-06-09 00:25:09 +02:00
|
|
|
stream_address = f"Denmark.{stream.email_token}@testserver"
|
|
|
|
stream_address_prefer_html = f"Denmark.{stream.email_token}.prefer-html@testserver"
|
2020-01-15 16:28:46 +01:00
|
|
|
|
|
|
|
text = "Test message"
|
|
|
|
html = "<html><body><b>Test html message</b></body></html>"
|
|
|
|
|
2020-06-05 23:26:35 +02:00
|
|
|
incoming_valid_message = EmailMessage()
|
|
|
|
incoming_valid_message.add_alternative(text)
|
|
|
|
incoming_valid_message.add_alternative(html, subtype="html")
|
2020-01-15 16:28:46 +01:00
|
|
|
|
|
|
|
incoming_valid_message['Subject'] = 'TestStreamEmailMessages Subject'
|
|
|
|
incoming_valid_message['From'] = self.example_email('hamlet')
|
|
|
|
incoming_valid_message['To'] = stream_address
|
|
|
|
incoming_valid_message['Reply-to'] = self.example_email('othello')
|
|
|
|
|
|
|
|
process_message(incoming_valid_message)
|
|
|
|
message = most_recent_message(user_profile)
|
|
|
|
|
|
|
|
self.assertEqual(message.content, "Test message")
|
|
|
|
|
|
|
|
del incoming_valid_message['To']
|
|
|
|
incoming_valid_message['To'] = stream_address_prefer_html
|
|
|
|
|
|
|
|
process_message(incoming_valid_message)
|
|
|
|
message = most_recent_message(user_profile)
|
|
|
|
|
|
|
|
self.assertEqual(message.content, "**Test html message**")
|
|
|
|
|
2020-01-16 13:07:04 +01:00
|
|
|
def test_receive_only_plaintext_with_prefer_html_option(self) -> None:
|
|
|
|
user_profile = self.example_user('hamlet')
|
2020-03-06 18:40:46 +01:00
|
|
|
self.login_user(user_profile)
|
2020-01-16 13:07:04 +01:00
|
|
|
self.subscribe(user_profile, "Denmark")
|
|
|
|
stream = get_stream("Denmark", user_profile.realm)
|
2020-06-09 00:25:09 +02:00
|
|
|
stream_address_prefer_html = f"Denmark.{stream.email_token}.prefer-html@testserver"
|
2020-01-16 13:07:04 +01:00
|
|
|
|
|
|
|
text = "Test message"
|
|
|
|
# This should be correctly identified as empty html body:
|
|
|
|
html = "<html><body></body></html>"
|
|
|
|
|
2020-06-05 23:26:35 +02:00
|
|
|
incoming_valid_message = EmailMessage()
|
|
|
|
incoming_valid_message.add_alternative(text)
|
|
|
|
incoming_valid_message.add_alternative(html, subtype="html")
|
2020-01-16 13:07:04 +01:00
|
|
|
|
|
|
|
incoming_valid_message['Subject'] = 'TestStreamEmailMessages Subject'
|
|
|
|
incoming_valid_message['From'] = self.example_email('hamlet')
|
|
|
|
incoming_valid_message['To'] = stream_address_prefer_html
|
|
|
|
incoming_valid_message['Reply-to'] = self.example_email('othello')
|
|
|
|
|
|
|
|
process_message(incoming_valid_message)
|
|
|
|
message = most_recent_message(user_profile)
|
|
|
|
|
|
|
|
# HTML body is empty, so the plaintext content should be picked, despite prefer-html option.
|
|
|
|
self.assertEqual(message.content, "Test message")
|
|
|
|
|
2016-08-23 02:08:42 +02:00
|
|
|
class TestStreamEmailMessagesEmptyBody(ZulipTestCase):
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_receive_stream_email_messages_empty_body(self) -> None:
|
2015-10-29 16:09:09 +01:00
|
|
|
# build dummy messages for stream
|
|
|
|
# test message with empty body is not sent
|
2017-05-07 17:21:26 +02:00
|
|
|
user_profile = self.example_user('hamlet')
|
2020-03-06 18:40:46 +01:00
|
|
|
self.login_user(user_profile)
|
2017-08-25 06:01:29 +02:00
|
|
|
self.subscribe(user_profile, "Denmark")
|
2015-10-29 16:09:09 +01:00
|
|
|
stream = get_stream("Denmark", user_profile.realm)
|
|
|
|
stream_to_address = encode_email_address(stream)
|
|
|
|
|
|
|
|
# empty body
|
2020-06-05 23:26:35 +02:00
|
|
|
incoming_valid_message = EmailMessage()
|
|
|
|
incoming_valid_message.set_content('')
|
2015-10-29 16:09:09 +01:00
|
|
|
|
|
|
|
incoming_valid_message['Subject'] = 'TestStreamEmailMessages Subject'
|
2017-05-24 05:08:49 +02:00
|
|
|
incoming_valid_message['From'] = self.example_email('hamlet')
|
2015-10-29 16:09:09 +01:00
|
|
|
incoming_valid_message['To'] = stream_to_address
|
2017-05-24 05:08:49 +02:00
|
|
|
incoming_valid_message['Reply-to'] = self.example_email('othello')
|
2015-10-29 16:09:09 +01:00
|
|
|
|
2019-03-22 10:06:44 +01:00
|
|
|
with mock.patch('zerver.lib.email_mirror.logging.warning') as mock_warn:
|
|
|
|
process_message(incoming_valid_message)
|
|
|
|
mock_warn.assert_called_with("Email has no nonempty body sections; ignoring.")
|
2019-01-15 20:19:49 +01:00
|
|
|
|
|
|
|
def test_receive_stream_email_messages_no_textual_body(self) -> None:
|
|
|
|
user_profile = self.example_user('hamlet')
|
2020-03-06 18:40:46 +01:00
|
|
|
self.login_user(user_profile)
|
2019-01-15 20:19:49 +01:00
|
|
|
self.subscribe(user_profile, "Denmark")
|
|
|
|
stream = get_stream("Denmark", user_profile.realm)
|
|
|
|
stream_to_address = encode_email_address(stream)
|
|
|
|
# No textual body
|
2020-06-05 23:26:35 +02:00
|
|
|
incoming_valid_message = EmailMessage()
|
2019-01-15 20:19:49 +01:00
|
|
|
with open(os.path.join(settings.DEPLOY_ROOT, "static/images/default-avatar.png"), 'rb') as f:
|
2020-06-05 23:26:35 +02:00
|
|
|
incoming_valid_message.add_attachment(
|
|
|
|
f.read(),
|
|
|
|
maintype="image",
|
|
|
|
subtype="png",
|
|
|
|
)
|
2019-01-15 20:19:49 +01:00
|
|
|
|
|
|
|
incoming_valid_message['Subject'] = 'TestStreamEmailMessages Subject'
|
|
|
|
incoming_valid_message['From'] = self.example_email('hamlet')
|
|
|
|
incoming_valid_message['To'] = stream_to_address
|
|
|
|
incoming_valid_message['Reply-to'] = self.example_email('othello')
|
|
|
|
|
2019-03-22 10:06:44 +01:00
|
|
|
with mock.patch('zerver.lib.email_mirror.logging.warning') as mock_warn:
|
|
|
|
process_message(incoming_valid_message)
|
|
|
|
mock_warn.assert_called_with("Unable to find plaintext or HTML message body")
|
2015-10-31 16:31:17 +01:00
|
|
|
|
2019-03-21 14:19:55 +01:00
|
|
|
def test_receive_stream_email_messages_empty_body_after_stripping(self) -> None:
|
|
|
|
user_profile = self.example_user('hamlet')
|
2020-03-06 18:40:46 +01:00
|
|
|
self.login_user(user_profile)
|
2019-03-21 14:19:55 +01:00
|
|
|
self.subscribe(user_profile, "Denmark")
|
|
|
|
stream = get_stream("Denmark", user_profile.realm)
|
|
|
|
|
|
|
|
stream_to_address = encode_email_address(stream)
|
|
|
|
headers = {}
|
|
|
|
headers['Reply-To'] = self.example_email('othello')
|
|
|
|
|
|
|
|
# empty body
|
2020-06-05 23:26:35 +02:00
|
|
|
incoming_valid_message = EmailMessage()
|
|
|
|
incoming_valid_message.set_content('-- \nFooter')
|
2019-03-21 14:19:55 +01:00
|
|
|
|
|
|
|
incoming_valid_message['Subject'] = 'TestStreamEmailMessages Subject'
|
|
|
|
incoming_valid_message['From'] = self.example_email('hamlet')
|
|
|
|
incoming_valid_message['To'] = stream_to_address
|
|
|
|
incoming_valid_message['Reply-to'] = self.example_email('othello')
|
|
|
|
|
|
|
|
process_message(incoming_valid_message)
|
|
|
|
message = most_recent_message(user_profile)
|
|
|
|
|
|
|
|
self.assertEqual(message.content, "(No email body)")
|
|
|
|
|
2019-09-21 01:36:43 +02:00
|
|
|
class TestMissedMessageEmailMessages(ZulipTestCase):
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_receive_missed_personal_message_email_messages(self) -> None:
|
2015-10-31 16:31:17 +01:00
|
|
|
|
|
|
|
# build dummy messages for missed messages email reply
|
|
|
|
# have Hamlet send Othello a PM. Othello will reply via email
|
|
|
|
# Hamlet will receive the message.
|
2020-03-06 18:40:46 +01:00
|
|
|
self.login('hamlet')
|
2020-03-12 14:17:25 +01:00
|
|
|
othello = self.example_user('othello')
|
2016-07-28 00:30:22 +02:00
|
|
|
result = self.client_post("/json/messages", {"type": "private",
|
2015-12-01 00:18:33 +01:00
|
|
|
"content": "test_receive_missed_message_email_messages",
|
|
|
|
"client": "test suite",
|
2020-08-07 01:09:47 +02:00
|
|
|
"to": orjson.dumps([othello.id]).decode()})
|
2015-10-31 16:31:17 +01:00
|
|
|
self.assert_json_success(result)
|
|
|
|
|
2017-05-07 17:21:26 +02:00
|
|
|
user_profile = self.example_user('othello')
|
2015-10-31 16:31:17 +01:00
|
|
|
usermessage = most_recent_usermessage(user_profile)
|
|
|
|
|
|
|
|
# we don't want to send actual emails but we do need to create and store the
|
|
|
|
# token for looking up who did reply.
|
|
|
|
mm_address = create_missed_message_address(user_profile, usermessage.message)
|
|
|
|
|
2020-06-05 23:26:35 +02:00
|
|
|
incoming_valid_message = EmailMessage()
|
|
|
|
incoming_valid_message.set_content('TestMissedMessageEmailMessages Body')
|
2015-10-31 16:31:17 +01:00
|
|
|
|
|
|
|
incoming_valid_message['Subject'] = 'TestMissedMessageEmailMessages Subject'
|
2017-05-24 05:08:49 +02:00
|
|
|
incoming_valid_message['From'] = self.example_email('othello')
|
2015-10-31 16:31:17 +01:00
|
|
|
incoming_valid_message['To'] = mm_address
|
2017-05-24 05:08:49 +02:00
|
|
|
incoming_valid_message['Reply-to'] = self.example_email('othello')
|
2015-10-31 16:31:17 +01:00
|
|
|
|
|
|
|
process_message(incoming_valid_message)
|
|
|
|
|
|
|
|
# confirm that Hamlet got the message
|
2017-05-07 17:21:26 +02:00
|
|
|
user_profile = self.example_user('hamlet')
|
2015-10-31 16:31:17 +01:00
|
|
|
message = most_recent_message(user_profile)
|
|
|
|
|
|
|
|
self.assertEqual(message.content, "TestMissedMessageEmailMessages Body")
|
2017-05-07 17:21:26 +02:00
|
|
|
self.assertEqual(message.sender, self.example_user('othello'))
|
2015-10-31 16:31:17 +01:00
|
|
|
self.assertEqual(message.recipient.id, user_profile.id)
|
|
|
|
self.assertEqual(message.recipient.type, Recipient.PERSONAL)
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_receive_missed_huddle_message_email_messages(self) -> None:
|
2015-10-31 16:31:17 +01:00
|
|
|
|
|
|
|
# build dummy messages for missed messages email reply
|
|
|
|
# have Othello send Iago and Cordelia a PM. Cordelia will reply via email
|
|
|
|
# Iago and Othello will receive the message.
|
2020-03-06 18:40:46 +01:00
|
|
|
self.login('othello')
|
2020-03-12 14:17:25 +01:00
|
|
|
cordelia = self.example_user('cordelia')
|
|
|
|
iago = self.example_user('iago')
|
2016-07-28 00:30:22 +02:00
|
|
|
result = self.client_post("/json/messages", {"type": "private",
|
2015-12-01 00:18:33 +01:00
|
|
|
"content": "test_receive_missed_message_email_messages",
|
|
|
|
"client": "test suite",
|
2020-08-07 01:09:47 +02:00
|
|
|
"to": orjson.dumps([cordelia.id, iago.id]).decode()})
|
2015-10-31 16:31:17 +01:00
|
|
|
self.assert_json_success(result)
|
|
|
|
|
2017-05-07 17:21:26 +02:00
|
|
|
user_profile = self.example_user('cordelia')
|
2015-10-31 16:31:17 +01:00
|
|
|
usermessage = most_recent_usermessage(user_profile)
|
|
|
|
|
|
|
|
# we don't want to send actual emails but we do need to create and store the
|
|
|
|
# token for looking up who did reply.
|
|
|
|
mm_address = create_missed_message_address(user_profile, usermessage.message)
|
|
|
|
|
2020-06-05 23:26:35 +02:00
|
|
|
incoming_valid_message = EmailMessage()
|
|
|
|
incoming_valid_message.set_content('TestMissedHuddleMessageEmailMessages Body')
|
2015-10-31 16:31:17 +01:00
|
|
|
|
|
|
|
incoming_valid_message['Subject'] = 'TestMissedHuddleMessageEmailMessages Subject'
|
2017-05-24 05:08:49 +02:00
|
|
|
incoming_valid_message['From'] = self.example_email('cordelia')
|
2015-10-31 16:31:17 +01:00
|
|
|
incoming_valid_message['To'] = mm_address
|
2017-05-24 05:08:49 +02:00
|
|
|
incoming_valid_message['Reply-to'] = self.example_email('cordelia')
|
2015-10-31 16:31:17 +01:00
|
|
|
|
|
|
|
process_message(incoming_valid_message)
|
|
|
|
|
|
|
|
# Confirm Iago received the message.
|
2017-05-07 17:21:26 +02:00
|
|
|
user_profile = self.example_user('iago')
|
2015-10-31 16:31:17 +01:00
|
|
|
message = most_recent_message(user_profile)
|
|
|
|
|
|
|
|
self.assertEqual(message.content, "TestMissedHuddleMessageEmailMessages Body")
|
2017-05-07 17:21:26 +02:00
|
|
|
self.assertEqual(message.sender, self.example_user('cordelia'))
|
2015-10-31 16:31:17 +01:00
|
|
|
self.assertEqual(message.recipient.type, Recipient.HUDDLE)
|
|
|
|
|
|
|
|
# Confirm Othello received the message.
|
2017-05-07 17:21:26 +02:00
|
|
|
user_profile = self.example_user('othello')
|
2015-10-31 16:31:17 +01:00
|
|
|
message = most_recent_message(user_profile)
|
|
|
|
|
|
|
|
self.assertEqual(message.content, "TestMissedHuddleMessageEmailMessages Body")
|
2017-05-07 17:21:26 +02:00
|
|
|
self.assertEqual(message.sender, self.example_user('cordelia'))
|
2015-10-31 16:31:17 +01:00
|
|
|
self.assertEqual(message.recipient.type, Recipient.HUDDLE)
|
2016-05-12 20:35:35 +02:00
|
|
|
|
2019-03-21 16:20:36 +01:00
|
|
|
def test_receive_missed_stream_message_email_messages(self) -> None:
|
|
|
|
# build dummy messages for missed messages email reply
|
|
|
|
# have Hamlet send a message to stream Denmark, that Othello
|
|
|
|
# will receive a missed message email about.
|
|
|
|
# Othello will reply via email.
|
|
|
|
# Hamlet will see the message in the stream.
|
|
|
|
self.subscribe(self.example_user("hamlet"), "Denmark")
|
|
|
|
self.subscribe(self.example_user("othello"), "Denmark")
|
2020-03-06 18:40:46 +01:00
|
|
|
self.login('hamlet')
|
2019-03-21 16:20:36 +01:00
|
|
|
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')
|
|
|
|
usermessage = most_recent_usermessage(user_profile)
|
|
|
|
|
|
|
|
mm_address = create_missed_message_address(user_profile, usermessage.message)
|
|
|
|
|
2020-06-05 23:26:35 +02:00
|
|
|
incoming_valid_message = EmailMessage()
|
|
|
|
incoming_valid_message.set_content('TestMissedMessageEmailMessages Body')
|
2019-03-21 16:20:36 +01:00
|
|
|
|
|
|
|
incoming_valid_message['Subject'] = 'TestMissedMessageEmailMessages Subject'
|
2020-03-12 14:17:25 +01:00
|
|
|
incoming_valid_message['From'] = user_profile.delivery_email
|
2019-03-21 16:20:36 +01:00
|
|
|
incoming_valid_message['To'] = mm_address
|
2020-03-12 14:17:25 +01:00
|
|
|
incoming_valid_message['Reply-to'] = user_profile.delivery_email
|
2019-03-21 16:20:36 +01:00
|
|
|
|
|
|
|
process_message(incoming_valid_message)
|
|
|
|
|
|
|
|
# confirm that Hamlet got the message
|
|
|
|
user_profile = self.example_user('hamlet')
|
|
|
|
message = most_recent_message(user_profile)
|
|
|
|
|
|
|
|
self.assertEqual(message.content, "TestMissedMessageEmailMessages Body")
|
|
|
|
self.assertEqual(message.sender, self.example_user('othello'))
|
|
|
|
self.assertEqual(message.recipient.type, Recipient.STREAM)
|
|
|
|
self.assertEqual(message.recipient.id, usermessage.message.recipient.id)
|
|
|
|
|
2019-12-26 13:46:55 +01:00
|
|
|
def test_missed_stream_message_email_response_tracks_topic_change(self) -> None:
|
|
|
|
self.subscribe(self.example_user("hamlet"), "Denmark")
|
|
|
|
self.subscribe(self.example_user("othello"), "Denmark")
|
2020-03-06 18:40:46 +01:00
|
|
|
self.login('hamlet')
|
2019-12-26 13:46:55 +01:00
|
|
|
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')
|
|
|
|
usermessage = most_recent_usermessage(user_profile)
|
|
|
|
|
|
|
|
mm_address = create_missed_message_address(user_profile, usermessage.message)
|
|
|
|
|
|
|
|
# The mm address has been generated, now we change the topic of the message and see
|
|
|
|
# if the response to the mm address will be correctly posted with the updated topic.
|
|
|
|
usermessage.message.subject = "updated topic"
|
|
|
|
usermessage.message.save(update_fields=["subject"])
|
|
|
|
|
2020-06-05 23:26:35 +02:00
|
|
|
incoming_valid_message = EmailMessage()
|
|
|
|
incoming_valid_message.set_content('TestMissedMessageEmailMessages Body')
|
2019-12-26 13:46:55 +01:00
|
|
|
|
|
|
|
incoming_valid_message['Subject'] = 'TestMissedMessageEmailMessages Subject'
|
2020-03-12 14:17:25 +01:00
|
|
|
incoming_valid_message['From'] = user_profile.delivery_email
|
2019-12-26 13:46:55 +01:00
|
|
|
incoming_valid_message['To'] = mm_address
|
2020-03-12 14:17:25 +01:00
|
|
|
incoming_valid_message['Reply-to'] = user_profile.delivery_email
|
2019-12-26 13:46:55 +01:00
|
|
|
|
|
|
|
process_message(incoming_valid_message)
|
|
|
|
|
|
|
|
# confirm that Hamlet got the message
|
|
|
|
user_profile = self.example_user('hamlet')
|
|
|
|
message = most_recent_message(user_profile)
|
|
|
|
|
|
|
|
self.assertEqual(message.subject, "updated topic")
|
|
|
|
self.assertEqual(message.content, "TestMissedMessageEmailMessages Body")
|
|
|
|
self.assertEqual(message.sender, self.example_user('othello'))
|
|
|
|
self.assertEqual(message.recipient.type, Recipient.STREAM)
|
|
|
|
self.assertEqual(message.recipient.id, usermessage.message.recipient.id)
|
|
|
|
|
2019-09-21 02:00:00 +02:00
|
|
|
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")
|
2020-03-06 18:40:46 +01:00
|
|
|
self.login('hamlet')
|
2019-09-21 02:00:00 +02:00
|
|
|
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)
|
|
|
|
|
2020-06-05 23:26:35 +02:00
|
|
|
incoming_valid_message = EmailMessage()
|
|
|
|
incoming_valid_message.set_content('TestMissedMessageEmailMessages Body')
|
2019-09-21 02:00:00 +02:00
|
|
|
|
|
|
|
incoming_valid_message['Subject'] = 'TestMissedMessageEmailMessages Subject'
|
2020-03-12 14:17:25 +01:00
|
|
|
incoming_valid_message['From'] = user_profile.delivery_email
|
2019-09-21 02:00:00 +02:00
|
|
|
incoming_valid_message['To'] = mm_address
|
2020-03-12 14:17:25 +01:00
|
|
|
incoming_valid_message['Reply-to'] = user_profile.delivery_email
|
2019-09-21 02:00:00 +02:00
|
|
|
|
|
|
|
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")
|
2020-03-06 18:40:46 +01:00
|
|
|
self.login('hamlet')
|
2019-09-21 02:00:00 +02:00
|
|
|
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)
|
|
|
|
|
2020-06-05 23:26:35 +02:00
|
|
|
incoming_valid_message = EmailMessage()
|
|
|
|
incoming_valid_message.set_content('TestMissedMessageEmailMessages Body')
|
2019-09-21 02:00:00 +02:00
|
|
|
|
|
|
|
incoming_valid_message['Subject'] = 'TestMissedMessageEmailMessages Subject'
|
2020-03-12 14:17:25 +01:00
|
|
|
incoming_valid_message['From'] = user_profile.delivery_email
|
2019-09-21 02:00:00 +02:00
|
|
|
incoming_valid_message['To'] = mm_address
|
2020-03-12 14:17:25 +01:00
|
|
|
incoming_valid_message['Reply-to'] = user_profile.delivery_email
|
2019-09-21 02:00:00 +02:00
|
|
|
|
|
|
|
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())
|
|
|
|
|
2019-12-26 13:46:55 +01:00
|
|
|
def test_missed_message_email_multiple_responses(self) -> None:
|
|
|
|
self.subscribe(self.example_user("hamlet"), "Denmark")
|
|
|
|
self.subscribe(self.example_user("othello"), "Denmark")
|
2020-03-06 18:40:46 +01:00
|
|
|
self.login('hamlet')
|
2019-12-26 13:46:55 +01:00
|
|
|
|
|
|
|
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)
|
2020-06-05 23:26:35 +02:00
|
|
|
incoming_valid_message = EmailMessage()
|
|
|
|
incoming_valid_message.set_content('TestMissedMessageEmailMessages Body')
|
2019-12-26 13:46:55 +01:00
|
|
|
|
|
|
|
incoming_valid_message['Subject'] = 'TestMissedMessageEmailMessages Subject'
|
2020-03-12 14:17:25 +01:00
|
|
|
incoming_valid_message['From'] = user_profile.delivery_email
|
2019-12-26 13:46:55 +01:00
|
|
|
incoming_valid_message['To'] = mm_address
|
2020-03-12 14:17:25 +01:00
|
|
|
incoming_valid_message['Reply-to'] = user_profile.delivery_email
|
2019-12-26 13:46:55 +01:00
|
|
|
|
|
|
|
for i in range(0, MissedMessageEmailAddress.ALLOWED_USES):
|
|
|
|
process_missed_message(mm_address, incoming_valid_message)
|
|
|
|
|
|
|
|
with self.assertRaises(ZulipEmailForwardError):
|
|
|
|
process_missed_message(mm_address, incoming_valid_message)
|
|
|
|
|
2017-05-23 21:57:22 +02:00
|
|
|
class TestEmptyGatewaySetting(ZulipTestCase):
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_missed_message(self) -> None:
|
2020-03-06 18:40:46 +01:00
|
|
|
self.login('othello')
|
2020-03-12 14:17:25 +01:00
|
|
|
cordelia = self.example_user('cordelia')
|
|
|
|
iago = self.example_user('iago')
|
|
|
|
payload = dict(
|
|
|
|
type="private",
|
|
|
|
content="test_receive_missed_message_email_messages",
|
|
|
|
client="test suite",
|
2020-08-07 01:09:47 +02:00
|
|
|
to=orjson.dumps([cordelia.id, iago.id]).decode(),
|
2020-03-12 14:17:25 +01:00
|
|
|
)
|
|
|
|
result = self.client_post("/json/messages", payload)
|
2016-07-31 16:59:52 +02:00
|
|
|
self.assert_json_success(result)
|
|
|
|
|
2017-05-07 17:21:26 +02:00
|
|
|
user_profile = self.example_user('cordelia')
|
2016-07-31 16:59:52 +02:00
|
|
|
usermessage = most_recent_usermessage(user_profile)
|
|
|
|
with self.settings(EMAIL_GATEWAY_PATTERN=''):
|
|
|
|
mm_address = create_missed_message_address(user_profile, usermessage.message)
|
2017-06-26 19:43:32 +02:00
|
|
|
self.assertEqual(mm_address, FromAddress.NOREPLY)
|
2016-07-31 16:59:52 +02:00
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_encode_email_addr(self) -> None:
|
2017-05-23 23:26:03 +02:00
|
|
|
stream = get_stream("Denmark", get_realm("zulip"))
|
|
|
|
|
|
|
|
with self.settings(EMAIL_GATEWAY_PATTERN=''):
|
|
|
|
test_address = encode_email_address(stream)
|
|
|
|
self.assertEqual(test_address, '')
|
2016-07-31 16:59:52 +02:00
|
|
|
|
2016-08-23 02:08:42 +02:00
|
|
|
class TestReplyExtraction(ZulipTestCase):
|
2019-03-09 22:35:45 +01:00
|
|
|
def test_is_forwarded(self) -> None:
|
|
|
|
self.assertTrue(is_forwarded("FWD: hey"))
|
|
|
|
self.assertTrue(is_forwarded("fwd: hi"))
|
|
|
|
self.assertTrue(is_forwarded("[fwd] subject"))
|
|
|
|
|
|
|
|
self.assertTrue(is_forwarded("FWD: RE:"))
|
|
|
|
self.assertTrue(is_forwarded("Fwd: RE: fwd: re: subject"))
|
|
|
|
|
|
|
|
self.assertFalse(is_forwarded("subject"))
|
|
|
|
self.assertFalse(is_forwarded("RE: FWD: hi"))
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_reply_is_extracted_from_plain(self) -> None:
|
2016-07-12 14:55:02 +02:00
|
|
|
|
|
|
|
# build dummy messages for stream
|
|
|
|
# test valid incoming stream message is processed properly
|
2020-03-06 18:40:46 +01:00
|
|
|
self.login('hamlet')
|
2017-05-07 17:21:26 +02:00
|
|
|
user_profile = self.example_user('hamlet')
|
2017-08-25 06:01:29 +02:00
|
|
|
self.subscribe(user_profile, "Denmark")
|
2016-07-12 14:55:02 +02:00
|
|
|
stream = get_stream("Denmark", user_profile.realm)
|
|
|
|
|
|
|
|
stream_to_address = encode_email_address(stream)
|
2016-11-30 22:49:02 +01:00
|
|
|
text = """Reply
|
2016-07-12 14:55:02 +02:00
|
|
|
|
|
|
|
-----Original Message-----
|
|
|
|
|
|
|
|
Quote"""
|
|
|
|
|
2020-06-05 23:26:35 +02:00
|
|
|
incoming_valid_message = EmailMessage()
|
|
|
|
incoming_valid_message.set_content(text)
|
2016-07-12 14:55:02 +02:00
|
|
|
|
|
|
|
incoming_valid_message['Subject'] = 'TestStreamEmailMessages Subject'
|
2020-03-12 14:17:25 +01:00
|
|
|
incoming_valid_message['From'] = user_profile.delivery_email
|
2016-07-12 14:55:02 +02:00
|
|
|
incoming_valid_message['To'] = stream_to_address
|
2020-03-12 14:17:25 +01:00
|
|
|
incoming_valid_message['Reply-to'] = user_profile.delivery_email
|
2016-07-12 14:55:02 +02:00
|
|
|
|
|
|
|
process_message(incoming_valid_message)
|
|
|
|
|
|
|
|
# Hamlet is subscribed to this stream so should see the email message from Othello.
|
|
|
|
message = most_recent_message(user_profile)
|
|
|
|
|
|
|
|
self.assertEqual(message.content, "Reply")
|
|
|
|
|
2019-03-09 22:35:45 +01:00
|
|
|
# Don't extract if Subject indicates the email has been forwarded into the mirror:
|
|
|
|
del incoming_valid_message['Subject']
|
|
|
|
incoming_valid_message['Subject'] = 'FWD: TestStreamEmailMessages Subject'
|
|
|
|
process_message(incoming_valid_message)
|
|
|
|
message = most_recent_message(user_profile)
|
|
|
|
self.assertEqual(message.content, text)
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_reply_is_extracted_from_html(self) -> None:
|
2016-07-12 14:55:02 +02:00
|
|
|
|
|
|
|
# build dummy messages for stream
|
|
|
|
# test valid incoming stream message is processed properly
|
2020-03-06 18:40:46 +01:00
|
|
|
self.login('hamlet')
|
2017-05-07 17:21:26 +02:00
|
|
|
user_profile = self.example_user('hamlet')
|
2017-08-25 06:01:29 +02:00
|
|
|
self.subscribe(user_profile, "Denmark")
|
2016-07-12 14:55:02 +02:00
|
|
|
stream = get_stream("Denmark", user_profile.realm)
|
|
|
|
|
|
|
|
stream_to_address = encode_email_address(stream)
|
|
|
|
html = """
|
|
|
|
<html>
|
|
|
|
<body>
|
|
|
|
<p>Reply</p>
|
|
|
|
<blockquote>
|
|
|
|
|
|
|
|
<div>
|
|
|
|
On 11-Apr-2011, at 6:54 PM, Bob <bob@example.com> wrote:
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<div>
|
|
|
|
Quote
|
|
|
|
</div>
|
|
|
|
|
|
|
|
</blockquote>
|
|
|
|
</body>
|
|
|
|
</html>
|
|
|
|
"""
|
|
|
|
|
2020-06-05 23:26:35 +02:00
|
|
|
incoming_valid_message = EmailMessage()
|
|
|
|
incoming_valid_message.set_content(html, subtype="html")
|
2016-07-12 14:55:02 +02:00
|
|
|
|
|
|
|
incoming_valid_message['Subject'] = 'TestStreamEmailMessages Subject'
|
2020-03-12 14:17:25 +01:00
|
|
|
incoming_valid_message['From'] = user_profile.delivery_email
|
2016-07-12 14:55:02 +02:00
|
|
|
incoming_valid_message['To'] = stream_to_address
|
2020-03-12 14:17:25 +01:00
|
|
|
incoming_valid_message['Reply-to'] = user_profile.delivery_email
|
2016-07-12 14:55:02 +02:00
|
|
|
|
|
|
|
process_message(incoming_valid_message)
|
|
|
|
|
|
|
|
# Hamlet is subscribed to this stream so should see the email message from Othello.
|
|
|
|
message = most_recent_message(user_profile)
|
|
|
|
|
|
|
|
self.assertEqual(message.content, 'Reply')
|
2016-07-28 15:36:26 +02:00
|
|
|
|
2019-03-09 22:35:45 +01:00
|
|
|
# Don't extract if Subject indicates the email has been forwarded into the mirror:
|
|
|
|
del incoming_valid_message['Subject']
|
|
|
|
incoming_valid_message['Subject'] = 'FWD: TestStreamEmailMessages Subject'
|
|
|
|
process_message(incoming_valid_message)
|
|
|
|
message = most_recent_message(user_profile)
|
|
|
|
self.assertEqual(message.content, convert_html_to_markdown(html))
|
2016-07-28 15:36:26 +02:00
|
|
|
|
2017-05-24 05:08:49 +02:00
|
|
|
class TestScriptMTA(ZulipTestCase):
|
2017-04-18 17:28:55 +02:00
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_success(self) -> None:
|
2017-04-18 17:28:55 +02:00
|
|
|
script = os.path.join(os.path.dirname(__file__),
|
|
|
|
'../../scripts/lib/email-mirror-postfix')
|
2016-07-28 15:36:26 +02:00
|
|
|
|
2017-05-24 05:08:49 +02:00
|
|
|
sender = self.example_email('hamlet')
|
2017-01-04 05:30:48 +01:00
|
|
|
stream = get_stream("Denmark", get_realm("zulip"))
|
2016-07-28 15:36:26 +02:00
|
|
|
stream_to_address = encode_email_address(stream)
|
|
|
|
|
2018-04-20 03:57:21 +02:00
|
|
|
mail_template = self.fixture_data('simple.txt', type='email')
|
2016-07-28 15:36:26 +02:00
|
|
|
mail = mail_template.format(stream_to_address=stream_to_address, sender=sender)
|
2017-04-18 17:28:55 +02:00
|
|
|
read_pipe, write_pipe = os.pipe()
|
|
|
|
os.write(write_pipe, mail.encode())
|
|
|
|
os.close(write_pipe)
|
|
|
|
subprocess.check_call(
|
2017-12-23 20:11:50 +01:00
|
|
|
[script, '-r', stream_to_address, '-s', settings.SHARED_SECRET, '-t'],
|
2017-04-18 17:28:55 +02:00
|
|
|
stdin=read_pipe)
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_error_no_recipient(self) -> None:
|
2017-04-18 17:28:55 +02:00
|
|
|
script = os.path.join(os.path.dirname(__file__),
|
|
|
|
'../../scripts/lib/email-mirror-postfix')
|
|
|
|
|
2017-05-24 05:08:49 +02:00
|
|
|
sender = self.example_email('hamlet')
|
2017-04-18 17:28:55 +02:00
|
|
|
stream = get_stream("Denmark", get_realm("zulip"))
|
|
|
|
stream_to_address = encode_email_address(stream)
|
2018-04-20 03:57:21 +02:00
|
|
|
mail_template = self.fixture_data('simple.txt', type='email')
|
2017-04-18 17:28:55 +02:00
|
|
|
mail = mail_template.format(stream_to_address=stream_to_address, sender=sender)
|
|
|
|
read_pipe, write_pipe = os.pipe()
|
|
|
|
os.write(write_pipe, mail.encode())
|
|
|
|
os.close(write_pipe)
|
|
|
|
success_call = True
|
|
|
|
try:
|
|
|
|
subprocess.check_output([script, '-s', settings.SHARED_SECRET, '-t'],
|
|
|
|
stdin=read_pipe)
|
|
|
|
except subprocess.CalledProcessError as e:
|
|
|
|
self.assertEqual(
|
|
|
|
e.output,
|
python: Use trailing commas consistently.
Automatically generated by the following script, based on the output
of lint with flake8-comma:
import re
import sys
last_filename = None
last_row = None
lines = []
for msg in sys.stdin:
m = re.match(
r"\x1b\[35mflake8 \|\x1b\[0m \x1b\[1;31m(.+):(\d+):(\d+): (\w+)", msg
)
if m:
filename, row_str, col_str, err = m.groups()
row, col = int(row_str), int(col_str)
if filename == last_filename:
assert last_row != row
else:
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
with open(filename) as f:
lines = f.readlines()
last_filename = filename
last_row = row
line = lines[row - 1]
if err in ["C812", "C815"]:
lines[row - 1] = line[: col - 1] + "," + line[col - 1 :]
elif err in ["C819"]:
assert line[col - 2] == ","
lines[row - 1] = line[: col - 2] + line[col - 1 :].lstrip(" ")
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-10 05:23:40 +02:00
|
|
|
b'5.1.1 Bad destination mailbox address: No missed message email address.\n',
|
2017-04-18 17:28:55 +02:00
|
|
|
)
|
|
|
|
self.assertEqual(e.returncode, 67)
|
|
|
|
success_call = False
|
|
|
|
self.assertFalse(success_call)
|
|
|
|
|
|
|
|
|
|
|
|
class TestEmailMirrorTornadoView(ZulipTestCase):
|
|
|
|
|
2018-05-10 19:00:29 +02:00
|
|
|
def send_private_message(self) -> str:
|
2020-03-06 18:40:46 +01:00
|
|
|
self.login('othello')
|
2020-03-12 14:17:25 +01:00
|
|
|
cordelia = self.example_user('cordelia')
|
|
|
|
iago = self.example_user('iago')
|
2017-04-18 17:28:55 +02:00
|
|
|
result = self.client_post(
|
|
|
|
"/json/messages",
|
|
|
|
{
|
|
|
|
"type": "private",
|
|
|
|
"content": "test_receive_missed_message_email_messages",
|
|
|
|
"client": "test suite",
|
2020-08-07 01:09:47 +02:00
|
|
|
"to": orjson.dumps([cordelia.id, iago.id]).decode(),
|
2017-04-18 17:28:55 +02:00
|
|
|
})
|
|
|
|
self.assert_json_success(result)
|
|
|
|
|
2017-05-07 17:21:26 +02:00
|
|
|
user_profile = self.example_user('cordelia')
|
2017-04-18 17:28:55 +02:00
|
|
|
user_message = most_recent_usermessage(user_profile)
|
|
|
|
return create_missed_message_address(user_profile, user_message.message)
|
|
|
|
|
2020-08-14 10:03:36 +02:00
|
|
|
def send_offline_message(self, to_address: str, sender: UserProfile) -> HttpResponse:
|
2018-04-20 03:57:21 +02:00
|
|
|
mail_template = self.fixture_data('simple.txt', type='email')
|
2020-03-12 14:17:25 +01:00
|
|
|
mail = mail_template.format(stream_to_address=to_address, sender=sender.delivery_email)
|
2020-06-05 23:35:52 +02:00
|
|
|
msg_base64 = base64.b64encode(mail.encode()).decode()
|
2016-07-28 15:36:26 +02:00
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def check_queue_json_publish(queue_name: str,
|
2019-12-26 13:46:55 +01:00
|
|
|
event: Mapping[str, Any],
|
2018-03-23 23:42:54 +01:00
|
|
|
processor: Optional[Callable[[Any], None]]=None) -> None:
|
2016-07-28 15:36:26 +02:00
|
|
|
self.assertEqual(queue_name, "email_mirror")
|
2020-06-05 23:35:52 +02:00
|
|
|
self.assertEqual(event, {"rcpt_to": to_address, "msg_base64": msg_base64})
|
2019-12-26 13:46:55 +01:00
|
|
|
MirrorWorker().consume(event)
|
|
|
|
|
|
|
|
self.assertEqual(self.get_last_message().content,
|
|
|
|
"This is a plain-text message for testing Zulip.")
|
2017-04-18 17:28:55 +02:00
|
|
|
|
2020-06-05 23:35:52 +02:00
|
|
|
post_data = {
|
|
|
|
"rcpt_to": to_address,
|
|
|
|
"msg_base64": msg_base64,
|
|
|
|
"secret": settings.SHARED_SECRET,
|
2017-04-18 17:28:55 +02:00
|
|
|
}
|
2020-08-14 10:03:36 +02:00
|
|
|
|
|
|
|
with mock_queue_publish('zerver.lib.email_mirror.queue_json_publish') as m:
|
|
|
|
m.side_effect = check_queue_json_publish
|
|
|
|
return self.client_post('/email_mirror_message', post_data)
|
2017-04-18 17:28:55 +02:00
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_success_stream(self) -> None:
|
2017-04-18 17:28:55 +02:00
|
|
|
stream = get_stream("Denmark", get_realm("zulip"))
|
|
|
|
stream_to_address = encode_email_address(stream)
|
2020-03-12 14:17:25 +01:00
|
|
|
result = self.send_offline_message(stream_to_address, self.example_user('hamlet'))
|
2017-04-18 17:28:55 +02:00
|
|
|
self.assert_json_success(result)
|
2016-07-28 15:36:26 +02:00
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_error_to_stream_with_wrong_address(self) -> None:
|
2017-04-18 17:28:55 +02:00
|
|
|
stream = get_stream("Denmark", get_realm("zulip"))
|
|
|
|
stream_to_address = encode_email_address(stream)
|
2019-03-17 10:36:16 +01:00
|
|
|
# get the email_token:
|
2019-03-21 11:28:14 +01:00
|
|
|
token = decode_email_address(stream_to_address)[0]
|
2019-03-17 10:36:16 +01:00
|
|
|
stream_to_address = stream_to_address.replace(token, "Wrong_token")
|
2016-07-28 15:36:26 +02:00
|
|
|
|
2020-03-12 14:17:25 +01:00
|
|
|
result = self.send_offline_message(stream_to_address, self.example_user('hamlet'))
|
2017-04-18 17:28:55 +02:00
|
|
|
self.assert_json_error(
|
|
|
|
result,
|
|
|
|
"5.1.1 Bad destination mailbox address: "
|
2020-01-10 10:21:47 +01:00
|
|
|
"Bad stream token from email recipient " + stream_to_address)
|
2017-04-18 17:28:55 +02:00
|
|
|
|
2019-03-17 10:36:16 +01:00
|
|
|
def test_success_to_stream_with_good_token_wrong_stream_name(self) -> None:
|
|
|
|
stream = get_stream("Denmark", get_realm("zulip"))
|
|
|
|
stream_to_address = encode_email_address(stream)
|
2019-03-17 13:13:23 +01:00
|
|
|
stream_to_address = stream_to_address.replace("denmark", "Wrong_name")
|
2019-03-17 10:36:16 +01:00
|
|
|
|
2020-03-12 14:17:25 +01:00
|
|
|
result = self.send_offline_message(stream_to_address, self.example_user('hamlet'))
|
2019-03-17 10:36:16 +01:00
|
|
|
self.assert_json_success(result)
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_success_to_private(self) -> None:
|
2017-04-18 17:28:55 +02:00
|
|
|
mm_address = self.send_private_message()
|
2020-03-12 14:17:25 +01:00
|
|
|
result = self.send_offline_message(mm_address, self.example_user('cordelia'))
|
2017-04-18 17:28:55 +02:00
|
|
|
self.assert_json_success(result)
|
|
|
|
|
2019-12-26 13:46:55 +01:00
|
|
|
def test_using_mm_address_multiple_times(self) -> None:
|
2017-04-18 17:28:55 +02:00
|
|
|
mm_address = self.send_private_message()
|
2019-12-26 13:46:55 +01:00
|
|
|
for i in range(0, MissedMessageEmailAddress.ALLOWED_USES):
|
2020-03-12 14:17:25 +01:00
|
|
|
result = self.send_offline_message(mm_address, self.example_user('cordelia'))
|
2019-12-26 13:46:55 +01:00
|
|
|
self.assert_json_success(result)
|
|
|
|
|
2020-03-12 14:17:25 +01:00
|
|
|
result = self.send_offline_message(mm_address, self.example_user('cordelia'))
|
2017-04-18 17:28:55 +02:00
|
|
|
self.assert_json_error(
|
|
|
|
result,
|
2020-01-10 10:21:47 +01:00
|
|
|
"5.1.1 Bad destination mailbox address: Missed message address out of uses.")
|
2017-04-18 17:28:55 +02:00
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_wrong_missed_email_private_message(self) -> None:
|
2017-04-18 17:28:55 +02:00
|
|
|
self.send_private_message()
|
|
|
|
mm_address = 'mm' + ('x' * 32) + '@testserver'
|
2020-03-12 14:17:25 +01:00
|
|
|
result = self.send_offline_message(mm_address, self.example_user('cordelia'))
|
2017-04-18 17:28:55 +02:00
|
|
|
self.assert_json_error(
|
|
|
|
result,
|
2020-01-10 10:21:47 +01:00
|
|
|
"5.1.1 Bad destination mailbox address: Missed message address expired or doesn't exist.")
|
2019-01-04 10:46:35 +01:00
|
|
|
|
|
|
|
|
|
|
|
class TestStreamEmailMessagesSubjectStripping(ZulipTestCase):
|
2019-03-09 20:36:59 +01:00
|
|
|
def test_process_message_strips_subject(self) -> None:
|
|
|
|
user_profile = self.example_user('hamlet')
|
2020-03-06 18:40:46 +01:00
|
|
|
self.login_user(user_profile)
|
2019-03-09 20:36:59 +01:00
|
|
|
self.subscribe(user_profile, "Denmark")
|
|
|
|
stream = get_stream("Denmark", user_profile.realm)
|
|
|
|
stream_to_address = encode_email_address(stream)
|
2020-06-05 23:26:35 +02:00
|
|
|
incoming_valid_message = EmailMessage()
|
|
|
|
incoming_valid_message.set_content('TestStreamEmailMessages Body')
|
2019-03-09 20:36:59 +01:00
|
|
|
incoming_valid_message['Subject'] = "Re: Fwd: Re: Test"
|
|
|
|
incoming_valid_message['From'] = self.example_email('hamlet')
|
|
|
|
incoming_valid_message['To'] = stream_to_address
|
|
|
|
incoming_valid_message['Reply-to'] = self.example_email('othello')
|
|
|
|
|
|
|
|
process_message(incoming_valid_message)
|
|
|
|
message = most_recent_message(user_profile)
|
|
|
|
self.assertEqual("Test", message.topic_name())
|
2019-01-04 10:46:35 +01:00
|
|
|
|
2019-03-09 20:36:59 +01:00
|
|
|
# If after stripping we get an empty subject, it should get set to (no topic)
|
|
|
|
del incoming_valid_message['Subject']
|
|
|
|
incoming_valid_message['Subject'] = "Re: Fwd: Re: "
|
|
|
|
process_message(incoming_valid_message)
|
|
|
|
message = most_recent_message(user_profile)
|
|
|
|
self.assertEqual("(no topic)", message.topic_name())
|
|
|
|
|
|
|
|
def test_strip_from_subject(self) -> None:
|
2020-08-07 01:09:47 +02:00
|
|
|
subject_list = orjson.loads(self.fixture_data('subjects.json', type='email'))
|
2019-01-04 10:46:35 +01:00
|
|
|
for subject in subject_list:
|
2019-03-09 20:36:59 +01:00
|
|
|
stripped = strip_from_subject(subject['original_subject'])
|
|
|
|
self.assertEqual(stripped, subject['stripped_subject'])
|
2019-05-09 16:01:34 +02:00
|
|
|
|
|
|
|
# If the Content-Type header didn't specify a charset, the text content
|
|
|
|
# of the email used to not be properly found. Test that this is fixed:
|
|
|
|
class TestContentTypeUnspecifiedCharset(ZulipTestCase):
|
|
|
|
def test_charset_not_specified(self) -> None:
|
|
|
|
message_as_string = self.fixture_data('1.txt', type='email')
|
|
|
|
message_as_string = message_as_string.replace("Content-Type: text/plain; charset=\"us-ascii\"",
|
|
|
|
"Content-Type: text/plain")
|
2020-06-05 23:26:35 +02:00
|
|
|
incoming_message = message_from_string(message_as_string, policy=email.policy.default)
|
|
|
|
assert isinstance(incoming_message, EmailMessage) # https://github.com/python/typeshed/issues/2417
|
2019-05-09 16:01:34 +02:00
|
|
|
|
|
|
|
user_profile = self.example_user('hamlet')
|
2020-03-06 18:40:46 +01:00
|
|
|
self.login_user(user_profile)
|
2019-05-09 16:01:34 +02:00
|
|
|
self.subscribe(user_profile, "Denmark")
|
|
|
|
stream = get_stream("Denmark", user_profile.realm)
|
|
|
|
stream_to_address = encode_email_address(stream)
|
|
|
|
|
|
|
|
del incoming_message['To']
|
|
|
|
incoming_message['To'] = stream_to_address
|
|
|
|
process_message(incoming_message)
|
|
|
|
message = most_recent_message(user_profile)
|
|
|
|
|
|
|
|
self.assertEqual(message.content, "Email fixture 1.txt body")
|
2019-03-22 10:38:24 +01:00
|
|
|
|
|
|
|
class TestEmailMirrorProcessMessageNoValidRecipient(ZulipTestCase):
|
|
|
|
def test_process_message_no_valid_recipient(self) -> None:
|
2020-06-05 23:26:35 +02:00
|
|
|
incoming_valid_message = EmailMessage()
|
|
|
|
incoming_valid_message.set_content('Test Body')
|
2019-03-22 10:38:24 +01:00
|
|
|
incoming_valid_message['Subject'] = "Test Subject"
|
|
|
|
incoming_valid_message['From'] = self.example_email('hamlet')
|
|
|
|
incoming_valid_message['To'] = "address@wrongdomain, address@notzulip"
|
|
|
|
incoming_valid_message['Reply-to'] = self.example_email('othello')
|
|
|
|
|
|
|
|
with mock.patch("zerver.lib.email_mirror.log_and_report") as mock_log_and_report:
|
|
|
|
process_message(incoming_valid_message)
|
|
|
|
mock_log_and_report.assert_called_with(incoming_valid_message,
|
2019-03-22 16:33:57 +01:00
|
|
|
"Missing recipient in mirror email", None)
|
|
|
|
|
|
|
|
class TestEmailMirrorLogAndReport(ZulipTestCase):
|
|
|
|
def test_log_and_report(self) -> None:
|
|
|
|
user_profile = self.example_user('hamlet')
|
2020-03-06 18:40:46 +01:00
|
|
|
self.login_user(user_profile)
|
2019-03-22 16:33:57 +01:00
|
|
|
self.subscribe(user_profile, "errors")
|
|
|
|
stream = get_stream("Denmark", user_profile.realm)
|
|
|
|
stream_to_address = encode_email_address(stream)
|
|
|
|
|
|
|
|
address_parts = stream_to_address.split('@')
|
|
|
|
scrubbed_address = 'X'*len(address_parts[0]) + '@' + address_parts[1]
|
|
|
|
|
2020-06-05 23:26:35 +02:00
|
|
|
incoming_valid_message = EmailMessage()
|
|
|
|
incoming_valid_message.set_content('Test Body')
|
2019-03-22 16:33:57 +01:00
|
|
|
incoming_valid_message['Subject'] = "Test Subject"
|
|
|
|
incoming_valid_message['From'] = self.example_email('hamlet')
|
|
|
|
incoming_valid_message['To'] = stream_to_address
|
2020-07-18 17:12:30 +02:00
|
|
|
with self.assertLogs('zerver.lib.email_mirror', 'ERROR') as error_log:
|
|
|
|
log_and_report(incoming_valid_message, "test error message", stream_to_address)
|
|
|
|
self.assertEqual(error_log.output, [
|
|
|
|
'ERROR:zerver.lib.email_mirror:Sender: hamlet@zulip.com\nTo: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX@testserver <Address to stream id: 1>\ntest error message'
|
|
|
|
])
|
2019-03-22 16:33:57 +01:00
|
|
|
message = most_recent_message(user_profile)
|
|
|
|
|
|
|
|
self.assertEqual("email mirror error", message.topic_name())
|
|
|
|
|
|
|
|
msg_content = message.content.strip('~').strip()
|
|
|
|
expected_content = "Sender: {}\nTo: {} <Address to stream id: {}>\ntest error message"
|
|
|
|
expected_content = expected_content.format(self.example_email('hamlet'), scrubbed_address,
|
|
|
|
stream.id)
|
|
|
|
self.assertEqual(msg_content, expected_content)
|
|
|
|
|
2020-07-18 17:12:30 +02:00
|
|
|
with self.assertLogs('zerver.lib.email_mirror', 'ERROR') as error_log:
|
|
|
|
log_and_report(incoming_valid_message, "test error message", None)
|
|
|
|
self.assertEqual(error_log.output, [
|
|
|
|
'ERROR:zerver.lib.email_mirror:Sender: hamlet@zulip.com\nTo: No recipient found\ntest error message'
|
|
|
|
])
|
|
|
|
|
2019-03-22 16:33:57 +01:00
|
|
|
message = most_recent_message(user_profile)
|
|
|
|
self.assertEqual("email mirror error", message.topic_name())
|
|
|
|
msg_content = message.content.strip('~').strip()
|
|
|
|
expected_content = "Sender: {}\nTo: No recipient found\ntest error message"
|
|
|
|
expected_content = expected_content.format(self.example_email('hamlet'))
|
|
|
|
self.assertEqual(msg_content, expected_content)
|
|
|
|
|
|
|
|
@mock.patch('zerver.lib.email_mirror.logger.error')
|
|
|
|
def test_log_and_report_no_errorbot(self, mock_error: mock.MagicMock) -> None:
|
|
|
|
with self.settings(ERROR_BOT=None):
|
2020-06-05 23:26:35 +02:00
|
|
|
incoming_valid_message = EmailMessage()
|
|
|
|
incoming_valid_message.set_content('Test Body')
|
2019-03-22 16:33:57 +01:00
|
|
|
incoming_valid_message['Subject'] = "Test Subject"
|
|
|
|
incoming_valid_message['From'] = self.example_email('hamlet')
|
|
|
|
log_and_report(incoming_valid_message, "test error message", None)
|
|
|
|
|
|
|
|
expected_content = "Sender: {}\nTo: No recipient found\ntest error message"
|
|
|
|
expected_content = expected_content.format(self.example_email('hamlet'))
|
|
|
|
mock_error.assert_called_with(expected_content)
|
|
|
|
|
|
|
|
def test_redact_email_address(self) -> None:
|
|
|
|
user_profile = self.example_user('hamlet')
|
2020-03-06 18:40:46 +01:00
|
|
|
self.login_user(user_profile)
|
2019-03-22 16:33:57 +01:00
|
|
|
self.subscribe(user_profile, "errors")
|
|
|
|
stream = get_stream("Denmark", user_profile.realm)
|
|
|
|
|
|
|
|
# Test for a stream address:
|
|
|
|
stream_to_address = encode_email_address(stream)
|
|
|
|
stream_address_parts = stream_to_address.split('@')
|
|
|
|
scrubbed_stream_address = 'X'*len(stream_address_parts[0]) + '@' + stream_address_parts[1]
|
|
|
|
|
|
|
|
error_message = "test message {}"
|
|
|
|
error_message = error_message.format(stream_to_address)
|
|
|
|
expected_message = "test message {} <Address to stream id: {}>"
|
|
|
|
expected_message = expected_message.format(scrubbed_stream_address, stream.id)
|
|
|
|
|
|
|
|
redacted_message = redact_email_address(error_message)
|
|
|
|
self.assertEqual(redacted_message, expected_message)
|
|
|
|
|
|
|
|
# Test for an invalid email address:
|
|
|
|
invalid_address = "invalid@testserver"
|
|
|
|
error_message = "test message {}"
|
|
|
|
error_message = error_message.format(invalid_address)
|
|
|
|
expected_message = "test message {} <Invalid address>"
|
|
|
|
expected_message = expected_message.format('XXXXXXX@testserver')
|
|
|
|
|
|
|
|
redacted_message = redact_email_address(error_message)
|
|
|
|
self.assertEqual(redacted_message, expected_message)
|
|
|
|
|
|
|
|
# Test for a missed message address:
|
2020-03-12 14:17:25 +01:00
|
|
|
cordelia = self.example_user('cordelia')
|
|
|
|
iago = self.example_user('iago')
|
2019-03-22 16:33:57 +01:00
|
|
|
result = self.client_post(
|
|
|
|
"/json/messages",
|
|
|
|
{
|
|
|
|
"type": "private",
|
|
|
|
"content": "test_redact_email_message",
|
|
|
|
"client": "test suite",
|
2020-08-07 01:09:47 +02:00
|
|
|
"to": orjson.dumps([cordelia.email, iago.email]).decode(),
|
2019-03-22 16:33:57 +01:00
|
|
|
})
|
|
|
|
self.assert_json_success(result)
|
|
|
|
|
|
|
|
cordelia_profile = self.example_user('cordelia')
|
|
|
|
user_message = most_recent_usermessage(cordelia_profile)
|
|
|
|
mm_address = create_missed_message_address(user_profile, user_message.message)
|
|
|
|
|
|
|
|
error_message = "test message {}"
|
|
|
|
error_message = error_message.format(mm_address)
|
|
|
|
expected_message = "test message {} <Missed message address>"
|
|
|
|
expected_message = expected_message.format('X'*34 + '@testserver')
|
|
|
|
|
|
|
|
redacted_message = redact_email_address(error_message)
|
|
|
|
self.assertEqual(redacted_message, expected_message)
|
|
|
|
|
|
|
|
# Test if redacting correctly scrubs multiple occurrences of the address:
|
|
|
|
error_message = "test message first occurrence: {} second occurrence: {}"
|
|
|
|
error_message = error_message.format(stream_to_address, stream_to_address)
|
|
|
|
expected_message = "test message first occurrence: {} <Address to stream id: {}>"
|
|
|
|
expected_message += " second occurrence: {} <Address to stream id: {}>"
|
|
|
|
expected_message = expected_message.format(scrubbed_stream_address, stream.id,
|
|
|
|
scrubbed_stream_address, stream.id)
|
|
|
|
|
|
|
|
redacted_message = redact_email_address(error_message)
|
|
|
|
self.assertEqual(redacted_message, expected_message)
|
|
|
|
|
|
|
|
# Test with EMAIL_GATEWAY_EXTRA_PATTERN_HACK:
|
|
|
|
with self.settings(EMAIL_GATEWAY_EXTRA_PATTERN_HACK='@zulip.org'):
|
|
|
|
stream_to_address = stream_to_address.replace('@testserver', '@zulip.org')
|
|
|
|
scrubbed_stream_address = scrubbed_stream_address.replace('@testserver', '@zulip.org')
|
|
|
|
error_message = "test message {}"
|
|
|
|
error_message = error_message.format(stream_to_address)
|
|
|
|
expected_message = "test message {} <Address to stream id: {}>"
|
|
|
|
expected_message = expected_message.format(scrubbed_stream_address, stream.id)
|
|
|
|
|
|
|
|
redacted_message = redact_email_address(error_message)
|
|
|
|
self.assertEqual(redacted_message, expected_message)
|