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:
1 parent
9aa88819a5
commit
519d9f2fd0
45 files changed
+1422
-1009
No files matched your search
@@ -1,10 +1,10 @@
|
||||
"""Test HTML5 notify platform."""
|
||||
import asyncio
|
||||
import json
|
||||
from unittest.mock import patch, MagicMock, mock_open
|
||||
|
||||
from werkzeug.test import EnvironBuilder
|
||||
from aiohttp import web
|
||||
|
||||
from homeassistant.components.http import request_class
|
||||
from homeassistant.components.notify import html5
|
||||
|
||||
SUBSCRIPTION_1 = {
|
||||
@@ -35,6 +35,9 @@ SUBSCRIPTION_3 = {
|
||||
},
|
||||
}
|
||||
|
||||
REGISTER_URL = '/api/notify.html5'
|
||||
PUBLISH_URL = '/api/notify.html5/callback'
|
||||
|
||||
|
||||
class TestHtml5Notify(object):
|
||||
"""Tests for HTML5 notify platform."""
|
||||
@@ -94,9 +97,13 @@ class TestHtml5Notify(object):
|
||||
assert payload['body'] == 'Hello'
|
||||
assert payload['icon'] == 'beer.png'
|
||||
|
||||
def test_registering_new_device_view(self):
|
||||
@asyncio.coroutine
|
||||
def test_registering_new_device_view(self, loop, test_client):
|
||||
"""Test that the HTML view works."""
|
||||
hass = MagicMock()
|
||||
expected = {
|
||||
'unnamed device': SUBSCRIPTION_1,
|
||||
}
|
||||
|
||||
m = mock_open()
|
||||
with patch(
|
||||
@@ -114,21 +121,20 @@ class TestHtml5Notify(object):
|
||||
assert view.json_path == hass.config.path.return_value
|
||||
assert view.registrations == {}
|
||||
|
||||
builder = EnvironBuilder(method='POST',
|
||||
data=json.dumps(SUBSCRIPTION_1))
|
||||
Request = request_class()
|
||||
resp = view.post(Request(builder.get_environ()))
|
||||
app = web.Application(loop=loop)
|
||||
view.register(app.router)
|
||||
client = yield from test_client(app)
|
||||
resp = yield from client.post(REGISTER_URL,
|
||||
data=json.dumps(SUBSCRIPTION_1))
|
||||
|
||||
expected = {
|
||||
'unnamed device': SUBSCRIPTION_1,
|
||||
}
|
||||
|
||||
assert resp.status_code == 200, resp.response
|
||||
content = yield from resp.text()
|
||||
assert resp.status == 200, content
|
||||
assert view.registrations == expected
|
||||
handle = m()
|
||||
assert json.loads(handle.write.call_args[0][0]) == expected
|
||||
|
||||
def test_registering_new_device_validation(self):
|
||||
@asyncio.coroutine
|
||||
def test_registering_new_device_validation(self, loop, test_client):
|
||||
"""Test various errors when registering a new device."""
|
||||
hass = MagicMock()
|
||||
|
||||
@@ -146,34 +152,34 @@ class TestHtml5Notify(object):
|
||||
|
||||
view = hass.mock_calls[1][1][0]
|
||||
|
||||
Request = request_class()
|
||||
app = web.Application(loop=loop)
|
||||
view.register(app.router)
|
||||
client = yield from test_client(app)
|
||||
|
||||
builder = EnvironBuilder(method='POST', data=json.dumps({
|
||||
resp = yield from client.post(REGISTER_URL, data=json.dumps({
|
||||
'browser': 'invalid browser',
|
||||
'subscription': 'sub info',
|
||||
}))
|
||||
resp = view.post(Request(builder.get_environ()))
|
||||
assert resp.status_code == 400, resp.response
|
||||
assert resp.status == 400
|
||||
|
||||
builder = EnvironBuilder(method='POST', data=json.dumps({
|
||||
resp = yield from client.post(REGISTER_URL, data=json.dumps({
|
||||
'browser': 'chrome',
|
||||
}))
|
||||
resp = view.post(Request(builder.get_environ()))
|
||||
assert resp.status_code == 400, resp.response
|
||||
assert resp.status == 400
|
||||
|
||||
builder = EnvironBuilder(method='POST', data=json.dumps({
|
||||
'browser': 'chrome',
|
||||
'subscription': 'sub info',
|
||||
}))
|
||||
with patch('homeassistant.components.notify.html5._save_config',
|
||||
return_value=False):
|
||||
resp = view.post(Request(builder.get_environ()))
|
||||
assert resp.status_code == 400, resp.response
|
||||
# resp = view.post(Request(builder.get_environ()))
|
||||
resp = yield from client.post(REGISTER_URL, data=json.dumps({
|
||||
'browser': 'chrome',
|
||||
'subscription': 'sub info',
|
||||
}))
|
||||
|
||||
@patch('homeassistant.components.notify.html5.os')
|
||||
def test_unregistering_device_view(self, mock_os):
|
||||
assert resp.status == 400
|
||||
|
||||
@asyncio.coroutine
|
||||
def test_unregistering_device_view(self, loop, test_client):
|
||||
"""Test that the HTML unregister view works."""
|
||||
mock_os.path.isfile.return_value = True
|
||||
hass = MagicMock()
|
||||
|
||||
config = {
|
||||
@@ -182,11 +188,14 @@ class TestHtml5Notify(object):
|
||||
}
|
||||
|
||||
m = mock_open(read_data=json.dumps(config))
|
||||
with patch(
|
||||
'homeassistant.components.notify.html5.open', m, create=True
|
||||
):
|
||||
|
||||
with patch('homeassistant.components.notify.html5.open', m,
|
||||
create=True):
|
||||
hass.config.path.return_value = 'file.conf'
|
||||
service = html5.get_service(hass, {})
|
||||
|
||||
with patch('homeassistant.components.notify.html5.os.path.isfile',
|
||||
return_value=True):
|
||||
service = html5.get_service(hass, {})
|
||||
|
||||
assert service is not None
|
||||
|
||||
@@ -197,23 +206,25 @@ class TestHtml5Notify(object):
|
||||
assert view.json_path == hass.config.path.return_value
|
||||
assert view.registrations == config
|
||||
|
||||
builder = EnvironBuilder(method='DELETE', data=json.dumps({
|
||||
app = web.Application(loop=loop)
|
||||
view.register(app.router)
|
||||
client = yield from test_client(app)
|
||||
|
||||
resp = yield from client.delete(REGISTER_URL, data=json.dumps({
|
||||
'subscription': SUBSCRIPTION_1['subscription'],
|
||||
}))
|
||||
Request = request_class()
|
||||
resp = view.delete(Request(builder.get_environ()))
|
||||
|
||||
config.pop('some device')
|
||||
|
||||
assert resp.status_code == 200, resp.response
|
||||
assert resp.status == 200, resp.response
|
||||
assert view.registrations == config
|
||||
handle = m()
|
||||
assert json.loads(handle.write.call_args[0][0]) == config
|
||||
|
||||
@patch('homeassistant.components.notify.html5.os')
|
||||
def test_unregister_device_view_handle_unknown_subscription(self, mock_os):
|
||||
@asyncio.coroutine
|
||||
def test_unregister_device_view_handle_unknown_subscription(self, loop,
|
||||
test_client):
|
||||
"""Test that the HTML unregister view handles unknown subscriptions."""
|
||||
mock_os.path.isfile.return_value = True
|
||||
hass = MagicMock()
|
||||
|
||||
config = {
|
||||
@@ -226,7 +237,9 @@ class TestHtml5Notify(object):
|
||||
'homeassistant.components.notify.html5.open', m, create=True
|
||||
):
|
||||
hass.config.path.return_value = 'file.conf'
|
||||
service = html5.get_service(hass, {})
|
||||
with patch('homeassistant.components.notify.html5.os.path.isfile',
|
||||
return_value=True):
|
||||
service = html5.get_service(hass, {})
|
||||
|
||||
assert service is not None
|
||||
|
||||
@@ -237,21 +250,23 @@ class TestHtml5Notify(object):
|
||||
assert view.json_path == hass.config.path.return_value
|
||||
assert view.registrations == config
|
||||
|
||||
builder = EnvironBuilder(method='DELETE', data=json.dumps({
|
||||
app = web.Application(loop=loop)
|
||||
view.register(app.router)
|
||||
client = yield from test_client(app)
|
||||
|
||||
resp = yield from client.delete(REGISTER_URL, data=json.dumps({
|
||||
'subscription': SUBSCRIPTION_3['subscription']
|
||||
}))
|
||||
Request = request_class()
|
||||
resp = view.delete(Request(builder.get_environ()))
|
||||
|
||||
assert resp.status_code == 200, resp.response
|
||||
assert resp.status == 200, resp.response
|
||||
assert view.registrations == config
|
||||
handle = m()
|
||||
assert handle.write.call_count == 0
|
||||
|
||||
@patch('homeassistant.components.notify.html5.os')
|
||||
def test_unregistering_device_view_handles_json_safe_error(self, mock_os):
|
||||
@asyncio.coroutine
|
||||
def test_unregistering_device_view_handles_json_safe_error(self, loop,
|
||||
test_client):
|
||||
"""Test that the HTML unregister view handles JSON write errors."""
|
||||
mock_os.path.isfile.return_value = True
|
||||
hass = MagicMock()
|
||||
|
||||
config = {
|
||||
@@ -264,7 +279,9 @@ class TestHtml5Notify(object):
|
||||
'homeassistant.components.notify.html5.open', m, create=True
|
||||
):
|
||||
hass.config.path.return_value = 'file.conf'
|
||||
service = html5.get_service(hass, {})
|
||||
with patch('homeassistant.components.notify.html5.os.path.isfile',
|
||||
return_value=True):
|
||||
service = html5.get_service(hass, {})
|
||||
|
||||
assert service is not None
|
||||
|
||||
@@ -275,21 +292,23 @@ class TestHtml5Notify(object):
|
||||
assert view.json_path == hass.config.path.return_value
|
||||
assert view.registrations == config
|
||||
|
||||
builder = EnvironBuilder(method='DELETE', data=json.dumps({
|
||||
'subscription': SUBSCRIPTION_1['subscription'],
|
||||
}))
|
||||
Request = request_class()
|
||||
app = web.Application(loop=loop)
|
||||
view.register(app.router)
|
||||
client = yield from test_client(app)
|
||||
|
||||
with patch('homeassistant.components.notify.html5._save_config',
|
||||
return_value=False):
|
||||
resp = view.delete(Request(builder.get_environ()))
|
||||
resp = yield from client.delete(REGISTER_URL, data=json.dumps({
|
||||
'subscription': SUBSCRIPTION_1['subscription'],
|
||||
}))
|
||||
|
||||
assert resp.status_code == 500, resp.response
|
||||
assert resp.status == 500, resp.response
|
||||
assert view.registrations == config
|
||||
handle = m()
|
||||
assert handle.write.call_count == 0
|
||||
|
||||
def test_callback_view_no_jwt(self):
|
||||
@asyncio.coroutine
|
||||
def test_callback_view_no_jwt(self, loop, test_client):
|
||||
"""Test that the notification callback view works without JWT."""
|
||||
hass = MagicMock()
|
||||
|
||||
@@ -307,20 +326,20 @@ class TestHtml5Notify(object):
|
||||
|
||||
view = hass.mock_calls[2][1][0]
|
||||
|
||||
builder = EnvironBuilder(method='POST', data=json.dumps({
|
||||
app = web.Application(loop=loop)
|
||||
view.register(app.router)
|
||||
client = yield from test_client(app)
|
||||
|
||||
resp = yield from client.post(PUBLISH_URL, data=json.dumps({
|
||||
'type': 'push',
|
||||
'tag': '3bc28d69-0921-41f1-ac6a-7a627ba0aa72'
|
||||
}))
|
||||
Request = request_class()
|
||||
resp = view.post(Request(builder.get_environ()))
|
||||
|
||||
assert resp.status_code == 401, resp.response
|
||||
assert resp.status == 401, resp.response
|
||||
|
||||
@patch('homeassistant.components.notify.html5.os')
|
||||
@patch('pywebpush.WebPusher')
|
||||
def test_callback_view_with_jwt(self, mock_wp, mock_os):
|
||||
@asyncio.coroutine
|
||||
def test_callback_view_with_jwt(self, loop, test_client):
|
||||
"""Test that the notification callback view works with JWT."""
|
||||
mock_os.path.isfile.return_value = True
|
||||
hass = MagicMock()
|
||||
|
||||
data = {
|
||||
@@ -332,15 +351,18 @@ class TestHtml5Notify(object):
|
||||
'homeassistant.components.notify.html5.open', m, create=True
|
||||
):
|
||||
hass.config.path.return_value = 'file.conf'
|
||||
service = html5.get_service(hass, {'gcm_sender_id': '100'})
|
||||
with patch('homeassistant.components.notify.html5.os.path.isfile',
|
||||
return_value=True):
|
||||
service = html5.get_service(hass, {'gcm_sender_id': '100'})
|
||||
|
||||
assert service is not None
|
||||
|
||||
# assert hass.called
|
||||
assert len(hass.mock_calls) == 3
|
||||
|
||||
service.send_message('Hello', target=['device'],
|
||||
data={'icon': 'beer.png'})
|
||||
with patch('pywebpush.WebPusher') as mock_wp:
|
||||
service.send_message('Hello', target=['device'],
|
||||
data={'icon': 'beer.png'})
|
||||
|
||||
assert len(mock_wp.mock_calls) == 2
|
||||
|
||||
@@ -359,13 +381,14 @@ class TestHtml5Notify(object):
|
||||
|
||||
bearer_token = "Bearer {}".format(push_payload['data']['jwt'])
|
||||
|
||||
builder = EnvironBuilder(method='POST', data=json.dumps({
|
||||
app = web.Application(loop=loop)
|
||||
view.register(app.router)
|
||||
client = yield from test_client(app)
|
||||
|
||||
resp = yield from client.post(PUBLISH_URL, data=json.dumps({
|
||||
'type': 'push',
|
||||
}), headers={'Authorization': bearer_token})
|
||||
Request = request_class()
|
||||
resp = view.post(Request(builder.get_environ()))
|
||||
|
||||
assert resp.status_code == 200, resp.response
|
||||
returned = resp.response[0].decode('utf-8')
|
||||
expected = '{"event": "push", "status": "ok"}'
|
||||
assert json.loads(returned) == json.loads(expected)
|
||||
assert resp.status == 200
|
||||
body = yield from resp.json()
|
||||
assert body == {"event": "push", "status": "ok"}
|
||||
Reference in new issue
Block a user