Files
core/tests/components/websocket_api/conftest.py
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

45 lines
1.2 KiB
Python
Raw Normal View History

2018-10-01 11:21:00 +02:00
"""Fixtures for websocket tests."""
from aiohttp.test_utils import TestClient
2018-10-01 11:21:00 +02:00
import pytest
2018-10-01 16:09:31 +02:00
from homeassistant.components.websocket_api.auth import TYPE_AUTH_REQUIRED
from homeassistant.components.websocket_api.http import URL
2023-06-19 18:27:22 -05:00
from homeassistant.core import HomeAssistant
from homeassistant.setup import async_setup_component
2018-10-01 11:21:00 +02:00
from tests.typing import (
ClientSessionGenerator,
MockHAClientWebSocket,
WebSocketGenerator,
)
2023-06-19 18:27:22 -05:00
2018-10-01 11:21:00 +02:00
@pytest.fixture
2023-06-19 18:27:22 -05:00
async def websocket_client(
hass: HomeAssistant, hass_ws_client: WebSocketGenerator
) -> MockHAClientWebSocket:
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
async def no_auth_websocket_client(
hass: HomeAssistant, hass_client_no_auth: ClientSessionGenerator
) -> TestClient:
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", {})
await hass.async_block_till_done()
2018-10-01 11:21:00 +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()
2018-10-01 16:09:31 +02:00
assert auth_ok["type"] == TYPE_AUTH_REQUIRED
2018-10-01 11:21:00 +02: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()