Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

tests: Add type narrowing for EmailMultiAlternatives.

Zixuan James Li7950baafe2e1
diff --git a/zerver/tests/test_email_notifications.py b/zerver/tests/test_email_notifications.py
index 899662d22c4..b2c712e2cdc 100644
--- a/zerver/tests/test_email_notifications.py
+++ b/zerver/tests/test_email_notifications.py
@@ -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(
diff --git a/zerver/tests/test_signup.py b/zerver/tests/test_signup.py
index e103613e1d0..701a0c9c7a1 100644
--- a/zerver/tests/test_signup.py
+++ b/zerver/tests/test_signup.py
@@ -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 @@ [email protected]""",
self.check_sent_emails(["[email protected]"])
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