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

+111 -109
View File
@@ -8,11 +8,14 @@ from pysmartthings import APIResponseError
from homeassistant import data_entry_flow
from homeassistant.components import cloud
from homeassistant.components.smartthings import smartapp
from homeassistant.components.smartthings.config_flow import (
SmartThingsFlowHandler)
from homeassistant.components.smartthings.config_flow import SmartThingsFlowHandler
from homeassistant.components.smartthings.const import (
CONF_INSTALLED_APP_ID, CONF_INSTALLED_APPS, CONF_LOCATION_ID,
CONF_REFRESH_TOKEN, DOMAIN)
CONF_INSTALLED_APP_ID,
CONF_INSTALLED_APPS,
CONF_LOCATION_ID,
CONF_REFRESH_TOKEN,
DOMAIN,
)
from tests.common import MockConfigEntry
@@ -23,8 +26,8 @@ async def test_step_user(hass):
flow.hass = 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"
async def test_step_init(hass):
@@ -33,31 +36,31 @@ async def test_step_init(hass):
flow.hass = hass
result = await flow.async_step_import()
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"
async def test_base_url_not_https(hass):
"""Test the base_url parameter starts with https://."""
hass.config.api.base_url = 'http://0.0.0.0'
hass.config.api.base_url = "http://0.0.0.0"
flow = SmartThingsFlowHandler()
flow.hass = hass
result = await flow.async_step_user({'access_token': str(uuid4())})
result = await flow.async_step_user({"access_token": str(uuid4())})
assert result['type'] == data_entry_flow.RESULT_TYPE_FORM
assert result['step_id'] == 'user'
assert result['errors'] == {'base': 'base_url_not_https'}
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
assert result["step_id"] == "user"
assert result["errors"] == {"base": "base_url_not_https"}
async def test_invalid_token_format(hass):
"""Test an error is shown for invalid token formats."""
flow = SmartThingsFlowHandler()
flow.hass = hass
result = await flow.async_step_user({'access_token': '123456789'})
result = await flow.async_step_user({"access_token": "123456789"})
assert result['type'] == data_entry_flow.RESULT_TYPE_FORM
assert result['step_id'] == 'user'
assert result['errors'] == {'access_token': 'token_invalid_format'}
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
assert result["step_id"] == "user"
assert result["errors"] == {"access_token": "token_invalid_format"}
async def test_token_already_setup(hass):
@@ -65,14 +68,14 @@ async def test_token_already_setup(hass):
flow = SmartThingsFlowHandler()
flow.hass = hass
token = str(uuid4())
entry = MockConfigEntry(domain=DOMAIN, data={'access_token': token})
entry = MockConfigEntry(domain=DOMAIN, data={"access_token": token})
entry.add_to_hass(hass)
result = await flow.async_step_user({'access_token': token})
result = await flow.async_step_user({"access_token": token})
assert result['type'] == data_entry_flow.RESULT_TYPE_FORM
assert result['step_id'] == 'user'
assert result['errors'] == {'access_token': 'token_already_setup'}
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
assert result["step_id"] == "user"
assert result["errors"] == {"access_token": "token_already_setup"}
async def test_token_unauthorized(hass, smartthings_mock):
@@ -80,14 +83,13 @@ async def test_token_unauthorized(hass, smartthings_mock):
flow = SmartThingsFlowHandler()
flow.hass = hass
smartthings_mock.apps.side_effect = \
ClientResponseError(None, None, status=401)
smartthings_mock.apps.side_effect = ClientResponseError(None, None, status=401)
result = await flow.async_step_user({'access_token': str(uuid4())})
result = await flow.async_step_user({"access_token": str(uuid4())})
assert result['type'] == data_entry_flow.RESULT_TYPE_FORM
assert result['step_id'] == 'user'
assert result['errors'] == {'access_token': 'token_unauthorized'}
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
assert result["step_id"] == "user"
assert result["errors"] == {"access_token": "token_unauthorized"}
async def test_token_forbidden(hass, smartthings_mock):
@@ -95,14 +97,13 @@ async def test_token_forbidden(hass, smartthings_mock):
flow = SmartThingsFlowHandler()
flow.hass = hass
smartthings_mock.apps.side_effect = \
ClientResponseError(None, None, status=403)
smartthings_mock.apps.side_effect = ClientResponseError(None, None, status=403)
result = await flow.async_step_user({'access_token': str(uuid4())})
result = await flow.async_step_user({"access_token": str(uuid4())})
assert result['type'] == data_entry_flow.RESULT_TYPE_FORM
assert result['step_id'] == 'user'
assert result['errors'] == {'access_token': 'token_forbidden'}
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
assert result["step_id"] == "user"
assert result["errors"] == {"access_token": "token_forbidden"}
async def test_webhook_error(hass, smartthings_mock):
@@ -110,17 +111,17 @@ async def test_webhook_error(hass, smartthings_mock):
flow = SmartThingsFlowHandler()
flow.hass = hass
data = {'error': {}}
data = {"error": {}}
error = APIResponseError(None, None, data=data, status=422)
error.is_target_error = Mock(return_value=True)
smartthings_mock.apps.side_effect = error
result = await flow.async_step_user({'access_token': str(uuid4())})
result = await flow.async_step_user({"access_token": str(uuid4())})
assert result['type'] == data_entry_flow.RESULT_TYPE_FORM
assert result['step_id'] == 'user'
assert result['errors'] == {'base': 'webhook_error'}
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
assert result["step_id"] == "user"
assert result["errors"] == {"base": "webhook_error"}
async def test_api_error(hass, smartthings_mock):
@@ -128,16 +129,16 @@ async def test_api_error(hass, smartthings_mock):
flow = SmartThingsFlowHandler()
flow.hass = hass
data = {'error': {}}
data = {"error": {}}
error = APIResponseError(None, None, data=data, status=400)
smartthings_mock.apps.side_effect = error
result = await flow.async_step_user({'access_token': str(uuid4())})
result = await flow.async_step_user({"access_token": str(uuid4())})
assert result['type'] == data_entry_flow.RESULT_TYPE_FORM
assert result['step_id'] == 'user'
assert result['errors'] == {'base': 'app_setup_error'}
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
assert result["step_id"] == "user"
assert result["errors"] == {"base": "app_setup_error"}
async def test_unknown_api_error(hass, smartthings_mock):
@@ -145,14 +146,13 @@ async def test_unknown_api_error(hass, smartthings_mock):
flow = SmartThingsFlowHandler()
flow.hass = hass
smartthings_mock.apps.side_effect = \
ClientResponseError(None, None, status=404)
smartthings_mock.apps.side_effect = ClientResponseError(None, None, status=404)
result = await flow.async_step_user({'access_token': str(uuid4())})
result = await flow.async_step_user({"access_token": str(uuid4())})
assert result['type'] == data_entry_flow.RESULT_TYPE_FORM
assert result['step_id'] == 'user'
assert result['errors'] == {'base': 'app_setup_error'}
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
assert result["step_id"] == "user"
assert result["errors"] == {"base": "app_setup_error"}
async def test_unknown_error(hass, smartthings_mock):
@@ -160,17 +160,18 @@ async def test_unknown_error(hass, smartthings_mock):
flow = SmartThingsFlowHandler()
flow.hass = hass
smartthings_mock.apps.side_effect = Exception('Unknown error')
smartthings_mock.apps.side_effect = Exception("Unknown error")
result = await flow.async_step_user({'access_token': str(uuid4())})
result = await flow.async_step_user({"access_token": str(uuid4())})
assert result['type'] == data_entry_flow.RESULT_TYPE_FORM
assert result['step_id'] == 'user'
assert result['errors'] == {'base': 'app_setup_error'}
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
assert result["step_id"] == "user"
assert result["errors"] == {"base": "app_setup_error"}
async def test_app_created_then_show_wait_form(
hass, app, app_oauth_client, smartthings_mock):
hass, app, app_oauth_client, smartthings_mock
):
"""Test SmartApp is created when one does not exist and shows wait form."""
flow = SmartThingsFlowHandler()
flow.hass = hass
@@ -178,22 +179,24 @@ async def test_app_created_then_show_wait_form(
smartthings_mock.apps.return_value = []
smartthings_mock.create_app.return_value = (app, app_oauth_client)
result = await flow.async_step_user({'access_token': str(uuid4())})
result = await flow.async_step_user({"access_token": str(uuid4())})
assert result['type'] == data_entry_flow.RESULT_TYPE_FORM
assert result['step_id'] == 'wait_install'
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
assert result["step_id"] == "wait_install"
async def test_cloudhook_app_created_then_show_wait_form(
hass, app, app_oauth_client, smartthings_mock):
hass, app, app_oauth_client, smartthings_mock
):
"""Test SmartApp is created with a cloudhoko and shows wait form."""
# Unload the endpoint so we can reload it under the cloud.
await smartapp.unload_smartapp_endpoint(hass)
with patch.object(cloud, 'async_active_subscription', return_value=True), \
patch.object(
cloud, 'async_create_cloudhook',
return_value='http://cloud.test') as mock_create_cloudhook:
with patch.object(
cloud, "async_active_subscription", return_value=True
), patch.object(
cloud, "async_create_cloudhook", return_value="http://cloud.test"
) as mock_create_cloudhook:
await smartapp.setup_smartapp_endpoint(hass)
@@ -202,15 +205,16 @@ async def test_cloudhook_app_created_then_show_wait_form(
smartthings_mock.apps.return_value = []
smartthings_mock.create_app.return_value = (app, app_oauth_client)
result = await flow.async_step_user({'access_token': str(uuid4())})
result = await flow.async_step_user({"access_token": str(uuid4())})
assert result['type'] == data_entry_flow.RESULT_TYPE_FORM
assert result['step_id'] == 'wait_install'
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
assert result["step_id"] == "wait_install"
assert mock_create_cloudhook.call_count == 1
async def test_app_updated_then_show_wait_form(
hass, app, app_oauth_client, smartthings_mock):
hass, app, app_oauth_client, smartthings_mock
):
"""Test SmartApp is updated when an existing is already created."""
flow = SmartThingsFlowHandler()
flow.hass = hass
@@ -218,10 +222,10 @@ async def test_app_updated_then_show_wait_form(
smartthings_mock.apps.return_value = [app]
smartthings_mock.generate_app_oauth.return_value = app_oauth_client
result = await flow.async_step_user({'access_token': str(uuid4())})
result = await flow.async_step_user({"access_token": str(uuid4())})
assert result['type'] == data_entry_flow.RESULT_TYPE_FORM
assert result['step_id'] == 'wait_install'
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
assert result["step_id"] == "wait_install"
async def test_wait_form_displayed(hass):
@@ -231,8 +235,8 @@ async def test_wait_form_displayed(hass):
result = await flow.async_step_wait_install(None)
assert result['type'] == data_entry_flow.RESULT_TYPE_FORM
assert result['step_id'] == 'wait_install'
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
assert result["step_id"] == "wait_install"
async def test_wait_form_displayed_after_checking(hass, smartthings_mock):
@@ -243,13 +247,14 @@ async def test_wait_form_displayed_after_checking(hass, smartthings_mock):
result = await flow.async_step_wait_install({})
assert result['type'] == data_entry_flow.RESULT_TYPE_FORM
assert result['step_id'] == 'wait_install'
assert result['errors'] == {'base': 'app_not_installed'}
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
assert result["step_id"] == "wait_install"
assert result["errors"] == {"base": "app_not_installed"}
async def test_config_entry_created_when_installed(
hass, location, installed_app, smartthings_mock):
hass, location, installed_app, smartthings_mock
):
"""Test a config entry is created once the app is installed."""
flow = SmartThingsFlowHandler()
flow.hass = hass
@@ -261,27 +266,27 @@ async def test_config_entry_created_when_installed(
data = {
CONF_REFRESH_TOKEN: str(uuid4()),
CONF_LOCATION_ID: installed_app.location_id,
CONF_INSTALLED_APP_ID: installed_app.installed_app_id
CONF_INSTALLED_APP_ID: installed_app.installed_app_id,
}
hass.data[DOMAIN][CONF_INSTALLED_APPS].append(data)
result = await flow.async_step_wait_install({})
assert not hass.data[DOMAIN][CONF_INSTALLED_APPS]
assert result['type'] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
assert result['data']['app_id'] == installed_app.app_id
assert result['data']['installed_app_id'] == \
installed_app.installed_app_id
assert result['data']['location_id'] == installed_app.location_id
assert result['data']['access_token'] == flow.access_token
assert result['data']['refresh_token'] == data[CONF_REFRESH_TOKEN]
assert result['data']['client_secret'] == flow.oauth_client_secret
assert result['data']['client_id'] == flow.oauth_client_id
assert result['title'] == location.name
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
assert result["data"]["app_id"] == installed_app.app_id
assert result["data"]["installed_app_id"] == installed_app.installed_app_id
assert result["data"]["location_id"] == installed_app.location_id
assert result["data"]["access_token"] == flow.access_token
assert result["data"]["refresh_token"] == data[CONF_REFRESH_TOKEN]
assert result["data"]["client_secret"] == flow.oauth_client_secret
assert result["data"]["client_id"] == flow.oauth_client_id
assert result["title"] == location.name
async def test_multiple_config_entry_created_when_installed(
hass, app, locations, installed_apps, smartthings_mock):
hass, app, locations, installed_apps, smartthings_mock
):
"""Test a config entries are created for multiple installs."""
flow = SmartThingsFlowHandler()
flow.hass = hass
@@ -294,7 +299,7 @@ async def test_multiple_config_entry_created_when_installed(
data = {
CONF_REFRESH_TOKEN: str(uuid4()),
CONF_LOCATION_ID: installed_app.location_id,
CONF_INSTALLED_APP_ID: installed_app.installed_app_id
CONF_INSTALLED_APP_ID: installed_app.installed_app_id,
}
hass.data[DOMAIN][CONF_INSTALLED_APPS].append(data)
install_data = hass.data[DOMAIN][CONF_INSTALLED_APPS].copy()
@@ -303,26 +308,23 @@ async def test_multiple_config_entry_created_when_installed(
assert not hass.data[DOMAIN][CONF_INSTALLED_APPS]
assert result['type'] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
assert result['data']['app_id'] == installed_apps[0].app_id
assert result['data']['installed_app_id'] == \
installed_apps[0].installed_app_id
assert result['data']['location_id'] == installed_apps[0].location_id
assert result['data']['access_token'] == flow.access_token
assert result['data']['refresh_token'] == \
install_data[0][CONF_REFRESH_TOKEN]
assert result['data']['client_secret'] == flow.oauth_client_secret
assert result['data']['client_id'] == flow.oauth_client_id
assert result['title'] == locations[0].name
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
assert result["data"]["app_id"] == installed_apps[0].app_id
assert result["data"]["installed_app_id"] == installed_apps[0].installed_app_id
assert result["data"]["location_id"] == installed_apps[0].location_id
assert result["data"]["access_token"] == flow.access_token
assert result["data"]["refresh_token"] == install_data[0][CONF_REFRESH_TOKEN]
assert result["data"]["client_secret"] == flow.oauth_client_secret
assert result["data"]["client_id"] == flow.oauth_client_id
assert result["title"] == locations[0].name
await hass.async_block_till_done()
entries = hass.config_entries.async_entries('smartthings')
entries = hass.config_entries.async_entries("smartthings")
assert len(entries) == 1
assert entries[0].data['app_id'] == installed_apps[1].app_id
assert entries[0].data['installed_app_id'] == \
installed_apps[1].installed_app_id
assert entries[0].data['location_id'] == installed_apps[1].location_id
assert entries[0].data['access_token'] == flow.access_token
assert entries[0].data['client_secret'] == flow.oauth_client_secret
assert entries[0].data['client_id'] == flow.oauth_client_id
assert entries[0].data["app_id"] == installed_apps[1].app_id
assert entries[0].data["installed_app_id"] == installed_apps[1].installed_app_id
assert entries[0].data["location_id"] == installed_apps[1].location_id
assert entries[0].data["access_token"] == flow.access_token
assert entries[0].data["client_secret"] == flow.oauth_client_secret
assert entries[0].data["client_id"] == flow.oauth_client_id
assert entries[0].title == locations[1].name