Clean up Ambient PWS tests (#64486)

This commit is contained in:
Aaron Bach 2022-01-19 17:03:53 -07:00 committed by GitHub
parent 801011863b
commit 31fe6d9592
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 72 additions and 72 deletions

View File

@ -0,0 +1,48 @@
"""Define test fixtures for Ambient PWS."""
import json
from unittest.mock import patch
import pytest
from homeassistant.components.ambient_station.const import CONF_APP_KEY, DOMAIN
from homeassistant.const import CONF_API_KEY
from homeassistant.setup import async_setup_component
from tests.common import MockConfigEntry, load_fixture
@pytest.fixture(name="config")
def config_fixture(hass):
"""Define a config entry data fixture."""
return {
CONF_API_KEY: "12345abcde12345abcde",
CONF_APP_KEY: "67890fghij67890fghij",
}
@pytest.fixture(name="config_entry")
def config_entry_fixture(hass, config):
"""Define a config entry fixture."""
entry = MockConfigEntry(domain=DOMAIN, data=config)
entry.add_to_hass(hass)
return entry
@pytest.fixture(name="devices", scope="session")
def devices_fixture():
"""Define devices data."""
return json.loads(load_fixture("devices.json", "ambient_station"))
@pytest.fixture(name="setup_ambient_station")
async def setup_ambient_station_fixture(hass, config, devices):
"""Define a fixture to set up AirVisual."""
with patch("homeassistant.components.ambient_station.PLATFORMS", []), patch(
"homeassistant.components.ambient_station.config_flow.API.get_devices",
side_effect=devices,
), patch("aioambient.api.API.get_devices", side_effect=devices), patch(
"aioambient.websocket.Websocket.connect"
):
assert await async_setup_component(hass, DOMAIN, config)
await hass.async_block_till_done()
yield

View File

@ -1,102 +1,54 @@
"""Define tests for the Ambient PWS config flow."""
import json
from unittest.mock import patch
from unittest.mock import AsyncMock
import aioambient
from aioambient.errors import AmbientError
import pytest
from homeassistant import data_entry_flow
from homeassistant.components.ambient_station import CONF_APP_KEY, DOMAIN, config_flow
from homeassistant.components.ambient_station import CONF_APP_KEY, DOMAIN
from homeassistant.config_entries import SOURCE_USER
from homeassistant.const import CONF_API_KEY
from tests.common import MockConfigEntry, load_fixture, mock_coro
@pytest.fixture
def get_devices_response():
"""Define a fixture for a successful /devices response."""
return mock_coro()
@pytest.fixture
def mock_aioambient(get_devices_response):
"""Mock the aioambient library."""
with patch("homeassistant.components.ambient_station.config_flow.API") as API:
api = API()
api.get_devices.return_value = get_devices_response
yield api
async def test_duplicate_error(hass):
async def test_duplicate_error(hass, config, config_entry, setup_ambient_station):
"""Test that errors are shown when duplicates are added."""
conf = {CONF_API_KEY: "12345abcde12345abcde", CONF_APP_KEY: "67890fghij67890fghij"}
MockConfigEntry(
domain=DOMAIN, unique_id="67890fghij67890fghij", data=conf
).add_to_hass(hass)
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_USER}, data=conf
DOMAIN, context={"source": SOURCE_USER}, data=config
)
assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT
assert result["reason"] == "already_configured"
@pytest.mark.parametrize(
"get_devices_response", [mock_coro(exception=aioambient.errors.AmbientError)]
"devices,error",
[
(AmbientError, "invalid_key"),
(AsyncMock(return_value=[]), "no_devices"),
],
)
async def test_invalid_api_key(hass, mock_aioambient):
"""Test that an invalid API/App Key throws an error."""
conf = {CONF_API_KEY: "12345abcde12345abcde", CONF_APP_KEY: "67890fghij67890fghij"}
flow = config_flow.AmbientStationFlowHandler()
flow.hass = hass
flow.context = {"source": SOURCE_USER}
result = await flow.async_step_user(user_input=conf)
assert result["errors"] == {"base": "invalid_key"}
@pytest.mark.parametrize("get_devices_response", [mock_coro(return_value=[])])
async def test_no_devices(hass, mock_aioambient):
"""Test that an account with no associated devices throws an error."""
conf = {CONF_API_KEY: "12345abcde12345abcde", CONF_APP_KEY: "67890fghij67890fghij"}
flow = config_flow.AmbientStationFlowHandler()
flow.hass = hass
flow.context = {"source": SOURCE_USER}
result = await flow.async_step_user(user_input=conf)
assert result["errors"] == {"base": "no_devices"}
async def test_errors(hass, config, devices, error, setup_ambient_station):
"""Test that various issues show the correct error."""
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_USER}, data=config
)
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
assert result["errors"] == {"base": error}
async def test_show_form(hass):
"""Test that the form is served with no input."""
flow = config_flow.AmbientStationFlowHandler()
flow.hass = hass
flow.context = {"source": SOURCE_USER}
result = await flow.async_step_user(user_input=None)
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_USER}
)
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
assert result["step_id"] == "user"
@pytest.mark.parametrize(
"get_devices_response",
[mock_coro(return_value=json.loads(load_fixture("ambient_devices.json")))],
)
async def test_step_user(hass, mock_aioambient):
async def test_step_user(hass, config, setup_ambient_station):
"""Test that the user step works."""
conf = {CONF_API_KEY: "12345abcde12345abcde", CONF_APP_KEY: "67890fghij67890fghij"}
flow = config_flow.AmbientStationFlowHandler()
flow.hass = hass
flow.context = {"source": SOURCE_USER}
result = await flow.async_step_user(user_input=conf)
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_USER}, data=config
)
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
assert result["title"] == "67890fghij67"
assert result["data"] == {