2018-02-20 23:24:31 +00:00
|
|
|
"""The tests for the hassio component."""
|
|
|
|
import asyncio
|
2019-04-01 12:16:16 +00:00
|
|
|
from unittest.mock import patch
|
2018-02-20 23:24:31 +00:00
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
from homeassistant.const import HTTP_HEADER_HA_AUTH
|
|
|
|
|
|
|
|
from . import API_PASSWORD
|
|
|
|
|
|
|
|
|
|
|
|
@asyncio.coroutine
|
2019-04-01 12:16:16 +00:00
|
|
|
def test_forward_request(hassio_client, aioclient_mock):
|
2018-02-20 23:24:31 +00:00
|
|
|
"""Test fetching normal path."""
|
2019-04-01 12:16:16 +00:00
|
|
|
aioclient_mock.post("http://127.0.0.1/beer", text="response")
|
|
|
|
|
|
|
|
resp = yield from hassio_client.post('/api/hassio/beer', headers={
|
|
|
|
HTTP_HEADER_HA_AUTH: API_PASSWORD
|
|
|
|
})
|
2018-02-20 23:24:31 +00:00
|
|
|
|
|
|
|
# Check we got right response
|
|
|
|
assert resp.status == 200
|
|
|
|
body = yield from resp.text()
|
|
|
|
assert body == 'response'
|
|
|
|
|
|
|
|
# Check we forwarded command
|
2019-04-01 12:16:16 +00:00
|
|
|
assert len(aioclient_mock.mock_calls) == 1
|
2018-02-20 23:24:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
@asyncio.coroutine
|
2018-09-19 10:57:55 +00:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
'build_type', [
|
|
|
|
'supervisor/info', 'homeassistant/update', 'host/info'
|
|
|
|
])
|
2018-12-17 10:27:03 +00:00
|
|
|
def test_auth_required_forward_request(hassio_noauth_client, build_type):
|
2018-02-20 23:24:31 +00:00
|
|
|
"""Test auth required for normal request."""
|
2018-12-17 11:11:33 +00:00
|
|
|
resp = yield from hassio_noauth_client.post(
|
|
|
|
"/api/hassio/{}".format(build_type))
|
2018-02-20 23:24:31 +00:00
|
|
|
|
|
|
|
# Check we got right response
|
|
|
|
assert resp.status == 401
|
|
|
|
|
|
|
|
|
|
|
|
@asyncio.coroutine
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
'build_type', [
|
2018-06-17 18:34:47 +00:00
|
|
|
'app/index.html', 'app/hassio-app.html', 'app/index.html',
|
|
|
|
'app/hassio-app.html', 'app/some-chunk.js', 'app/app.js',
|
2018-02-20 23:24:31 +00:00
|
|
|
])
|
2019-04-01 12:16:16 +00:00
|
|
|
def test_forward_request_no_auth_for_panel(
|
|
|
|
hassio_client, build_type, aioclient_mock):
|
2018-02-20 23:24:31 +00:00
|
|
|
"""Test no auth needed for ."""
|
2019-04-01 12:16:16 +00:00
|
|
|
aioclient_mock.get(
|
|
|
|
"http://127.0.0.1/{}".format(build_type), text="response")
|
|
|
|
|
|
|
|
resp = yield from hassio_client.get('/api/hassio/{}'.format(build_type))
|
2018-02-20 23:24:31 +00:00
|
|
|
|
|
|
|
# Check we got right response
|
|
|
|
assert resp.status == 200
|
|
|
|
body = yield from resp.text()
|
|
|
|
assert body == 'response'
|
|
|
|
|
|
|
|
# Check we forwarded command
|
2019-04-01 12:16:16 +00:00
|
|
|
assert len(aioclient_mock.mock_calls) == 1
|
2018-02-20 23:24:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
@asyncio.coroutine
|
2019-04-01 12:16:16 +00:00
|
|
|
def test_forward_request_no_auth_for_logo(hassio_client, aioclient_mock):
|
2018-02-20 23:24:31 +00:00
|
|
|
"""Test no auth needed for ."""
|
2019-04-01 12:16:16 +00:00
|
|
|
aioclient_mock.get(
|
|
|
|
"http://127.0.0.1/addons/bl_b392/logo", text="response")
|
2018-02-20 23:24:31 +00:00
|
|
|
|
2019-04-01 12:16:16 +00:00
|
|
|
resp = yield from hassio_client.get('/api/hassio/addons/bl_b392/logo')
|
2018-02-20 23:24:31 +00:00
|
|
|
|
|
|
|
# Check we got right response
|
|
|
|
assert resp.status == 200
|
|
|
|
body = yield from resp.text()
|
|
|
|
assert body == 'response'
|
|
|
|
|
|
|
|
# Check we forwarded command
|
2019-04-01 12:16:16 +00:00
|
|
|
assert len(aioclient_mock.mock_calls) == 1
|
2018-02-20 23:24:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
@asyncio.coroutine
|
2019-04-01 12:16:16 +00:00
|
|
|
def test_forward_log_request(hassio_client, aioclient_mock):
|
2018-12-02 14:46:14 +00:00
|
|
|
"""Test fetching normal log path doesn't remove ANSI color escape codes."""
|
2019-04-01 12:16:16 +00:00
|
|
|
aioclient_mock.get(
|
|
|
|
"http://127.0.0.1/beer/logs", text="\033[32mresponse\033[0m")
|
|
|
|
|
|
|
|
resp = yield from hassio_client.get('/api/hassio/beer/logs', headers={
|
|
|
|
HTTP_HEADER_HA_AUTH: API_PASSWORD
|
|
|
|
})
|
2018-02-20 23:24:31 +00:00
|
|
|
|
|
|
|
# Check we got right response
|
|
|
|
assert resp.status == 200
|
|
|
|
body = yield from resp.text()
|
2018-12-02 14:46:14 +00:00
|
|
|
assert body == '\033[32mresponse\033[0m'
|
2018-02-20 23:24:31 +00:00
|
|
|
|
|
|
|
# Check we forwarded command
|
2019-04-01 12:16:16 +00:00
|
|
|
assert len(aioclient_mock.mock_calls) == 1
|
2018-02-20 23:24:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
@asyncio.coroutine
|
|
|
|
def test_bad_gateway_when_cannot_find_supervisor(hassio_client):
|
|
|
|
"""Test we get a bad gateway error if we can't find supervisor."""
|
|
|
|
with patch('homeassistant.components.hassio.http.async_timeout.timeout',
|
|
|
|
side_effect=asyncio.TimeoutError):
|
|
|
|
resp = yield from hassio_client.get(
|
|
|
|
'/api/hassio/addons/test/info', headers={
|
|
|
|
HTTP_HEADER_HA_AUTH: API_PASSWORD
|
|
|
|
})
|
|
|
|
assert resp.status == 502
|
2018-12-17 10:27:03 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_forwarding_user_info(hassio_client, hass_admin_user,
|
|
|
|
aioclient_mock):
|
|
|
|
"""Test that we forward user info correctly."""
|
|
|
|
aioclient_mock.get('http://127.0.0.1/hello')
|
|
|
|
|
|
|
|
resp = await hassio_client.get('/api/hassio/hello')
|
|
|
|
|
|
|
|
# Check we got right response
|
|
|
|
assert resp.status == 200
|
|
|
|
|
|
|
|
assert len(aioclient_mock.mock_calls) == 1
|
|
|
|
|
|
|
|
req_headers = aioclient_mock.mock_calls[0][-1]
|
2019-04-01 12:16:16 +00:00
|
|
|
req_headers['X-Hass-User-ID'] == hass_admin_user.id
|
|
|
|
req_headers['X-Hass-Is-Admin'] == '1'
|