2014-11-23 17:51:16 +00:00
|
|
|
"""
|
2016-02-13 13:19:11 +00:00
|
|
|
tests.components.test_api
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~
|
2014-11-23 17:51:16 +00:00
|
|
|
|
2014-11-23 20:57:29 +00:00
|
|
|
Tests Home Assistant HTTP component does what it should do.
|
2014-11-23 17:51:16 +00:00
|
|
|
"""
|
|
|
|
# pylint: disable=protected-access,too-many-public-methods
|
2015-12-15 07:20:43 +00:00
|
|
|
from contextlib import closing
|
2014-11-23 17:51:16 +00:00
|
|
|
import json
|
2015-11-28 22:08:01 +00:00
|
|
|
import tempfile
|
2015-12-15 07:20:43 +00:00
|
|
|
import unittest
|
|
|
|
from unittest.mock import patch
|
2014-11-23 17:51:16 +00:00
|
|
|
|
|
|
|
import requests
|
|
|
|
|
2015-11-28 22:08:01 +00:00
|
|
|
from homeassistant import bootstrap, const
|
2015-08-17 03:44:46 +00:00
|
|
|
import homeassistant.core as ha
|
2014-11-23 17:51:16 +00:00
|
|
|
import homeassistant.components.http as http
|
|
|
|
|
2016-02-14 20:54:16 +00:00
|
|
|
from tests.common import get_test_instance_port
|
2014-11-23 20:57:29 +00:00
|
|
|
|
2016-02-14 20:54:16 +00:00
|
|
|
API_PASSWORD = "test1234"
|
|
|
|
SERVER_PORT = get_test_instance_port()
|
2014-11-23 20:57:29 +00:00
|
|
|
HTTP_BASE_URL = "http://127.0.0.1:{}".format(SERVER_PORT)
|
2015-11-28 22:08:01 +00:00
|
|
|
HA_HEADERS = {const.HTTP_HEADER_HA_AUTH: API_PASSWORD}
|
2014-11-23 17:51:16 +00:00
|
|
|
|
2014-11-24 06:18:51 +00:00
|
|
|
hass = None
|
|
|
|
|
2014-11-23 17:51:16 +00:00
|
|
|
|
2014-11-23 20:57:29 +00:00
|
|
|
def _url(path=""):
|
|
|
|
""" Helper method to generate urls. """
|
|
|
|
return HTTP_BASE_URL + path
|
|
|
|
|
|
|
|
|
2015-09-01 07:18:26 +00:00
|
|
|
@patch('homeassistant.components.http.util.get_local_ip',
|
|
|
|
return_value='127.0.0.1')
|
|
|
|
def setUpModule(mock_get_local_ip): # pylint: disable=invalid-name
|
2016-02-01 10:49:44 +00:00
|
|
|
""" Initializes a Home Assistant server. """
|
2014-11-23 20:57:29 +00:00
|
|
|
global hass
|
|
|
|
|
|
|
|
hass = ha.HomeAssistant()
|
|
|
|
|
|
|
|
hass.bus.listen('test_event', lambda _: _)
|
|
|
|
hass.states.set('test.test', 'a_state')
|
|
|
|
|
2015-01-30 07:56:04 +00:00
|
|
|
bootstrap.setup_component(
|
|
|
|
hass, http.DOMAIN,
|
|
|
|
{http.DOMAIN: {http.CONF_API_PASSWORD: API_PASSWORD,
|
|
|
|
http.CONF_SERVER_PORT: SERVER_PORT}})
|
|
|
|
|
|
|
|
bootstrap.setup_component(hass, 'api')
|
2014-11-23 20:57:29 +00:00
|
|
|
|
|
|
|
hass.start()
|
|
|
|
|
|
|
|
|
2014-11-24 06:18:51 +00:00
|
|
|
def tearDownModule(): # pylint: disable=invalid-name
|
2014-11-23 20:57:29 +00:00
|
|
|
""" Stops the Home Assistant server. """
|
|
|
|
hass.stop()
|
|
|
|
|
|
|
|
|
2015-01-30 07:56:04 +00:00
|
|
|
class TestAPI(unittest.TestCase):
|
|
|
|
""" Test the API. """
|
2014-11-29 06:27:44 +00:00
|
|
|
|
2015-01-30 07:56:04 +00:00
|
|
|
# TODO move back to http component and test with use_auth.
|
|
|
|
def test_access_denied_without_password(self):
|
2015-12-07 05:09:39 +00:00
|
|
|
req = requests.get(_url(const.URL_API))
|
2014-11-23 17:51:16 +00:00
|
|
|
|
|
|
|
self.assertEqual(401, req.status_code)
|
|
|
|
|
2015-01-30 07:56:04 +00:00
|
|
|
def test_access_denied_with_wrong_password(self):
|
2014-11-23 17:51:16 +00:00
|
|
|
req = requests.get(
|
2015-12-07 05:09:39 +00:00
|
|
|
_url(const.URL_API),
|
2015-11-28 22:08:01 +00:00
|
|
|
headers={const.HTTP_HEADER_HA_AUTH: 'wrongpassword'})
|
2014-11-23 17:51:16 +00:00
|
|
|
|
|
|
|
self.assertEqual(401, req.status_code)
|
|
|
|
|
2015-12-07 05:09:39 +00:00
|
|
|
def test_access_with_password_in_url(self):
|
|
|
|
req = requests.get(
|
|
|
|
"{}?api_password={}".format(_url(const.URL_API), API_PASSWORD))
|
|
|
|
|
|
|
|
self.assertEqual(200, req.status_code)
|
|
|
|
|
|
|
|
def test_access_via_session(self):
|
|
|
|
session = requests.Session()
|
|
|
|
req = session.get(_url(const.URL_API), headers=HA_HEADERS)
|
|
|
|
self.assertEqual(200, req.status_code)
|
|
|
|
|
|
|
|
req = session.get(_url(const.URL_API))
|
|
|
|
self.assertEqual(200, req.status_code)
|
|
|
|
|
2014-11-23 17:51:16 +00:00
|
|
|
def test_api_list_state_entities(self):
|
|
|
|
""" Test if the debug interface allows us to list state entities. """
|
2015-11-28 22:08:01 +00:00
|
|
|
req = requests.get(_url(const.URL_API_STATES),
|
2014-11-23 17:51:16 +00:00
|
|
|
headers=HA_HEADERS)
|
|
|
|
|
|
|
|
remote_data = [ha.State.from_dict(item) for item in req.json()]
|
|
|
|
|
2014-11-23 20:57:29 +00:00
|
|
|
self.assertEqual(hass.states.all(), remote_data)
|
2014-11-23 17:51:16 +00:00
|
|
|
|
|
|
|
def test_api_get_state(self):
|
|
|
|
""" Test if the debug interface allows us to get a state. """
|
|
|
|
req = requests.get(
|
2015-11-28 22:08:01 +00:00
|
|
|
_url(const.URL_API_STATES_ENTITY.format("test.test")),
|
2014-11-23 17:51:16 +00:00
|
|
|
headers=HA_HEADERS)
|
|
|
|
|
|
|
|
data = ha.State.from_dict(req.json())
|
|
|
|
|
2014-11-23 20:57:29 +00:00
|
|
|
state = hass.states.get("test.test")
|
2014-11-23 17:51:16 +00:00
|
|
|
|
|
|
|
self.assertEqual(state.state, data.state)
|
|
|
|
self.assertEqual(state.last_changed, data.last_changed)
|
|
|
|
self.assertEqual(state.attributes, data.attributes)
|
|
|
|
|
|
|
|
def test_api_get_non_existing_state(self):
|
|
|
|
""" Test if the debug interface allows us to get a state. """
|
|
|
|
req = requests.get(
|
2015-11-28 22:08:01 +00:00
|
|
|
_url(const.URL_API_STATES_ENTITY.format("does_not_exist")),
|
2014-11-23 17:51:16 +00:00
|
|
|
headers=HA_HEADERS)
|
|
|
|
|
|
|
|
self.assertEqual(404, req.status_code)
|
|
|
|
|
|
|
|
def test_api_state_change(self):
|
|
|
|
""" Test if we can change the state of an entity that exists. """
|
|
|
|
|
2014-11-23 20:57:29 +00:00
|
|
|
hass.states.set("test.test", "not_to_be_set")
|
2014-11-23 17:51:16 +00:00
|
|
|
|
2015-11-28 22:08:01 +00:00
|
|
|
requests.post(_url(const.URL_API_STATES_ENTITY.format("test.test")),
|
2014-11-29 06:27:44 +00:00
|
|
|
data=json.dumps({"state": "debug_state_change2"}),
|
|
|
|
headers=HA_HEADERS)
|
2014-11-23 17:51:16 +00:00
|
|
|
|
|
|
|
self.assertEqual("debug_state_change2",
|
2014-11-23 20:57:29 +00:00
|
|
|
hass.states.get("test.test").state)
|
2014-11-23 17:51:16 +00:00
|
|
|
|
|
|
|
# pylint: disable=invalid-name
|
|
|
|
def test_api_state_change_of_non_existing_entity(self):
|
|
|
|
""" Test if the API allows us to change a state of
|
|
|
|
a non existing entity. """
|
|
|
|
|
|
|
|
new_state = "debug_state_change"
|
|
|
|
|
|
|
|
req = requests.post(
|
2015-11-28 22:08:01 +00:00
|
|
|
_url(const.URL_API_STATES_ENTITY.format(
|
2014-11-23 17:51:16 +00:00
|
|
|
"test_entity.that_does_not_exist")),
|
2014-11-29 06:27:44 +00:00
|
|
|
data=json.dumps({'state': new_state}),
|
|
|
|
headers=HA_HEADERS)
|
2014-11-23 17:51:16 +00:00
|
|
|
|
2014-11-23 20:57:29 +00:00
|
|
|
cur_state = (hass.states.
|
2014-11-23 17:51:16 +00:00
|
|
|
get("test_entity.that_does_not_exist").state)
|
|
|
|
|
|
|
|
self.assertEqual(201, req.status_code)
|
|
|
|
self.assertEqual(cur_state, new_state)
|
|
|
|
|
2014-11-29 06:27:44 +00:00
|
|
|
# pylint: disable=invalid-name
|
|
|
|
def test_api_state_change_with_bad_data(self):
|
|
|
|
""" Test if API sends appropriate error if we omit state. """
|
|
|
|
|
|
|
|
req = requests.post(
|
2015-11-28 22:08:01 +00:00
|
|
|
_url(const.URL_API_STATES_ENTITY.format(
|
2014-11-29 06:27:44 +00:00
|
|
|
"test_entity.that_does_not_exist")),
|
|
|
|
data=json.dumps({}),
|
|
|
|
headers=HA_HEADERS)
|
|
|
|
|
|
|
|
self.assertEqual(400, req.status_code)
|
|
|
|
|
2014-11-23 17:51:16 +00:00
|
|
|
# pylint: disable=invalid-name
|
|
|
|
def test_api_fire_event_with_no_data(self):
|
|
|
|
""" Test if the API allows us to fire an event. """
|
|
|
|
test_value = []
|
|
|
|
|
2015-01-20 07:40:51 +00:00
|
|
|
def listener(event):
|
2014-11-23 17:51:16 +00:00
|
|
|
""" Helper method that will verify our event got called. """
|
|
|
|
test_value.append(1)
|
|
|
|
|
2014-11-29 07:19:59 +00:00
|
|
|
hass.bus.listen_once("test.event_no_data", listener)
|
2014-11-23 17:51:16 +00:00
|
|
|
|
|
|
|
requests.post(
|
2015-11-28 22:08:01 +00:00
|
|
|
_url(const.URL_API_EVENTS_EVENT.format("test.event_no_data")),
|
2014-11-23 17:51:16 +00:00
|
|
|
headers=HA_HEADERS)
|
|
|
|
|
2014-12-17 05:46:02 +00:00
|
|
|
hass.pool.block_till_done()
|
2014-11-23 17:51:16 +00:00
|
|
|
|
|
|
|
self.assertEqual(1, len(test_value))
|
|
|
|
|
|
|
|
# pylint: disable=invalid-name
|
|
|
|
def test_api_fire_event_with_data(self):
|
|
|
|
""" Test if the API allows us to fire an event. """
|
|
|
|
test_value = []
|
|
|
|
|
2015-01-20 07:40:51 +00:00
|
|
|
def listener(event):
|
2014-11-23 17:51:16 +00:00
|
|
|
""" Helper method that will verify that our event got called and
|
|
|
|
that test if our data came through. """
|
|
|
|
if "test" in event.data:
|
|
|
|
test_value.append(1)
|
|
|
|
|
2014-11-29 07:19:59 +00:00
|
|
|
hass.bus.listen_once("test_event_with_data", listener)
|
2014-11-23 17:51:16 +00:00
|
|
|
|
|
|
|
requests.post(
|
2015-11-28 22:08:01 +00:00
|
|
|
_url(const.URL_API_EVENTS_EVENT.format("test_event_with_data")),
|
2014-11-23 17:51:16 +00:00
|
|
|
data=json.dumps({"test": 1}),
|
|
|
|
headers=HA_HEADERS)
|
|
|
|
|
2014-12-17 05:46:02 +00:00
|
|
|
hass.pool.block_till_done()
|
2014-11-23 17:51:16 +00:00
|
|
|
|
|
|
|
self.assertEqual(1, len(test_value))
|
|
|
|
|
|
|
|
# pylint: disable=invalid-name
|
|
|
|
def test_api_fire_event_with_invalid_json(self):
|
|
|
|
""" Test if the API allows us to fire an event. """
|
|
|
|
test_value = []
|
|
|
|
|
2015-01-20 07:40:51 +00:00
|
|
|
def listener(event):
|
2014-11-23 17:51:16 +00:00
|
|
|
""" Helper method that will verify our event got called. """
|
|
|
|
test_value.append(1)
|
|
|
|
|
2014-11-29 07:19:59 +00:00
|
|
|
hass.bus.listen_once("test_event_bad_data", listener)
|
2014-11-23 17:51:16 +00:00
|
|
|
|
|
|
|
req = requests.post(
|
2015-11-28 22:08:01 +00:00
|
|
|
_url(const.URL_API_EVENTS_EVENT.format("test_event_bad_data")),
|
2014-11-23 17:51:16 +00:00
|
|
|
data=json.dumps('not an object'),
|
|
|
|
headers=HA_HEADERS)
|
|
|
|
|
2014-12-17 05:46:02 +00:00
|
|
|
hass.pool.block_till_done()
|
2014-11-23 17:51:16 +00:00
|
|
|
|
|
|
|
self.assertEqual(422, req.status_code)
|
|
|
|
self.assertEqual(0, len(test_value))
|
|
|
|
|
2014-11-29 06:27:44 +00:00
|
|
|
# Try now with valid but unusable JSON
|
|
|
|
req = requests.post(
|
2015-11-28 22:08:01 +00:00
|
|
|
_url(const.URL_API_EVENTS_EVENT.format("test_event_bad_data")),
|
2014-11-29 06:27:44 +00:00
|
|
|
data=json.dumps([1, 2, 3]),
|
|
|
|
headers=HA_HEADERS)
|
|
|
|
|
2014-12-17 05:46:02 +00:00
|
|
|
hass.pool.block_till_done()
|
2014-11-29 06:27:44 +00:00
|
|
|
|
|
|
|
self.assertEqual(422, req.status_code)
|
|
|
|
self.assertEqual(0, len(test_value))
|
|
|
|
|
2015-11-28 22:08:01 +00:00
|
|
|
def test_api_get_config(self):
|
|
|
|
req = requests.get(_url(const.URL_API_CONFIG),
|
|
|
|
headers=HA_HEADERS)
|
|
|
|
self.assertEqual(hass.config.as_dict(), req.json())
|
|
|
|
|
|
|
|
def test_api_get_components(self):
|
|
|
|
req = requests.get(_url(const.URL_API_COMPONENTS),
|
|
|
|
headers=HA_HEADERS)
|
|
|
|
self.assertEqual(hass.config.components, req.json())
|
|
|
|
|
|
|
|
def test_api_get_error_log(self):
|
|
|
|
test_content = 'Test String'
|
|
|
|
with tempfile.NamedTemporaryFile() as log:
|
|
|
|
log.write(test_content.encode('utf-8'))
|
|
|
|
log.flush()
|
|
|
|
|
|
|
|
with patch.object(hass.config, 'path', return_value=log.name):
|
|
|
|
req = requests.get(_url(const.URL_API_ERROR_LOG),
|
|
|
|
headers=HA_HEADERS)
|
|
|
|
self.assertEqual(test_content, req.text)
|
|
|
|
self.assertIsNone(req.headers.get('expires'))
|
|
|
|
|
2014-11-23 17:51:16 +00:00
|
|
|
def test_api_get_event_listeners(self):
|
|
|
|
""" Test if we can get the list of events being listened for. """
|
2015-11-28 22:08:01 +00:00
|
|
|
req = requests.get(_url(const.URL_API_EVENTS),
|
2014-11-23 17:51:16 +00:00
|
|
|
headers=HA_HEADERS)
|
|
|
|
|
2014-11-23 20:57:29 +00:00
|
|
|
local = hass.bus.listeners
|
2014-11-23 17:51:16 +00:00
|
|
|
|
|
|
|
for event in req.json():
|
|
|
|
self.assertEqual(event["listener_count"],
|
|
|
|
local.pop(event["event"]))
|
|
|
|
|
|
|
|
self.assertEqual(0, len(local))
|
|
|
|
|
|
|
|
def test_api_get_services(self):
|
|
|
|
""" Test if we can get a dict describing current services. """
|
2015-11-28 22:08:01 +00:00
|
|
|
req = requests.get(_url(const.URL_API_SERVICES),
|
2014-11-23 17:51:16 +00:00
|
|
|
headers=HA_HEADERS)
|
|
|
|
|
2014-11-23 20:57:29 +00:00
|
|
|
local_services = hass.services.services
|
2014-11-23 17:51:16 +00:00
|
|
|
|
|
|
|
for serv_domain in req.json():
|
|
|
|
local = local_services.pop(serv_domain["domain"])
|
|
|
|
|
|
|
|
self.assertEqual(local, serv_domain["services"])
|
|
|
|
|
|
|
|
def test_api_call_service_no_data(self):
|
|
|
|
""" Test if the API allows us to call a service. """
|
|
|
|
test_value = []
|
|
|
|
|
2015-01-20 07:40:51 +00:00
|
|
|
def listener(service_call):
|
2014-11-23 17:51:16 +00:00
|
|
|
""" Helper method that will verify that our service got called. """
|
|
|
|
test_value.append(1)
|
|
|
|
|
2014-11-23 20:57:29 +00:00
|
|
|
hass.services.register("test_domain", "test_service", listener)
|
2014-11-23 17:51:16 +00:00
|
|
|
|
|
|
|
requests.post(
|
2015-11-28 22:08:01 +00:00
|
|
|
_url(const.URL_API_SERVICES_SERVICE.format(
|
2014-11-23 17:51:16 +00:00
|
|
|
"test_domain", "test_service")),
|
|
|
|
headers=HA_HEADERS)
|
|
|
|
|
2014-12-17 05:46:02 +00:00
|
|
|
hass.pool.block_till_done()
|
2014-11-23 17:51:16 +00:00
|
|
|
|
|
|
|
self.assertEqual(1, len(test_value))
|
|
|
|
|
|
|
|
def test_api_call_service_with_data(self):
|
|
|
|
""" Test if the API allows us to call a service. """
|
|
|
|
test_value = []
|
|
|
|
|
2015-01-20 07:40:51 +00:00
|
|
|
def listener(service_call):
|
2014-11-23 17:51:16 +00:00
|
|
|
""" Helper method that will verify that our service got called and
|
|
|
|
that test if our data came through. """
|
|
|
|
if "test" in service_call.data:
|
|
|
|
test_value.append(1)
|
|
|
|
|
2014-11-23 20:57:29 +00:00
|
|
|
hass.services.register("test_domain", "test_service", listener)
|
2014-11-23 17:51:16 +00:00
|
|
|
|
|
|
|
requests.post(
|
2015-11-28 22:08:01 +00:00
|
|
|
_url(const.URL_API_SERVICES_SERVICE.format(
|
2014-11-23 17:51:16 +00:00
|
|
|
"test_domain", "test_service")),
|
|
|
|
data=json.dumps({"test": 1}),
|
|
|
|
headers=HA_HEADERS)
|
|
|
|
|
2014-12-17 05:46:02 +00:00
|
|
|
hass.pool.block_till_done()
|
2014-11-23 17:51:16 +00:00
|
|
|
|
|
|
|
self.assertEqual(1, len(test_value))
|
2014-11-29 06:27:44 +00:00
|
|
|
|
2015-12-10 07:56:20 +00:00
|
|
|
def test_api_template(self):
|
|
|
|
""" Test template API. """
|
|
|
|
hass.states.set('sensor.temperature', 10)
|
|
|
|
|
|
|
|
req = requests.post(
|
|
|
|
_url(const.URL_API_TEMPLATE),
|
|
|
|
data=json.dumps({"template":
|
|
|
|
'{{ states.sensor.temperature.state }}'}),
|
|
|
|
headers=HA_HEADERS)
|
|
|
|
|
|
|
|
self.assertEqual('10', req.text)
|
|
|
|
|
2015-12-18 07:33:09 +00:00
|
|
|
def test_api_template_error(self):
|
|
|
|
""" Test template API. """
|
|
|
|
hass.states.set('sensor.temperature', 10)
|
|
|
|
|
|
|
|
req = requests.post(
|
|
|
|
_url(const.URL_API_TEMPLATE),
|
|
|
|
data=json.dumps({"template":
|
|
|
|
'{{ states.sensor.temperature.state'}),
|
|
|
|
headers=HA_HEADERS)
|
|
|
|
|
|
|
|
self.assertEqual(422, req.status_code)
|
|
|
|
|
2014-11-29 06:27:44 +00:00
|
|
|
def test_api_event_forward(self):
|
|
|
|
""" Test setting up event forwarding. """
|
|
|
|
|
|
|
|
req = requests.post(
|
2015-11-28 22:08:01 +00:00
|
|
|
_url(const.URL_API_EVENT_FORWARD),
|
2014-11-29 06:27:44 +00:00
|
|
|
headers=HA_HEADERS)
|
|
|
|
self.assertEqual(400, req.status_code)
|
|
|
|
|
|
|
|
req = requests.post(
|
2015-11-28 22:08:01 +00:00
|
|
|
_url(const.URL_API_EVENT_FORWARD),
|
2014-11-29 06:27:44 +00:00
|
|
|
data=json.dumps({'host': '127.0.0.1'}),
|
|
|
|
headers=HA_HEADERS)
|
|
|
|
self.assertEqual(400, req.status_code)
|
|
|
|
|
|
|
|
req = requests.post(
|
2015-11-28 22:08:01 +00:00
|
|
|
_url(const.URL_API_EVENT_FORWARD),
|
2014-11-29 06:27:44 +00:00
|
|
|
data=json.dumps({'api_password': 'bla-di-bla'}),
|
|
|
|
headers=HA_HEADERS)
|
|
|
|
self.assertEqual(400, req.status_code)
|
|
|
|
|
|
|
|
req = requests.post(
|
2015-11-28 22:08:01 +00:00
|
|
|
_url(const.URL_API_EVENT_FORWARD),
|
2014-11-29 06:27:44 +00:00
|
|
|
data=json.dumps({
|
|
|
|
'api_password': 'bla-di-bla',
|
|
|
|
'host': '127.0.0.1',
|
|
|
|
'port': 'abcd'
|
|
|
|
}),
|
|
|
|
headers=HA_HEADERS)
|
|
|
|
self.assertEqual(422, req.status_code)
|
|
|
|
|
|
|
|
req = requests.post(
|
2015-11-28 22:08:01 +00:00
|
|
|
_url(const.URL_API_EVENT_FORWARD),
|
2014-11-29 06:27:44 +00:00
|
|
|
data=json.dumps({
|
|
|
|
'api_password': 'bla-di-bla',
|
|
|
|
'host': '127.0.0.1',
|
2016-02-14 20:54:16 +00:00
|
|
|
'port': get_test_instance_port()
|
2014-11-29 06:27:44 +00:00
|
|
|
}),
|
|
|
|
headers=HA_HEADERS)
|
|
|
|
self.assertEqual(422, req.status_code)
|
|
|
|
|
|
|
|
# Setup a real one
|
|
|
|
req = requests.post(
|
2015-11-28 22:08:01 +00:00
|
|
|
_url(const.URL_API_EVENT_FORWARD),
|
2014-11-29 06:27:44 +00:00
|
|
|
data=json.dumps({
|
|
|
|
'api_password': API_PASSWORD,
|
|
|
|
'host': '127.0.0.1',
|
|
|
|
'port': SERVER_PORT
|
|
|
|
}),
|
|
|
|
headers=HA_HEADERS)
|
|
|
|
self.assertEqual(200, req.status_code)
|
|
|
|
|
|
|
|
# Delete it again..
|
|
|
|
req = requests.delete(
|
2015-11-28 22:08:01 +00:00
|
|
|
_url(const.URL_API_EVENT_FORWARD),
|
2014-11-29 06:27:44 +00:00
|
|
|
data=json.dumps({}),
|
|
|
|
headers=HA_HEADERS)
|
|
|
|
self.assertEqual(400, req.status_code)
|
|
|
|
|
|
|
|
req = requests.delete(
|
2015-11-28 22:08:01 +00:00
|
|
|
_url(const.URL_API_EVENT_FORWARD),
|
2014-11-29 06:27:44 +00:00
|
|
|
data=json.dumps({
|
|
|
|
'host': '127.0.0.1',
|
|
|
|
'port': 'abcd'
|
|
|
|
}),
|
|
|
|
headers=HA_HEADERS)
|
|
|
|
self.assertEqual(422, req.status_code)
|
|
|
|
|
|
|
|
req = requests.delete(
|
2015-11-28 22:08:01 +00:00
|
|
|
_url(const.URL_API_EVENT_FORWARD),
|
2014-11-29 06:27:44 +00:00
|
|
|
data=json.dumps({
|
|
|
|
'host': '127.0.0.1',
|
|
|
|
'port': SERVER_PORT
|
|
|
|
}),
|
|
|
|
headers=HA_HEADERS)
|
|
|
|
self.assertEqual(200, req.status_code)
|
2015-12-15 07:20:43 +00:00
|
|
|
|
|
|
|
def test_stream(self):
|
|
|
|
listen_count = self._listen_count()
|
|
|
|
with closing(requests.get(_url(const.URL_API_STREAM),
|
|
|
|
stream=True, headers=HA_HEADERS)) as req:
|
|
|
|
|
|
|
|
data = self._stream_next_event(req)
|
|
|
|
self.assertEqual('ping', data)
|
|
|
|
|
2015-12-15 07:27:22 +00:00
|
|
|
self.assertEqual(listen_count + 1, self._listen_count())
|
|
|
|
|
2015-12-15 07:20:43 +00:00
|
|
|
hass.bus.fire('test_event')
|
|
|
|
hass.pool.block_till_done()
|
|
|
|
|
|
|
|
data = self._stream_next_event(req)
|
|
|
|
|
|
|
|
self.assertEqual('test_event', data['event_type'])
|
|
|
|
|
|
|
|
def test_stream_with_restricted(self):
|
|
|
|
listen_count = self._listen_count()
|
|
|
|
with closing(requests.get(_url(const.URL_API_STREAM),
|
|
|
|
data=json.dumps({
|
|
|
|
'restrict': 'test_event1,test_event3'}),
|
|
|
|
stream=True, headers=HA_HEADERS)) as req:
|
|
|
|
|
|
|
|
data = self._stream_next_event(req)
|
|
|
|
self.assertEqual('ping', data)
|
|
|
|
|
2015-12-15 07:27:22 +00:00
|
|
|
self.assertEqual(listen_count + 2, self._listen_count())
|
|
|
|
|
2015-12-15 07:20:43 +00:00
|
|
|
hass.bus.fire('test_event1')
|
|
|
|
hass.pool.block_till_done()
|
|
|
|
hass.bus.fire('test_event2')
|
|
|
|
hass.pool.block_till_done()
|
|
|
|
hass.bus.fire('test_event3')
|
|
|
|
hass.pool.block_till_done()
|
|
|
|
|
|
|
|
data = self._stream_next_event(req)
|
|
|
|
self.assertEqual('test_event1', data['event_type'])
|
|
|
|
data = self._stream_next_event(req)
|
|
|
|
self.assertEqual('test_event3', data['event_type'])
|
|
|
|
|
|
|
|
def _stream_next_event(self, stream):
|
|
|
|
data = b''
|
|
|
|
last_new_line = False
|
|
|
|
for dat in stream.iter_content(1):
|
|
|
|
if dat == b'\n' and last_new_line:
|
|
|
|
break
|
|
|
|
data += dat
|
|
|
|
last_new_line = dat == b'\n'
|
|
|
|
|
|
|
|
conv = data.decode('utf-8').strip()[6:]
|
|
|
|
|
|
|
|
return conv if conv == 'ping' else json.loads(conv)
|
|
|
|
|
|
|
|
def _listen_count(self):
|
|
|
|
""" Return number of event listeners. """
|
|
|
|
return sum(hass.bus.listeners.values())
|