コード例 #1
0
async def test_cloudhook_app_created_then_show_wait_form(
        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)

    mock_async_active_subscription = Mock(return_value=True)
    mock_create_cloudhook = Mock(return_value=mock_coro(
        return_value="http://cloud.test"))
    with patch.object(cloud, 'async_active_subscription',
                      new=mock_async_active_subscription), \
        patch.object(cloud, 'async_create_cloudhook',
                     new=mock_create_cloudhook):

        await smartapp.setup_smartapp_endpoint(hass)

        flow = SmartThingsFlowHandler()
        flow.hass = hass
        smartthings = smartthings_mock.return_value
        smartthings.apps.return_value = mock_coro(return_value=[])
        smartthings.create_app.return_value = \
            mock_coro(return_value=(app, app_oauth_client))
        smartthings.update_app_settings.return_value = mock_coro()
        smartthings.update_app_oauth.return_value = mock_coro()

        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 mock_create_cloudhook.call_count == 1
コード例 #2
0
async def test_multiple_config_entry_created_when_installed(
        hass, app, locations, installed_apps, smartthings_mock):
    """Test a config entries are created for multiple installs."""
    flow = SmartThingsFlowHandler()
    flow.hass = hass
    flow.access_token = str(uuid4())
    flow.app_id = app.app_id
    flow.api = smartthings_mock.return_value
    flow.api.installed_apps.return_value = \
        mock_coro(return_value=installed_apps)

    result = await flow.async_step_wait_install({})

    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['title'] == locations[0].name

    await hass.async_block_till_done()
    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].title == locations[1].name
コード例 #3
0
async def test_step_init(hass):
    """Test the access token form is shown for an init flow."""
    flow = SmartThingsFlowHandler()
    flow.hass = hass
    result = await flow.async_step_import()

    assert result['type'] == data_entry_flow.RESULT_TYPE_FORM
    assert result['step_id'] == 'user'
コード例 #4
0
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'})

    assert result['type'] == data_entry_flow.RESULT_TYPE_FORM
    assert result['step_id'] == 'user'
    assert result['errors'] == {'access_token': 'token_invalid_format'}
コード例 #5
0
async def test_wait_form_displayed(hass):
    """Test the wait for installation form is displayed."""
    flow = SmartThingsFlowHandler()
    flow.hass = hass

    result = await flow.async_step_wait_install(None)

    assert result['type'] == data_entry_flow.RESULT_TYPE_FORM
    assert result['step_id'] == 'wait_install'
コード例 #6
0
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'
    flow = SmartThingsFlowHandler()
    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['errors'] == {'base': 'base_url_not_https'}
コード例 #7
0
async def test_wait_form_displayed_after_checking(hass, smartthings_mock):
    """Test error is shown when the user has not installed the app."""
    flow = SmartThingsFlowHandler()
    flow.hass = hass
    flow.access_token = str(uuid4())

    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'}
コード例 #8
0
async def test_token_forbidden(hass, smartthings_mock):
    """Test an error is shown when the token is forbidden."""
    flow = SmartThingsFlowHandler()
    flow.hass = hass

    smartthings_mock.return_value.apps.return_value = mock_coro(
        exception=ClientResponseError(None, None, status=403))

    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'}
コード例 #9
0
async def test_unknown_error(hass, smartthings_mock):
    """Test an error is shown when there is an unknown API error."""
    flow = SmartThingsFlowHandler()
    flow.hass = hass

    smartthings_mock.return_value.apps.return_value = mock_coro(
        exception=Exception('Unknown error'))

    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'}
コード例 #10
0
async def test_app_updated_then_show_wait_form(
        hass, app, smartthings_mock):
    """Test SmartApp is updated when an existing is already created."""
    flow = SmartThingsFlowHandler()
    flow.hass = hass

    api = smartthings_mock.return_value
    api.apps.return_value = mock_coro(return_value=[app])

    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'
コード例 #11
0
async def test_api_error(hass, smartthings_mock):
    """Test an error is shown when other API errors occur."""
    flow = SmartThingsFlowHandler()
    flow.hass = hass

    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())})

    assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
    assert result["step_id"] == "user"
    assert result["errors"] == {"base": "app_setup_error"}
