2016-11-25 21:04:06 +00:00
|
|
|
"""The tests for the Home Assistant HTTP component."""
|
|
|
|
# pylint: disable=protected-access
|
|
|
|
from unittest.mock import patch, mock_open
|
|
|
|
|
2018-02-15 21:06:14 +00:00
|
|
|
from aiohttp import web
|
|
|
|
from aiohttp.web_exceptions import HTTPUnauthorized
|
2016-11-25 21:04:06 +00:00
|
|
|
|
2017-05-19 14:37:39 +00:00
|
|
|
from homeassistant.setup import async_setup_component
|
2016-11-25 21:04:06 +00:00
|
|
|
import homeassistant.components.http as http
|
2018-02-15 21:06:14 +00:00
|
|
|
from homeassistant.components.http.ban import (
|
|
|
|
IpBan, IP_BANS_FILE, setup_bans, KEY_BANNED_IPS)
|
2016-11-25 21:04:06 +00:00
|
|
|
|
2018-02-15 21:06:14 +00:00
|
|
|
from . import mock_real_ip
|
2016-11-25 21:04:06 +00:00
|
|
|
|
2018-02-15 21:06:14 +00:00
|
|
|
BANNED_IPS = ['200.201.202.203', '100.64.0.2']
|
2017-05-19 14:37:39 +00:00
|
|
|
|
|
|
|
|
2018-03-09 01:51:49 +00:00
|
|
|
async def test_access_from_banned_ip(hass, test_client):
|
2017-05-19 14:37:39 +00:00
|
|
|
"""Test accessing to server from banned IP. Both trusted and not."""
|
2018-02-15 21:06:14 +00:00
|
|
|
app = web.Application()
|
|
|
|
setup_bans(hass, app, 5)
|
|
|
|
set_real_ip = mock_real_ip(app)
|
|
|
|
|
|
|
|
with patch('homeassistant.components.http.ban.load_ip_bans_config',
|
|
|
|
return_value=[IpBan(banned_ip) for banned_ip
|
|
|
|
in BANNED_IPS]):
|
2018-03-09 01:51:49 +00:00
|
|
|
client = await test_client(app)
|
2018-02-15 21:06:14 +00:00
|
|
|
|
2017-05-19 14:37:39 +00:00
|
|
|
for remote_addr in BANNED_IPS:
|
2018-02-15 21:06:14 +00:00
|
|
|
set_real_ip(remote_addr)
|
2018-03-09 01:51:49 +00:00
|
|
|
resp = await client.get('/')
|
2018-02-15 21:06:14 +00:00
|
|
|
assert resp.status == 403
|
2017-05-19 14:37:39 +00:00
|
|
|
|
|
|
|
|
2018-03-09 01:51:49 +00:00
|
|
|
async def test_ban_middleware_not_loaded_by_config(hass):
|
2017-05-19 14:37:39 +00:00
|
|
|
"""Test accessing to server from banned IP when feature is off."""
|
2018-02-15 21:06:14 +00:00
|
|
|
with patch('homeassistant.components.http.setup_bans') as mock_setup:
|
2018-03-09 01:51:49 +00:00
|
|
|
await async_setup_component(hass, 'http', {
|
2018-02-15 21:06:14 +00:00
|
|
|
'http': {
|
|
|
|
http.CONF_IP_BAN_ENABLED: False,
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
assert len(mock_setup.mock_calls) == 0
|
2017-05-19 14:37:39 +00:00
|
|
|
|
|
|
|
|
2018-03-09 01:51:49 +00:00
|
|
|
async def test_ban_middleware_loaded_by_default(hass):
|
2018-02-15 21:06:14 +00:00
|
|
|
"""Test accessing to server from banned IP when feature is off."""
|
|
|
|
with patch('homeassistant.components.http.setup_bans') as mock_setup:
|
2018-03-09 01:51:49 +00:00
|
|
|
await async_setup_component(hass, 'http', {
|
2018-02-15 21:06:14 +00:00
|
|
|
'http': {}
|
|
|
|
})
|
2017-05-19 14:37:39 +00:00
|
|
|
|
2018-02-15 21:06:14 +00:00
|
|
|
assert len(mock_setup.mock_calls) == 1
|
|
|
|
|
|
|
|
|
2018-03-09 01:51:49 +00:00
|
|
|
async def test_ip_bans_file_creation(hass, test_client):
|
2018-02-15 21:06:14 +00:00
|
|
|
"""Testing if banned IP file created."""
|
|
|
|
app = web.Application()
|
|
|
|
app['hass'] = hass
|
2017-05-19 14:37:39 +00:00
|
|
|
|
2018-03-09 01:51:49 +00:00
|
|
|
async def unauth_handler(request):
|
2018-02-15 21:06:14 +00:00
|
|
|
"""Return a mock web response."""
|
|
|
|
raise HTTPUnauthorized
|
|
|
|
|
|
|
|
app.router.add_get('/', unauth_handler)
|
|
|
|
setup_bans(hass, app, 1)
|
|
|
|
mock_real_ip(app)("200.201.202.204")
|
|
|
|
|
|
|
|
with patch('homeassistant.components.http.ban.load_ip_bans_config',
|
|
|
|
return_value=[IpBan(banned_ip) for banned_ip
|
|
|
|
in BANNED_IPS]):
|
2018-03-09 01:51:49 +00:00
|
|
|
client = await test_client(app)
|
2018-02-15 21:06:14 +00:00
|
|
|
|
|
|
|
m = mock_open()
|
2017-05-19 14:37:39 +00:00
|
|
|
|
|
|
|
with patch('homeassistant.components.http.ban.open', m, create=True):
|
2018-03-09 01:51:49 +00:00
|
|
|
resp = await client.get('/')
|
2017-05-19 14:37:39 +00:00
|
|
|
assert resp.status == 401
|
2018-02-15 21:06:14 +00:00
|
|
|
assert len(app[KEY_BANNED_IPS]) == len(BANNED_IPS)
|
2017-05-19 14:37:39 +00:00
|
|
|
assert m.call_count == 0
|
|
|
|
|
2018-03-09 01:51:49 +00:00
|
|
|
resp = await client.get('/')
|
2017-05-19 14:37:39 +00:00
|
|
|
assert resp.status == 401
|
2018-02-15 21:06:14 +00:00
|
|
|
assert len(app[KEY_BANNED_IPS]) == len(BANNED_IPS) + 1
|
2017-05-19 14:37:39 +00:00
|
|
|
m.assert_called_once_with(hass.config.path(IP_BANS_FILE), 'a')
|
|
|
|
|
2018-03-09 01:51:49 +00:00
|
|
|
resp = await client.get('/')
|
2017-05-19 14:37:39 +00:00
|
|
|
assert resp.status == 403
|
|
|
|
assert m.call_count == 1
|