Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions homeassistant/components/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,8 @@ def setup(hass, config):
hass.http.register_view(APIComponentsView)
hass.http.register_view(APITemplateView)

log_path = hass.data.get(DATA_LOGGING, None)
if log_path:
hass.http.register_static_path(URL_API_ERROR_LOG, log_path, False)
if DATA_LOGGING in hass.data:
hass.http.register_view(APIErrorLog)

return True

Expand Down Expand Up @@ -356,6 +355,17 @@ def post(self, 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
def async_services_json(hass):
"""Generate services data to JSONify."""
Expand Down
33 changes: 33 additions & 0 deletions tests/components/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,18 @@
# pylint: disable=protected-access
import asyncio
import json
from unittest.mock import patch

from aiohttp import web
import pytest

from homeassistant import const
from homeassistant.bootstrap import DATA_LOGGING
import homeassistant.core as ha
from homeassistant.setup import async_setup_component

from tests.common import mock_coro


@pytest.fixture
def mock_api_client(hass, aiohttp_client):
Expand Down Expand Up @@ -398,3 +403,31 @@ def _stream_next_event(stream):
def _listen_count(hass):
"""Return number of event listeners."""
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'