Cleanup http (#12424)
* Clean up HTTP component * Clean up HTTP mock * Remove unused import * Fix test * Lint
This commit is contained in:
1 parent
ad8fe8a93a
commit
f32911d036
28 files changed
+811
-1014
No files matched your search
+113
-152
@@ -1,195 +1,156 @@
|
||||
"""The tests for the Home Assistant HTTP component."""
|
||||
# pylint: disable=protected-access
|
||||
import asyncio
|
||||
from ipaddress import ip_address, ip_network
|
||||
from ipaddress import ip_network
|
||||
from unittest.mock import patch
|
||||
|
||||
import aiohttp
|
||||
from aiohttp import BasicAuth, web
|
||||
from aiohttp.web_exceptions import HTTPUnauthorized
|
||||
import pytest
|
||||
|
||||
from homeassistant import const
|
||||
from homeassistant.const import HTTP_HEADER_HA_AUTH
|
||||
from homeassistant.setup import async_setup_component
|
||||
import homeassistant.components.http as http
|
||||
from homeassistant.components.http.const import (
|
||||
KEY_TRUSTED_NETWORKS, KEY_USE_X_FORWARDED_FOR, HTTP_HEADER_X_FORWARDED_FOR)
|
||||
from homeassistant.components.http.auth import setup_auth
|
||||
from homeassistant.components.http.real_ip import setup_real_ip
|
||||
from homeassistant.components.http.const import KEY_AUTHENTICATED
|
||||
|
||||
from . import mock_real_ip
|
||||
|
||||
API_PASSWORD = 'test1234'
|
||||
|
||||
# Don't add 127.0.0.1/::1 as trusted, as it may interfere with other test cases
|
||||
TRUSTED_NETWORKS = ['192.0.2.0/24', '2001:DB8:ABCD::/48', '100.64.0.1',
|
||||
'FD01:DB8::1']
|
||||
TRUSTED_NETWORKS = [
|
||||
ip_network('192.0.2.0/24'),
|
||||
ip_network('2001:DB8:ABCD::/48'),
|
||||
ip_network('100.64.0.1'),
|
||||
ip_network('FD01:DB8::1'),
|
||||
]
|
||||
TRUSTED_ADDRESSES = ['100.64.0.1', '192.0.2.100', 'FD01:DB8::1',
|
||||
'2001:DB8:ABCD::1']
|
||||
UNTRUSTED_ADDRESSES = ['198.51.100.1', '2001:DB8:FA1::1', '127.0.0.1', '::1']
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_api_client(hass, test_client):
|
||||
"""Start the Hass HTTP component."""
|
||||
hass.loop.run_until_complete(async_setup_component(hass, 'api', {
|
||||
'http': {
|
||||
http.CONF_API_PASSWORD: API_PASSWORD,
|
||||
}
|
||||
}))
|
||||
return hass.loop.run_until_complete(test_client(hass.http.app))
|
||||
@asyncio.coroutine
|
||||
def mock_handler(request):
|
||||
"""Return if request was authenticated."""
|
||||
if not request[KEY_AUTHENTICATED]:
|
||||
raise HTTPUnauthorized
|
||||
return web.Response(status=200)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_trusted_networks(hass, mock_api_client):
|
||||
"""Mock trusted networks."""
|
||||
hass.http.app[KEY_TRUSTED_NETWORKS] = [
|
||||
ip_network(trusted_network)
|
||||
for trusted_network in TRUSTED_NETWORKS]
|
||||
def app():
|
||||
"""Fixture to setup a web.Application."""
|
||||
app = web.Application()
|
||||
app.router.add_get('/', mock_handler)
|
||||
setup_real_ip(app, False)
|
||||
return app
|
||||
|
||||
|
||||
@asyncio.coroutine
|
||||
def test_access_denied_without_password(mock_api_client):
|
||||
def test_auth_middleware_loaded_by_default(hass):
|
||||
"""Test accessing to server from banned IP when feature is off."""
|
||||
with patch('homeassistant.components.http.setup_auth') as mock_setup:
|
||||
yield from async_setup_component(hass, 'http', {
|
||||
'http': {}
|
||||
})
|
||||
|
||||
assert len(mock_setup.mock_calls) == 1
|
||||
|
||||
|
||||
@asyncio.coroutine
|
||||
def test_access_without_password(app, test_client):
|
||||
"""Test access without password."""
|
||||
resp = yield from mock_api_client.get(const.URL_API)
|
||||
setup_auth(app, [], None)
|
||||
client = yield from test_client(app)
|
||||
|
||||
resp = yield from client.get('/')
|
||||
assert resp.status == 200
|
||||
|
||||
|
||||
@asyncio.coroutine
|
||||
def test_access_with_password_in_header(app, test_client):
|
||||
"""Test access with password in URL."""
|
||||
setup_auth(app, [], API_PASSWORD)
|
||||
client = yield from test_client(app)
|
||||
|
||||
req = yield from client.get(
|
||||
'/', headers={HTTP_HEADER_HA_AUTH: API_PASSWORD})
|
||||
assert req.status == 200
|
||||
|
||||
req = yield from client.get(
|
||||
'/', headers={HTTP_HEADER_HA_AUTH: 'wrong-pass'})
|
||||
assert req.status == 401
|
||||
|
||||
|
||||
@asyncio.coroutine
|
||||
def test_access_with_password_in_query(app, test_client):
|
||||
"""Test access without password."""
|
||||
setup_auth(app, [], API_PASSWORD)
|
||||
client = yield from test_client(app)
|
||||
|
||||
resp = yield from client.get('/', params={
|
||||
'api_password': API_PASSWORD
|
||||
})
|
||||
assert resp.status == 200
|
||||
|
||||
resp = yield from client.get('/')
|
||||
assert resp.status == 401
|
||||
|
||||
|
||||
@asyncio.coroutine
|
||||
def test_access_denied_with_wrong_password_in_header(mock_api_client):
|
||||
"""Test access with wrong password."""
|
||||
resp = yield from mock_api_client.get(const.URL_API, headers={
|
||||
const.HTTP_HEADER_HA_AUTH: 'wrongpassword'
|
||||
resp = yield from client.get('/', params={
|
||||
'api_password': 'wrong-password'
|
||||
})
|
||||
assert resp.status == 401
|
||||
|
||||
|
||||
@asyncio.coroutine
|
||||
def test_access_denied_with_x_forwarded_for(hass, mock_api_client,
|
||||
mock_trusted_networks):
|
||||
"""Test access denied through the X-Forwarded-For http header."""
|
||||
hass.http.use_x_forwarded_for = True
|
||||
for remote_addr in UNTRUSTED_ADDRESSES:
|
||||
resp = yield from mock_api_client.get(const.URL_API, headers={
|
||||
HTTP_HEADER_X_FORWARDED_FOR: remote_addr})
|
||||
|
||||
assert resp.status == 401, \
|
||||
"{} shouldn't be trusted".format(remote_addr)
|
||||
|
||||
|
||||
@asyncio.coroutine
|
||||
def test_access_denied_with_untrusted_ip(mock_api_client,
|
||||
mock_trusted_networks):
|
||||
"""Test access with an untrusted ip address."""
|
||||
for remote_addr in UNTRUSTED_ADDRESSES:
|
||||
with patch('homeassistant.components.http.'
|
||||
'util.get_real_ip',
|
||||
return_value=ip_address(remote_addr)):
|
||||
resp = yield from mock_api_client.get(
|
||||
const.URL_API, params={'api_password': ''})
|
||||
|
||||
assert resp.status == 401, \
|
||||
"{} shouldn't be trusted".format(remote_addr)
|
||||
|
||||
|
||||
@asyncio.coroutine
|
||||
def test_access_with_password_in_header(mock_api_client, caplog):
|
||||
"""Test access with password in URL."""
|
||||
# Hide logging from requests package that we use to test logging
|
||||
req = yield from mock_api_client.get(
|
||||
const.URL_API, headers={const.HTTP_HEADER_HA_AUTH: API_PASSWORD})
|
||||
|
||||
assert req.status == 200
|
||||
|
||||
logs = caplog.text
|
||||
|
||||
assert const.URL_API in logs
|
||||
assert API_PASSWORD not in logs
|
||||
|
||||
|
||||
@asyncio.coroutine
|
||||
def test_access_denied_with_wrong_password_in_url(mock_api_client):
|
||||
"""Test access with wrong password."""
|
||||
resp = yield from mock_api_client.get(
|
||||
const.URL_API, params={'api_password': 'wrongpassword'})
|
||||
|
||||
assert resp.status == 401
|
||||
|
||||
|
||||
@asyncio.coroutine
|
||||
def test_access_with_password_in_url(mock_api_client, caplog):
|
||||
"""Test access with password in URL."""
|
||||
req = yield from mock_api_client.get(
|
||||
const.URL_API, params={'api_password': API_PASSWORD})
|
||||
|
||||
assert req.status == 200
|
||||
|
||||
logs = caplog.text
|
||||
|
||||
assert const.URL_API in logs
|
||||
assert API_PASSWORD not in logs
|
||||
|
||||
|
||||
@asyncio.coroutine
|
||||
def test_access_granted_with_x_forwarded_for(hass, mock_api_client, caplog,
|
||||
mock_trusted_networks):
|
||||
"""Test access denied through the X-Forwarded-For http header."""
|
||||
hass.http.app[KEY_USE_X_FORWARDED_FOR] = True
|
||||
for remote_addr in TRUSTED_ADDRESSES:
|
||||
resp = yield from mock_api_client.get(const.URL_API, headers={
|
||||
HTTP_HEADER_X_FORWARDED_FOR: remote_addr})
|
||||
|
||||
assert resp.status == 200, \
|
||||
"{} should be trusted".format(remote_addr)
|
||||
|
||||
|
||||
@asyncio.coroutine
|
||||
def test_access_granted_with_trusted_ip(mock_api_client, caplog,
|
||||
mock_trusted_networks):
|
||||
"""Test access with trusted addresses."""
|
||||
for remote_addr in TRUSTED_ADDRESSES:
|
||||
with patch('homeassistant.components.http.'
|
||||
'auth.get_real_ip',
|
||||
return_value=ip_address(remote_addr)):
|
||||
resp = yield from mock_api_client.get(
|
||||
const.URL_API, params={'api_password': ''})
|
||||
|
||||
assert resp.status == 200, \
|
||||
'{} should be trusted'.format(remote_addr)
|
||||
|
||||
|
||||
@asyncio.coroutine
|
||||
def test_basic_auth_works(mock_api_client, caplog):
|
||||
def test_basic_auth_works(app, test_client):
|
||||
"""Test access with basic authentication."""
|
||||
req = yield from mock_api_client.get(
|
||||
const.URL_API,
|
||||
auth=aiohttp.BasicAuth('homeassistant', API_PASSWORD))
|
||||
setup_auth(app, [], API_PASSWORD)
|
||||
client = yield from test_client(app)
|
||||
|
||||
req = yield from client.get(
|
||||
'/',
|
||||
auth=BasicAuth('homeassistant', API_PASSWORD))
|
||||
assert req.status == 200
|
||||
assert const.URL_API in caplog.text
|
||||
|
||||
|
||||
@asyncio.coroutine
|
||||
def test_basic_auth_username_homeassistant(mock_api_client, caplog):
|
||||
"""Test access with basic auth requires username homeassistant."""
|
||||
req = yield from mock_api_client.get(
|
||||
const.URL_API,
|
||||
auth=aiohttp.BasicAuth('wrong_username', API_PASSWORD))
|
||||
|
||||
req = yield from client.get(
|
||||
'/',
|
||||
auth=BasicAuth('wrong_username', API_PASSWORD))
|
||||
assert req.status == 401
|
||||
|
||||
|
||||
@asyncio.coroutine
|
||||
def test_basic_auth_wrong_password(mock_api_client, caplog):
|
||||
"""Test access with basic auth not allowed with wrong password."""
|
||||
req = yield from mock_api_client.get(
|
||||
const.URL_API,
|
||||
auth=aiohttp.BasicAuth('homeassistant', 'wrong password'))
|
||||
|
||||
req = yield from client.get(
|
||||
'/',
|
||||
auth=BasicAuth('homeassistant', 'wrong password'))
|
||||
assert req.status == 401
|
||||
|
||||
|
||||
@asyncio.coroutine
|
||||
def test_authorization_header_must_be_basic_type(mock_api_client, caplog):
|
||||
"""Test only basic authorization is allowed for auth header."""
|
||||
req = yield from mock_api_client.get(
|
||||
const.URL_API,
|
||||
req = yield from client.get(
|
||||
'/',
|
||||
headers={
|
||||
'authorization': 'NotBasic abcdefg'
|
||||
})
|
||||
|
||||
assert req.status == 401
|
||||
|
||||
|
||||
@asyncio.coroutine
|
||||
def test_access_with_trusted_ip(test_client):
|
||||
"""Test access with an untrusted ip address."""
|
||||
app = web.Application()
|
||||
app.router.add_get('/', mock_handler)
|
||||
|
||||
setup_auth(app, TRUSTED_NETWORKS, 'some-pass')
|
||||
|
||||
set_mock_ip = mock_real_ip(app)
|
||||
client = yield from test_client(app)
|
||||
|
||||
for remote_addr in UNTRUSTED_ADDRESSES:
|
||||
set_mock_ip(remote_addr)
|
||||
resp = yield from client.get('/')
|
||||
assert resp.status == 401, \
|
||||
"{} shouldn't be trusted".format(remote_addr)
|
||||
|
||||
for remote_addr in TRUSTED_ADDRESSES:
|
||||
set_mock_ip(remote_addr)
|
||||
resp = yield from client.get('/')
|
||||
assert resp.status == 200, \
|
||||
"{} should be trusted".format(remote_addr)
|
||||
Reference in new issue
Block a user