2018-10-01 09:21:00 +00:00
|
|
|
"""Tests for the Home Assistant Websocket API."""
|
2019-12-09 11:30:23 +00:00
|
|
|
from unittest.mock import Mock, patch
|
2018-10-01 09:21:00 +00:00
|
|
|
|
|
|
|
from aiohttp import WSMsgType
|
2019-03-27 17:40:39 +00:00
|
|
|
import voluptuous as vol
|
2018-10-01 09:21:00 +00:00
|
|
|
|
2019-03-06 03:31:26 +00:00
|
|
|
from homeassistant.components.websocket_api import const, messages
|
2018-10-01 09:21:00 +00:00
|
|
|
|
|
|
|
|
2020-01-01 23:15:29 +00:00
|
|
|
async def test_invalid_message_format(websocket_client):
|
2018-10-01 09:21:00 +00:00
|
|
|
"""Test sending invalid JSON."""
|
2020-01-01 23:15:29 +00:00
|
|
|
await websocket_client.send_json({"type": 5})
|
2018-10-01 09:21:00 +00:00
|
|
|
|
2020-01-01 23:15:29 +00:00
|
|
|
msg = await websocket_client.receive_json()
|
2018-10-01 09:21:00 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
assert msg["type"] == const.TYPE_RESULT
|
|
|
|
error = msg["error"]
|
|
|
|
assert error["code"] == const.ERR_INVALID_FORMAT
|
|
|
|
assert error["message"].startswith("Message incorrectly formatted")
|
2018-10-01 09:21:00 +00:00
|
|
|
|
|
|
|
|
2020-01-01 23:15:29 +00:00
|
|
|
async def test_invalid_json(websocket_client):
|
2018-10-01 09:21:00 +00:00
|
|
|
"""Test sending invalid JSON."""
|
2020-01-01 23:15:29 +00:00
|
|
|
await websocket_client.send_str("this is not JSON")
|
2018-10-01 09:21:00 +00:00
|
|
|
|
2020-01-01 23:15:29 +00:00
|
|
|
msg = await websocket_client.receive()
|
2018-10-01 09:21:00 +00:00
|
|
|
|
|
|
|
assert msg.type == WSMsgType.close
|
|
|
|
|
|
|
|
|
2020-01-01 23:15:29 +00:00
|
|
|
async def test_quiting_hass(hass, websocket_client):
|
2018-10-01 09:21:00 +00:00
|
|
|
"""Test sending invalid JSON."""
|
2019-07-31 19:25:30 +00:00
|
|
|
with patch.object(hass.loop, "stop"):
|
2020-01-01 23:15:29 +00:00
|
|
|
await hass.async_stop()
|
2018-10-01 09:21:00 +00:00
|
|
|
|
2020-01-01 23:15:29 +00:00
|
|
|
msg = await websocket_client.receive()
|
2018-10-01 09:21:00 +00:00
|
|
|
|
|
|
|
assert msg.type == WSMsgType.CLOSE
|
|
|
|
|
|
|
|
|
2020-01-01 23:15:29 +00:00
|
|
|
async def test_unknown_command(websocket_client):
|
2018-10-01 09:21:00 +00:00
|
|
|
"""Test get_panels command."""
|
2020-01-01 23:15:29 +00:00
|
|
|
await websocket_client.send_json({"id": 5, "type": "unknown_command"})
|
2018-10-01 09:21:00 +00:00
|
|
|
|
2020-01-01 23:15:29 +00:00
|
|
|
msg = await websocket_client.receive_json()
|
2019-07-31 19:25:30 +00:00
|
|
|
assert not msg["success"]
|
|
|
|
assert msg["error"]["code"] == const.ERR_UNKNOWN_COMMAND
|
2018-10-01 09:21:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_handler_failing(hass, websocket_client):
|
|
|
|
"""Test a command that raises."""
|
|
|
|
hass.components.websocket_api.async_register_command(
|
2019-07-31 19:25:30 +00:00
|
|
|
"bla",
|
|
|
|
Mock(side_effect=TypeError),
|
|
|
|
messages.BASE_COMMAND_MESSAGE_SCHEMA.extend({"type": "bla"}),
|
|
|
|
)
|
|
|
|
await websocket_client.send_json({"id": 5, "type": "bla"})
|
2018-10-01 09:21:00 +00:00
|
|
|
|
|
|
|
msg = await websocket_client.receive_json()
|
2019-07-31 19:25:30 +00:00
|
|
|
assert msg["id"] == 5
|
|
|
|
assert msg["type"] == const.TYPE_RESULT
|
|
|
|
assert not msg["success"]
|
|
|
|
assert msg["error"]["code"] == const.ERR_UNKNOWN_ERROR
|
2019-03-27 17:40:39 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_invalid_vol(hass, websocket_client):
|
|
|
|
"""Test a command that raises invalid vol error."""
|
|
|
|
hass.components.websocket_api.async_register_command(
|
2019-07-31 19:25:30 +00:00
|
|
|
"bla",
|
|
|
|
Mock(side_effect=TypeError),
|
|
|
|
messages.BASE_COMMAND_MESSAGE_SCHEMA.extend(
|
|
|
|
{"type": "bla", vol.Required("test_config"): str}
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
await websocket_client.send_json({"id": 5, "type": "bla", "test_config": 5})
|
2019-03-27 17:40:39 +00:00
|
|
|
|
|
|
|
msg = await websocket_client.receive_json()
|
2019-07-31 19:25:30 +00:00
|
|
|
assert msg["id"] == 5
|
|
|
|
assert msg["type"] == const.TYPE_RESULT
|
|
|
|
assert not msg["success"]
|
|
|
|
assert msg["error"]["code"] == const.ERR_INVALID_FORMAT
|
|
|
|
assert "expected str for dictionary value" in msg["error"]["message"]
|