2019-09-06 20:21:56 +00:00
|
|
|
"""Tests for iAqualink config flow."""
|
|
|
|
from unittest.mock import patch
|
|
|
|
|
2021-12-27 20:20:55 +00:00
|
|
|
from iaqualink.exception import (
|
|
|
|
AqualinkServiceException,
|
|
|
|
AqualinkServiceUnauthorizedException,
|
|
|
|
)
|
2019-09-06 20:21:56 +00:00
|
|
|
import pytest
|
|
|
|
|
|
|
|
from homeassistant.components.iaqualink import config_flow
|
2019-12-09 10:56:51 +00:00
|
|
|
|
2019-09-06 20:21:56 +00:00
|
|
|
|
|
|
|
@pytest.mark.parametrize("step", ["import", "user"])
|
2021-12-27 20:20:55 +00:00
|
|
|
async def test_already_configured(hass, config_entry, config_data, step):
|
2019-09-06 20:21:56 +00:00
|
|
|
"""Test config flow when iaqualink component is already setup."""
|
2021-12-27 20:20:55 +00:00
|
|
|
config_entry.add_to_hass(hass)
|
2019-09-06 20:21:56 +00:00
|
|
|
|
|
|
|
flow = config_flow.AqualinkFlowHandler()
|
|
|
|
flow.hass = hass
|
|
|
|
flow.context = {}
|
|
|
|
|
|
|
|
fname = f"async_step_{step}"
|
|
|
|
func = getattr(flow, fname)
|
2021-12-27 20:20:55 +00:00
|
|
|
result = await func(config_data)
|
2019-09-06 20:21:56 +00:00
|
|
|
|
|
|
|
assert result["type"] == "abort"
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize("step", ["import", "user"])
|
|
|
|
async def test_without_config(hass, step):
|
2021-12-27 20:20:55 +00:00
|
|
|
"""Test config flow with no configuration."""
|
2019-09-06 20:21:56 +00:00
|
|
|
flow = config_flow.AqualinkFlowHandler()
|
|
|
|
flow.hass = hass
|
|
|
|
flow.context = {}
|
|
|
|
|
|
|
|
fname = f"async_step_{step}"
|
|
|
|
func = getattr(flow, fname)
|
|
|
|
result = await func()
|
|
|
|
|
|
|
|
assert result["type"] == "form"
|
|
|
|
assert result["step_id"] == "user"
|
|
|
|
assert result["errors"] == {}
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize("step", ["import", "user"])
|
2021-12-27 20:20:55 +00:00
|
|
|
async def test_with_invalid_credentials(hass, config_data, step):
|
2019-09-06 20:21:56 +00:00
|
|
|
"""Test config flow with invalid username and/or password."""
|
|
|
|
flow = config_flow.AqualinkFlowHandler()
|
|
|
|
flow.hass = hass
|
|
|
|
|
|
|
|
fname = f"async_step_{step}"
|
|
|
|
func = getattr(flow, fname)
|
|
|
|
with patch(
|
2021-12-27 20:20:55 +00:00
|
|
|
"homeassistant.components.iaqualink.config_flow.AqualinkClient.login",
|
|
|
|
side_effect=AqualinkServiceUnauthorizedException,
|
2019-09-06 20:21:56 +00:00
|
|
|
):
|
2021-12-27 20:20:55 +00:00
|
|
|
result = await func(config_data)
|
|
|
|
|
|
|
|
assert result["type"] == "form"
|
|
|
|
assert result["step_id"] == "user"
|
|
|
|
assert result["errors"] == {"base": "invalid_auth"}
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize("step", ["import", "user"])
|
|
|
|
async def test_service_exception(hass, config_data, step):
|
|
|
|
"""Test config flow encountering service exception."""
|
|
|
|
flow = config_flow.AqualinkFlowHandler()
|
|
|
|
flow.hass = hass
|
|
|
|
|
|
|
|
fname = f"async_step_{step}"
|
|
|
|
func = getattr(flow, fname)
|
|
|
|
with patch(
|
|
|
|
"homeassistant.components.iaqualink.config_flow.AqualinkClient.login",
|
|
|
|
side_effect=AqualinkServiceException,
|
|
|
|
):
|
|
|
|
result = await func(config_data)
|
2019-09-06 20:21:56 +00:00
|
|
|
|
|
|
|
assert result["type"] == "form"
|
|
|
|
assert result["step_id"] == "user"
|
2020-10-05 19:55:12 +00:00
|
|
|
assert result["errors"] == {"base": "cannot_connect"}
|
2019-09-06 20:21:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize("step", ["import", "user"])
|
2021-12-27 20:20:55 +00:00
|
|
|
async def test_with_existing_config(hass, config_data, step):
|
|
|
|
"""Test config flow with existing configuration."""
|
2019-09-06 20:21:56 +00:00
|
|
|
flow = config_flow.AqualinkFlowHandler()
|
|
|
|
flow.hass = hass
|
|
|
|
flow.context = {}
|
|
|
|
|
|
|
|
fname = f"async_step_{step}"
|
|
|
|
func = getattr(flow, fname)
|
2021-12-27 20:20:55 +00:00
|
|
|
with patch(
|
|
|
|
"homeassistant.components.iaqualink.config_flow.AqualinkClient.login",
|
|
|
|
return_value=None,
|
|
|
|
):
|
|
|
|
result = await func(config_data)
|
2019-09-06 20:21:56 +00:00
|
|
|
|
|
|
|
assert result["type"] == "create_entry"
|
2021-12-27 20:20:55 +00:00
|
|
|
assert result["title"] == config_data["username"]
|
|
|
|
assert result["data"] == config_data
|