1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
|
"""Tests for the connection module / Part 6 / Logging."""
from unittest.mock import patch
import pytest
from asusrouter.config import ARConfig, ARConfigKey as ARConfKey
from asusrouter.modules.endpoint import (
EndpointControl,
EndpointService,
EndpointType,
)
from asusrouter.tools.security import ARSecurityLevel
from tests.helpers import ConnectionFactory, SyncPatch
class TestConnectionLogging:
"""Test for the Connection class logging."""
LOGIN_ENDPOINT = EndpointService.LOGIN
SENSITIVE_ENDPOINT = EndpointControl.APPLY
SAFE_ENDPOINT = EndpointService.LOGOUT
@pytest.mark.asyncio
@pytest.mark.parametrize(
("level", "endpoint", "payload", "expected"),
[
# STRICT: never log payloads
(ARSecurityLevel.STRICT, SAFE_ENDPOINT, "not-a-secret", None),
# Login: never log payload even in unsafe mode
(ARSecurityLevel.UNSAFE, LOGIN_ENDPOINT, "top-secret", None),
# DEFAULT: non-sensitive
(
ARSecurityLevel.DEFAULT,
SAFE_ENDPOINT,
"not-a-secret",
"not-a-secret",
),
# DEFAULT: sensitive
(ARSecurityLevel.DEFAULT, SENSITIVE_ENDPOINT, "secret", None),
# SANITIZED: non-sensitive
(
ARSecurityLevel.SANITIZED,
SAFE_ENDPOINT,
"not-a-secret",
"not-a-secret",
),
# SANITIZED: sensitive
(
ARSecurityLevel.SANITIZED,
SENSITIVE_ENDPOINT,
"secret",
"[SANITIZED PLACEHOLDER]",
),
# UNSAFE: non-sensitive
(
ARSecurityLevel.UNSAFE,
SAFE_ENDPOINT,
"not-a-secret",
"not-a-secret",
),
# UNSAFE: sensitive
(ARSecurityLevel.UNSAFE, SENSITIVE_ENDPOINT, "secret", "secret"),
# Empty payload
(ARSecurityLevel.DEFAULT, SAFE_ENDPOINT, "", None),
# None payload
(ARSecurityLevel.DEFAULT, SAFE_ENDPOINT, None, None),
],
ids=[
"strict",
"login",
"default_safe",
"default_sensitive",
"sanitized_safe",
"sanitized_sensitive",
"unsafe_safe",
"unsafe_sensitive",
"empty_payload",
"none_payload",
],
)
async def test_payload_for_logging(
self,
level: ARSecurityLevel,
endpoint: EndpointType,
payload: str | None,
expected: str | None,
connection_factory: ConnectionFactory,
) -> None:
"""Test _payload_for_logging method."""
conn = connection_factory()
result = conn._payload_for_logging(level, endpoint, payload)
assert result == expected
@pytest.mark.asyncio
@pytest.mark.parametrize(
("endpoint", "payload", "level"),
[
(EndpointService.LOGIN, "payload", ARSecurityLevel.DEFAULT),
(EndpointService.LOGIN, None, ARSecurityLevel.SANITIZED),
],
)
async def test_log_request(
self,
endpoint: EndpointType,
payload: str | None,
level: ARSecurityLevel,
connection_factory: ConnectionFactory,
payload_for_logging: SyncPatch,
) -> None:
"""Test _log_request method."""
connection = connection_factory()
# Set logging config
ARConfig.set(ARConfKey.DEBUG_PAYLOAD, level)
# Prepare a mock for payload preparation
mock_payload_for_logging = payload_for_logging(connection)
mock_payload_for_logging.return_value = payload
# Mock the logger
with patch("asusrouter.connection._LOGGER.debug") as mock_logger:
# Call the method
connection._log_request(endpoint, payload)
# Check that a correct log message was generated
if payload is not None:
mock_logger.assert_called_once_with(
"Sending request to `%s` with payload: %s",
endpoint,
payload,
)
else:
mock_logger.assert_called_once_with(
"Sending request to `%s`", endpoint
)
mock_payload_for_logging.assert_called_once_with(
level, endpoint, payload
)
|