Skip to content

Commit

Permalink
Fix/empty attachment (#439)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kiruha01 committed Dec 14, 2023
1 parent 69d5f34 commit d11b47e
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 1 deletion.
3 changes: 3 additions & 0 deletions pybotx/models/attachments.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())


Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>",
Expand Down
42 changes: 42 additions & 0 deletions tests/test_attachments.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"",
)

0 comments on commit d11b47e

Please sign in to comment.