mirror of
https://github.com/python-kasa/python-kasa.git
synced 2026-11-22 05:53:33 +00:00
Keep tests from changing logger levels and fixture data for later tests (#1844)
Some checks failed
CI / Perform Lint Checks (3.15) (push) Has been cancelled
CodeQL Checks / Analyze (python) (push) Has been cancelled
CI / Python 3.11 on macos-latest (push) Has been cancelled
CI / Python 3.12 on macos-latest (push) Has been cancelled
CI / Python 3.13 on macos-latest (push) Has been cancelled
CI / Python 3.14 on macos-latest (push) Has been cancelled
CI / Python 3.15 on macos-latest (push) Has been cancelled
CI / Python 3.11 on ubuntu-latest (push) Has been cancelled
CI / Python 3.12 on ubuntu-latest (push) Has been cancelled
CI / Python 3.13 on ubuntu-latest (push) Has been cancelled
CI / Python 3.14 on ubuntu-latest (push) Has been cancelled
CI / Python 3.15 on ubuntu-latest (push) Has been cancelled
CI / Python 3.11 on windows-latest (push) Has been cancelled
CI / Python 3.12 on windows-latest (push) Has been cancelled
CI / Python 3.13 on windows-latest (push) Has been cancelled
CI / Python 3.14 on windows-latest (push) Has been cancelled
CI / Python 3.15 on windows-latest (push) Has been cancelled
Stale / stale (push) Has been cancelled
Some checks failed
CI / Perform Lint Checks (3.15) (push) Has been cancelled
CodeQL Checks / Analyze (python) (push) Has been cancelled
CI / Python 3.11 on macos-latest (push) Has been cancelled
CI / Python 3.12 on macos-latest (push) Has been cancelled
CI / Python 3.13 on macos-latest (push) Has been cancelled
CI / Python 3.14 on macos-latest (push) Has been cancelled
CI / Python 3.15 on macos-latest (push) Has been cancelled
CI / Python 3.11 on ubuntu-latest (push) Has been cancelled
CI / Python 3.12 on ubuntu-latest (push) Has been cancelled
CI / Python 3.13 on ubuntu-latest (push) Has been cancelled
CI / Python 3.14 on ubuntu-latest (push) Has been cancelled
CI / Python 3.15 on ubuntu-latest (push) Has been cancelled
CI / Python 3.11 on windows-latest (push) Has been cancelled
CI / Python 3.12 on windows-latest (push) Has been cancelled
CI / Python 3.13 on windows-latest (push) Has been cancelled
CI / Python 3.14 on windows-latest (push) Has been cancelled
CI / Python 3.15 on windows-latest (push) Has been cancelled
Stale / stale (push) Has been cancelled
Some tests leave the `kasa` logger at a different level or rename devices in shared fixture data. Later log assertions and device dump comparisons then pass or fail depending on the test order. This PR restores logger levels through `caplog` and renames devices on copies, so the affected tests pass independently of their order. In details: - Use `caplog.set_level` for tests that change the `kasa` logger, and remove an unused logger override. - Add an autouse fixture that restores any changed logger levels and fails the test with a pointer to `caplog.set_level`. - Rename README devices on deep copies of the fixture data. - Cover KLAP handshake debug logging explicitly, instead of relying on an earlier test leaving DEBUG enabled. Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -222,7 +222,7 @@ async def test_protocol_logging(
|
||||
mocker: MockerFixture, caplog: pytest.LogCaptureFixture, log_level: int
|
||||
) -> None:
|
||||
caplog.set_level(log_level)
|
||||
logging.getLogger("kasa").setLevel(log_level)
|
||||
caplog.set_level(log_level, logger="kasa")
|
||||
|
||||
def _return_encrypted(*_, **__):
|
||||
# Devices sign the response for the sequence number of the request
|
||||
@@ -583,6 +583,34 @@ async def test_handshake(
|
||||
await protocol.close()
|
||||
|
||||
|
||||
@pytest.mark.xdist_group(name="caplog")
|
||||
async def test_handshake_debug_logging(
|
||||
mocker: MockerFixture, caplog: pytest.LogCaptureFixture
|
||||
) -> None:
|
||||
"""Both handshake steps are logged when debug logging is enabled."""
|
||||
caplog.set_level(logging.DEBUG, logger="kasa")
|
||||
server_seed = secrets.token_bytes(16)
|
||||
credentials = Credentials("foo", "bar")
|
||||
device_auth_hash = KlapTransport.generate_auth_hash(credentials)
|
||||
|
||||
async def _return_handshake_response(url: URL, params=None, data=None, *_, **__):
|
||||
if url == URL("http://127.0.0.1:80/app/handshake1"):
|
||||
seed_auth_hash = _sha256(data + device_auth_hash)
|
||||
return _mock_response(200, server_seed + seed_auth_hash)
|
||||
return _mock_response(200, b"")
|
||||
|
||||
mocker.patch.object(
|
||||
aiohttp.ClientSession, "post", side_effect=_return_handshake_response
|
||||
)
|
||||
transport = KlapTransport(config=DeviceConfig("127.0.0.1", credentials=credentials))
|
||||
|
||||
await transport.perform_handshake()
|
||||
assert "Handshake1 posted" in caplog.text
|
||||
assert "Handshake1 success" in caplog.text
|
||||
assert "Handshake2 posted" in caplog.text
|
||||
await transport.close()
|
||||
|
||||
|
||||
async def test_query(mocker: MockerFixture) -> None:
|
||||
client_seed = None
|
||||
last_seq = None
|
||||
|
||||
Reference in New Issue
Block a user