2016-11-25 21:04:06 +00:00
|
|
|
"""The tests for the Home Assistant HTTP component."""
|
2018-02-15 21:06:14 +00:00
|
|
|
from homeassistant.setup import async_setup_component
|
2016-11-25 21:04:06 +00:00
|
|
|
|
2018-02-15 21:06:14 +00:00
|
|
|
import homeassistant.components.http as http
|
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'
|
|
|
|
|
2018-03-09 01:51:49 +00:00
|
|
|
async def get(self, request):
|
2016-11-27 22:01:12 +00:00
|
|
|
"""Return a get request."""
|
|
|
|
return 'hello'
|
|
|
|
|
|
|
|
|
2018-03-15 20:49:49 +00:00
|
|
|
async def test_registering_view_while_running(hass, aiohttp_client,
|
|
|
|
aiohttp_unused_port):
|
2016-11-27 22:01:12 +00:00
|
|
|
"""Test that we can register a view while the server is running."""
|
2018-03-09 01:51:49 +00:00
|
|
|
await async_setup_component(
|
2016-11-27 22:01:12 +00:00
|
|
|
hass, http.DOMAIN, {
|
|
|
|
http.DOMAIN: {
|
2018-03-15 20:49:49 +00:00
|
|
|
http.CONF_SERVER_PORT: aiohttp_unused_port(),
|
2016-11-27 22:01:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2018-03-09 01:51:49 +00:00
|
|
|
await 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
|
|
|
|
2018-03-09 01:51:49 +00:00
|
|
|
async def test_api_base_url_with_domain(hass):
|
2017-11-04 19:04:05 +00:00
|
|
|
"""Test setting API URL."""
|
2018-03-09 01:51:49 +00:00
|
|
|
result = await 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
|
|
|
|
|
|
|
|
2018-03-09 01:51:49 +00:00
|
|
|
async def test_api_base_url_with_ip(hass):
|
2017-03-01 04:33:19 +00:00
|
|
|
"""Test setting api url."""
|
2018-03-09 01:51:49 +00:00
|
|
|
result = await 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'
|
|
|
|
|
|
|
|
|
2018-03-09 01:51:49 +00:00
|
|
|
async def test_api_base_url_with_ip_port(hass):
|
2017-03-01 04:33:19 +00:00
|
|
|
"""Test setting api url."""
|
2018-03-09 01:51:49 +00:00
|
|
|
result = await 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
|
|
|
|
|
|
|
|
2018-03-09 01:51:49 +00:00
|
|
|
async def test_api_no_base_url(hass):
|
2017-03-01 04:33:19 +00:00
|
|
|
"""Test setting api url."""
|
2018-03-09 01:51:49 +00:00
|
|
|
result = await 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'
|
2018-02-15 21:06:14 +00:00
|
|
|
|
|
|
|
|
2018-03-15 20:49:49 +00:00
|
|
|
async def test_not_log_password(hass, aiohttp_client, caplog):
|
2018-02-15 21:06:14 +00:00
|
|
|
"""Test access with password doesn't get logged."""
|
2018-03-09 01:51:49 +00:00
|
|
|
result = await async_setup_component(hass, 'api', {
|
2018-02-15 21:06:14 +00:00
|
|
|
'http': {
|
|
|
|
http.CONF_API_PASSWORD: 'some-pass'
|
|
|
|
}
|
|
|
|
})
|
|
|
|
assert result
|
|
|
|
|
2018-03-15 20:49:49 +00:00
|
|
|
client = await aiohttp_client(hass.http.app)
|
2018-02-15 21:06:14 +00:00
|
|
|
|
2018-03-09 01:51:49 +00:00
|
|
|
resp = await client.get('/api/', params={
|
2018-02-15 21:06:14 +00:00
|
|
|
'api_password': 'some-pass'
|
|
|
|
})
|
|
|
|
|
|
|
|
assert resp.status == 200
|
|
|
|
logs = caplog.text
|
|
|
|
|
|
|
|
# Ensure we don't log API passwords
|
|
|
|
assert '/api/' in logs
|
|
|
|
assert 'some-pass' not in logs
|