Check if imap message text has a value instead of checking if its not None (#118901)

* Check if message_text has a value instead of checking if its not None

* Strip message_text to ensure that its actually empty or not

* Add test with multipart payload having empty plain text
pull/119096/head
Rami Mosleh 2024-06-06 16:10:03 +04:00 committed by Franck Nijhof
parent 0f9a91d369
commit 5a7332a135
No known key found for this signature in database
GPG Key ID: D62583BA8AB11CA3
3 changed files with 45 additions and 3 deletions

View File

@ -195,13 +195,13 @@ class ImapMessage:
):
message_untyped_text = str(part.get_payload())
if message_text is not None:
if message_text is not None and message_text.strip():
return message_text
if message_html is not None:
if message_html:
return message_html
if message_untyped_text is not None:
if message_untyped_text:
return message_untyped_text
return str(self.email_message.get_payload())

View File

@ -59,6 +59,11 @@ TEST_CONTENT_TEXT_PLAIN = (
b"Content-Transfer-Encoding: 7bit\r\n\r\nTest body\r\n"
)
TEST_CONTENT_TEXT_PLAIN_EMPTY = (
b'Content-Type: text/plain; charset="utf-8"\r\n'
b"Content-Transfer-Encoding: 7bit\r\n\r\n \r\n"
)
TEST_CONTENT_TEXT_BASE64 = (
b'Content-Type: text/plain; charset="utf-8"\r\n'
b"Content-Transfer-Encoding: base64\r\n\r\nVGVzdCBib2R5\r\n"
@ -108,6 +113,15 @@ TEST_CONTENT_MULTIPART = (
+ b"\r\n--Mark=_100584970350292485166--\r\n"
)
TEST_CONTENT_MULTIPART_EMPTY_PLAIN = (
b"\r\nThis is a multi-part message in MIME format.\r\n"
b"\r\n--Mark=_100584970350292485166\r\n"
+ TEST_CONTENT_TEXT_PLAIN_EMPTY
+ b"\r\n--Mark=_100584970350292485166\r\n"
+ TEST_CONTENT_HTML
+ b"\r\n--Mark=_100584970350292485166--\r\n"
)
TEST_CONTENT_MULTIPART_BASE64 = (
b"\r\nThis is a multi-part message in MIME format.\r\n"
b"\r\n--Mark=_100584970350292485166\r\n"
@ -155,6 +169,18 @@ TEST_FETCH_RESPONSE_TEXT_PLAIN = (
],
)
TEST_FETCH_RESPONSE_TEXT_PLAIN_EMPTY = (
"OK",
[
b"1 FETCH (BODY[] {"
+ str(len(TEST_MESSAGE + TEST_CONTENT_TEXT_PLAIN_EMPTY)).encode("utf-8")
+ b"}",
bytearray(TEST_MESSAGE + TEST_CONTENT_TEXT_PLAIN_EMPTY),
b")",
b"Fetch completed (0.0001 + 0.000 secs).",
],
)
TEST_FETCH_RESPONSE_TEXT_PLAIN_ALT = (
"OK",
[
@ -249,6 +275,19 @@ TEST_FETCH_RESPONSE_MULTIPART = (
b"Fetch completed (0.0001 + 0.000 secs).",
],
)
TEST_FETCH_RESPONSE_MULTIPART_EMPTY_PLAIN = (
"OK",
[
b"1 FETCH (BODY[] {"
+ str(len(TEST_MESSAGE_MULTIPART + TEST_CONTENT_MULTIPART_EMPTY_PLAIN)).encode(
"utf-8"
)
+ b"}",
bytearray(TEST_MESSAGE_MULTIPART + TEST_CONTENT_MULTIPART_EMPTY_PLAIN),
b")",
b"Fetch completed (0.0001 + 0.000 secs).",
],
)
TEST_FETCH_RESPONSE_MULTIPART_BASE64 = (
"OK",
[

View File

@ -29,6 +29,7 @@ from .const import (
TEST_FETCH_RESPONSE_MULTIPART,
TEST_FETCH_RESPONSE_MULTIPART_BASE64,
TEST_FETCH_RESPONSE_MULTIPART_BASE64_INVALID,
TEST_FETCH_RESPONSE_MULTIPART_EMPTY_PLAIN,
TEST_FETCH_RESPONSE_NO_SUBJECT_TO_FROM,
TEST_FETCH_RESPONSE_TEXT_BARE,
TEST_FETCH_RESPONSE_TEXT_OTHER,
@ -116,6 +117,7 @@ async def test_entry_startup_fails(
(TEST_FETCH_RESPONSE_TEXT_OTHER, True),
(TEST_FETCH_RESPONSE_HTML, True),
(TEST_FETCH_RESPONSE_MULTIPART, True),
(TEST_FETCH_RESPONSE_MULTIPART_EMPTY_PLAIN, True),
(TEST_FETCH_RESPONSE_MULTIPART_BASE64, True),
(TEST_FETCH_RESPONSE_BINARY, True),
],
@ -129,6 +131,7 @@ async def test_entry_startup_fails(
"other",
"html",
"multipart",
"multipart_empty_plain",
"multipart_base64",
"binary",
],