Cloudhooks for webhook config flows (#22611)

This commit is contained in:
Paulus Schoutsen 2019-04-01 05:07:12 -07:00 committed by Pascal Vizeli
parent c96804954c
commit 42e3e878df
9 changed files with 108 additions and 6 deletions

View file

@ -3,9 +3,9 @@ from unittest.mock import patch, Mock
import pytest
from homeassistant import config_entries, data_entry_flow, loader
from homeassistant import config_entries, data_entry_flow, loader, setup
from homeassistant.helpers import config_entry_flow
from tests.common import MockConfigEntry, MockModule
from tests.common import MockConfigEntry, MockModule, mock_coro
@pytest.fixture
@ -193,3 +193,51 @@ async def test_webhook_config_flow_registers_webhook(hass, webhook_flow_conf):
assert result['type'] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
assert result['data']['webhook_id'] is not None
async def test_webhook_create_cloudhook(hass, webhook_flow_conf):
"""Test only a single entry is allowed."""
assert await setup.async_setup_component(hass, 'cloud', {})
async_setup_entry = Mock(return_value=mock_coro(True))
async_unload_entry = Mock(return_value=mock_coro(True))
loader.set_component(hass, 'test_single', MockModule(
'test_single',
async_setup_entry=async_setup_entry,
async_unload_entry=async_unload_entry,
async_remove_entry=config_entry_flow.webhook_async_remove_entry,
))
result = await hass.config_entries.flow.async_init(
'test_single', context={'source': config_entries.SOURCE_USER})
assert result['type'] == data_entry_flow.RESULT_TYPE_FORM
coro = mock_coro({
'cloudhook_url': 'https://example.com'
})
with patch('hass_nabucasa.cloudhooks.Cloudhooks.async_create',
return_value=coro) as mock_create, \
patch('homeassistant.components.cloud.async_active_subscription',
return_value=True), \
patch('homeassistant.components.cloud.async_is_logged_in',
return_value=True):
result = await hass.config_entries.flow.async_configure(
result['flow_id'], {})
assert result['type'] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
assert result['description_placeholders']['webhook_url'] == \
'https://example.com'
assert len(mock_create.mock_calls) == 1
assert len(async_setup_entry.mock_calls) == 1
with patch('hass_nabucasa.cloudhooks.Cloudhooks.async_delete',
return_value=coro) as mock_delete:
result = \
await hass.config_entries.async_remove(result['result'].entry_id)
assert len(mock_delete.mock_calls) == 1
assert result['require_restart'] is False