Improve websocket api coverage and typing (#94891)

This commit is contained in:
J. Nick Koston 2023-06-20 15:21:24 +01:00 committed by GitHub
parent b51dcb600e
commit 3f18f515e7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 246 additions and 31 deletions

View file

@ -2,6 +2,7 @@
from unittest.mock import patch
import aiohttp
from aiohttp import WSMsgType
import pytest
from homeassistant.auth.providers.legacy_api_password import (
@ -223,3 +224,79 @@ async def test_auth_close_after_revoke(
msg = await websocket_client.receive()
assert msg.type == aiohttp.WSMsgType.CLOSE
assert websocket_client.closed
async def test_auth_sending_invalid_json_disconnects(
hass: HomeAssistant, hass_client_no_auth: ClientSessionGenerator
) -> None:
"""Test sending invalid json during auth."""
assert await async_setup_component(hass, "websocket_api", {})
await hass.async_block_till_done()
client = await hass_client_no_auth()
async with client.ws_connect(URL) as ws:
auth_msg = await ws.receive_json()
assert auth_msg["type"] == TYPE_AUTH_REQUIRED
await ws.send_str("[--INVALID--JSON--]")
auth_msg = await ws.receive()
assert auth_msg.type == WSMsgType.close
async def test_auth_sending_binary_disconnects(
hass: HomeAssistant, hass_client_no_auth: ClientSessionGenerator
) -> None:
"""Test sending bytes during auth."""
assert await async_setup_component(hass, "websocket_api", {})
await hass.async_block_till_done()
client = await hass_client_no_auth()
async with client.ws_connect(URL) as ws:
auth_msg = await ws.receive_json()
assert auth_msg["type"] == TYPE_AUTH_REQUIRED
await ws.send_bytes(b"[INVALID]")
auth_msg = await ws.receive()
assert auth_msg.type == WSMsgType.close
async def test_auth_close_disconnects(
hass: HomeAssistant, hass_client_no_auth: ClientSessionGenerator
) -> None:
"""Test closing during auth."""
assert await async_setup_component(hass, "websocket_api", {})
await hass.async_block_till_done()
client = await hass_client_no_auth()
async with client.ws_connect(URL) as ws:
auth_msg = await ws.receive_json()
assert auth_msg["type"] == TYPE_AUTH_REQUIRED
await ws.close()
auth_msg = await ws.receive()
assert auth_msg.type == WSMsgType.CLOSED
async def test_auth_sending_unknown_type_disconnects(
hass: HomeAssistant, hass_client_no_auth: ClientSessionGenerator
) -> None:
"""Test sending unknown type during auth."""
assert await async_setup_component(hass, "websocket_api", {})
await hass.async_block_till_done()
client = await hass_client_no_auth()
async with client.ws_connect(URL) as ws:
auth_msg = await ws.receive_json()
assert auth_msg["type"] == TYPE_AUTH_REQUIRED
# pylint: disable-next=protected-access
await ws._writer._send_frame(b"1" * 130, 0x30)
auth_msg = await ws.receive()
assert auth_msg.type == WSMsgType.close