Skip to content

Commit 103b121

Browse files
authored
Enhance Switcher config flow tests (home-assistant#155292)
1 parent 76a6b3c commit 103b121

File tree

2 files changed

+39
-8
lines changed

2 files changed

+39
-8
lines changed

homeassistant/components/switcher_kis/quality_scale.yaml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@ rules:
88
comment: The integration does not poll.
99
brands: done
1010
common-modules: done
11-
config-flow-test-coverage:
12-
status: todo
13-
comment: make sure flows end with created entry or abort
11+
config-flow-test-coverage: done
1412
config-flow: done
1513
dependency-transparency: done
1614
docs-actions: done

tests/components/switcher_kis/test_config_flow.py

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

3-
from unittest.mock import AsyncMock, patch
3+
from unittest.mock import AsyncMock, MagicMock, patch
44

55
import pytest
66

@@ -35,7 +35,7 @@
3535
indirect=True,
3636
)
3737
async def test_user_setup(
38-
hass: HomeAssistant, mock_setup_entry: AsyncMock, mock_bridge
38+
hass: HomeAssistant, mock_setup_entry: AsyncMock, mock_bridge: MagicMock
3939
) -> None:
4040
"""Test we can finish a config flow."""
4141
with patch("homeassistant.components.switcher_kis.utils.DISCOVERY_TIME_SEC", 0):
@@ -69,7 +69,7 @@ async def test_user_setup(
6969
indirect=True,
7070
)
7171
async def test_user_setup_found_token_device_valid_token(
72-
hass: HomeAssistant, mock_setup_entry: AsyncMock, mock_bridge
72+
hass: HomeAssistant, mock_setup_entry: AsyncMock, mock_bridge: MagicMock
7373
) -> None:
7474
"""Test we can finish a config flow with token device found."""
7575
with patch("homeassistant.components.switcher_kis.utils.DISCOVERY_TIME_SEC", 0):
@@ -102,6 +102,8 @@ async def test_user_setup_found_token_device_valid_token(
102102
CONF_TOKEN: DUMMY_TOKEN,
103103
}
104104

105+
assert len(mock_setup_entry.mock_calls) == 1
106+
105107

106108
@pytest.mark.parametrize(
107109
"mock_bridge",
@@ -113,8 +115,9 @@ async def test_user_setup_found_token_device_valid_token(
113115
],
114116
indirect=True,
115117
)
118+
@pytest.mark.usefixtures("mock_bridge")
116119
async def test_user_setup_found_token_device_invalid_token(
117-
hass: HomeAssistant, mock_setup_entry: AsyncMock, mock_bridge
120+
hass: HomeAssistant, mock_setup_entry: AsyncMock
118121
) -> None:
119122
"""Test we can finish a config flow with token device found."""
120123
with patch("homeassistant.components.switcher_kis.utils.DISCOVERY_TIME_SEC", 0):
@@ -142,9 +145,27 @@ async def test_user_setup_found_token_device_invalid_token(
142145
assert result3["type"] is FlowResultType.FORM
143146
assert result3["errors"] == {"base": "invalid_auth"}
144147

148+
with patch(
149+
"homeassistant.components.switcher_kis.config_flow.validate_token",
150+
return_value=True,
151+
):
152+
result4 = await hass.config_entries.flow.async_configure(
153+
result3["flow_id"],
154+
{CONF_USERNAME: DUMMY_USERNAME, CONF_TOKEN: DUMMY_TOKEN},
155+
)
156+
157+
assert result4["type"] is FlowResultType.CREATE_ENTRY
158+
assert result4["title"] == "Switcher"
159+
assert result4["result"].data == {
160+
CONF_USERNAME: DUMMY_USERNAME,
161+
CONF_TOKEN: DUMMY_TOKEN,
162+
}
163+
164+
assert len(mock_setup_entry.mock_calls) == 1
165+
145166

146167
async def test_user_setup_abort_no_devices_found(
147-
hass: HomeAssistant, mock_setup_entry: AsyncMock, mock_bridge
168+
hass: HomeAssistant, mock_setup_entry: AsyncMock, mock_bridge: MagicMock
148169
) -> None:
149170
"""Test we abort a config flow if no devices found."""
150171
with patch("homeassistant.components.switcher_kis.utils.DISCOVERY_TIME_SEC", 0):
@@ -236,3 +257,15 @@ async def test_reauth_invalid_auth(hass: HomeAssistant) -> None:
236257

237258
assert result2["type"] is FlowResultType.FORM
238259
assert result2["errors"] == {"base": "invalid_auth"}
260+
261+
with patch(
262+
"homeassistant.components.switcher_kis.config_flow.validate_token",
263+
return_value=True,
264+
):
265+
result3 = await hass.config_entries.flow.async_configure(
266+
result2["flow_id"],
267+
{CONF_USERNAME: DUMMY_USERNAME, CONF_TOKEN: DUMMY_TOKEN},
268+
)
269+
270+
assert result3["type"] is FlowResultType.ABORT
271+
assert result3["reason"] == "reauth_successful"

0 commit comments

Comments
 (0)