コード例 #12
0
ファイル: test_config_flow.py プロジェクト: mmmahammm/core
async def test_app_created_then_show_wait_form(hass, app, smartthings_mock):
    """Test SmartApp is created when one does not exist and shows wait form."""
    flow = SmartThingsFlowHandler()
    flow.hass = hass

    smartthings = smartthings_mock.return_value
    smartthings.apps.return_value = mock_coro(return_value=[])
    smartthings.create_app.return_value = mock_coro(return_value=(app, None))
    smartthings.update_app_settings.return_value = mock_coro()
    smartthings.update_app_oauth.return_value = mock_coro()

    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'
コード例 #13
0
async def test_step_select_location_advances(hass):
    """Test select location aborts if no available locations."""
    location_id = str(uuid4())
    app_id = str(uuid4())
    flow = SmartThingsFlowHandler()
    flow.hass = hass
    flow.app_id = app_id

    result = await flow.async_step_select_location(
        {CONF_LOCATION_ID: location_id})

    assert flow.location_id == location_id
    assert result["type"] == data_entry_flow.RESULT_TYPE_EXTERNAL_STEP
    assert result["step_id"] == "authorize"
    assert result["url"] == format_install_url(app_id, location_id)
コード例 #14
0
async def test_step_select_location(hass, location, smartthings_mock):
    """Test select location shows form with available locations."""
    smartthings_mock.locations.return_value = [location]
    flow = SmartThingsFlowHandler()
    flow.hass = hass
    flow.api = smartthings_mock

    result = await flow.async_step_select_location()
    assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
    assert result["step_id"] == "select_location"
    assert result["data_schema"]({
        CONF_LOCATION_ID: location.location_id
    }) == {
        CONF_LOCATION_ID: location.location_id
    }
コード例 #15
0
async def test_step_pat_unknown_api_error(hass, smartthings_mock):
    """Test an error is shown when there is an unknown API error."""
    flow = SmartThingsFlowHandler()
    flow.hass = hass
    request_info = Mock(real_url="http://example.com")
    smartthings_mock.apps.side_effect = ClientResponseError(
        request_info=request_info, history=None, status=HTTP_NOT_FOUND)
    token = str(uuid4())

    result = await flow.async_step_pat({CONF_ACCESS_TOKEN: token})

    assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
    assert result["step_id"] == "pat"
    assert result["errors"] == {"base": "app_setup_error"}
    assert result["data_schema"]({}) == {CONF_ACCESS_TOKEN: token}
コード例 #16
0
async def test_app_updated_then_show_wait_form(hass, app, app_oauth_client,
                                               smartthings_mock):
    """Test SmartApp is updated when an existing is already created."""
    flow = SmartThingsFlowHandler()
    flow.hass = hass

    api = smartthings_mock.return_value
    api.apps.return_value = mock_coro(return_value=[app])
    api.generate_app_oauth.return_value = \
        mock_coro(return_value=app_oauth_client)

    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'
コード例 #17
0
async def test_app_created_then_show_wait_form(hass, app, smartthings_mock):
    """Test SmartApp is created when one does not exist and shows wait form."""
    flow = SmartThingsFlowHandler()
    flow.hass = hass

    smartthings = smartthings_mock.return_value
    smartthings.apps.return_value = mock_coro(return_value=[])
    smartthings.create_app.return_value = mock_coro(return_value=(app, None))
    smartthings.update_app_settings.return_value = mock_coro()
    smartthings.update_app_oauth.return_value = mock_coro()

    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'
コード例 #18
0
async def test_step_pat_forbidden(hass, smartthings_mock):
    """Test an error is shown when the token is forbidden."""
    flow = SmartThingsFlowHandler()
    flow.hass = hass
    request_info = Mock(real_url="http://example.com")
    smartthings_mock.apps.side_effect = ClientResponseError(
        request_info=request_info, history=None, status=HTTP_FORBIDDEN)
    token = str(uuid4())

    result = await flow.async_step_pat({CONF_ACCESS_TOKEN: token})

    assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
    assert result["step_id"] == "pat"
    assert result["errors"] == {CONF_ACCESS_TOKEN: "token_forbidden"}
    assert result["data_schema"]({}) == {CONF_ACCESS_TOKEN: token}
