2
0
Эх сурвалжийг харах

Fixed #35528 -- Added EmailMultiAlternatives.body_contains() helper method.

Ronny Vedrilla 9 сар өмнө
parent
commit
5fef6d2445

+ 13 - 0
django/core/mail/message.py

@@ -505,3 +505,16 @@ class EmailMultiAlternatives(EmailMessage):
                     )
                 )
         return msg
+
+    def body_contains(self, text):
+        """
+        Checks that ``text`` occurs in the email body and in all attached MIME
+        type text/* alternatives.
+        """
+        if text not in self.body:
+            return False
+
+        for content, mimetype in self.alternatives:
+            if mimetype.startswith("text/") and text not in content:
+                return False
+        return True

+ 4 - 0
docs/releases/5.2.txt

@@ -143,6 +143,10 @@ Email
   <django.core.mail.EmailMultiAlternatives.alternatives>` is now a list of
   named tuples, as opposed to regular tuples.
 
+* The new :meth:`~django.core.mail.EmailMultiAlternatives.body_contains` method
+  returns a boolean indicating whether a provided text is contained in the
+  email ``body`` and in all attached MIME type ``text/*`` alternatives.
+
 Error Reporting
 ~~~~~~~~~~~~~~~
 

+ 20 - 0
docs/topics/email.txt

@@ -436,6 +436,26 @@ Django's email library, you can do this using the
             msg.attach_alternative(html_content, "text/html")
             msg.send()
 
+    .. method:: body_contains(text)
+
+        .. versionadded:: 5.2
+
+        Returns a boolean indicating whether the provided ``text`` is
+        contained in the email ``body`` and in all attached MIME type
+        ``text/*`` alternatives.
+
+        This can be useful when testing emails. For example::
+
+            def test_contains_email_content(self):
+                subject = "Hello World"
+                from_email = "from@example.com"
+                to = "to@example.com"
+                msg = EmailMultiAlternatives(subject, "I am content.", from_email, [to])
+                msg.attach_alternative("<p>I am content.</p>", "text/html")
+
+                self.assertIs(msg.body_contains("I am content"), True)
+                self.assertIs(msg.body_contains("<p>I am content.</p>"), False)
+
 Updating the default content type
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 

+ 18 - 0
tests/mail/tests.py

@@ -1163,6 +1163,24 @@ class MailTests(HeadersCheckMixin, SimpleTestCase):
         with self.assertRaisesMessage(ValueError, msg):
             email_msg.attach_alternative("<p>content</p>", None)
 
+    def test_body_contains(self):
+        email_msg = EmailMultiAlternatives()
+        email_msg.body = "I am content."
+        self.assertIs(email_msg.body_contains("I am"), True)
+        self.assertIs(email_msg.body_contains("I am content."), True)
+
+        email_msg.attach_alternative("<p>I am different content.</p>", "text/html")
+        self.assertIs(email_msg.body_contains("I am"), True)
+        self.assertIs(email_msg.body_contains("I am content."), False)
+        self.assertIs(email_msg.body_contains("<p>I am different content.</p>"), False)
+
+    def test_body_contains_alternative_non_text(self):
+        email_msg = EmailMultiAlternatives()
+        email_msg.body = "I am content."
+        email_msg.attach_alternative("I am content.", "text/html")
+        email_msg.attach_alternative(b"I am a song.", "audio/mpeg")
+        self.assertIs(email_msg.body_contains("I am content"), True)
+
 
 @requires_tz_support
 class MailTimeZoneTests(SimpleTestCase):