Skip to content

Commit 5632dbe

Browse files
jcfrtsijis
andauthored
Log filtered command name (#1631)
* Log filtered command name If a command is blocked by a cmdfilter, the name of the command should be logged instead of logged as "None". * Update cmdfilter docstring Returning cmd and args is not used by anything but for logging purposes. * test: add tests to validate logging of cmdfilter * docs: add info to CHANGES --------- Co-authored-by: Sijis Aviles <sijis.aviles@gmail.com>
1 parent 74b732c commit 5632dbe

4 files changed

Lines changed: 72 additions & 4 deletions

File tree

CHANGES.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ fixes:
3434
- chore: update Dockerfile python version (#1744)
3535
- chore: update errbot-backend-slackv3 version to 0.3.2 (#1743)
3636
- fix: allow webtest to work on python 3.13 (#1729)
37+
- fix: add command in logged when blocked by cmdfilter (#1631)
3738

3839

3940
v6.2.0 (2024-01-01)

errbot/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -562,7 +562,7 @@ def some_filter(self, msg, cmd, args, dry_run):
562562
command is authorized or not.
563563
\"\"\"
564564
# If wishing to block the incoming command:
565-
return None, None, None
565+
return None, cmd, args
566566
# Otherwise pass data through to the (potential) next filter:
567567
return msg, cmd, args
568568

errbot/core.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,7 @@ def _process_command_filters(
420420
for cmd_filter in self.command_filters:
421421
msg, cmd, args = cmd_filter(msg, cmd, args, dry_run)
422422
if msg is None:
423-
return None, None, None
423+
return None, cmd, args
424424
return msg, cmd, args
425425
except Exception:
426426
log.exception(
@@ -434,7 +434,7 @@ def _process_command(self, msg, cmd, args, match):
434434
# first it must go through the command filters
435435
msg, cmd, args = self._process_command_filters(msg, cmd, args, False)
436436
if msg is None:
437-
log.info("Command %s blocked or deferred.", cmd)
437+
log.info("Command \"%s\" blocked or deferred.", cmd)
438438
return
439439

440440
frm = msg.frm

tests/core_test.py

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
"""Test _admins_to_notify wrapper functionality"""
1+
"""Tests for errbot.core internals."""
2+
import logging
3+
4+
from errbot.core import ErrBot
25

36
extra_config = {"BOT_ADMINS_NOTIFICATIONS": "zoni@localdomain"}
47

@@ -13,3 +16,67 @@ def test_admins_not_notified(testbot):
1316
"""Test which admins will not be notified"""
1417
notified_admins = testbot._bot._admins_to_notify()
1518
assert "gbin@local" not in notified_admins
19+
20+
21+
# --- _process_command_filters --------------------------------------------
22+
# Regression tests for PR #1631: when a cmdfilter blocks a command by
23+
# returning ``(None, cmd, args)``, the command name must propagate back to
24+
# the caller so it can be logged instead of "None".
25+
26+
27+
class _FilterStub:
28+
"""Minimal stand-in for an ErrBot exposing only what
29+
``_process_command_filters`` touches: a ``command_filters`` list."""
30+
31+
def __init__(self, filters):
32+
self.command_filters = filters
33+
34+
35+
def _block_filter(msg, cmd, args, dry_run):
36+
return None, cmd, args
37+
38+
39+
def _passthrough_filter(msg, cmd, args, dry_run):
40+
return msg, cmd, args
41+
42+
43+
def _raising_filter(msg, cmd, args, dry_run):
44+
raise RuntimeError("boom")
45+
46+
47+
def test_blocked_command_preserves_cmd_and_args():
48+
stub = _FilterStub([_block_filter])
49+
msg, cmd, args = ErrBot._process_command_filters(
50+
stub, msg="hello", cmd="mycmd", args=("a", "b")
51+
)
52+
assert msg is None
53+
assert cmd == "mycmd"
54+
assert args == ("a", "b")
55+
56+
57+
def test_passthrough_filter_returns_unchanged():
58+
stub = _FilterStub([_passthrough_filter])
59+
msg, cmd, args = ErrBot._process_command_filters(
60+
stub, msg="hello", cmd="mycmd", args=("a",)
61+
)
62+
assert msg == "hello"
63+
assert cmd == "mycmd"
64+
assert args == ("a",)
65+
66+
67+
def test_raising_filter_blocks_with_none_tuple():
68+
stub = _FilterStub([_raising_filter])
69+
msg, cmd, args = ErrBot._process_command_filters(
70+
stub, msg="hello", cmd="mycmd", args=("a",)
71+
)
72+
assert (msg, cmd, args) == (None, None, None)
73+
74+
75+
def test_blocked_command_logs_cmd_name(caplog):
76+
stub = _FilterStub([_block_filter])
77+
with caplog.at_level(logging.INFO, logger="errbot.core"):
78+
msg, cmd, args = ErrBot._process_command_filters(
79+
stub, msg="hello", cmd="mycmd", args=()
80+
)
81+
assert msg is None
82+
assert cmd == "mycmd"

0 commit comments

Comments
 (0)