Adding a filter at interpreter startup created Red-Lavalink's logger before Red set its logger class, so the logger lacked trace() and verbose() and Audio could not connect. Wrapping the record factory touches no logger; a test checks that importing the module creates none. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01GUebqhE8dK19vyn4Ct5g1L
58 lines
2.0 KiB
Python
58 lines
2.0 KiB
Python
"""The Lavalink password never reaches the logs."""
|
|
|
|
import logging
|
|
import subprocess
|
|
import sys
|
|
|
|
import pytest
|
|
|
|
from dogebot import logfilter
|
|
|
|
|
|
def test_password_is_redacted(caplog: pytest.LogCaptureFixture) -> None:
|
|
"""Red-Lavalink's connect message keeps everything but the password."""
|
|
logfilter.install()
|
|
logger = logging.getLogger(logfilter.LOGGER)
|
|
headers = {"Authorization": "hunter2", "User-Id": "42"}
|
|
with caplog.at_level(logging.INFO, logger=logfilter.LOGGER):
|
|
logger.info("connect %s headers %s", "ws://x", headers)
|
|
assert "hunter2" not in caplog.text
|
|
assert "<redacted>" in caplog.text
|
|
assert "'User-Id': '42'" in caplog.text
|
|
assert headers["Authorization"] == "hunter2"
|
|
|
|
|
|
def test_other_loggers_are_untouched(caplog: pytest.LogCaptureFixture) -> None:
|
|
"""Only Red-Lavalink's records are rewritten."""
|
|
logfilter.install()
|
|
with caplog.at_level(logging.INFO, logger="other"):
|
|
logging.getLogger("other").info("%s", {"Authorization": "x"})
|
|
assert "'Authorization': 'x'" in caplog.text
|
|
|
|
|
|
def test_install_is_idempotent() -> None:
|
|
"""Installing twice wraps the factory only once."""
|
|
logfilter.install()
|
|
factory = logging.getLogRecordFactory()
|
|
logfilter.install()
|
|
assert logging.getLogRecordFactory() is factory
|
|
|
|
|
|
def test_import_creates_no_logger() -> None:
|
|
"""Importing it early must not create Red-Lavalink's logger.
|
|
|
|
Red sets its own logger class later; a logger created before that
|
|
lacks the trace() and verbose() methods Red-Lavalink calls.
|
|
"""
|
|
code = (
|
|
"import logging, dogebot.logfilter as f; "
|
|
"print(f.LOGGER in logging.Logger.manager.loggerDict)"
|
|
)
|
|
out = subprocess.run( # noqa: S603
|
|
[sys.executable, "-c", code],
|
|
capture_output=True,
|
|
text=True,
|
|
check=True,
|
|
)
|
|
assert out.stdout.strip() == "False"
|