This commit is contained in:
Paulus Schoutsen committed 2019-07-31 12:25:30 -07:00
1 parent da05dfe708
commit 4de97abc3a
2676 files changed
+163166 -140084

No files matched your search

@@ -7,7 +7,9 @@ import pytest
from homeassistant import data_entry_flow
from homeassistant.components.logi_circle import config_flow
from homeassistant.components.logi_circle.config_flow import (
DOMAIN, LogiCircleAuthCallbackView)
DOMAIN,
LogiCircleAuthCallbackView,
)
from homeassistant.setup import async_setup_component
from tests.common import MockDependency, mock_coro
@@ -17,7 +19,7 @@ class AuthorizationFailed(Exception):
"""Dummy Exception."""
class MockRequest():
class MockRequest:
"""Mock request passed to HomeAssistantView."""
def __init__(self, hass, query):
@@ -28,16 +30,19 @@ class MockRequest():
def init_config_flow(hass):
"""Init a configuration flow."""
config_flow.register_flow_implementation(hass,
DOMAIN,
client_id='id',
client_secret='secret',
api_key='123',
redirect_uri='http://example.com',
sensors=None)
config_flow.register_flow_implementation(
hass,
DOMAIN,
client_id="id",
client_secret="secret",
api_key="123",
redirect_uri="http://example.com",
sensors=None,
)
flow = config_flow.LogiCircleFlowHandler()
flow._get_authorization_url = Mock( # pylint: disable=W0212
return_value='http://example.com')
return_value="http://example.com"
)
flow.hass = hass
return flow
@@ -45,15 +50,18 @@ def init_config_flow(hass):
@pytest.fixture
def mock_logi_circle():
"""Mock logi_circle."""
with MockDependency('logi_circle', 'exception') as mock_logi_circle_:
with MockDependency("logi_circle", "exception") as mock_logi_circle_:
mock_logi_circle_.exception.AuthorizationFailed = AuthorizationFailed
mock_logi_circle_.LogiCircle().authorize = Mock(
return_value=mock_coro(return_value=True))
return_value=mock_coro(return_value=True)
)
mock_logi_circle_.LogiCircle().close = Mock(
return_value=mock_coro(return_value=True))
return_value=mock_coro(return_value=True)
)
mock_logi_circle_.LogiCircle().account = mock_coro(
return_value={'accountId': 'testId'})
mock_logi_circle_.LogiCircle().authorize_url = 'http://authorize.url'
return_value={"accountId": "testId"}
)
mock_logi_circle_.LogiCircle().authorize_url = "http://authorize.url"
yield mock_logi_circle_
@@ -62,44 +70,47 @@ async def test_step_import(hass, mock_logi_circle): # pylint: disable=W0621
flow = init_config_flow(hass)
result = await flow.async_step_import()
assert result['type'] == data_entry_flow.RESULT_TYPE_FORM
assert result['step_id'] == 'auth'
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
assert result["step_id"] == "auth"
async def test_full_flow_implementation(hass, mock_logi_circle): # noqa pylint: disable=W0621
async def test_full_flow_implementation(
hass, mock_logi_circle
): # noqa pylint: disable=W0621
"""Test registering an implementation and finishing flow works."""
config_flow.register_flow_implementation(
hass,
'test-other',
"test-other",
client_id=None,
client_secret=None,
api_key=None,
redirect_uri=None,
sensors=None)
sensors=None,
)
flow = init_config_flow(hass)
result = await flow.async_step_user()
assert result['type'] == data_entry_flow.RESULT_TYPE_FORM
assert result['step_id'] == 'user'
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
assert result["step_id"] == "user"
result = await flow.async_step_user({'flow_impl': 'test-other'})
assert result['type'] == data_entry_flow.RESULT_TYPE_FORM
assert result['step_id'] == 'auth'
assert result['description_placeholders'] == {
'authorization_url': 'http://example.com',
result = await flow.async_step_user({"flow_impl": "test-other"})
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
assert result["step_id"] == "auth"
assert result["description_placeholders"] == {
"authorization_url": "http://example.com"
}
result = await flow.async_step_code('123ABC')
assert result['type'] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
assert result['title'] == 'Logi Circle ({})'.format('testId')
result = await flow.async_step_code("123ABC")
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
assert result["title"] == "Logi Circle ({})".format("testId")
async def test_we_reprompt_user_to_follow_link(hass):
"""Test we prompt user to follow link if previously prompted."""
flow = init_config_flow(hass)
result = await flow.async_step_auth('dummy')
assert result['errors']['base'] == 'follow_link'
result = await flow.async_step_auth("dummy")
assert result["errors"]["base"] == "follow_link"
async def test_abort_if_no_implementation_registered(hass):
@@ -108,49 +119,52 @@ async def test_abort_if_no_implementation_registered(hass):
flow.hass = hass
result = await flow.async_step_user()
assert result['type'] == data_entry_flow.RESULT_TYPE_ABORT
assert result['reason'] == 'no_flows'
assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT
assert result["reason"] == "no_flows"
async def test_abort_if_already_setup(hass):
"""Test we abort if Logi Circle is already setup."""
flow = init_config_flow(hass)
with patch.object(hass.config_entries, 'async_entries', return_value=[{}]):
with patch.object(hass.config_entries, "async_entries", return_value=[{}]):
result = await flow.async_step_user()
assert result['type'] == data_entry_flow.RESULT_TYPE_ABORT
assert result['reason'] == 'already_setup'
assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT
assert result["reason"] == "already_setup"
with patch.object(hass.config_entries, 'async_entries', return_value=[{}]):
with patch.object(hass.config_entries, "async_entries", return_value=[{}]):
result = await flow.async_step_import()
assert result['type'] == data_entry_flow.RESULT_TYPE_ABORT
assert result['reason'] == 'already_setup'
assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT
assert result["reason"] == "already_setup"
with patch.object(hass.config_entries, 'async_entries', return_value=[{}]):
with patch.object(hass.config_entries, "async_entries", return_value=[{}]):
result = await flow.async_step_code()
assert result['type'] == data_entry_flow.RESULT_TYPE_ABORT
assert result['reason'] == 'already_setup'
assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT
assert result["reason"] == "already_setup"
with patch.object(hass.config_entries, 'async_entries', return_value=[{}]):
with patch.object(hass.config_entries, "async_entries", return_value=[{}]):
result = await flow.async_step_auth()
assert result['type'] == data_entry_flow.RESULT_TYPE_ABORT
assert result['reason'] == 'external_setup'
assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT
assert result["reason"] == "external_setup"
@pytest.mark.parametrize('side_effect,error',
[(asyncio.TimeoutError, 'auth_timeout'),
(AuthorizationFailed, 'auth_error')])
async def test_abort_if_authorize_fails(hass, mock_logi_circle, side_effect, error): # noqa pylint: disable=W0621
@pytest.mark.parametrize(
"side_effect,error",
[(asyncio.TimeoutError, "auth_timeout"), (AuthorizationFailed, "auth_error")],
)
async def test_abort_if_authorize_fails(
hass, mock_logi_circle, side_effect, error
): # noqa pylint: disable=W0621
"""Test we abort if authorizing fails."""
flow = init_config_flow(hass)
mock_logi_circle.LogiCircle().authorize.side_effect = side_effect
result = await flow.async_step_code('123ABC')
assert result['type'] == data_entry_flow.RESULT_TYPE_ABORT
assert result['reason'] == 'external_error'
result = await flow.async_step_code("123ABC")
assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT
assert result["reason"] == "external_error"
result = await flow.async_step_auth()
assert result['errors']['base'] == error
assert result["errors"]["base"] == error
async def test_not_pick_implementation_if_only_one(hass):
@@ -158,26 +172,28 @@ async def test_not_pick_implementation_if_only_one(hass):
flow = init_config_flow(hass)
result = await flow.async_step_user()
assert result['type'] == data_entry_flow.RESULT_TYPE_FORM
assert result['step_id'] == 'auth'
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
assert result["step_id"] == "auth"
async def test_gen_auth_url(hass, mock_logi_circle): # pylint: disable=W0621
"""Test generating authorize URL from Logi Circle API."""
config_flow.register_flow_implementation(hass,
'test-auth-url',
client_id='id',
client_secret='secret',
api_key='123',
redirect_uri='http://example.com',
sensors=None)
config_flow.register_flow_implementation(
hass,
"test-auth-url",
client_id="id",
client_secret="secret",
api_key="123",
redirect_uri="http://example.com",
sensors=None,
)
flow = config_flow.LogiCircleFlowHandler()
flow.hass = hass
flow.flow_impl = 'test-auth-url'
await async_setup_component(hass, 'http', {})
flow.flow_impl = "test-auth-url"
await async_setup_component(hass, "http", {})
result = flow._get_authorization_url() # pylint: disable=W0212
assert result == 'http://authorize.url'
assert result == "http://authorize.url"
async def test_callback_view_rejects_missing_code(hass):
@@ -188,7 +204,9 @@ async def test_callback_view_rejects_missing_code(hass):
assert resp.status == 400
async def test_callback_view_accepts_code(hass, mock_logi_circle): # noqa pylint: disable=W0621
async def test_callback_view_accepts_code(
hass, mock_logi_circle
): # noqa pylint: disable=W0621
"""Test the auth callback view handles requests with auth code."""
init_config_flow(hass)
view = LogiCircleAuthCallbackView()
@@ -197,5 +215,4 @@ async def test_callback_view_accepts_code(hass, mock_logi_circle): # noqa pylin
assert resp.status == 200
await hass.async_block_till_done()
mock_logi_circle.LogiCircle.return_value.authorize.assert_called_with(
'456')
mock_logi_circle.LogiCircle.return_value.authorize.assert_called_with("456")