User eager task in github config flow (#117066)

This commit is contained in:
Erik Montnemery 2024-05-08 11:25:57 +02:00 committed by GitHub
parent dc1aba0a05
commit fd8c36d93b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 30 additions and 15 deletions

View file

@ -148,9 +148,7 @@ class GitHubConfigFlow(ConfigFlow, domain=DOMAIN):
return self.async_abort(reason="could_not_register") return self.async_abort(reason="could_not_register")
if self.login_task is None: if self.login_task is None:
self.login_task = self.hass.async_create_task( self.login_task = self.hass.async_create_task(_wait_for_login())
_wait_for_login(), eager_start=False
)
if self.login_task.done(): if self.login_task.done():
if self.login_task.exception(): if self.login_task.exception():

View file

@ -3,6 +3,7 @@
from unittest.mock import AsyncMock, MagicMock, patch from unittest.mock import AsyncMock, MagicMock, patch
from aiogithubapi import GitHubException from aiogithubapi import GitHubException
from freezegun.api import FrozenDateTimeFactory
import pytest import pytest
from homeassistant import config_entries from homeassistant import config_entries
@ -26,6 +27,7 @@ async def test_full_user_flow_implementation(
hass: HomeAssistant, hass: HomeAssistant,
mock_setup_entry: None, mock_setup_entry: None,
aioclient_mock: AiohttpClientMocker, aioclient_mock: AiohttpClientMocker,
freezer: FrozenDateTimeFactory,
) -> None: ) -> None:
"""Test the full manual user flow from start to finish.""" """Test the full manual user flow from start to finish."""
aioclient_mock.post( aioclient_mock.post(
@ -39,18 +41,10 @@ async def test_full_user_flow_implementation(
}, },
headers={"Content-Type": "application/json"}, headers={"Content-Type": "application/json"},
) )
# User has not yet entered the code
aioclient_mock.post( aioclient_mock.post(
"https://github.com/login/oauth/access_token", "https://github.com/login/oauth/access_token",
json={ json={"error": "authorization_pending"},
CONF_ACCESS_TOKEN: MOCK_ACCESS_TOKEN,
"token_type": "bearer",
"scope": "",
},
headers={"Content-Type": "application/json"},
)
aioclient_mock.get(
"https://api.github.com/user/starred",
json=[{"full_name": "home-assistant/core"}, {"full_name": "esphome/esphome"}],
headers={"Content-Type": "application/json"}, headers={"Content-Type": "application/json"},
) )
@ -62,8 +56,20 @@ async def test_full_user_flow_implementation(
assert result["step_id"] == "device" assert result["step_id"] == "device"
assert result["type"] is FlowResultType.SHOW_PROGRESS assert result["type"] is FlowResultType.SHOW_PROGRESS
# Wait for the task to start before configuring # User enters the code
aioclient_mock.clear_requests()
aioclient_mock.post(
"https://github.com/login/oauth/access_token",
json={
CONF_ACCESS_TOKEN: MOCK_ACCESS_TOKEN,
"token_type": "bearer",
"scope": "",
},
headers={"Content-Type": "application/json"},
)
freezer.tick(10)
await hass.async_block_till_done() await hass.async_block_till_done()
result = await hass.config_entries.flow.async_configure(result["flow_id"]) result = await hass.config_entries.flow.async_configure(result["flow_id"])
result = await hass.config_entries.flow.async_configure( result = await hass.config_entries.flow.async_configure(
@ -101,6 +107,7 @@ async def test_flow_with_registration_failure(
async def test_flow_with_activation_failure( async def test_flow_with_activation_failure(
hass: HomeAssistant, hass: HomeAssistant,
aioclient_mock: AiohttpClientMocker, aioclient_mock: AiohttpClientMocker,
freezer: FrozenDateTimeFactory,
) -> None: ) -> None:
"""Test flow with activation failure of the device.""" """Test flow with activation failure of the device."""
aioclient_mock.post( aioclient_mock.post(
@ -114,9 +121,11 @@ async def test_flow_with_activation_failure(
}, },
headers={"Content-Type": "application/json"}, headers={"Content-Type": "application/json"},
) )
# User has not yet entered the code
aioclient_mock.post( aioclient_mock.post(
"https://github.com/login/oauth/access_token", "https://github.com/login/oauth/access_token",
exc=GitHubException("Activation failed"), json={"error": "authorization_pending"},
headers={"Content-Type": "application/json"},
) )
result = await hass.config_entries.flow.async_init( result = await hass.config_entries.flow.async_init(
DOMAIN, DOMAIN,
@ -124,6 +133,14 @@ async def test_flow_with_activation_failure(
) )
assert result["step_id"] == "device" assert result["step_id"] == "device"
assert result["type"] is FlowResultType.SHOW_PROGRESS assert result["type"] is FlowResultType.SHOW_PROGRESS
# Activation fails
aioclient_mock.clear_requests()
aioclient_mock.post(
"https://github.com/login/oauth/access_token",
exc=GitHubException("Activation failed"),
)
freezer.tick(10)
await hass.async_block_till_done() await hass.async_block_till_done()
result = await hass.config_entries.flow.async_configure(result["flow_id"]) result = await hass.config_entries.flow.async_configure(result["flow_id"])