Skip to content

Commit

Permalink
feat: added logging flag for incoming requests data (#444)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexhook authored Jan 22, 2024
1 parent 8543ea1 commit c57b791
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 8 deletions.
19 changes: 12 additions & 7 deletions pybotx/bot/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,13 +271,18 @@ def __init__(

self.state: SimpleNamespace = SimpleNamespace()

def async_execute_raw_bot_command(self, raw_bot_command: Dict[str, Any]) -> None:
logger.opt(lazy=True).debug(
"Got command: {command}",
command=lambda: pformat_jsonable_obj(
trim_file_data_in_incoming_json(raw_bot_command),
),
)
def async_execute_raw_bot_command(
self,
raw_bot_command: Dict[str, Any],
logging_command: bool = True,
) -> None:
if logging_command:
logger.opt(lazy=True).debug(
"Got command: {command}",
command=lambda: pformat_jsonable_obj(
trim_file_data_in_incoming_json(raw_bot_command),
),
)

try:
bot_api_command: BotAPICommand = parse_obj_as(
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.4"
version = "0.62.0"
description = "A python library for interacting with eXpress BotX API"
authors = [
"Sidnev Nikolay <[email protected]>",
Expand Down
49 changes: 49 additions & 0 deletions tests/test_base_command.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import json
import logging
from typing import Any, Callable, Dict

import pytest

from pybotx import (
Bot,
BotAccountWithSecret,
HandlerCollector,
UnknownSystemEventError,
UnsupportedBotAPIVersionError,
Expand Down Expand Up @@ -90,3 +95,47 @@ async def test__async_execute_raw_bot_command__unknown_system_event() -> None:
# - Assert -
assert "Unknown system event" in str(exc.value)
assert "system:baz" in str(exc.value)


async def test__async_execute_raw_bot_command__logging_incoming_request(
bot_account: BotAccountWithSecret,
loguru_caplog: pytest.LogCaptureFixture,
api_incoming_message_factory: Callable[..., Dict[str, Any]],
mock_authorization: None,
) -> None:
# - Arrange -
payload = api_incoming_message_factory(bot_id=bot_account.id)
log_message = "Got command: {command}".format(
command=json.dumps(payload, sort_keys=True, indent=4, ensure_ascii=False),
)
built_bot = Bot(collectors=[HandlerCollector()], bot_accounts=[bot_account])

# - Act -
async with lifespan_wrapper(built_bot) as bot:
with loguru_caplog.at_level(logging.DEBUG):
bot.async_execute_raw_bot_command(payload)

# - Assert -
assert log_message in loguru_caplog.messages


async def test__async_execute_raw_bot_command__not_logging_incoming_request(
bot_account: BotAccountWithSecret,
loguru_caplog: pytest.LogCaptureFixture,
api_incoming_message_factory: Callable[..., Dict[str, Any]],
mock_authorization: None,
) -> None:
# - Arrange -
payload = api_incoming_message_factory(bot_id=bot_account.id)
log_message = "Got command: {command}".format(
command=json.dumps(payload, sort_keys=True, indent=4, ensure_ascii=False),
)
built_bot = Bot(collectors=[HandlerCollector()], bot_accounts=[bot_account])

# - Act -
async with lifespan_wrapper(built_bot) as bot:
with loguru_caplog.at_level(logging.DEBUG):
bot.async_execute_raw_bot_command(payload, logging_command=False)

# - Assert -
assert log_message not in loguru_caplog.messages

0 comments on commit c57b791

Please sign in to comment.