Skip to content

Commit aacff4d

Browse files
Shutgunjoostlek
andauthored
Rework devolo Home Control config flow tests (home-assistant#147083)
Co-authored-by: Joost Lekkerkerker <[email protected]>
1 parent f833b56 commit aacff4d

File tree

3 files changed

+115
-147
lines changed

3 files changed

+115
-147
lines changed

tests/components/devolo_home_control/conftest.py

Lines changed: 11 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,25 @@
11
"""Fixtures for tests."""
22

33
from collections.abc import Generator
4+
from itertools import cycle
45
from unittest.mock import MagicMock, patch
56

67
import pytest
78

89

9-
@pytest.fixture
10-
def credentials_valid() -> bool:
11-
"""Mark test as credentials invalid."""
12-
return True
13-
14-
15-
@pytest.fixture
16-
def maintenance() -> bool:
17-
"""Mark test as maintenance mode on."""
18-
return False
19-
20-
2110
@pytest.fixture(autouse=True)
22-
def patch_mydevolo(credentials_valid: bool, maintenance: bool) -> Generator[None]:
11+
def mydevolo() -> Generator[None]:
2312
"""Fixture to patch mydevolo into a desired state."""
24-
with (
25-
patch(
26-
"homeassistant.components.devolo_home_control.Mydevolo.credentials_valid",
27-
return_value=credentials_valid,
28-
),
29-
patch(
30-
"homeassistant.components.devolo_home_control.Mydevolo.maintenance",
31-
return_value=maintenance,
32-
),
33-
patch(
34-
"homeassistant.components.devolo_home_control.Mydevolo.get_gateway_ids",
35-
return_value=["1400000000000001", "1400000000000002"],
36-
),
13+
mydevolo = MagicMock()
14+
mydevolo.uuid.return_value = "123456"
15+
mydevolo.credentials_valid.return_value = True
16+
mydevolo.maintenance.return_value = False
17+
mydevolo.get_gateway_ids.return_value = ["1400000000000001", "1400000000000002"]
18+
with patch(
19+
"homeassistant.components.devolo_home_control.Mydevolo",
20+
side_effect=cycle([mydevolo]),
3721
):
38-
yield
22+
yield mydevolo
3923

4024

4125
@pytest.fixture(autouse=True)
Lines changed: 95 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
"""Test the devolo_home_control config flow."""
22

3-
from unittest.mock import patch
4-
5-
import pytest
3+
from unittest.mock import MagicMock
64

75
from homeassistant import config_entries
86
from homeassistant.components.devolo_home_control.const import DOMAIN
7+
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
98
from homeassistant.core import HomeAssistant
10-
from homeassistant.data_entry_flow import FlowResult, FlowResultType
9+
from homeassistant.data_entry_flow import FlowResultType
1110

1211
from .const import (
1312
DISCOVERY_INFO,
@@ -20,50 +19,63 @@
2019

2120
async def test_form(hass: HomeAssistant) -> None:
2221
"""Test we get the form."""
23-
2422
result = await hass.config_entries.flow.async_init(
2523
DOMAIN, context={"source": config_entries.SOURCE_USER}
2624
)
2725
assert result["step_id"] == "user"
2826
assert result["type"] is FlowResultType.FORM
2927
assert result["errors"] == {}
3028

31-
await _setup(hass, result)
29+
result = await hass.config_entries.flow.async_configure(
30+
result["flow_id"],
31+
{CONF_USERNAME: "test-username", CONF_PASSWORD: "test-password"},
32+
)
33+
assert result["type"] is FlowResultType.CREATE_ENTRY
34+
assert result["title"] == "devolo Home Control"
35+
assert result["data"] == {
36+
CONF_USERNAME: "test-username",
37+
CONF_PASSWORD: "test-password",
38+
}
3239

3340

34-
@pytest.mark.parametrize("credentials_valid", [False])
35-
async def test_form_invalid_credentials_user(hass: HomeAssistant) -> None:
41+
async def test_form_invalid_credentials_user(
42+
hass: HomeAssistant, mydevolo: MagicMock
43+
) -> None:
3644
"""Test if we get the error message on invalid credentials."""
37-
45+
mydevolo.credentials_valid.return_value = False
3846
result = await hass.config_entries.flow.async_init(
3947
DOMAIN, context={"source": config_entries.SOURCE_USER}
4048
)
41-
assert result["step_id"] == "user"
42-
assert result["type"] is FlowResultType.FORM
43-
assert result["errors"] == {}
4449

4550
result = await hass.config_entries.flow.async_configure(
4651
result["flow_id"],
47-
{"username": "test-username", "password": "test-password"},
52+
{CONF_USERNAME: "test-username", CONF_PASSWORD: "wrong-password"},
4853
)
49-
54+
assert result["type"] is FlowResultType.FORM
5055
assert result["errors"] == {"base": "invalid_auth"}
5156

57+
mydevolo.credentials_valid.return_value = True
58+
result = await hass.config_entries.flow.async_configure(
59+
result["flow_id"],
60+
{CONF_USERNAME: "test-username", CONF_PASSWORD: "correct-password"},
61+
)
62+
assert result["type"] is FlowResultType.CREATE_ENTRY
63+
assert result["data"] == {
64+
CONF_USERNAME: "test-username",
65+
CONF_PASSWORD: "correct-password",
66+
}
67+
5268

5369
async def test_form_already_configured(hass: HomeAssistant) -> None:
5470
"""Test if we get the error message on already configured."""
55-
with patch(
56-
"homeassistant.components.devolo_home_control.Mydevolo.uuid",
57-
return_value="123456",
58-
):
59-
MockConfigEntry(domain=DOMAIN, unique_id="123456", data={}).add_to_hass(hass)
60-
result = await hass.config_entries.flow.async_init(
61-
DOMAIN,
62-
context={"source": config_entries.SOURCE_USER},
63-
data={"username": "test-username", "password": "test-password"},
64-
)
65-
assert result["type"] is FlowResultType.ABORT
66-
assert result["reason"] == "already_configured"
71+
MockConfigEntry(domain=DOMAIN, unique_id="123456", data={}).add_to_hass(hass)
72+
result = await hass.config_entries.flow.async_init(
73+
DOMAIN,
74+
context={"source": config_entries.SOURCE_USER},
75+
data={CONF_USERNAME: "test-username", CONF_PASSWORD: "test-password"},
76+
)
77+
assert result["type"] is FlowResultType.ABORT
78+
assert result["reason"] == "already_configured"
6779

6880

6981
async def test_form_zeroconf(hass: HomeAssistant) -> None:
@@ -73,32 +85,45 @@ async def test_form_zeroconf(hass: HomeAssistant) -> None:
7385
context={"source": config_entries.SOURCE_ZEROCONF},
7486
data=DISCOVERY_INFO,
7587
)
76-
7788
assert result["step_id"] == "zeroconf_confirm"
7889
assert result["type"] is FlowResultType.FORM
7990

80-
await _setup(hass, result)
91+
result = await hass.config_entries.flow.async_configure(
92+
result["flow_id"],
93+
{CONF_USERNAME: "test-username", CONF_PASSWORD: "test-password"},
94+
)
95+
assert result["type"] is FlowResultType.CREATE_ENTRY
96+
assert result["title"] == "devolo Home Control"
97+
assert result["data"] == {
98+
CONF_USERNAME: "test-username",
99+
CONF_PASSWORD: "test-password",
100+
}
81101

82102

83-
@pytest.mark.parametrize("credentials_valid", [False])
84-
async def test_form_invalid_credentials_zeroconf(hass: HomeAssistant) -> None:
103+
async def test_form_invalid_credentials_zeroconf(
104+
hass: HomeAssistant, mydevolo: MagicMock
105+
) -> None:
85106
"""Test if we get the error message on invalid credentials."""
86-
107+
mydevolo.credentials_valid.return_value = False
87108
result = await hass.config_entries.flow.async_init(
88109
DOMAIN,
89110
context={"source": config_entries.SOURCE_ZEROCONF},
90111
data=DISCOVERY_INFO,
91112
)
92113

93-
assert result["step_id"] == "zeroconf_confirm"
114+
result = await hass.config_entries.flow.async_configure(
115+
result["flow_id"],
116+
{CONF_USERNAME: "test-username", CONF_PASSWORD: "test-password"},
117+
)
94118
assert result["type"] is FlowResultType.FORM
119+
assert result["errors"] == {"base": "invalid_auth"}
95120

121+
mydevolo.credentials_valid.return_value = True
96122
result = await hass.config_entries.flow.async_configure(
97123
result["flow_id"],
98-
{"username": "test-username", "password": "test-password"},
124+
{CONF_USERNAME: "test-username", CONF_PASSWORD: "correct-password"},
99125
)
100-
101-
assert result["errors"] == {"base": "invalid_auth"}
126+
assert result["type"] is FlowResultType.CREATE_ENTRY
102127

103128

104129
async def test_zeroconf_wrong_device(hass: HomeAssistant) -> None:
@@ -108,7 +133,6 @@ async def test_zeroconf_wrong_device(hass: HomeAssistant) -> None:
108133
context={"source": config_entries.SOURCE_ZEROCONF},
109134
data=DISCOVERY_INFO_WRONG_DEVOLO_DEVICE,
110135
)
111-
112136
assert result["reason"] == "Not a devolo Home Control gateway."
113137
assert result["type"] is FlowResultType.ABORT
114138

@@ -128,116 +152,73 @@ async def test_form_reauth(hass: HomeAssistant) -> None:
128152
domain=DOMAIN,
129153
unique_id="123456",
130154
data={
131-
"username": "test-username",
132-
"password": "test-password",
155+
CONF_USERNAME: "test-username",
156+
CONF_PASSWORD: "test-password",
133157
},
134158
)
135159
mock_config.add_to_hass(hass)
136160
result = await mock_config.start_reauth_flow(hass)
137161
assert result["step_id"] == "reauth_confirm"
138162
assert result["type"] is FlowResultType.FORM
139163

