1 parent
ae9e3d83d7
commit
d1a621601d
42 files changed
+307
-473
No files matched your search
@@ -13,7 +13,7 @@ from tests.common import mock_coro
|
||||
from . import API_PASSWORD
|
||||
|
||||
|
||||
async def test_auth_via_msg(no_auth_websocket_client):
|
||||
async def test_auth_via_msg(no_auth_websocket_client, legacy_auth):
|
||||
"""Test authenticating."""
|
||||
await no_auth_websocket_client.send_json({
|
||||
'type': TYPE_AUTH,
|
||||
@@ -70,18 +70,16 @@ 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(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'] == TYPE_AUTH_REQUIRED
|
||||
auth_msg = await ws.receive_json()
|
||||
assert auth_msg['type'] == TYPE_AUTH_REQUIRED
|
||||
|
||||
await ws.send_json({
|
||||
'type': TYPE_AUTH,
|
||||
'access_token': hass_access_token
|
||||
})
|
||||
await ws.send_json({
|
||||
'type': TYPE_AUTH,
|
||||
'access_token': hass_access_token
|
||||
})
|
||||
|
||||
auth_msg = await ws.receive_json()
|
||||
assert auth_msg['type'] == TYPE_AUTH_OK
|
||||
auth_msg = await ws.receive_json()
|
||||
assert auth_msg['type'] == TYPE_AUTH_OK
|
||||
|
||||
|
||||
async def test_auth_active_user_inactive(hass, aiohttp_client,
|
||||
@@ -99,18 +97,16 @@ async def test_auth_active_user_inactive(hass, aiohttp_client,
|
||||
client = await aiohttp_client(hass.http.app)
|
||||
|
||||
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'] == TYPE_AUTH_REQUIRED
|
||||
auth_msg = await ws.receive_json()
|
||||
assert auth_msg['type'] == TYPE_AUTH_REQUIRED
|
||||
|
||||
await ws.send_json({
|
||||
'type': TYPE_AUTH,
|
||||
'access_token': hass_access_token
|
||||
})
|
||||
await ws.send_json({
|
||||
'type': TYPE_AUTH,
|
||||
'access_token': hass_access_token
|
||||
})
|
||||
|
||||
auth_msg = await ws.receive_json()
|
||||
assert auth_msg['type'] == TYPE_AUTH_INVALID
|
||||
auth_msg = await ws.receive_json()
|
||||
assert auth_msg['type'] == TYPE_AUTH_INVALID
|
||||
|
||||
|
||||
async def test_auth_active_with_password_not_allow(hass, aiohttp_client):
|
||||
@@ -124,18 +120,16 @@ async def test_auth_active_with_password_not_allow(hass, aiohttp_client):
|
||||
client = await aiohttp_client(hass.http.app)
|
||||
|
||||
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'] == TYPE_AUTH_REQUIRED
|
||||
auth_msg = await ws.receive_json()
|
||||
assert auth_msg['type'] == TYPE_AUTH_REQUIRED
|
||||
|
||||
await ws.send_json({
|
||||
'type': TYPE_AUTH,
|
||||
'api_password': API_PASSWORD
|
||||
})
|
||||
await ws.send_json({
|
||||
'type': TYPE_AUTH,
|
||||
'api_password': API_PASSWORD
|
||||
})
|
||||
|
||||
auth_msg = await ws.receive_json()
|
||||
assert auth_msg['type'] == TYPE_AUTH_INVALID
|
||||
auth_msg = await ws.receive_json()
|
||||
assert auth_msg['type'] == TYPE_AUTH_INVALID
|
||||
|
||||
|
||||
async def test_auth_legacy_support_with_password(hass, aiohttp_client):
|
||||
@@ -149,9 +143,7 @@ async def test_auth_legacy_support_with_password(hass, aiohttp_client):
|
||||
client = await aiohttp_client(hass.http.app)
|
||||
|
||||
async with client.ws_connect(URL) as ws:
|
||||
with patch('homeassistant.auth.AuthManager.active',
|
||||
return_value=True),\
|
||||
patch('homeassistant.auth.AuthManager.support_legacy',
|
||||
with patch('homeassistant.auth.AuthManager.support_legacy',
|
||||
return_value=True):
|
||||
auth_msg = await ws.receive_json()
|
||||
assert auth_msg['type'] == TYPE_AUTH_REQUIRED
|
||||
@@ -176,15 +168,13 @@ async def test_auth_with_invalid_token(hass, aiohttp_client):
|
||||
client = await aiohttp_client(hass.http.app)
|
||||
|
||||
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'] == TYPE_AUTH_REQUIRED
|
||||
auth_msg = await ws.receive_json()
|
||||
assert auth_msg['type'] == TYPE_AUTH_REQUIRED
|
||||
|
||||
await ws.send_json({
|
||||
'type': TYPE_AUTH,
|
||||
'access_token': 'incorrect'
|
||||
})
|
||||
await ws.send_json({
|
||||
'type': TYPE_AUTH,
|
||||
'access_token': 'incorrect'
|
||||
})
|
||||
|
||||
auth_msg = await ws.receive_json()
|
||||
assert auth_msg['type'] == TYPE_AUTH_INVALID
|
||||
auth_msg = await ws.receive_json()
|
||||
assert auth_msg['type'] == TYPE_AUTH_INVALID
|
||||
@@ -1,6 +1,4 @@
|
||||
"""Tests for WebSocket API commands."""
|
||||
from unittest.mock import patch
|
||||
|
||||
from async_timeout import timeout
|
||||
|
||||
from homeassistant.core import callback
|
||||
@@ -201,18 +199,16 @@ async def test_call_service_context_with_user(hass, aiohttp_client,
|
||||
client = await aiohttp_client(hass.http.app)
|
||||
|
||||
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'] == TYPE_AUTH_REQUIRED
|
||||
auth_msg = await ws.receive_json()
|
||||
assert auth_msg['type'] == TYPE_AUTH_REQUIRED
|
||||
|
||||
await ws.send_json({
|
||||
'type': TYPE_AUTH,
|
||||
'access_token': hass_access_token
|
||||
})
|
||||
await ws.send_json({
|
||||
'type': TYPE_AUTH,
|
||||
'access_token': hass_access_token
|
||||
})
|
||||
|
||||
auth_msg = await ws.receive_json()
|
||||
assert auth_msg['type'] == TYPE_AUTH_OK
|
||||
auth_msg = await ws.receive_json()
|
||||
assert auth_msg['type'] == TYPE_AUTH_OK
|
||||
|
||||
await ws.send_json({
|
||||
'id': 5,
|
||||
@@ -238,50 +234,6 @@ async def test_call_service_context_with_user(hass, aiohttp_client,
|
||||
assert call.context.user_id == refresh_token.user.id
|
||||
|
||||
|
||||
async def test_call_service_context_no_user(hass, aiohttp_client):
|
||||
"""Test that connection without user sets context."""
|
||||
assert await async_setup_component(hass, 'websocket_api', {
|
||||
'http': {
|
||||
'api_password': API_PASSWORD
|
||||
}
|
||||
})
|
||||
|
||||
calls = async_mock_service(hass, 'domain_test', 'test_service')
|
||||
client = await aiohttp_client(hass.http.app)
|
||||
|
||||
async with client.ws_connect(URL) as ws:
|
||||
auth_msg = await ws.receive_json()
|
||||
assert auth_msg['type'] == TYPE_AUTH_REQUIRED
|
||||
|
||||
await ws.send_json({
|
||||
'type': TYPE_AUTH,
|
||||
'api_password': API_PASSWORD
|
||||
})
|
||||
|
||||
auth_msg = await ws.receive_json()
|
||||
assert auth_msg['type'] == TYPE_AUTH_OK
|
||||
|
||||
await ws.send_json({
|
||||
'id': 5,
|
||||
'type': commands.TYPE_CALL_SERVICE,
|
||||
'domain': 'domain_test',
|
||||
'service': 'test_service',
|
||||
'service_data': {
|
||||
'hello': 'world'
|
||||
}
|
||||
})
|
||||
|
||||
msg = await ws.receive_json()
|
||||
assert msg['success']
|
||||
|
||||
assert len(calls) == 1
|
||||
call = calls[0]
|
||||
assert call.domain == 'domain_test'
|
||||
assert call.service == 'test_service'
|
||||
assert call.data == {'hello': 'world'}
|
||||
assert call.context.user_id is None
|
||||
|
||||
|
||||
async def test_subscribe_requires_admin(websocket_client, hass_admin_user):
|
||||
"""Test subscribing events without being admin."""
|
||||
hass_admin_user.groups = []
|
||||
|
||||
Reference in new issue
Block a user