Cleanup http (#12424)

* Clean up HTTP component

* Clean up HTTP mock

* Remove unused import

* Fix test

* Lint
This commit is contained in:
Paulus Schoutsen 2018-02-15 13:06:14 -08:00 committed by Pascal Vizeli
parent ad8fe8a93a
commit f32911d036
28 changed files with 811 additions and 1014 deletions

View file

@ -4,12 +4,10 @@ import json
from unittest.mock import patch, MagicMock, mock_open
from aiohttp.hdrs import AUTHORIZATION
from homeassistant.setup import async_setup_component
from homeassistant.exceptions import HomeAssistantError
from homeassistant.util.json import save_json
from homeassistant.components.notify import html5
from tests.common import mock_http_component_app
CONFIG_FILE = 'file.conf'
SUBSCRIPTION_1 = {
@ -52,6 +50,23 @@ REGISTER_URL = '/api/notify.html5'
PUBLISH_URL = '/api/notify.html5/callback'
@asyncio.coroutine
def mock_client(hass, test_client, registrations=None):
"""Create a test client for HTML5 views."""
if registrations is None:
registrations = {}
with patch('homeassistant.components.notify.html5._load_config',
return_value=registrations):
yield from async_setup_component(hass, 'notify', {
'notify': {
'platform': 'html5'
}
})
return (yield from test_client(hass.http.app))
class TestHtml5Notify(object):
"""Tests for HTML5 notify platform."""
@ -89,8 +104,6 @@ class TestHtml5Notify(object):
service.send_message('Hello', target=['device', 'non_existing'],
data={'icon': 'beer.png'})
print(mock_wp.mock_calls)
assert len(mock_wp.mock_calls) == 3
# WebPusher constructor
@ -104,421 +117,224 @@ class TestHtml5Notify(object):
assert payload['body'] == 'Hello'
assert payload['icon'] == 'beer.png'
@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,
}
hass.config.path.return_value = CONFIG_FILE
service = html5.get_service(hass, {})
@asyncio.coroutine
def test_registering_new_device_view(hass, test_client):
"""Test that the HTML view works."""
client = yield from mock_client(hass, test_client)
assert service is not None
assert len(hass.mock_calls) == 3
view = hass.mock_calls[1][1][0]
assert view.json_path == hass.config.path.return_value
assert view.registrations == {}
hass.loop = loop
app = mock_http_component_app(hass)
view.register(app.router)
client = yield from test_client(app)
hass.http.is_banned_ip.return_value = False
with patch('homeassistant.components.notify.html5.save_json') as mock_save:
resp = yield from client.post(REGISTER_URL,
data=json.dumps(SUBSCRIPTION_1))
content = yield from resp.text()
assert resp.status == 200, content
assert view.registrations == expected
assert resp.status == 200
assert len(mock_save.mock_calls) == 1
assert mock_save.mock_calls[0][1][1] == {
'unnamed device': SUBSCRIPTION_1,
}
hass.async_add_job.assert_called_with(save_json, CONFIG_FILE, expected)
@asyncio.coroutine
def test_registering_new_device_expiration_view(self, loop, test_client):
"""Test that the HTML view works."""
hass = MagicMock()
expected = {
'unnamed device': SUBSCRIPTION_4,
}
@asyncio.coroutine
def test_registering_new_device_expiration_view(hass, test_client):
"""Test that the HTML view works."""
client = yield from mock_client(hass, test_client)
hass.config.path.return_value = CONFIG_FILE
service = html5.get_service(hass, {})
assert service is not None
# assert hass.called
assert len(hass.mock_calls) == 3
view = hass.mock_calls[1][1][0]
assert view.json_path == hass.config.path.return_value
assert view.registrations == {}
hass.loop = loop
app = mock_http_component_app(hass)
view.register(app.router)
client = yield from test_client(app)
hass.http.is_banned_ip.return_value = False
with patch('homeassistant.components.notify.html5.save_json') as mock_save:
resp = yield from client.post(REGISTER_URL,
data=json.dumps(SUBSCRIPTION_4))
content = yield from resp.text()
assert resp.status == 200, content
assert view.registrations == expected
assert resp.status == 200
assert mock_save.mock_calls[0][1][1] == {
'unnamed device': SUBSCRIPTION_4,
}
hass.async_add_job.assert_called_with(save_json, CONFIG_FILE, expected)
@asyncio.coroutine
def test_registering_new_device_fails_view(self, loop, test_client):
"""Test subs. are not altered when registering a new device fails."""
hass = MagicMock()
expected = {}
hass.config.path.return_value = CONFIG_FILE
html5.get_service(hass, {})
view = hass.mock_calls[1][1][0]
hass.loop = loop
app = mock_http_component_app(hass)
view.register(app.router)
client = yield from test_client(app)
hass.http.is_banned_ip.return_value = False
hass.async_add_job.side_effect = HomeAssistantError()
@asyncio.coroutine
def test_registering_new_device_fails_view(hass, test_client):
"""Test subs. are not altered when registering a new device fails."""
registrations = {}
client = yield from mock_client(hass, test_client, registrations)
with patch('homeassistant.components.notify.html5.save_json',
side_effect=HomeAssistantError()):
resp = yield from client.post(REGISTER_URL,
data=json.dumps(SUBSCRIPTION_1))
data=json.dumps(SUBSCRIPTION_4))
content = yield from resp.text()
assert resp.status == 500, content
assert view.registrations == expected
assert resp.status == 500
assert registrations == {}
@asyncio.coroutine
def test_registering_existing_device_view(self, loop, test_client):
"""Test subscription is updated when registering existing device."""
hass = MagicMock()
expected = {
'unnamed device': SUBSCRIPTION_4,
}
hass.config.path.return_value = CONFIG_FILE
html5.get_service(hass, {})
view = hass.mock_calls[1][1][0]
hass.loop = loop
app = mock_http_component_app(hass)
view.register(app.router)
client = yield from test_client(app)
hass.http.is_banned_ip.return_value = False
@asyncio.coroutine
def test_registering_existing_device_view(hass, test_client):
"""Test subscription is updated when registering existing device."""
registrations = {}
client = yield from mock_client(hass, test_client, registrations)
with patch('homeassistant.components.notify.html5.save_json') as mock_save:
yield from client.post(REGISTER_URL,
data=json.dumps(SUBSCRIPTION_1))
resp = yield from client.post(REGISTER_URL,
data=json.dumps(SUBSCRIPTION_4))
content = yield from resp.text()
assert resp.status == 200, content
assert view.registrations == expected
assert resp.status == 200
assert mock_save.mock_calls[0][1][1] == {
'unnamed device': SUBSCRIPTION_4,
}
assert registrations == {
'unnamed device': SUBSCRIPTION_4,
}
hass.async_add_job.assert_called_with(save_json, CONFIG_FILE, expected)
@asyncio.coroutine
def test_registering_existing_device_fails_view(self, loop, test_client):
"""Test sub. is not updated when registering existing device fails."""
hass = MagicMock()
expected = {
'unnamed device': SUBSCRIPTION_1,
}
hass.config.path.return_value = CONFIG_FILE
html5.get_service(hass, {})
view = hass.mock_calls[1][1][0]
hass.loop = loop
app = mock_http_component_app(hass)
view.register(app.router)
client = yield from test_client(app)
hass.http.is_banned_ip.return_value = False
@asyncio.coroutine
def test_registering_existing_device_fails_view(hass, test_client):
"""Test sub. is not updated when registering existing device fails."""
registrations = {}
client = yield from mock_client(hass, test_client, registrations)
with patch('homeassistant.components.notify.html5.save_json') as mock_save:
yield from client.post(REGISTER_URL,
data=json.dumps(SUBSCRIPTION_1))
hass.async_add_job.side_effect = HomeAssistantError()
mock_save.side_effect = HomeAssistantError
resp = yield from client.post(REGISTER_URL,
data=json.dumps(SUBSCRIPTION_4))
content = yield from resp.text()
assert resp.status == 500, content
assert view.registrations == expected
assert resp.status == 500
assert registrations == {
'unnamed device': SUBSCRIPTION_1,
}
@asyncio.coroutine
def test_registering_new_device_validation(self, loop, test_client):
"""Test various errors when registering a new device."""
hass = MagicMock()
m = mock_open()
with patch(
'homeassistant.util.json.open',
m, create=True
):
hass.config.path.return_value = CONFIG_FILE
service = html5.get_service(hass, {})
@asyncio.coroutine
def test_registering_new_device_validation(hass, test_client):
"""Test various errors when registering a new device."""
client = yield from mock_client(hass, test_client)
assert service is not None
resp = yield from client.post(REGISTER_URL, data=json.dumps({
'browser': 'invalid browser',
'subscription': 'sub info',
}))
assert resp.status == 400
# assert hass.called
assert len(hass.mock_calls) == 3
resp = yield from client.post(REGISTER_URL, data=json.dumps({
'browser': 'chrome',
}))
assert resp.status == 400
view = hass.mock_calls[1][1][0]
with patch('homeassistant.components.notify.html5.save_json',
return_value=False):
resp = yield from client.post(REGISTER_URL, data=json.dumps({
'browser': 'chrome',
'subscription': 'sub info',
}))
assert resp.status == 400
hass.loop = loop
app = mock_http_component_app(hass)
view.register(app.router)
client = yield from test_client(app)
hass.http.is_banned_ip.return_value = False
resp = yield from client.post(REGISTER_URL, data=json.dumps({
'browser': 'invalid browser',
'subscription': 'sub info',
}))
assert resp.status == 400
@asyncio.coroutine
def test_unregistering_device_view(hass, test_client):
"""Test that the HTML unregister view works."""
registrations = {
'some device': SUBSCRIPTION_1,
'other device': SUBSCRIPTION_2,
}
client = yield from mock_client(hass, test_client, registrations)
resp = yield from client.post(REGISTER_URL, data=json.dumps({
'browser': 'chrome',
}))
assert resp.status == 400
with patch('homeassistant.components.notify.html5.save_json') as mock_save:
resp = yield from client.delete(REGISTER_URL, data=json.dumps({
'subscription': SUBSCRIPTION_1['subscription'],
}))
with patch('homeassistant.components.notify.html5.save_json',
return_value=False):
# resp = view.post(Request(builder.get_environ()))
resp = yield from client.post(REGISTER_URL, data=json.dumps({
'browser': 'chrome',
'subscription': 'sub info',
}))
assert resp.status == 200
assert len(mock_save.mock_calls) == 1
assert registrations == {
'other device': SUBSCRIPTION_2
}
assert resp.status == 400
@asyncio.coroutine
def test_unregistering_device_view(self, loop, test_client):
"""Test that the HTML unregister view works."""
hass = MagicMock()
@asyncio.coroutine
def test_unregister_device_view_handle_unknown_subscription(hass, test_client):
"""Test that the HTML unregister view handles unknown subscriptions."""
registrations = {}
client = yield from mock_client(hass, test_client, registrations)
config = {
'some device': SUBSCRIPTION_1,
'other device': SUBSCRIPTION_2,
}
with patch('homeassistant.components.notify.html5.save_json') as mock_save:
resp = yield from client.delete(REGISTER_URL, data=json.dumps({
'subscription': SUBSCRIPTION_3['subscription']
}))
m = mock_open(read_data=json.dumps(config))
with patch(
'homeassistant.util.json.open',
m, create=True
):
hass.config.path.return_value = CONFIG_FILE
service = html5.get_service(hass, {})
assert resp.status == 200, resp.response
assert registrations == {}
assert len(mock_save.mock_calls) == 0
assert service is not None
# assert hass.called
assert len(hass.mock_calls) == 3
@asyncio.coroutine
def test_unregistering_device_view_handles_save_error(hass, test_client):
"""Test that the HTML unregister view handles save errors."""
registrations = {
'some device': SUBSCRIPTION_1,
'other device': SUBSCRIPTION_2,
}
client = yield from mock_client(hass, test_client, registrations)
view = hass.mock_calls[1][1][0]
assert view.json_path == hass.config.path.return_value
assert view.registrations == config
with patch('homeassistant.components.notify.html5.save_json',
side_effect=HomeAssistantError()):
resp = yield from client.delete(REGISTER_URL, data=json.dumps({
'subscription': SUBSCRIPTION_1['subscription'],
}))
hass.loop = loop
app = mock_http_component_app(hass)
view.register(app.router)
client = yield from test_client(app)
hass.http.is_banned_ip.return_value = False
assert resp.status == 500, resp.response
assert registrations == {
'some device': SUBSCRIPTION_1,
'other device': SUBSCRIPTION_2,
}
resp = yield from client.delete(REGISTER_URL, data=json.dumps({
'subscription': SUBSCRIPTION_1['subscription'],
}))
config.pop('some device')
@asyncio.coroutine
def test_callback_view_no_jwt(hass, test_client):
"""Test that the notification callback view works without JWT."""
client = yield from mock_client(hass, test_client)
resp = yield from client.post(PUBLISH_URL, data=json.dumps({
'type': 'push',
'tag': '3bc28d69-0921-41f1-ac6a-7a627ba0aa72'
}))
assert resp.status == 200, resp.response
assert view.registrations == config
assert resp.status == 401, resp.response
hass.async_add_job.assert_called_with(save_json, CONFIG_FILE,
config)
@asyncio.coroutine
def test_unregister_device_view_handle_unknown_subscription(
self, loop, test_client):
"""Test that the HTML unregister view handles unknown subscriptions."""
hass = MagicMock()
@asyncio.coroutine
def test_callback_view_with_jwt(hass, test_client):
"""Test that the notification callback view works with JWT."""
registrations = {
'device': SUBSCRIPTION_1
}
client = yield from mock_client(hass, test_client, registrations)
config = {
'some device': SUBSCRIPTION_1,
'other device': SUBSCRIPTION_2,
}
with patch('pywebpush.WebPusher') as mock_wp:
yield from hass.services.async_call('notify', 'notify', {
'message': 'Hello',
'target': ['device'],
'data': {'icon': 'beer.png'}
}, blocking=True)
m = mock_open(read_data=json.dumps(config))
with patch(
'homeassistant.util.json.open',
m, create=True
):
hass.config.path.return_value = CONFIG_FILE
service = html5.get_service(hass, {})
assert len(mock_wp.mock_calls) == 3
assert service is not None
# WebPusher constructor
assert mock_wp.mock_calls[0][1][0] == \
SUBSCRIPTION_1['subscription']
# Third mock_call checks the status_code of the response.
assert mock_wp.mock_calls[2][0] == '().send().status_code.__eq__'
# assert hass.called
assert len(hass.mock_calls) == 3
# Call to send
push_payload = json.loads(mock_wp.mock_calls[1][1][0])
view = hass.mock_calls[1][1][0]
assert view.json_path == hass.config.path.return_value
assert view.registrations == config
assert push_payload['body'] == 'Hello'
assert push_payload['icon'] == 'beer.png'
hass.loop = loop
app = mock_http_component_app(hass)
view.register(app.router)
client = yield from test_client(app)
hass.http.is_banned_ip.return_value = False
bearer_token = "Bearer {}".format(push_payload['data']['jwt'])
resp = yield from client.delete(REGISTER_URL, data=json.dumps({
'subscription': SUBSCRIPTION_3['subscription']
}))
resp = yield from client.post(PUBLISH_URL, json={
'type': 'push',
}, headers={AUTHORIZATION: bearer_token})
assert resp.status == 200, resp.response
assert view.registrations == config
hass.async_add_job.assert_not_called()
@asyncio.coroutine
def test_unregistering_device_view_handles_save_error(
self, loop, test_client):
"""Test that the HTML unregister view handles save errors."""
hass = MagicMock()
config = {
'some device': SUBSCRIPTION_1,
'other device': SUBSCRIPTION_2,
}
m = mock_open(read_data=json.dumps(config))
with patch(
'homeassistant.util.json.open',
m, create=True
):
hass.config.path.return_value = CONFIG_FILE
service = html5.get_service(hass, {})
assert service is not None
# assert hass.called
assert len(hass.mock_calls) == 3
view = hass.mock_calls[1][1][0]
assert view.json_path == hass.config.path.return_value
assert view.registrations == config
hass.loop = loop
app = mock_http_component_app(hass)
view.register(app.router)
client = yield from test_client(app)
hass.http.is_banned_ip.return_value = False
hass.async_add_job.side_effect = HomeAssistantError()
resp = yield from client.delete(REGISTER_URL, data=json.dumps({
'subscription': SUBSCRIPTION_1['subscription'],
}))
assert resp.status == 500, resp.response
assert view.registrations == config
@asyncio.coroutine
def test_callback_view_no_jwt(self, loop, test_client):
"""Test that the notification callback view works without JWT."""
hass = MagicMock()
m = mock_open()
with patch(
'homeassistant.util.json.open',
m, create=True
):
hass.config.path.return_value = CONFIG_FILE
service = html5.get_service(hass, {})
assert service is not None
# assert hass.called
assert len(hass.mock_calls) == 3
view = hass.mock_calls[2][1][0]
hass.loop = loop
app = mock_http_component_app(hass)
view.register(app.router)
client = yield from test_client(app)
hass.http.is_banned_ip.return_value = False
resp = yield from client.post(PUBLISH_URL, data=json.dumps({
'type': 'push',
'tag': '3bc28d69-0921-41f1-ac6a-7a627ba0aa72'
}))
assert resp.status == 401, resp.response
@asyncio.coroutine
def test_callback_view_with_jwt(self, loop, test_client):
"""Test that the notification callback view works with JWT."""
hass = MagicMock()
data = {
'device': SUBSCRIPTION_1
}
m = mock_open(read_data=json.dumps(data))
with patch(
'homeassistant.util.json.open',
m, create=True
):
hass.config.path.return_value = CONFIG_FILE
service = html5.get_service(hass, {'gcm_sender_id': '100'})
assert service is not None
# assert hass.called
assert len(hass.mock_calls) == 3
with patch('pywebpush.WebPusher') as mock_wp:
service.send_message(
'Hello', target=['device'], data={'icon': 'beer.png'})
assert len(mock_wp.mock_calls) == 3
# WebPusher constructor
assert mock_wp.mock_calls[0][1][0] == \
SUBSCRIPTION_1['subscription']
# Third mock_call checks the status_code of the response.
assert mock_wp.mock_calls[2][0] == '().send().status_code.__eq__'
# Call to send
push_payload = json.loads(mock_wp.mock_calls[1][1][0])
assert push_payload['body'] == 'Hello'
assert push_payload['icon'] == 'beer.png'
view = hass.mock_calls[2][1][0]
view.registrations = data
bearer_token = "Bearer {}".format(push_payload['data']['jwt'])
hass.loop = loop
app = mock_http_component_app(hass)
view.register(app.router)
client = yield from test_client(app)
hass.http.is_banned_ip.return_value = False
resp = yield from client.post(PUBLISH_URL, data=json.dumps({
'type': 'push',
}), headers={AUTHORIZATION: bearer_token})
assert resp.status == 200
body = yield from resp.json()
assert body == {"event": "push", "status": "ok"}
assert resp.status == 200
body = yield from resp.json()
assert body == {"event": "push", "status": "ok"}