-
-
Notifications
You must be signed in to change notification settings - Fork 31.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Catch Hydrawise authorization errors in the correct place (#132727)
- Loading branch information
Showing
3 changed files
with
41 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,7 @@ | |
async def test_form( | ||
hass: HomeAssistant, | ||
mock_setup_entry: AsyncMock, | ||
mock_auth: AsyncMock, | ||
mock_pydrawise: AsyncMock, | ||
user: User, | ||
) -> None: | ||
|
@@ -46,11 +47,12 @@ async def test_form( | |
CONF_PASSWORD: "__password__", | ||
} | ||
assert len(mock_setup_entry.mock_calls) == 1 | ||
mock_pydrawise.get_user.assert_called_once_with(fetch_zones=False) | ||
mock_auth.token.assert_awaited_once_with() | ||
mock_pydrawise.get_user.assert_awaited_once_with(fetch_zones=False) | ||
|
||
|
||
async def test_form_api_error( | ||
hass: HomeAssistant, mock_pydrawise: AsyncMock, user: User | ||
hass: HomeAssistant, mock_auth: AsyncMock, mock_pydrawise: AsyncMock, user: User | ||
) -> None: | ||
"""Test we handle API errors.""" | ||
mock_pydrawise.get_user.side_effect = ClientError("XXX") | ||
|
@@ -71,8 +73,29 @@ async def test_form_api_error( | |
assert result2["type"] is FlowResultType.CREATE_ENTRY | ||
|
||
|
||
async def test_form_connect_timeout( | ||
hass: HomeAssistant, mock_pydrawise: AsyncMock, user: User | ||
async def test_form_auth_connect_timeout( | ||
hass: HomeAssistant, mock_auth: AsyncMock, mock_pydrawise: AsyncMock | ||
) -> None: | ||
"""Test we handle API errors.""" | ||
mock_auth.token.side_effect = TimeoutError | ||
init_result = await hass.config_entries.flow.async_init( | ||
DOMAIN, context={"source": config_entries.SOURCE_USER} | ||
) | ||
data = {CONF_USERNAME: "[email protected]", CONF_PASSWORD: "__password__"} | ||
result = await hass.config_entries.flow.async_configure( | ||
init_result["flow_id"], data | ||
) | ||
|
||
assert result["type"] is FlowResultType.FORM | ||
assert result["errors"] == {"base": "timeout_connect"} | ||
|
||
mock_auth.token.reset_mock(side_effect=True) | ||
result2 = await hass.config_entries.flow.async_configure(result["flow_id"], data) | ||
assert result2["type"] is FlowResultType.CREATE_ENTRY | ||
|
||
|
||
async def test_form_client_connect_timeout( | ||
hass: HomeAssistant, mock_auth: AsyncMock, mock_pydrawise: AsyncMock, user: User | ||
) -> None: | ||
"""Test we handle API errors.""" | ||
mock_pydrawise.get_user.side_effect = TimeoutError | ||
|
@@ -94,10 +117,10 @@ async def test_form_connect_timeout( | |
|
||
|
||
async def test_form_not_authorized_error( | ||
hass: HomeAssistant, mock_pydrawise: AsyncMock, user: User | ||
hass: HomeAssistant, mock_auth: AsyncMock, mock_pydrawise: AsyncMock | ||
) -> None: | ||
"""Test we handle API errors.""" | ||
mock_pydrawise.get_user.side_effect = NotAuthorizedError | ||
mock_auth.token.side_effect = NotAuthorizedError | ||
|
||
init_result = await hass.config_entries.flow.async_init( | ||
DOMAIN, context={"source": config_entries.SOURCE_USER} | ||
|
@@ -109,15 +132,15 @@ async def test_form_not_authorized_error( | |
assert result["type"] is FlowResultType.FORM | ||
assert result["errors"] == {"base": "invalid_auth"} | ||
|
||
mock_pydrawise.get_user.reset_mock(side_effect=True) | ||
mock_pydrawise.get_user.return_value = user | ||
mock_auth.token.reset_mock(side_effect=True) | ||
result2 = await hass.config_entries.flow.async_configure(result["flow_id"], data) | ||
assert result2["type"] is FlowResultType.CREATE_ENTRY | ||
|
||
|
||
async def test_reauth( | ||
hass: HomeAssistant, | ||
user: User, | ||
mock_auth: AsyncMock, | ||
mock_pydrawise: AsyncMock, | ||
) -> None: | ||
"""Test that re-authorization works.""" | ||
|