From 5a7332a13507820300f4992c752ea278f666f971 Mon Sep 17 00:00:00 2001 From: Rami Mosleh Date: Thu, 6 Jun 2024 16:10:03 +0400 Subject: [PATCH] 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 --- homeassistant/components/imap/coordinator.py | 6 +-- tests/components/imap/const.py | 39 ++++++++++++++++++++ tests/components/imap/test_init.py | 3 ++ 3 files changed, 45 insertions(+), 3 deletions(-) diff --git a/homeassistant/components/imap/coordinator.py b/homeassistant/components/imap/coordinator.py index c0123b89ee4..a9d0fdfbd48 100644 --- a/homeassistant/components/imap/coordinator.py +++ b/homeassistant/components/imap/coordinator.py @@ -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()) diff --git a/tests/components/imap/const.py b/tests/components/imap/const.py index 677eea7a473..037960c9e5d 100644 --- a/tests/components/imap/const.py +++ b/tests/components/imap/const.py @@ -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", [ diff --git a/tests/components/imap/test_init.py b/tests/components/imap/test_init.py index e6e6ffe7114..fe10770fc64 100644 --- a/tests/components/imap/test_init.py +++ b/tests/components/imap/test_init.py @@ -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", ],