From d11b47ea356b92405d67e4b10f998969692bec36 Mon Sep 17 00:00:00 2001 From: Kirill Lisov Date: Thu, 14 Dec 2023 16:33:54 +0300 Subject: [PATCH] Fix/empty attachment (#439) --- pybotx/models/attachments.py | 3 +++ pyproject.toml | 2 +- tests/test_attachments.py | 42 ++++++++++++++++++++++++++++++++++++ 3 files changed, 46 insertions(+), 1 deletion(-) diff --git a/pybotx/models/attachments.py b/pybotx/models/attachments.py index 30fe73a4..1a92488f 100644 --- a/pybotx/models/attachments.py +++ b/pybotx/models/attachments.py @@ -319,6 +319,9 @@ def convert_api_attachment_to_domain( # noqa: WPS212 def decode_rfc2397(encoded_content: str) -> bytes: # "data:image/gif;base64,aGVsbG8=" -> b"hello" + if not encoded_content: + return b"" + return base64.b64decode(encoded_content.split(",", 1)[1].encode()) diff --git a/pyproject.toml b/pyproject.toml index 9e1baaca..2ac7ebf8 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "pybotx" -version = "0.61.0" +version = "0.61.1" description = "A python library for interacting with eXpress BotX API" authors = [ "Sidnev Nikolay ", diff --git a/tests/test_attachments.py b/tests/test_attachments.py index 3585d3e3..f3fd15a3 100644 --- a/tests/test_attachments.py +++ b/tests/test_attachments.py @@ -306,3 +306,45 @@ async def test__async_execute_raw_bot_command__unknown_attachment_type( # - Assert - assert "Received unknown attachment type" in loguru_caplog.text + + +async def test__async_execute_raw_bot_command__empty_attachment( + api_incoming_message_factory: Callable[..., Dict[str, Any]], + bot_account: BotAccountWithSecret, + loguru_caplog: pytest.LogCaptureFixture, +) -> None: + # - Arrange - + empty_attachment = { + "data": { + "content": "", + "file_name": "empty_file.txt", + }, + "type": "document", + } + payload = api_incoming_message_factory(attachment=empty_attachment) + + collector = HandlerCollector() + incoming_message: Optional[IncomingMessage] = None + + @collector.default_message_handler + async def default_handler(message: IncomingMessage, bot: Bot) -> None: + nonlocal incoming_message + incoming_message = message + # Drop `raw_command` from asserting + incoming_message.raw_command = None + + built_bot = Bot(collectors=[collector], bot_accounts=[bot_account]) + + # - Act - + async with lifespan_wrapper(built_bot) as bot: + bot.async_execute_raw_bot_command(payload) + + # - Assert - + assert incoming_message + assert incoming_message.file == AttachmentDocument( + type=AttachmentTypes.DOCUMENT, + filename="empty_file.txt", + size=0, + is_async_file=False, + content=b"", + )