Refactor Airly tests (#44315)
This commit is contained in:
parent
7c63119ad2
commit
94e1f8e631
5 changed files with 171 additions and 185 deletions
|
@ -1,6 +1,4 @@
|
|||
"""Define tests for the Airly config flow."""
|
||||
import json
|
||||
|
||||
from airly.exceptions import AirlyError
|
||||
|
||||
from homeassistant import data_entry_flow
|
||||
|
@ -11,14 +9,15 @@ from homeassistant.const import (
|
|||
CONF_LATITUDE,
|
||||
CONF_LONGITUDE,
|
||||
CONF_NAME,
|
||||
HTTP_FORBIDDEN,
|
||||
HTTP_UNAUTHORIZED,
|
||||
)
|
||||
|
||||
from tests.async_mock import patch
|
||||
from tests.common import MockConfigEntry, load_fixture
|
||||
from . import API_KEY_VALIDATION_URL, API_POINT_URL
|
||||
|
||||
from tests.common import MockConfigEntry, load_fixture, patch
|
||||
|
||||
CONFIG = {
|
||||
CONF_NAME: "abcd",
|
||||
CONF_NAME: "Home",
|
||||
CONF_API_KEY: "foo",
|
||||
CONF_LATITUDE: 123,
|
||||
CONF_LONGITUDE: 456,
|
||||
|
@ -35,69 +34,63 @@ async def test_show_form(hass):
|
|||
assert result["step_id"] == SOURCE_USER
|
||||
|
||||
|
||||
async def test_invalid_api_key(hass):
|
||||
async def test_invalid_api_key(hass, aioclient_mock):
|
||||
"""Test that errors are shown when API key is invalid."""
|
||||
with patch(
|
||||
"airly._private._RequestsHandler.get",
|
||||
side_effect=AirlyError(
|
||||
HTTP_FORBIDDEN, {"message": "Invalid authentication credentials"}
|
||||
aioclient_mock.get(
|
||||
API_KEY_VALIDATION_URL,
|
||||
exc=AirlyError(
|
||||
HTTP_UNAUTHORIZED, {"message": "Invalid authentication credentials"}
|
||||
),
|
||||
):
|
||||
)
|
||||
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": SOURCE_USER}, data=CONFIG
|
||||
)
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": SOURCE_USER}, data=CONFIG
|
||||
)
|
||||
|
||||
assert result["errors"] == {"base": "invalid_api_key"}
|
||||
assert result["errors"] == {"base": "invalid_api_key"}
|
||||
|
||||
|
||||
async def test_invalid_location(hass):
|
||||
async def test_invalid_location(hass, aioclient_mock):
|
||||
"""Test that errors are shown when location is invalid."""
|
||||
with patch(
|
||||
"airly._private._RequestsHandler.get",
|
||||
return_value=json.loads(load_fixture("airly_no_station.json")),
|
||||
):
|
||||
aioclient_mock.get(
|
||||
API_KEY_VALIDATION_URL, text=load_fixture("airly_valid_station.json")
|
||||
)
|
||||
aioclient_mock.get(API_POINT_URL, text=load_fixture("airly_no_station.json"))
|
||||
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": SOURCE_USER}, data=CONFIG
|
||||
)
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": SOURCE_USER}, data=CONFIG
|
||||
)
|
||||
|
||||
assert result["errors"] == {"base": "wrong_location"}
|
||||
assert result["errors"] == {"base": "wrong_location"}
|
||||
|
||||
|
||||
async def test_duplicate_error(hass):
|
||||
async def test_duplicate_error(hass, aioclient_mock):
|
||||
"""Test that errors are shown when duplicates are added."""
|
||||
aioclient_mock.get(API_POINT_URL, text=load_fixture("airly_valid_station.json"))
|
||||
MockConfigEntry(domain=DOMAIN, unique_id="123-456", data=CONFIG).add_to_hass(hass)
|
||||
|
||||
with patch(
|
||||
"airly._private._RequestsHandler.get",
|
||||
return_value=json.loads(load_fixture("airly_valid_station.json")),
|
||||
):
|
||||
MockConfigEntry(domain=DOMAIN, unique_id="123-456", data=CONFIG).add_to_hass(
|
||||
hass
|
||||
)
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": SOURCE_USER}, data=CONFIG
|
||||
)
|
||||
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": SOURCE_USER}, data=CONFIG
|
||||
)
|
||||
|
||||
assert result["type"] == "abort"
|
||||
assert result["reason"] == "already_configured"
|
||||
assert result["type"] == "abort"
|
||||
assert result["reason"] == "already_configured"
|
||||
|
||||
|
||||
async def test_create_entry(hass):
|
||||
async def test_create_entry(hass, aioclient_mock):
|
||||
"""Test that the user step works."""
|
||||
aioclient_mock.get(
|
||||
API_KEY_VALIDATION_URL, text=load_fixture("airly_valid_station.json")
|
||||
)
|
||||
aioclient_mock.get(API_POINT_URL, text=load_fixture("airly_valid_station.json"))
|
||||
|
||||
with patch(
|
||||
"airly._private._RequestsHandler.get",
|
||||
return_value=json.loads(load_fixture("airly_valid_station.json")),
|
||||
):
|
||||
|
||||
with patch("homeassistant.components.airly.async_setup_entry", return_value=True):
|
||||
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"] == CONFIG[CONF_NAME]
|
||||
assert result["data"][CONF_LATITUDE] == CONFIG[CONF_LATITUDE]
|
||||
assert result["data"][CONF_LONGITUDE] == CONFIG[CONF_LONGITUDE]
|
||||
assert result["data"][CONF_API_KEY] == CONFIG[CONF_API_KEY]
|
||||
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
|
||||
assert result["title"] == CONFIG[CONF_NAME]
|
||||
assert result["data"][CONF_LATITUDE] == CONFIG[CONF_LATITUDE]
|
||||
assert result["data"][CONF_LONGITUDE] == CONFIG[CONF_LONGITUDE]
|
||||
assert result["data"][CONF_API_KEY] == CONFIG[CONF_API_KEY]
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue