2018-05-01 17:35:23 +00:00
|
|
|
"""Fixtures for component testing."""
|
2018-09-11 16:08:03 +00:00
|
|
|
from unittest.mock import patch
|
|
|
|
|
2018-05-01 17:35:23 +00:00
|
|
|
import pytest
|
|
|
|
|
|
|
|
from homeassistant.setup import async_setup_component
|
2018-10-01 14:09:31 +00:00
|
|
|
from homeassistant.components.websocket_api.http import URL
|
|
|
|
from homeassistant.components.websocket_api.auth import (
|
2019-07-31 19:25:30 +00:00
|
|
|
TYPE_AUTH,
|
|
|
|
TYPE_AUTH_OK,
|
|
|
|
TYPE_AUTH_REQUIRED,
|
|
|
|
)
|
2018-05-01 17:35:23 +00:00
|
|
|
|
2018-12-02 15:32:53 +00:00
|
|
|
from tests.common import mock_coro
|
2018-11-21 19:55:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
|
|
def prevent_io():
|
|
|
|
"""Fixture to prevent certain I/O from happening."""
|
2019-07-31 19:25:30 +00:00
|
|
|
with patch(
|
|
|
|
"homeassistant.components.http.ban.async_load_ip_bans_config",
|
|
|
|
side_effect=lambda *args: mock_coro([]),
|
|
|
|
):
|
2018-11-21 19:55:21 +00:00
|
|
|
yield
|
2018-05-10 08:38:11 +00:00
|
|
|
|
2018-05-01 17:35:23 +00:00
|
|
|
|
|
|
|
@pytest.fixture
|
2018-12-02 15:32:53 +00:00
|
|
|
def hass_ws_client(aiohttp_client, hass_access_token):
|
2018-05-01 17:35:23 +00:00
|
|
|
"""Websocket client fixture connected to websocket server."""
|
2019-07-31 19:25:30 +00:00
|
|
|
|
2018-12-02 15:32:53 +00:00
|
|
|
async def create_client(hass, access_token=hass_access_token):
|
2018-05-01 17:35:23 +00:00
|
|
|
"""Create a websocket client."""
|
2019-07-31 19:25:30 +00:00
|
|
|
assert await async_setup_component(hass, "websocket_api", {})
|
2018-05-01 17:35:23 +00:00
|
|
|
|
|
|
|
client = await aiohttp_client(hass.http.app)
|
2018-07-13 13:31:20 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
with patch("homeassistant.components.http.auth.setup_auth"):
|
2018-10-01 14:09:31 +00:00
|
|
|
websocket = await client.ws_connect(URL)
|
2018-09-11 16:08:03 +00:00
|
|
|
auth_resp = await websocket.receive_json()
|
2019-07-31 19:25:30 +00:00
|
|
|
assert auth_resp["type"] == TYPE_AUTH_REQUIRED
|
2018-10-01 14:09:31 +00:00
|
|
|
|
|
|
|
if access_token is None:
|
2019-07-31 19:25:30 +00:00
|
|
|
await websocket.send_json({"type": TYPE_AUTH, "api_password": "bla"})
|
2018-10-01 14:09:31 +00:00
|
|
|
else:
|
2019-07-31 19:25:30 +00:00
|
|
|
await websocket.send_json(
|
|
|
|
{"type": TYPE_AUTH, "access_token": access_token}
|
|
|
|
)
|
2018-07-13 13:31:20 +00:00
|
|
|
|
2018-09-11 16:08:03 +00:00
|
|
|
auth_ok = await websocket.receive_json()
|
2019-07-31 19:25:30 +00:00
|
|
|
assert auth_ok["type"] == TYPE_AUTH_OK
|
2018-07-13 13:31:20 +00:00
|
|
|
|
2018-09-11 10:05:15 +00:00
|
|
|
# wrap in client
|
|
|
|
websocket.client = client
|
2018-05-01 17:35:23 +00:00
|
|
|
return websocket
|
|
|
|
|
|
|
|
return create_client
|