Add myuplink reauth flow (#110587)
* WIP test * WIP * WIP Reauth flow. Test fail otherways OK. * Minor adjustments to tests * Merge * Merge * Next level... * Cleanup according to review * It works! * Simplify setup * Remove default * Remove files from PR * Add back test_init * Add back test_sensor * Adjust error message --------- Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
This commit is contained in:
parent
d99a7e2825
commit
f5dad1d312
8 changed files with 158 additions and 31 deletions
|
@ -2,35 +2,24 @@
|
|||
|
||||
from unittest.mock import patch
|
||||
|
||||
import pytest
|
||||
|
||||
from homeassistant import config_entries
|
||||
from homeassistant.components.application_credentials import (
|
||||
ClientCredential,
|
||||
async_import_client_credential,
|
||||
)
|
||||
from homeassistant.components.myuplink.const import (
|
||||
DOMAIN,
|
||||
OAUTH2_AUTHORIZE,
|
||||
OAUTH2_TOKEN,
|
||||
)
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.data_entry_flow import FlowResultType
|
||||
from homeassistant.helpers import config_entry_oauth2_flow
|
||||
from homeassistant.setup import async_setup_component
|
||||
|
||||
CLIENT_ID = "1234"
|
||||
CLIENT_SECRET = "5678"
|
||||
from .const import CLIENT_ID
|
||||
|
||||
from tests.common import MockConfigEntry
|
||||
from tests.test_util.aiohttp import AiohttpClientMocker
|
||||
from tests.typing import ClientSessionGenerator
|
||||
|
||||
@pytest.fixture
|
||||
async def setup_credentials(hass: HomeAssistant) -> None:
|
||||
"""Fixture to setup credentials."""
|
||||
assert await async_setup_component(hass, "application_credentials", {})
|
||||
await async_import_client_credential(
|
||||
hass,
|
||||
DOMAIN,
|
||||
ClientCredential(CLIENT_ID, CLIENT_SECRET),
|
||||
)
|
||||
REDIRECT_URL = "https://example.com/auth/external/callback"
|
||||
CURRENT_SCOPE = "WRITESYSTEM READSYSTEM offline_access"
|
||||
|
||||
|
||||
async def test_full_flow(
|
||||
|
@ -42,21 +31,21 @@ async def test_full_flow(
|
|||
) -> None:
|
||||
"""Check full flow."""
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
"myuplink", context={"source": config_entries.SOURCE_USER}
|
||||
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
||||
)
|
||||
state = config_entry_oauth2_flow._encode_jwt(
|
||||
hass,
|
||||
{
|
||||
"flow_id": result["flow_id"],
|
||||
"redirect_uri": "https://example.com/auth/external/callback",
|
||||
"redirect_uri": REDIRECT_URL,
|
||||
},
|
||||
)
|
||||
|
||||
assert result["url"] == (
|
||||
f"{OAUTH2_AUTHORIZE}?response_type=code&client_id={CLIENT_ID}"
|
||||
"&redirect_uri=https://example.com/auth/external/callback"
|
||||
f"&redirect_uri={REDIRECT_URL}"
|
||||
f"&state={state}"
|
||||
"&scope=READSYSTEM+offline_access"
|
||||
f"&scope={CURRENT_SCOPE.replace(' ', '+')}"
|
||||
)
|
||||
|
||||
client = await hass_client_no_auth()
|
||||
|
@ -75,9 +64,100 @@ async def test_full_flow(
|
|||
)
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.myuplink.async_setup_entry", return_value=True
|
||||
f"homeassistant.components.{DOMAIN}.async_setup_entry", return_value=True
|
||||
) as mock_setup:
|
||||
await hass.config_entries.flow.async_configure(result["flow_id"])
|
||||
|
||||
assert len(hass.config_entries.async_entries(DOMAIN)) == 1
|
||||
assert len(mock_setup.mock_calls) == 1
|
||||
|
||||
|
||||
async def test_flow_reauth(
|
||||
hass: HomeAssistant,
|
||||
hass_client_no_auth: ClientSessionGenerator,
|
||||
aioclient_mock: AiohttpClientMocker,
|
||||
current_request_with_host: None,
|
||||
setup_credentials: None,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
expires_at: float,
|
||||
) -> None:
|
||||
"""Test reauth step."""
|
||||
|
||||
OLD_SCOPE = "READSYSTEM offline_access"
|
||||
OLD_SCOPE_TOKEN = {
|
||||
"auth_implementation": DOMAIN,
|
||||
"token": {
|
||||
"access_token": "Fake_token",
|
||||
"scope": OLD_SCOPE,
|
||||
"expires_in": 86399,
|
||||
"refresh_token": "3012bc9f-7a65-4240-b817-9154ffdcc30f",
|
||||
"token_type": "Bearer",
|
||||
"expires_at": expires_at,
|
||||
},
|
||||
}
|
||||
assert mock_config_entry.data["token"]["scope"] == CURRENT_SCOPE
|
||||
assert hass.config_entries.async_update_entry(
|
||||
mock_config_entry, data=OLD_SCOPE_TOKEN
|
||||
)
|
||||
assert mock_config_entry.data["token"]["scope"] == OLD_SCOPE
|
||||
|
||||
assert len(hass.config_entries.async_entries(DOMAIN)) == 1
|
||||
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN,
|
||||
context={
|
||||
"source": config_entries.SOURCE_REAUTH,
|
||||
"entry_id": mock_config_entry.entry_id,
|
||||
},
|
||||
data=mock_config_entry.data,
|
||||
)
|
||||
|
||||
assert result["step_id"] == "reauth_confirm"
|
||||
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"], user_input={}
|
||||
)
|
||||
assert result["step_id"] == "auth"
|
||||
|
||||
state = config_entry_oauth2_flow._encode_jwt(
|
||||
hass,
|
||||
{
|
||||
"flow_id": result["flow_id"],
|
||||
"redirect_uri": REDIRECT_URL,
|
||||
},
|
||||
)
|
||||
assert result["url"] == (
|
||||
f"{OAUTH2_AUTHORIZE}?response_type=code&client_id={CLIENT_ID}"
|
||||
f"&redirect_uri={REDIRECT_URL}"
|
||||
f"&state={state}"
|
||||
f"&scope={CURRENT_SCOPE.replace(' ', '+')}"
|
||||
)
|
||||
|
||||
client = await hass_client_no_auth()
|
||||
resp = await client.get(f"/auth/external/callback?code=abcd&state={state}")
|
||||
assert resp.status == 200
|
||||
assert resp.headers["content-type"] == "text/html; charset=utf-8"
|
||||
|
||||
aioclient_mock.post(
|
||||
OAUTH2_TOKEN,
|
||||
json={
|
||||
"refresh_token": "updated-refresh-token",
|
||||
"access_token": "updated-access-token",
|
||||
"type": "Bearer",
|
||||
"expires_in": "60",
|
||||
"scope": CURRENT_SCOPE,
|
||||
},
|
||||
)
|
||||
|
||||
with patch(
|
||||
f"homeassistant.components.{DOMAIN}.async_setup_entry", return_value=True
|
||||
) as mock_setup:
|
||||
result = await hass.config_entries.flow.async_configure(result["flow_id"])
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert result.get("type") == FlowResultType.ABORT
|
||||
assert result.get("reason") == "reauth_successful"
|
||||
|
||||
assert len(hass.config_entries.async_entries(DOMAIN)) == 1
|
||||
assert len(mock_setup.mock_calls) == 1
|
||||
assert mock_config_entry.data["token"]["scope"] == CURRENT_SCOPE
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue