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:
parent
867f80715e
commit
c7f4bdafc0
16 changed files with 363 additions and 109 deletions
|
@ -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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue