示例#1
0
async def test_full_flow_implementation(hass):
    """Test registering an implementation and finishing flow works."""
    config_flow.register_flow_implementation(hass, None, None)
    flow = await 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['description_placeholders']['cb_url']\
        == 'https://hass.com/api/ambiclimate'

    url = result['description_placeholders']['authorization_url']
    assert 'https://api.ambiclimate.com/oauth2/authorize' in url
    assert 'client_id=id' in url
    assert 'response_type=code' in url
    assert 'redirect_uri=https%3A%2F%2Fhass.com%2Fapi%2Fambiclimate' in url

    with patch('ambiclimate.AmbiclimateOAuth.get_access_token',
               return_value=mock_coro('test')):
        result = await flow.async_step_code('123ABC')
    assert result['type'] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
    assert result['title'] == 'Ambiclimate'
    assert result['data']['callback_url'] == 'https://hass.com/api/ambiclimate'
    assert result['data']['client_secret'] == 'secret'
    assert result['data']['client_id'] == 'id'

    with patch('ambiclimate.AmbiclimateOAuth.get_access_token',
               return_value=mock_coro(None)):
        result = await flow.async_step_code('123ABC')
    assert result['type'] == data_entry_flow.RESULT_TYPE_ABORT

    with patch('ambiclimate.AmbiclimateOAuth.get_access_token',
               side_effect=ambiclimate.AmbiclimateOauthError()):
        result = await flow.async_step_code('123ABC')
    assert result['type'] == data_entry_flow.RESULT_TYPE_ABORT
示例#2
0
async def test_abort_no_code(hass):
    """Test if no code is given to step_code."""
    config_flow.register_flow_implementation(hass, None, None)
    flow = await init_config_flow(hass)

    result = await flow.async_step_code('invalid')
    assert result['type'] == data_entry_flow.RESULT_TYPE_ABORT
    assert result['reason'] == 'access_token'
示例#3
0
async def test_abort_invalid_code(hass):
    """Test if no code is given to step_code."""
    config_flow.register_flow_implementation(hass, None, None)
    flow = await init_config_flow(hass)

    with patch("ambiclimate.AmbiclimateOAuth.get_access_token", return_value=None):
        result = await flow.async_step_code("invalid")
    assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT
    assert result["reason"] == "access_token"
async def test_already_setup(hass):
    """Test when already setup."""
    config_flow.register_flow_implementation(hass, None, None)
    flow = await init_config_flow(hass)

    with patch.object(hass.config_entries, "async_entries", return_value=True):
        result = await flow.async_step_user()

    assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT
    assert result["reason"] == "already_setup"
示例#5
0
async def test_abort_invalid_code(hass):
    """Test if no code is given to step_code."""
    config_flow.register_flow_implementation(hass, None, None)
    flow = await init_config_flow(hass)

    with patch('ambiclimate.AmbiclimateOAuth.get_access_token',
               return_value=mock_coro(None)):
        result = await flow.async_step_code('invalid')
    assert result['type'] == data_entry_flow.RESULT_TYPE_ABORT
    assert result['reason'] == 'access_token'
示例#6
0
async def init_config_flow(hass):
    """Init a configuration flow."""
    await async_setup_component(
        hass, "http", {"http": {"base_url": "https://hass.com"}}
    )

    config_flow.register_flow_implementation(hass, "id", "secret")
    flow = config_flow.AmbiclimateFlowHandler()

    flow.hass = hass
    return flow
示例#7
0
async def init_config_flow(hass):
    """Init a configuration flow."""
    await async_setup_component(hass, 'http',
                                {'http': {
                                    'base_url': 'https://hass.com'
                                }})

    config_flow.register_flow_implementation(hass, 'id', 'secret')
    flow = config_flow.AmbiclimateFlowHandler()

    flow.hass = hass
    return flow
示例#8
0
async def init_config_flow(hass):
    """Init a configuration flow."""
    await async_process_ha_core_config(
        hass,
        {"external_url": "https://example.com"},
    )
    await async_setup_component(hass, "http", {})

    config_flow.register_flow_implementation(hass, "id", "secret")
    flow = config_flow.AmbiclimateFlowHandler()

    flow.hass = hass
    return flow
示例#9
0
async def test_full_flow_implementation(hass):
    """Test registering an implementation and finishing flow works."""
    config_flow.register_flow_implementation(hass, None, None)
    flow = await init_config_flow(hass)

    result = await flow.async_step_user()
    assert result["type"] == data_entry_flow.FlowResultType.FORM
    assert result["step_id"] == "auth"
    assert (result["description_placeholders"]["cb_url"] ==
            "https://example.com/api/ambiclimate")

    url = result["description_placeholders"]["authorization_url"]
    assert "https://api.ambiclimate.com/oauth2/authorize" in url
    assert "client_id=id" in url
    assert "response_type=code" in url
    assert "redirect_uri=https%3A%2F%2Fexample.com%2Fapi%2Fambiclimate" in url

    with patch("ambiclimate.AmbiclimateOAuth.get_access_token",
               return_value="test"):
        result = await flow.async_step_code("123ABC")
    assert result["type"] == data_entry_flow.FlowResultType.CREATE_ENTRY
    assert result["title"] == "Ambiclimate"
    assert result["data"][
        "callback_url"] == "https://example.com/api/ambiclimate"
    assert result["data"][CONF_CLIENT_SECRET] == "secret"
    assert result["data"][CONF_CLIENT_ID] == "id"

    with patch("ambiclimate.AmbiclimateOAuth.get_access_token",
               return_value=None):
        result = await flow.async_step_code("123ABC")
    assert result["type"] == data_entry_flow.FlowResultType.ABORT

    with patch(
            "ambiclimate.AmbiclimateOAuth.get_access_token",
            side_effect=ambiclimate.AmbiclimateOauthError(),
    ):
        result = await flow.async_step_code("123ABC")
    assert result["type"] == data_entry_flow.FlowResultType.ABORT