This commit is contained in:
Paulus Schoutsen committed 2019-07-31 12:25:30 -07:00
1 parent da05dfe708
commit 4de97abc3a
2676 files changed
+163166 -140084

No files matched your search

+114 -122
View File
@@ -19,14 +19,14 @@ from tests.common import async_mock_service
@pytest.fixture
def mock_api_client(hass, hass_client):
"""Start the Hass HTTP component and return admin API client."""
hass.loop.run_until_complete(async_setup_component(hass, 'api', {}))
hass.loop.run_until_complete(async_setup_component(hass, "api", {}))
return hass.loop.run_until_complete(hass_client())
@asyncio.coroutine
def test_api_list_state_entities(hass, mock_api_client):
"""Test if the debug interface allows us to list state entities."""
hass.states.async_set('test.entity', 'hello')
hass.states.async_set("test.entity", "hello")
resp = yield from mock_api_client.get(const.URL_API_STATES)
assert resp.status == 200
json = yield from resp.json()
@@ -38,11 +38,10 @@ def test_api_list_state_entities(hass, mock_api_client):
@asyncio.coroutine
def test_api_get_state(hass, mock_api_client):
"""Test if the debug interface allows us to get a state."""
hass.states.async_set('hello.world', 'nice', {
'attr': 1,
})
hass.states.async_set("hello.world", "nice", {"attr": 1})
resp = yield from mock_api_client.get(
const.URL_API_STATES_ENTITY.format("hello.world"))
const.URL_API_STATES_ENTITY.format("hello.world")
)
assert resp.status == 200
json = yield from resp.json()
@@ -59,7 +58,8 @@ def test_api_get_state(hass, mock_api_client):
def test_api_get_non_existing_state(hass, mock_api_client):
"""Test if the debug interface allows us to get a state."""
resp = yield from mock_api_client.get(
const.URL_API_STATES_ENTITY.format("does_not_exist"))
const.URL_API_STATES_ENTITY.format("does_not_exist")
)
assert resp.status == 404
@@ -70,7 +70,8 @@ def test_api_state_change(hass, mock_api_client):
yield from mock_api_client.post(
const.URL_API_STATES_ENTITY.format("test.test"),
json={"state": "debug_state_change2"})
json={"state": "debug_state_change2"},
)
assert hass.states.get("test.test").state == "debug_state_change2"
@@ -83,12 +84,12 @@ def test_api_state_change_of_non_existing_entity(hass, mock_api_client):
resp = yield from mock_api_client.post(
const.URL_API_STATES_ENTITY.format("test_entity.that_does_not_exist"),
json={'state': new_state})
json={"state": new_state},
)
assert resp.status == 201
assert hass.states.get("test_entity.that_does_not_exist").state == \
new_state
assert hass.states.get("test_entity.that_does_not_exist").state == new_state
# pylint: disable=invalid-name
@@ -96,8 +97,8 @@ def test_api_state_change_of_non_existing_entity(hass, mock_api_client):
def test_api_state_change_with_bad_data(hass, mock_api_client):
"""Test if API sends appropriate error if we omit state."""
resp = yield from mock_api_client.post(
const.URL_API_STATES_ENTITY.format("test_entity.that_does_not_exist"),
json={})
const.URL_API_STATES_ENTITY.format("test_entity.that_does_not_exist"), json={}
)
assert resp.status == 400
@@ -108,13 +109,15 @@ def test_api_state_change_to_zero_value(hass, mock_api_client):
"""Test if changing a state to a zero value is possible."""
resp = yield from mock_api_client.post(
const.URL_API_STATES_ENTITY.format("test_entity.with_zero_state"),
json={'state': 0})
json={"state": 0},
)
assert resp.status == 201
resp = yield from mock_api_client.post(
const.URL_API_STATES_ENTITY.format("test_entity.with_zero_state"),
json={'state': 0.})
json={"state": 0.0},
)
assert resp.status == 200
@@ -135,14 +138,15 @@ def test_api_state_change_push(hass, mock_api_client):
hass.bus.async_listen(const.EVENT_STATE_CHANGED, event_listener)
yield from mock_api_client.post(
const.URL_API_STATES_ENTITY.format("test.test"),
json={"state": "not_to_be_set"})
const.URL_API_STATES_ENTITY.format("test.test"), json={"state": "not_to_be_set"}
)
yield from hass.async_block_till_done()
assert len(events) == 0
yield from mock_api_client.post(
const.URL_API_STATES_ENTITY.format("test.test"),
json={"state": "not_to_be_set", "force_update": True})
json={"state": "not_to_be_set", "force_update": True},
)
yield from hass.async_block_till_done()
assert len(events) == 1
@@ -161,7 +165,8 @@ def test_api_fire_event_with_no_data(hass, mock_api_client):
hass.bus.async_listen_once("test.event_no_data", listener)
yield from mock_api_client.post(
const.URL_API_EVENTS_EVENT.format("test.event_no_data"))
const.URL_API_EVENTS_EVENT.format("test.event_no_data")
)
yield from hass.async_block_till_done()
assert len(test_value) == 1
@@ -185,8 +190,8 @@ def test_api_fire_event_with_data(hass, mock_api_client):
hass.bus.async_listen_once("test_event_with_data", listener)
yield from mock_api_client.post(
const.URL_API_EVENTS_EVENT.format("test_event_with_data"),
json={"test": 1})
const.URL_API_EVENTS_EVENT.format("test_event_with_data"), json={"test": 1}
)
yield from hass.async_block_till_done()
@@ -208,7 +213,8 @@ def test_api_fire_event_with_invalid_json(hass, mock_api_client):
resp = yield from mock_api_client.post(
const.URL_API_EVENTS_EVENT.format("test_event_bad_data"),
data=json.dumps('not an object'))
data=json.dumps("not an object"),
)
yield from hass.async_block_till_done()
@@ -218,7 +224,8 @@ def test_api_fire_event_with_invalid_json(hass, mock_api_client):
# Try now with valid but unusable JSON
resp = yield from mock_api_client.post(
const.URL_API_EVENTS_EVENT.format("test_event_bad_data"),
data=json.dumps([1, 2, 3]))
data=json.dumps([1, 2, 3]),
)
yield from hass.async_block_till_done()
@@ -231,11 +238,10 @@ def test_api_get_config(hass, mock_api_client):
"""Test the return of the configuration."""
resp = yield from mock_api_client.get(const.URL_API_CONFIG)
result = yield from resp.json()
if 'components' in result:
result['components'] = set(result['components'])
if 'whitelist_external_dirs' in result:
result['whitelist_external_dirs'] = \
set(result['whitelist_external_dirs'])
if "components" in result:
result["components"] = set(result["components"])
if "whitelist_external_dirs" in result:
result["whitelist_external_dirs"] = set(result["whitelist_external_dirs"])
assert hass.config.as_dict() == result
@@ -288,8 +294,8 @@ def test_api_call_service_no_data(hass, mock_api_client):
hass.services.async_register("test_domain", "test_service", listener)
yield from mock_api_client.post(
const.URL_API_SERVICES_SERVICE.format(
"test_domain", "test_service"))
const.URL_API_SERVICES_SERVICE.format("test_domain", "test_service")
)
yield from hass.async_block_till_done()
assert len(test_value) == 1
@@ -312,7 +318,8 @@ def test_api_call_service_with_data(hass, mock_api_client):
yield from mock_api_client.post(
const.URL_API_SERVICES_SERVICE.format("test_domain", "test_service"),
json={"test": 1})
json={"test": 1},
)
yield from hass.async_block_till_done()
assert len(test_value) == 1
@@ -321,25 +328,26 @@ def test_api_call_service_with_data(hass, mock_api_client):
@asyncio.coroutine
def test_api_template(hass, mock_api_client):
"""Test the template API."""
hass.states.async_set('sensor.temperature', 10)
hass.states.async_set("sensor.temperature", 10)
resp = yield from mock_api_client.post(
const.URL_API_TEMPLATE,
json={"template": '{{ states.sensor.temperature.state }}'})
json={"template": "{{ states.sensor.temperature.state }}"},
)
body = yield from resp.text()
assert body == '10'
assert body == "10"
@asyncio.coroutine
def test_api_template_error(hass, mock_api_client):
"""Test the template API."""
hass.states.async_set('sensor.temperature', 10)
hass.states.async_set("sensor.temperature", 10)
resp = yield from mock_api_client.post(
const.URL_API_TEMPLATE,
json={"template": '{{ states.sensor.temperature.state'})
const.URL_API_TEMPLATE, json={"template": "{{ states.sensor.temperature.state"}
)
assert resp.status == 400
@@ -353,11 +361,11 @@ def test_stream(hass, mock_api_client):
assert resp.status == 200
assert listen_count + 1 == _listen_count(hass)
hass.bus.async_fire('test_event')
hass.bus.async_fire("test_event")
data = yield from _stream_next_event(resp.content)
assert data['event_type'] == 'test_event'
assert data["event_type"] == "test_event"
@asyncio.coroutine
@@ -366,18 +374,19 @@ def test_stream_with_restricted(hass, mock_api_client):
listen_count = _listen_count(hass)
resp = yield from mock_api_client.get(
'{}?restrict=test_event1,test_event3'.format(const.URL_API_STREAM))
"{}?restrict=test_event1,test_event3".format(const.URL_API_STREAM)
)
assert resp.status == 200
assert listen_count + 1 == _listen_count(hass)
hass.bus.async_fire('test_event1')
hass.bus.async_fire("test_event1")
data = yield from _stream_next_event(resp.content)
assert data['event_type'] == 'test_event1'
assert data["event_type"] == "test_event1"
hass.bus.async_fire('test_event2')
hass.bus.async_fire('test_event3')
hass.bus.async_fire("test_event2")
hass.bus.async_fire("test_event3")
data = yield from _stream_next_event(resp.content)
assert data['event_type'] == 'test_event3'
assert data["event_type"] == "test_event3"
@asyncio.coroutine
@@ -385,18 +394,18 @@ def _stream_next_event(stream):
"""Read the stream for next event while ignoring ping."""
while True:
last_new_line = False
data = b''
data = b""
while True:
dat = yield from stream.read(1)
if dat == b'\n' and last_new_line:
if dat == b"\n" and last_new_line:
break
data += dat
last_new_line = dat == b'\n'
last_new_line = dat == b"\n"
conv = data.decode('utf-8').strip()[6:]
conv = data.decode("utf-8").strip()[6:]
if conv != 'ping':
if conv != "ping":
break
return json.loads(conv)
@@ -406,11 +415,10 @@ def _listen_count(hass):
return sum(hass.bus.async_listeners().values())
async def test_api_error_log(hass, aiohttp_client, hass_access_token,
hass_admin_user):
async def test_api_error_log(hass, aiohttp_client, hass_access_token, hass_admin_user):
"""Test if we can fetch the error log."""
hass.data[DATA_LOGGING] = '/some/path'
await async_setup_component(hass, 'api', {})
hass.data[DATA_LOGGING] = "/some/path"
await async_setup_component(hass, "api", {})
client = await aiohttp_client(hass.http.app)
resp = await client.get(const.URL_API_ERROR_LOG)
@@ -418,28 +426,28 @@ async def test_api_error_log(hass, aiohttp_client, hass_access_token,
assert resp.status == 401
with patch(
'aiohttp.web.FileResponse',
return_value=web.Response(status=200, text='Hello')
) as mock_file:
resp = await client.get(const.URL_API_ERROR_LOG, headers={
'Authorization': 'Bearer {}'.format(hass_access_token)
})
"aiohttp.web.FileResponse", return_value=web.Response(status=200, text="Hello")
) as mock_file:
resp = await client.get(
const.URL_API_ERROR_LOG,
headers={"Authorization": "Bearer {}".format(hass_access_token)},
)
assert len(mock_file.mock_calls) == 1
assert mock_file.mock_calls[0][1][0] == hass.data[DATA_LOGGING]
assert resp.status == 200
assert await resp.text() == 'Hello'
assert await resp.text() == "Hello"
# Verify we require admin user
hass_admin_user.groups = []
resp = await client.get(const.URL_API_ERROR_LOG, headers={
'Authorization': 'Bearer {}'.format(hass_access_token)
})
resp = await client.get(
const.URL_API_ERROR_LOG,
headers={"Authorization": "Bearer {}".format(hass_access_token)},
)
assert resp.status == 401
async def test_api_fire_event_context(hass, mock_api_client,
hass_access_token):
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 = []
@@ -452,32 +460,27 @@ async def test_api_fire_event_context(hass, mock_api_client,
await mock_api_client.post(
const.URL_API_EVENTS_EVENT.format("test.event"),
headers={
'authorization': 'Bearer {}'.format(hass_access_token)
})
headers={"authorization": "Bearer {}".format(hass_access_token)},
)
await hass.async_block_till_done()
refresh_token = await hass.auth.async_validate_access_token(
hass_access_token)
refresh_token = await hass.auth.async_validate_access_token(hass_access_token)
assert len(test_value) == 1
assert test_value[0].context.user_id == refresh_token.user.id
async def test_api_call_service_context(hass, mock_api_client,
hass_access_token):
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')
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)
})
"/api/services/test_domain/test_service",
headers={"authorization": "Bearer {}".format(hass_access_token)},
)
await hass.async_block_till_done()
refresh_token = await hass.auth.async_validate_access_token(
hass_access_token)
refresh_token = await hass.auth.async_validate_access_token(hass_access_token)
assert len(calls) == 1
assert calls[0].context.user_id == refresh_token.user.id
@@ -486,79 +489,65 @@ async def test_api_call_service_context(hass, mock_api_client,
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)
})
"/api/states/light.kitchen",
json={"state": "on"},
headers={"authorization": "Bearer {}".format(hass_access_token)},
)
refresh_token = await hass.auth.async_validate_access_token(
hass_access_token)
refresh_token = await hass.auth.async_validate_access_token(hass_access_token)
state = hass.states.get('light.kitchen')
state = hass.states.get("light.kitchen")
assert state.context.user_id == refresh_token.user.id
async def test_event_stream_requires_admin(hass, mock_api_client,
hass_admin_user):
async def test_event_stream_requires_admin(hass, mock_api_client, hass_admin_user):
"""Test user needs to be admin to access event stream."""
hass_admin_user.groups = []
resp = await mock_api_client.get('/api/stream')
resp = await mock_api_client.get("/api/stream")
assert resp.status == 401
async def test_states_view_filters(hass, mock_api_client, hass_admin_user):
"""Test filtering only visible states."""
hass_admin_user.mock_policy({
'entities': {
'entity_ids': {
'test.entity': True
}
}
})
hass.states.async_set('test.entity', 'hello')
hass.states.async_set('test.not_visible_entity', 'invisible')
hass_admin_user.mock_policy({"entities": {"entity_ids": {"test.entity": True}}})
hass.states.async_set("test.entity", "hello")
hass.states.async_set("test.not_visible_entity", "invisible")
resp = await mock_api_client.get(const.URL_API_STATES)
assert resp.status == 200
json = await resp.json()
assert len(json) == 1
assert json[0]['entity_id'] == 'test.entity'
assert json[0]["entity_id"] == "test.entity"
async def test_get_entity_state_read_perm(hass, mock_api_client,
hass_admin_user):
async def test_get_entity_state_read_perm(hass, mock_api_client, hass_admin_user):
"""Test getting a state requires read permission."""
hass_admin_user.mock_policy({})
resp = await mock_api_client.get('/api/states/light.test')
resp = await mock_api_client.get("/api/states/light.test")
assert resp.status == 401
async def test_post_entity_state_admin(hass, mock_api_client, hass_admin_user):
"""Test updating state requires admin."""
hass_admin_user.groups = []
resp = await mock_api_client.post('/api/states/light.test')
resp = await mock_api_client.post("/api/states/light.test")
assert resp.status == 401
async def test_delete_entity_state_admin(hass, mock_api_client,
hass_admin_user):
async def test_delete_entity_state_admin(hass, mock_api_client, hass_admin_user):
"""Test deleting entity requires admin."""
hass_admin_user.groups = []
resp = await mock_api_client.delete('/api/states/light.test')
resp = await mock_api_client.delete("/api/states/light.test")
assert resp.status == 401
async def test_post_event_admin(hass, mock_api_client, hass_admin_user):
"""Test sending event requires admin."""
hass_admin_user.groups = []
resp = await mock_api_client.post('/api/events/state_changed')
resp = await mock_api_client.post("/api/events/state_changed")
assert resp.status == 401
async def test_rendering_template_admin(hass, mock_api_client,
hass_admin_user):
async def test_rendering_template_admin(hass, mock_api_client, hass_admin_user):
"""Test rendering a template requires admin."""
hass_admin_user.groups = []
resp = await mock_api_client.post(const.URL_API_TEMPLATE)
@@ -566,13 +555,14 @@ async def test_rendering_template_admin(hass, mock_api_client,
async def test_rendering_template_legacy_user(
hass, mock_api_client, aiohttp_client, legacy_auth):
hass, mock_api_client, aiohttp_client, legacy_auth
):
"""Test rendering a template with legacy API password."""
hass.states.async_set('sensor.temperature', 10)
hass.states.async_set("sensor.temperature", 10)
client = await aiohttp_client(hass.http.app)
resp = await client.post(
const.URL_API_TEMPLATE,
json={"template": '{{ states.sensor.temperature.state }}'}
json={"template": "{{ states.sensor.temperature.state }}"},
)
assert resp.status == 401
@@ -580,8 +570,8 @@ async def test_rendering_template_legacy_user(
async def test_api_call_service_not_found(hass, mock_api_client):
"""Test if the API failes 400 if unknown service."""
resp = await mock_api_client.post(
const.URL_API_SERVICES_SERVICE.format(
"test_domain", "test_service"))
const.URL_API_SERVICES_SERVICE.format("test_domain", "test_service")
)
assert resp.status == 400
@@ -594,10 +584,12 @@ async def test_api_call_service_bad_data(hass, mock_api_client):
"""Record that our service got called."""
test_value.append(1)
hass.services.async_register("test_domain", "test_service", listener,
schema=vol.Schema({'hello': str}))
hass.services.async_register(
"test_domain", "test_service", listener, schema=vol.Schema({"hello": str})
)
resp = await mock_api_client.post(
const.URL_API_SERVICES_SERVICE.format(
"test_domain", "test_service"), json={'hello': 5})
const.URL_API_SERVICES_SERVICE.format("test_domain", "test_service"),
json={"hello": 5},
)
assert resp.status == 400