Skip to content

Commit f55fc78

Browse files
authored
Cleanup homelink config flow (home-assistant#158479)
1 parent 6152e0f commit f55fc78

File tree

3 files changed

+98
-72
lines changed

3 files changed

+98
-72
lines changed

homeassistant/components/gentex_homelink/config_flow.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from homelink.auth.srp_auth import SRPAuth
88
import voluptuous as vol
99

10-
from homeassistant import config_entries
10+
from homeassistant.config_entries import ConfigFlowResult
1111
from homeassistant.const import CONF_EMAIL, CONF_PASSWORD
1212
from homeassistant.helpers.config_entry_oauth2_flow import AbstractOAuth2FlowHandler
1313

@@ -34,7 +34,7 @@ def logger(self):
3434

3535
async def async_step_user(
3636
self, user_input: dict[str, Any] | None = None
37-
) -> config_entries.ConfigFlowResult:
37+
) -> ConfigFlowResult:
3838
"""Ask for username and password."""
3939
errors: dict[str, str] = {}
4040
if user_input is not None:
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
"""Fixtures for Gentex HomeLink tests."""
2+
3+
from collections.abc import Generator
4+
from unittest.mock import AsyncMock, patch
5+
6+
import pytest
7+
8+
9+
@pytest.fixture
10+
def mock_srp_auth() -> Generator[AsyncMock]:
11+
"""Mock SRP authentication."""
12+
with patch(
13+
"homeassistant.components.gentex_homelink.config_flow.SRPAuth"
14+
) as mock_srp_auth:
15+
instance = mock_srp_auth.return_value
16+
instance.async_get_access_token.return_value = {
17+
"AuthenticationResult": {
18+
"AccessToken": "access",
19+
"RefreshToken": "refresh",
20+
"TokenType": "bearer",
21+
"ExpiresIn": 3600,
22+
}
23+
}
24+
yield instance
25+
26+
27+
@pytest.fixture
28+
def mock_setup_entry() -> Generator[AsyncMock]:
29+
"""Mock setup entry."""
30+
with patch(
31+
"homeassistant.components.gentex_homelink.async_setup_entry", return_value=True
32+
) as mock_setup_entry:
33+
yield mock_setup_entry
Lines changed: 63 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,91 +1,84 @@
11
"""Test the homelink config flow."""
22

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

55
import botocore.exceptions
6+
import pytest
67

7-
from homeassistant import config_entries
88
from homeassistant.components.gentex_homelink.const import DOMAIN
9+
from homeassistant.config_entries import SOURCE_USER
10+
from homeassistant.const import CONF_EMAIL, CONF_PASSWORD
911
from homeassistant.core import HomeAssistant
1012
from homeassistant.data_entry_flow import FlowResultType
1113

1214

13-
async def test_show_user_form(hass: HomeAssistant) -> None:
14-
"""Test that the user set up form is served."""
15+
async def test_full_flow(
16+
hass: HomeAssistant, mock_srp_auth: AsyncMock, mock_setup_entry: AsyncMock
17+
) -> None:
18+
"""Check full flow."""
1519
result = await hass.config_entries.flow.async_init(
16-
DOMAIN,
17-
context={"source": config_entries.SOURCE_USER},
20+
DOMAIN, context={"source": SOURCE_USER}
1821
)
1922

23+
assert result["type"] is FlowResultType.FORM
2024
assert result["step_id"] == "user"
21-
assert result["type"] == FlowResultType.FORM
22-
23-
24-
async def test_full_flow(hass: HomeAssistant) -> None:
25-
"""Check full flow."""
26-
with patch(
27-
"homeassistant.components.gentex_homelink.config_flow.SRPAuth"
28-
) as MockSRPAuth:
29-
instance = MockSRPAuth.return_value
30-
instance.async_get_access_token.return_value = {
31-
"AuthenticationResult": {
32-
"AccessToken": "access",
33-
"RefreshToken": "refresh",
34-
"TokenType": "bearer",
35-
"ExpiresIn": 3600,
36-
}
37-
}
38-
result = await hass.config_entries.flow.async_init(
39-
DOMAIN, context={"source": config_entries.SOURCE_USER}
40-
)
41-
result = await hass.config_entries.flow.async_configure(
42-
result["flow_id"],
43-
user_input={"email": "[email protected]", "password": "SomePassword"},
44-
)
45-
assert result["type"] == FlowResultType.CREATE_ENTRY
46-
assert result["data"]
47-
assert result["data"]["token"]
48-
assert result["data"]["token"]["access_token"] == "access"
49-
assert result["data"]["token"]["refresh_token"] == "refresh"
50-
assert result["data"]["token"]["expires_in"] == 3600
51-
assert result["data"]["token"]["expires_at"]
52-
25+
assert not result["errors"]
5326

