2016-08-25 18:55:37 +02:00
|
|
|
"""The tests for local file camera component."""
|
2016-08-20 23:04:55 -07:00
|
|
|
import unittest
|
|
|
|
from unittest import mock
|
|
|
|
|
|
|
|
from werkzeug.test import EnvironBuilder
|
|
|
|
|
|
|
|
from homeassistant.bootstrap import setup_component
|
|
|
|
from homeassistant.components.http import request_class
|
|
|
|
|
2016-10-08 20:27:35 +02:00
|
|
|
from tests.common import get_test_home_assistant, assert_setup_component
|
2016-08-20 23:04:55 -07:00
|
|
|
|
|
|
|
|
|
|
|
class TestLocalCamera(unittest.TestCase):
|
2016-08-25 18:55:37 +02:00
|
|
|
"""Test the local file camera component."""
|
2016-08-20 23:04:55 -07:00
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
"""Setup things to be run when tests are started."""
|
|
|
|
self.hass = get_test_home_assistant()
|
|
|
|
self.hass.wsgi = mock.MagicMock()
|
|
|
|
self.hass.config.components.append('http')
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
"""Stop everything that was started."""
|
|
|
|
self.hass.stop()
|
|
|
|
|
|
|
|
def test_loading_file(self):
|
|
|
|
"""Test that it loads image from disk."""
|
2016-10-17 23:16:36 -04:00
|
|
|
test_string = 'hello'
|
2016-08-20 23:04:55 -07:00
|
|
|
self.hass.wsgi = mock.MagicMock()
|
|
|
|
|
2016-10-17 23:16:36 -04:00
|
|
|
with mock.patch('os.path.isfile', mock.Mock(return_value=True)), \
|
|
|
|
mock.patch('os.access', mock.Mock(return_value=True)):
|
2016-08-20 23:04:55 -07:00
|
|
|
assert setup_component(self.hass, 'camera', {
|
|
|
|
'camera': {
|
|
|
|
'name': 'config_test',
|
|
|
|
'platform': 'local_file',
|
2016-10-17 23:16:36 -04:00
|
|
|
'file_path': 'mock.file',
|
2016-08-20 23:04:55 -07:00
|
|
|
}})
|
|
|
|
|
2016-10-17 23:16:36 -04:00
|
|
|
image_view = self.hass.wsgi.mock_calls[0][1][0]
|
2016-08-20 23:04:55 -07:00
|
|
|
|
2016-10-17 23:16:36 -04:00
|
|
|
m_open = mock.mock_open(read_data=test_string)
|
|
|
|
with mock.patch(
|
|
|
|
'homeassistant.components.camera.local_file.open',
|
|
|
|
m_open, create=True
|
|
|
|
):
|
2016-08-20 23:04:55 -07:00
|
|
|
builder = EnvironBuilder(method='GET')
|
2016-10-08 20:27:35 +02:00
|
|
|
Request = request_class() # pylint: disable=invalid-name
|
2016-08-20 23:04:55 -07:00
|
|
|
request = Request(builder.get_environ())
|
|
|
|
request.authenticated = True
|
|
|
|
resp = image_view.get(request, 'camera.config_test')
|
|
|
|
|
2016-10-17 23:16:36 -04:00
|
|
|
assert resp.status_code == 200, resp.response
|
|
|
|
assert resp.response[0].decode('utf-8') == test_string
|
2016-08-20 23:04:55 -07:00
|
|
|
|
|
|
|
def test_file_not_readable(self):
|
|
|
|
"""Test local file will not setup when file is not readable."""
|
|
|
|
self.hass.wsgi = mock.MagicMock()
|
|
|
|
|
2016-10-17 23:16:36 -04:00
|
|
|
with mock.patch('os.path.isfile', mock.Mock(return_value=True)), \
|
|
|
|
mock.patch('os.access', return_value=False), \
|
|
|
|
assert_setup_component(0):
|
|
|
|
assert setup_component(self.hass, 'camera', {
|
|
|
|
'camera': {
|
|
|
|
'name': 'config_test',
|
|
|
|
'platform': 'local_file',
|
|
|
|
'file_path': 'mock.file',
|
|
|
|
}})
|
2016-08-20 23:04:55 -07:00
|
|
|
|
2016-10-17 23:16:36 -04:00
|
|
|
assert [] == self.hass.states.all()
|