Renovate Ridwell config flow tests (#85135)

* Renovate Ridwell config flow tests

* Better fixture name

* Inconsistent typing
This commit is contained in:
Aaron Bach 2023-01-04 14:00:59 -07:00 committed by GitHub
parent 6e9d3bf8e9
commit 80c357c00f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 66 additions and 64 deletions

View File

@ -7,18 +7,19 @@ import pytest
from homeassistant.components.ridwell.const import DOMAIN from homeassistant.components.ridwell.const import DOMAIN
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
from homeassistant.setup import async_setup_component
from tests.common import MockConfigEntry from tests.common import MockConfigEntry
ACCOUNT_ID = "12345" TEST_ACCOUNT_ID = "12345"
TEST_PASSWORD = "password"
TEST_USERNAME = "user@email.com"
@pytest.fixture(name="account") @pytest.fixture(name="account")
def account_fixture(): def account_fixture():
"""Define a Ridwell account.""" """Define a Ridwell account."""
return Mock( return Mock(
account_id=ACCOUNT_ID, account_id=TEST_ACCOUNT_ID,
address={ address={
"street1": "123 Main Street", "street1": "123 Main Street",
"city": "New York", "city": "New York",
@ -42,7 +43,7 @@ def client_fixture(account):
"""Define an aioridwell client.""" """Define an aioridwell client."""
return Mock( return Mock(
async_authenticate=AsyncMock(), async_authenticate=AsyncMock(),
async_get_accounts=AsyncMock(return_value={ACCOUNT_ID: account}), async_get_accounts=AsyncMock(return_value={TEST_ACCOUNT_ID: account}),
) )
@ -58,22 +59,24 @@ def config_entry_fixture(hass, config):
def config_fixture(hass): def config_fixture(hass):
"""Define a config entry data fixture.""" """Define a config entry data fixture."""
return { return {
CONF_USERNAME: "user@email.com", CONF_USERNAME: TEST_USERNAME,
CONF_PASSWORD: "password", CONF_PASSWORD: TEST_PASSWORD,
} }
@pytest.fixture(name="setup_ridwell") @pytest.fixture(name="mock_aioridwell")
async def setup_ridwell_fixture(hass, client, config): async def mock_aioridwell_fixture(hass, client, config):
"""Define a fixture to set up Ridwell.""" """Define a fixture to patch aioridwell."""
with patch( with patch(
"homeassistant.components.ridwell.config_flow.async_get_client", "homeassistant.components.ridwell.config_flow.async_get_client",
return_value=client, return_value=client,
), patch( ), patch("homeassistant.components.ridwell.async_get_client", return_value=client):
"homeassistant.components.ridwell.async_get_client", return_value=client
), patch(
"homeassistant.components.ridwell.PLATFORMS", []
):
assert await async_setup_component(hass, DOMAIN, config)
await hass.async_block_till_done()
yield yield
@pytest.fixture(name="setup_config_entry")
async def setup_config_entry_fixture(hass, config_entry, mock_aioridwell):
"""Define a fixture to set up ridwell."""
assert await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done()
yield

View File

@ -1,5 +1,5 @@
"""Test the Ridwell config flow.""" """Test the Ridwell config flow."""
from unittest.mock import patch from unittest.mock import AsyncMock, patch
from aioridwell.errors import InvalidCredentialsError, RidwellError from aioridwell.errors import InvalidCredentialsError, RidwellError
import pytest import pytest
@ -7,11 +7,51 @@ import pytest
from homeassistant import config_entries from homeassistant import config_entries
from homeassistant.components.ridwell.const import DOMAIN from homeassistant.components.ridwell.const import DOMAIN
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
from homeassistant.core import HomeAssistant
from homeassistant.data_entry_flow import FlowResultType from homeassistant.data_entry_flow import FlowResultType
from .conftest import TEST_PASSWORD, TEST_USERNAME
async def test_duplicate_error(hass: HomeAssistant, config, config_entry):
@pytest.mark.parametrize(
"get_client_response,errors",
[
(AsyncMock(side_effect=InvalidCredentialsError), {"base": "invalid_auth"}),
(AsyncMock(side_effect=RidwellError), {"base": "unknown"}),
],
)
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",
get_client_response,
):
result = await hass.config_entries.flow.async_configure(
result["flow_id"], user_input=config
)
assert result["type"] == FlowResultType.FORM
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_duplicate_error(hass, config, setup_config_entry):
"""Test that errors are shown when duplicate entries are added.""" """Test that errors are shown when duplicate entries are added."""
result = await hass.config_entries.flow.async_init( result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}, data=config DOMAIN, context={"source": config_entries.SOURCE_USER}, data=config
@ -20,56 +60,15 @@ async def test_duplicate_error(hass: HomeAssistant, config, config_entry):
assert result["reason"] == "already_configured" assert result["reason"] == "already_configured"
@pytest.mark.parametrize( async def test_step_reauth(hass, config, config_entry, setup_config_entry) -> None:
"exc,error",
[
(InvalidCredentialsError, "invalid_auth"),
(RidwellError, "unknown"),
],
)
async def test_errors(hass: HomeAssistant, config, error, exc) -> None:
"""Test that various exceptions show the correct error."""
with patch(
"homeassistant.components.ridwell.config_flow.async_get_client", side_effect=exc
):
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}, data=config
)
assert result["type"] == FlowResultType.FORM
assert result["errors"]["base"] == error
async def test_show_form_user(hass: HomeAssistant) -> None:
"""Test showing the form to input credentials."""
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"
assert result["errors"] is None
async def test_step_reauth(
hass: HomeAssistant, config, config_entry, setup_ridwell
) -> None:
"""Test a full reauth flow.""" """Test a full reauth flow."""
result = await hass.config_entries.flow.async_init( result = await hass.config_entries.flow.async_init(
DOMAIN, DOMAIN, context={"source": config_entries.SOURCE_REAUTH}, data=config
context={"source": config_entries.SOURCE_REAUTH},
data={CONF_USERNAME: "user@email.com", CONF_PASSWORD: "password"},
) )
result = await hass.config_entries.flow.async_configure( result = await hass.config_entries.flow.async_configure(
result["flow_id"], result["flow_id"],
user_input={CONF_PASSWORD: "password"}, user_input={CONF_PASSWORD: "new_password"},
) )
assert result["type"] == FlowResultType.ABORT assert result["type"] == FlowResultType.ABORT
assert result["reason"] == "reauth_successful" assert result["reason"] == "reauth_successful"
assert len(hass.config_entries.async_entries()) == 1 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

View File

@ -4,7 +4,7 @@ from homeassistant.components.diagnostics import REDACTED
from tests.components.diagnostics import get_diagnostics_for_config_entry from tests.components.diagnostics import get_diagnostics_for_config_entry
async def test_entry_diagnostics(hass, config_entry, hass_client, setup_ridwell): async def test_entry_diagnostics(hass, config_entry, hass_client, setup_config_entry):
"""Test config entry diagnostics.""" """Test config entry diagnostics."""
assert await get_diagnostics_for_config_entry(hass, hass_client, config_entry) == { assert await get_diagnostics_for_config_entry(hass, hass_client, config_entry) == {
"entry": { "entry": {