Add authentication to error log endpoint (#13836)

This commit is contained in:
Paulus Schoutsen 2018-04-13 07:32:05 -04:00 committed by Pascal Vizeli
parent d3b261a25d
commit 20ababec3e
2 changed files with 46 additions and 3 deletions

View file

@ -52,9 +52,8 @@ def setup(hass, config):
hass.http.register_view(APIComponentsView) hass.http.register_view(APIComponentsView)
hass.http.register_view(APITemplateView) hass.http.register_view(APITemplateView)
log_path = hass.data.get(DATA_LOGGING, None) if DATA_LOGGING in hass.data:
if log_path: hass.http.register_view(APIErrorLog)
hass.http.register_static_path(URL_API_ERROR_LOG, log_path, False)
return True return True
@ -356,6 +355,17 @@ class APITemplateView(HomeAssistantView):
HTTP_BAD_REQUEST) HTTP_BAD_REQUEST)
class APIErrorLog(HomeAssistantView):
"""View to fetch the error log."""
url = URL_API_ERROR_LOG
name = "api:error_log"
async def get(self, request):
"""Retrieve API error log."""
return await self.file(request, request.app['hass'].data[DATA_LOGGING])
@asyncio.coroutine @asyncio.coroutine
def async_services_json(hass): def async_services_json(hass):
"""Generate services data to JSONify.""" """Generate services data to JSONify."""

View file

@ -2,13 +2,18 @@
# pylint: disable=protected-access # pylint: disable=protected-access
import asyncio import asyncio
import json import json
from unittest.mock import patch
from aiohttp import web
import pytest import pytest
from homeassistant import const from homeassistant import const
from homeassistant.bootstrap import DATA_LOGGING
import homeassistant.core as ha import homeassistant.core as ha
from homeassistant.setup import async_setup_component from homeassistant.setup import async_setup_component
from tests.common import mock_coro
@pytest.fixture @pytest.fixture
def mock_api_client(hass, aiohttp_client): def mock_api_client(hass, aiohttp_client):
@ -398,3 +403,31 @@ def _stream_next_event(stream):
def _listen_count(hass): def _listen_count(hass):
"""Return number of event listeners.""" """Return number of event listeners."""
return sum(hass.bus.async_listeners().values()) return sum(hass.bus.async_listeners().values())
async def test_api_error_log(hass, aiohttp_client):
"""Test if we can fetch the error log."""
hass.data[DATA_LOGGING] = '/some/path'
await async_setup_component(hass, 'api', {
'http': {
'api_password': 'yolo'
}
})
client = await aiohttp_client(hass.http.app)
resp = await client.get(const.URL_API_ERROR_LOG)
# Verufy auth required
assert resp.status == 401
with patch(
'homeassistant.components.http.view.HomeAssistantView.file',
return_value=mock_coro(web.Response(status=200, text='Hello'))
) as mock_file:
resp = await client.get(const.URL_API_ERROR_LOG, headers={
'x-ha-access': 'yolo'
})
assert len(mock_file.mock_calls) == 1
assert mock_file.mock_calls[0][1][1] == hass.data[DATA_LOGGING]
assert resp.status == 200
assert await resp.text() == 'Hello'