Fixing the api_streams sensor (#22200)

* Fire events with websocket messages.

* Added tests to validate

* Fixed api_streams sensor to use new sensor

* Delete from coverageac as now works.

* Removed websocket request event.

* Use dispatcher instead of events.

* Moved sensor to under websocket_api

* Changes as per code review

* Fixed tests.

* Modified test

* Patch
This commit is contained in:
Penny Wood 2019-03-23 02:59:10 +08:00 committed by Paulus Schoutsen
parent 2b6e197deb
commit 1ddc65a0ce
8 changed files with 142 additions and 168 deletions

View file

@ -1,7 +1,8 @@
"""Test auth of websocket API."""
from unittest.mock import patch
from homeassistant.components.websocket_api.const import URL
from homeassistant.components.websocket_api.const import (
URL, SIGNAL_WEBSOCKET_CONNECTED, SIGNAL_WEBSOCKET_DISCONNECTED)
from homeassistant.components.websocket_api.auth import (
TYPE_AUTH, TYPE_AUTH_INVALID, TYPE_AUTH_OK, TYPE_AUTH_REQUIRED)
@ -24,6 +25,28 @@ async def test_auth_via_msg(no_auth_websocket_client, legacy_auth):
assert msg['type'] == TYPE_AUTH_OK
async def test_auth_events(hass, no_auth_websocket_client, legacy_auth):
"""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(no_auth_websocket_client, legacy_auth)
assert len(connected_evt) == 1
assert not disconnected_evt
await no_auth_websocket_client.close()
await hass.async_block_till_done()
assert len(disconnected_evt) == 1
async def test_auth_via_msg_incorrect_pass(no_auth_websocket_client):
"""Test authenticating."""
with patch('homeassistant.components.websocket_api.auth.'
@ -41,6 +64,29 @@ 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):
"""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
await no_auth_websocket_client.close()
await hass.async_block_till_done()
assert not connected_evt
assert not disconnected_evt
async def test_pre_auth_only_auth_allowed(no_auth_websocket_client):
"""Verify that before authentication, only auth messages are allowed."""
await no_auth_websocket_client.send_json({