Introduce new OAuth2 config flow helper (#27727)
* Refactor the Somfy auth implementation * Typing * Migrate Somfy to OAuth2 flow helper * Add tests * Add more tests * Fix tests * Fix type error * More tests * Remove side effect from constructor * implementation -> auth_implementation * Make get_implementation async * Minor cleanup + Allow picking implementations. * Add support for extra authorize data
This commit is contained in:
1 parent
6157be23dc
commit
b6c26cb363
15 files changed
+900
-214
No files matched your search
@@ -1,19 +1,35 @@
|
||||
"""Tests for the Somfy config flow."""
|
||||
import asyncio
|
||||
from unittest.mock import Mock, patch
|
||||
from unittest.mock import patch
|
||||
|
||||
from pymfy.api.somfy_api import SomfyApi
|
||||
import pytest
|
||||
|
||||
from homeassistant import data_entry_flow
|
||||
from homeassistant import data_entry_flow, setup, config_entries
|
||||
from homeassistant.components.somfy import config_flow, DOMAIN
|
||||
from homeassistant.components.somfy.config_flow import register_flow_implementation
|
||||
from tests.common import MockConfigEntry, mock_coro
|
||||
from homeassistant.helpers import config_entry_oauth2_flow
|
||||
|
||||
from tests.common import MockConfigEntry
|
||||
|
||||
CLIENT_SECRET_VALUE = "5678"
|
||||
|
||||
CLIENT_ID_VALUE = "1234"
|
||||
|
||||
AUTH_URL = "http://somfy.com"
|
||||
|
||||
@pytest.fixture()
|
||||
async def mock_impl(hass):
|
||||
"""Mock implementation."""
|
||||
await setup.async_setup_component(hass, "http", {})
|
||||
|
||||
impl = config_entry_oauth2_flow.LocalOAuth2Implementation(
|
||||
hass,
|
||||
DOMAIN,
|
||||
CLIENT_ID_VALUE,
|
||||
CLIENT_SECRET_VALUE,
|
||||
"https://accounts.somfy.com/oauth/oauth/v2/auth",
|
||||
"https://accounts.somfy.com/oauth/oauth/v2/token",
|
||||
)
|
||||
config_flow.SomfyFlowHandler.async_register_implementation(hass, impl)
|
||||
return impl
|
||||
|
||||
|
||||
async def test_abort_if_no_configuration(hass):
|
||||
@@ -30,47 +46,84 @@ async def test_abort_if_existing_entry(hass):
|
||||
flow = config_flow.SomfyFlowHandler()
|
||||
flow.hass = hass
|
||||
MockConfigEntry(domain=DOMAIN).add_to_hass(hass)
|
||||
result = await flow.async_step_import()
|
||||
assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT
|
||||
assert result["reason"] == "already_setup"
|
||||
|
||||
result = await flow.async_step_user()
|
||||
assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT
|
||||
assert result["reason"] == "already_setup"
|
||||
|
||||
|
||||
async def test_full_flow(hass):
|
||||
"""Check classic use case."""
|
||||
hass.data[DOMAIN] = {}
|
||||
register_flow_implementation(hass, CLIENT_ID_VALUE, CLIENT_SECRET_VALUE)
|
||||
flow = config_flow.SomfyFlowHandler()
|
||||
flow.hass = hass
|
||||
hass.config.api = Mock(base_url="https://example.com")
|
||||
flow._get_authorization_url = Mock(return_value=mock_coro((AUTH_URL, "state")))
|
||||
result = await flow.async_step_import()
|
||||
async def test_full_flow(hass, aiohttp_client, aioclient_mock):
|
||||
"""Check full flow."""
|
||||
assert await setup.async_setup_component(
|
||||
hass,
|
||||
"somfy",
|
||||
{
|
||||
"somfy": {
|
||||
"client_id": CLIENT_ID_VALUE,
|
||||
"client_secret": CLIENT_SECRET_VALUE,
|
||||
},
|
||||
"http": {"base_url": "https://example.com"},
|
||||
},
|
||||
)
|
||||
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
"somfy", context={"source": config_entries.SOURCE_USER}
|
||||
)
|
||||
state = config_entry_oauth2_flow._encode_jwt(hass, {"flow_id": result["flow_id"]})
|
||||
|
||||
assert result["type"] == data_entry_flow.RESULT_TYPE_EXTERNAL_STEP
|
||||
assert result["url"] == AUTH_URL
|
||||
result = await flow.async_step_auth("my_super_code")
|
||||
assert result["type"] == data_entry_flow.RESULT_TYPE_EXTERNAL_STEP_DONE
|
||||
assert result["step_id"] == "creation"
|
||||
assert flow.code == "my_super_code"
|
||||
with patch.object(
|
||||
SomfyApi, "request_token", return_value={"access_token": "super_token"}
|
||||
):
|
||||
result = await flow.async_step_creation()
|
||||
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
|
||||
assert result["data"]["refresh_args"] == {
|
||||
"client_id": CLIENT_ID_VALUE,
|
||||
"client_secret": CLIENT_SECRET_VALUE,
|
||||
assert result["url"] == (
|
||||
"https://accounts.somfy.com/oauth/oauth/v2/auth"
|
||||
f"?response_type=code&client_id={CLIENT_ID_VALUE}"
|
||||
"&redirect_uri=https://example.com/auth/external/callback"
|
||||
f"&state={state}"
|
||||
)
|
||||
|
||||
client = await aiohttp_client(hass.http.app)
|
||||
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(
|
||||
"https://accounts.somfy.com/oauth/oauth/v2/token",
|
||||
json={
|
||||
"refresh_token": "mock-refresh-token",
|
||||
"access_token": "mock-access-token",
|
||||
"type": "Bearer",
|
||||
"expires_in": 60,
|
||||
},
|
||||
)
|
||||
|
||||
with patch("homeassistant.components.somfy.api.ConfigEntrySomfyApi"):
|
||||
result = await hass.config_entries.flow.async_configure(result["flow_id"])
|
||||
|
||||
assert result["data"]["auth_implementation"] == "somfy"
|
||||
|
||||
result["data"]["token"].pop("expires_at")
|
||||
assert result["data"]["token"] == {
|
||||
"refresh_token": "mock-refresh-token",
|
||||
"access_token": "mock-access-token",
|
||||
"type": "Bearer",
|
||||
"expires_in": 60,
|
||||
}
|
||||
assert result["title"] == "Somfy"
|
||||
assert result["data"]["token"] == {"access_token": "super_token"}
|
||||
|
||||
assert "somfy" in hass.config.components
|
||||
entry = hass.config_entries.async_entries("somfy")[0]
|
||||
assert entry.state == config_entries.ENTRY_STATE_LOADED
|
||||
|
||||
assert await hass.config_entries.async_unload(entry.entry_id)
|
||||
assert entry.state == config_entries.ENTRY_STATE_NOT_LOADED
|
||||
|
||||
|
||||
async def test_abort_if_authorization_timeout(hass):
|
||||
async def test_abort_if_authorization_timeout(hass, mock_impl):
|
||||
"""Check Somfy authorization timeout."""
|
||||
flow = config_flow.SomfyFlowHandler()
|
||||
flow.hass = hass
|
||||
flow._get_authorization_url = Mock(side_effect=asyncio.TimeoutError)
|
||||
result = await flow.async_step_auth()
|
||||
|
||||
with patch.object(
|
||||
mock_impl, "async_generate_authorize_url", side_effect=asyncio.TimeoutError
|
||||
):
|
||||
result = await flow.async_step_user()
|
||||
|
||||
assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT
|
||||
assert result["reason"] == "authorize_url_timeout"
|
||||
Reference in new issue
Block a user