140-
with (
141-
patch(
142-
"homeassistant.components.devolo_home_control.async_setup_entry",
143-
return_value=True,
144-
) as mock_setup_entry,
145-
patch(
146-
"homeassistant.components.devolo_home_control.Mydevolo.uuid",
147-
return_value="123456",
148-
),
149-
):
150-
result2 = await hass.config_entries.flow.async_configure(
151-
result["flow_id"],
152-
{"username": "test-username-new", "password": "test-password-new"},
153-
)
154-
await hass.async_block_till_done()
155-
156-
assert result2["type"] is FlowResultType.ABORT
157-
assert len(mock_setup_entry.mock_calls) == 1
158-
159-
160-
@pytest.mark.parametrize("credentials_valid", [False])
161-
async def test_form_invalid_credentials_reauth(hass: HomeAssistant) -> None:
164+
result = await hass.config_entries.flow.async_configure(
165+
result["flow_id"],
166+
{CONF_USERNAME: "test-username-new", CONF_PASSWORD: "test-password-new"},
167+
)
168+
assert result["type"] is FlowResultType.ABORT
169+
assert result["reason"] == "reauth_successful"
170+
171+
172+
async def test_form_invalid_credentials_reauth(
173+
hass: HomeAssistant, mydevolo: MagicMock
174+
) -> None:
162175
"""Test if we get the error message on invalid credentials."""
176+
mydevolo.credentials_valid.return_value = False
163177
mock_config = MockConfigEntry(
164178
domain=DOMAIN,
165179
unique_id="123456",
166180
data={
167-
"username": "test-username",
168-
"password": "test-password",
181+
CONF_USERNAME: "test-username",
182+
CONF_PASSWORD: "test-password",
169183
},
170184
)
171185
mock_config.add_to_hass(hass)
172186
result = await mock_config.start_reauth_flow(hass)
173187

