Hassio api v3 (#7323)
* HassIO rest API v3 * fix content type * fix lint * Update comment * fix content type * change proxy handling * fix handling * fix register * fix addons * fix routing * Update hassio to just proxy * Fix tests * Lint
This commit is contained in:
1 parent
3374169c74
commit
b14c07a60c
2 files changed
+261
-807
No files matched your search
+143
-564
@@ -1,605 +1,184 @@
|
||||
"""The tests for the hassio component."""
|
||||
import asyncio
|
||||
import os
|
||||
from unittest.mock import patch
|
||||
from unittest.mock import patch, Mock, MagicMock
|
||||
|
||||
import aiohttp
|
||||
import pytest
|
||||
|
||||
import homeassistant.components.hassio as ho
|
||||
from homeassistant.setup import setup_component, async_setup_component
|
||||
from homeassistant.setup import async_setup_component
|
||||
|
||||
from tests.common import (
|
||||
get_test_home_assistant, assert_setup_component)
|
||||
from tests.common import mock_coro, mock_http_component_app
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def hassio_env():
|
||||
"""Fixture to inject hassio env."""
|
||||
with patch.dict(os.environ, {'HASSIO': "127.0.0.1"}) as env_mock:
|
||||
yield env_mock
|
||||
|
||||
|
||||
class TestHassIOSetup(object):
|
||||
"""Test the hassio component."""
|
||||
|
||||
def setup_method(self):
|
||||
"""Setup things to be run when tests are started."""
|
||||
self.hass = get_test_home_assistant()
|
||||
|
||||
self.config = {
|
||||
ho.DOMAIN: {},
|
||||
}
|
||||
|
||||
def teardown_method(self):
|
||||
"""Stop everything that was started."""
|
||||
self.hass.stop()
|
||||
|
||||
def test_setup_component(self, aioclient_mock, hassio_env):
|
||||
"""Test setup component."""
|
||||
aioclient_mock.get("http://127.0.0.1/supervisor/ping", json={
|
||||
'result': 'ok', 'data': {}
|
||||
})
|
||||
with assert_setup_component(0, ho.DOMAIN):
|
||||
setup_component(self.hass, ho.DOMAIN, self.config)
|
||||
|
||||
def test_setup_component_bad(self, aioclient_mock):
|
||||
"""Test setup component bad."""
|
||||
aioclient_mock.get("http://127.0.0.1/supervisor/ping", json={
|
||||
'result': 'ok', 'data': {}
|
||||
})
|
||||
with assert_setup_component(0, ho.DOMAIN):
|
||||
assert not setup_component(self.hass, ho.DOMAIN, self.config)
|
||||
|
||||
assert len(aioclient_mock.mock_calls) == 0
|
||||
|
||||
def test_setup_component_test_service(self, aioclient_mock, hassio_env):
|
||||
"""Test setup component and check if service exits."""
|
||||
aioclient_mock.get("http://127.0.0.1/supervisor/ping", json={
|
||||
'result': 'ok', 'data': {}
|
||||
})
|
||||
with assert_setup_component(0, ho.DOMAIN):
|
||||
setup_component(self.hass, ho.DOMAIN, self.config)
|
||||
|
||||
assert self.hass.services.has_service(
|
||||
ho.DOMAIN, ho.SERVICE_HOST_REBOOT)
|
||||
assert self.hass.services.has_service(
|
||||
ho.DOMAIN, ho.SERVICE_HOST_SHUTDOWN)
|
||||
assert self.hass.services.has_service(
|
||||
ho.DOMAIN, ho.SERVICE_HOST_UPDATE)
|
||||
|
||||
assert self.hass.services.has_service(
|
||||
ho.DOMAIN, ho.SERVICE_SUPERVISOR_UPDATE)
|
||||
assert self.hass.services.has_service(
|
||||
ho.DOMAIN, ho.SERVICE_SUPERVISOR_RELOAD)
|
||||
|
||||
assert self.hass.services.has_service(
|
||||
ho.DOMAIN, ho.SERVICE_ADDON_INSTALL)
|
||||
assert self.hass.services.has_service(
|
||||
ho.DOMAIN, ho.SERVICE_ADDON_UNINSTALL)
|
||||
assert self.hass.services.has_service(
|
||||
ho.DOMAIN, ho.SERVICE_ADDON_UPDATE)
|
||||
assert self.hass.services.has_service(
|
||||
ho.DOMAIN, ho.SERVICE_ADDON_START)
|
||||
assert self.hass.services.has_service(
|
||||
ho.DOMAIN, ho.SERVICE_ADDON_STOP)
|
||||
|
||||
|
||||
class TestHassIOComponent(object):
|
||||
"""Test the HassIO component."""
|
||||
|
||||
def setup_method(self):
|
||||
"""Setup things to be run when tests are started."""
|
||||
self.hass = get_test_home_assistant()
|
||||
self.config = {
|
||||
ho.DOMAIN: {},
|
||||
}
|
||||
|
||||
self.url = "http://127.0.0.1/{}"
|
||||
|
||||
self.error_msg = {
|
||||
'result': 'error',
|
||||
'message': 'Test error',
|
||||
}
|
||||
self.ok_msg = {
|
||||
'result': 'ok',
|
||||
'data': {},
|
||||
}
|
||||
|
||||
def teardown_method(self):
|
||||
"""Stop everything that was started."""
|
||||
self.hass.stop()
|
||||
|
||||
def test_rest_command_timeout(self, aioclient_mock, hassio_env):
|
||||
"""Call a hassio with timeout."""
|
||||
aioclient_mock.get(
|
||||
"http://127.0.0.1/supervisor/ping", json=self.ok_msg)
|
||||
with assert_setup_component(0, ho.DOMAIN):
|
||||
setup_component(self.hass, ho.DOMAIN, self.config)
|
||||
|
||||
aioclient_mock.get(
|
||||
self.url.format("host/update"), exc=asyncio.TimeoutError())
|
||||
|
||||
self.hass.services.call(ho.DOMAIN, ho.SERVICE_HOST_UPDATE, {})
|
||||
self.hass.block_till_done()
|
||||
|
||||
assert len(aioclient_mock.mock_calls) == 2
|
||||
|
||||
def test_rest_command_aiohttp_error(self, aioclient_mock, hassio_env):
|
||||
"""Call a hassio with aiohttp exception."""
|
||||
aioclient_mock.get(
|
||||
"http://127.0.0.1/supervisor/ping", json=self.ok_msg)
|
||||
with assert_setup_component(0, ho.DOMAIN):
|
||||
setup_component(self.hass, ho.DOMAIN, self.config)
|
||||
|
||||
aioclient_mock.get(
|
||||
self.url.format("host/update"), exc=aiohttp.ClientError())
|
||||
|
||||
self.hass.services.call(ho.DOMAIN, ho.SERVICE_HOST_UPDATE, {})
|
||||
self.hass.block_till_done()
|
||||
|
||||
assert len(aioclient_mock.mock_calls) == 2
|
||||
|
||||
def test_rest_command_http_error(self, aioclient_mock, hassio_env):
|
||||
"""Call a hassio with status code 503."""
|
||||
aioclient_mock.get(
|
||||
"http://127.0.0.1/supervisor/ping", json=self.ok_msg)
|
||||
with assert_setup_component(0, ho.DOMAIN):
|
||||
setup_component(self.hass, ho.DOMAIN, self.config)
|
||||
|
||||
aioclient_mock.get(
|
||||
self.url.format("host/update"), status=503)
|
||||
|
||||
self.hass.services.call(ho.DOMAIN, ho.SERVICE_HOST_UPDATE, {})
|
||||
self.hass.block_till_done()
|
||||
|
||||
assert len(aioclient_mock.mock_calls) == 2
|
||||
|
||||
def test_rest_command_http_error_api(self, aioclient_mock, hassio_env):
|
||||
"""Call a hassio with status code 503."""
|
||||
aioclient_mock.get(
|
||||
"http://127.0.0.1/supervisor/ping", json=self.ok_msg)
|
||||
with assert_setup_component(0, ho.DOMAIN):
|
||||
setup_component(self.hass, ho.DOMAIN, self.config)
|
||||
|
||||
aioclient_mock.get(
|
||||
self.url.format("host/update"), json=self.error_msg)
|
||||
|
||||
self.hass.services.call(ho.DOMAIN, ho.SERVICE_HOST_UPDATE, {})
|
||||
self.hass.block_till_done()
|
||||
|
||||
assert len(aioclient_mock.mock_calls) == 2
|
||||
|
||||
def test_rest_command_http_host_reboot(self, aioclient_mock, hassio_env):
|
||||
"""Call a hassio for host reboot."""
|
||||
aioclient_mock.get(
|
||||
"http://127.0.0.1/supervisor/ping", json=self.ok_msg)
|
||||
with assert_setup_component(0, ho.DOMAIN):
|
||||
setup_component(self.hass, ho.DOMAIN, self.config)
|
||||
|
||||
aioclient_mock.get(
|
||||
self.url.format("host/reboot"), json=self.ok_msg)
|
||||
|
||||
self.hass.services.call(ho.DOMAIN, ho.SERVICE_HOST_REBOOT, {})
|
||||
self.hass.block_till_done()
|
||||
|
||||
assert len(aioclient_mock.mock_calls) == 2
|
||||
|
||||
def test_rest_command_http_host_shutdown(self, aioclient_mock, hassio_env):
|
||||
"""Call a hassio for host shutdown."""
|
||||
aioclient_mock.get(
|
||||
"http://127.0.0.1/supervisor/ping", json=self.ok_msg)
|
||||
with assert_setup_component(0, ho.DOMAIN):
|
||||
setup_component(self.hass, ho.DOMAIN, self.config)
|
||||
|
||||
aioclient_mock.get(
|
||||
self.url.format("host/shutdown"), json=self.ok_msg)
|
||||
|
||||
self.hass.services.call(ho.DOMAIN, ho.SERVICE_HOST_SHUTDOWN, {})
|
||||
self.hass.block_till_done()
|
||||
|
||||
assert len(aioclient_mock.mock_calls) == 2
|
||||
|
||||
def test_rest_command_http_host_update(self, aioclient_mock, hassio_env):
|
||||
"""Call a hassio for host update."""
|
||||
aioclient_mock.get(
|
||||
"http://127.0.0.1/supervisor/ping", json=self.ok_msg)
|
||||
with assert_setup_component(0, ho.DOMAIN):
|
||||
setup_component(self.hass, ho.DOMAIN, self.config)
|
||||
|
||||
aioclient_mock.get(
|
||||
self.url.format("host/update"), json=self.ok_msg)
|
||||
|
||||
self.hass.services.call(
|
||||
ho.DOMAIN, ho.SERVICE_HOST_UPDATE, {'version': '0.4'})
|
||||
self.hass.block_till_done()
|
||||
|
||||
assert len(aioclient_mock.mock_calls) == 2
|
||||
assert aioclient_mock.mock_calls[-1][2]['version'] == '0.4'
|
||||
|
||||
def test_rest_command_http_supervisor_update(self, aioclient_mock,
|
||||
hassio_env):
|
||||
"""Call a hassio for supervisor update."""
|
||||
aioclient_mock.get(
|
||||
"http://127.0.0.1/supervisor/ping", json=self.ok_msg)
|
||||
with assert_setup_component(0, ho.DOMAIN):
|
||||
setup_component(self.hass, ho.DOMAIN, self.config)
|
||||
|
||||
aioclient_mock.get(
|
||||
self.url.format("supervisor/update"), json=self.ok_msg)
|
||||
|
||||
self.hass.services.call(
|
||||
ho.DOMAIN, ho.SERVICE_SUPERVISOR_UPDATE, {'version': '0.4'})
|
||||
self.hass.block_till_done()
|
||||
|
||||
assert len(aioclient_mock.mock_calls) == 2
|
||||
assert aioclient_mock.mock_calls[-1][2]['version'] == '0.4'
|
||||
|
||||
def test_rest_command_http_supervisor_reload(self, aioclient_mock,
|
||||
hassio_env):
|
||||
"""Call a hassio for supervisor reload."""
|
||||
aioclient_mock.get(
|
||||
"http://127.0.0.1/supervisor/ping", json=self.ok_msg)
|
||||
with assert_setup_component(0, ho.DOMAIN):
|
||||
setup_component(self.hass, ho.DOMAIN, self.config)
|
||||
|
||||
aioclient_mock.get(
|
||||
self.url.format("supervisor/reload"), json=self.ok_msg)
|
||||
|
||||
self.hass.services.call(
|
||||
ho.DOMAIN, ho.SERVICE_SUPERVISOR_RELOAD, {})
|
||||
self.hass.block_till_done()
|
||||
|
||||
assert len(aioclient_mock.mock_calls) == 2
|
||||
|
||||
def test_rest_command_http_homeassistant_update(self, aioclient_mock,
|
||||
hassio_env):
|
||||
"""Call a hassio for homeassistant update."""
|
||||
aioclient_mock.get(
|
||||
"http://127.0.0.1/supervisor/ping", json=self.ok_msg)
|
||||
with assert_setup_component(0, ho.DOMAIN):
|
||||
setup_component(self.hass, ho.DOMAIN, self.config)
|
||||
|
||||
aioclient_mock.get(
|
||||
self.url.format("homeassistant/update"), json=self.ok_msg)
|
||||
|
||||
self.hass.services.call(
|
||||
ho.DOMAIN, ho.SERVICE_HOMEASSISTANT_UPDATE, {'version': '0.4'})
|
||||
self.hass.block_till_done()
|
||||
|
||||
assert len(aioclient_mock.mock_calls) == 2
|
||||
assert aioclient_mock.mock_calls[-1][2]['version'] == '0.4'
|
||||
|
||||
def test_rest_command_http_addon_install(self, aioclient_mock, hassio_env):
|
||||
"""Call a hassio for addon install."""
|
||||
aioclient_mock.get(
|
||||
"http://127.0.0.1/supervisor/ping", json=self.ok_msg)
|
||||
with assert_setup_component(0, ho.DOMAIN):
|
||||
setup_component(self.hass, ho.DOMAIN, self.config)
|
||||
|
||||
aioclient_mock.get(
|
||||
self.url.format("addons/smb_config/install"), json=self.ok_msg)
|
||||
|
||||
self.hass.services.call(
|
||||
ho.DOMAIN, ho.SERVICE_ADDON_INSTALL, {
|
||||
'addon': 'smb_config',
|
||||
'version': '0.4'
|
||||
})
|
||||
self.hass.block_till_done()
|
||||
|
||||
assert len(aioclient_mock.mock_calls) == 2
|
||||
assert aioclient_mock.mock_calls[-1][2]['version'] == '0.4'
|
||||
|
||||
def test_rest_command_http_addon_uninstall(self, aioclient_mock,
|
||||
hassio_env):
|
||||
"""Call a hassio for addon uninstall."""
|
||||
aioclient_mock.get(
|
||||
"http://127.0.0.1/supervisor/ping", json=self.ok_msg)
|
||||
with assert_setup_component(0, ho.DOMAIN):
|
||||
setup_component(self.hass, ho.DOMAIN, self.config)
|
||||
|
||||
aioclient_mock.get(
|
||||
self.url.format("addons/smb_config/uninstall"), json=self.ok_msg)
|
||||
|
||||
self.hass.services.call(
|
||||
ho.DOMAIN, ho.SERVICE_ADDON_UNINSTALL, {
|
||||
'addon': 'smb_config'
|
||||
})
|
||||
self.hass.block_till_done()
|
||||
|
||||
assert len(aioclient_mock.mock_calls) == 2
|
||||
|
||||
def test_rest_command_http_addon_update(self, aioclient_mock, hassio_env):
|
||||
"""Call a hassio for addon update."""
|
||||
aioclient_mock.get(
|
||||
"http://127.0.0.1/supervisor/ping", json=self.ok_msg)
|
||||
with assert_setup_component(0, ho.DOMAIN):
|
||||
setup_component(self.hass, ho.DOMAIN, self.config)
|
||||
|
||||
aioclient_mock.get(
|
||||
self.url.format("addons/smb_config/update"), json=self.ok_msg)
|
||||
|
||||
self.hass.services.call(
|
||||
ho.DOMAIN, ho.SERVICE_ADDON_UPDATE, {
|
||||
'addon': 'smb_config',
|
||||
'version': '0.4'
|
||||
})
|
||||
self.hass.block_till_done()
|
||||
|
||||
assert len(aioclient_mock.mock_calls) == 2
|
||||
assert aioclient_mock.mock_calls[-1][2]['version'] == '0.4'
|
||||
|
||||
def test_rest_command_http_addon_start(self, aioclient_mock, hassio_env):
|
||||
"""Call a hassio for addon start."""
|
||||
aioclient_mock.get(
|
||||
"http://127.0.0.1/supervisor/ping", json=self.ok_msg)
|
||||
with assert_setup_component(0, ho.DOMAIN):
|
||||
setup_component(self.hass, ho.DOMAIN, self.config)
|
||||
|
||||
aioclient_mock.get(
|
||||
self.url.format("addons/smb_config/start"), json=self.ok_msg)
|
||||
|
||||
self.hass.services.call(
|
||||
ho.DOMAIN, ho.SERVICE_ADDON_START, {
|
||||
'addon': 'smb_config',
|
||||
})
|
||||
self.hass.block_till_done()
|
||||
|
||||
assert len(aioclient_mock.mock_calls) == 2
|
||||
|
||||
def test_rest_command_http_addon_stop(self, aioclient_mock, hassio_env):
|
||||
"""Call a hassio for addon stop."""
|
||||
aioclient_mock.get(
|
||||
"http://127.0.0.1/supervisor/ping", json=self.ok_msg)
|
||||
with assert_setup_component(0, ho.DOMAIN):
|
||||
setup_component(self.hass, ho.DOMAIN, self.config)
|
||||
|
||||
aioclient_mock.get(
|
||||
self.url.format("addons/smb_config/stop"), json=self.ok_msg)
|
||||
|
||||
self.hass.services.call(
|
||||
ho.DOMAIN, ho.SERVICE_ADDON_STOP, {
|
||||
'addon': 'smb_config'
|
||||
})
|
||||
self.hass.block_till_done()
|
||||
|
||||
assert len(aioclient_mock.mock_calls) == 2
|
||||
with patch.dict(os.environ, {'HASSIO': "127.0.0.1"}), \
|
||||
patch('homeassistant.components.hassio.HassIO.is_connected',
|
||||
Mock(return_value=mock_coro(True))):
|
||||
yield
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def hassio_client(hassio_env, hass, test_client):
|
||||
"""Create mock hassio http client."""
|
||||
app = mock_http_component_app(hass)
|
||||
hass.loop.run_until_complete(async_setup_component(hass, 'hassio', {}))
|
||||
hass.http.views['api:hassio'].register(app.router)
|
||||
yield hass.loop.run_until_complete(test_client(app))
|
||||
|
||||
|
||||
@asyncio.coroutine
|
||||
def test_async_hassio_host_view(aioclient_mock, hass, test_client, hassio_env):
|
||||
"""Test that it fetches the given url."""
|
||||
aioclient_mock.get("http://127.0.0.1/supervisor/ping", json={
|
||||
'result': 'ok', 'data': {}
|
||||
})
|
||||
result = yield from async_setup_component(hass, ho.DOMAIN, {ho.DOMAIN: {}})
|
||||
assert result, 'Failed to setup hasio'
|
||||
|
||||
client = yield from test_client(hass.http.app)
|
||||
|
||||
aioclient_mock.get('http://127.0.0.1/host/info', json={
|
||||
'result': 'ok',
|
||||
'data': {
|
||||
'os': 'resinos',
|
||||
'version': '0.3',
|
||||
'current': '0.4',
|
||||
'level': 16,
|
||||
'hostname': 'test',
|
||||
}
|
||||
})
|
||||
|
||||
resp = yield from client.get('/api/hassio/host')
|
||||
data = yield from resp.json()
|
||||
|
||||
assert len(aioclient_mock.mock_calls) == 2
|
||||
assert resp.status == 200
|
||||
assert data['os'] == 'resinos'
|
||||
assert data['version'] == '0.3'
|
||||
assert data['current'] == '0.4'
|
||||
assert data['level'] == 16
|
||||
assert data['hostname'] == 'test'
|
||||
def test_fail_setup_without_environ_var(hass):
|
||||
"""Fail setup if no environ variable set."""
|
||||
with patch.dict(os.environ, {}, clear=True):
|
||||
result = yield from async_setup_component(hass, 'hassio', {})
|
||||
assert not result
|
||||
|
||||
|
||||
@asyncio.coroutine
|
||||
def test_async_hassio_homeassistant_view(aioclient_mock, hass, test_client,
|
||||
hassio_env):
|
||||
"""Test that it fetches the given url."""
|
||||
aioclient_mock.get("http://127.0.0.1/supervisor/ping", json={
|
||||
'result': 'ok', 'data': {}
|
||||
})
|
||||
result = yield from async_setup_component(hass, ho.DOMAIN, {ho.DOMAIN: {}})
|
||||
assert result, 'Failed to setup hasio'
|
||||
|
||||
client = yield from test_client(hass.http.app)
|
||||
|
||||
aioclient_mock.get('http://127.0.0.1/homeassistant/info', json={
|
||||
'result': 'ok',
|
||||
'data': {
|
||||
'version': '0.41',
|
||||
'current': '0.41.1',
|
||||
}
|
||||
})
|
||||
|
||||
resp = yield from client.get('/api/hassio/homeassistant')
|
||||
data = yield from resp.json()
|
||||
|
||||
assert len(aioclient_mock.mock_calls) == 2
|
||||
assert resp.status == 200
|
||||
assert data['version'] == '0.41'
|
||||
assert data['current'] == '0.41.1'
|
||||
|
||||
aioclient_mock.get('http://127.0.0.1/homeassistant/logs',
|
||||
content=b"That is a test log")
|
||||
|
||||
resp = yield from client.get('/api/hassio/logs/homeassistant')
|
||||
data = yield from resp.read()
|
||||
|
||||
assert len(aioclient_mock.mock_calls) == 3
|
||||
assert resp.status == 200
|
||||
assert data == b"That is a test log"
|
||||
def test_fail_setup_cannot_connect(hass):
|
||||
"""Fail setup if cannot connect."""
|
||||
with patch.dict(os.environ, {'HASSIO': "127.0.0.1"}), \
|
||||
patch('homeassistant.components.hassio.HassIO.is_connected',
|
||||
Mock(return_value=mock_coro(False))):
|
||||
result = yield from async_setup_component(hass, 'hassio', {})
|
||||
assert not result
|
||||
|
||||
|
||||
@asyncio.coroutine
|
||||
def test_async_hassio_supervisor_view(aioclient_mock, hass, test_client,
|
||||
hassio_env):
|
||||
"""Test that it fetches the given url."""
|
||||
aioclient_mock.get("http://127.0.0.1/supervisor/ping", json={
|
||||
'result': 'ok', 'data': {}
|
||||
})
|
||||
result = yield from async_setup_component(hass, ho.DOMAIN, {ho.DOMAIN: {}})
|
||||
assert result, 'Failed to setup hasio'
|
||||
def test_invalid_path(hassio_client):
|
||||
"""Test requesting invalid path."""
|
||||
with patch.dict(ho.HASSIO_REST_COMMANDS, {}, clear=True):
|
||||
resp = yield from hassio_client.post('/api/hassio/beer')
|
||||
|
||||
client = yield from test_client(hass.http.app)
|
||||
|
||||
aioclient_mock.get('http://127.0.0.1/supervisor/info', json={
|
||||
'result': 'ok',
|
||||
'data': {
|
||||
'version': '0.3',
|
||||
'current': '0.4',
|
||||
'beta': False,
|
||||
}
|
||||
})
|
||||
|
||||
resp = yield from client.get('/api/hassio/supervisor')
|
||||
data = yield from resp.json()
|
||||
|
||||
assert len(aioclient_mock.mock_calls) == 2
|
||||
assert resp.status == 200
|
||||
assert data['version'] == '0.3'
|
||||
assert data['current'] == '0.4'
|
||||
assert not data['beta']
|
||||
|
||||
aioclient_mock.get('http://127.0.0.1/supervisor/options', json={
|
||||
'result': 'ok',
|
||||
'data': {},
|
||||
})
|
||||
|
||||
resp = yield from client.post('/api/hassio/supervisor', json={
|
||||
'beta': True,
|
||||
})
|
||||
data = yield from resp.json()
|
||||
|
||||
assert len(aioclient_mock.mock_calls) == 3
|
||||
assert resp.status == 200
|
||||
assert aioclient_mock.mock_calls[-1][2]['beta']
|
||||
|
||||
aioclient_mock.get('http://127.0.0.1/supervisor/logs',
|
||||
content=b"That is a test log")
|
||||
|
||||
resp = yield from client.get('/api/hassio/logs/supervisor')
|
||||
data = yield from resp.read()
|
||||
|
||||
assert len(aioclient_mock.mock_calls) == 4
|
||||
assert resp.status == 200
|
||||
assert data == b"That is a test log"
|
||||
assert resp.status == 404
|
||||
|
||||
|
||||
@asyncio.coroutine
|
||||
def test_async_hassio_network_view(aioclient_mock, hass, test_client,
|
||||
hassio_env):
|
||||
"""Test that it fetches the given url."""
|
||||
aioclient_mock.get("http://127.0.0.1/supervisor/ping", json={
|
||||
'result': 'ok', 'data': {}
|
||||
})
|
||||
result = yield from async_setup_component(hass, ho.DOMAIN, {ho.DOMAIN: {}})
|
||||
assert result, 'Failed to setup hasio'
|
||||
def test_invalid_method(hassio_client):
|
||||
"""Test requesting path with invalid method."""
|
||||
with patch.dict(ho.HASSIO_REST_COMMANDS, {'beer': ['POST']}):
|
||||
resp = yield from hassio_client.get('/api/hassio/beer')
|
||||
|
||||
client = yield from test_client(hass.http.app)
|
||||
|
||||
aioclient_mock.get('http://127.0.0.1/network/info', json={
|
||||
'result': 'ok',
|
||||
'data': {
|
||||
'mode': 'dhcp',
|
||||
'ssid': 'my_wlan',
|
||||
'password': '123456',
|
||||
}
|
||||
})
|
||||
|
||||
resp = yield from client.get('/api/hassio/network')
|
||||
data = yield from resp.json()
|
||||
|
||||
assert len(aioclient_mock.mock_calls) == 2
|
||||
assert resp.status == 200
|
||||
assert data['mode'] == 'dhcp'
|
||||
assert data['ssid'] == 'my_wlan'
|
||||
assert data['password'] == '123456'
|
||||
|
||||
aioclient_mock.get('http://127.0.0.1/network/options', json={
|
||||
'result': 'ok',
|
||||
'data': {},
|
||||
})
|
||||
|
||||
resp = yield from client.post('/api/hassio/network', json={
|
||||
'mode': 'dhcp',
|
||||
'ssid': 'my_wlan2',
|
||||
'password': '654321',
|
||||
})
|
||||
data = yield from resp.json()
|
||||
|
||||
assert len(aioclient_mock.mock_calls) == 3
|
||||
assert resp.status == 200
|
||||
assert aioclient_mock.mock_calls[-1][2]['ssid'] == 'my_wlan2'
|
||||
assert aioclient_mock.mock_calls[-1][2]['password'] == '654321'
|
||||
assert resp.status == 405
|
||||
|
||||
|
||||
@asyncio.coroutine
|
||||
def test_async_hassio_addon_view(aioclient_mock, hass, test_client,
|
||||
hassio_env):
|
||||
"""Test that it fetches the given url."""
|
||||
aioclient_mock.get("http://127.0.0.1/supervisor/ping", json={
|
||||
'result': 'ok', 'data': {}
|
||||
})
|
||||
result = yield from async_setup_component(hass, ho.DOMAIN, {ho.DOMAIN: {}})
|
||||
assert result, 'Failed to setup hasio'
|
||||
def test_forward_normal_path(hassio_client):
|
||||
"""Test fetching normal path."""
|
||||
response = MagicMock()
|
||||
response.read.return_value = mock_coro('data')
|
||||
|
||||
client = yield from test_client(hass.http.app)
|
||||
with patch.dict(ho.HASSIO_REST_COMMANDS, {'beer': ['POST']}), \
|
||||
patch('homeassistant.components.hassio.HassIO.command_proxy',
|
||||
Mock(return_value=mock_coro(response))), \
|
||||
patch('homeassistant.components.hassio._create_response') as mresp:
|
||||
mresp.return_value = 'response'
|
||||
resp = yield from hassio_client.post('/api/hassio/beer')
|
||||
|
||||
aioclient_mock.get('http://127.0.0.1/addons/smb_config/info', json={
|
||||
'result': 'ok',
|
||||
'data': {
|
||||
'name': 'SMB Config',
|
||||
'state': 'running',
|
||||
'boot': 'auto',
|
||||
'options': {
|
||||
'bla': False,
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
resp = yield from client.get('/api/hassio/addons/smb_config')
|
||||
data = yield from resp.json()
|
||||
|
||||
assert len(aioclient_mock.mock_calls) == 2
|
||||
# Check we got right response
|
||||
assert resp.status == 200
|
||||
assert data['name'] == 'SMB Config'
|
||||
assert data['state'] == 'running'
|
||||
assert data['boot'] == 'auto'
|
||||
assert not data['options']['bla']
|
||||
body = yield from resp.text()
|
||||
assert body == 'response'
|
||||
|
||||
aioclient_mock.get('http://127.0.0.1/addons/smb_config/options', json={
|
||||
'result': 'ok',
|
||||
'data': {},
|
||||
})
|
||||
# Check we forwarded command
|
||||
assert len(mresp.mock_calls) == 1
|
||||
assert mresp.mock_calls[0][1] == (response, 'data')
|
||||
|
||||
resp = yield from client.post('/api/hassio/addons/smb_config', json={
|
||||
'boot': 'manual',
|
||||
'options': {
|
||||
'bla': True,
|
||||
}
|
||||
})
|
||||
data = yield from resp.json()
|
||||
|
||||
assert len(aioclient_mock.mock_calls) == 3
|
||||
@asyncio.coroutine
|
||||
def test_forward_normal_log_path(hassio_client):
|
||||
"""Test fetching normal log path."""
|
||||
response = MagicMock()
|
||||
response.read.return_value = mock_coro('data')
|
||||
|
||||
with patch.dict(ho.HASSIO_REST_COMMANDS, {'beer/logs': ['GET']}), \
|
||||
patch('homeassistant.components.hassio.HassIO.command_proxy',
|
||||
Mock(return_value=mock_coro(response))), \
|
||||
patch('homeassistant.components.hassio.'
|
||||
'_create_response_log') as mresp:
|
||||
mresp.return_value = 'response'
|
||||
resp = yield from hassio_client.get('/api/hassio/beer/logs')
|
||||
|
||||
# Check we got right response
|
||||
assert resp.status == 200
|
||||
assert aioclient_mock.mock_calls[-1][2]['boot'] == 'manual'
|
||||
assert aioclient_mock.mock_calls[-1][2]['options']['bla']
|
||||
body = yield from resp.text()
|
||||
assert body == 'response'
|
||||
|
||||
aioclient_mock.get('http://127.0.0.1/addons/smb_config/logs',
|
||||
content=b"That is a test log")
|
||||
# Check we forwarded command
|
||||
assert len(mresp.mock_calls) == 1
|
||||
assert mresp.mock_calls[0][1] == (response, 'data')
|
||||
|
||||
resp = yield from client.get('/api/hassio/logs/addons/smb_config')
|
||||
data = yield from resp.read()
|
||||
|
||||
assert len(aioclient_mock.mock_calls) == 4
|
||||
@asyncio.coroutine
|
||||
def test_forward_addon_path(hassio_client):
|
||||
"""Test fetching addon path."""
|
||||
response = MagicMock()
|
||||
response.read.return_value = mock_coro('data')
|
||||
|
||||
with patch.dict(ho.ADDON_REST_COMMANDS, {'install': ['POST']}), \
|
||||
patch('homeassistant.components.hassio.'
|
||||
'HassIO.command_proxy') as proxy_command, \
|
||||
patch('homeassistant.components.hassio._create_response') as mresp:
|
||||
proxy_command.return_value = mock_coro(response)
|
||||
mresp.return_value = 'response'
|
||||
resp = yield from hassio_client.post('/api/hassio/addons/beer/install')
|
||||
|
||||
# Check we got right response
|
||||
assert resp.status == 200
|
||||
assert data == b"That is a test log"
|
||||
body = yield from resp.text()
|
||||
assert body == 'response'
|
||||
|
||||
assert proxy_command.mock_calls[0][1][0] == 'addons/beer/install'
|
||||
|
||||
# Check we forwarded command
|
||||
assert len(mresp.mock_calls) == 1
|
||||
assert mresp.mock_calls[0][1] == (response, 'data')
|
||||
|
||||
|
||||
@asyncio.coroutine
|
||||
def test_forward_addon_log_path(hassio_client):
|
||||
"""Test fetching addon log path."""
|
||||
response = MagicMock()
|
||||
response.read.return_value = mock_coro('data')
|
||||
|
||||
with patch.dict(ho.ADDON_REST_COMMANDS, {'logs': ['GET']}), \
|
||||
patch('homeassistant.components.hassio.'
|
||||
'HassIO.command_proxy') as proxy_command, \
|
||||
patch('homeassistant.components.hassio.'
|
||||
'_create_response_log') as mresp:
|
||||
proxy_command.return_value = mock_coro(response)
|
||||
mresp.return_value = 'response'
|
||||
resp = yield from hassio_client.get('/api/hassio/addons/beer/logs')
|
||||
|
||||
# Check we got right response
|
||||
assert resp.status == 200
|
||||
body = yield from resp.text()
|
||||
assert body == 'response'
|
||||
|
||||
assert proxy_command.mock_calls[0][1][0] == 'addons/beer/logs'
|
||||
|
||||
# Check we forwarded command
|
||||
assert len(mresp.mock_calls) == 1
|
||||
assert mresp.mock_calls[0][1] == (response, 'data')
|
||||
|
||||
|
||||
@asyncio.coroutine
|
||||
def test_bad_request_when_wrong_addon_url(hassio_client):
|
||||
"""Test we cannot mess with addon url."""
|
||||
resp = yield from hassio_client.get('/api/hassio/addons/../../info')
|
||||
assert resp.status == 404
|
||||
|
||||
resp = yield from hassio_client.get('/api/hassio/addons/info')
|
||||
assert resp.status == 404
|
||||
|
||||
|
||||
@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.async_timeout.timeout',
|
||||
side_effect=asyncio.TimeoutError):
|
||||
resp = yield from hassio_client.get('/api/hassio/addons/test/info')
|
||||
assert resp.status == 502
|
||||
Reference in new issue
Block a user