forked from home-assistant/core
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Renovate Ridwell config flow tests (home-assistant#85135)
* Renovate Ridwell config flow tests * Better fixture name * Inconsistent typing
- Loading branch information
Showing
3 changed files
with
59 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,75 +1,74 @@ | ||
"""Test the Ridwell config flow.""" | ||
from unittest.mock import patch | ||
from unittest.mock import AsyncMock, patch | ||
|
||
from aioridwell.errors import InvalidCredentialsError, RidwellError | ||
import pytest | ||
|
||
from homeassistant import config_entries | ||
from homeassistant.components.ridwell.const import DOMAIN | ||
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME | ||
from homeassistant.core import HomeAssistant | ||
from homeassistant.data_entry_flow import FlowResultType | ||
|
||
|
||
async def test_duplicate_error(hass: HomeAssistant, config, config_entry): | ||
"""Test that errors are shown when duplicate entries are added.""" | ||
result = await hass.config_entries.flow.async_init( | ||
DOMAIN, context={"source": config_entries.SOURCE_USER}, data=config | ||
) | ||
assert result["type"] == FlowResultType.ABORT | ||
assert result["reason"] == "already_configured" | ||
from .conftest import TEST_PASSWORD, TEST_USERNAME | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"exc,error", | ||
"get_client_response,errors", | ||
[ | ||
(InvalidCredentialsError, "invalid_auth"), | ||
(RidwellError, "unknown"), | ||
(AsyncMock(side_effect=InvalidCredentialsError), {"base": "invalid_auth"}), | ||
(AsyncMock(side_effect=RidwellError), {"base": "unknown"}), | ||
], | ||
) | ||
async def test_errors(hass: HomeAssistant, config, error, exc) -> None: | ||
"""Test that various exceptions show the correct error.""" | ||
async def test_create_entry(hass, config, errors, get_client_response, mock_aioridwell): | ||
"""Test creating an entry.""" | ||
result = await hass.config_entries.flow.async_init( | ||
DOMAIN, context={"source": config_entries.SOURCE_USER} | ||
) | ||
assert result["type"] == FlowResultType.FORM | ||
assert result["step_id"] == "user" | ||
|
||
# Test errors that can arise: | ||
with patch( | ||
"homeassistant.components.ridwell.config_flow.async_get_client", side_effect=exc | ||
"homeassistant.components.ridwell.config_flow.async_get_client", | ||
get_client_response, | ||
): | ||
result = await hass.config_entries.flow.async_init( | ||
DOMAIN, context={"source": config_entries.SOURCE_USER}, data=config | ||
result = await hass.config_entries.flow.async_configure( | ||
result["flow_id"], user_input=config | ||
) | ||
assert result["type"] == FlowResultType.FORM | ||
assert result["errors"]["base"] == error | ||
assert result["step_id"] == "user" | ||
assert result["errors"] == errors | ||
|
||
# Test that we can recover and finish the flow after errors occur: | ||
result = await hass.config_entries.flow.async_configure( | ||
result["flow_id"], user_input=config | ||
) | ||
assert result["type"] == FlowResultType.CREATE_ENTRY | ||
assert result["title"] == TEST_USERNAME | ||
assert result["data"] == { | ||
CONF_USERNAME: TEST_USERNAME, | ||
CONF_PASSWORD: TEST_PASSWORD, | ||
} | ||
|
||
|
||
async def test_show_form_user(hass: HomeAssistant) -> None: | ||
"""Test showing the form to input credentials.""" | ||
async def test_duplicate_error(hass, config, setup_config_entry): | ||
"""Test that errors are shown when duplicate entries are added.""" | ||
result = await hass.config_entries.flow.async_init( | ||
DOMAIN, context={"source": config_entries.SOURCE_USER} | ||
DOMAIN, context={"source": config_entries.SOURCE_USER}, data=config | ||
) | ||
assert result["type"] == FlowResultType.FORM | ||
assert result["step_id"] == "user" | ||
assert result["errors"] is None | ||
assert result["type"] == FlowResultType.ABORT | ||
assert result["reason"] == "already_configured" | ||
|
||
|
||
async def test_step_reauth( | ||
hass: HomeAssistant, config, config_entry, setup_ridwell | ||
) -> None: | ||
async def test_step_reauth(hass, config, config_entry, setup_config_entry) -> None: | ||
"""Test a full reauth flow.""" | ||
result = await hass.config_entries.flow.async_init( | ||
DOMAIN, | ||
context={"source": config_entries.SOURCE_REAUTH}, | ||
data={CONF_USERNAME: "user@email.com", CONF_PASSWORD: "password"}, | ||
DOMAIN, context={"source": config_entries.SOURCE_REAUTH}, data=config | ||
) | ||
result = await hass.config_entries.flow.async_configure( | ||
result["flow_id"], | ||
user_input={CONF_PASSWORD: "password"}, | ||
user_input={CONF_PASSWORD: "new_password"}, | ||
) | ||
assert result["type"] == FlowResultType.ABORT | ||
assert result["reason"] == "reauth_successful" | ||
assert len(hass.config_entries.async_entries()) == 1 | ||
|
||
|
||
async def test_step_user(hass: HomeAssistant, config, setup_ridwell) -> None: | ||
"""Test that the full user step succeeds.""" | ||
result = await hass.config_entries.flow.async_init( | ||
DOMAIN, context={"source": config_entries.SOURCE_USER}, data=config | ||
) | ||
assert result["type"] == FlowResultType.CREATE_ENTRY |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters