|
10 | 10 | OAUTH2_AUTHORIZE, |
11 | 11 | OAUTH2_TOKEN, |
12 | 12 | ) |
| 13 | +from homeassistant.config_entries import SOURCE_DHCP |
13 | 14 | from homeassistant.core import HomeAssistant |
14 | 15 | from homeassistant.data_entry_flow import FlowResultType |
15 | 16 | from homeassistant.helpers import config_entry_oauth2_flow |
| 17 | +from homeassistant.helpers.service_info.dhcp import DhcpServiceInfo |
16 | 18 |
|
17 | 19 | from .const import CLIENT_ID, USER_ID |
18 | 20 |
|
@@ -95,6 +97,79 @@ async def test_full_flow( |
95 | 97 | assert result["result"].unique_id == USER_ID |
96 | 98 |
|
97 | 99 |
|
| 100 | +@pytest.mark.usefixtures("current_request_with_host") |
| 101 | +async def test_full_dhcp_flow( |
| 102 | + hass: HomeAssistant, |
| 103 | + hass_client_no_auth: ClientSessionGenerator, |
| 104 | + aioclient_mock: AiohttpClientMocker, |
| 105 | + access_token, |
| 106 | +) -> None: |
| 107 | + """Check full flow.""" |
| 108 | + result = await hass.config_entries.flow.async_init( |
| 109 | + DOMAIN, |
| 110 | + context={"source": SOURCE_DHCP}, |
| 111 | + data=DhcpServiceInfo( |
| 112 | + ip="192.168.0.123", macaddress="001122334455", hostname="gdocntl-334455" |
| 113 | + ), |
| 114 | + ) |
| 115 | + |
| 116 | + assert result["type"] is FlowResultType.FORM |
| 117 | + assert result["step_id"] == "oauth_discovery" |
| 118 | + assert not result["errors"] |
| 119 | + |
| 120 | + result = await hass.config_entries.flow.async_configure( |
| 121 | + result["flow_id"], |
| 122 | + {}, |
| 123 | + ) |
| 124 | + state = config_entry_oauth2_flow._encode_jwt( |
| 125 | + hass, |
| 126 | + { |
| 127 | + "flow_id": result["flow_id"], |
| 128 | + "redirect_uri": "https://example.com/auth/external/callback", |
| 129 | + }, |
| 130 | + ) |
| 131 | + |
| 132 | + assert result["url"] == ( |
| 133 | + f"{OAUTH2_AUTHORIZE}?response_type=code&client_id={CLIENT_ID}" |
| 134 | + "&redirect_uri=https://example.com/auth/external/callback" |
| 135 | + f"&state={state}" |
| 136 | + ) |
| 137 | + |
| 138 | + client = await hass_client_no_auth() |
| 139 | + resp = await client.get(f"/auth/external/callback?code=abcd&state={state}") |
| 140 | + assert resp.status == 200 |
| 141 | + assert resp.headers["content-type"] == "text/html; charset=utf-8" |
| 142 | + |
| 143 | + aioclient_mock.post( |
| 144 | + OAUTH2_TOKEN, |
| 145 | + json={ |
| 146 | + "refresh_token": "mock-refresh-token", |
| 147 | + "access_token": access_token, |
| 148 | + "type": "Bearer", |
| 149 | + "expires_in": 60, |
| 150 | + }, |
| 151 | + ) |
| 152 | + |
| 153 | + with patch( |
| 154 | + "homeassistant.components.aladdin_connect.async_setup_entry", return_value=True |
| 155 | + ): |
| 156 | + result = await hass.config_entries.flow.async_configure(result["flow_id"]) |
| 157 | + |
| 158 | + assert result["type"] is FlowResultType.CREATE_ENTRY |
| 159 | + assert result["title"] == "Aladdin Connect" |
| 160 | + assert result["data"] == { |
| 161 | + "auth_implementation": DOMAIN, |
| 162 | + "token": { |
| 163 | + "access_token": access_token, |
| 164 | + "refresh_token": "mock-refresh-token", |
| 165 | + "expires_in": 60, |
| 166 | + "expires_at": result["data"]["token"]["expires_at"], |
| 167 | + "type": "Bearer", |
| 168 | + }, |
| 169 | + } |
| 170 | + assert result["result"].unique_id == USER_ID |
| 171 | + |
| 172 | + |
98 | 173 | @pytest.mark.usefixtures("current_request_with_host") |
99 | 174 | async def test_duplicate_entry( |
100 | 175 | hass: HomeAssistant, |
@@ -146,6 +221,28 @@ async def test_duplicate_entry( |
146 | 221 | assert result["reason"] == "already_configured" |
147 | 222 |
|
148 | 223 |
|
| 224 | +@pytest.mark.usefixtures("current_request_with_host") |
| 225 | +async def test_duplicate_dhcp_entry( |
| 226 | + hass: HomeAssistant, |
| 227 | + hass_client_no_auth: ClientSessionGenerator, |
| 228 | + aioclient_mock: AiohttpClientMocker, |
| 229 | + access_token, |
| 230 | + mock_config_entry: MockConfigEntry, |
| 231 | +) -> None: |
| 232 | + """Check full flow.""" |
| 233 | + mock_config_entry.add_to_hass(hass) |
| 234 | + result = await hass.config_entries.flow.async_init( |
| 235 | + DOMAIN, |
| 236 | + context={"source": SOURCE_DHCP}, |
| 237 | + data=DhcpServiceInfo( |
| 238 | + ip="192.168.0.123", macaddress="001122334455", hostname="gdocntl-334455" |
| 239 | + ), |
| 240 | + ) |
| 241 | + |
| 242 | + assert result["type"] is FlowResultType.ABORT |
| 243 | + assert result["reason"] == "already_configured" |
| 244 | + |
| 245 | + |
149 | 246 | @pytest.mark.usefixtures("current_request_with_host") |
150 | 247 | async def test_flow_reauth( |
151 | 248 | hass: HomeAssistant, |
|
0 commit comments