コード例 #19
0
async def test_webhook_error(hass, smartthings_mock):
    """Test an error is when there's an error with the webhook endpoint."""
    flow = SmartThingsFlowHandler()
    flow.hass = hass

    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())})

    assert result['type'] == data_entry_flow.RESULT_TYPE_FORM
    assert result['step_id'] == 'user'
    assert result['errors'] == {'base': 'webhook_error'}
コード例 #20
0
async def test_step_authorize_advances(hass):
    """Test authorize step advances when completed."""
    installed_app_id = str(uuid4())
    refresh_token = str(uuid4())
    flow = SmartThingsFlowHandler()
    flow.hass = hass

    result = await flow.async_step_authorize({
        CONF_INSTALLED_APP_ID: installed_app_id,
        CONF_REFRESH_TOKEN: refresh_token
    })

    assert flow.installed_app_id == installed_app_id
    assert flow.refresh_token == refresh_token
    assert result["type"] == data_entry_flow.RESULT_TYPE_EXTERNAL_STEP_DONE
    assert result["step_id"] == "install"
コード例 #21
0
async def test_api_error(hass, smartthings_mock):
    """Test an error is shown when other API errors occur."""
    flow = SmartThingsFlowHandler()
    flow.hass = hass

    data = {'error': {}}
    error = APIResponseError(None, None, data=data, status=400)

    smartthings_mock.return_value.apps.return_value = mock_coro(
        exception=error)

    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'}
コード例 #22
0
async def test_api_error(hass, smartthings_mock):
    """Test an error is shown when other API errors occur."""
    flow = SmartThingsFlowHandler()
    flow.hass = hass

    data = {'error': {}}
    error = APIResponseError(None, None, data=data, status=400)

    smartthings_mock.return_value.apps.return_value = mock_coro(
        exception=error)

    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'}
コード例 #23
0
async def test_token_already_setup(hass):
    """Test an error is shown when the token is already setup."""
    flow = SmartThingsFlowHandler()
    flow.hass = hass
    token = str(uuid4())
    entries = [ConfigEntry(
        version='', domain='', title='', data={'access_token': token},
        source='', connection_class='')]

    with patch.object(hass.config_entries, 'async_entries',
                      return_value=entries):
        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'}
コード例 #24
0
async def test_step_pat(hass):
    """Test pat step shows the input form."""
    flow = SmartThingsFlowHandler()
    flow.hass = hass

    result = await flow.async_step_pat()

    assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
    assert result["step_id"] == "pat"
    assert result["errors"] == {}
    assert result["data_schema"]({
        CONF_ACCESS_TOKEN: ""
    }) == {
        CONF_ACCESS_TOKEN: ""
    }
    assert "token_url" in result["description_placeholders"]
    assert "component_url" in result["description_placeholders"]
コード例 #25
0
async def test_step_pat_defaults_token(hass):
    """Test pat form defaults the token from another entry."""
    token = str(uuid4())
    entry = MockConfigEntry(domain=DOMAIN, data={CONF_ACCESS_TOKEN: token})
    entry.add_to_hass(hass)
    flow = SmartThingsFlowHandler()
    flow.hass = hass

    result = await flow.async_step_pat()

    assert flow.access_token == token
    assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
    assert result["step_id"] == "pat"
    assert result["errors"] == {}
    assert result["data_schema"]({}) == {CONF_ACCESS_TOKEN: token}
    assert "token_url" in result["description_placeholders"]
    assert "component_url" in result["description_placeholders"]
コード例 #26
0
async def test_webhook_error(hass, smartthings_mock):
    """Test an error is when there's an error with the webhook endpoint."""
    flow = SmartThingsFlowHandler()
    flow.hass = hass

    data = {'error': {}}
    error = APIResponseError(None, None, data=data, status=422)
    error.is_target_error = Mock(return_value=True)

    smartthings_mock.return_value.apps.return_value = mock_coro(
        exception=error)

    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'}
