1
0
mirror of https://github.com/home-assistant/core.git synced 2025-12-24 12:59:34 +00:00

Do async_setup_platform in background (#36244)

Co-authored-by: J. Nick Koston <nick@koston.org>
This commit is contained in:
Paulus Schoutsen
2020-05-31 22:18:30 -07:00
committed by GitHub
parent d488c779fc
commit 276f3afb00
161 changed files with 1184 additions and 305 deletions

View File

@@ -16,6 +16,7 @@ async def websocket_client(hass, hass_ws_client):
async def no_auth_websocket_client(hass, aiohttp_client):
"""Websocket connection that requires authentication."""
assert await async_setup_component(hass, "websocket_api", {})
await hass.async_block_till_done()
client = await aiohttp_client(hass.http.app)
ws = await client.ws_connect(URL)
@@ -23,6 +24,7 @@ async def no_auth_websocket_client(hass, aiohttp_client):
auth_ok = await ws.receive_json()
assert auth_ok["type"] == TYPE_AUTH_REQUIRED
ws.client = client
yield ws
if not ws.closed:

View File

@@ -1,6 +1,8 @@
"""Test auth of websocket API."""
from unittest.mock import patch
import pytest
from homeassistant.components.websocket_api.auth import (
TYPE_AUTH,
TYPE_AUTH_INVALID,
@@ -12,33 +14,51 @@ from homeassistant.components.websocket_api.const import (
SIGNAL_WEBSOCKET_DISCONNECTED,
URL,
)
from homeassistant.core import callback
from homeassistant.setup import async_setup_component
from tests.common import mock_coro
async def test_auth_events(
hass, no_auth_websocket_client, legacy_auth, hass_access_token
):
"""Test authenticating."""
@pytest.fixture
def track_connected(hass):
"""Track connected and disconnected events."""
connected_evt = []
@callback
def track_connected():
connected_evt.append(1)
hass.helpers.dispatcher.async_dispatcher_connect(
SIGNAL_WEBSOCKET_CONNECTED, lambda: connected_evt.append(1)
SIGNAL_WEBSOCKET_CONNECTED, track_connected
)
disconnected_evt = []
@callback
def track_disconnected():
disconnected_evt.append(1)
hass.helpers.dispatcher.async_dispatcher_connect(
SIGNAL_WEBSOCKET_DISCONNECTED, lambda: disconnected_evt.append(1)
SIGNAL_WEBSOCKET_DISCONNECTED, track_disconnected
)
return {"connected": connected_evt, "disconnected": disconnected_evt}
async def test_auth_events(
hass, no_auth_websocket_client, legacy_auth, hass_access_token, track_connected
):
"""Test authenticating."""
await test_auth_active_with_token(hass, no_auth_websocket_client, hass_access_token)
assert len(connected_evt) == 1
assert not disconnected_evt
assert len(track_connected["connected"]) == 1
assert not track_connected["disconnected"]
await no_auth_websocket_client.close()
await hass.async_block_till_done()
assert len(disconnected_evt) == 1
assert len(track_connected["disconnected"]) == 1
async def test_auth_via_msg_incorrect_pass(no_auth_websocket_client):
@@ -58,27 +78,18 @@ async def test_auth_via_msg_incorrect_pass(no_auth_websocket_client):
assert msg["message"] == "Invalid access token or password"
async def test_auth_events_incorrect_pass(hass, no_auth_websocket_client):
async def test_auth_events_incorrect_pass(no_auth_websocket_client, track_connected):
"""Test authenticating."""
connected_evt = []
hass.helpers.dispatcher.async_dispatcher_connect(
SIGNAL_WEBSOCKET_CONNECTED, lambda: connected_evt.append(1)
)
disconnected_evt = []
hass.helpers.dispatcher.async_dispatcher_connect(
SIGNAL_WEBSOCKET_DISCONNECTED, lambda: disconnected_evt.append(1)
)
await test_auth_via_msg_incorrect_pass(no_auth_websocket_client)
assert not connected_evt
assert not disconnected_evt
assert not track_connected["connected"]
assert not track_connected["disconnected"]
await no_auth_websocket_client.close()
await hass.async_block_till_done()
assert not connected_evt
assert not disconnected_evt
assert not track_connected["connected"]
assert not track_connected["disconnected"]
async def test_pre_auth_only_auth_allowed(no_auth_websocket_client):
@@ -102,13 +113,11 @@ async def test_auth_active_with_token(
hass, no_auth_websocket_client, hass_access_token
):
"""Test authenticating with a token."""
assert await async_setup_component(hass, "websocket_api", {})
await no_auth_websocket_client.send_json(
{"type": TYPE_AUTH, "access_token": hass_access_token}
)
auth_msg = await no_auth_websocket_client.receive_json()
assert auth_msg["type"] == TYPE_AUTH_OK
@@ -117,6 +126,7 @@ async def test_auth_active_user_inactive(hass, aiohttp_client, hass_access_token
refresh_token = await hass.auth.async_validate_access_token(hass_access_token)
refresh_token.user.is_active = False
assert await async_setup_component(hass, "websocket_api", {})
await hass.async_block_till_done()
client = await aiohttp_client(hass.http.app)
@@ -133,6 +143,7 @@ async def test_auth_active_user_inactive(hass, aiohttp_client, hass_access_token
async def test_auth_active_with_password_not_allow(hass, aiohttp_client):
"""Test authenticating with a token."""
assert await async_setup_component(hass, "websocket_api", {})
await hass.async_block_till_done()
client = await aiohttp_client(hass.http.app)
@@ -149,6 +160,7 @@ async def test_auth_active_with_password_not_allow(hass, aiohttp_client):
async def test_auth_legacy_support_with_password(hass, aiohttp_client, legacy_auth):
"""Test authenticating with a token."""
assert await async_setup_component(hass, "websocket_api", {})
await hass.async_block_till_done()
client = await aiohttp_client(hass.http.app)
@@ -165,6 +177,7 @@ async def test_auth_legacy_support_with_password(hass, aiohttp_client, legacy_au
async def test_auth_with_invalid_token(hass, aiohttp_client):
"""Test authenticating with a token."""
assert await async_setup_component(hass, "websocket_api", {})
await hass.async_block_till_done()
client = await aiohttp_client(hass.http.app)

View File

@@ -1,30 +1,37 @@
"""Test cases for the API stream sensor."""
from homeassistant.bootstrap import async_setup_component
from homeassistant.components.websocket_api.auth import TYPE_AUTH_REQUIRED
from homeassistant.components.websocket_api.http import URL
from .test_auth import test_auth_active_with_token
from tests.common import assert_setup_component
async def test_websocket_api(
hass, no_auth_websocket_client, hass_access_token, legacy_auth
):
async def test_websocket_api(hass, aiohttp_client, hass_access_token, legacy_auth):
"""Test API streams."""
with assert_setup_component(1):
await async_setup_component(
hass, "sensor", {"sensor": {"platform": "websocket_api"}}
)
await async_setup_component(
hass, "sensor", {"sensor": {"platform": "websocket_api"}}
)
await hass.async_block_till_done()
client = await aiohttp_client(hass.http.app)
ws = await client.ws_connect(URL)
auth_ok = await ws.receive_json()
assert auth_ok["type"] == TYPE_AUTH_REQUIRED
ws.client = client
state = hass.states.get("sensor.connected_clients")
assert state.state == "0"
await test_auth_active_with_token(hass, no_auth_websocket_client, hass_access_token)
await test_auth_active_with_token(hass, ws, hass_access_token)
state = hass.states.get("sensor.connected_clients")
assert state.state == "1"
await no_auth_websocket_client.close()
await ws.close()
await hass.async_block_till_done()
state = hass.states.get("sensor.connected_clients")