|
2 | 2 |
|
3 | 3 | import asyncio |
4 | 4 | from datetime import timedelta |
| 5 | +import logging |
5 | 6 | from typing import Any, cast |
6 | 7 | from unittest.mock import patch |
7 | 8 |
|
|
20 | 21 | from homeassistant.util.dt import utcnow |
21 | 22 |
|
22 | 23 | from tests.common import async_call_logger_set_level, async_fire_time_changed |
23 | | -from tests.typing import MockHAClientWebSocket, WebSocketGenerator |
| 24 | +from tests.typing import ( |
| 25 | + ClientSessionGenerator, |
| 26 | + MockHAClientWebSocket, |
| 27 | + WebSocketGenerator, |
| 28 | +) |
24 | 29 |
|
25 | 30 |
|
26 | 31 | @pytest.fixture |
@@ -400,6 +405,48 @@ async def test_prepare_fail_connection_reset( |
400 | 405 | assert "Connection reset by peer while preparing WebSocket" in caplog.text |
401 | 406 |
|
402 | 407 |
|
| 408 | +async def test_auth_timeout_logs_at_debug( |
| 409 | + hass: HomeAssistant, |
| 410 | + hass_client: ClientSessionGenerator, |
| 411 | + caplog: pytest.LogCaptureFixture, |
| 412 | +) -> None: |
| 413 | + """Test auth timeout is logged at debug level not warning.""" |
| 414 | + # Setup websocket API |
| 415 | + assert await async_setup_component(hass, "websocket_api", {}) |
| 416 | + |
| 417 | + client = await hass_client() |
| 418 | + |
| 419 | + # Patch the auth timeout to be very short (0.001 seconds) |
| 420 | + with ( |
| 421 | + caplog.at_level(logging.DEBUG, "homeassistant.components.websocket_api"), |
| 422 | + patch( |
| 423 | + "homeassistant.components.websocket_api.http.AUTH_MESSAGE_TIMEOUT", 0.001 |
| 424 | + ), |
| 425 | + ): |
| 426 | + # Try to connect - will timeout quickly since we don't send auth |
| 427 | + ws = await client.ws_connect("/api/websocket") |
| 428 | + # Wait a bit for the timeout to trigger and cleanup to complete |
| 429 | + await asyncio.sleep(0.1) |
| 430 | + await ws.close() |
| 431 | + await asyncio.sleep(0.1) |
| 432 | + |
| 433 | + # Check that "Did not receive auth message" is logged at debug, not warning |
| 434 | + debug_messages = [ |
| 435 | + r.message for r in caplog.records if r.levelno == logging.DEBUG |
| 436 | + ] |
| 437 | + assert any( |
| 438 | + "Disconnected during auth phase: Did not receive auth message" in msg |
| 439 | + for msg in debug_messages |
| 440 | + ) |
| 441 | + |
| 442 | + # Check it's NOT logged at warning level |
| 443 | + warning_messages = [ |
| 444 | + r.message for r in caplog.records if r.levelno >= logging.WARNING |
| 445 | + ] |
| 446 | + for msg in warning_messages: |
| 447 | + assert "Did not receive auth message" not in msg |
| 448 | + |
| 449 | + |
403 | 450 | async def test_enable_coalesce( |
404 | 451 | hass: HomeAssistant, |
405 | 452 | hass_ws_client: WebSocketGenerator, |
|
0 commit comments