コード例 #1
0
    def test_body_props(self):
        msg = AnymailInboundMessage.construct(text="Test plaintext",
                                              html="Test HTML")
        self.assertEqual(msg.text, "Test plaintext")
        self.assertEqual(msg.html, "Test HTML")

        # Make sure attachments don't confuse it
        att_text = AnymailInboundMessage.construct_attachment(
            'text/plain', "text attachment")
        att_html = AnymailInboundMessage.construct_attachment(
            'text/html', "html attachment")

        msg = AnymailInboundMessage.construct(text="Test plaintext",
                                              attachments=[att_text, att_html])
        self.assertEqual(msg.text, "Test plaintext")
        self.assertIsNone(
            msg.html)  # no html body (the html attachment doesn't count)

        msg = AnymailInboundMessage.construct(html="Test HTML",
                                              attachments=[att_text, att_html])
        self.assertIsNone(
            msg.text)  # no plaintext body (the text attachment doesn't count)
        self.assertEqual(msg.html, "Test HTML")

        # Default None
        msg = AnymailInboundMessage()
        self.assertIsNone(msg.text)
        self.assertIsNone(msg.html)
コード例 #2
0
    def test_construct_attachments(self):
        att1 = AnymailInboundMessage.construct_attachment(
            'text/csv',
            "One,Two\n1,2".encode('iso-8859-1'),
            charset="iso-8859-1",
            filename="test.csv")

        att2 = AnymailInboundMessage.construct_attachment(
            'image/png',
            SAMPLE_IMAGE_CONTENT,
            filename=SAMPLE_IMAGE_FILENAME,
            content_id="abc123")

        msg = AnymailInboundMessage.construct(attachments=[att1, att2])
        self.assertEqual(msg['Content-Type'], "multipart/mixed")
        self.assertEqual(len(msg.get_payload()), 2)  # bodies (related), att1

        att1_part = msg.get_payload(1)
        self.assertEqual(att1_part['Content-Type'],
                         'text/csv; name="test.csv"; charset="iso-8859-1"')
        self.assertEqual(att1_part['Content-Disposition'],
                         'attachment; filename="test.csv"')
        self.assertNotIn('Content-ID', att1_part)
        self.assertEqual(att1_part.get_content_text(), "One,Two\n1,2")

        related = msg.get_payload(0)
        self.assertEqual(len(related.get_payload()),
                         2)  # alternatives (with no bodies in this test); att2
        att2_part = related.get_payload(1)
        self.assertEqual(att2_part['Content-Type'],
                         'image/png; name="sample_image.png"')
        self.assertEqual(att2_part['Content-Disposition'],
                         'inline; filename="sample_image.png"')
        self.assertEqual(att2_part['Content-ID'], '<abc123>')
        self.assertEqual(att2_part.get_content_bytes(), SAMPLE_IMAGE_CONTENT)
コード例 #3
0
 def test_construct_attachments_from_base64_data(self):
     # This is a fairly common way for ESPs to provide attachment content to webhooks
     from base64 import b64encode
     content = b64encode(SAMPLE_IMAGE_CONTENT)
     att = AnymailInboundMessage.construct_attachment(
         content_type="image/png", content=content, base64=True)
     self.assertEqual(att.get_content_bytes(), SAMPLE_IMAGE_CONTENT)
コード例 #4
0
    def test_inline_attachments_prop(self):
        att = AnymailInboundMessage.construct_attachment(
            'image/png', SAMPLE_IMAGE_CONTENT, filename=SAMPLE_IMAGE_FILENAME, content_id="abc123")

        msg = AnymailInboundMessage.construct(attachments=[att])
        self.assertEqual(msg.inline_attachments, {'abc123': att})

        # Default empty dict
        self.assertEqual(AnymailInboundMessage().inline_attachments, {})
コード例 #5
0
    def test_attachments_prop(self):
        att = AnymailInboundMessage.construct_attachment(
            'image/png', SAMPLE_IMAGE_CONTENT, filename=SAMPLE_IMAGE_FILENAME)

        msg = AnymailInboundMessage.construct(attachments=[att])
        self.assertEqual(msg.attachments, [att])

        # Default empty list
        self.assertEqual(AnymailInboundMessage().attachments, [])
コード例 #6
0
    def test_construct_rfc822_attachment_from_data(self):
        # constructed message/rfc822 attachment should end up as parsed message
        # (same as if attachment was parsed from raw mime, as in previous test)
        att = AnymailInboundMessage.construct_attachment('message/rfc822', self.original_raw_message)
        self.assertIsInstance(att, AnymailInboundMessage)
        self.assertEqual(att.get_content_type(), "message/rfc822")
        self.assertTrue(att.is_attachment())
        self.assertEqual(att.get_content_text(), self.original_raw_message)

        orig_msg = att.get_payload(0)
        self.assertIsInstance(orig_msg, AnymailInboundMessage)
        self.assertEqual(orig_msg['Subject'], "Original message")
        self.assertEqual(orig_msg.get_content_type(), "multipart/related")