2016-11-25 21:04:06 +00:00
|
|
|
"""The tests for the Home Assistant HTTP component."""
|
2016-11-27 22:01:12 +00:00
|
|
|
import asyncio
|
2017-11-04 19:04:05 +00:00
|
|
|
|
|
|
|
from aiohttp.hdrs import (
|
|
|
|
ORIGIN, ACCESS_CONTROL_ALLOW_ORIGIN, ACCESS_CONTROL_ALLOW_HEADERS,
|
|
|
|
ACCESS_CONTROL_REQUEST_METHOD, ACCESS_CONTROL_REQUEST_HEADERS,
|
|
|
|
CONTENT_TYPE)
|
2016-11-25 21:04:06 +00:00
|
|
|
import requests
|
2017-11-04 19:04:05 +00:00
|
|
|
from tests.common import get_test_instance_port, get_test_home_assistant
|
2016-11-25 21:04:06 +00:00
|
|
|
|
2017-11-04 19:04:05 +00:00
|
|
|
from homeassistant import const, setup
|
2016-11-25 21:04:06 +00:00
|
|
|
import homeassistant.components.http as http
|
|
|
|
|
|
|
|
API_PASSWORD = 'test1234'
|
|
|
|
SERVER_PORT = get_test_instance_port()
|
|
|
|
HTTP_BASE = '127.0.0.1:{}'.format(SERVER_PORT)
|
|
|
|
HTTP_BASE_URL = 'http://{}'.format(HTTP_BASE)
|
|
|
|
HA_HEADERS = {
|
|
|
|
const.HTTP_HEADER_HA_AUTH: API_PASSWORD,
|
2017-11-04 19:04:05 +00:00
|
|
|
CONTENT_TYPE: const.CONTENT_TYPE_JSON,
|
2016-11-25 21:04:06 +00:00
|
|
|
}
|
|
|
|
CORS_ORIGINS = [HTTP_BASE_URL, HTTP_BASE]
|
|
|
|
|
|
|
|
hass = None
|
|
|
|
|
|
|
|
|
|
|
|
def _url(path=''):
|
|
|
|
"""Helper method to generate URLs."""
|
|
|
|
return HTTP_BASE_URL + path
|
|
|
|
|
|
|
|
|
|
|
|
# pylint: disable=invalid-name
|
|
|
|
def setUpModule():
|
|
|
|
"""Initialize a Home Assistant server."""
|
|
|
|
global hass
|
|
|
|
|
|
|
|
hass = get_test_home_assistant()
|
|
|
|
|
2017-03-05 09:41:54 +00:00
|
|
|
setup.setup_component(
|
2016-11-25 21:04:06 +00:00
|
|
|
hass, http.DOMAIN, {
|
|
|
|
http.DOMAIN: {
|
|
|
|
http.CONF_API_PASSWORD: API_PASSWORD,
|
|
|
|
http.CONF_SERVER_PORT: SERVER_PORT,
|
|
|
|
http.CONF_CORS_ORIGINS: CORS_ORIGINS,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2017-03-05 09:41:54 +00:00
|
|
|
setup.setup_component(hass, 'api')
|
2016-11-25 21:04:06 +00:00
|
|
|
|
2016-12-04 18:57:24 +00:00
|
|
|
# Registering static path as it caused CORS to blow up
|
|
|
|
hass.http.register_static_path(
|
|
|
|
'/custom_components', hass.config.path('custom_components'))
|
|
|
|
|
2016-11-25 21:04:06 +00:00
|
|
|
hass.start()
|
|
|
|
|
|
|
|
|
|
|
|
# pylint: disable=invalid-name
|
|
|
|
def tearDownModule():
|
|
|
|
"""Stop the Home Assistant server."""
|
|
|
|
hass.stop()
|
|
|
|
|
|
|
|
|
2016-12-04 18:57:24 +00:00
|
|
|
class TestCors:
|
2016-11-25 21:04:06 +00:00
|
|
|
"""Test HTTP component."""
|
|
|
|
|
|
|
|
def test_cors_allowed_with_password_in_url(self):
|
|
|
|
"""Test cross origin resource sharing with password in url."""
|
|
|
|
req = requests.get(_url(const.URL_API),
|
|
|
|
params={'api_password': API_PASSWORD},
|
2017-11-04 19:04:05 +00:00
|
|
|
headers={ORIGIN: HTTP_BASE_URL})
|
2016-11-25 21:04:06 +00:00
|
|
|
|
2017-11-04 19:04:05 +00:00
|
|
|
allow_origin = ACCESS_CONTROL_ALLOW_ORIGIN
|
2016-11-25 21:04:06 +00:00
|
|
|
|
|
|
|
assert req.status_code == 200
|
|
|
|
assert req.headers.get(allow_origin) == HTTP_BASE_URL
|
|
|
|
|
|
|
|
def test_cors_allowed_with_password_in_header(self):
|
|
|
|
"""Test cross origin resource sharing with password in header."""
|
|
|
|
headers = {
|
|
|
|
const.HTTP_HEADER_HA_AUTH: API_PASSWORD,
|
2017-11-04 19:04:05 +00:00
|
|
|
ORIGIN: HTTP_BASE_URL
|
2016-11-25 21:04:06 +00:00
|
|
|
}
|
|
|
|
req = requests.get(_url(const.URL_API), headers=headers)
|
|
|
|
|
2017-11-04 19:04:05 +00:00
|
|
|
allow_origin = ACCESS_CONTROL_ALLOW_ORIGIN
|
2016-11-25 21:04:06 +00:00
|
|
|
|
|
|
|
assert req.status_code == 200
|
|
|
|
assert req.headers.get(allow_origin) == HTTP_BASE_URL
|
|
|
|
|
|
|
|
def test_cors_denied_without_origin_header(self):
|
|
|
|
"""Test cross origin resource sharing with password in header."""
|
|
|
|
headers = {
|
|
|
|
const.HTTP_HEADER_HA_AUTH: API_PASSWORD
|
|
|
|
}
|
|
|
|
req = requests.get(_url(const.URL_API), headers=headers)
|
|
|
|
|
2017-11-04 19:04:05 +00:00
|
|
|
allow_origin = ACCESS_CONTROL_ALLOW_ORIGIN
|
|
|
|
allow_headers = ACCESS_CONTROL_ALLOW_HEADERS
|
2016-11-25 21:04:06 +00:00
|
|
|
|
|
|
|
assert req.status_code == 200
|
|
|
|
assert allow_origin not in req.headers
|
|
|
|
assert allow_headers not in req.headers
|
|
|
|
|
|
|
|
def test_cors_preflight_allowed(self):
|
|
|
|
"""Test cross origin resource sharing preflight (OPTIONS) request."""
|
|
|
|
headers = {
|
2017-11-04 19:04:05 +00:00
|
|
|
ORIGIN: HTTP_BASE_URL,
|
|
|
|
ACCESS_CONTROL_REQUEST_METHOD: 'GET',
|
|
|
|
ACCESS_CONTROL_REQUEST_HEADERS: 'x-ha-access'
|
2016-11-25 21:04:06 +00:00
|
|
|
}
|
|
|
|
req = requests.options(_url(const.URL_API), headers=headers)
|
|
|
|
|
2017-11-04 19:04:05 +00:00
|
|
|
allow_origin = ACCESS_CONTROL_ALLOW_ORIGIN
|
|
|
|
allow_headers = ACCESS_CONTROL_ALLOW_HEADERS
|
2016-11-25 21:04:06 +00:00
|
|
|
|
|
|
|
assert req.status_code == 200
|
|
|
|
assert req.headers.get(allow_origin) == HTTP_BASE_URL
|
|
|
|
assert req.headers.get(allow_headers) == \
|
|
|
|
const.HTTP_HEADER_HA_AUTH.upper()
|
2016-11-27 22:01:12 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TestView(http.HomeAssistantView):
|
2016-12-28 18:04:59 +00:00
|
|
|
"""Test the HTTP views."""
|
2016-11-27 22:01:12 +00:00
|
|
|
|
|
|
|
name = 'test'
|
|
|
|
url = '/hello'
|
|
|
|
|
|
|
|
@asyncio.coroutine
|
|
|
|
def get(self, request):
|
|
|
|
"""Return a get request."""
|
|
|
|
return 'hello'
|
|
|
|
|
|
|
|
|
|
|
|
@asyncio.coroutine
|
|
|
|
def test_registering_view_while_running(hass, test_client):
|
|
|
|
"""Test that we can register a view while the server is running."""
|
2017-03-05 09:41:54 +00:00
|
|
|
yield from setup.async_setup_component(
|
2016-11-27 22:01:12 +00:00
|
|
|
hass, http.DOMAIN, {
|
|
|
|
http.DOMAIN: {
|
|
|
|
http.CONF_SERVER_PORT: get_test_instance_port(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
yield from hass.async_start()
|
2017-11-06 02:42:31 +00:00
|
|
|
# This raises a RuntimeError if app is frozen
|
2016-11-27 22:01:12 +00:00
|
|
|
hass.http.register_view(TestView)
|
|
|
|
|
2016-12-18 20:56:07 +00:00
|
|
|
|
2017-03-01 04:33:19 +00:00
|
|
|
@asyncio.coroutine
|
|
|
|
def test_api_base_url_with_domain(hass):
|
2017-11-04 19:04:05 +00:00
|
|
|
"""Test setting API URL."""
|
2017-03-05 09:41:54 +00:00
|
|
|
result = yield from setup.async_setup_component(hass, 'http', {
|
2017-03-01 04:33:19 +00:00
|
|
|
'http': {
|
|
|
|
'base_url': 'example.com'
|
|
|
|
}
|
|
|
|
})
|
|
|
|
assert result
|
2016-12-18 22:59:45 +00:00
|
|
|
assert hass.config.api.base_url == 'http://example.com'
|
2016-12-18 20:56:07 +00:00
|
|
|
|
|
|
|
|
2017-03-01 04:33:19 +00:00
|
|
|
@asyncio.coroutine
|
|
|
|
def test_api_base_url_with_ip(hass):
|
|
|
|
"""Test setting api url."""
|
2017-03-05 09:41:54 +00:00
|
|
|
result = yield from setup.async_setup_component(hass, 'http', {
|
2017-03-01 04:33:19 +00:00
|
|
|
'http': {
|
|
|
|
'server_host': '1.1.1.1'
|
|
|
|
}
|
|
|
|
})
|
|
|
|
assert result
|
2016-12-18 20:56:07 +00:00
|
|
|
assert hass.config.api.base_url == 'http://1.1.1.1:8123'
|
|
|
|
|
|
|
|
|
2017-03-01 04:33:19 +00:00
|
|
|
@asyncio.coroutine
|
|
|
|
def test_api_base_url_with_ip_port(hass):
|
|
|
|
"""Test setting api url."""
|
2017-03-05 09:41:54 +00:00
|
|
|
result = yield from setup.async_setup_component(hass, 'http', {
|
2017-03-01 04:33:19 +00:00
|
|
|
'http': {
|
|
|
|
'base_url': '1.1.1.1:8124'
|
|
|
|
}
|
|
|
|
})
|
|
|
|
assert result
|
|
|
|
assert hass.config.api.base_url == 'http://1.1.1.1:8124'
|
2016-12-18 20:56:07 +00:00
|
|
|
|
|
|
|
|
2017-03-01 04:33:19 +00:00
|
|
|
@asyncio.coroutine
|
|
|
|
def test_api_no_base_url(hass):
|
|
|
|
"""Test setting api url."""
|
2017-03-05 09:41:54 +00:00
|
|
|
result = yield from setup.async_setup_component(hass, 'http', {
|
2017-03-01 04:33:19 +00:00
|
|
|
'http': {
|
|
|
|
}
|
|
|
|
})
|
|
|
|
assert result
|
2016-12-18 20:56:07 +00:00
|
|
|
assert hass.config.api.base_url == 'http://127.0.0.1:8123'
|