コード例 #27
0
async def test_config_entry_created_when_installed(
        hass, location, installed_app, smartthings_mock):
    """Test a config entry is created once the app is installed."""
    flow = SmartThingsFlowHandler()
    flow.hass = hass
    flow.access_token = str(uuid4())
    flow.app_id = installed_app.app_id
    flow.api = smartthings_mock.return_value
    flow.oauth_client_id = str(uuid4())
    flow.oauth_client_secret = str(uuid4())
    data = {
        CONF_REFRESH_TOKEN: str(uuid4()),
        CONF_LOCATION_ID: installed_app.location_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
コード例 #28
0
async def test_step_pat_app_updated_webhook(hass, app, app_oauth_client,
                                            location, smartthings_mock):
    """Test SmartApp is updated then show location form."""
    flow = SmartThingsFlowHandler()
    flow.hass = hass

    smartthings_mock.apps.return_value = [app]
    smartthings_mock.generate_app_oauth.return_value = app_oauth_client
    smartthings_mock.locations.return_value = [location]
    token = str(uuid4())

    result = await flow.async_step_pat({CONF_ACCESS_TOKEN: token})

    assert flow.access_token == token
    assert flow.app_id == app.app_id
    assert flow.oauth_client_secret == app_oauth_client.client_secret
    assert flow.oauth_client_id == app_oauth_client.client_id
    assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
    assert result["step_id"] == "select_location"
コード例 #29
0
async def test_webhook_error(hass, smartthings_mock):
    """Test an error is when there's an error with the webhook endpoint."""
    flow = SmartThingsFlowHandler()
    flow.hass = hass

    data = {"error": {}}
    request_info = Mock(real_url="http://example.com")
    error = APIResponseError(request_info=request_info,
                             history=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())})

    assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
    assert result["step_id"] == "user"
    assert result["errors"] == {"base": "webhook_error"}
コード例 #30
0
async def test_step_pat_webhook_error(hass, smartthings_mock):
    """Test an error is shown when there's an problem with the webhook endpoint."""
    flow = SmartThingsFlowHandler()
    flow.hass = hass
    data = {"error": {}}
    request_info = Mock(real_url="http://example.com")
    error = APIResponseError(request_info=request_info,
                             history=None,
                             data=data,
                             status=422)
    error.is_target_error = Mock(return_value=True)
    smartthings_mock.apps.side_effect = error
    token = str(uuid4())

    result = await flow.async_step_pat({CONF_ACCESS_TOKEN: token})

    assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
    assert result["step_id"] == "pat"
    assert result["errors"] == {"base": "webhook_error"}
    assert result["data_schema"]({}) == {CONF_ACCESS_TOKEN: token}
コード例 #31
0
async def test_config_entry_created_when_installed(
        hass, location, installed_app, smartthings_mock):
    """Test a config entry is created once the app is installed."""
    flow = SmartThingsFlowHandler()
    flow.hass = hass
    flow.access_token = str(uuid4())
    flow.api = smartthings_mock.return_value
    flow.app_id = installed_app.app_id
    flow.api.installed_apps.return_value = \
        mock_coro(return_value=[installed_app])

    result = await flow.async_step_wait_install({})

    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['title'] == location.name
コード例 #32
0
async def test_multiple_config_entry_created_when_installed(
        hass, app, locations, installed_apps, smartthings_mock):
    """Test a config entries are created for multiple installs."""
    flow = SmartThingsFlowHandler()
    flow.hass = hass
    flow.access_token = str(uuid4())
    flow.app_id = app.app_id
    flow.api = smartthings_mock.return_value
    flow.api.installed_apps.return_value = \
        mock_coro(return_value=installed_apps)

    result = await flow.async_step_wait_install({})

    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['title'] == locations[0].name

    await hass.async_block_till_done()
    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].title == locations[1].name
コード例 #33
0
async def test_token_already_setup(hass):
    """Test an error is shown when the token is already setup."""
    flow = SmartThingsFlowHandler()
    flow.hass = hass
    token = str(uuid4())
    entries = [
        ConfigEntry(version='',
                    domain='',
                    title='',
                    data={'access_token': token},
                    source='',
                    connection_class='')
    ]

    with patch.object(hass.config_entries,
                      'async_entries',
                      return_value=entries):
        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'}
