2016-08-25 16:55:37 +00:00
|
|
|
"""The tests for local file camera component."""
|
2016-10-24 06:48:01 +00:00
|
|
|
import asyncio
|
2016-08-21 06:04:55 +00:00
|
|
|
from unittest import mock
|
|
|
|
|
2016-10-24 06:48:01 +00:00
|
|
|
# Using third party package because of a bug reading binary data in Python 3.4
|
|
|
|
# https://bugs.python.org/issue23004
|
|
|
|
from mock_open import MockOpen
|
2016-08-21 06:04:55 +00:00
|
|
|
|
2017-03-05 09:41:54 +00:00
|
|
|
from homeassistant.setup import setup_component
|
2016-08-21 06:04:55 +00:00
|
|
|
|
2017-02-27 05:16:11 +00:00
|
|
|
from tests.common import mock_http_component
|
|
|
|
import logging
|
2016-08-21 06:04:55 +00:00
|
|
|
|
|
|
|
|
2016-10-24 06:48:01 +00:00
|
|
|
@asyncio.coroutine
|
|
|
|
def test_loading_file(hass, test_client):
|
|
|
|
"""Test that it loads image from disk."""
|
|
|
|
@mock.patch('os.path.isfile', mock.Mock(return_value=True))
|
|
|
|
@mock.patch('os.access', mock.Mock(return_value=True))
|
|
|
|
def setup_platform():
|
|
|
|
"""Setup platform inside callback."""
|
|
|
|
assert setup_component(hass, 'camera', {
|
|
|
|
'camera': {
|
|
|
|
'name': 'config_test',
|
|
|
|
'platform': 'local_file',
|
|
|
|
'file_path': 'mock.file',
|
|
|
|
}})
|
2016-08-21 06:04:55 +00:00
|
|
|
|
2016-10-24 06:48:01 +00:00
|
|
|
yield from hass.loop.run_in_executor(None, setup_platform)
|
2016-08-21 06:04:55 +00:00
|
|
|
|
2016-10-24 06:48:01 +00:00
|
|
|
client = yield from test_client(hass.http.app)
|
2016-08-21 06:04:55 +00:00
|
|
|
|
2016-10-24 06:48:01 +00:00
|
|
|
m_open = MockOpen(read_data=b'hello')
|
|
|
|
with mock.patch(
|
|
|
|
'homeassistant.components.camera.local_file.open',
|
|
|
|
m_open, create=True
|
|
|
|
):
|
|
|
|
resp = yield from client.get('/api/camera_proxy/camera.config_test')
|
2016-08-21 06:04:55 +00:00
|
|
|
|
2016-10-24 06:48:01 +00:00
|
|
|
assert resp.status == 200
|
|
|
|
body = yield from resp.text()
|
|
|
|
assert body == 'hello'
|
2016-08-21 06:04:55 +00:00
|
|
|
|
|
|
|
|
2016-10-24 06:48:01 +00:00
|
|
|
@asyncio.coroutine
|
2017-02-27 05:16:11 +00:00
|
|
|
def test_file_not_readable(hass, caplog):
|
|
|
|
"""Test a warning is shown setup when file is not readable."""
|
2016-10-24 06:48:01 +00:00
|
|
|
mock_http_component(hass)
|
2016-08-21 06:04:55 +00:00
|
|
|
|
2017-02-27 05:16:11 +00:00
|
|
|
@mock.patch('os.path.isfile', mock.Mock(return_value=True))
|
|
|
|
@mock.patch('os.access', mock.Mock(return_value=False))
|
2016-10-24 06:48:01 +00:00
|
|
|
def run_test():
|
2017-02-27 05:16:11 +00:00
|
|
|
|
|
|
|
caplog.set_level(
|
|
|
|
logging.WARNING, logger='requests.packages.urllib3.connectionpool')
|
|
|
|
|
|
|
|
assert setup_component(hass, 'camera', {
|
|
|
|
'camera': {
|
|
|
|
'name': 'config_test',
|
|
|
|
'platform': 'local_file',
|
|
|
|
'file_path': 'mock.file',
|
|
|
|
}})
|
|
|
|
assert 'Could not read' in caplog.text
|
|
|
|
assert 'config_test' in caplog.text
|
|
|
|
assert 'mock.file' in caplog.text
|
2016-08-21 06:04:55 +00:00
|
|
|
|
2016-10-24 06:48:01 +00:00
|
|
|
yield from hass.loop.run_in_executor(None, run_test)
|