email_mirror: Reuse exception messages in mirror_email_message.

This commit is contained in:
Mateusz Mandera 2020-01-10 10:21:47 +01:00 committed by Tim Abbott
parent 38ac8e9f3e
commit 90a69ab24f
2 changed files with 20 additions and 24 deletions

View File

@ -113,7 +113,7 @@ def get_missed_message_address(address: str) -> MissedMessageEmailAddress:
timestamp__gt=timezone_now() - timedelta(seconds=MissedMessageEmailAddress.EXPIRY_SECONDS)
)
except MissedMessageEmailAddress.DoesNotExist:
raise ZulipEmailForwardError("Missed message address expired or doesn't exist")
raise ZulipEmailForwardError("Missed message address expired or doesn't exist.")
def create_missed_message_address(user_profile: UserProfile, message: Message) -> str:
if settings.EMAIL_GATEWAY_PATTERN == '':
@ -369,28 +369,24 @@ def process_message(message: message.Message, rcpt_to: Optional[str]=None) -> No
else:
log_and_report(message, str(e), to)
def validate_to_address(rcpt_to: str) -> None:
if is_missed_message_address(rcpt_to):
mm_address = get_missed_message_address(rcpt_to)
if not mm_address.is_usable():
raise ZulipEmailForwardError("Missed message address out of uses.")
else:
extract_and_validate(rcpt_to)
def mirror_email_message(data: Dict[str, str]) -> Dict[str, str]:
rcpt_to = data['recipient']
if is_missed_message_address(rcpt_to):
try:
mm_address = get_missed_message_address(rcpt_to)
if not mm_address.is_usable():
raise ZulipEmailForwardError("Missed message address out of uses.")
except ZulipEmailForwardError:
return {
"status": "error",
"msg": "5.1.1 Bad destination mailbox address: "
"Bad or expired missed message address."
}
else:
try:
extract_and_validate(rcpt_to)
except ZulipEmailForwardError:
return {
"status": "error",
"msg": "5.1.1 Bad destination mailbox address: "
"Please use the address specified in your Streams page."
}
try:
validate_to_address(rcpt_to)
except ZulipEmailForwardError as e:
return {
"status": "error",
"msg": "5.1.1 Bad destination mailbox address: {}".format(e)
}
queue_json_publish(
"email_mirror",
{

View File

@ -1008,7 +1008,7 @@ class TestEmailMirrorTornadoView(ZulipTestCase):
self.assert_json_error(
result,
"5.1.1 Bad destination mailbox address: "
"Please use the address specified in your Streams page.")
"Bad stream token from email recipient " + stream_to_address)
def test_success_to_stream_with_good_token_wrong_stream_name(self) -> None:
stream = get_stream("Denmark", get_realm("zulip"))
@ -1032,7 +1032,7 @@ class TestEmailMirrorTornadoView(ZulipTestCase):
result = self.send_offline_message(mm_address, self.example_email('cordelia'))
self.assert_json_error(
result,
"5.1.1 Bad destination mailbox address: Bad or expired missed message address.")
"5.1.1 Bad destination mailbox address: Missed message address out of uses.")
def test_wrong_missed_email_private_message(self) -> None:
self.send_private_message()
@ -1040,7 +1040,7 @@ class TestEmailMirrorTornadoView(ZulipTestCase):
result = self.send_offline_message(mm_address, self.example_email('cordelia'))
self.assert_json_error(
result,
"5.1.1 Bad destination mailbox address: Bad or expired missed message address.")
"5.1.1 Bad destination mailbox address: Missed message address expired or doesn't exist.")
class TestStreamEmailMessagesSubjectStripping(ZulipTestCase):