Add webhook + IFTTT example (#16817)

* Add webhook + IFTTT example

* Abort if not externally accessible

* Abort on local url

* Add description to create entry

* Make body optional

* Allow ifttt setup without config

* Add tests

* Lint

* Fix Lint + Tests

* Fix typing
This commit is contained in:
Paulus Schoutsen authored and GitHub committed 2018-09-30 14:45:48 +02:00
1 parent 06d959ed43
commit f5632a5da5
13 files changed
+448 -91

No files matched your search

@@ -206,6 +206,8 @@ def test_create_account(hass, client):
'title': 'Test Entry',
'type': 'create_entry',
'version': 1,
'description': None,
'description_placeholders': None,
}
@@ -266,6 +268,8 @@ def test_two_step_flow(hass, client):
'type': 'create_entry',
'title': 'user-title',
'version': 1,
'description': None,
'description_placeholders': None,
}
+1
View File
@@ -0,0 +1 @@
"""Tests for the IFTTT component."""
+48
View File
@@ -0,0 +1,48 @@
"""Test the init file of IFTTT."""
from unittest.mock import Mock, patch
from homeassistant import data_entry_flow
from homeassistant.core import callback
from homeassistant.components import ifttt
async def test_config_flow_registers_webhook(hass, aiohttp_client):
"""Test setting up IFTTT and sending webhook."""
with patch('homeassistant.util.get_local_ip', return_value='example.com'):
result = await hass.config_entries.flow.async_init('ifttt', context={
'source': 'user'
})
assert result['type'] == data_entry_flow.RESULT_TYPE_FORM, result
result = await hass.config_entries.flow.async_configure(
result['flow_id'], {})
assert result['type'] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
webhook_id = result['result'].data['webhook_id']
ifttt_events = []
@callback
def handle_event(event):
"""Handle IFTTT event."""
ifttt_events.append(event)
hass.bus.async_listen(ifttt.EVENT_RECEIVED, handle_event)
client = await aiohttp_client(hass.http.app)
await client.post('/api/webhook/{}'.format(webhook_id), json={
'hello': 'ifttt'
})
assert len(ifttt_events) == 1
assert ifttt_events[0].data['webhook_id'] == webhook_id
assert ifttt_events[0].data['hello'] == 'ifttt'
async def test_config_flow_aborts_external_url(hass, aiohttp_client):
"""Test setting up IFTTT and sending webhook."""
hass.config.api = Mock(base_url='http://192.168.1.10')
result = await hass.config_entries.flow.async_init('ifttt', context={
'source': 'user'
})
assert result['type'] == data_entry_flow.RESULT_TYPE_ABORT
assert result['reason'] == 'not_internet_accessible'
+98
View File
@@ -0,0 +1,98 @@
"""Test the webhook component."""
from unittest.mock import Mock
import pytest
from homeassistant.setup import async_setup_component
@pytest.fixture
def mock_client(hass, aiohttp_client):
"""Create http client for webhooks."""
hass.loop.run_until_complete(async_setup_component(hass, 'webhook', {}))
return hass.loop.run_until_complete(aiohttp_client(hass.http.app))
async def test_unregistering_webhook(hass, mock_client):
"""Test unregistering a webhook."""
hooks = []
webhook_id = hass.components.webhook.async_generate_id()
async def handle(*args):
"""Handle webhook."""
hooks.append(args)
hass.components.webhook.async_register(webhook_id, handle)
resp = await mock_client.post('/api/webhook/{}'.format(webhook_id))
assert resp.status == 200
assert len(hooks) == 1
hass.components.webhook.async_unregister(webhook_id)
resp = await mock_client.post('/api/webhook/{}'.format(webhook_id))
assert resp.status == 200
assert len(hooks) == 1
async def test_generate_webhook_url(hass):
"""Test we generate a webhook url correctly."""
hass.config.api = Mock(base_url='https://example.com')
url = hass.components.webhook.async_generate_url('some_id')
assert url == 'https://example.com/api/webhook/some_id'
async def test_posting_webhook_nonexisting(hass, mock_client):
"""Test posting to a nonexisting webhook."""
resp = await mock_client.post('/api/webhook/non-existing')
assert resp.status == 200
async def test_posting_webhook_invalid_json(hass, mock_client):
"""Test posting to a nonexisting webhook."""
hass.components.webhook.async_register('hello', None)
resp = await mock_client.post('/api/webhook/hello', data='not-json')
assert resp.status == 200
async def test_posting_webhook_json(hass, mock_client):
"""Test posting a webhook with JSON data."""
hooks = []
webhook_id = hass.components.webhook.async_generate_id()
async def handle(*args):
"""Handle webhook."""
hooks.append(args)
hass.components.webhook.async_register(webhook_id, handle)
resp = await mock_client.post('/api/webhook/{}'.format(webhook_id), json={
'data': True
})
assert resp.status == 200
assert len(hooks) == 1
assert hooks[0][0] is hass
assert hooks[0][1] == webhook_id
assert hooks[0][2] == {
'data': True
}
async def test_posting_webhook_no_data(hass, mock_client):
"""Test posting a webhook with no data."""
hooks = []
webhook_id = hass.components.webhook.async_generate_id()
async def handle(*args):
"""Handle webhook."""
hooks.append(args)
hass.components.webhook.async_register(webhook_id, handle)
resp = await mock_client.post('/api/webhook/{}'.format(webhook_id))
assert resp.status == 200
assert len(hooks) == 1
assert hooks[0][0] is hass
assert hooks[0][1] == webhook_id
assert hooks[0][2] == {}