For text/* attachments, EmailMessage.attach() decodes bytes as UTF-8 if possible and changes to DEFAULT_ATTACHMENT_MIME_TYPE if not.
(self)
| 1032 | self.assertEqual(actual.mimetype, expected_mimetype) |
| 1033 | |
| 1034 | def test_attach_text_as_bytes(self): |
| 1035 | """ |
| 1036 | For text/* attachments, EmailMessage.attach() decodes bytes as UTF-8 |
| 1037 | if possible and changes to DEFAULT_ATTACHMENT_MIME_TYPE if not. |
| 1038 | """ |
| 1039 | email = EmailMessage() |
| 1040 | # Mimetype guessing identifies these as text/plain from the .txt |
| 1041 | # extensions. |
| 1042 | email.attach("utf8.txt", "ütƒ-8\n".encode()) |
| 1043 | email.attach("not-utf8.txt", b"\x86unknown-encoding\n") |
| 1044 | attachments = self.get_decoded_attachments(email) |
| 1045 | self.assertEqual(attachments[0], ("utf8.txt", "ütƒ-8\n", "text/plain")) |
| 1046 | self.assertEqual( |
| 1047 | attachments[1], |
| 1048 | ("not-utf8.txt", b"\x86unknown-encoding\n", "application/octet-stream"), |
| 1049 | ) |
| 1050 | |
| 1051 | def test_attach_text_as_bytes_using_property(self): |
| 1052 | """ |
nothing calls this directly
no test coverage detected