Hass.io ingress (#22505)

* Fix API stream of snapshot / Add ingress

* fix lint

* Fix stream handling

* Cleanup api handling

* fix typing

* Set proxy header

* Use header constant

* Enable the ingress setup

* fix lint

* Fix name

* Fix tests

* fix lint

* forward params

* Add tests for ingress

* Cleanup cookie handling with aiohttp 3.5

* Add more tests

* Fix tests

* Fix lint

* Fix header handling for steam

* forward header too

* fix lint

* fix flake
This commit is contained in:
Pascal Vizeli authored and GitHub committed 2019-04-01 14:16:16 +02:00
1 parent 42e3e878df
commit 6829ecad9d
8 files changed
+458 -88

No files matched your search

+27 -53
View File
@@ -1,29 +1,22 @@
"""The tests for the hassio component."""
import asyncio
from unittest.mock import patch, Mock, MagicMock
from unittest.mock import patch
import pytest
from homeassistant.const import HTTP_HEADER_HA_AUTH
from tests.common import mock_coro
from . import API_PASSWORD
@asyncio.coroutine
def test_forward_request(hassio_client):
def test_forward_request(hassio_client, aioclient_mock):
"""Test fetching normal path."""
response = MagicMock()
response.read.return_value = mock_coro('data')
aioclient_mock.post("http://127.0.0.1/beer", text="response")
with patch('homeassistant.components.hassio.HassIOView._command_proxy',
Mock(return_value=mock_coro(response))), \
patch('homeassistant.components.hassio.http'
'._create_response') as mresp:
mresp.return_value = 'response'
resp = yield from hassio_client.post('/api/hassio/beer', headers={
HTTP_HEADER_HA_AUTH: API_PASSWORD
})
resp = yield from hassio_client.post('/api/hassio/beer', headers={
HTTP_HEADER_HA_AUTH: API_PASSWORD
})
# Check we got right response
assert resp.status == 200
@@ -31,8 +24,7 @@ def test_forward_request(hassio_client):
assert body == 'response'
# Check we forwarded command
assert len(mresp.mock_calls) == 1
assert mresp.mock_calls[0][1] == (response, 'data')
assert len(aioclient_mock.mock_calls) == 1
@asyncio.coroutine
@@ -55,18 +47,13 @@ def test_auth_required_forward_request(hassio_noauth_client, build_type):
'app/index.html', 'app/hassio-app.html', 'app/index.html',
'app/hassio-app.html', 'app/some-chunk.js', 'app/app.js',
])
def test_forward_request_no_auth_for_panel(hassio_client, build_type):
def test_forward_request_no_auth_for_panel(
hassio_client, build_type, aioclient_mock):
"""Test no auth needed for ."""
response = MagicMock()
response.read.return_value = mock_coro('data')
aioclient_mock.get(
"http://127.0.0.1/{}".format(build_type), text="response")
with patch('homeassistant.components.hassio.HassIOView._command_proxy',
Mock(return_value=mock_coro(response))), \
patch('homeassistant.components.hassio.http.'
'_create_response') as mresp:
mresp.return_value = 'response'
resp = yield from hassio_client.get(
'/api/hassio/{}'.format(build_type))
resp = yield from hassio_client.get('/api/hassio/{}'.format(build_type))
# Check we got right response
assert resp.status == 200
@@ -74,22 +61,16 @@ def test_forward_request_no_auth_for_panel(hassio_client, build_type):
assert body == 'response'
# Check we forwarded command
assert len(mresp.mock_calls) == 1
assert mresp.mock_calls[0][1] == (response, 'data')
assert len(aioclient_mock.mock_calls) == 1
@asyncio.coroutine
def test_forward_request_no_auth_for_logo(hassio_client):
def test_forward_request_no_auth_for_logo(hassio_client, aioclient_mock):
"""Test no auth needed for ."""
response = MagicMock()
response.read.return_value = mock_coro('data')
aioclient_mock.get(
"http://127.0.0.1/addons/bl_b392/logo", text="response")
with patch('homeassistant.components.hassio.HassIOView._command_proxy',
Mock(return_value=mock_coro(response))), \
patch('homeassistant.components.hassio.http.'
'_create_response') as mresp:
mresp.return_value = 'response'
resp = yield from hassio_client.get('/api/hassio/addons/bl_b392/logo')
resp = yield from hassio_client.get('/api/hassio/addons/bl_b392/logo')
# Check we got right response
assert resp.status == 200
@@ -97,24 +78,18 @@ def test_forward_request_no_auth_for_logo(hassio_client):
assert body == 'response'
# Check we forwarded command
assert len(mresp.mock_calls) == 1
assert mresp.mock_calls[0][1] == (response, 'data')
assert len(aioclient_mock.mock_calls) == 1
@asyncio.coroutine
def test_forward_log_request(hassio_client):
def test_forward_log_request(hassio_client, aioclient_mock):
"""Test fetching normal log path doesn't remove ANSI color escape codes."""
response = MagicMock()
response.read.return_value = mock_coro('data')
aioclient_mock.get(
"http://127.0.0.1/beer/logs", text="\033[32mresponse\033[0m")
with patch('homeassistant.components.hassio.HassIOView._command_proxy',
Mock(return_value=mock_coro(response))), \
patch('homeassistant.components.hassio.http.'
'_create_response') as mresp:
mresp.return_value = '\033[32mresponse\033[0m'
resp = yield from hassio_client.get('/api/hassio/beer/logs', headers={
HTTP_HEADER_HA_AUTH: API_PASSWORD
})
resp = yield from hassio_client.get('/api/hassio/beer/logs', headers={
HTTP_HEADER_HA_AUTH: API_PASSWORD
})
# Check we got right response
assert resp.status == 200
@@ -122,8 +97,7 @@ def test_forward_log_request(hassio_client):
assert body == '\033[32mresponse\033[0m'
# Check we forwarded command
assert len(mresp.mock_calls) == 1
assert mresp.mock_calls[0][1] == (response, 'data')
assert len(aioclient_mock.mock_calls) == 1
@asyncio.coroutine
@@ -151,5 +125,5 @@ async def test_forwarding_user_info(hassio_client, hass_admin_user,
assert len(aioclient_mock.mock_calls) == 1
req_headers = aioclient_mock.mock_calls[0][-1]
req_headers['X-HASS-USER-ID'] == hass_admin_user.id
req_headers['X-HASS-IS-ADMIN'] == '1'
req_headers['X-Hass-User-ID'] == hass_admin_user.id
req_headers['X-Hass-Is-Admin'] == '1'
+162
View File
@@ -0,0 +1,162 @@
"""The tests for the hassio component."""
from aiohttp.hdrs import X_FORWARDED_FOR, X_FORWARDED_HOST, X_FORWARDED_PROTO
from aiohttp.client_exceptions import WSServerHandshakeError
import pytest
@pytest.mark.parametrize(
'build_type', [
("a3_vl", "test/beer/ping?index=1"), ("core", "index.html"),
("local", "panel/config"), ("jk_921", "editor.php?idx=3&ping=5")
])
async def test_ingress_request_get(
hassio_client, build_type, aioclient_mock):
"""Test no auth needed for ."""
aioclient_mock.get("http://127.0.0.1/addons/{}/web/{}".format(
build_type[0], build_type[1]), text="test")
resp = await hassio_client.get(
'/api/hassio_ingress/{}/{}'.format(build_type[0], build_type[1]),
headers={"X-Test-Header": "beer"}
)
# Check we got right response
assert resp.status == 200
body = await resp.text()
assert body == "test"
# Check we forwarded command
assert len(aioclient_mock.mock_calls) == 1
assert aioclient_mock.mock_calls[-1][3]["X-Hassio-Key"] == "123456"
assert aioclient_mock.mock_calls[-1][3]["X-Ingress-Path"] == \
"/api/hassio_ingress/{}".format(build_type[0])
assert aioclient_mock.mock_calls[-1][3]["X-Test-Header"] == "beer"
assert aioclient_mock.mock_calls[-1][3][X_FORWARDED_FOR]
assert aioclient_mock.mock_calls[-1][3][X_FORWARDED_HOST]
assert aioclient_mock.mock_calls[-1][3][X_FORWARDED_PROTO]
@pytest.mark.parametrize(
'build_type', [
("a3_vl", "test/beer/ping?index=1"), ("core", "index.html"),
("local", "panel/config"), ("jk_921", "editor.php?idx=3&ping=5")
])
async def test_ingress_request_post(
hassio_client, build_type, aioclient_mock):
"""Test no auth needed for ."""
aioclient_mock.post("http://127.0.0.1/addons/{}/web/{}".format(
build_type[0], build_type[1]), text="test")
resp = await hassio_client.post(
'/api/hassio_ingress/{}/{}'.format(build_type[0], build_type[1]),
headers={"X-Test-Header": "beer"}
)
# Check we got right response
assert resp.status == 200
body = await resp.text()
assert body == "test"
# Check we forwarded command
assert len(aioclient_mock.mock_calls) == 1
assert aioclient_mock.mock_calls[-1][3]["X-Hassio-Key"] == "123456"
assert aioclient_mock.mock_calls[-1][3]["X-Ingress-Path"] == \
"/api/hassio_ingress/{}".format(build_type[0])
assert aioclient_mock.mock_calls[-1][3]["X-Test-Header"] == "beer"
assert aioclient_mock.mock_calls[-1][3][X_FORWARDED_FOR]
assert aioclient_mock.mock_calls[-1][3][X_FORWARDED_HOST]
assert aioclient_mock.mock_calls[-1][3][X_FORWARDED_PROTO]
@pytest.mark.parametrize(
'build_type', [
("a3_vl", "test/beer/ping?index=1"), ("core", "index.html"),
("local", "panel/config"), ("jk_921", "editor.php?idx=3&ping=5")
])
async def test_ingress_request_put(
hassio_client, build_type, aioclient_mock):
"""Test no auth needed for ."""
aioclient_mock.put("http://127.0.0.1/addons/{}/web/{}".format(
build_type[0], build_type[1]), text="test")
resp = await hassio_client.put(
'/api/hassio_ingress/{}/{}'.format(build_type[0], build_type[1]),
headers={"X-Test-Header": "beer"}
)
# Check we got right response
assert resp.status == 200
body = await resp.text()
assert body == "test"
# Check we forwarded command
assert len(aioclient_mock.mock_calls) == 1
assert aioclient_mock.mock_calls[-1][3]["X-Hassio-Key"] == "123456"
assert aioclient_mock.mock_calls[-1][3]["X-Ingress-Path"] == \
"/api/hassio_ingress/{}".format(build_type[0])
assert aioclient_mock.mock_calls[-1][3]["X-Test-Header"] == "beer"
assert aioclient_mock.mock_calls[-1][3][X_FORWARDED_FOR]
assert aioclient_mock.mock_calls[-1][3][X_FORWARDED_HOST]
assert aioclient_mock.mock_calls[-1][3][X_FORWARDED_PROTO]
@pytest.mark.parametrize(
'build_type', [
("a3_vl", "test/beer/ping?index=1"), ("core", "index.html"),
("local", "panel/config"), ("jk_921", "editor.php?idx=3&ping=5")
])
async def test_ingress_request_delete(
hassio_client, build_type, aioclient_mock):
"""Test no auth needed for ."""
aioclient_mock.delete("http://127.0.0.1/addons/{}/web/{}".format(
build_type[0], build_type[1]), text="test")
resp = await hassio_client.delete(
'/api/hassio_ingress/{}/{}'.format(build_type[0], build_type[1]),
headers={"X-Test-Header": "beer"}
)
# Check we got right response
assert resp.status == 200
body = await resp.text()
assert body == "test"
# Check we forwarded command
assert len(aioclient_mock.mock_calls) == 1
assert aioclient_mock.mock_calls[-1][3]["X-Hassio-Key"] == "123456"
assert aioclient_mock.mock_calls[-1][3]["X-Ingress-Path"] == \
"/api/hassio_ingress/{}".format(build_type[0])
assert aioclient_mock.mock_calls[-1][3]["X-Test-Header"] == "beer"
assert aioclient_mock.mock_calls[-1][3][X_FORWARDED_FOR]
assert aioclient_mock.mock_calls[-1][3][X_FORWARDED_HOST]
assert aioclient_mock.mock_calls[-1][3][X_FORWARDED_PROTO]
@pytest.mark.parametrize(
'build_type', [
("a3_vl", "test/beer/ws"), ("core", "ws.php"),
("local", "panel/config/stream"), ("jk_921", "hulk")
])
async def test_ingress_websocket(
hassio_client, build_type, aioclient_mock):
"""Test no auth needed for ."""
aioclient_mock.get("http://127.0.0.1/addons/{}/web/{}".format(
build_type[0], build_type[1]))
# Ignore error because we can setup a full IO infrastructure
with pytest.raises(WSServerHandshakeError):
await hassio_client.ws_connect(
'/api/hassio_ingress/{}/{}'.format(build_type[0], build_type[1]),
headers={"X-Test-Header": "beer"}
)
# Check we forwarded command
assert len(aioclient_mock.mock_calls) == 1
assert aioclient_mock.mock_calls[-1][3]["X-Hassio-Key"] == "123456"
assert aioclient_mock.mock_calls[-1][3]["X-Ingress-Path"] == \
"/api/hassio_ingress/{}".format(build_type[0])
assert aioclient_mock.mock_calls[-1][3]["X-Test-Header"] == "beer"
assert aioclient_mock.mock_calls[-1][3][X_FORWARDED_FOR]
assert aioclient_mock.mock_calls[-1][3][X_FORWARDED_HOST]
assert aioclient_mock.mock_calls[-1][3][X_FORWARDED_PROTO]
+1 -1
View File
@@ -207,7 +207,7 @@ def test_setup_hassio_no_additional_data(hass, aioclient_mock):
assert result
assert aioclient_mock.call_count == 3
assert aioclient_mock.mock_calls[-1][3]['X-HASSIO-KEY'] == "123456"
assert aioclient_mock.mock_calls[-1][3]['X-Hassio-Key'] == "123456"
@asyncio.coroutine