home-assistant/tests/components/ifttt/test_init.py
Paulus Schoutsen 61605ea6a2 Add tests
2018-09-27 14:38:12 +02:00

54 lines
1.9 KiB
Python

"""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
# @pytest.fixture
# def mock_client(hass, aiohttp_client):
# """Create http client for webhooks."""
# hass.loop.run_until_complete(async_setup_component(hass, 'http', {}))
# return hass.loop.run_until_complete(aiohttp_client(hass.http.app))
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'