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 (
|
|
|
|
TYPE_AUTH, TYPE_AUTH_OK, TYPE_AUTH_REQUIRED)
|
2018-05-01 17:35:23 +00:00
|
|
|
|
2018-07-09 16:24:46 +00:00
|
|
|
from tests.common import MockUser, CLIENT_ID
|
2018-05-10 08:38:11 +00:00
|
|
|
|
2018-05-01 17:35:23 +00:00
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def hass_ws_client(aiohttp_client):
|
|
|
|
"""Websocket client fixture connected to websocket server."""
|
2018-07-13 13:31:20 +00:00
|
|
|
async def create_client(hass, access_token=None):
|
2018-05-01 17:35:23 +00:00
|
|
|
"""Create a websocket client."""
|
|
|
|
assert await async_setup_component(hass, 'websocket_api')
|
|
|
|
|
|
|
|
client = await aiohttp_client(hass.http.app)
|
2018-07-13 13:31:20 +00:00
|
|
|
|
2018-10-01 14:09:31 +00:00
|
|
|
patches = []
|
|
|
|
|
|
|
|
if access_token is None:
|
|
|
|
patches.append(patch(
|
|
|
|
'homeassistant.auth.AuthManager.active', return_value=False))
|
|
|
|
patches.append(patch(
|
|
|
|
'homeassistant.auth.AuthManager.support_legacy',
|
|
|
|
return_value=True))
|
|
|
|
patches.append(patch(
|
|
|
|
'homeassistant.components.websocket_api.auth.'
|
|
|
|
'validate_password', return_value=True))
|
|
|
|
else:
|
|
|
|
patches.append(patch(
|
|
|
|
'homeassistant.auth.AuthManager.active', return_value=True))
|
|
|
|
patches.append(patch(
|
|
|
|
'homeassistant.components.http.auth.setup_auth'))
|
|
|
|
|
|
|
|
for p in patches:
|
|
|
|
p.start()
|
2018-09-11 16:08:03 +00:00
|
|
|
|
|
|
|
try:
|
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()
|
2018-10-01 14:09:31 +00:00
|
|
|
assert auth_resp['type'] == TYPE_AUTH_REQUIRED
|
|
|
|
|
|
|
|
if access_token is None:
|
|
|
|
await websocket.send_json({
|
|
|
|
'type': TYPE_AUTH,
|
|
|
|
'api_password': 'bla'
|
|
|
|
})
|
|
|
|
else:
|
|
|
|
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()
|
2018-10-01 14:09:31 +00:00
|
|
|
assert auth_ok['type'] == TYPE_AUTH_OK
|
2018-07-13 13:31:20 +00:00
|
|
|
|
2018-09-11 16:08:03 +00:00
|
|
|
finally:
|
2018-10-01 14:09:31 +00:00
|
|
|
for p in patches:
|
|
|
|
p.stop()
|
2018-05-01 17:35:23 +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
|
2018-05-10 08:38:11 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def hass_access_token(hass):
|
|
|
|
"""Return an access token to access Home Assistant."""
|
|
|
|
user = MockUser().add_to_hass(hass)
|
|
|
|
refresh_token = hass.loop.run_until_complete(
|
2018-07-09 16:24:46 +00:00
|
|
|
hass.auth.async_create_refresh_token(user, CLIENT_ID))
|
2018-05-10 08:38:11 +00:00
|
|
|
yield hass.auth.async_create_access_token(refresh_token)
|