tests: Add type narrowing for EmailMultiAlternatives.

Items in `django.core.mail.outbox` are by default typed as the less
general `EmailMessage` type. Before accessing the attribute
`alternatives`, we need to narrow the type to `EmailMultiAlternatives`.
Then narrow the tuple value we want to access to `str` before using
it in `assertIn` or `self.normalize_string`.

Signed-off-by: Zixuan James Li <p359101898@gmail.com>
This commit is contained in:
Zixuan James Li 2022-06-30 21:05:12 -04:00 committed by Tim Abbott
parent 039c46a4d2
commit 7950baafe2
2 changed files with 12 additions and 0 deletions

View File

@ -11,6 +11,7 @@ import lxml.html
import orjson
from django.conf import settings
from django.core import mail
from django.core.mail.message import EmailMultiAlternatives
from django.test import override_settings
from django.utils.timezone import now as timezone_now
from django_auth_ldap.config import LDAPSearch
@ -467,6 +468,7 @@ class TestMissedMessages(ZulipTestCase):
else:
reply_to_emails = ["noreply@testserver"]
msg = mail.outbox[0]
assert isinstance(msg, EmailMultiAlternatives)
from_email = str(Address(display_name="Zulip notifications", addr_spec=FromAddress.NOREPLY))
self.assert_length(mail.outbox, 1)
if send_as_user:
@ -478,6 +480,7 @@ class TestMissedMessages(ZulipTestCase):
self.assertIn(msg.reply_to[0], reply_to_emails)
if verify_html_body:
for text in verify_body_include:
assert isinstance(msg.alternatives[0][0], str)
self.assertIn(text, self.normalize_string(msg.alternatives[0][0]))
else:
for text in verify_body_include:
@ -1132,12 +1135,16 @@ class TestMissedMessages(ZulipTestCase):
],
)
assert isinstance(mail.outbox[0], EmailMultiAlternatives)
assert isinstance(mail.outbox[0].alternatives[0][0], str)
self.assertIn("Iago: @**King Hamlet**\n\n--\nYou are", mail.outbox[0].body)
# If message content starts with <p> tag the sender name is appended inside the <p> tag.
self.assertIn(
'<p><b>Iago</b>: <span class="user-mention"', mail.outbox[0].alternatives[0][0]
)
assert isinstance(mail.outbox[1], EmailMultiAlternatives)
assert isinstance(mail.outbox[1].alternatives[0][0], str)
self.assertIn("Iago: * 1\n *2\n\n--\nYou are receiving", mail.outbox[1].body)
# If message content does not starts with <p> tag sender name is appended before the <p> tag
self.assertIn(
@ -1145,6 +1152,8 @@ class TestMissedMessages(ZulipTestCase):
mail.outbox[1].alternatives[0][0],
)
assert isinstance(mail.outbox[2], EmailMultiAlternatives)
assert isinstance(mail.outbox[2].alternatives[0][0], str)
self.assertEqual("Hello\n\n--\n\nReply", mail.outbox[2].body[:16])
# Sender name is not appended to message for PM missed messages
self.assertIn(

View File

@ -11,6 +11,7 @@ from django.conf import settings
from django.contrib.auth.views import PasswordResetConfirmView
from django.contrib.contenttypes.models import ContentType
from django.core.exceptions import ValidationError
from django.core.mail.message import EmailMultiAlternatives
from django.http import HttpRequest, HttpResponse
from django.test import Client, override_settings
from django.urls import reverse
@ -1821,6 +1822,8 @@ earl-test@zulip.com""",
self.check_sent_emails(["newuser@zulip.com"])
from django.core.mail import outbox
assert isinstance(outbox[0], EmailMultiAlternatives)
assert isinstance(outbox[0].alternatives[0][0], str)
body = self.normalize_string(outbox[0].alternatives[0][0])
# Verify that one can't get Zulip to send invitation emails