Add websocket commands for refresh tokens (#16559)

* Add websocket commands for refresh tokens

* Comment
This commit is contained in:
Paulus Schoutsen 2018-09-11 18:08:03 +02:00 committed by Jason Hu
parent 4e3faf6108
commit 0db13a99aa
3 changed files with 140 additions and 44 deletions

View file

@ -1,4 +1,6 @@
"""Fixtures for component testing."""
from unittest.mock import patch
import pytest
from homeassistant.setup import async_setup_component
@ -16,23 +18,37 @@ def hass_ws_client(aiohttp_client):
assert await async_setup_component(hass, 'websocket_api')
client = await aiohttp_client(hass.http.app)
websocket = await client.ws_connect(wapi.URL)
auth_resp = await websocket.receive_json()
if auth_resp['type'] == wapi.TYPE_AUTH_OK:
assert access_token is None, \
'Access token given but no auth required'
return websocket
patching = None
assert access_token is not None, 'Access token required for fixture'
if access_token is not None:
patching = patch('homeassistant.auth.AuthManager.active',
return_value=True)
patching.start()
await websocket.send_json({
'type': websocket_api.TYPE_AUTH,
'access_token': access_token
})
try:
websocket = await client.ws_connect(wapi.URL)
auth_resp = await websocket.receive_json()
auth_ok = await websocket.receive_json()
assert auth_ok['type'] == wapi.TYPE_AUTH_OK
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
})
auth_ok = await websocket.receive_json()
assert auth_ok['type'] == wapi.TYPE_AUTH_OK
finally:
if patching is not None:
patching.stop()
# wrap in client
websocket.client = client