2018-10-01 09:21:00 +00:00
|
|
|
"""Fixtures for websocket tests."""
|
|
|
|
import pytest
|
|
|
|
|
2018-10-01 14:09:31 +00:00
|
|
|
from homeassistant.components.websocket_api.auth import TYPE_AUTH_REQUIRED
|
2019-12-09 11:30:23 +00:00
|
|
|
from homeassistant.components.websocket_api.http import URL
|
|
|
|
from homeassistant.setup import async_setup_component
|
2018-10-01 09:21:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
2020-04-14 01:50:36 +00:00
|
|
|
async def websocket_client(hass, hass_ws_client):
|
2018-10-01 09:21:00 +00:00
|
|
|
"""Create a websocket client."""
|
2020-04-14 01:50:36 +00:00
|
|
|
return await hass_ws_client(hass)
|
2018-10-01 09:21:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
2020-04-14 01:50:36 +00:00
|
|
|
async def no_auth_websocket_client(hass, aiohttp_client):
|
2018-10-01 09:21:00 +00:00
|
|
|
"""Websocket connection that requires authentication."""
|
2020-04-14 01:50:36 +00:00
|
|
|
assert await async_setup_component(hass, "websocket_api", {})
|
2018-10-01 09:21:00 +00:00
|
|
|
|
2020-04-14 01:50:36 +00:00
|
|
|
client = await aiohttp_client(hass.http.app)
|
|
|
|
ws = await client.ws_connect(URL)
|
2018-10-01 09:21:00 +00:00
|
|
|
|
2020-04-14 01:50:36 +00:00
|
|
|
auth_ok = await ws.receive_json()
|
2019-07-31 19:25:30 +00:00
|
|
|
assert auth_ok["type"] == TYPE_AUTH_REQUIRED
|
2018-10-01 09:21:00 +00:00
|
|
|
|
|
|
|
yield ws
|
|
|
|
|
|
|
|
if not ws.closed:
|
2020-04-14 01:50:36 +00:00
|
|
|
await ws.close()
|