Break up websocket 2 (#17028)

* Break up websocket 2

* Lint+Test

* Lintttt

* Rename
This commit is contained in:
Paulus Schoutsen authored and Pascal Vizeli committed 2018-10-01 16:09:31 +02:00
1 parent b5e3d8c337
commit 2e6346ca43
27 files changed
+641 -568

No files matched your search

+36 -23
View File
@@ -4,7 +4,9 @@ from unittest.mock import patch
import pytest
from homeassistant.setup import async_setup_component
from homeassistant.components import websocket_api
from homeassistant.components.websocket_api.http import URL
from homeassistant.components.websocket_api.auth import (
TYPE_AUTH, TYPE_AUTH_OK, TYPE_AUTH_REQUIRED)
from tests.common import MockUser, CLIENT_ID
@@ -14,41 +16,52 @@ def hass_ws_client(aiohttp_client):
"""Websocket client fixture connected to websocket server."""
async def create_client(hass, access_token=None):
"""Create a websocket client."""
wapi = hass.components.websocket_api
assert await async_setup_component(hass, 'websocket_api')
client = await aiohttp_client(hass.http.app)
patching = None
patches = []
if access_token is not None:
patching = patch('homeassistant.auth.AuthManager.active',
return_value=True)
patching.start()
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()
try:
websocket = await client.ws_connect(wapi.URL)
websocket = await client.ws_connect(URL)
auth_resp = await websocket.receive_json()
assert auth_resp['type'] == TYPE_AUTH_REQUIRED
if auth_resp['type'] == wapi.TYPE_AUTH_OK:
assert access_token is None, \
'Access token given but no auth required'
return websocket
assert access_token is not None, \
'Access token required for fixture'
await websocket.send_json({
'type': websocket_api.TYPE_AUTH,
'access_token': access_token
})
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
})
auth_ok = await websocket.receive_json()
assert auth_ok['type'] == wapi.TYPE_AUTH_OK
assert auth_ok['type'] == TYPE_AUTH_OK
finally:
if patching is not None:
patching.stop()
for p in patches:
p.stop()
# wrap in client
websocket.client = client
+4 -4
View File
@@ -62,7 +62,7 @@ class TestPanelIframe(unittest.TestCase):
panels = self.hass.data[frontend.DATA_PANELS]
assert panels.get('router').to_response(self.hass, None) == {
assert panels.get('router').to_response() == {
'component_name': 'iframe',
'config': {'url': 'http://192.168.1.1'},
'icon': 'mdi:network-wireless',
@@ -70,7 +70,7 @@ class TestPanelIframe(unittest.TestCase):
'url_path': 'router'
}
assert panels.get('weather').to_response(self.hass, None) == {
assert panels.get('weather').to_response() == {
'component_name': 'iframe',
'config': {'url': 'https://www.wunderground.com/us/ca/san-diego'},
'icon': 'mdi:weather',
@@ -78,7 +78,7 @@ class TestPanelIframe(unittest.TestCase):
'url_path': 'weather',
}
assert panels.get('api').to_response(self.hass, None) == {
assert panels.get('api').to_response() == {
'component_name': 'iframe',
'config': {'url': '/api'},
'icon': 'mdi:weather',
@@ -86,7 +86,7 @@ class TestPanelIframe(unittest.TestCase):
'url_path': 'api',
}
assert panels.get('ftp').to_response(self.hass, None) == {
assert panels.get('ftp').to_response() == {
'component_name': 'iframe',
'config': {'url': 'ftp://some/ftp'},
'icon': 'mdi:weather',
+4 -3
View File
@@ -2,7 +2,8 @@
import pytest
from homeassistant.setup import async_setup_component
from homeassistant.components import websocket_api as wapi
from homeassistant.components.websocket_api.http import URL
from homeassistant.components.websocket_api.auth import TYPE_AUTH_REQUIRED
from . import API_PASSWORD
@@ -24,10 +25,10 @@ def no_auth_websocket_client(hass, loop, aiohttp_client):
}))
client = loop.run_until_complete(aiohttp_client(hass.http.app))
ws = loop.run_until_complete(client.ws_connect(wapi.URL))
ws = loop.run_until_complete(client.ws_connect(URL))
auth_ok = loop.run_until_complete(ws.receive_json())
assert auth_ok['type'] == wapi.TYPE_AUTH_REQUIRED
assert auth_ok['type'] == TYPE_AUTH_REQUIRED
yield ws
+33 -29
View File
@@ -1,7 +1,10 @@
"""Test auth of websocket API."""
from unittest.mock import patch
from homeassistant.components import websocket_api as wapi
from homeassistant.components.websocket_api.const import URL
from homeassistant.components.websocket_api.auth import (
TYPE_AUTH, TYPE_AUTH_INVALID, TYPE_AUTH_OK, TYPE_AUTH_REQUIRED)
from homeassistant.components.websocket_api import commands
from homeassistant.setup import async_setup_component
@@ -13,28 +16,29 @@ from . import API_PASSWORD
async def test_auth_via_msg(no_auth_websocket_client):
"""Test authenticating."""
await no_auth_websocket_client.send_json({
'type': wapi.TYPE_AUTH,
'type': TYPE_AUTH,
'api_password': API_PASSWORD
})
msg = await no_auth_websocket_client.receive_json()
assert msg['type'] == wapi.TYPE_AUTH_OK
assert msg['type'] == TYPE_AUTH_OK
async def test_auth_via_msg_incorrect_pass(no_auth_websocket_client):
"""Test authenticating."""
with patch('homeassistant.components.websocket_api.process_wrong_login',
return_value=mock_coro()) as mock_process_wrong_login:
with patch('homeassistant.components.websocket_api.auth.'
'process_wrong_login', return_value=mock_coro()) \
as mock_process_wrong_login:
await no_auth_websocket_client.send_json({
'type': wapi.TYPE_AUTH,
'type': TYPE_AUTH,
'api_password': API_PASSWORD + 'wrong'
})
msg = await no_auth_websocket_client.receive_json()
assert mock_process_wrong_login.called
assert msg['type'] == wapi.TYPE_AUTH_INVALID
assert msg['type'] == TYPE_AUTH_INVALID
assert msg['message'] == 'Invalid access token or password'
@@ -51,8 +55,8 @@ async def test_pre_auth_only_auth_allowed(no_auth_websocket_client):
msg = await no_auth_websocket_client.receive_json()
assert msg['type'] == wapi.TYPE_AUTH_INVALID
assert msg['message'].startswith('Message incorrectly formatted')
assert msg['type'] == TYPE_AUTH_INVALID
assert msg['message'].startswith('Auth message incorrectly formatted')
async def test_auth_active_with_token(hass, aiohttp_client, hass_access_token):
@@ -65,19 +69,19 @@ async def test_auth_active_with_token(hass, aiohttp_client, hass_access_token):
client = await aiohttp_client(hass.http.app)
async with client.ws_connect(wapi.URL) as ws:
async with client.ws_connect(URL) as ws:
with patch('homeassistant.auth.AuthManager.active') as auth_active:
auth_active.return_value = True
auth_msg = await ws.receive_json()
assert auth_msg['type'] == wapi.TYPE_AUTH_REQUIRED
assert auth_msg['type'] == TYPE_AUTH_REQUIRED
await ws.send_json({
'type': wapi.TYPE_AUTH,
'type': TYPE_AUTH,
'access_token': hass_access_token
})
auth_msg = await ws.receive_json()
assert auth_msg['type'] == wapi.TYPE_AUTH_OK
assert auth_msg['type'] == TYPE_AUTH_OK
async def test_auth_active_user_inactive(hass, aiohttp_client,
@@ -94,19 +98,19 @@ async def test_auth_active_user_inactive(hass, aiohttp_client,
client = await aiohttp_client(hass.http.app)
async with client.ws_connect(wapi.URL) as ws:
async with client.ws_connect(URL) as ws:
with patch('homeassistant.auth.AuthManager.active') as auth_active:
auth_active.return_value = True
auth_msg = await ws.receive_json()
assert auth_msg['type'] == wapi.TYPE_AUTH_REQUIRED
assert auth_msg['type'] == TYPE_AUTH_REQUIRED
await ws.send_json({
'type': wapi.TYPE_AUTH,
'type': TYPE_AUTH,
'access_token': hass_access_token
})
auth_msg = await ws.receive_json()
assert auth_msg['type'] == wapi.TYPE_AUTH_INVALID
assert auth_msg['type'] == TYPE_AUTH_INVALID
async def test_auth_active_with_password_not_allow(hass, aiohttp_client):
@@ -119,19 +123,19 @@ async def test_auth_active_with_password_not_allow(hass, aiohttp_client):
client = await aiohttp_client(hass.http.app)
async with client.ws_connect(wapi.URL) as ws:
async with client.ws_connect(URL) as ws:
with patch('homeassistant.auth.AuthManager.active',
return_value=True):
auth_msg = await ws.receive_json()
assert auth_msg['type'] == wapi.TYPE_AUTH_REQUIRED
assert auth_msg['type'] == TYPE_AUTH_REQUIRED
await ws.send_json({
'type': wapi.TYPE_AUTH,
'type': TYPE_AUTH,
'api_password': API_PASSWORD
})
auth_msg = await ws.receive_json()
assert auth_msg['type'] == wapi.TYPE_AUTH_INVALID
assert auth_msg['type'] == TYPE_AUTH_INVALID
async def test_auth_legacy_support_with_password(hass, aiohttp_client):
@@ -144,21 +148,21 @@ async def test_auth_legacy_support_with_password(hass, aiohttp_client):
client = await aiohttp_client(hass.http.app)
async with client.ws_connect(wapi.URL) as ws:
async with client.ws_connect(URL) as ws:
with patch('homeassistant.auth.AuthManager.active',
return_value=True),\
patch('homeassistant.auth.AuthManager.support_legacy',
return_value=True):
auth_msg = await ws.receive_json()
assert auth_msg['type'] == wapi.TYPE_AUTH_REQUIRED
assert auth_msg['type'] == TYPE_AUTH_REQUIRED
await ws.send_json({
'type': wapi.TYPE_AUTH,
'type': TYPE_AUTH,
'api_password': API_PASSWORD
})
auth_msg = await ws.receive_json()
assert auth_msg['type'] == wapi.TYPE_AUTH_OK
assert auth_msg['type'] == TYPE_AUTH_OK
async def test_auth_with_invalid_token(hass, aiohttp_client):
@@ -171,16 +175,16 @@ async def test_auth_with_invalid_token(hass, aiohttp_client):
client = await aiohttp_client(hass.http.app)
async with client.ws_connect(wapi.URL) as ws:
async with client.ws_connect(URL) as ws:
with patch('homeassistant.auth.AuthManager.active') as auth_active:
auth_active.return_value = True
auth_msg = await ws.receive_json()
assert auth_msg['type'] == wapi.TYPE_AUTH_REQUIRED
assert auth_msg['type'] == TYPE_AUTH_REQUIRED
await ws.send_json({
'type': wapi.TYPE_AUTH,
'type': TYPE_AUTH,
'access_token': 'incorrect'
})
auth_msg = await ws.receive_json()
assert auth_msg['type'] == wapi.TYPE_AUTH_INVALID
assert auth_msg['type'] == TYPE_AUTH_INVALID
@@ -4,7 +4,10 @@ from unittest.mock import patch
from async_timeout import timeout
from homeassistant.core import callback
from homeassistant.components import websocket_api as wapi
from homeassistant.components.websocket_api.const import URL
from homeassistant.components.websocket_api.auth import (
TYPE_AUTH, TYPE_AUTH_OK, TYPE_AUTH_REQUIRED
)
from homeassistant.components.websocket_api import const, commands
from homeassistant.setup import async_setup_component
@@ -178,19 +181,19 @@ async def test_call_service_context_with_user(hass, aiohttp_client,
calls = async_mock_service(hass, 'domain_test', 'test_service')
client = await aiohttp_client(hass.http.app)
async with client.ws_connect(wapi.URL) as ws:
async with client.ws_connect(URL) as ws:
with patch('homeassistant.auth.AuthManager.active') as auth_active:
auth_active.return_value = True
auth_msg = await ws.receive_json()
assert auth_msg['type'] == wapi.TYPE_AUTH_REQUIRED
assert auth_msg['type'] == TYPE_AUTH_REQUIRED
await ws.send_json({
'type': wapi.TYPE_AUTH,
'type': TYPE_AUTH,
'access_token': hass_access_token
})
auth_msg = await ws.receive_json()
assert auth_msg['type'] == wapi.TYPE_AUTH_OK
assert auth_msg['type'] == TYPE_AUTH_OK
await ws.send_json({
'id': 5,
@@ -227,17 +230,17 @@ async def test_call_service_context_no_user(hass, aiohttp_client):
calls = async_mock_service(hass, 'domain_test', 'test_service')
client = await aiohttp_client(hass.http.app)
async with client.ws_connect(wapi.URL) as ws:
async with client.ws_connect(URL) as ws:
auth_msg = await ws.receive_json()
assert auth_msg['type'] == wapi.TYPE_AUTH_REQUIRED
assert auth_msg['type'] == TYPE_AUTH_REQUIRED
await ws.send_json({
'type': wapi.TYPE_AUTH,
'type': TYPE_AUTH,
'api_password': API_PASSWORD
})
auth_msg = await ws.receive_json()
assert auth_msg['type'] == wapi.TYPE_AUTH_OK
assert auth_msg['type'] == TYPE_AUTH_OK
await ws.send_json({
'id': 5,
+2 -2
View File
@@ -5,14 +5,14 @@ from unittest.mock import patch, Mock
from aiohttp import WSMsgType
import pytest
from homeassistant.components import websocket_api as wapi
from homeassistant.components.websocket_api import const, commands, messages
@pytest.fixture
def mock_low_queue():
"""Mock a low queue."""
with patch.object(wapi, 'MAX_PENDING_MSG', 5):
with patch('homeassistant.components.websocket_api.http.MAX_PENDING_MSG',
5):
yield