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
|
2022-07-27 23:33:49 +02:00
|
|
|
from email.headerregistry import Address
|
2020-06-05 23:26:35 +02:00
|
|
|
from email.message import EmailMessage, MIMEPart
|
2022-06-08 04:52:09 +02:00
|
|
|
from typing import TYPE_CHECKING, Any, Callable, Dict, Mapping, Optional
|
2020-06-11 00:54:34 +02:00
|
|
|
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
|
2015-10-29 16:09:09 +01:00
|
|
|
|
2022-04-14 23:57:15 +02:00
|
|
|
from zerver.actions.realm_settings import do_deactivate_realm
|
2022-04-14 23:51:16 +02:00
|
|
|
from zerver.actions.streams import do_change_stream_post_policy
|
2022-04-14 23:48:28 +02:00
|
|
|
from zerver.actions.users import do_deactivate_user
|
2015-10-29 16:09:09 +01:00
|
|
|
from zerver.lib.email_mirror import (
|
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,
|
2023-04-11 19:51:14 +02:00
|
|
|
log_error,
|
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 (
|
2021-07-16 22:11:10 +02:00
|
|
|
ZulipEmailForwardError,
|
2019-03-21 10:24:56 +01:00
|
|
|
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
|
2022-04-14 23:42:50 +02:00
|
|
|
from zerver.lib.streams import ensure_stream
|
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
|
2023-12-15 03:57:04 +01:00
|
|
|
from zerver.models import Attachment, Recipient, Stream, UserProfile
|
2023-12-15 02:14:24 +01:00
|
|
|
from zerver.models.realms import get_realm
|
2023-12-15 03:57:04 +01:00
|
|
|
from zerver.models.streams import get_stream
|
2023-12-15 01:16:00 +01:00
|
|
|
from zerver.models.users import 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
|
|
|
|
2022-06-08 04:52:09 +02:00
|
|
|
if TYPE_CHECKING:
|
|
|
|
from django.test.client import _MonkeyPatchedWSGIResponse as TestHttpResponse
|
|
|
|
|
2020-08-01 18:38:54 +02:00
|
|
|
logger_name = "zerver.lib.email_mirror"
|
2015-10-29 16:09:09 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2018-08-13 18:28:24 +02:00
|
|
|
class TestEncodeDecode(ZulipTestCase):
|
2021-02-12 08:19:30 +01:00
|
|
|
def _assert_options(
|
|
|
|
self,
|
|
|
|
options: Dict[str, bool],
|
|
|
|
show_sender: bool = False,
|
|
|
|
include_footer: bool = False,
|
|
|
|
include_quotes: bool = False,
|
|
|
|
prefer_text: bool = True,
|
|
|
|
) -> None:
|
2021-02-12 08:20:45 +01:00
|
|
|
self.assertEqual(show_sender, ("show_sender" in options) and options["show_sender"])
|
2021-02-12 08:19:30 +01:00
|
|
|
self.assertEqual(
|
2021-02-12 08:20:45 +01:00
|
|
|
include_footer, ("include_footer" in options) and options["include_footer"]
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
|
|
|
self.assertEqual(
|
2021-02-12 08:20:45 +01:00
|
|
|
include_quotes, ("include_quotes" in options) and options["include_quotes"]
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
2021-02-12 08:20:45 +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:
|
2021-02-12 08:20:45 +01:00
|
|
|
realm = get_realm("zulip")
|
|
|
|
stream_name = "dev. help"
|
2021-04-02 18:11:45 +02:00
|
|
|
stream = ensure_stream(realm, stream_name, acting_user=None)
|
2018-08-13 18:28:24 +02:00
|
|
|
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.
|
2021-02-12 08:19:30 +01:00
|
|
|
email_address_all_options = (
|
|
|
|
"dev-help.{}+include-footer.show-sender+include-quotes@testserver"
|
|
|
|
)
|
2019-09-05 11:27:59 +02:00
|
|
|
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)
|
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
email_address = email_address.replace("@testserver", "@zulip.org")
|
|
|
|
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
|
|
|
|
2021-02-12 08:20:45 +01: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)
|
2021-02-12 08:19:30 +01: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):
|
2021-02-12 08:20:45 +01:00
|
|
|
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:
|
2021-02-12 08:20:45 +01:00
|
|
|
realm = get_realm("zulip")
|
|
|
|
stream_name = "Тестовы some ascii letters"
|
2021-04-02 18:11:45 +02:00
|
|
|
stream = ensure_stream(realm, stream_name, acting_user=None)
|
2019-03-17 13:13:23 +01:00
|
|
|
email_address = encode_email_address(stream)
|
|
|
|
|
|
|
|
msg_string = get_email_gateway_message_string_from_address(email_address)
|
2021-02-12 08:20:45 +01:00
|
|
|
parts = msg_string.split("+")
|
2019-03-17 13:13:23 +01:00
|
|
|
# Stream name should be completely stripped to '', so msg_string
|
|
|
|
# should only have the email_token in it.
|
2021-05-17 05:41:32 +02:00
|
|
|
self.assert_length(parts, 1)
|
2019-03-17 13:13:23 +01:00
|
|
|
|
|
|
|
# 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 = "ąężć"
|
2021-04-02 18:11:45 +02:00
|
|
|
stream = ensure_stream(realm, asciiable_stream_name, acting_user=None)
|
2019-03-17 13:13:23 +01:00
|
|
|
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)
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
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"):
|
2021-02-12 08:20:45 +01:00
|
|
|
address = "mm" + ("x" * 32) + "@example.com"
|
2019-03-22 17:05:26 +01:00
|
|
|
self.assertTrue(is_missed_message_address(address))
|
|
|
|
token = get_missed_message_token_from_address(address)
|
2021-02-12 08:20:45 +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.
|
2021-02-12 08:20:45 +01:00
|
|
|
address = "mmathers@example.com"
|
2019-03-22 17:05:26 +01:00
|
|
|
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.
|
2021-02-12 08:20:45 +01:00
|
|
|
address = "alice@not-the-domain-we-were-expecting.com"
|
2019-03-22 17:05:26 +01:00
|
|
|
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
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2019-03-21 16:35:31 +01:00
|
|
|
class TestFilterFooter(ZulipTestCase):
|
|
|
|
def test_filter_footer(self) -> None:
|
|
|
|
text = """Test message
|
2021-03-12 04:27:19 +01:00
|
|
|
--Not a delimiter--
|
|
|
|
More message
|
2019-03-21 16:35:31 +01:00
|
|
|
--
|
|
|
|
Footer"""
|
2021-03-12 04:27:19 +01:00
|
|
|
expected_output = """Test message
|
|
|
|
--Not a delimiter--
|
|
|
|
More message"""
|
2019-03-21 16:35:31 +01:00
|
|
|
result = filter_footer(text)
|
2021-03-12 04:27:19 +01:00
|
|
|
self.assertEqual(result, expected_output)
|
2019-03-21 16:35:31 +01:00
|
|
|
|
|
|
|
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)
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2016-08-23 02:08:42 +02:00
|
|
|
class TestStreamEmailMessagesSuccess(ZulipTestCase):
|
2022-07-27 13:31:38 +02:00
|
|
|
def create_incoming_valid_message(
|
|
|
|
self, msgtext: str, stream: Stream, include_quotes: bool
|
|
|
|
) -> EmailMessage:
|
2022-07-27 23:33:49 +02:00
|
|
|
address = Address(addr_spec=encode_email_address(stream))
|
|
|
|
email_username = address.username + "+show-sender"
|
2022-07-27 13:31:38 +02:00
|
|
|
if include_quotes:
|
2022-07-27 23:33:49 +02:00
|
|
|
email_username += "+include-quotes"
|
|
|
|
stream_to_address = Address(username=email_username, domain=address.domain).addr_spec
|
2022-07-27 13:31:38 +02:00
|
|
|
|
|
|
|
incoming_valid_message = EmailMessage()
|
|
|
|
incoming_valid_message.set_content(msgtext)
|
|
|
|
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")
|
|
|
|
return incoming_valid_message
|
|
|
|
|
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
|
2021-02-12 08:20:45 +01: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()
|
2021-05-10 07:02:14 +02:00
|
|
|
incoming_valid_message.set_content("TestStreamEmailMessages body")
|
2015-10-29 16:09:09 +01:00
|
|
|
|
2021-05-10 07:02:14 +02:00
|
|
|
incoming_valid_message["Subject"] = "TestStreamEmailMessages subject"
|
2021-02-12 08:20:45 +01:00
|
|
|
incoming_valid_message["From"] = self.example_email("hamlet")
|
|
|
|
incoming_valid_message["To"] = stream_to_address
|
|
|
|
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)
|
|
|
|
|
2021-05-10 07:02:14 +02:00
|
|
|
self.assertEqual(message.content, "TestStreamEmailMessages body")
|
2023-07-16 12:08:57 +02:00
|
|
|
self.assert_message_stream_name(message, stream.name)
|
2021-02-12 08:20:45 +01: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:
|
2021-02-12 08:20:45 +01:00
|
|
|
user_profile = self.example_user("hamlet")
|
2020-07-22 21:11:25 +02:00
|
|
|
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()
|
2021-05-10 07:02:14 +02:00
|
|
|
incoming_valid_message.set_content("TestStreamEmailMessages body")
|
2020-07-22 21:11:25 +02:00
|
|
|
|
2021-05-10 07:02:14 +02:00
|
|
|
incoming_valid_message["Subject"] = "TestStreamEmailMessages subject"
|
2021-02-12 08:20:45 +01:00
|
|
|
incoming_valid_message["From"] = self.example_email("hamlet")
|
2020-07-22 21:11:25 +02:00
|
|
|
# Simulate a mailing list
|
2021-02-12 08:20:45 +01:00
|
|
|
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")
|
2020-07-22 21:11:25 +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)
|
|
|
|
|
2021-05-10 07:02:14 +02:00
|
|
|
self.assertEqual(message.content, "TestStreamEmailMessages body")
|
2023-07-16 12:08:57 +02:00
|
|
|
self.assert_message_stream_name(message, stream.name)
|
2021-02-12 08:20:45 +01:00
|
|
|
self.assertEqual(message.topic_name(), incoming_valid_message["Subject"])
|
2020-07-22 21:11:25 +02:00
|
|
|
|
2018-04-26 19:17:29 +02:00
|
|
|
def test_receive_stream_email_messages_blank_subject_success(self) -> None:
|
2021-02-12 08:20:45 +01:00
|
|
|
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()
|
2021-05-10 07:02:14 +02:00
|
|
|
incoming_valid_message.set_content("TestStreamEmailMessages body")
|
2018-04-26 19:17:29 +02:00
|
|
|
|
2021-02-12 08:20:45 +01: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")
|
2018-04-26 19:17:29 +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)
|
|
|
|
|
2021-05-10 07:02:14 +02:00
|
|
|
self.assertEqual(message.content, "TestStreamEmailMessages body")
|
2023-07-16 12:08:57 +02:00
|
|
|
self.assert_message_stream_name(message, stream.name)
|
2018-04-26 19:17:29 +02:00
|
|
|
self.assertEqual(message.topic_name(), "(no topic)")
|
|
|
|
|
2023-01-24 21:45:49 +01:00
|
|
|
def test_receive_stream_email_messages_subject_with_nonprintable_chars(
|
|
|
|
self,
|
|
|
|
) -> None:
|
2022-08-20 18:11:55 +02:00
|
|
|
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"] = "Test \u0000 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.topic_name(), "Test subject")
|
|
|
|
|
|
|
|
# Now check that a subject that will be stripped to the empty string
|
|
|
|
# is handled correctly.
|
|
|
|
incoming_valid_message.replace_header("Subject", "\u0000")
|
|
|
|
process_message(incoming_valid_message)
|
|
|
|
message = most_recent_message(user_profile)
|
|
|
|
|
|
|
|
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:
|
2021-02-12 08:20:45 +01:00
|
|
|
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()
|
2021-05-10 07:02:14 +02:00
|
|
|
incoming_valid_message.set_content("TestStreamEmailMessages body")
|
2018-04-27 22:53:18 +02:00
|
|
|
|
2021-05-10 07:02:14 +02:00
|
|
|
incoming_valid_message["Subject"] = "TestStreamEmailMessages subject"
|
2021-02-12 08:20:45 +01:00
|
|
|
incoming_valid_message["From"] = self.example_email("hamlet")
|
|
|
|
incoming_valid_message["To"] = stream_to_address
|
|
|
|
incoming_valid_message["Reply-to"] = self.example_email("othello")
|
2018-04-27 22:53:18 +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)
|
|
|
|
|
2021-05-10 07:02:14 +02:00
|
|
|
self.assertEqual(message.content, "TestStreamEmailMessages body")
|
2023-07-16 12:08:57 +02:00
|
|
|
self.assert_message_stream_name(message, stream.name)
|
2021-02-12 08:20:45 +01:00
|
|
|
self.assertEqual(message.topic_name(), incoming_valid_message["Subject"])
|
2018-04-27 22:53:18 +02:00
|
|
|
|
2019-01-03 15:53:27 +01:00
|
|
|
def test_receive_stream_email_multiple_recipient_success(self) -> None:
|
2021-02-12 08:20:45 +01:00
|
|
|
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
|
2021-02-12 08:19:30 +01:00
|
|
|
stream_to_addresses = [
|
|
|
|
"A.N. Other <another@example.org>",
|
|
|
|
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()
|
2021-05-10 07:02:14 +02:00
|
|
|
incoming_valid_message.set_content("TestStreamEmailMessages body")
|
2019-01-03 15:53:27 +01:00
|
|
|
|
2021-05-10 07:02:14 +02:00
|
|
|
incoming_valid_message["Subject"] = "TestStreamEmailMessages subject"
|
2021-02-12 08:20:45 +01:00
|
|
|
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")
|
2019-01-03 15:53:27 +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)
|
|
|
|
|
2021-05-10 07:02:14 +02:00
|
|
|
self.assertEqual(message.content, "TestStreamEmailMessages body")
|
2023-07-16 12:08:57 +02:00
|
|
|
self.assert_message_stream_name(message, stream.name)
|
2021-02-12 08:20:45 +01:00
|
|
|
self.assertEqual(message.topic_name(), incoming_valid_message["Subject"])
|
2019-01-03 15:53:27 +01:00
|
|
|
|
2019-02-08 14:13:33 +01:00
|
|
|
def test_receive_stream_email_show_sender_success(self) -> None:
|
2021-02-12 08:20:45 +01:00
|
|
|
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)
|
|
|
|
|
2022-07-27 13:31:38 +02:00
|
|
|
msgtext = "TestStreamEmailMessages Body"
|
|
|
|
incoming_valid_message = self.create_incoming_valid_message(
|
|
|
|
msgtext, stream, include_quotes=False
|
|
|
|
)
|
2019-02-08 14:13:33 +01:00
|
|
|
process_message(incoming_valid_message)
|
|
|
|
message = most_recent_message(user_profile)
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
self.assertEqual(
|
|
|
|
message.content,
|
2022-07-27 13:31:38 +02:00
|
|
|
"From: {}\n{}".format(self.example_email("hamlet"), msgtext),
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
2023-07-16 12:08:57 +02:00
|
|
|
self.assert_message_stream_name(message, stream.name)
|
2021-02-12 08:20:45 +01:00
|
|
|
self.assertEqual(message.topic_name(), incoming_valid_message["Subject"])
|
2019-01-03 15:53:27 +01:00
|
|
|
|
2022-07-27 13:31:38 +02:00
|
|
|
def test_receive_stream_email_forwarded_success(self) -> None:
|
|
|
|
msgtext = """
|
|
|
|
Hello! Here is a message I am forwarding to this list.
|
|
|
|
I hope you enjoy reading it!
|
|
|
|
-Glen
|
|
|
|
|
|
|
|
From: John Doe johndoe@wherever
|
|
|
|
To: A Zulip-subscribed mailing list somelist@elsewhere
|
|
|
|
Subject: Some subject
|
|
|
|
|
|
|
|
Here is the original email. It is full of text
|
|
|
|
and other things
|
|
|
|
-John
|
|
|
|
"""
|
|
|
|
user_profile = self.example_user("hamlet")
|
|
|
|
self.login_user(user_profile)
|
|
|
|
self.subscribe(user_profile, "Denmark")
|
|
|
|
stream = get_stream("Denmark", user_profile.realm)
|
|
|
|
|
|
|
|
def send_and_check_contents(
|
|
|
|
msgtext: str, stream: Stream, include_quotes: bool, expected_body: str
|
|
|
|
) -> None:
|
|
|
|
incoming_valid_message = self.create_incoming_valid_message(
|
|
|
|
msgtext, stream, include_quotes
|
|
|
|
)
|
|
|
|
process_message(incoming_valid_message)
|
|
|
|
message = most_recent_message(user_profile)
|
|
|
|
expected = "From: {}\n{}".format(self.example_email("hamlet"), expected_body)
|
|
|
|
self.assertEqual(message.content, expected.strip())
|
2023-07-16 12:08:57 +02:00
|
|
|
self.assert_message_stream_name(message, stream.name)
|
2022-07-27 13:31:38 +02:00
|
|
|
self.assertEqual(message.topic_name(), incoming_valid_message["Subject"])
|
|
|
|
|
|
|
|
# include_quotes=True: expect the From:... to be preserved
|
|
|
|
send_and_check_contents(msgtext, stream, include_quotes=True, expected_body=msgtext)
|
|
|
|
|
|
|
|
# include_quotes=False: expect the From:... to be stripped
|
|
|
|
send_and_check_contents(
|
|
|
|
msgtext,
|
|
|
|
stream,
|
|
|
|
include_quotes=False,
|
|
|
|
expected_body="Hello! Here is a message I am forwarding to this list.\nI hope you enjoy reading it!\n-Glen",
|
|
|
|
)
|
|
|
|
|
2020-01-29 12:32:56 +01:00
|
|
|
def test_receive_stream_email_show_sender_utf8_encoded_sender(self) -> None:
|
2021-02-12 08:20:45 +01:00
|
|
|
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)
|
|
|
|
|
2022-07-27 23:33:49 +02:00
|
|
|
address = Address(addr_spec=encode_email_address(stream))
|
|
|
|
email_username = address.username + "+show-sender"
|
|
|
|
stream_to_address = Address(username=email_username, domain=address.domain).addr_spec
|
2020-01-29 12:32:56 +01:00
|
|
|
|
2020-06-05 23:26:35 +02:00
|
|
|
incoming_valid_message = EmailMessage()
|
2021-05-10 07:02:14 +02:00
|
|
|
incoming_valid_message.set_content("TestStreamEmailMessages body")
|
|
|
|
incoming_valid_message["Subject"] = "TestStreamEmailMessages subject"
|
2024-01-29 00:32:21 +01:00
|
|
|
incoming_valid_message["From"] = (
|
|
|
|
"Test =?utf-8?b?VXNlcsOzxIXEmQ==?= <=?utf-8?q?hamlet=5F=C4=99?=@zulip.com>"
|
|
|
|
)
|
2021-02-12 08:20:45 +01:00
|
|
|
incoming_valid_message["To"] = stream_to_address
|
|
|
|
incoming_valid_message["Reply-to"] = self.example_email("othello")
|
2020-01-29 12:32:56 +01:00
|
|
|
|
|
|
|
process_message(incoming_valid_message)
|
|
|
|
message = most_recent_message(user_profile)
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
self.assertEqual(
|
|
|
|
message.content,
|
|
|
|
"From: {}\n{}".format(
|
2021-05-10 07:02:14 +02:00
|
|
|
"Test Useróąę <hamlet_ę@zulip.com>", "TestStreamEmailMessages body"
|
2021-02-12 08:19:30 +01:00
|
|
|
),
|
|
|
|
)
|
2023-07-16 12:08:57 +02:00
|
|
|
self.assert_message_stream_name(message, stream.name)
|
2021-02-12 08:20:45 +01:00
|
|
|
self.assertEqual(message.topic_name(), incoming_valid_message["Subject"])
|
2020-01-29 12:32:56 +01:00
|
|
|
|
2019-06-06 12:14:12 +02:00
|
|
|
def test_receive_stream_email_include_footer_success(self) -> None:
|
2021-02-12 08:20:45 +01: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)
|
|
|
|
|
2022-07-27 23:33:49 +02:00
|
|
|
address = Address(addr_spec=encode_email_address(stream))
|
|
|
|
email_username = address.username + "+include-footer"
|
|
|
|
stream_to_address = Address(username=email_username, domain=address.domain).addr_spec
|
2019-05-26 18:07:21 +02:00
|
|
|
|
|
|
|
text = """Test message
|
|
|
|
--
|
|
|
|
Footer"""
|
|
|
|
|
2020-06-05 23:26:35 +02:00
|
|
|
incoming_valid_message = EmailMessage()
|
|
|
|
incoming_valid_message.set_content(text)
|
2021-05-10 07:02:14 +02:00
|
|
|
incoming_valid_message["Subject"] = "TestStreamEmailMessages subject"
|
2021-02-12 08:20:45 +01:00
|
|
|
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-05-26 18:28:39 +02:00
|
|
|
|
|
|
|
process_message(incoming_valid_message)
|
|
|
|
message = most_recent_message(user_profile)
|
|
|
|
|
|
|
|
self.assertEqual(message.content, text)
|
2023-07-16 12:08:57 +02:00
|
|
|
self.assert_message_stream_name(message, stream.name)
|
2021-02-12 08:20:45 +01:00
|
|
|
self.assertEqual(message.topic_name(), incoming_valid_message["Subject"])
|
2019-05-26 18:28:39 +02:00
|
|
|
|
2019-07-14 03:51:53 +02:00
|
|
|
def test_receive_stream_email_include_quotes_success(self) -> None:
|
2021-02-12 08:20:45 +01: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)
|
|
|
|
|
2022-07-27 23:33:49 +02:00
|
|
|
address = Address(addr_spec=encode_email_address(stream))
|
|
|
|
email_username = address.username + "+include-quotes"
|
|
|
|
stream_to_address = Address(username=email_username, domain=address.domain).addr_spec
|
2019-05-26 18:28:39 +02:00
|
|
|
|
|
|
|
text = """Reply
|
|
|
|
|
|
|
|
-----Original Message-----
|
|
|
|
|
|
|
|
Quote"""
|
|
|
|
|
2020-06-05 23:26:35 +02:00
|
|
|
incoming_valid_message = EmailMessage()
|
|
|
|
incoming_valid_message.set_content(text)
|
2021-05-10 07:02:14 +02:00
|
|
|
incoming_valid_message["Subject"] = "TestStreamEmailMessages subject"
|
2021-02-12 08:20:45 +01:00
|
|
|
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-05-26 18:07:21 +02:00
|
|
|
|
|
|
|
process_message(incoming_valid_message)
|
|
|
|
message = most_recent_message(user_profile)
|
|
|
|
|
|
|
|
self.assertEqual(message.content, text)
|
2023-07-16 12:08:57 +02:00
|
|
|
self.assert_message_stream_name(message, stream.name)
|
2021-02-12 08:20:45 +01:00
|
|
|
self.assertEqual(message.topic_name(), incoming_valid_message["Subject"])
|
2019-05-26 18:07:21 +02:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2019-03-22 09:22:52 +01:00
|
|
|
class TestEmailMirrorMessagesWithAttachments(ZulipTestCase):
|
|
|
|
def test_message_with_valid_attachment(self) -> None:
|
2021-02-12 08:20:45 +01:00
|
|
|
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")
|
2021-02-12 08:19:30 +01:00
|
|
|
with open(
|
2021-02-12 08:20:45 +01:00
|
|
|
os.path.join(settings.DEPLOY_ROOT, "static/images/default-avatar.png"), "rb"
|
2021-02-12 08:19:30 +01:00
|
|
|
) as f:
|
2019-03-22 09:22:52 +01:00
|
|
|
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
|
|
|
|
2021-05-10 07:02:14 +02:00
|
|
|
incoming_valid_message["Subject"] = "TestStreamEmailMessages subject"
|
2021-02-12 08:20:45 +01:00
|
|
|
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 09:22:52 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
with mock.patch(
|
2023-02-28 03:46:41 +01:00
|
|
|
"zerver.lib.email_mirror.upload_message_attachment",
|
2023-01-24 21:45:49 +01:00
|
|
|
return_value="https://test_url",
|
2023-02-28 03:46:41 +01:00
|
|
|
) as upload_message_attachment:
|
2019-03-22 09:22:52 +01:00
|
|
|
process_message(incoming_valid_message)
|
2023-02-28 03:46:41 +01:00
|
|
|
upload_message_attachment.assert_called_with(
|
2021-02-12 08:20:45 +01:00
|
|
|
"image.png",
|
2021-02-12 08:19:30 +01:00
|
|
|
len(image_bytes),
|
2021-02-12 08:20:45 +01:00
|
|
|
"image/png",
|
2021-02-12 08:19:30 +01:00
|
|
|
image_bytes,
|
2021-03-07 20:35:35 +01:00
|
|
|
get_system_bot(settings.EMAIL_GATEWAY_BOT, stream.realm_id),
|
2021-02-12 08:19:30 +01:00
|
|
|
target_realm=user_profile.realm,
|
|
|
|
)
|
2020-01-14 16:33:48 +01:00
|
|
|
|
|
|
|
message = most_recent_message(user_profile)
|
2023-01-24 21:45:49 +01:00
|
|
|
self.assertEqual(message.content, "Test body\n\n[image.png](https://test_url)")
|
2020-01-14 16:33:48 +01:00
|
|
|
|
2022-01-26 20:17:12 +01:00
|
|
|
def test_message_with_valid_attachment_model_attributes_set_correctly(self) -> None:
|
|
|
|
"""
|
|
|
|
Verifies that the Attachment attributes are set correctly.
|
|
|
|
"""
|
|
|
|
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("Test body")
|
|
|
|
with open(
|
|
|
|
os.path.join(settings.DEPLOY_ROOT, "static/images/default-avatar.png"), "rb"
|
|
|
|
) as f:
|
|
|
|
image_bytes = f.read()
|
|
|
|
|
|
|
|
incoming_valid_message.add_attachment(
|
|
|
|
image_bytes,
|
|
|
|
maintype="image",
|
|
|
|
subtype="png",
|
|
|
|
filename="image.png",
|
|
|
|
)
|
|
|
|
|
|
|
|
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)
|
|
|
|
attachment = Attachment.objects.last()
|
2022-05-31 01:34:34 +02:00
|
|
|
assert attachment is not None
|
2022-01-26 20:17:12 +01:00
|
|
|
self.assertEqual(list(attachment.messages.values_list("id", flat=True)), [message.id])
|
|
|
|
self.assertEqual(
|
|
|
|
message.sender, get_system_bot(settings.EMAIL_GATEWAY_BOT, stream.realm_id)
|
|
|
|
)
|
|
|
|
self.assertEqual(attachment.realm, stream.realm)
|
|
|
|
self.assertEqual(attachment.is_realm_public, True)
|
|
|
|
|
2023-01-24 21:45:49 +01:00
|
|
|
def test_message_with_attachment_long_body(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("a" * settings.MAX_MESSAGE_LENGTH)
|
|
|
|
with open(
|
|
|
|
os.path.join(settings.DEPLOY_ROOT, "static/images/default-avatar.png"), "rb"
|
|
|
|
) as f:
|
|
|
|
image_bytes = f.read()
|
|
|
|
|
|
|
|
incoming_valid_message.add_attachment(
|
|
|
|
image_bytes,
|
|
|
|
maintype="image",
|
|
|
|
subtype="png",
|
|
|
|
filename="image.png",
|
|
|
|
)
|
|
|
|
|
|
|
|
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)
|
|
|
|
attachment = Attachment.objects.last()
|
|
|
|
assert attachment is not None
|
|
|
|
self.assertEqual(list(attachment.messages.values_list("id", flat=True)), [message.id])
|
|
|
|
self.assertEqual(
|
|
|
|
message.sender, get_system_bot(settings.EMAIL_GATEWAY_BOT, stream.realm_id)
|
|
|
|
)
|
|
|
|
self.assertEqual(attachment.realm, stream.realm)
|
|
|
|
self.assertEqual(attachment.is_realm_public, True)
|
|
|
|
|
|
|
|
assert message.content.endswith(
|
|
|
|
f"aaaaaa\n[message truncated]\n[image.png](/user_uploads/{attachment.path_id})"
|
|
|
|
)
|
|
|
|
|
2020-01-30 15:37:33 +01:00
|
|
|
def test_message_with_attachment_utf8_filename(self) -> None:
|
2021-02-12 08:20:45 +01:00
|
|
|
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")
|
2021-02-12 08:19:30 +01:00
|
|
|
with open(
|
2021-02-12 08:20:45 +01:00
|
|
|
os.path.join(settings.DEPLOY_ROOT, "static/images/default-avatar.png"), "rb"
|
2021-02-12 08:19:30 +01:00
|
|
|
) as f:
|
2020-01-30 15:37:33 +01:00
|
|
|
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
|
|
|
|
2021-05-10 07:02:14 +02:00
|
|
|
incoming_valid_message["Subject"] = "TestStreamEmailMessages subject"
|
2021-02-12 08:20:45 +01:00
|
|
|
incoming_valid_message["From"] = self.example_email("hamlet")
|
|
|
|
incoming_valid_message["To"] = stream_to_address
|
|
|
|
incoming_valid_message["Reply-to"] = self.example_email("othello")
|
2020-01-30 15:37:33 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
with mock.patch(
|
2023-02-28 03:46:41 +01:00
|
|
|
"zerver.lib.email_mirror.upload_message_attachment",
|
2023-01-24 21:45:49 +01:00
|
|
|
return_value="https://test_url",
|
2023-02-28 03:46:41 +01:00
|
|
|
) as upload_message_attachment:
|
2020-01-30 15:37:33 +01:00
|
|
|
process_message(incoming_valid_message)
|
2023-02-28 03:46:41 +01:00
|
|
|
upload_message_attachment.assert_called_with(
|
2021-02-12 08:19:30 +01:00
|
|
|
utf8_filename,
|
|
|
|
len(image_bytes),
|
2021-02-12 08:20:45 +01:00
|
|
|
"image/png",
|
2021-02-12 08:19:30 +01:00
|
|
|
image_bytes,
|
2021-03-07 20:35:35 +01:00
|
|
|
get_system_bot(settings.EMAIL_GATEWAY_BOT, stream.realm_id),
|
2021-02-12 08:19:30 +01:00
|
|
|
target_realm=user_profile.realm,
|
|
|
|
)
|
2020-01-30 15:37:33 +01:00
|
|
|
|
|
|
|
message = most_recent_message(user_profile)
|
2023-01-24 21:45:49 +01:00
|
|
|
self.assertEqual(message.content, f"Test body\n\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:
|
2021-02-12 08:20:45 +01:00
|
|
|
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.")
|
2021-02-12 08:19:30 +01:00
|
|
|
with open(
|
2021-02-12 08:20:45 +01:00
|
|
|
os.path.join(settings.DEPLOY_ROOT, "static/images/default-avatar.png"), "rb"
|
2021-02-12 08:19:30 +01:00
|
|
|
) as f:
|
2020-01-14 16:33:48 +01:00
|
|
|
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
|
|
|
|
2021-02-12 08:20:45 +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")
|
2020-01-14 16:33:48 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
with mock.patch(
|
2023-02-28 03:46:41 +01:00
|
|
|
"zerver.lib.email_mirror.upload_message_attachment",
|
2023-01-24 21:45:49 +01:00
|
|
|
return_value="https://test_url",
|
2023-02-28 03:46:41 +01:00
|
|
|
) as upload_message_attachment:
|
2020-01-14 16:33:48 +01:00
|
|
|
process_message(incoming_valid_message)
|
2023-02-28 03:46:41 +01:00
|
|
|
upload_message_attachment.assert_called_with(
|
2021-02-12 08:20:45 +01:00
|
|
|
"image.png",
|
2021-02-12 08:19:30 +01:00
|
|
|
len(image_bytes),
|
2021-02-12 08:20:45 +01:00
|
|
|
"image/png",
|
2021-02-12 08:19:30 +01:00
|
|
|
image_bytes,
|
2021-03-07 20:35:35 +01:00
|
|
|
get_system_bot(settings.EMAIL_GATEWAY_BOT, stream.realm_id),
|
2021-02-12 08:19:30 +01:00
|
|
|
target_realm=user_profile.realm,
|
|
|
|
)
|
2019-03-22 09:22:52 +01:00
|
|
|
|
|
|
|
message = most_recent_message(user_profile)
|
2023-01-24 21:45:49 +01:00
|
|
|
self.assertEqual(message.content, "Test body\n\n[image.png](https://test_url)")
|
2019-03-22 09:22:52 +01:00
|
|
|
|
|
|
|
def test_message_with_invalid_attachment(self) -> None:
|
2021-02-12 08:20:45 +01:00
|
|
|
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()
|
2021-02-12 08:20:45 +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
|
|
|
|
2021-05-10 07:02:14 +02:00
|
|
|
incoming_valid_message["Subject"] = "TestStreamEmailMessages subject"
|
2021-02-12 08:20:45 +01:00
|
|
|
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 09:22:52 +01:00
|
|
|
|
2020-08-01 18:38:54 +02:00
|
|
|
with self.assertLogs(logger_name, level="WARNING") as m:
|
2019-03-22 09:22:52 +01:00
|
|
|
process_message(incoming_valid_message)
|
2020-08-01 18:38:54 +02:00
|
|
|
self.assertEqual(
|
|
|
|
m.output,
|
2021-02-12 08:19:30 +01:00
|
|
|
[
|
|
|
|
"WARNING:{}:Payload is not bytes (invalid attachment {} in message from {}).".format(
|
2021-02-12 08:20:45 +01:00
|
|
|
logger_name, "some_attachment", self.example_email("hamlet")
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
|
|
|
],
|
2020-08-01 18:38:54 +02:00
|
|
|
)
|
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:
|
2021-02-12 08:20:45 +01:00
|
|
|
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
|
|
|
|
2021-05-10 07:02:14 +02:00
|
|
|
incoming_valid_message["Subject"] = "TestStreamEmailMessages subject"
|
2021-02-12 08:20:45 +01:00
|
|
|
incoming_valid_message["From"] = self.example_email("hamlet")
|
|
|
|
incoming_valid_message["To"] = stream_address
|
|
|
|
incoming_valid_message["Reply-to"] = self.example_email("othello")
|
2020-01-15 16:28:46 +01:00
|
|
|
|
|
|
|
process_message(incoming_valid_message)
|
|
|
|
message = most_recent_message(user_profile)
|
|
|
|
|
|
|
|
self.assertEqual(message.content, "Test message")
|
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
del incoming_valid_message["To"]
|
|
|
|
incoming_valid_message["To"] = stream_address_prefer_html
|
2020-01-15 16:28:46 +01:00
|
|
|
|
|
|
|
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:
|
2021-02-12 08:20:45 +01:00
|
|
|
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
|
|
|
|
2021-05-10 07:02:14 +02:00
|
|
|
incoming_valid_message["Subject"] = "TestStreamEmailMessages subject"
|
2021-02-12 08:20:45 +01:00
|
|
|
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")
|
2020-01-16 13:07:04 +01:00
|
|
|
|
|
|
|
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")
|
|
|
|
|
2023-01-18 18:38:21 +01:00
|
|
|
def test_message_with_valid_attachment_missed_message(self) -> None:
|
|
|
|
user_profile = self.example_user("othello")
|
|
|
|
usermessage = most_recent_usermessage(user_profile)
|
|
|
|
mm_address = create_missed_message_address(user_profile, usermessage.message)
|
|
|
|
|
|
|
|
incoming_valid_message = EmailMessage()
|
|
|
|
incoming_valid_message.set_content("Test body")
|
|
|
|
with open(
|
|
|
|
os.path.join(settings.DEPLOY_ROOT, "static/images/default-avatar.png"), "rb"
|
|
|
|
) as f:
|
|
|
|
image_bytes = f.read()
|
|
|
|
|
|
|
|
incoming_valid_message.add_attachment(
|
|
|
|
image_bytes,
|
|
|
|
maintype="image",
|
|
|
|
subtype="png",
|
|
|
|
filename="image.png",
|
|
|
|
)
|
|
|
|
|
|
|
|
incoming_valid_message["Subject"] = "TestStreamEmailMessages subject"
|
|
|
|
incoming_valid_message["From"] = self.example_email("othello")
|
|
|
|
incoming_valid_message["To"] = mm_address
|
|
|
|
incoming_valid_message["Reply-to"] = self.example_email("othello")
|
|
|
|
|
|
|
|
process_message(incoming_valid_message)
|
|
|
|
|
|
|
|
message = most_recent_message(user_profile)
|
|
|
|
self.assertEqual(message.sender, user_profile)
|
|
|
|
self.assertTrue(message.has_attachment)
|
|
|
|
|
|
|
|
attachment = Attachment.objects.last()
|
|
|
|
assert attachment is not None
|
|
|
|
self.assertEqual(attachment.realm, user_profile.realm)
|
|
|
|
self.assertEqual(attachment.owner, user_profile)
|
|
|
|
self.assertEqual(attachment.is_realm_public, True)
|
|
|
|
self.assertEqual(list(attachment.messages.values_list("id", flat=True)), [message.id])
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
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
|
2021-02-12 08:20:45 +01: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()
|
2021-02-12 08:20:45 +01:00
|
|
|
incoming_valid_message.set_content("")
|
2015-10-29 16:09:09 +01:00
|
|
|
|
2021-05-10 07:02:14 +02:00
|
|
|
incoming_valid_message["Subject"] = "TestStreamEmailMessages subject"
|
2021-02-12 08:20:45 +01:00
|
|
|
incoming_valid_message["From"] = self.example_email("hamlet")
|
|
|
|
incoming_valid_message["To"] = stream_to_address
|
|
|
|
incoming_valid_message["Reply-to"] = self.example_email("othello")
|
2015-10-29 16:09:09 +01:00
|
|
|
|
2021-08-31 23:47:03 +02:00
|
|
|
with self.assertLogs(logger_name, level="INFO") as m:
|
2019-03-22 10:06:44 +01:00
|
|
|
process_message(incoming_valid_message)
|
2021-02-12 08:19:30 +01:00
|
|
|
self.assertEqual(
|
2023-01-24 21:45:49 +01:00
|
|
|
m.output,
|
|
|
|
[f"INFO:{logger_name}:Email has no nonempty body sections; ignoring."],
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
2019-01-15 20:19:49 +01:00
|
|
|
|
|
|
|
def test_receive_stream_email_messages_no_textual_body(self) -> None:
|
2021-02-12 08:20:45 +01:00
|
|
|
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()
|
2021-02-12 08:19:30 +01:00
|
|
|
with open(
|
2021-02-12 08:20:45 +01:00
|
|
|
os.path.join(settings.DEPLOY_ROOT, "static/images/default-avatar.png"), "rb"
|
2021-02-12 08:19:30 +01:00
|
|
|
) 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
|
|
|
|
2021-05-10 07:02:14 +02:00
|
|
|
incoming_valid_message["Subject"] = "TestStreamEmailMessages subject"
|
2021-02-12 08:20:45 +01:00
|
|
|
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-01-15 20:19:49 +01:00
|
|
|
|
2021-08-31 23:47:03 +02:00
|
|
|
with self.assertLogs(logger_name, level="INFO") as m:
|
2019-03-22 10:06:44 +01:00
|
|
|
process_message(incoming_valid_message)
|
2020-08-01 18:38:54 +02:00
|
|
|
self.assertEqual(
|
|
|
|
m.output,
|
2021-02-12 08:19:30 +01:00
|
|
|
[
|
|
|
|
f"WARNING:{logger_name}:Content types: ['multipart/mixed', 'image/png']",
|
2021-08-31 23:47:03 +02:00
|
|
|
f"INFO:{logger_name}:Unable to find plaintext or HTML message body",
|
2021-02-12 08:19:30 +01:00
|
|
|
],
|
2020-08-01 18:38:54 +02:00
|
|
|
)
|
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:
|
2021-02-12 08:20:45 +01:00
|
|
|
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 = {}
|
2021-02-12 08:20:45 +01:00
|
|
|
headers["Reply-To"] = self.example_email("othello")
|
2019-03-21 14:19:55 +01:00
|
|
|
|
|
|
|
# empty body
|
2020-06-05 23:26:35 +02:00
|
|
|
incoming_valid_message = EmailMessage()
|
2021-02-12 08:20:45 +01:00
|
|
|
incoming_valid_message.set_content("-- \nFooter")
|
2019-03-21 14:19:55 +01:00
|
|
|
|
2021-05-10 07:02:14 +02:00
|
|
|
incoming_valid_message["Subject"] = "TestStreamEmailMessages subject"
|
2021-02-12 08:20:45 +01:00
|
|
|
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-21 14:19:55 +01:00
|
|
|
|
|
|
|
process_message(incoming_valid_message)
|
|
|
|
message = most_recent_message(user_profile)
|
|
|
|
|
|
|
|
self.assertEqual(message.content, "(No email body)")
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
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:
|
2023-06-19 16:26:12 +02:00
|
|
|
# Build dummy messages for message notification email reply.
|
|
|
|
# Have Hamlet send Othello a direct message. Othello will
|
|
|
|
# reply via email Hamlet will receive the message.
|
2021-02-12 08:20:45 +01:00
|
|
|
self.login("hamlet")
|
|
|
|
othello = self.example_user("othello")
|
2021-02-12 08:19:30 +01:00
|
|
|
result = self.client_post(
|
|
|
|
"/json/messages",
|
|
|
|
{
|
|
|
|
"type": "private",
|
|
|
|
"content": "test_receive_missed_message_email_messages",
|
|
|
|
"to": orjson.dumps([othello.id]).decode(),
|
|
|
|
},
|
|
|
|
)
|
2015-10-31 16:31:17 +01:00
|
|
|
self.assert_json_success(result)
|
|
|
|
|
2021-02-12 08:20:45 +01: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()
|
2021-05-10 07:02:14 +02:00
|
|
|
incoming_valid_message.set_content("TestMissedMessageEmailMessages body")
|
2015-10-31 16:31:17 +01:00
|
|
|
|
2021-05-10 07:02:14 +02:00
|
|
|
incoming_valid_message["Subject"] = "TestMissedMessageEmailMessages subject"
|
2021-02-12 08:20:45 +01:00
|
|
|
incoming_valid_message["From"] = self.example_email("othello")
|
|
|
|
incoming_valid_message["To"] = mm_address
|
|
|
|
incoming_valid_message["Reply-to"] = self.example_email("othello")
|
2015-10-31 16:31:17 +01:00
|
|
|
|
2023-09-27 02:55:39 +02:00
|
|
|
with self.assert_database_query_count(16):
|
2023-08-08 18:30:05 +02:00
|
|
|
process_message(incoming_valid_message)
|
2015-10-31 16:31:17 +01:00
|
|
|
|
|
|
|
# confirm that Hamlet got the message
|
2021-02-12 08:20:45 +01:00
|
|
|
user_profile = self.example_user("hamlet")
|
2015-10-31 16:31:17 +01:00
|
|
|
message = most_recent_message(user_profile)
|
|
|
|
|
2021-05-10 07:02:14 +02:00
|
|
|
self.assertEqual(message.content, "TestMissedMessageEmailMessages body")
|
2021-02-12 08:20:45 +01:00
|
|
|
self.assertEqual(message.sender, self.example_user("othello"))
|
2021-02-02 14:09:11 +01:00
|
|
|
self.assertEqual(message.recipient.type_id, user_profile.id)
|
2015-10-31 16:31:17 +01:00
|
|
|
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:
|
2023-06-19 16:26:12 +02:00
|
|
|
# Build dummy messages for message notification email reply.
|
|
|
|
# Have Othello send Iago and Cordelia a group direct message.
|
|
|
|
# Cordelia will reply via email Iago and Othello will receive
|
|
|
|
# the message.
|
2021-02-12 08:20:45 +01:00
|
|
|
self.login("othello")
|
|
|
|
cordelia = self.example_user("cordelia")
|
|
|
|
iago = self.example_user("iago")
|
2021-02-12 08:19:30 +01:00
|
|
|
result = self.client_post(
|
|
|
|
"/json/messages",
|
|
|
|
{
|
|
|
|
"type": "private",
|
|
|
|
"content": "test_receive_missed_message_email_messages",
|
|
|
|
"to": orjson.dumps([cordelia.id, iago.id]).decode(),
|
|
|
|
},
|
|
|
|
)
|
2015-10-31 16:31:17 +01:00
|
|
|
self.assert_json_success(result)
|
|
|
|
|
2021-02-12 08:20:45 +01: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()
|
2021-05-10 07:02:14 +02:00
|
|
|
incoming_valid_message.set_content("TestMissedHuddleMessageEmailMessages body")
|
2015-10-31 16:31:17 +01:00
|
|
|
|
2021-05-10 07:02:14 +02:00
|
|
|
incoming_valid_message["Subject"] = "TestMissedHuddleMessageEmailMessages subject"
|
2021-02-12 08:20:45 +01:00
|
|
|
incoming_valid_message["From"] = self.example_email("cordelia")
|
|
|
|
incoming_valid_message["To"] = mm_address
|
|
|
|
incoming_valid_message["Reply-to"] = self.example_email("cordelia")
|
2015-10-31 16:31:17 +01:00
|
|
|
|
2023-09-27 02:55:39 +02:00
|
|
|
with self.assert_database_query_count(21):
|
2023-08-08 18:30:05 +02:00
|
|
|
process_message(incoming_valid_message)
|
2015-10-31 16:31:17 +01:00
|
|
|
|
|
|
|
# Confirm Iago received the message.
|
2021-02-12 08:20:45 +01:00
|
|
|
user_profile = self.example_user("iago")
|
2015-10-31 16:31:17 +01:00
|
|
|
message = most_recent_message(user_profile)
|
|
|
|
|
2021-05-10 07:02:14 +02:00
|
|
|
self.assertEqual(message.content, "TestMissedHuddleMessageEmailMessages body")
|
2021-02-12 08:20:45 +01: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.
|
2021-02-12 08:20:45 +01:00
|
|
|
user_profile = self.example_user("othello")
|
2015-10-31 16:31:17 +01:00
|
|
|
message = most_recent_message(user_profile)
|
|
|
|
|
2021-05-10 07:02:14 +02:00
|
|
|
self.assertEqual(message.content, "TestMissedHuddleMessageEmailMessages body")
|
2021-02-12 08:20:45 +01: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:
|
2021-04-20 23:27:25 +02:00
|
|
|
# build dummy messages for message notification email reply
|
2019-03-21 16:20:36 +01:00
|
|
|
# have Hamlet send a message to stream Denmark, that Othello
|
2021-04-20 23:27:25 +02:00
|
|
|
# will receive a message notification email about.
|
2019-03-21 16:20:36 +01:00
|
|
|
# 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")
|
2021-02-12 08:20:45 +01:00
|
|
|
self.login("hamlet")
|
2021-02-12 08:19:30 +01:00
|
|
|
result = self.client_post(
|
|
|
|
"/json/messages",
|
|
|
|
{
|
|
|
|
"type": "stream",
|
|
|
|
"topic": "test topic",
|
|
|
|
"content": "test_receive_missed_stream_message_email_messages",
|
2022-09-13 08:39:44 +02:00
|
|
|
"to": orjson.dumps("Denmark").decode(),
|
2021-02-12 08:19:30 +01:00
|
|
|
},
|
|
|
|
)
|
2019-03-21 16:20:36 +01:00
|
|
|
self.assert_json_success(result)
|
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
user_profile = self.example_user("othello")
|
2019-03-21 16:20:36 +01:00
|
|
|
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()
|
2021-05-10 07:02:14 +02:00
|
|
|
incoming_valid_message.set_content("TestMissedMessageEmailMessages body")
|
2019-03-21 16:20:36 +01:00
|
|
|
|
2021-05-10 07:02:14 +02:00
|
|
|
incoming_valid_message["Subject"] = "TestMissedMessageEmailMessages subject"
|
2021-02-12 08:20:45 +01:00
|
|
|
incoming_valid_message["From"] = user_profile.delivery_email
|
|
|
|
incoming_valid_message["To"] = mm_address
|
|
|
|
incoming_valid_message["Reply-to"] = user_profile.delivery_email
|
2019-03-21 16:20:36 +01:00
|
|
|
|
2023-09-27 02:55:39 +02:00
|
|
|
with self.assert_database_query_count(17):
|
2023-08-08 18:30:05 +02:00
|
|
|
process_message(incoming_valid_message)
|
2019-03-21 16:20:36 +01:00
|
|
|
|
|
|
|
# confirm that Hamlet got the message
|
2021-02-12 08:20:45 +01:00
|
|
|
user_profile = self.example_user("hamlet")
|
2019-03-21 16:20:36 +01:00
|
|
|
message = most_recent_message(user_profile)
|
|
|
|
|
2021-05-10 07:02:14 +02:00
|
|
|
self.assertEqual(message.content, "TestMissedMessageEmailMessages body")
|
2021-02-12 08:20:45 +01:00
|
|
|
self.assertEqual(message.sender, self.example_user("othello"))
|
2019-03-21 16:20:36 +01:00
|
|
|
self.assertEqual(message.recipient.type, Recipient.STREAM)
|
|
|
|
self.assertEqual(message.recipient.id, usermessage.message.recipient.id)
|
|
|
|
|
2021-01-06 04:06:18 +01:00
|
|
|
def test_receive_email_response_for_auth_failures(self) -> None:
|
|
|
|
user_profile = self.example_user("hamlet")
|
|
|
|
self.subscribe(user_profile, "announce")
|
|
|
|
self.login("hamlet")
|
|
|
|
result = self.client_post(
|
|
|
|
"/json/messages",
|
|
|
|
{
|
|
|
|
"type": "stream",
|
|
|
|
"topic": "test topic",
|
|
|
|
"content": "test_receive_email_response_for_auth_failures",
|
2022-09-13 08:39:44 +02:00
|
|
|
"to": orjson.dumps("announce").decode(),
|
2021-01-06 04:06:18 +01:00
|
|
|
},
|
|
|
|
)
|
|
|
|
self.assert_json_success(result)
|
|
|
|
|
|
|
|
stream = get_stream("announce", user_profile.realm)
|
2021-12-15 01:04:35 +01:00
|
|
|
do_change_stream_post_policy(
|
|
|
|
stream, Stream.STREAM_POST_POLICY_ADMINS, acting_user=user_profile
|
|
|
|
)
|
2021-01-06 04:06:18 +01:00
|
|
|
|
|
|
|
usermessage = most_recent_usermessage(user_profile)
|
|
|
|
|
|
|
|
mm_address = create_missed_message_address(user_profile, usermessage.message)
|
|
|
|
|
|
|
|
incoming_valid_message = EmailMessage()
|
2021-05-10 07:02:14 +02:00
|
|
|
incoming_valid_message.set_content("TestMissedMessageEmailMessages body")
|
2021-01-06 04:06:18 +01:00
|
|
|
|
2021-05-10 07:02:14 +02:00
|
|
|
incoming_valid_message["Subject"] = "TestMissedMessageEmailMessages subject"
|
2021-01-06 04:06:18 +01:00
|
|
|
incoming_valid_message["From"] = user_profile.delivery_email
|
|
|
|
incoming_valid_message["To"] = mm_address
|
|
|
|
incoming_valid_message["Reply-to"] = user_profile.delivery_email
|
|
|
|
|
|
|
|
process_message(incoming_valid_message)
|
|
|
|
|
|
|
|
message = most_recent_message(user_profile)
|
|
|
|
|
|
|
|
self.assertEqual(
|
|
|
|
message.content,
|
2021-04-20 23:27:25 +02:00
|
|
|
"Error sending message to stream announce via message notification email reply:\nOnly organization administrators can send to this stream.",
|
2021-01-06 04:06:18 +01:00
|
|
|
)
|
2021-03-07 20:35:35 +01:00
|
|
|
self.assertEqual(
|
2023-01-24 21:45:49 +01:00
|
|
|
message.sender,
|
|
|
|
get_system_bot(settings.NOTIFICATION_BOT, user_profile.realm_id),
|
2021-03-07 20:35:35 +01:00
|
|
|
)
|
2021-01-06 04:06:18 +01:00
|
|
|
|
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")
|
2021-02-12 08:20:45 +01:00
|
|
|
self.login("hamlet")
|
2021-02-12 08:19:30 +01:00
|
|
|
result = self.client_post(
|
|
|
|
"/json/messages",
|
|
|
|
{
|
|
|
|
"type": "stream",
|
|
|
|
"topic": "test topic",
|
|
|
|
"content": "test_receive_missed_stream_message_email_messages",
|
2022-09-13 08:39:44 +02:00
|
|
|
"to": orjson.dumps("Denmark").decode(),
|
2021-02-12 08:19:30 +01:00
|
|
|
},
|
|
|
|
)
|
2019-12-26 13:46:55 +01:00
|
|
|
self.assert_json_success(result)
|
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
user_profile = self.example_user("othello")
|
2019-12-26 13:46:55 +01:00
|
|
|
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()
|
2021-05-10 07:02:14 +02:00
|
|
|
incoming_valid_message.set_content("TestMissedMessageEmailMessages body")
|
2019-12-26 13:46:55 +01:00
|
|
|
|
2021-05-10 07:02:14 +02:00
|
|
|
incoming_valid_message["Subject"] = "TestMissedMessageEmailMessages subject"
|
2021-02-12 08:20:45 +01:00
|
|
|
incoming_valid_message["From"] = user_profile.delivery_email
|
|
|
|
incoming_valid_message["To"] = mm_address
|
|
|
|
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
|
2021-02-12 08:20:45 +01:00
|
|
|
user_profile = self.example_user("hamlet")
|
2019-12-26 13:46:55 +01:00
|
|
|
message = most_recent_message(user_profile)
|
|
|
|
|
|
|
|
self.assertEqual(message.subject, "updated topic")
|
2021-05-10 07:02:14 +02:00
|
|
|
self.assertEqual(message.content, "TestMissedMessageEmailMessages body")
|
2021-02-12 08:20:45 +01:00
|
|
|
self.assertEqual(message.sender, self.example_user("othello"))
|
2019-12-26 13:46:55 +01:00
|
|
|
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")
|
2021-02-12 08:20:45 +01:00
|
|
|
self.login("hamlet")
|
2021-02-12 08:19:30 +01:00
|
|
|
result = self.client_post(
|
|
|
|
"/json/messages",
|
|
|
|
{
|
|
|
|
"type": "stream",
|
|
|
|
"topic": "test topic",
|
|
|
|
"content": "test_receive_missed_stream_message_email_messages",
|
2022-09-13 08:39:44 +02:00
|
|
|
"to": orjson.dumps("Denmark").decode(),
|
2021-02-12 08:19:30 +01:00
|
|
|
},
|
|
|
|
)
|
2019-09-21 02:00:00 +02:00
|
|
|
self.assert_json_success(result)
|
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
user_profile = self.example_user("othello")
|
2019-09-21 02:00:00 +02:00
|
|
|
message = most_recent_message(user_profile)
|
|
|
|
|
|
|
|
mm_address = create_missed_message_address(user_profile, message)
|
|
|
|
|
2021-03-27 06:02:12 +01:00
|
|
|
do_deactivate_user(user_profile, acting_user=None)
|
2019-09-21 02:00:00 +02:00
|
|
|
|
2020-06-05 23:26:35 +02:00
|
|
|
incoming_valid_message = EmailMessage()
|
2021-05-10 07:02:14 +02:00
|
|
|
incoming_valid_message.set_content("TestMissedMessageEmailMessages body")
|
2019-09-21 02:00:00 +02:00
|
|
|
|
2021-05-10 07:02:14 +02:00
|
|
|
incoming_valid_message["Subject"] = "TestMissedMessageEmailMessages subject"
|
2021-02-12 08:20:45 +01:00
|
|
|
incoming_valid_message["From"] = user_profile.delivery_email
|
|
|
|
incoming_valid_message["To"] = mm_address
|
|
|
|
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")
|
2021-02-12 08:20:45 +01:00
|
|
|
self.login("hamlet")
|
2021-02-12 08:19:30 +01:00
|
|
|
result = self.client_post(
|
|
|
|
"/json/messages",
|
|
|
|
{
|
|
|
|
"type": "stream",
|
|
|
|
"topic": "test topic",
|
|
|
|
"content": "test_receive_missed_stream_message_email_messages",
|
2022-09-13 08:39:44 +02:00
|
|
|
"to": orjson.dumps("Denmark").decode(),
|
2021-02-12 08:19:30 +01:00
|
|
|
},
|
|
|
|
)
|
2019-09-21 02:00:00 +02:00
|
|
|
self.assert_json_success(result)
|
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
user_profile = self.example_user("othello")
|
2019-09-21 02:00:00 +02:00
|
|
|
message = most_recent_message(user_profile)
|
|
|
|
|
|
|
|
mm_address = create_missed_message_address(user_profile, message)
|
|
|
|
|
2021-04-02 17:11:25 +02:00
|
|
|
do_deactivate_realm(user_profile.realm, acting_user=None)
|
2019-09-21 02:00:00 +02:00
|
|
|
|
2020-06-05 23:26:35 +02:00
|
|
|
incoming_valid_message = EmailMessage()
|
2021-05-10 07:02:14 +02:00
|
|
|
incoming_valid_message.set_content("TestMissedMessageEmailMessages body")
|
2019-09-21 02:00:00 +02:00
|
|
|
|
2021-05-10 07:02:14 +02:00
|
|
|
incoming_valid_message["Subject"] = "TestMissedMessageEmailMessages subject"
|
2021-02-12 08:20:45 +01:00
|
|
|
incoming_valid_message["From"] = user_profile.delivery_email
|
|
|
|
incoming_valid_message["To"] = mm_address
|
|
|
|
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")
|
2021-02-12 08:20:45 +01:00
|
|
|
self.login("hamlet")
|
2019-12-26 13:46:55 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
result = self.client_post(
|
|
|
|
"/json/messages",
|
|
|
|
{
|
|
|
|
"type": "stream",
|
|
|
|
"topic": "test topic",
|
|
|
|
"content": "test_receive_missed_stream_message_email_messages",
|
2022-09-13 08:39:44 +02:00
|
|
|
"to": orjson.dumps("Denmark").decode(),
|
2021-02-12 08:19:30 +01:00
|
|
|
},
|
|
|
|
)
|
2019-12-26 13:46:55 +01:00
|
|
|
self.assert_json_success(result)
|
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
user_profile = self.example_user("othello")
|
2019-12-26 13:46:55 +01:00
|
|
|
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()
|
2021-05-10 07:02:14 +02:00
|
|
|
incoming_valid_message.set_content("TestMissedMessageEmailMessages body")
|
2019-12-26 13:46:55 +01:00
|
|
|
|
2021-05-10 07:02:14 +02:00
|
|
|
incoming_valid_message["Subject"] = "TestMissedMessageEmailMessages subject"
|
2021-02-12 08:20:45 +01:00
|
|
|
incoming_valid_message["From"] = user_profile.delivery_email
|
|
|
|
incoming_valid_message["To"] = mm_address
|
|
|
|
incoming_valid_message["Reply-to"] = user_profile.delivery_email
|
2019-12-26 13:46:55 +01:00
|
|
|
|
2022-02-23 05:32:17 +01:00
|
|
|
# there is no longer a usage limit. Ensure we can send multiple times.
|
2023-09-01 23:05:08 +02:00
|
|
|
for i in range(5):
|
2019-12-26 13:46:55 +01:00
|
|
|
process_missed_message(mm_address, incoming_valid_message)
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
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:
|
2021-02-12 08:20:45 +01:00
|
|
|
self.login("othello")
|
|
|
|
cordelia = self.example_user("cordelia")
|
|
|
|
iago = self.example_user("iago")
|
2020-03-12 14:17:25 +01:00
|
|
|
payload = dict(
|
|
|
|
type="private",
|
|
|
|
content="test_receive_missed_message_email_messages",
|
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)
|
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
user_profile = self.example_user("cordelia")
|
2016-07-31 16:59:52 +02:00
|
|
|
usermessage = most_recent_usermessage(user_profile)
|
2021-02-12 08:20:45 +01:00
|
|
|
with self.settings(EMAIL_GATEWAY_PATTERN=""):
|
2016-07-31 16:59:52 +02:00
|
|
|
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"))
|
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
with self.settings(EMAIL_GATEWAY_PATTERN=""):
|
2017-05-23 23:26:03 +02:00
|
|
|
test_address = encode_email_address(stream)
|
2021-02-12 08:20:45 +01:00
|
|
|
self.assertEqual(test_address, "")
|
2016-07-31 16:59:52 +02:00
|
|
|
|
2021-02-12 08:19:30 +01: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
|
2021-02-12 08:20:45 +01:00
|
|
|
self.login("hamlet")
|
|
|
|
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
|
|
|
|
2021-05-10 07:02:14 +02:00
|
|
|
incoming_valid_message["Subject"] = "TestStreamEmailMessages subject"
|
2021-02-12 08:20:45 +01:00
|
|
|
incoming_valid_message["From"] = user_profile.delivery_email
|
|
|
|
incoming_valid_message["To"] = stream_to_address
|
|
|
|
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:
|
2021-02-12 08:20:45 +01:00
|
|
|
del incoming_valid_message["Subject"]
|
2021-05-10 07:02:14 +02:00
|
|
|
incoming_valid_message["Subject"] = "FWD: TestStreamEmailMessages subject"
|
2019-03-09 22:35:45 +01:00
|
|
|
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
|
2021-02-12 08:20:45 +01:00
|
|
|
self.login("hamlet")
|
|
|
|
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
|
|
|
|
2021-05-10 07:02:14 +02:00
|
|
|
incoming_valid_message["Subject"] = "TestStreamEmailMessages subject"
|
2021-02-12 08:20:45 +01:00
|
|
|
incoming_valid_message["From"] = user_profile.delivery_email
|
|
|
|
incoming_valid_message["To"] = stream_to_address
|
|
|
|
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)
|
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
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:
|
2021-02-12 08:20:45 +01:00
|
|
|
del incoming_valid_message["Subject"]
|
2021-05-10 07:02:14 +02:00
|
|
|
incoming_valid_message["Subject"] = "FWD: TestStreamEmailMessages subject"
|
2019-03-09 22:35:45 +01:00
|
|
|
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-04-18 17:28:55 +02:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
class TestScriptMTA(ZulipTestCase):
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_success(self) -> None:
|
2021-02-12 08:20:45 +01:00
|
|
|
script = os.path.join(os.path.dirname(__file__), "../../scripts/lib/email-mirror-postfix")
|
2016-07-28 15:36:26 +02:00
|
|
|
|
2021-02-12 08:20:45 +01: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)
|
|
|
|
|
2021-02-12 08:20:45 +01: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)
|
2020-10-30 01:36:18 +01:00
|
|
|
subprocess.run(
|
2021-02-12 08:20:45 +01:00
|
|
|
[script, "-r", stream_to_address, "-s", settings.SHARED_SECRET, "-t"],
|
2020-10-30 01:36:18 +01:00
|
|
|
input=mail,
|
|
|
|
check=True,
|
2022-01-22 07:52:54 +01:00
|
|
|
text=True,
|
2020-10-30 01:36:18 +01:00
|
|
|
)
|
2017-04-18 17:28:55 +02:00
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_error_no_recipient(self) -> None:
|
2021-02-12 08:20:45 +01:00
|
|
|
script = os.path.join(os.path.dirname(__file__), "../../scripts/lib/email-mirror-postfix")
|
2017-04-18 17:28:55 +02:00
|
|
|
|
2021-02-12 08:20:45 +01: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)
|
2021-02-12 08:20:45 +01: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)
|
2020-10-30 01:36:18 +01:00
|
|
|
p = subprocess.run(
|
2021-02-12 08:20:45 +01:00
|
|
|
[script, "-s", settings.SHARED_SECRET, "-t"],
|
2020-10-30 01:36:18 +01:00
|
|
|
input=mail,
|
|
|
|
stdout=subprocess.PIPE,
|
2022-01-22 07:52:54 +01:00
|
|
|
text=True,
|
2023-08-18 00:50:03 +02:00
|
|
|
check=False,
|
2020-10-30 01:36:18 +01:00
|
|
|
)
|
|
|
|
self.assertEqual(
|
|
|
|
p.stdout,
|
2021-02-12 08:20:45 +01:00
|
|
|
"5.1.1 Bad destination mailbox address: No missed message email address.\n",
|
2020-10-30 01:36:18 +01:00
|
|
|
)
|
|
|
|
self.assertEqual(p.returncode, 67)
|
2017-04-18 17:28:55 +02:00
|
|
|
|
|
|
|
|
|
|
|
class TestEmailMirrorTornadoView(ZulipTestCase):
|
2018-05-10 19:00:29 +02:00
|
|
|
def send_private_message(self) -> str:
|
2021-02-12 08:20:45 +01:00
|
|
|
self.login("othello")
|
|
|
|
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",
|
2020-08-07 01:09:47 +02:00
|
|
|
"to": orjson.dumps([cordelia.id, iago.id]).decode(),
|
2021-02-12 08:19:30 +01:00
|
|
|
},
|
|
|
|
)
|
2017-04-18 17:28:55 +02:00
|
|
|
self.assert_json_success(result)
|
|
|
|
|
2021-02-12 08:20:45 +01: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)
|
|
|
|
|
2022-06-08 04:52:09 +02:00
|
|
|
def send_offline_message(self, to_address: str, sender: UserProfile) -> "TestHttpResponse":
|
2021-02-12 08:20:45 +01: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
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
def check_queue_json_publish(
|
|
|
|
queue_name: str,
|
|
|
|
event: Mapping[str, Any],
|
|
|
|
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)
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
self.assertEqual(
|
2023-01-24 21:45:49 +01:00
|
|
|
self.get_last_message().content,
|
|
|
|
"This is a plain-text message for testing Zulip.",
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
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
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
with mock_queue_publish("zerver.lib.email_mirror.queue_json_publish") as m:
|
2020-08-14 10:03:36 +02:00
|
|
|
m.side_effect = check_queue_json_publish
|
2021-02-12 08:20:45 +01:00
|
|
|
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)
|
2021-02-12 08:20:45 +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
|
|
|
|
2021-02-12 08:20:45 +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: "
|
2021-02-12 08:19:30 +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
|
|
|
|
2021-02-12 08:20:45 +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()
|
2021-02-12 08:20:45 +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()
|
2022-02-23 05:32:17 +01:00
|
|
|
# there is no longer a usage limit. Ensure we can send multiple times.
|
2023-09-01 23:05:08 +02:00
|
|
|
for i in range(5):
|
2021-02-12 08:20:45 +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)
|
|
|
|
|
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()
|
2021-02-12 08:20:45 +01:00
|
|
|
mm_address = "mm" + ("x" * 32) + "@testserver"
|
|
|
|
result = self.send_offline_message(mm_address, self.example_user("cordelia"))
|
2017-04-18 17:28:55 +02:00
|
|
|
self.assert_json_error(
|
|
|
|
result,
|
2022-02-23 05:32:17 +01:00
|
|
|
"5.1.1 Bad destination mailbox address: Zulip notification reply address is invalid.",
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
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:
|
2021-02-12 08:20:45 +01:00
|
|
|
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()
|
2021-05-10 07:02:14 +02:00
|
|
|
incoming_valid_message.set_content("TestStreamEmailMessages body")
|
2021-02-12 08:20:45 +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")
|
2019-03-09 20:36:59 +01:00
|
|
|
|
|
|
|
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)
|
2021-02-12 08:20:45 +01:00
|
|
|
del incoming_valid_message["Subject"]
|
|
|
|
incoming_valid_message["Subject"] = "Re: Fwd: Re: "
|
2019-03-09 20:36:59 +01:00
|
|
|
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:
|
2021-02-12 08:20:45 +01: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:
|
2021-02-12 08:20:45 +01:00
|
|
|
stripped = strip_from_subject(subject["original_subject"])
|
|
|
|
self.assertEqual(stripped, subject["stripped_subject"])
|
2019-05-09 16:01:34 +02:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
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:
|
2021-02-12 08:20:45 +01:00
|
|
|
message_as_string = self.fixture_data("1.txt", type="email")
|
2021-02-12 08:19:30 +01:00
|
|
|
message_as_string = message_as_string.replace(
|
2021-02-12 08:20:45 +01:00
|
|
|
'Content-Type: text/plain; charset="us-ascii"', "Content-Type: text/plain"
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
2020-06-05 23:26:35 +02:00
|
|
|
incoming_message = message_from_string(message_as_string, policy=email.policy.default)
|
2020-09-02 02:50:08 +02:00
|
|
|
# https://github.com/python/typeshed/issues/2417
|
|
|
|
assert isinstance(incoming_message, EmailMessage)
|
2019-05-09 16:01:34 +02:00
|
|
|
|
2021-02-12 08:20:45 +01: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)
|
2022-05-13 08:22:52 +02:00
|
|
|
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")
|
|
|
|
|
|
|
|
|
|
|
|
class TestContentTypeInvalidCharset(ZulipTestCase):
|
|
|
|
def test_unknown_charset(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; charset="bogus"',
|
|
|
|
)
|
|
|
|
incoming_message = message_from_string(message_as_string, policy=email.policy.default)
|
|
|
|
# https://github.com/python/typeshed/issues/2417
|
|
|
|
assert isinstance(incoming_message, EmailMessage)
|
|
|
|
|
|
|
|
user_profile = self.example_user("hamlet")
|
|
|
|
self.login_user(user_profile)
|
|
|
|
self.subscribe(user_profile, "Denmark")
|
|
|
|
stream = get_stream("Denmark", user_profile.realm)
|
2019-05-09 16:01:34 +02:00
|
|
|
stream_to_address = encode_email_address(stream)
|
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
del incoming_message["To"]
|
|
|
|
incoming_message["To"] = stream_to_address
|
2019-05-09 16:01:34 +02:00
|
|
|
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
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
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()
|
2021-05-10 07:02:14 +02:00
|
|
|
incoming_valid_message.set_content("Test body")
|
|
|
|
incoming_valid_message["Subject"] = "Test subject"
|
2021-02-12 08:20:45 +01:00
|
|
|
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")
|
2019-03-22 10:38:24 +01:00
|
|
|
|
2023-04-11 19:51:14 +02:00
|
|
|
with mock.patch("zerver.lib.email_mirror.log_error") as mock_log_error:
|
2019-03-22 10:38:24 +01:00
|
|
|
process_message(incoming_valid_message)
|
2023-04-11 19:51:14 +02:00
|
|
|
mock_log_error.assert_called_with(
|
2021-02-12 08:19:30 +01:00
|
|
|
incoming_valid_message, "Missing recipient in mirror email", None
|
|
|
|
)
|
|
|
|
|
2019-03-22 16:33:57 +01:00
|
|
|
|
|
|
|
class TestEmailMirrorLogAndReport(ZulipTestCase):
|
2023-04-11 19:51:14 +02:00
|
|
|
def test_log_error(self) -> None:
|
2021-02-12 08:20:45 +01:00
|
|
|
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)
|
|
|
|
|
2020-06-05 23:26:35 +02:00
|
|
|
incoming_valid_message = EmailMessage()
|
2021-05-10 07:02:14 +02:00
|
|
|
incoming_valid_message.set_content("Test body")
|
|
|
|
incoming_valid_message["Subject"] = "Test subject"
|
2021-02-12 08:20:45 +01:00
|
|
|
incoming_valid_message["From"] = self.example_email("hamlet")
|
|
|
|
incoming_valid_message["To"] = stream_to_address
|
|
|
|
with self.assertLogs("zerver.lib.email_mirror", "ERROR") as error_log:
|
2023-04-11 19:51:14 +02:00
|
|
|
log_error(incoming_valid_message, "test error message", stream_to_address)
|
2021-02-12 08:19:30 +01:00
|
|
|
self.assertEqual(
|
|
|
|
error_log.output,
|
|
|
|
[
|
2021-04-27 16:56:45 +02:00
|
|
|
f"ERROR:zerver.lib.email_mirror:Sender: hamlet@zulip.com\nTo: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX@testserver <Address to stream id: {stream.id}>\ntest error message"
|
2021-02-12 08:19:30 +01:00
|
|
|
],
|
|
|
|
)
|
2019-03-22 16:33:57 +01:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
with self.assertLogs("zerver.lib.email_mirror", "ERROR") as error_log:
|
2023-04-11 19:51:14 +02:00
|
|
|
log_error(incoming_valid_message, "test error message", None)
|
2021-02-12 08:19:30 +01:00
|
|
|
self.assertEqual(
|
|
|
|
error_log.output,
|
|
|
|
[
|
2021-02-12 08:20:45 +01:00
|
|
|
"ERROR:zerver.lib.email_mirror:Sender: hamlet@zulip.com\nTo: No recipient found\ntest error message"
|
2021-02-12 08:19:30 +01:00
|
|
|
],
|
|
|
|
)
|
2020-07-18 17:12:30 +02:00
|
|
|
|
2019-03-22 16:33:57 +01:00
|
|
|
def test_redact_email_address(self) -> None:
|
2021-02-12 08:20:45 +01:00
|
|
|
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)
|
2022-07-27 23:33:49 +02:00
|
|
|
address = Address(addr_spec=stream_to_address)
|
|
|
|
scrubbed_stream_address = Address(
|
|
|
|
username="X" * len(address.username), domain=address.domain
|
|
|
|
).addr_spec
|
2019-03-22 16:33:57 +01:00
|
|
|
|
|
|
|
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>"
|
2021-02-12 08:20:45 +01:00
|
|
|
expected_message = expected_message.format("XXXXXXX@testserver")
|
2019-03-22 16:33:57 +01:00
|
|
|
|
|
|
|
redacted_message = redact_email_address(error_message)
|
|
|
|
self.assertEqual(redacted_message, expected_message)
|
|
|
|
|
|
|
|
# Test for a missed message address:
|
2021-02-12 08:20:45 +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",
|
2020-08-07 01:09:47 +02:00
|
|
|
"to": orjson.dumps([cordelia.email, iago.email]).decode(),
|
2021-02-12 08:19:30 +01:00
|
|
|
},
|
|
|
|
)
|
2019-03-22 16:33:57 +01:00
|
|
|
self.assert_json_success(result)
|
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
cordelia_profile = self.example_user("cordelia")
|
2019-03-22 16:33:57 +01:00
|
|
|
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>"
|
2021-02-12 08:20:45 +01:00
|
|
|
expected_message = expected_message.format("X" * 34 + "@testserver")
|
2019-03-22 16:33:57 +01:00
|
|
|
|
|
|
|
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: {}>"
|
2021-02-12 08:19:30 +01:00
|
|
|
expected_message = expected_message.format(
|
|
|
|
scrubbed_stream_address, stream.id, scrubbed_stream_address, stream.id
|
|
|
|
)
|
2019-03-22 16:33:57 +01:00
|
|
|
|
|
|
|
redacted_message = redact_email_address(error_message)
|
|
|
|
self.assertEqual(redacted_message, expected_message)
|
|
|
|
|
|
|
|
# Test with EMAIL_GATEWAY_EXTRA_PATTERN_HACK:
|
2021-02-12 08:20:45 +01:00
|
|
|
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")
|
2019-03-22 16:33:57 +01:00
|
|
|
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)
|