|
1 | 1 | """Test the homelink config flow.""" |
2 | 2 |
|
3 | | -from unittest.mock import patch |
| 3 | +from unittest.mock import AsyncMock |
4 | 4 |
|
5 | 5 | import botocore.exceptions |
| 6 | +import pytest |
6 | 7 |
|
7 | | -from homeassistant import config_entries |
8 | 8 | 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 |
9 | 11 | from homeassistant.core import HomeAssistant |
10 | 12 | from homeassistant.data_entry_flow import FlowResultType |
11 | 13 |
|
12 | 14 |
|
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.""" |
15 | 19 | result = await hass.config_entries.flow.async_init( |
16 | | - DOMAIN, |
17 | | - context={"source": config_entries.SOURCE_USER}, |
| 20 | + DOMAIN, context={"source": SOURCE_USER} |
18 | 21 | ) |
19 | 22 |
|
| 23 | + assert result["type"] is FlowResultType.FORM |
20 | 24 | 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"] |
53 | 26 |
|
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.""" |
63 | 63 |
|
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 | + ) |
71 | 67 |
|
72 | | - assert len(hass.config_entries.async_entries(DOMAIN)) == 0 |
| 68 | + mock_srp_auth.async_get_access_token.side_effect = exception |
73 | 69 |
|
| 70 | + result = await hass.config_entries.flow.async_configure( |
| 71 | + result["flow_id"], |
| 72 | + user_input={ CONF_EMAIL: "[email protected]", CONF_PASSWORD: "SomePassword"}, |
| 73 | + ) |
74 | 74 |
|
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} |
82 | 77 |
|
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 |
90 | 79 |
|
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