174188
result = await hass.config_entries.flow.async_configure(
175189
result["flow_id"],
176-
{"username": "test-username", "password": "test-password"},
190+
{CONF_USERNAME: "test-username", CONF_PASSWORD: "wrong-password"},
177191
)
178-
179192
assert result["errors"] == {"base": "invalid_auth"}
193+
assert result["type"] is FlowResultType.FORM
194+
195+
mydevolo.credentials_valid.return_value = True
196+
result = await hass.config_entries.flow.async_configure(
197+
result["flow_id"],
198+
{CONF_USERNAME: "test-username-new", CONF_PASSWORD: "correct-password"},
199+
)
200+
assert result["reason"] == "reauth_successful"
201+
assert result["type"] is FlowResultType.ABORT
180202

181203

182204
async def test_form_uuid_change_reauth(hass: HomeAssistant) -> None:
183205
"""Test that the reauth confirmation form is served."""
184206
mock_config = MockConfigEntry(
185207
domain=DOMAIN,
186-
unique_id="123456",
208+
unique_id="123457",
187209
data={
188-
"username": "test-username",
189-
"password": "test-password",
210+
CONF_USERNAME: "test-username",
211+
CONF_PASSWORD: "test-password",
190212
},
191213
)
192214
mock_config.add_to_hass(hass)
193215
result = await mock_config.start_reauth_flow(hass)
194-
195216
assert result["step_id"] == "reauth_confirm"
196217
assert result["type"] is FlowResultType.FORM
197218

