hass-core/tests/components/http/test_init.py

127 lines
3.4 KiB
Python
Raw Normal View History

"""The tests for the Home Assistant HTTP component."""
import logging
from homeassistant.setup import async_setup_component
import homeassistant.components.http as http
class TestView(http.HomeAssistantView):
2016-12-28 20:04:59 +02:00
"""Test the HTTP views."""
name = 'test'
url = '/hello'
async def get(self, request):
"""Return a get request."""
return 'hello'
async def test_registering_view_while_running(hass, aiohttp_client,
aiohttp_unused_port):
"""Test that we can register a view while the server is running."""
await async_setup_component(
hass, http.DOMAIN, {
http.DOMAIN: {
http.CONF_SERVER_PORT: aiohttp_unused_port(),
}
}
)
await hass.async_start()
# This raises a RuntimeError if app is frozen
hass.http.register_view(TestView)
2016-12-18 12:56:07 -08:00
async def test_api_base_url_with_domain(hass):
"""Test setting API URL."""
result = await async_setup_component(hass, 'http', {
'http': {
'base_url': 'example.com'
}
})
assert result
assert hass.config.api.base_url == 'http://example.com'
2016-12-18 12:56:07 -08:00
async def test_api_base_url_with_ip(hass):
"""Test setting api url."""
result = await async_setup_component(hass, 'http', {
'http': {
'server_host': '1.1.1.1'
}
})
assert result
2016-12-18 12:56:07 -08:00
assert hass.config.api.base_url == 'http://1.1.1.1:8123'
async def test_api_base_url_with_ip_port(hass):
"""Test setting api url."""
result = await async_setup_component(hass, 'http', {
'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 12:56:07 -08:00
async def test_api_no_base_url(hass):
"""Test setting api url."""
result = await async_setup_component(hass, 'http', {
'http': {
}
})
assert result
2016-12-18 12:56:07 -08:00
assert hass.config.api.base_url == 'http://127.0.0.1:8123'
async def test_not_log_password(hass, aiohttp_client, caplog):
"""Test access with password doesn't get logged."""
assert await async_setup_component(hass, 'api', {
'http': {
http.CONF_API_PASSWORD: 'some-pass'
}
})
client = await aiohttp_client(hass.http.app)
logging.getLogger('aiohttp.access').setLevel(logging.INFO)
resp = await client.get('/api/', params={
'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
async def test_proxy_config(hass):
"""Test use_x_forwarded_for must config together with trusted_proxies."""
assert await async_setup_component(hass, 'http', {
'http': {
http.CONF_USE_X_FORWARDED_FOR: True,
http.CONF_TRUSTED_PROXIES: ['127.0.0.1']
}
}) is True
async def test_proxy_config_only_use_xff(hass):
"""Test use_x_forwarded_for must config together with trusted_proxies."""
assert await async_setup_component(hass, 'http', {
'http': {
http.CONF_USE_X_FORWARDED_FOR: True
}
}) is not True
async def test_proxy_config_only_trust_proxies(hass):
"""Test use_x_forwarded_for must config together with trusted_proxies."""
assert await async_setup_component(hass, 'http', {
'http': {
http.CONF_TRUSTED_PROXIES: ['127.0.0.1']
}
}) is not True