コード例 #34
0
async def test_cloudhook_app_created_then_show_wait_form(
        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:

        await smartapp.setup_smartapp_endpoint(hass)

        flow = SmartThingsFlowHandler()
        flow.hass = hass
        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())})

        assert result['type'] == data_entry_flow.RESULT_TYPE_FORM
        assert result['step_id'] == 'wait_install'
        assert mock_create_cloudhook.call_count == 1
コード例 #35
0
async def test_multiple_config_entry_created_when_installed(
        hass, app, locations, installed_apps, smartthings_mock):
    """Test a config entries are created for multiple installs."""
    assert await async_setup_component(hass, "persistent_notification", {})
    flow = SmartThingsFlowHandler()
    flow.hass = hass
    flow.access_token = str(uuid4())
    flow.app_id = app.app_id
    flow.api = smartthings_mock
    flow.oauth_client_id = str(uuid4())
    flow.oauth_client_secret = str(uuid4())
    for installed_app in installed_apps:
        data = {
            CONF_REFRESH_TOKEN: str(uuid4()),
            CONF_LOCATION_ID: installed_app.location_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()

    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_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")
    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].title == locations[1].name
コード例 #36
0
async def test_multiple_config_entry_created_when_installed(
        hass, app, locations, installed_apps, smartthings_mock):
    """Test a config entries are created for multiple installs."""
    flow = SmartThingsFlowHandler()
    flow.hass = hass
    flow.access_token = str(uuid4())
    flow.app_id = app.app_id
    flow.api = smartthings_mock.return_value
    flow.oauth_client_id = str(uuid4())
    flow.oauth_client_secret = str(uuid4())
    for installed_app in installed_apps:
        data = {
            CONF_REFRESH_TOKEN: str(uuid4()),
            CONF_LOCATION_ID: installed_app.location_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()

    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_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')
    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].title == locations[1].name
コード例 #37
0
async def test_multiple_config_entry_created_when_installed(
        hass, app, locations, installed_apps, smartthings_mock):
    """Test a config entries are created for multiple installs."""
    flow = SmartThingsFlowHandler()
    flow.hass = hass
    flow.access_token = str(uuid4())
    flow.app_id = app.app_id
    flow.api = smartthings_mock.return_value
    flow.oauth_client_id = str(uuid4())
    flow.oauth_client_secret = str(uuid4())
    for installed_app in installed_apps:
        data = {
            CONF_REFRESH_TOKEN: str(uuid4()),
            CONF_LOCATION_ID: installed_app.location_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()

    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_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')
    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].title == locations[1].name
コード例 #38
0
async def test_config_entry_created_when_installed(hass, location,
                                                   installed_app,
                                                   smartthings_mock):
    """Test a config entry is created once the app is installed."""
    flow = SmartThingsFlowHandler()
    flow.hass = hass
    flow.access_token = str(uuid4())
    flow.api = smartthings_mock.return_value
    flow.app_id = installed_app.app_id
    flow.api.installed_apps.return_value = \
        mock_coro(return_value=[installed_app])

    result = await flow.async_step_wait_install({})

    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['title'] == location.name
コード例 #39
0
async def test_step_install_creates_entry(hass, location, smartthings_mock):
    """Test a config entry is created once the app is installed."""
    flow = SmartThingsFlowHandler()
    flow.hass = hass
    flow.api = smartthings_mock
    flow.access_token = str(uuid4())
    flow.app_id = str(uuid4())
    flow.installed_app_id = str(uuid4())
    flow.location_id = location.location_id
    flow.oauth_client_id = str(uuid4())
    flow.oauth_client_secret = str(uuid4())
    flow.refresh_token = str(uuid4())

    result = await flow.async_step_install()

    assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
    assert result["data"]["app_id"] == flow.app_id
    assert result["data"]["installed_app_id"] == flow.installed_app_id
    assert result["data"]["location_id"] == flow.location_id
    assert result["data"]["access_token"] == flow.access_token
    assert result["data"]["refresh_token"] == flow.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