2019-06-11 15:45:34 +00:00
|
|
|
"""Tests for the Somfy config flow."""
|
|
|
|
import asyncio
|
|
|
|
from unittest.mock import Mock, patch
|
|
|
|
|
|
|
|
from pymfy.api.somfy_api import SomfyApi
|
|
|
|
|
|
|
|
from homeassistant import data_entry_flow
|
|
|
|
from homeassistant.components.somfy import config_flow, DOMAIN
|
2019-07-31 19:25:30 +00:00
|
|
|
from homeassistant.components.somfy.config_flow import register_flow_implementation
|
2019-06-11 15:45:34 +00:00
|
|
|
from tests.common import MockConfigEntry, mock_coro
|
|
|
|
|
|
|
|
CLIENT_SECRET_VALUE = "5678"
|
|
|
|
|
|
|
|
CLIENT_ID_VALUE = "1234"
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
AUTH_URL = "http://somfy.com"
|
2019-06-11 15:45:34 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_abort_if_no_configuration(hass):
|
|
|
|
"""Check flow abort when no configuration."""
|
|
|
|
flow = config_flow.SomfyFlowHandler()
|
|
|
|
flow.hass = hass
|
|
|
|
result = await flow.async_step_user()
|
2019-07-31 19:25:30 +00:00
|
|
|
assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT
|
|
|
|
assert result["reason"] == "missing_configuration"
|
2019-06-11 15:45:34 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_abort_if_existing_entry(hass):
|
|
|
|
"""Check flow abort when an entry already exist."""
|
|
|
|
flow = config_flow.SomfyFlowHandler()
|
|
|
|
flow.hass = hass
|
|
|
|
MockConfigEntry(domain=DOMAIN).add_to_hass(hass)
|
|
|
|
result = await flow.async_step_import()
|
2019-07-31 19:25:30 +00:00
|
|
|
assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT
|
|
|
|
assert result["reason"] == "already_setup"
|
2019-06-11 15:45:34 +00:00
|
|
|
result = await flow.async_step_user()
|
2019-07-31 19:25:30 +00:00
|
|
|
assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT
|
|
|
|
assert result["reason"] == "already_setup"
|
2019-06-11 15:45:34 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_full_flow(hass):
|
|
|
|
"""Check classic use case."""
|
|
|
|
hass.data[DOMAIN] = {}
|
|
|
|
register_flow_implementation(hass, CLIENT_ID_VALUE, CLIENT_SECRET_VALUE)
|
|
|
|
flow = config_flow.SomfyFlowHandler()
|
|
|
|
flow.hass = hass
|
2019-07-31 19:25:30 +00:00
|
|
|
hass.config.api = Mock(base_url="https://example.com")
|
|
|
|
flow._get_authorization_url = Mock(return_value=mock_coro((AUTH_URL, "state")))
|
2019-06-11 15:45:34 +00:00
|
|
|
result = await flow.async_step_import()
|
2019-07-31 19:25:30 +00:00
|
|
|
assert result["type"] == data_entry_flow.RESULT_TYPE_EXTERNAL_STEP
|
|
|
|
assert result["url"] == AUTH_URL
|
2019-06-11 15:45:34 +00:00
|
|
|
result = await flow.async_step_auth("my_super_code")
|
2019-07-31 19:25:30 +00:00
|
|
|
assert result["type"] == data_entry_flow.RESULT_TYPE_EXTERNAL_STEP_DONE
|
|
|
|
assert result["step_id"] == "creation"
|
|
|
|
assert flow.code == "my_super_code"
|
|
|
|
with patch.object(
|
|
|
|
SomfyApi, "request_token", return_value={"access_token": "super_token"}
|
|
|
|
):
|
2019-06-11 15:45:34 +00:00
|
|
|
result = await flow.async_step_creation()
|
2019-07-31 19:25:30 +00:00
|
|
|
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
|
|
|
|
assert result["data"]["refresh_args"] == {
|
|
|
|
"client_id": CLIENT_ID_VALUE,
|
|
|
|
"client_secret": CLIENT_SECRET_VALUE,
|
2019-06-11 15:45:34 +00:00
|
|
|
}
|
2019-07-31 19:25:30 +00:00
|
|
|
assert result["title"] == "Somfy"
|
|
|
|
assert result["data"]["token"] == {"access_token": "super_token"}
|
2019-06-11 15:45:34 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_abort_if_authorization_timeout(hass):
|
|
|
|
"""Check Somfy authorization timeout."""
|
|
|
|
flow = config_flow.SomfyFlowHandler()
|
|
|
|
flow.hass = hass
|
|
|
|
flow._get_authorization_url = Mock(side_effect=asyncio.TimeoutError)
|
|
|
|
result = await flow.async_step_auth()
|
2019-07-31 19:25:30 +00:00
|
|
|
assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT
|
|
|
|
assert result["reason"] == "authorize_url_timeout"
|