Context (#15674)
* Add context * Add context to switch/light services * Test set_state API * Lint * Fix tests * Do not include context yet in comparison * Do not pass in loop * Fix Z-Wave tests * Add websocket test without user
This commit is contained in:
1 parent
867f80715e
commit
c7f4bdafc0
16 files changed
+363
-109
No files matched your search
@@ -5,12 +5,12 @@ import unittest.mock as mock
|
||||
import os
|
||||
from io import StringIO
|
||||
|
||||
from homeassistant.setup import setup_component
|
||||
import homeassistant.loader as loader
|
||||
from homeassistant import core, loader
|
||||
from homeassistant.setup import setup_component, async_setup_component
|
||||
from homeassistant.const import (
|
||||
ATTR_ENTITY_ID, STATE_ON, STATE_OFF, CONF_PLATFORM,
|
||||
SERVICE_TURN_ON, SERVICE_TURN_OFF, SERVICE_TOGGLE, ATTR_SUPPORTED_FEATURES)
|
||||
import homeassistant.components.light as light
|
||||
from homeassistant.components import light
|
||||
from homeassistant.helpers.intent import IntentHandleError
|
||||
|
||||
from tests.common import (
|
||||
@@ -475,3 +475,24 @@ async def test_intent_set_color_and_brightness(hass):
|
||||
assert call.data.get(ATTR_ENTITY_ID) == 'light.hello_2'
|
||||
assert call.data.get(light.ATTR_RGB_COLOR) == (0, 0, 255)
|
||||
assert call.data.get(light.ATTR_BRIGHTNESS_PCT) == 20
|
||||
|
||||
|
||||
async def test_light_context(hass):
|
||||
"""Test that light context works."""
|
||||
assert await async_setup_component(hass, 'light', {
|
||||
'light': {
|
||||
'platform': 'test'
|
||||
}
|
||||
})
|
||||
|
||||
state = hass.states.get('light.ceiling')
|
||||
assert state is not None
|
||||
|
||||
await hass.services.async_call('light', 'toggle', {
|
||||
'entity_id': state.entity_id,
|
||||
}, True, core.Context(user_id='abcd'))
|
||||
|
||||
state2 = hass.states.get('light.ceiling')
|
||||
assert state2 is not None
|
||||
assert state.state != state2.state
|
||||
assert state2.context.user_id == 'abcd'
|
||||
@@ -2,8 +2,8 @@
|
||||
# pylint: disable=protected-access
|
||||
import unittest
|
||||
|
||||
from homeassistant.setup import setup_component
|
||||
from homeassistant import loader
|
||||
from homeassistant.setup import setup_component, async_setup_component
|
||||
from homeassistant import core, loader
|
||||
from homeassistant.components import switch
|
||||
from homeassistant.const import STATE_ON, STATE_OFF, CONF_PLATFORM
|
||||
|
||||
@@ -91,3 +91,24 @@ class TestSwitch(unittest.TestCase):
|
||||
'{} 2'.format(switch.DOMAIN): {CONF_PLATFORM: 'test2'},
|
||||
}
|
||||
))
|
||||
|
||||
|
||||
async def test_switch_context(hass):
|
||||
"""Test that switch context works."""
|
||||
assert await async_setup_component(hass, 'switch', {
|
||||
'switch': {
|
||||
'platform': 'test'
|
||||
}
|
||||
})
|
||||
|
||||
state = hass.states.get('switch.ac')
|
||||
assert state is not None
|
||||
|
||||
await hass.services.async_call('switch', 'toggle', {
|
||||
'entity_id': state.entity_id,
|
||||
}, True, core.Context(user_id='abcd'))
|
||||
|
||||
state2 = hass.states.get('switch.ac')
|
||||
assert state2 is not None
|
||||
assert state.state != state2.state
|
||||
assert state2.context.user_id == 'abcd'
|
||||
@@ -12,6 +12,8 @@ from homeassistant.bootstrap import DATA_LOGGING
|
||||
import homeassistant.core as ha
|
||||
from homeassistant.setup import async_setup_component
|
||||
|
||||
from tests.common import async_mock_service
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_api_client(hass, aiohttp_client):
|
||||
@@ -429,3 +431,58 @@ async def test_api_error_log(hass, aiohttp_client):
|
||||
assert mock_file.mock_calls[0][1][0] == hass.data[DATA_LOGGING]
|
||||
assert resp.status == 200
|
||||
assert await resp.text() == 'Hello'
|
||||
|
||||
|
||||
async def test_api_fire_event_context(hass, mock_api_client,
|
||||
hass_access_token):
|
||||
"""Test if the API sets right context if we fire an event."""
|
||||
test_value = []
|
||||
|
||||
@ha.callback
|
||||
def listener(event):
|
||||
"""Helper method that will verify our event got called."""
|
||||
test_value.append(event)
|
||||
|
||||
hass.bus.async_listen("test.event", listener)
|
||||
|
||||
await mock_api_client.post(
|
||||
const.URL_API_EVENTS_EVENT.format("test.event"),
|
||||
headers={
|
||||
'authorization': 'Bearer {}'.format(hass_access_token.token)
|
||||
})
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert len(test_value) == 1
|
||||
assert test_value[0].context.user_id == \
|
||||
hass_access_token.refresh_token.user.id
|
||||
|
||||
|
||||
async def test_api_call_service_context(hass, mock_api_client,
|
||||
hass_access_token):
|
||||
"""Test if the API sets right context if we call a service."""
|
||||
calls = async_mock_service(hass, 'test_domain', 'test_service')
|
||||
|
||||
await mock_api_client.post(
|
||||
'/api/services/test_domain/test_service',
|
||||
headers={
|
||||
'authorization': 'Bearer {}'.format(hass_access_token.token)
|
||||
})
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert len(calls) == 1
|
||||
assert calls[0].context.user_id == hass_access_token.refresh_token.user.id
|
||||
|
||||
|
||||
async def test_api_set_state_context(hass, mock_api_client, hass_access_token):
|
||||
"""Test if the API sets right context if we set state."""
|
||||
await mock_api_client.post(
|
||||
'/api/states/light.kitchen',
|
||||
json={
|
||||
'state': 'on'
|
||||
},
|
||||
headers={
|
||||
'authorization': 'Bearer {}'.format(hass_access_token.token)
|
||||
})
|
||||
|
||||
state = hass.states.get('light.kitchen')
|
||||
assert state.context.user_id == hass_access_token.refresh_token.user.id
|
||||
@@ -104,12 +104,14 @@ class TestMqttEventStream:
|
||||
"state": "on",
|
||||
"entity_id": e_id,
|
||||
"attributes": {},
|
||||
"last_changed": now.isoformat()
|
||||
"last_changed": now.isoformat(),
|
||||
}
|
||||
event['event_data'] = {"new_state": new_state, "entity_id": e_id}
|
||||
|
||||
# Verify that the message received was that expected
|
||||
assert json.loads(msg) == event
|
||||
result = json.loads(msg)
|
||||
result['event_data']['new_state'].pop('context')
|
||||
assert result == event
|
||||
|
||||
@patch('homeassistant.components.mqtt.async_publish')
|
||||
def test_time_event_does_not_send_message(self, mock_pub):
|
||||
|
||||
@@ -10,7 +10,7 @@ from homeassistant.core import callback
|
||||
from homeassistant.components import websocket_api as wapi
|
||||
from homeassistant.setup import async_setup_component
|
||||
|
||||
from tests.common import mock_coro
|
||||
from tests.common import mock_coro, async_mock_service
|
||||
|
||||
API_PASSWORD = 'test1234'
|
||||
|
||||
@@ -443,3 +443,94 @@ async def test_auth_with_invalid_token(hass, aiohttp_client):
|
||||
|
||||
auth_msg = await ws.receive_json()
|
||||
assert auth_msg['type'] == wapi.TYPE_AUTH_INVALID
|
||||
|
||||
|
||||
async def test_call_service_context_with_user(hass, aiohttp_client,
|
||||
hass_access_token):
|
||||
"""Test that the user is set in the service call 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(wapi.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
|
||||
|
||||
await ws.send_json({
|
||||
'type': wapi.TYPE_AUTH,
|
||||
'access_token': hass_access_token.token
|
||||
})
|
||||
|
||||
auth_msg = await ws.receive_json()
|
||||
assert auth_msg['type'] == wapi.TYPE_AUTH_OK
|
||||
|
||||
await ws.send_json({
|
||||
'id': 5,
|
||||
'type': wapi.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 == hass_access_token.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(wapi.URL) as ws:
|
||||
auth_msg = await ws.receive_json()
|
||||
assert auth_msg['type'] == wapi.TYPE_AUTH_REQUIRED
|
||||
|
||||
await ws.send_json({
|
||||
'type': wapi.TYPE_AUTH,
|
||||
'api_password': API_PASSWORD
|
||||
})
|
||||
|
||||
auth_msg = await ws.receive_json()
|
||||
assert auth_msg['type'] == wapi.TYPE_AUTH_OK
|
||||
|
||||
await ws.send_json({
|
||||
'id': 5,
|
||||
'type': wapi.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
|
||||
@@ -163,10 +163,10 @@ def test_zwave_ready_wait(hass, mock_openzwave):
|
||||
asyncio_sleep = asyncio.sleep
|
||||
|
||||
@asyncio.coroutine
|
||||
def sleep(duration, loop):
|
||||
def sleep(duration, loop=None):
|
||||
if duration > 0:
|
||||
sleeps.append(duration)
|
||||
yield from asyncio_sleep(0, loop=loop)
|
||||
yield from asyncio_sleep(0)
|
||||
|
||||
with patch('homeassistant.components.zwave.dt_util.utcnow', new=utcnow):
|
||||
with patch('asyncio.sleep', new=sleep):
|
||||
@@ -248,10 +248,10 @@ async def test_unparsed_node_discovery(hass, mock_openzwave):
|
||||
|
||||
asyncio_sleep = asyncio.sleep
|
||||
|
||||
async def sleep(duration, loop):
|
||||
async def sleep(duration, loop=None):
|
||||
if duration > 0:
|
||||
sleeps.append(duration)
|
||||
await asyncio_sleep(0, loop=loop)
|
||||
await asyncio_sleep(0)
|
||||
|
||||
with patch('homeassistant.components.zwave.dt_util.utcnow', new=utcnow):
|
||||
with patch('asyncio.sleep', new=sleep):
|
||||
|
||||
Reference in new issue
Block a user