Tweak axis test fixtures (#122469)

* Don't automatically add config entry to hass

* Improve RTSP fixture typing

* Improve typing of config entry factory and remove unnecessary use of it

* Remove redundant fixture in config flow tests

* Parametrize config flow error test
This commit is contained in:
Robert Svensson 2024-07-23 19:27:38 +02:00 committed by GitHub
parent 1b7fb9ae12
commit b53800a69d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 108 additions and 123 deletions

View file

@ -2,7 +2,7 @@
from collections.abc import Callable
from ipaddress import ip_address
from unittest.mock import AsyncMock, patch
from unittest.mock import patch
import pytest
@ -17,13 +17,11 @@ from homeassistant.components.axis.const import (
)
from homeassistant.config_entries import (
SOURCE_DHCP,
SOURCE_IGNORE,
SOURCE_REAUTH,
SOURCE_RECONFIGURE,
SOURCE_SSDP,
SOURCE_USER,
SOURCE_ZEROCONF,
ConfigEntry,
)
from homeassistant.const import (
CONF_HOST,
@ -45,21 +43,9 @@ from tests.common import MockConfigEntry
DHCP_FORMATTED_MAC = dr.format_mac(MAC).replace(":", "")
@pytest.fixture(name="mock_config_entry")
async def mock_config_entry_fixture(
hass: HomeAssistant, config_entry: MockConfigEntry, mock_setup_entry: AsyncMock
) -> MockConfigEntry:
"""Mock config entry and setup entry."""
assert await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done()
return config_entry
@pytest.mark.usefixtures("mock_default_requests", "mock_setup_entry")
@pytest.mark.usefixtures("mock_default_requests")
async def test_flow_manual_configuration(hass: HomeAssistant) -> None:
"""Test that config flow works."""
MockConfigEntry(domain=AXIS_DOMAIN, source=SOURCE_IGNORE).add_to_hass(hass)
result = await hass.config_entries.flow.async_init(
AXIS_DOMAIN, context={"source": SOURCE_USER}
)
@ -93,11 +79,11 @@ async def test_flow_manual_configuration(hass: HomeAssistant) -> None:
async def test_manual_configuration_update_configuration(
hass: HomeAssistant,
mock_config_entry: MockConfigEntry,
config_entry_setup: MockConfigEntry,
mock_requests: Callable[[str], None],
) -> None:
"""Test that config flow fails on already configured device."""
assert mock_config_entry.data[CONF_HOST] == "1.2.3.4"
assert config_entry_setup.data[CONF_HOST] == "1.2.3.4"
result = await hass.config_entries.flow.async_init(
AXIS_DOMAIN, context={"source": SOURCE_USER}
@ -121,10 +107,19 @@ async def test_manual_configuration_update_configuration(
assert result["type"] is FlowResultType.ABORT
assert result["reason"] == "already_configured"
assert mock_config_entry.data[CONF_HOST] == "2.3.4.5"
assert config_entry_setup.data[CONF_HOST] == "2.3.4.5"
async def test_flow_fails_faulty_credentials(hass: HomeAssistant) -> None:
@pytest.mark.parametrize(
("exc", "error"),
[
(config_flow.AuthenticationRequired, "invalid_auth"),
(config_flow.CannotConnect, "cannot_connect"),
],
)
async def test_flow_fails_on_api(
hass: HomeAssistant, exc: Exception, error: str
) -> None:
"""Test that config flow fails on faulty credentials."""
result = await hass.config_entries.flow.async_init(
AXIS_DOMAIN, context={"source": SOURCE_USER}
@ -135,7 +130,7 @@ async def test_flow_fails_faulty_credentials(hass: HomeAssistant) -> None:
with patch(
"homeassistant.components.axis.config_flow.get_axis_api",
side_effect=config_flow.AuthenticationRequired,
side_effect=exc,
):
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
@ -148,37 +143,10 @@ async def test_flow_fails_faulty_credentials(hass: HomeAssistant) -> None:
},
)
assert result["errors"] == {"base": "invalid_auth"}
assert result["errors"] == {"base": error}
async def test_flow_fails_cannot_connect(hass: HomeAssistant) -> None:
"""Test that config flow fails on cannot connect."""
result = await hass.config_entries.flow.async_init(
AXIS_DOMAIN, context={"source": SOURCE_USER}
)
assert result["type"] is FlowResultType.FORM
assert result["step_id"] == "user"
with patch(
"homeassistant.components.axis.config_flow.get_axis_api",
side_effect=config_flow.CannotConnect,
):
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
user_input={
CONF_PROTOCOL: "http",
CONF_HOST: "1.2.3.4",
CONF_USERNAME: "user",
CONF_PASSWORD: "pass",
CONF_PORT: 80,
},
)
assert result["errors"] == {"base": "cannot_connect"}
@pytest.mark.usefixtures("mock_default_requests", "mock_setup_entry")
@pytest.mark.usefixtures("mock_default_requests")
async def test_flow_create_entry_multiple_existing_entries_of_same_model(
hass: HomeAssistant,
) -> None:
@ -229,18 +197,18 @@ async def test_flow_create_entry_multiple_existing_entries_of_same_model(
async def test_reauth_flow_update_configuration(
hass: HomeAssistant,
mock_config_entry: MockConfigEntry,
config_entry_setup: MockConfigEntry,
mock_requests: Callable[[str], None],
) -> None:
"""Test that config flow fails on already configured device."""
assert mock_config_entry.data[CONF_HOST] == "1.2.3.4"
assert mock_config_entry.data[CONF_USERNAME] == "root"
assert mock_config_entry.data[CONF_PASSWORD] == "pass"
assert config_entry_setup.data[CONF_HOST] == "1.2.3.4"
assert config_entry_setup.data[CONF_USERNAME] == "root"
assert config_entry_setup.data[CONF_PASSWORD] == "pass"
result = await hass.config_entries.flow.async_init(
AXIS_DOMAIN,
context={"source": SOURCE_REAUTH},
data=mock_config_entry.data,
data=config_entry_setup.data,
)
assert result["type"] is FlowResultType.FORM
@ -261,28 +229,28 @@ async def test_reauth_flow_update_configuration(
assert result["type"] is FlowResultType.ABORT
assert result["reason"] == "already_configured"
assert mock_config_entry.data[CONF_PROTOCOL] == "https"
assert mock_config_entry.data[CONF_HOST] == "2.3.4.5"
assert mock_config_entry.data[CONF_PORT] == 443
assert mock_config_entry.data[CONF_USERNAME] == "user2"
assert mock_config_entry.data[CONF_PASSWORD] == "pass2"
assert config_entry_setup.data[CONF_PROTOCOL] == "https"
assert config_entry_setup.data[CONF_HOST] == "2.3.4.5"
assert config_entry_setup.data[CONF_PORT] == 443
assert config_entry_setup.data[CONF_USERNAME] == "user2"
assert config_entry_setup.data[CONF_PASSWORD] == "pass2"
async def test_reconfiguration_flow_update_configuration(
hass: HomeAssistant,
mock_config_entry: MockConfigEntry,
config_entry_setup: MockConfigEntry,
mock_requests: Callable[[str], None],
) -> None:
"""Test that config flow reconfiguration updates configured device."""
assert mock_config_entry.data[CONF_HOST] == "1.2.3.4"
assert mock_config_entry.data[CONF_USERNAME] == "root"
assert mock_config_entry.data[CONF_PASSWORD] == "pass"
assert config_entry_setup.data[CONF_HOST] == "1.2.3.4"
assert config_entry_setup.data[CONF_USERNAME] == "root"
assert config_entry_setup.data[CONF_PASSWORD] == "pass"
result = await hass.config_entries.flow.async_init(
AXIS_DOMAIN,
context={
"source": SOURCE_RECONFIGURE,
"entry_id": mock_config_entry.entry_id,
"entry_id": config_entry_setup.entry_id,
},
)
@ -301,11 +269,11 @@ async def test_reconfiguration_flow_update_configuration(
assert result["type"] is FlowResultType.ABORT
assert result["reason"] == "already_configured"
assert mock_config_entry.data[CONF_PROTOCOL] == "http"
assert mock_config_entry.data[CONF_HOST] == "2.3.4.5"
assert mock_config_entry.data[CONF_PORT] == 80
assert mock_config_entry.data[CONF_USERNAME] == "user"
assert mock_config_entry.data[CONF_PASSWORD] == "pass"
assert config_entry_setup.data[CONF_PROTOCOL] == "http"
assert config_entry_setup.data[CONF_HOST] == "2.3.4.5"
assert config_entry_setup.data[CONF_PORT] == 80
assert config_entry_setup.data[CONF_USERNAME] == "user"
assert config_entry_setup.data[CONF_PASSWORD] == "pass"
@pytest.mark.parametrize(
@ -372,7 +340,7 @@ async def test_reconfiguration_flow_update_configuration(
),
],
)
@pytest.mark.usefixtures("mock_default_requests", "mock_setup_entry")
@pytest.mark.usefixtures("mock_default_requests")
async def test_discovery_flow(
hass: HomeAssistant,
source: str,
@ -455,12 +423,12 @@ async def test_discovery_flow(
)
async def test_discovered_device_already_configured(
hass: HomeAssistant,
mock_config_entry: MockConfigEntry,
config_entry_setup: MockConfigEntry,
source: str,
discovery_info: BaseServiceInfo,
) -> None:
"""Test that discovery doesn't setup already configured devices."""
assert mock_config_entry.data[CONF_HOST] == DEFAULT_HOST
assert config_entry_setup.data[CONF_HOST] == DEFAULT_HOST
result = await hass.config_entries.flow.async_init(
AXIS_DOMAIN, data=discovery_info, context={"source": source}
@ -468,7 +436,7 @@ async def test_discovered_device_already_configured(
assert result["type"] is FlowResultType.ABORT
assert result["reason"] == "already_configured"
assert mock_config_entry.data[CONF_HOST] == DEFAULT_HOST
assert config_entry_setup.data[CONF_HOST] == DEFAULT_HOST
@pytest.mark.parametrize(
@ -513,14 +481,14 @@ async def test_discovered_device_already_configured(
)
async def test_discovery_flow_updated_configuration(
hass: HomeAssistant,
mock_config_entry: MockConfigEntry,
config_entry_setup: MockConfigEntry,
mock_requests: Callable[[str], None],
source: str,
discovery_info: BaseServiceInfo,
expected_port: int,
) -> None:
"""Test that discovery flow update configuration with new parameters."""
assert mock_config_entry.data == {
assert config_entry_setup.data == {
CONF_HOST: DEFAULT_HOST,
CONF_PORT: 80,
CONF_USERNAME: "root",
@ -537,7 +505,7 @@ async def test_discovery_flow_updated_configuration(
assert result["type"] is FlowResultType.ABORT
assert result["reason"] == "already_configured"
assert mock_config_entry.data == {
assert config_entry_setup.data == {
CONF_HOST: "2.3.4.5",
CONF_PORT: expected_port,
CONF_USERNAME: "root",
@ -646,7 +614,7 @@ async def test_discovery_flow_ignore_link_local_address(
async def test_option_flow(
hass: HomeAssistant, config_entry_setup: ConfigEntry
hass: HomeAssistant, config_entry_setup: MockConfigEntry
) -> None:
"""Test config flow options."""
assert CONF_STREAM_PROFILE not in config_entry_setup.options