Add re-auth flow to AirVisual Pro (#84012)

* Add re-auth flow to AirVisual Pro

* Code review
This commit is contained in:
Aaron Bach 2022-12-18 11:00:08 -07:00 committed by GitHub
parent 47522546e6
commit 4c73826baf
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 218 additions and 82 deletions

View file

@ -1,6 +1,6 @@
"""Define test fixtures for AirVisual Pro."""
import json
from unittest.mock import patch
from unittest.mock import AsyncMock, Mock, patch
import pytest
@ -28,20 +28,41 @@ def config_fixture(hass):
}
@pytest.fixture(name="connect")
def connect_fixture():
"""Define a mocked async_connect method."""
return AsyncMock(return_value=True)
@pytest.fixture(name="disconnect")
def disconnect_fixture():
"""Define a mocked async_connect method."""
return AsyncMock()
@pytest.fixture(name="data", scope="session")
def data_fixture():
"""Define an update coordinator data example."""
return json.loads(load_fixture("data.json", "airvisual_pro"))
@pytest.fixture(name="pro")
def pro_fixture(connect, data, disconnect):
"""Define a mocked NodeSamba object."""
return Mock(
async_connect=connect,
async_disconnect=disconnect,
async_get_latest_measurements=AsyncMock(return_value=data),
)
@pytest.fixture(name="setup_airvisual_pro")
async def setup_airvisual_pro_fixture(hass, config, data):
async def setup_airvisual_pro_fixture(hass, config, pro):
"""Define a fixture to set up AirVisual Pro."""
with patch("homeassistant.components.airvisual_pro.NodeSamba.async_connect"), patch(
"homeassistant.components.airvisual_pro.NodeSamba.async_get_latest_measurements",
return_value=data,
with patch(
"homeassistant.components.airvisual_pro.config_flow.NodeSamba", return_value=pro
), patch(
"homeassistant.components.airvisual_pro.NodeSamba.async_disconnect"
"homeassistant.components.airvisual_pro.NodeSamba", return_value=pro
), patch(
"homeassistant.components.airvisual.PLATFORMS", []
):

View file

@ -1,15 +1,57 @@
"""Test the AirVisual Pro config flow."""
from unittest.mock import patch
from unittest.mock import AsyncMock, patch
from pyairvisual.node import NodeProError
from pyairvisual.node import (
InvalidAuthenticationError,
NodeConnectionError,
NodeProError,
)
import pytest
from homeassistant import data_entry_flow
from homeassistant.components.airvisual_pro.const import DOMAIN
from homeassistant.config_entries import SOURCE_USER
from homeassistant.config_entries import SOURCE_REAUTH, SOURCE_USER
from homeassistant.const import CONF_IP_ADDRESS, CONF_PASSWORD
@pytest.mark.parametrize(
"connect_mock,connect_errors",
[
(AsyncMock(side_effect=Exception), {"base": "unknown"}),
(AsyncMock(side_effect=InvalidAuthenticationError), {"base": "invalid_auth"}),
(AsyncMock(side_effect=NodeConnectionError), {"base": "cannot_connect"}),
(AsyncMock(side_effect=NodeProError), {"base": "unknown"}),
],
)
async def test_create_entry(
hass, config, connect_errors, connect_mock, pro, setup_airvisual_pro
):
"""Test creating an entry."""
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_USER}
)
assert result["type"] == data_entry_flow.FlowResultType.FORM
assert result["step_id"] == "user"
# Test errors that can arise when connecting to a Pro:
with patch.object(pro, "async_connect", connect_mock):
result = await hass.config_entries.flow.async_configure(
result["flow_id"], user_input=config
)
assert result["type"] == data_entry_flow.FlowResultType.FORM
assert result["errors"] == connect_errors
result = await hass.config_entries.flow.async_configure(
result["flow_id"], user_input=config
)
assert result["type"] == data_entry_flow.FlowResultType.CREATE_ENTRY
assert result["title"] == "192.168.1.101"
assert result["data"] == {
CONF_IP_ADDRESS: "192.168.1.101",
CONF_PASSWORD: "password123",
}
async def test_duplicate_error(hass, config, config_entry):
"""Test that errors are shown when duplicates are added."""
result = await hass.config_entries.flow.async_init(
@ -20,51 +62,45 @@ async def test_duplicate_error(hass, config, config_entry):
@pytest.mark.parametrize(
"exc,errors",
"connect_mock,connect_errors",
[
(NodeProError, {"base": "cannot_connect"}),
(Exception, {"base": "unknown"}),
(AsyncMock(side_effect=Exception), {"base": "unknown"}),
(AsyncMock(side_effect=InvalidAuthenticationError), {"base": "invalid_auth"}),
(AsyncMock(side_effect=NodeConnectionError), {"base": "cannot_connect"}),
(AsyncMock(side_effect=NodeProError), {"base": "unknown"}),
],
)
async def test_errors(hass, config, exc, errors, setup_airvisual_pro):
"""Test that an exceptions show an error."""
with patch(
"homeassistant.components.airvisual_pro.config_flow.NodeSamba.async_connect",
side_effect=exc,
):
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_USER}, data=config
)
assert result["type"] == data_entry_flow.FlowResultType.FORM
assert result["errors"] == errors
# Validate that we can still proceed after an error if the underlying condition
# resolves:
result = await hass.config_entries.flow.async_configure(
result["flow_id"], user_input=config
)
assert result["type"] == data_entry_flow.FlowResultType.CREATE_ENTRY
assert result["title"] == "192.168.1.101"
assert result["data"] == {
CONF_IP_ADDRESS: "192.168.1.101",
CONF_PASSWORD: "password123",
}
async def test_step_user(hass, config, setup_airvisual_pro):
"""Test that the user step works."""
async def test_reauth(
hass, config, config_entry, connect_errors, connect_mock, pro, setup_airvisual_pro
):
"""Test re-auth (including errors)."""
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_USER}
DOMAIN,
context={
"source": SOURCE_REAUTH,
"entry_id": config_entry.entry_id,
"unique_id": config_entry.unique_id,
},
data=config,
)
assert result["type"] == data_entry_flow.FlowResultType.FORM
assert result["step_id"] == "user"
assert result["step_id"] == "reauth_confirm"
# Test errors that can arise when connecting to a Pro:
with patch.object(pro, "async_connect", connect_mock):
result = await hass.config_entries.flow.async_configure(
result["flow_id"], user_input={CONF_PASSWORD: "new_password"}
)
assert result["type"] == data_entry_flow.FlowResultType.FORM
assert result["errors"] == connect_errors
result = await hass.config_entries.flow.async_configure(
result["flow_id"], user_input=config
result["flow_id"], user_input={CONF_PASSWORD: "new_password"}
)
assert result["type"] == data_entry_flow.FlowResultType.CREATE_ENTRY
assert result["title"] == "192.168.1.101"
assert result["data"] == {
CONF_IP_ADDRESS: "192.168.1.101",
CONF_PASSWORD: "password123",
}
# Allow reload to finish:
await hass.async_block_till_done()
assert result["type"] == data_entry_flow.FlowResultType.ABORT
assert result["reason"] == "reauth_successful"
assert len(hass.config_entries.async_entries()) == 1