198-
with (
199-
patch(
200-
"homeassistant.components.devolo_home_control.async_setup_entry",
201-
return_value=True,
202-
),
203-
patch(
204-
"homeassistant.components.devolo_home_control.Mydevolo.uuid",
205-
return_value="789123",
206-
),
207-
):
208-
result2 = await hass.config_entries.flow.async_configure(
209-
result["flow_id"],
210-
{"username": "test-username-new", "password": "test-password-new"},
211-
)
212-
await hass.async_block_till_done()
213-
214-
assert result2["type"] is FlowResultType.FORM
215-
assert result2["errors"] == {"base": "reauth_failed"}
216-
217-
218-
async def _setup(hass: HomeAssistant, result: FlowResult) -> None:
219-
"""Finish configuration steps."""
220-
with (
221-
patch(
222-
"homeassistant.components.devolo_home_control.async_setup_entry",
223-
return_value=True,
224-
) as mock_setup_entry,
225-
patch(
226-
"homeassistant.components.devolo_home_control.Mydevolo.uuid",
227-
return_value="123456",
228-
),
229-
):
230-
result2 = await hass.config_entries.flow.async_configure(
231-
result["flow_id"],
232-
{"username": "test-username", "password": "test-password"},
233-
)
234-
await hass.async_block_till_done()
235-
236-
assert result2["type"] is FlowResultType.CREATE_ENTRY
237-
assert result2["title"] == "devolo Home Control"
238-
assert result2["data"] == {
239-
"username": "test-username",
240-
"password": "test-password",
241-
}
242-
243-
assert len(mock_setup_entry.mock_calls) == 1
219+
result = await hass.config_entries.flow.async_configure(
220+
result["flow_id"],
221+
{CONF_USERNAME: "test-username-new", CONF_PASSWORD: "test-password-new"},
222+
)
223+
assert result["type"] is FlowResultType.FORM
224+
assert result["errors"] == {"base": "reauth_failed"}

0 commit comments

Comments
 (0)