async HTTP component (#3914)

* Migrate WSGI to asyncio

* Rename wsgi -> http

* Python 3.4 compat

* Move linting to Python 3.4

* lint

* Lint

* Fix Python 3.4 mock_open + binary data

* Surpress logging aiohttp.access

* Spelling

* Sending files is a coroutine

* More callback annotations and naming fixes

* Fix ios
This commit is contained in:
Paulus Schoutsen authored and GitHub committed 2016-10-23 23:48:01 -07:00
1 parent 9aa88819a5
commit 519d9f2fd0
45 files changed
+1422 -1009

No files matched your search

+37 -47
View File
@@ -1,70 +1,60 @@
"""The tests for local file camera component."""
import unittest
import asyncio
from unittest import mock
from werkzeug.test import EnvironBuilder
# 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
from homeassistant.bootstrap import setup_component
from homeassistant.components.http import request_class
from tests.common import get_test_home_assistant, assert_setup_component
from tests.common import assert_setup_component, mock_http_component
class TestLocalCamera(unittest.TestCase):
"""Test the local file camera component."""
@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',
}})
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')
yield from hass.loop.run_in_executor(None, setup_platform)
def tearDown(self):
"""Stop everything that was started."""
self.hass.stop()
client = yield from test_client(hass.http.app)
def test_loading_file(self):
"""Test that it loads image from disk."""
test_string = 'hello'
self.hass.wsgi = mock.MagicMock()
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')
with mock.patch('os.path.isfile', mock.Mock(return_value=True)), \
mock.patch('os.access', mock.Mock(return_value=True)):
assert setup_component(self.hass, 'camera', {
'camera': {
'name': 'config_test',
'platform': 'local_file',
'file_path': 'mock.file',
}})
assert resp.status == 200
body = yield from resp.text()
assert body == 'hello'
image_view = self.hass.wsgi.mock_calls[0][1][0]
m_open = mock.mock_open(read_data=test_string)
with mock.patch(
'homeassistant.components.camera.local_file.open',
m_open, create=True
):
builder = EnvironBuilder(method='GET')
Request = request_class() # pylint: disable=invalid-name
request = Request(builder.get_environ())
request.authenticated = True
resp = image_view.get(request, 'camera.config_test')
assert resp.status_code == 200, resp.response
assert resp.response[0].decode('utf-8') == test_string
def test_file_not_readable(self):
"""Test local file will not setup when file is not readable."""
self.hass.wsgi = mock.MagicMock()
@asyncio.coroutine
def test_file_not_readable(hass):
"""Test local file will not setup when file is not readable."""
mock_http_component(hass)
def run_test():
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', {
assert_setup_component(0, 'camera'):
assert setup_component(hass, 'camera', {
'camera': {
'name': 'config_test',
'platform': 'local_file',
'file_path': 'mock.file',
}})
assert [] == self.hass.states.all()
yield from hass.loop.run_in_executor(None, run_test)