Skip to content

Commit a88599b

Browse files
epenetCopilot
andauthored
Improve tests in SFR Box (home-assistant#157444)
Co-authored-by: Copilot <[email protected]>
1 parent 4503427 commit a88599b

File tree

4 files changed

+131
-27
lines changed

4 files changed

+131
-27
lines changed

homeassistant/components/sfr_box/quality_scale.yaml

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@ rules:
55
unique-config-entry: done
66
config-flow-test-coverage:
77
status: todo
8-
comment: |
9-
- test_config_flow_skip_auth -> I'd split the happy from the not happy flows
10-
- We should test created mac address
8+
comment: We should test created mac address
119
runtime-data: done
1210
test-before-setup: done
1311
appropriate-polling: done
@@ -40,10 +38,8 @@ rules:
4038
status: todo
4139
comment: |
4240
- 93% on diagnostics / 92% on sensors, need to improve overall coverage
43-
- you can use load_json_object_fixture
4441
- It would be nice to use the snapshot helper as currently it would just throw everything in a list
4542
- We also test the devices in each platform, kinda overkill
46-
- assert not hass.data.get(DOMAIN) not needed
4743
- We should use entity_registry_enabled_by_default instead to enable entities
4844
integration-owner: done
4945
docs-installation-parameters:

tests/components/sfr_box/conftest.py

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
"""Provide common SFR Box fixtures."""
22

3-
from collections.abc import Generator
4-
import json
3+
from collections.abc import AsyncGenerator, Generator
54
from unittest.mock import AsyncMock, patch
65

76
import pytest
@@ -12,7 +11,7 @@
1211
from homeassistant.const import CONF_HOST, CONF_PASSWORD, CONF_USERNAME
1312
from homeassistant.core import HomeAssistant
1413

15-
from tests.common import MockConfigEntry, load_fixture
14+
from tests.common import MockConfigEntry, async_load_json_object_fixture
1615

1716

1817
@pytest.fixture
@@ -59,9 +58,11 @@ def get_config_entry_with_auth(hass: HomeAssistant) -> ConfigEntry:
5958

6059

6160
@pytest.fixture
62-
def dsl_get_info() -> Generator[DslInfo]:
61+
async def dsl_get_info(hass: HomeAssistant) -> AsyncGenerator[DslInfo]:
6362
"""Fixture for SFRBox.dsl_get_info."""
64-
dsl_info = DslInfo(**json.loads(load_fixture("dsl_getInfo.json", DOMAIN)))
63+
dsl_info = DslInfo(
64+
**(await async_load_json_object_fixture(hass, "dsl_getInfo.json", DOMAIN))
65+
)
6566
with patch(
6667
"homeassistant.components.sfr_box.coordinator.SFRBox.dsl_get_info",
6768
return_value=dsl_info,
@@ -70,9 +71,11 @@ def dsl_get_info() -> Generator[DslInfo]:
7071

7172

7273
@pytest.fixture
73-
def ftth_get_info() -> Generator[FtthInfo]:
74+
async def ftth_get_info(hass: HomeAssistant) -> AsyncGenerator[FtthInfo]:
7475
"""Fixture for SFRBox.ftth_get_info."""
75-
info = FtthInfo(**json.loads(load_fixture("ftth_getInfo.json", DOMAIN)))
76+
info = FtthInfo(
77+
**(await async_load_json_object_fixture(hass, "ftth_getInfo.json", DOMAIN))
78+
)
7679
with patch(
7780
"homeassistant.components.sfr_box.coordinator.SFRBox.ftth_get_info",
7881
return_value=info,
@@ -81,9 +84,11 @@ def ftth_get_info() -> Generator[FtthInfo]:
8184

8285

8386
@pytest.fixture
84-
def system_get_info() -> Generator[SystemInfo]:
87+
async def system_get_info(hass: HomeAssistant) -> AsyncGenerator[SystemInfo]:
8588
"""Fixture for SFRBox.system_get_info."""
86-
info = SystemInfo(**json.loads(load_fixture("system_getInfo.json", DOMAIN)))
89+
info = SystemInfo(
90+
**(await async_load_json_object_fixture(hass, "system_getInfo.json", DOMAIN))
91+
)
8792
with patch(
8893
"homeassistant.components.sfr_box.coordinator.SFRBox.system_get_info",
8994
return_value=info,
@@ -92,9 +97,11 @@ def system_get_info() -> Generator[SystemInfo]:
9297

9398

9499
@pytest.fixture
95-
def wan_get_info() -> Generator[WanInfo]:
100+
async def wan_get_info(hass: HomeAssistant) -> AsyncGenerator[WanInfo]:
96101
"""Fixture for SFRBox.wan_get_info."""
97-
info = WanInfo(**json.loads(load_fixture("wan_getInfo.json", DOMAIN)))
102+
info = WanInfo(
103+
**(await async_load_json_object_fixture(hass, "wan_getInfo.json", DOMAIN))
104+
)
98105
with patch(
99106
"homeassistant.components.sfr_box.coordinator.SFRBox.wan_get_info",
100107
return_value=info,

tests/components/sfr_box/test_config_flow.py

Lines changed: 112 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
"""Test the SFR Box config flow."""
22

3-
import json
43
from unittest.mock import AsyncMock, patch
54

65
import pytest
@@ -14,15 +13,57 @@
1413
from homeassistant.core import HomeAssistant
1514
from homeassistant.data_entry_flow import FlowResultType
1615

17-
from tests.common import async_load_fixture
16+
from tests.common import async_load_json_object_fixture
1817

1918
pytestmark = pytest.mark.usefixtures("mock_setup_entry")
2019

2120

2221
async def test_config_flow_skip_auth(
2322
hass: HomeAssistant, mock_setup_entry: AsyncMock
2423
) -> None:
25-
"""Test we get the form."""
24+
"""Test user flow (no authentication)."""
25+
result = await hass.config_entries.flow.async_init(
26+
DOMAIN, context={"source": config_entries.SOURCE_USER}
27+
)
28+
assert result["type"] is FlowResultType.FORM
29+
assert result["errors"] == {}
30+
31+
with patch(
32+
"homeassistant.components.sfr_box.config_flow.SFRBox.system_get_info",
33+
return_value=SystemInfo(
34+
**(
35+
await async_load_json_object_fixture(
36+
hass, "system_getInfo.json", DOMAIN
37+
)
38+
)
39+
),
40+
):
41+
result = await hass.config_entries.flow.async_configure(
42+
result["flow_id"],
43+
user_input={
44+
CONF_HOST: "192.168.0.1",
45+
},
46+
)
47+
48+
assert result["type"] is FlowResultType.MENU
49+
assert result["step_id"] == "choose_auth"
50+
51+
result = await hass.config_entries.flow.async_configure(
52+
result["flow_id"],
53+
{"next_step_id": "skip_auth"},
54+
)
55+
56+
assert result["type"] is FlowResultType.CREATE_ENTRY
57+
assert result["title"] == "SFR Box"
58+
assert result["data"] == {CONF_HOST: "192.168.0.1"}
59+
60+
assert len(mock_setup_entry.mock_calls) == 1
61+
62+
63+
async def test_config_flow_skip_auth_failure(
64+
hass: HomeAssistant, mock_setup_entry: AsyncMock
65+
) -> None:
66+
"""Test user flow (no authentication) with failure and recovery."""
2667
result = await hass.config_entries.flow.async_init(
2768
DOMAIN, context={"source": config_entries.SOURCE_USER}
2869
)
@@ -46,7 +87,11 @@ async def test_config_flow_skip_auth(
4687
with patch(
4788
"homeassistant.components.sfr_box.config_flow.SFRBox.system_get_info",
4889
return_value=SystemInfo(
49-
**json.loads(await async_load_fixture(hass, "system_getInfo.json", DOMAIN))
90+
**(
91+
await async_load_json_object_fixture(
92+
hass, "system_getInfo.json", DOMAIN
93+
)
94+
)
5095
),
5196
):
5297
result = await hass.config_entries.flow.async_configure(
@@ -74,7 +119,62 @@ async def test_config_flow_skip_auth(
74119
async def test_config_flow_with_auth(
75120
hass: HomeAssistant, mock_setup_entry: AsyncMock
76121
) -> None:
77-
"""Test we get the form."""
122+
"""Test user flow (with authentication)."""
123+
result = await hass.config_entries.flow.async_init(
124+
DOMAIN, context={"source": config_entries.SOURCE_USER}
125+
)
126+
assert result["type"] is FlowResultType.FORM
127+
assert result["errors"] == {}
128+
129+
with patch(
130+
"homeassistant.components.sfr_box.config_flow.SFRBox.system_get_info",
131+
return_value=SystemInfo(
132+
**(
133+
await async_load_json_object_fixture(
134+
hass, "system_getInfo.json", DOMAIN
135+
)
136+
)
137+
),
138+
):
139+
result = await hass.config_entries.flow.async_configure(
140+
result["flow_id"],
141+
user_input={
142+
CONF_HOST: "192.168.0.1",
143+
},
144+
)
145+
146+
assert result["type"] is FlowResultType.MENU
147+
assert result["step_id"] == "choose_auth"
148+
149+
result = await hass.config_entries.flow.async_configure(
150+
result["flow_id"],
151+
{"next_step_id": "auth"},
152+
)
153+
154+
with patch("homeassistant.components.sfr_box.config_flow.SFRBox.authenticate"):
155+
result = await hass.config_entries.flow.async_configure(
156+
result["flow_id"],
157+
user_input={
158+
CONF_USERNAME: "admin",
159+
CONF_PASSWORD: "valid",
160+
},
161+
)
162+
163+
assert result["type"] is FlowResultType.CREATE_ENTRY
164+
assert result["title"] == "SFR Box"
165+
assert result["data"] == {
166+
CONF_HOST: "192.168.0.1",
167+
CONF_USERNAME: "admin",
168+
CONF_PASSWORD: "valid",
169+
}
170+
171+
assert len(mock_setup_entry.mock_calls) == 1
172+
173+
174+
async def test_config_flow_with_auth_failure(
175+
hass: HomeAssistant, mock_setup_entry: AsyncMock
176+
) -> None:
177+
"""Test user flow (with authentication) with failure and recovery."""
78178
result = await hass.config_entries.flow.async_init(
79179
DOMAIN, context={"source": config_entries.SOURCE_USER}
80180
)
@@ -84,7 +184,11 @@ async def test_config_flow_with_auth(
84184
with patch(
85185
"homeassistant.components.sfr_box.config_flow.SFRBox.system_get_info",
86186
return_value=SystemInfo(
87-
**json.loads(await async_load_fixture(hass, "system_getInfo.json", DOMAIN))
187+
**(
188+
await async_load_json_object_fixture(
189+
hass, "system_getInfo.json", DOMAIN
190+
)
191+
)
88192
),
89193
):
90194
result = await hass.config_entries.flow.async_configure(
@@ -151,7 +255,7 @@ async def test_config_flow_duplicate_host(
151255
assert result["errors"] == {}
152256

153257
system_info = SystemInfo(
154-
**json.loads(await async_load_fixture(hass, "system_getInfo.json", DOMAIN))
258+
**(await async_load_json_object_fixture(hass, "system_getInfo.json", DOMAIN))
155259
)
156260
# Ensure mac doesn't match existing mock entry
157261
system_info.mac_addr = "aa:bb:cc:dd:ee:ff"
@@ -187,7 +291,7 @@ async def test_config_flow_duplicate_mac(
187291
assert result["errors"] == {}
188292

189293
system_info = SystemInfo(
190-
**json.loads(await async_load_fixture(hass, "system_getInfo.json", DOMAIN))
294+
**(await async_load_json_object_fixture(hass, "system_getInfo.json", DOMAIN))
191295
)
192296
with patch(
193297
"homeassistant.components.sfr_box.config_flow.SFRBox.system_get_info",

tests/components/sfr_box/test_init.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ async def test_setup_entry_exception(
4848

4949
assert len(hass.config_entries.async_entries(DOMAIN)) == 1
5050
assert config_entry.state is ConfigEntryState.SETUP_RETRY
51-
assert not hass.data.get(DOMAIN)
5251

5352

5453
async def test_setup_entry_auth_exception(
@@ -64,7 +63,6 @@ async def test_setup_entry_auth_exception(
6463

6564
assert len(hass.config_entries.async_entries(DOMAIN)) == 1
6665
assert config_entry_with_auth.state is ConfigEntryState.SETUP_RETRY
67-
assert not hass.data.get(DOMAIN)
6866

6967

7068
async def test_setup_entry_invalid_auth(
@@ -80,4 +78,3 @@ async def test_setup_entry_invalid_auth(
8078

8179
assert len(hass.config_entries.async_entries(DOMAIN)) == 1
8280
assert config_entry_with_auth.state is ConfigEntryState.SETUP_ERROR
83-
assert not hass.data.get(DOMAIN)

0 commit comments

Comments
 (0)