54-
async def test_boto_error(hass: HomeAssistant) -> None:
55-
"""Test exceptions from boto are handled correctly."""
56-
with patch(
57-
"homeassistant.components.gentex_homelink.config_flow.SRPAuth"
58-
) as MockSRPAuth:
59-
instance = MockSRPAuth.return_value
60-
instance.async_get_access_token.side_effect = botocore.exceptions.ClientError(
61-
{"Error": {}}, "Some operation"
62-
)
27+
result = await hass.config_entries.flow.async_configure(
28+
result["flow_id"],
29+
user_input={CONF_EMAIL: "[email protected]", CONF_PASSWORD: "SomePassword"},
30+
)
31+
assert result["type"] is FlowResultType.CREATE_ENTRY
32+
assert result["data"] == {
33+
"auth_implementation": "gentex_homelink",
34+
"token": {
35+
"access_token": "access",
36+
"refresh_token": "refresh",
37+
"expires_in": 3600,
38+
"token_type": "bearer",
39+
"expires_at": result["data"]["token"]["expires_at"],
40+
},
41+
}
42+
assert result["title"] == "SRPAuth"
43+
44+
45+
@pytest.mark.parametrize(
46+
("exception", "error"),
47+
[
48+
(
49+
botocore.exceptions.ClientError({"Error": {}}, "Some operation"),
50+
"srp_auth_failed",
51+
),
52+
(Exception("Some error"), "unknown"),
53+
],
54+
)
55+
async def test_exceptions(
56+
hass: HomeAssistant,
57+
mock_srp_auth: AsyncMock,
58+
mock_setup_entry: AsyncMock,
59+
exception: Exception,
60+
error: str,
61+
) -> None:
62+
"""Test exceptions are handled correctly."""
6363

64-
result = await hass.config_entries.flow.async_init(
65-
DOMAIN, context={"source": config_entries.SOURCE_USER}
66-
)
67-
result = await hass.config_entries.flow.async_configure(
68-
result["flow_id"],
69-
user_input={"email": "[email protected]", "password": "SomePassword"},
70-
)
64+
result = await hass.config_entries.flow.async_init(
65+
DOMAIN, context={"source": SOURCE_USER}
66+
)
7167

72-
assert len(hass.config_entries.async_entries(DOMAIN)) == 0
68+
mock_srp_auth.async_get_access_token.side_effect = exception
7369

70+
result = await hass.config_entries.flow.async_configure(
71+
result["flow_id"],
72+
user_input={CONF_EMAIL: "[email protected]", CONF_PASSWORD: "SomePassword"},
73+
)
7474

75-
async def test_generic_error(hass: HomeAssistant) -> None:
76-
"""Test exceptions from boto are handled correctly."""
77-
with patch(
78-
"homeassistant.components.gentex_homelink.config_flow.SRPAuth"
79-
) as MockSRPAuth:
80-
instance = MockSRPAuth.return_value
81-
instance.async_get_access_token.side_effect = Exception("Some error")
75+
assert result["type"] is FlowResultType.FORM
76+
assert result["errors"] == {"base": error}
8277

83-
result = await hass.config_entries.flow.async_init(
84-
DOMAIN, context={"source": config_entries.SOURCE_USER}
85-
)
86-
result = await hass.config_entries.flow.async_configure(
87-
result["flow_id"],
88-
user_input={"email": "[email protected]", "password": "SomePassword"},
89-
)
78+
mock_srp_auth.async_get_access_token.side_effect = None
9079

91-
assert len(hass.config_entries.async_entries(DOMAIN)) == 0
80+
result = await hass.config_entries.flow.async_configure(
81+
result["flow_id"],
82+
user_input={CONF_EMAIL: "[email protected]", CONF_PASSWORD: "SomePassword"},
83+
)
84+
assert result["type"] is FlowResultType.CREATE_ENTRY

0 commit comments

Comments
 (0)