Move HomeAssistantView to separate file. Convert http to async syntax. [skip ci] (#12982)

* Move HomeAssistantView to separate file. Convert http to async syntax.

* pylint

* websocket api

* update emulated_hue for async/await

* Lint
This commit is contained in:
Boyi C authored and Paulus Schoutsen committed 2018-03-08 17:51:49 -08:00
1 parent 2ee73ca911
commit 321eb2ec6f
17 files changed
+292 -344

No files matched your search

+13 -19
View File
@@ -1,6 +1,5 @@
"""The tests for the Home Assistant HTTP component."""
# pylint: disable=protected-access
import asyncio
from unittest.mock import patch, mock_open
from aiohttp import web
@@ -16,8 +15,7 @@ from . import mock_real_ip
BANNED_IPS = ['200.201.202.203', '100.64.0.2']
@asyncio.coroutine
def test_access_from_banned_ip(hass, test_client):
async def test_access_from_banned_ip(hass, test_client):
"""Test accessing to server from banned IP. Both trusted and not."""
app = web.Application()
setup_bans(hass, app, 5)
@@ -26,19 +24,18 @@ def test_access_from_banned_ip(hass, test_client):
with patch('homeassistant.components.http.ban.load_ip_bans_config',
return_value=[IpBan(banned_ip) for banned_ip
in BANNED_IPS]):
client = yield from test_client(app)
client = await test_client(app)
for remote_addr in BANNED_IPS:
set_real_ip(remote_addr)
resp = yield from client.get('/')
resp = await client.get('/')
assert resp.status == 403
@asyncio.coroutine
def test_ban_middleware_not_loaded_by_config(hass):
async def test_ban_middleware_not_loaded_by_config(hass):
"""Test accessing to server from banned IP when feature is off."""
with patch('homeassistant.components.http.setup_bans') as mock_setup:
yield from async_setup_component(hass, 'http', {
await async_setup_component(hass, 'http', {
'http': {
http.CONF_IP_BAN_ENABLED: False,
}
@@ -47,25 +44,22 @@ def test_ban_middleware_not_loaded_by_config(hass):
assert len(mock_setup.mock_calls) == 0
@asyncio.coroutine
def test_ban_middleware_loaded_by_default(hass):
async def test_ban_middleware_loaded_by_default(hass):
"""Test accessing to server from banned IP when feature is off."""
with patch('homeassistant.components.http.setup_bans') as mock_setup:
yield from async_setup_component(hass, 'http', {
await async_setup_component(hass, 'http', {
'http': {}
})
assert len(mock_setup.mock_calls) == 1
@asyncio.coroutine
def test_ip_bans_file_creation(hass, test_client):
async def test_ip_bans_file_creation(hass, test_client):
"""Testing if banned IP file created."""
app = web.Application()
app['hass'] = hass
@asyncio.coroutine
def unauth_handler(request):
async def unauth_handler(request):
"""Return a mock web response."""
raise HTTPUnauthorized
@@ -76,21 +70,21 @@ def test_ip_bans_file_creation(hass, test_client):
with patch('homeassistant.components.http.ban.load_ip_bans_config',
return_value=[IpBan(banned_ip) for banned_ip
in BANNED_IPS]):
client = yield from test_client(app)
client = await test_client(app)
m = mock_open()
with patch('homeassistant.components.http.ban.open', m, create=True):
resp = yield from client.get('/')
resp = await client.get('/')
assert resp.status == 401
assert len(app[KEY_BANNED_IPS]) == len(BANNED_IPS)
assert m.call_count == 0
resp = yield from client.get('/')
resp = await client.get('/')
assert resp.status == 401
assert len(app[KEY_BANNED_IPS]) == len(BANNED_IPS) + 1
m.assert_called_once_with(hass.config.path(IP_BANS_FILE), 'a')
resp = yield from client.get('/')
resp = await client.get('/')
assert resp.status == 403
assert m.call_count == 1