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