Refactor nest test ConfigEntry setup in config flow tests (#59459)

* Refactor nest ConfigEntry creation in tests into a helper

This is pulled out of PR#59260 to make that overall diff smaller.

* Add typing consistently on new functions
This commit is contained in:
Allen Porter 2021-11-14 10:12:25 -08:00 committed by GitHub
parent 03176dad7d
commit dbfe0cad52
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -8,6 +8,7 @@ from homeassistant import config_entries, setup
from homeassistant.components.nest.const import DOMAIN, OAUTH2_AUTHORIZE, OAUTH2_TOKEN
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_CLIENT_ID, CONF_CLIENT_SECRET
from homeassistant.core import HomeAssistant
from homeassistant.helpers import config_entry_oauth2_flow
from .common import MockConfigEntry
@ -34,13 +35,24 @@ WEB_REDIRECT_URL = "https://example.com/auth/external/callback"
APP_REDIRECT_URL = "urn:ietf:wg:oauth:2.0:oob"
def get_config_entry(hass):
def get_config_entry(hass: HomeAssistant) -> ConfigEntry:
"""Return a single config entry."""
entries = hass.config_entries.async_entries(DOMAIN)
assert len(entries) == 1
return entries[0]
def create_config_entry(hass: HomeAssistant, data: dict) -> ConfigEntry:
"""Create the ConfigEntry."""
entry = MockConfigEntry(
domain=DOMAIN,
data=data,
unique_id=DOMAIN,
)
entry.add_to_hass(hass)
return entry
class OAuthFixture:
"""Simulate the oauth flow used by the config flow."""
@ -165,9 +177,9 @@ async def test_web_reauth(hass, oauth):
assert await setup.async_setup_component(hass, DOMAIN, CONFIG)
old_entry = MockConfigEntry(
domain=DOMAIN,
data={
old_entry = create_config_entry(
hass,
{
"auth_implementation": WEB_AUTH_DOMAIN,
"token": {
# Verify this is replaced at end of the test
@ -175,9 +187,7 @@ async def test_web_reauth(hass, oauth):
},
"sdm": {},
},
unique_id=DOMAIN,
)
old_entry.add_to_hass(hass)
entry = get_config_entry(hass)
assert entry.data["token"] == {
@ -211,10 +221,7 @@ async def test_web_reauth(hass, oauth):
async def test_single_config_entry(hass):
"""Test that only a single config entry is allowed."""
old_entry = MockConfigEntry(
domain=DOMAIN, data={"auth_implementation": WEB_AUTH_DOMAIN, "sdm": {}}
)
old_entry.add_to_hass(hass)
create_config_entry(hass, {"auth_implementation": WEB_AUTH_DOMAIN, "sdm": {}})
assert await setup.async_setup_component(hass, DOMAIN, CONFIG)
@ -299,9 +306,9 @@ async def test_app_reauth(hass, oauth):
assert await setup.async_setup_component(hass, DOMAIN, CONFIG)
old_entry = MockConfigEntry(
domain=DOMAIN,
data={
old_entry = create_config_entry(
hass,
{
"auth_implementation": APP_AUTH_DOMAIN,
"token": {
# Verify this is replaced at end of the test
@ -309,9 +316,7 @@ async def test_app_reauth(hass, oauth):
},
"sdm": {},
},
unique_id=DOMAIN,
)
old_entry.add_to_hass(hass)
entry = get_config_entry(hass)
assert entry.data["token"] == {