Improve tests in SFR Box (#157444)

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
This commit is contained in:
epenet
2025-11-28 10:10:38 +01:00
committed by GitHub
parent 45034279c8
commit a88599bc09
4 changed files with 131 additions and 27 deletions

View File

@@ -5,9 +5,7 @@ rules:
unique-config-entry: done
config-flow-test-coverage:
status: todo
comment: |
- test_config_flow_skip_auth -> I'd split the happy from the not happy flows
- We should test created mac address
comment: We should test created mac address
runtime-data: done
test-before-setup: done
appropriate-polling: done
@@ -40,10 +38,8 @@ rules:
status: todo
comment: |
- 93% on diagnostics / 92% on sensors, need to improve overall coverage
- you can use load_json_object_fixture
- It would be nice to use the snapshot helper as currently it would just throw everything in a list
- We also test the devices in each platform, kinda overkill
- assert not hass.data.get(DOMAIN) not needed
- We should use entity_registry_enabled_by_default instead to enable entities
integration-owner: done
docs-installation-parameters:

View File

@@ -1,7 +1,6 @@
"""Provide common SFR Box fixtures."""
from collections.abc import Generator
import json
from collections.abc import AsyncGenerator, Generator
from unittest.mock import AsyncMock, patch
import pytest
@@ -12,7 +11,7 @@ from homeassistant.config_entries import SOURCE_USER, ConfigEntry
from homeassistant.const import CONF_HOST, CONF_PASSWORD, CONF_USERNAME
from homeassistant.core import HomeAssistant
from tests.common import MockConfigEntry, load_fixture
from tests.common import MockConfigEntry, async_load_json_object_fixture
@pytest.fixture
@@ -59,9 +58,11 @@ def get_config_entry_with_auth(hass: HomeAssistant) -> ConfigEntry:
@pytest.fixture
def dsl_get_info() -> Generator[DslInfo]:
async def dsl_get_info(hass: HomeAssistant) -> AsyncGenerator[DslInfo]:
"""Fixture for SFRBox.dsl_get_info."""
dsl_info = DslInfo(**json.loads(load_fixture("dsl_getInfo.json", DOMAIN)))
dsl_info = DslInfo(
**(await async_load_json_object_fixture(hass, "dsl_getInfo.json", DOMAIN))
)
with patch(
"homeassistant.components.sfr_box.coordinator.SFRBox.dsl_get_info",
return_value=dsl_info,
@@ -70,9 +71,11 @@ def dsl_get_info() -> Generator[DslInfo]:
@pytest.fixture
def ftth_get_info() -> Generator[FtthInfo]:
async def ftth_get_info(hass: HomeAssistant) -> AsyncGenerator[FtthInfo]:
"""Fixture for SFRBox.ftth_get_info."""
info = FtthInfo(**json.loads(load_fixture("ftth_getInfo.json", DOMAIN)))
info = FtthInfo(
**(await async_load_json_object_fixture(hass, "ftth_getInfo.json", DOMAIN))
)
with patch(
"homeassistant.components.sfr_box.coordinator.SFRBox.ftth_get_info",
return_value=info,
@@ -81,9 +84,11 @@ def ftth_get_info() -> Generator[FtthInfo]:
@pytest.fixture
def system_get_info() -> Generator[SystemInfo]:
async def system_get_info(hass: HomeAssistant) -> AsyncGenerator[SystemInfo]:
"""Fixture for SFRBox.system_get_info."""
info = SystemInfo(**json.loads(load_fixture("system_getInfo.json", DOMAIN)))
info = SystemInfo(
**(await async_load_json_object_fixture(hass, "system_getInfo.json", DOMAIN))
)
with patch(
"homeassistant.components.sfr_box.coordinator.SFRBox.system_get_info",
return_value=info,
@@ -92,9 +97,11 @@ def system_get_info() -> Generator[SystemInfo]:
@pytest.fixture
def wan_get_info() -> Generator[WanInfo]:
async def wan_get_info(hass: HomeAssistant) -> AsyncGenerator[WanInfo]:
"""Fixture for SFRBox.wan_get_info."""
info = WanInfo(**json.loads(load_fixture("wan_getInfo.json", DOMAIN)))
info = WanInfo(
**(await async_load_json_object_fixture(hass, "wan_getInfo.json", DOMAIN))
)
with patch(
"homeassistant.components.sfr_box.coordinator.SFRBox.wan_get_info",
return_value=info,

View File

@@ -1,6 +1,5 @@
"""Test the SFR Box config flow."""
import json
from unittest.mock import AsyncMock, patch
import pytest
@@ -14,7 +13,7 @@ from homeassistant.const import CONF_HOST, CONF_PASSWORD, CONF_USERNAME
from homeassistant.core import HomeAssistant
from homeassistant.data_entry_flow import FlowResultType
from tests.common import async_load_fixture
from tests.common import async_load_json_object_fixture
pytestmark = pytest.mark.usefixtures("mock_setup_entry")
@@ -22,7 +21,49 @@ pytestmark = pytest.mark.usefixtures("mock_setup_entry")
async def test_config_flow_skip_auth(
hass: HomeAssistant, mock_setup_entry: AsyncMock
) -> None:
"""Test we get the form."""
"""Test user flow (no authentication)."""
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
assert result["type"] is FlowResultType.FORM
assert result["errors"] == {}
with patch(
"homeassistant.components.sfr_box.config_flow.SFRBox.system_get_info",
return_value=SystemInfo(
**(
await async_load_json_object_fixture(
hass, "system_getInfo.json", DOMAIN
)
)
),
):
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
user_input={
CONF_HOST: "192.168.0.1",
},
)
assert result["type"] is FlowResultType.MENU
assert result["step_id"] == "choose_auth"
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
{"next_step_id": "skip_auth"},
)
assert result["type"] is FlowResultType.CREATE_ENTRY
assert result["title"] == "SFR Box"
assert result["data"] == {CONF_HOST: "192.168.0.1"}
assert len(mock_setup_entry.mock_calls) == 1
async def test_config_flow_skip_auth_failure(
hass: HomeAssistant, mock_setup_entry: AsyncMock
) -> None:
"""Test user flow (no authentication) with failure and recovery."""
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
@@ -46,7 +87,11 @@ async def test_config_flow_skip_auth(
with patch(
"homeassistant.components.sfr_box.config_flow.SFRBox.system_get_info",
return_value=SystemInfo(
**json.loads(await async_load_fixture(hass, "system_getInfo.json", DOMAIN))
**(
await async_load_json_object_fixture(
hass, "system_getInfo.json", DOMAIN
)
)
),
):
result = await hass.config_entries.flow.async_configure(
@@ -74,7 +119,7 @@ async def test_config_flow_skip_auth(
async def test_config_flow_with_auth(
hass: HomeAssistant, mock_setup_entry: AsyncMock
) -> None:
"""Test we get the form."""
"""Test user flow (with authentication)."""
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
@@ -84,7 +129,66 @@ async def test_config_flow_with_auth(
with patch(
"homeassistant.components.sfr_box.config_flow.SFRBox.system_get_info",
return_value=SystemInfo(
**json.loads(await async_load_fixture(hass, "system_getInfo.json", DOMAIN))
**(
await async_load_json_object_fixture(
hass, "system_getInfo.json", DOMAIN
)
)
),
):
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
user_input={
CONF_HOST: "192.168.0.1",
},
)
assert result["type"] is FlowResultType.MENU
assert result["step_id"] == "choose_auth"
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
{"next_step_id": "auth"},
)
with patch("homeassistant.components.sfr_box.config_flow.SFRBox.authenticate"):
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
user_input={
CONF_USERNAME: "admin",
CONF_PASSWORD: "valid",
},
)
assert result["type"] is FlowResultType.CREATE_ENTRY
assert result["title"] == "SFR Box"
assert result["data"] == {
CONF_HOST: "192.168.0.1",
CONF_USERNAME: "admin",
CONF_PASSWORD: "valid",
}
assert len(mock_setup_entry.mock_calls) == 1
async def test_config_flow_with_auth_failure(
hass: HomeAssistant, mock_setup_entry: AsyncMock
) -> None:
"""Test user flow (with authentication) with failure and recovery."""
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
assert result["type"] is FlowResultType.FORM
assert result["errors"] == {}
with patch(
"homeassistant.components.sfr_box.config_flow.SFRBox.system_get_info",
return_value=SystemInfo(
**(
await async_load_json_object_fixture(
hass, "system_getInfo.json", DOMAIN
)
)
),
):
result = await hass.config_entries.flow.async_configure(
@@ -151,7 +255,7 @@ async def test_config_flow_duplicate_host(
assert result["errors"] == {}
system_info = SystemInfo(
**json.loads(await async_load_fixture(hass, "system_getInfo.json", DOMAIN))
**(await async_load_json_object_fixture(hass, "system_getInfo.json", DOMAIN))
)
# Ensure mac doesn't match existing mock entry
system_info.mac_addr = "aa:bb:cc:dd:ee:ff"
@@ -187,7 +291,7 @@ async def test_config_flow_duplicate_mac(
assert result["errors"] == {}
system_info = SystemInfo(
**json.loads(await async_load_fixture(hass, "system_getInfo.json", DOMAIN))
**(await async_load_json_object_fixture(hass, "system_getInfo.json", DOMAIN))
)
with patch(
"homeassistant.components.sfr_box.config_flow.SFRBox.system_get_info",

View File

@@ -48,7 +48,6 @@ async def test_setup_entry_exception(
assert len(hass.config_entries.async_entries(DOMAIN)) == 1
assert config_entry.state is ConfigEntryState.SETUP_RETRY
assert not hass.data.get(DOMAIN)
async def test_setup_entry_auth_exception(
@@ -64,7 +63,6 @@ async def test_setup_entry_auth_exception(
assert len(hass.config_entries.async_entries(DOMAIN)) == 1
assert config_entry_with_auth.state is ConfigEntryState.SETUP_RETRY
assert not hass.data.get(DOMAIN)
async def test_setup_entry_invalid_auth(
@@ -80,4 +78,3 @@ async def test_setup_entry_invalid_auth(
assert len(hass.config_entries.async_entries(DOMAIN)) == 1
assert config_entry_with_auth.state is ConfigEntryState.SETUP_ERROR
assert not hass.data.get(DOMAIN)