Renovate OpenUV config flow tests (#85150)
This commit is contained in:
parent
9ed629d838
commit
39b110b9b0
3 changed files with 106 additions and 98 deletions
|
@ -1,6 +1,6 @@
|
|||
"""Define test fixtures for OpenUV."""
|
||||
import json
|
||||
from unittest.mock import patch
|
||||
from unittest.mock import AsyncMock, Mock, patch
|
||||
|
||||
import pytest
|
||||
|
||||
|
@ -11,10 +11,23 @@ from homeassistant.const import (
|
|||
CONF_LATITUDE,
|
||||
CONF_LONGITUDE,
|
||||
)
|
||||
from homeassistant.setup import async_setup_component
|
||||
|
||||
from tests.common import MockConfigEntry, load_fixture
|
||||
|
||||
TEST_API_KEY = "abcde12345"
|
||||
TEST_ELEVATION = 0
|
||||
TEST_LATITUDE = 51.528308
|
||||
TEST_LONGITUDE = -0.3817765
|
||||
|
||||
|
||||
@pytest.fixture(name="client")
|
||||
def client_fixture(data_protection_window, data_uv_index):
|
||||
"""Define a mock Client object."""
|
||||
return Mock(
|
||||
uv_index=AsyncMock(return_value=data_uv_index),
|
||||
uv_protection_window=AsyncMock(return_value=data_protection_window),
|
||||
)
|
||||
|
||||
|
||||
@pytest.fixture(name="config_entry")
|
||||
def config_entry_fixture(hass, config):
|
||||
|
@ -30,13 +43,13 @@ def config_entry_fixture(hass, config):
|
|||
|
||||
|
||||
@pytest.fixture(name="config")
|
||||
def config_fixture(hass):
|
||||
def config_fixture():
|
||||
"""Define a config entry data fixture."""
|
||||
return {
|
||||
CONF_API_KEY: "abcde12345",
|
||||
CONF_ELEVATION: 0,
|
||||
CONF_LATITUDE: 51.528308,
|
||||
CONF_LONGITUDE: -0.3817765,
|
||||
CONF_API_KEY: TEST_API_KEY,
|
||||
CONF_ELEVATION: TEST_ELEVATION,
|
||||
CONF_LATITUDE: TEST_LATITUDE,
|
||||
CONF_LONGITUDE: TEST_LONGITUDE,
|
||||
}
|
||||
|
||||
|
||||
|
@ -52,17 +65,18 @@ def data_uv_index_fixture():
|
|||
return json.loads(load_fixture("uv_index_data.json", "openuv"))
|
||||
|
||||
|
||||
@pytest.fixture(name="setup_openuv")
|
||||
async def setup_openuv_fixture(hass, config, data_protection_window, data_uv_index):
|
||||
"""Define a fixture to set up OpenUV."""
|
||||
@pytest.fixture(name="mock_pyopenuv")
|
||||
async def mock_pyopenuv_fixture(client):
|
||||
"""Define a fixture to patch pyopenuv."""
|
||||
with patch(
|
||||
"homeassistant.components.openuv.Client.uv_index", return_value=data_uv_index
|
||||
), patch(
|
||||
"homeassistant.components.openuv.Client.uv_protection_window",
|
||||
return_value=data_protection_window,
|
||||
), patch(
|
||||
"homeassistant.components.openuv.PLATFORMS", []
|
||||
):
|
||||
assert await async_setup_component(hass, DOMAIN, config)
|
||||
await hass.async_block_till_done()
|
||||
"homeassistant.components.openuv.config_flow.Client", return_value=client
|
||||
), patch("homeassistant.components.openuv.Client", return_value=client):
|
||||
yield
|
||||
|
||||
|
||||
@pytest.fixture(name="setup_config_entry")
|
||||
async def setup_config_entry_fixture(hass, config_entry, mock_pyopenuv):
|
||||
"""Define a fixture to set up openuv."""
|
||||
assert await hass.config_entries.async_setup(config_entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
yield
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
"""Define tests for the OpenUV config flow."""
|
||||
from unittest.mock import patch
|
||||
from unittest.mock import AsyncMock, patch
|
||||
|
||||
from pyopenuv.errors import InvalidApiKeyError
|
||||
import voluptuous as vol
|
||||
|
@ -14,8 +14,41 @@ from homeassistant.const import (
|
|||
CONF_LONGITUDE,
|
||||
)
|
||||
|
||||
from .conftest import TEST_API_KEY, TEST_ELEVATION, TEST_LATITUDE, TEST_LONGITUDE
|
||||
|
||||
async def test_duplicate_error(hass, config, config_entry):
|
||||
|
||||
async def test_create_entry(hass, client, config, mock_pyopenuv):
|
||||
"""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 an error occurring:
|
||||
with patch.object(client, "uv_index", AsyncMock(side_effect=InvalidApiKeyError)):
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"], user_input=config
|
||||
)
|
||||
assert result["type"] == data_entry_flow.FlowResultType.FORM
|
||||
assert result["step_id"] == "user"
|
||||
assert result["errors"] == {CONF_API_KEY: "invalid_api_key"}
|
||||
|
||||
# Test that we can recover from the error:
|
||||
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"] == f"{TEST_LATITUDE}, {TEST_LONGITUDE}"
|
||||
assert result["data"] == {
|
||||
CONF_API_KEY: TEST_API_KEY,
|
||||
CONF_ELEVATION: TEST_ELEVATION,
|
||||
CONF_LATITUDE: TEST_LATITUDE,
|
||||
CONF_LONGITUDE: TEST_LONGITUDE,
|
||||
}
|
||||
|
||||
|
||||
async def test_duplicate_error(hass, config, config_entry, setup_config_entry):
|
||||
"""Test that errors are shown when duplicates are added."""
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": SOURCE_USER}, data=config
|
||||
|
@ -24,60 +57,45 @@ async def test_duplicate_error(hass, config, config_entry):
|
|||
assert result["reason"] == "already_configured"
|
||||
|
||||
|
||||
async def test_invalid_api_key(hass, config):
|
||||
"""Test that an invalid API key throws an error."""
|
||||
with patch(
|
||||
"homeassistant.components.openuv.Client.uv_index",
|
||||
side_effect=InvalidApiKeyError,
|
||||
):
|
||||
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"] == {CONF_API_KEY: "invalid_api_key"}
|
||||
|
||||
|
||||
def _get_schema_marker(data_schema: vol.Schema, key: str) -> vol.Marker:
|
||||
for k in data_schema.schema:
|
||||
if k == key and isinstance(k, vol.Marker):
|
||||
return k
|
||||
return None
|
||||
|
||||
|
||||
async def test_options_flow(hass, config_entry):
|
||||
async def test_options_flow(hass, config_entry, setup_config_entry):
|
||||
"""Test config flow options."""
|
||||
with patch("homeassistant.components.openuv.async_setup_entry", return_value=True):
|
||||
await hass.config_entries.async_setup(config_entry.entry_id)
|
||||
result = await hass.config_entries.options.async_init(config_entry.entry_id)
|
||||
assert result["type"] == data_entry_flow.FlowResultType.FORM
|
||||
assert result["step_id"] == "init"
|
||||
# Original schema uses defaults for suggested values
|
||||
assert _get_schema_marker(
|
||||
result["data_schema"], CONF_FROM_WINDOW
|
||||
).description == {"suggested_value": 3.5}
|
||||
assert _get_schema_marker(
|
||||
result["data_schema"], CONF_TO_WINDOW
|
||||
).description == {"suggested_value": 3.5}
|
||||
result = await hass.config_entries.options.async_init(config_entry.entry_id)
|
||||
assert result["type"] == data_entry_flow.FlowResultType.FORM
|
||||
assert result["step_id"] == "init"
|
||||
|
||||
result = await hass.config_entries.options.async_configure(
|
||||
result["flow_id"], user_input={CONF_FROM_WINDOW: 3.5, CONF_TO_WINDOW: 2.0}
|
||||
)
|
||||
assert result["type"] == data_entry_flow.FlowResultType.CREATE_ENTRY
|
||||
assert config_entry.options == {CONF_FROM_WINDOW: 3.5, CONF_TO_WINDOW: 2.0}
|
||||
def get_schema_marker(data_schema: vol.Schema, key: str) -> vol.Marker:
|
||||
for k in data_schema.schema:
|
||||
if k == key and isinstance(k, vol.Marker):
|
||||
return k
|
||||
return None
|
||||
|
||||
# Subsequent schema uses previous input for suggested values
|
||||
result = await hass.config_entries.options.async_init(config_entry.entry_id)
|
||||
assert result["type"] == data_entry_flow.FlowResultType.FORM
|
||||
assert result["step_id"] == "init"
|
||||
assert _get_schema_marker(
|
||||
result["data_schema"], CONF_FROM_WINDOW
|
||||
).description == {"suggested_value": 3.5}
|
||||
assert _get_schema_marker(
|
||||
result["data_schema"], CONF_TO_WINDOW
|
||||
).description == {"suggested_value": 2.0}
|
||||
# Original schema uses defaults for suggested values:
|
||||
assert get_schema_marker(result["data_schema"], CONF_FROM_WINDOW).description == {
|
||||
"suggested_value": 3.5
|
||||
}
|
||||
assert get_schema_marker(result["data_schema"], CONF_TO_WINDOW).description == {
|
||||
"suggested_value": 3.5
|
||||
}
|
||||
|
||||
result = await hass.config_entries.options.async_configure(
|
||||
result["flow_id"], user_input={CONF_FROM_WINDOW: 3.5, CONF_TO_WINDOW: 2.0}
|
||||
)
|
||||
assert result["type"] == data_entry_flow.FlowResultType.CREATE_ENTRY
|
||||
assert config_entry.options == {CONF_FROM_WINDOW: 3.5, CONF_TO_WINDOW: 2.0}
|
||||
|
||||
# Subsequent schema uses previous input for suggested values:
|
||||
result = await hass.config_entries.options.async_init(config_entry.entry_id)
|
||||
assert result["type"] == data_entry_flow.FlowResultType.FORM
|
||||
assert result["step_id"] == "init"
|
||||
assert get_schema_marker(result["data_schema"], CONF_FROM_WINDOW).description == {
|
||||
"suggested_value": 3.5
|
||||
}
|
||||
assert get_schema_marker(result["data_schema"], CONF_TO_WINDOW).description == {
|
||||
"suggested_value": 2.0
|
||||
}
|
||||
|
||||
|
||||
async def test_step_reauth(hass, config, config_entry, setup_openuv):
|
||||
async def test_step_reauth(hass, config, config_entry, setup_config_entry):
|
||||
"""Test that the reauth step works."""
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": SOURCE_REAUTH}, data=config
|
||||
|
@ -88,33 +106,9 @@ async def test_step_reauth(hass, config, config_entry, setup_openuv):
|
|||
assert result["type"] == data_entry_flow.FlowResultType.FORM
|
||||
assert result["step_id"] == "reauth_confirm"
|
||||
|
||||
with patch("homeassistant.components.openuv.async_setup_entry", return_value=True):
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"], user_input={CONF_API_KEY: "new_api_key"}
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"], user_input={CONF_API_KEY: "new_api_key"}
|
||||
)
|
||||
assert result["type"] == data_entry_flow.FlowResultType.ABORT
|
||||
assert result["reason"] == "reauth_successful"
|
||||
assert len(hass.config_entries.async_entries()) == 1
|
||||
|
||||
|
||||
async def test_step_user(hass, config, setup_openuv):
|
||||
"""Test that the user step works."""
|
||||
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"
|
||||
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": SOURCE_USER}, data=config
|
||||
)
|
||||
assert result["type"] == data_entry_flow.FlowResultType.CREATE_ENTRY
|
||||
assert result["title"] == "51.528308, -0.3817765"
|
||||
assert result["data"] == {
|
||||
CONF_API_KEY: "abcde12345",
|
||||
CONF_ELEVATION: 0,
|
||||
CONF_LATITUDE: 51.528308,
|
||||
CONF_LONGITUDE: -0.3817765,
|
||||
}
|
||||
|
|
|
@ -5,7 +5,7 @@ from homeassistant.setup import async_setup_component
|
|||
from tests.components.diagnostics import get_diagnostics_for_config_entry
|
||||
|
||||
|
||||
async def test_entry_diagnostics(hass, config_entry, hass_client, setup_openuv):
|
||||
async def test_entry_diagnostics(hass, config_entry, hass_client, setup_config_entry):
|
||||
"""Test config entry diagnostics."""
|
||||
await async_setup_component(hass, "homeassistant", {})
|
||||
assert await get_diagnostics_for_config_entry(hass, hass_client, config_entry) == {
|
||||
|
|
Loading…
Add table
Reference in a new issue