Handle OAuth2 rejection (#72040)

This commit is contained in:
Paulus Schoutsen
2022-05-28 20:43:48 -07:00
parent e974a432aa
commit afcc8679dd
4 changed files with 79 additions and 8 deletions
@@ -223,6 +223,61 @@ async def test_abort_if_oauth_error(
assert result["reason"] == "oauth_error"
async def test_abort_if_oauth_rejected(
hass,
flow_handler,
local_impl,
hass_client_no_auth,
aioclient_mock,
current_request_with_host,
):
"""Check bad oauth token."""
flow_handler.async_register_implementation(hass, local_impl)
config_entry_oauth2_flow.async_register_implementation(
hass, TEST_DOMAIN, MockOAuth2Implementation()
)
result = await hass.config_entries.flow.async_init(
TEST_DOMAIN, context={"source": config_entries.SOURCE_USER}
)
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
assert result["step_id"] == "pick_implementation"
# Pick implementation
result = await hass.config_entries.flow.async_configure(
result["flow_id"], user_input={"implementation": TEST_DOMAIN}
)
state = config_entry_oauth2_flow._encode_jwt(
hass,
{
"flow_id": result["flow_id"],
"redirect_uri": "https://example.com/auth/external/callback",
},
)
assert result["type"] == data_entry_flow.RESULT_TYPE_EXTERNAL_STEP
assert result["url"] == (
f"{AUTHORIZE_URL}?response_type=code&client_id={CLIENT_ID}"
"&redirect_uri=https://example.com/auth/external/callback"
f"&state={state}&scope=read+write"
)
client = await hass_client_no_auth()
resp = await client.get(
f"/auth/external/callback?error=access_denied&state={state}"
)
assert resp.status == 200
assert resp.headers["content-type"] == "text/html; charset=utf-8"
result = await hass.config_entries.flow.async_configure(result["flow_id"])
assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT
assert result["reason"] == "user_rejected_authorize"
assert result["description_placeholders"] == {"error": "access_denied"}
async def test_step_discovery(hass, flow_handler, local_impl):
"""Check flow triggers from discovery."""
flow_handler.async_register_implementation(hass, local_impl)