Clean somfy config flow tests (#50461)

This commit is contained in:
Martin Hjelmare 2021-05-11 16:19:07 +02:00 committed by GitHub
parent 4e24640ff7
commit f71eb4d34d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 32 additions and 38 deletions

View File

@ -934,7 +934,12 @@ omit =
homeassistant/components/soma/__init__.py
homeassistant/components/soma/cover.py
homeassistant/components/soma/sensor.py
homeassistant/components/somfy/*
homeassistant/components/somfy/__init__.py
homeassistant/components/somfy/api.py
homeassistant/components/somfy/climate.py
homeassistant/components/somfy/cover.py
homeassistant/components/somfy/sensor.py
homeassistant/components/somfy/switch.py
homeassistant/components/somfy_mylink/__init__.py
homeassistant/components/somfy_mylink/cover.py
homeassistant/components/sonos/*

View File

@ -51,7 +51,6 @@ ALLOWED_IGNORE_VIOLATIONS = {
("sense", "config_flow.py"),
("sms", "config_flow.py"),
("solarlog", "config_flow.py"),
("somfy", "config_flow.py"),
("sonos", "config_flow.py"),
("speedtestdotnet", "config_flow.py"),
("spider", "config_flow.py"),

View File

@ -2,10 +2,8 @@
import asyncio
from unittest.mock import patch
import pytest
from homeassistant import config_entries, data_entry_flow, setup
from homeassistant.components.somfy import DOMAIN, config_flow
from homeassistant.components.somfy import DOMAIN
from homeassistant.const import CONF_CLIENT_ID, CONF_CLIENT_SECRET
from homeassistant.helpers import config_entry_oauth2_flow
@ -15,39 +13,22 @@ CLIENT_ID_VALUE = "1234"
CLIENT_SECRET_VALUE = "5678"
@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):
"""Check flow abort when no configuration."""
flow = config_flow.SomfyFlowHandler()
flow.hass = hass
result = await flow.async_step_user()
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT
assert result["reason"] == "missing_configuration"
async def test_abort_if_existing_entry(hass):
"""Check flow abort when an entry already exist."""
flow = config_flow.SomfyFlowHandler()
flow.hass = hass
MockConfigEntry(domain=DOMAIN).add_to_hass(hass)
result = await flow.async_step_user()
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT
assert result["reason"] == "single_instance_allowed"
@ -63,8 +44,7 @@ async def test_full_flow(
DOMAIN: {
CONF_CLIENT_ID: CLIENT_ID_VALUE,
CONF_CLIENT_SECRET: CLIENT_SECRET_VALUE,
},
"http": {"base_url": "https://example.com"},
}
},
)
@ -123,17 +103,27 @@ async def test_full_flow(
assert entry.state == config_entries.ENTRY_STATE_NOT_LOADED
async def test_abort_if_authorization_timeout(
hass, mock_impl, current_request_with_host
):
async def test_abort_if_authorization_timeout(hass, current_request_with_host):
"""Check Somfy authorization timeout."""
flow = config_flow.SomfyFlowHandler()
flow.hass = hass
assert await setup.async_setup_component(
hass,
DOMAIN,
{
DOMAIN: {
CONF_CLIENT_ID: CLIENT_ID_VALUE,
CONF_CLIENT_SECRET: CLIENT_SECRET_VALUE,
}
},
)
with patch.object(
mock_impl, "async_generate_authorize_url", side_effect=asyncio.TimeoutError
with patch(
"homeassistant.components.somfy.config_entry_oauth2_flow."
"LocalOAuth2Implementation.async_generate_authorize_url",
side_effect=asyncio.TimeoutError,
):
result = await flow.async_step_user()
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT
assert result["reason"] == "authorize_url_timeout"