Skip to content

Commit

Permalink
First unittest for view
Browse files Browse the repository at this point in the history
  • Loading branch information
pvizeli committed Apr 6, 2017
1 parent 7bb1570 commit 2999a36
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 4 deletions.
18 changes: 15 additions & 3 deletions homeassistant/components/hassio.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,13 @@ def async_setup(hass, config):
_LOGGER.error("No HassIO supervisor detect!")
return False

# register api views
for base in ('host', 'supervisor', 'network', 'homeassistant'):
# register base api views
for base in ('host', 'homeassistant'):
hass.http.register_view(HassIOBaseView(hassio, base))
for base in ('supervisor', 'network'):
hass.http.register_view(HassIOBaseEditView(hassio, base))

# register view for addons
hass.http.register_view(HassIOAddonsView(hassio))

@asyncio.coroutine
Expand Down Expand Up @@ -193,7 +197,6 @@ def __init__(self, hassio, base):
"""Initialize a hassio base view."""
self.hassio = hassio
self._url_info = "/{}/info".format(base)
self._url_options = "/{}/options".format(base)

self.url = "/api/hassio/{}".format(base)
self.name = "api:hassio:{}".format(base)
Expand All @@ -206,6 +209,15 @@ def get(self, request):
raise HTTPBadGateway()
return web.json_response(data)


class HassIOBaseEditView(HassIOBaseView):
"""HassIO view to handle base with options support."""

def __init__(self, hassio, base):
"""Initialize a hassio base edit view."""
super().__init__(hassio, base)
self._url_options = "/{}/options".format(base)

@asyncio.coroutine
def post(self, request):
"""Set options on host."""
Expand Down
34 changes: 33 additions & 1 deletion tests/components/test_hassio.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import aiohttp

import homeassistant.components.hassio as ho
from homeassistant.setup import setup_component
from homeassistant.setup import setup_component, async_setup_component

from tests.common import (
get_test_home_assistant, assert_setup_component)
Expand Down Expand Up @@ -292,3 +292,35 @@ def test_rest_command_http_addon_stop(self, aioclient_mock):
self.hass.block_till_done()

assert len(aioclient_mock.mock_calls) == 1


@asyncio.coroutine
def test_async_hassio_host_view(aioclient_mock, hass, test_client):
"""Test that it fetches the given url."""
os.environ['HASSIO'] = "127.0.0.1"

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 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'

0 comments on commit 2999a36

Please sign in to comment.