Improve type hints in Config entry oauth2 tests (#120090)

This commit is contained in:
epenet 2024-06-21 14:44:28 +02:00 committed by GitHub
parent 5c2f78a4b9
commit e2a34d209f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -8,6 +8,7 @@ from unittest.mock import patch
import aiohttp
import pytest
from typing_extensions import Generator
from homeassistant import config_entries, data_entry_flow, setup
from homeassistant.core import HomeAssistant
@ -29,7 +30,9 @@ TOKEN_URL = "https://example.como/auth/token"
@pytest.fixture
async def local_impl(hass):
async def local_impl(
hass: HomeAssistant,
) -> config_entry_oauth2_flow.LocalOAuth2Implementation:
"""Local implementation."""
assert await setup.async_setup_component(hass, "auth", {})
return config_entry_oauth2_flow.LocalOAuth2Implementation(
@ -38,7 +41,9 @@ async def local_impl(hass):
@pytest.fixture
def flow_handler(hass):
def flow_handler(
hass: HomeAssistant,
) -> Generator[type[config_entry_oauth2_flow.AbstractOAuth2FlowHandler]]:
"""Return a registered config flow."""
mock_platform(hass, f"{TEST_DOMAIN}.config_flow")
@ -111,7 +116,10 @@ def test_inherit_enforces_domain_set() -> None:
TestFlowHandler()
async def test_abort_if_no_implementation(hass: HomeAssistant, flow_handler) -> None:
async def test_abort_if_no_implementation(
hass: HomeAssistant,
flow_handler: type[config_entry_oauth2_flow.AbstractOAuth2FlowHandler],
) -> None:
"""Check flow abort when no implementations."""
flow = flow_handler()
flow.hass = hass
@ -121,7 +129,8 @@ async def test_abort_if_no_implementation(hass: HomeAssistant, flow_handler) ->
async def test_missing_credentials_for_domain(
hass: HomeAssistant, flow_handler
hass: HomeAssistant,
flow_handler: type[config_entry_oauth2_flow.AbstractOAuth2FlowHandler],
) -> None:
"""Check flow abort for integration supporting application credentials."""
flow = flow_handler()
@ -135,7 +144,9 @@ async def test_missing_credentials_for_domain(
@pytest.mark.usefixtures("current_request_with_host")
async def test_abort_if_authorization_timeout(
hass: HomeAssistant, flow_handler, local_impl
hass: HomeAssistant,
flow_handler: type[config_entry_oauth2_flow.AbstractOAuth2FlowHandler],
local_impl: config_entry_oauth2_flow.LocalOAuth2Implementation,
) -> None:
"""Check timeout generating authorization url."""
flow_handler.async_register_implementation(hass, local_impl)
@ -155,7 +166,9 @@ async def test_abort_if_authorization_timeout(
@pytest.mark.usefixtures("current_request_with_host")
async def test_abort_if_no_url_available(
hass: HomeAssistant, flow_handler, local_impl
hass: HomeAssistant,
flow_handler: type[config_entry_oauth2_flow.AbstractOAuth2FlowHandler],
local_impl: config_entry_oauth2_flow.LocalOAuth2Implementation,
) -> None:
"""Check no_url_available generating authorization url."""
flow_handler.async_register_implementation(hass, local_impl)
@ -176,8 +189,8 @@ async def test_abort_if_no_url_available(
@pytest.mark.usefixtures("current_request_with_host")
async def test_abort_if_oauth_error(
hass: HomeAssistant,
flow_handler,
local_impl,
flow_handler: type[config_entry_oauth2_flow.AbstractOAuth2FlowHandler],
local_impl: config_entry_oauth2_flow.LocalOAuth2Implementation,
hass_client_no_auth: ClientSessionGenerator,
aioclient_mock: AiohttpClientMocker,
expires_in_dict: dict[str, str],
@ -239,8 +252,8 @@ async def test_abort_if_oauth_error(
@pytest.mark.usefixtures("current_request_with_host")
async def test_abort_if_oauth_rejected(
hass: HomeAssistant,
flow_handler,
local_impl,
flow_handler: type[config_entry_oauth2_flow.AbstractOAuth2FlowHandler],
local_impl: config_entry_oauth2_flow.LocalOAuth2Implementation,
hass_client_no_auth: ClientSessionGenerator,
) -> None:
"""Check bad oauth token."""
@ -293,8 +306,8 @@ async def test_abort_if_oauth_rejected(
@pytest.mark.usefixtures("current_request_with_host")
async def test_abort_on_oauth_timeout_error(
hass: HomeAssistant,
flow_handler,
local_impl,
flow_handler: type[config_entry_oauth2_flow.AbstractOAuth2FlowHandler],
local_impl: config_entry_oauth2_flow.LocalOAuth2Implementation,
hass_client_no_auth: ClientSessionGenerator,
aioclient_mock: AiohttpClientMocker,
) -> None:
@ -346,7 +359,11 @@ async def test_abort_on_oauth_timeout_error(
assert result["reason"] == "oauth_timeout"
async def test_step_discovery(hass: HomeAssistant, flow_handler, local_impl) -> None:
async def test_step_discovery(
hass: HomeAssistant,
flow_handler: type[config_entry_oauth2_flow.AbstractOAuth2FlowHandler],
local_impl: config_entry_oauth2_flow.LocalOAuth2Implementation,
) -> None:
"""Check flow triggers from discovery."""
flow_handler.async_register_implementation(hass, local_impl)
config_entry_oauth2_flow.async_register_implementation(
@ -364,7 +381,9 @@ async def test_step_discovery(hass: HomeAssistant, flow_handler, local_impl) ->
async def test_abort_discovered_multiple(
hass: HomeAssistant, flow_handler, local_impl
hass: HomeAssistant,
flow_handler: type[config_entry_oauth2_flow.AbstractOAuth2FlowHandler],
local_impl: config_entry_oauth2_flow.LocalOAuth2Implementation,
) -> None:
"""Test if aborts when discovered multiple times."""
flow_handler.async_register_implementation(hass, local_impl)
@ -427,8 +446,8 @@ async def test_abort_discovered_multiple(
@pytest.mark.usefixtures("current_request_with_host")
async def test_abort_if_oauth_token_error(
hass: HomeAssistant,
flow_handler,
local_impl,
flow_handler: type[config_entry_oauth2_flow.AbstractOAuth2FlowHandler],
local_impl: config_entry_oauth2_flow.LocalOAuth2Implementation,
hass_client_no_auth: ClientSessionGenerator,
aioclient_mock: AiohttpClientMocker,
status_code: HTTPStatus,
@ -491,8 +510,8 @@ async def test_abort_if_oauth_token_error(
@pytest.mark.usefixtures("current_request_with_host")
async def test_abort_if_oauth_token_closing_error(
hass: HomeAssistant,
flow_handler,
local_impl,
flow_handler: type[config_entry_oauth2_flow.AbstractOAuth2FlowHandler],
local_impl: config_entry_oauth2_flow.LocalOAuth2Implementation,
hass_client_no_auth: ClientSessionGenerator,
aioclient_mock: AiohttpClientMocker,
caplog: pytest.LogCaptureFixture,
@ -550,7 +569,9 @@ async def test_abort_if_oauth_token_closing_error(
async def test_abort_discovered_existing_entries(
hass: HomeAssistant, flow_handler, local_impl
hass: HomeAssistant,
flow_handler: type[config_entry_oauth2_flow.AbstractOAuth2FlowHandler],
local_impl: config_entry_oauth2_flow.LocalOAuth2Implementation,
) -> None:
"""Test if abort discovery when entries exists."""
flow_handler.async_register_implementation(hass, local_impl)
@ -577,8 +598,8 @@ async def test_abort_discovered_existing_entries(
@pytest.mark.usefixtures("current_request_with_host")
async def test_full_flow(
hass: HomeAssistant,
flow_handler,
local_impl,
flow_handler: type[config_entry_oauth2_flow.AbstractOAuth2FlowHandler],
local_impl: config_entry_oauth2_flow.LocalOAuth2Implementation,
hass_client_no_auth: ClientSessionGenerator,
aioclient_mock: AiohttpClientMocker,
) -> None:
@ -653,7 +674,9 @@ async def test_full_flow(
async def test_local_refresh_token(
hass: HomeAssistant, local_impl, aioclient_mock: AiohttpClientMocker
hass: HomeAssistant,
local_impl: config_entry_oauth2_flow.LocalOAuth2Implementation,
aioclient_mock: AiohttpClientMocker,
) -> None:
"""Test we can refresh token."""
aioclient_mock.post(
@ -687,7 +710,10 @@ async def test_local_refresh_token(
async def test_oauth_session(
hass: HomeAssistant, flow_handler, local_impl, aioclient_mock: AiohttpClientMocker
hass: HomeAssistant,
flow_handler: type[config_entry_oauth2_flow.AbstractOAuth2FlowHandler],
local_impl: config_entry_oauth2_flow.LocalOAuth2Implementation,
aioclient_mock: AiohttpClientMocker,
) -> None:
"""Test the OAuth2 session helper."""
flow_handler.async_register_implementation(hass, local_impl)
@ -734,7 +760,10 @@ async def test_oauth_session(
async def test_oauth_session_with_clock_slightly_out_of_sync(
hass: HomeAssistant, flow_handler, local_impl, aioclient_mock: AiohttpClientMocker
hass: HomeAssistant,
flow_handler: type[config_entry_oauth2_flow.AbstractOAuth2FlowHandler],
local_impl: config_entry_oauth2_flow.LocalOAuth2Implementation,
aioclient_mock: AiohttpClientMocker,
) -> None:
"""Test the OAuth2 session helper when the remote clock is slightly out of sync."""
flow_handler.async_register_implementation(hass, local_impl)
@ -781,7 +810,10 @@ async def test_oauth_session_with_clock_slightly_out_of_sync(
async def test_oauth_session_no_token_refresh_needed(
hass: HomeAssistant, flow_handler, local_impl, aioclient_mock: AiohttpClientMocker
hass: HomeAssistant,
flow_handler: type[config_entry_oauth2_flow.AbstractOAuth2FlowHandler],
local_impl: config_entry_oauth2_flow.LocalOAuth2Implementation,
aioclient_mock: AiohttpClientMocker,
) -> None:
"""Test the OAuth2 session helper when no refresh is needed."""
flow_handler.async_register_implementation(hass, local_impl)
@ -879,7 +911,10 @@ async def test_implementation_provider(hass: HomeAssistant, local_impl) -> None:
async def test_oauth_session_refresh_failure(
hass: HomeAssistant, flow_handler, local_impl, aioclient_mock: AiohttpClientMocker
hass: HomeAssistant,
flow_handler: type[config_entry_oauth2_flow.AbstractOAuth2FlowHandler],
local_impl: config_entry_oauth2_flow.LocalOAuth2Implementation,
aioclient_mock: AiohttpClientMocker,
) -> None:
"""Test the OAuth2 session helper when no refresh is needed."""
flow_handler.async_register_implementation(hass, local_impl)
@ -908,7 +943,8 @@ async def test_oauth_session_refresh_failure(
async def test_oauth2_without_secret_init(
local_impl, hass_client_no_auth: ClientSessionGenerator
local_impl: config_entry_oauth2_flow.LocalOAuth2Implementation,
hass_client_no_auth: ClientSessionGenerator,
) -> None:
"""Check authorize callback without secret initalizated."""
client = await hass_client_no_auth()