2018-10-16 08:35:35 +00:00
|
|
|
"""Test UniFi Controller."""
|
2021-02-05 15:31:47 +00:00
|
|
|
|
2020-01-03 18:23:17 +00:00
|
|
|
from copy import deepcopy
|
2019-10-07 19:55:35 +00:00
|
|
|
from datetime import timedelta
|
2021-01-01 21:31:56 +00:00
|
|
|
from unittest.mock import patch
|
2019-10-07 19:55:35 +00:00
|
|
|
|
2019-12-09 11:19:34 +00:00
|
|
|
import aiounifi
|
2019-02-14 04:36:06 +00:00
|
|
|
import pytest
|
|
|
|
|
2020-04-23 14:48:24 +00:00
|
|
|
from homeassistant.components.device_tracker import DOMAIN as TRACKER_DOMAIN
|
|
|
|
from homeassistant.components.sensor import DOMAIN as SENSOR_DOMAIN
|
|
|
|
from homeassistant.components.switch import DOMAIN as SWITCH_DOMAIN
|
2019-07-14 19:57:09 +00:00
|
|
|
from homeassistant.components.unifi.const import (
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_CONTROLLER,
|
|
|
|
CONF_SITE_ID,
|
2020-04-23 14:48:24 +00:00
|
|
|
DEFAULT_ALLOW_BANDWIDTH_SENSORS,
|
2020-09-18 17:33:37 +00:00
|
|
|
DEFAULT_ALLOW_UPTIME_SENSORS,
|
2020-04-23 14:48:24 +00:00
|
|
|
DEFAULT_DETECTION_TIME,
|
|
|
|
DEFAULT_TRACK_CLIENTS,
|
|
|
|
DEFAULT_TRACK_DEVICES,
|
|
|
|
DEFAULT_TRACK_WIRED_CLIENTS,
|
|
|
|
DOMAIN as UNIFI_DOMAIN,
|
2019-10-02 19:43:14 +00:00
|
|
|
UNIFI_WIRELESS_CLIENTS,
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2020-04-23 14:48:24 +00:00
|
|
|
from homeassistant.components.unifi.controller import (
|
|
|
|
SUPPORTED_PLATFORMS,
|
|
|
|
get_controller,
|
|
|
|
)
|
|
|
|
from homeassistant.components.unifi.errors import AuthenticationRequired, CannotConnect
|
2019-05-13 08:16:55 +00:00
|
|
|
from homeassistant.const import (
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_HOST,
|
|
|
|
CONF_PASSWORD,
|
|
|
|
CONF_PORT,
|
|
|
|
CONF_USERNAME,
|
|
|
|
CONF_VERIFY_SSL,
|
2021-02-05 15:31:47 +00:00
|
|
|
CONTENT_TYPE_JSON,
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2020-01-03 18:23:17 +00:00
|
|
|
from homeassistant.setup import async_setup_component
|
|
|
|
|
|
|
|
from tests.common import MockConfigEntry
|
2019-10-07 19:55:35 +00:00
|
|
|
|
2021-02-05 15:31:47 +00:00
|
|
|
DEFAULT_HOST = "1.2.3.4"
|
|
|
|
DEFAULT_SITE = "site_id"
|
|
|
|
|
2019-10-07 19:55:35 +00:00
|
|
|
CONTROLLER_HOST = {
|
|
|
|
"hostname": "controller_host",
|
2021-02-05 15:31:47 +00:00
|
|
|
"ip": DEFAULT_HOST,
|
2019-10-07 19:55:35 +00:00
|
|
|
"is_wired": True,
|
|
|
|
"last_seen": 1562600145,
|
|
|
|
"mac": "10:00:00:00:00:01",
|
|
|
|
"name": "Controller host",
|
|
|
|
"oui": "Producer",
|
|
|
|
"sw_mac": "00:00:00:00:01:01",
|
|
|
|
"sw_port": 1,
|
|
|
|
"wired-rx_bytes": 1234000000,
|
|
|
|
"wired-tx_bytes": 5678000000,
|
2020-09-18 17:33:37 +00:00
|
|
|
"uptime": 1562600160,
|
2019-10-07 19:55:35 +00:00
|
|
|
}
|
2019-07-14 19:57:09 +00:00
|
|
|
|
2018-10-16 08:35:35 +00:00
|
|
|
CONTROLLER_DATA = {
|
2021-02-05 15:31:47 +00:00
|
|
|
CONF_HOST: DEFAULT_HOST,
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_USERNAME: "username",
|
|
|
|
CONF_PASSWORD: "password",
|
2019-05-13 08:16:55 +00:00
|
|
|
CONF_PORT: 1234,
|
2021-02-05 15:31:47 +00:00
|
|
|
CONF_SITE_ID: DEFAULT_SITE,
|
2019-10-07 19:55:35 +00:00
|
|
|
CONF_VERIFY_SSL: False,
|
2018-10-16 08:35:35 +00:00
|
|
|
}
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
ENTRY_CONFIG = {CONF_CONTROLLER: CONTROLLER_DATA}
|
2020-01-03 18:23:17 +00:00
|
|
|
ENTRY_OPTIONS = {}
|
|
|
|
|
|
|
|
CONFIGURATION = []
|
2018-10-16 08:35:35 +00:00
|
|
|
|
2021-02-05 15:31:47 +00:00
|
|
|
SITE = [{"desc": "Site name", "name": "site_id", "role": "admin"}]
|
2020-05-12 22:37:16 +00:00
|
|
|
DESCRIPTION = [{"name": "username", "site_name": "site_id", "site_role": "admin"}]
|
2019-10-07 19:55:35 +00:00
|
|
|
|
|
|
|
|
2021-02-05 15:31:47 +00:00
|
|
|
def mock_default_unifi_requests(
|
|
|
|
aioclient_mock,
|
|
|
|
host,
|
|
|
|
site_id,
|
|
|
|
sites=None,
|
|
|
|
description=None,
|
|
|
|
clients_response=None,
|
|
|
|
clients_all_response=None,
|
|
|
|
devices_response=None,
|
|
|
|
dpiapp_response=None,
|
|
|
|
dpigroup_response=None,
|
|
|
|
wlans_response=None,
|
|
|
|
):
|
|
|
|
"""Mock default UniFi requests responses."""
|
|
|
|
aioclient_mock.get(f"https://{host}:1234", status=302) # Check UniFi OS
|
|
|
|
|
|
|
|
aioclient_mock.post(
|
|
|
|
f"https://{host}:1234/api/login",
|
|
|
|
json={"data": "login successful", "meta": {"rc": "ok"}},
|
|
|
|
headers={"content-type": CONTENT_TYPE_JSON},
|
|
|
|
)
|
|
|
|
|
|
|
|
aioclient_mock.get(
|
|
|
|
f"https://{host}:1234/api/self/sites",
|
|
|
|
json={"data": sites or [], "meta": {"rc": "ok"}},
|
|
|
|
headers={"content-type": CONTENT_TYPE_JSON},
|
|
|
|
)
|
|
|
|
|
|
|
|
aioclient_mock.get(
|
|
|
|
f"https://{host}:1234/api/s/{site_id}/self",
|
|
|
|
json={"data": description or [], "meta": {"rc": "ok"}},
|
|
|
|
headers={"content-type": CONTENT_TYPE_JSON},
|
|
|
|
)
|
|
|
|
|
|
|
|
aioclient_mock.get(
|
|
|
|
f"https://{host}:1234/api/s/{site_id}/stat/sta",
|
|
|
|
json={"data": clients_response or [], "meta": {"rc": "ok"}},
|
|
|
|
headers={"content-type": CONTENT_TYPE_JSON},
|
|
|
|
)
|
|
|
|
aioclient_mock.get(
|
|
|
|
f"https://{host}:1234/api/s/{site_id}/rest/user",
|
|
|
|
json={"data": clients_all_response or [], "meta": {"rc": "ok"}},
|
|
|
|
headers={"content-type": CONTENT_TYPE_JSON},
|
|
|
|
)
|
|
|
|
aioclient_mock.get(
|
|
|
|
f"https://{host}:1234/api/s/{site_id}/stat/device",
|
|
|
|
json={"data": devices_response or [], "meta": {"rc": "ok"}},
|
|
|
|
headers={"content-type": CONTENT_TYPE_JSON},
|
|
|
|
)
|
|
|
|
aioclient_mock.get(
|
|
|
|
f"https://{host}:1234/api/s/{site_id}/rest/dpiapp",
|
|
|
|
json={"data": dpiapp_response or [], "meta": {"rc": "ok"}},
|
|
|
|
headers={"content-type": CONTENT_TYPE_JSON},
|
|
|
|
)
|
|
|
|
aioclient_mock.get(
|
|
|
|
f"https://{host}:1234/api/s/{site_id}/rest/dpigroup",
|
|
|
|
json={"data": dpigroup_response or [], "meta": {"rc": "ok"}},
|
|
|
|
headers={"content-type": CONTENT_TYPE_JSON},
|
|
|
|
)
|
|
|
|
aioclient_mock.get(
|
|
|
|
f"https://{host}:1234/api/s/{site_id}/rest/wlanconf",
|
|
|
|
json={"data": wlans_response or [], "meta": {"rc": "ok"}},
|
|
|
|
headers={"content-type": CONTENT_TYPE_JSON},
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2019-10-07 19:55:35 +00:00
|
|
|
async def setup_unifi_integration(
|
|
|
|
hass,
|
2021-02-05 15:31:47 +00:00
|
|
|
aioclient_mock=None,
|
|
|
|
*,
|
2020-01-03 18:23:17 +00:00
|
|
|
config=ENTRY_CONFIG,
|
|
|
|
options=ENTRY_OPTIONS,
|
2021-02-05 15:31:47 +00:00
|
|
|
sites=SITE,
|
2020-05-12 22:37:16 +00:00
|
|
|
site_description=DESCRIPTION,
|
2020-01-03 18:23:17 +00:00
|
|
|
clients_response=None,
|
|
|
|
clients_all_response=None,
|
2021-02-05 15:31:47 +00:00
|
|
|
devices_response=None,
|
2020-11-03 07:36:37 +00:00
|
|
|
dpiapp_response=None,
|
2021-02-05 15:31:47 +00:00
|
|
|
dpigroup_response=None,
|
|
|
|
wlans_response=None,
|
2019-12-30 18:40:52 +00:00
|
|
|
known_wireless_clients=None,
|
2020-01-03 18:23:17 +00:00
|
|
|
controllers=None,
|
2019-10-07 19:55:35 +00:00
|
|
|
):
|
|
|
|
"""Create the UniFi controller."""
|
2020-04-23 14:48:24 +00:00
|
|
|
assert await async_setup_component(hass, UNIFI_DOMAIN, {})
|
2020-01-03 18:23:17 +00:00
|
|
|
|
|
|
|
config_entry = MockConfigEntry(
|
2020-04-23 14:48:24 +00:00
|
|
|
domain=UNIFI_DOMAIN,
|
2020-01-03 18:23:17 +00:00
|
|
|
data=deepcopy(config),
|
|
|
|
options=deepcopy(options),
|
2019-10-07 19:55:35 +00:00
|
|
|
entry_id=1,
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2020-01-03 18:23:17 +00:00
|
|
|
config_entry.add_to_hass(hass)
|
2018-10-16 08:35:35 +00:00
|
|
|
|
2019-12-30 18:40:52 +00:00
|
|
|
if known_wireless_clients:
|
|
|
|
hass.data[UNIFI_WIRELESS_CLIENTS].update_data(
|
|
|
|
known_wireless_clients, config_entry
|
|
|
|
)
|
|
|
|
|
2021-02-05 15:31:47 +00:00
|
|
|
if aioclient_mock:
|
|
|
|
mock_default_unifi_requests(
|
|
|
|
aioclient_mock,
|
|
|
|
host=config_entry.data[CONF_CONTROLLER][CONF_HOST],
|
|
|
|
site_id=config_entry.data[CONF_CONTROLLER][CONF_SITE_ID],
|
|
|
|
sites=sites,
|
|
|
|
description=site_description,
|
|
|
|
clients_response=clients_response,
|
|
|
|
clients_all_response=clients_all_response,
|
|
|
|
devices_response=devices_response,
|
|
|
|
dpiapp_response=dpiapp_response,
|
|
|
|
dpigroup_response=dpigroup_response,
|
|
|
|
wlans_response=wlans_response,
|
|
|
|
)
|
|
|
|
|
|
|
|
with patch.object(aiounifi.websocket.WSClient, "start", return_value=True):
|
2020-01-03 18:23:17 +00:00
|
|
|
await hass.config_entries.async_setup(config_entry.entry_id)
|
2019-10-07 19:55:35 +00:00
|
|
|
await hass.async_block_till_done()
|
2019-07-14 19:57:09 +00:00
|
|
|
|
2020-04-23 14:48:24 +00:00
|
|
|
if config_entry.entry_id not in hass.data[UNIFI_DOMAIN]:
|
2019-10-07 19:55:35 +00:00
|
|
|
return None
|
2018-10-16 08:35:35 +00:00
|
|
|
|
2021-02-05 15:31:47 +00:00
|
|
|
return config_entry
|
2018-10-16 08:35:35 +00:00
|
|
|
|
|
|
|
|
2021-02-05 15:31:47 +00:00
|
|
|
async def test_controller_setup(hass, aioclient_mock):
|
2019-10-07 19:55:35 +00:00
|
|
|
"""Successful setup."""
|
|
|
|
with patch(
|
|
|
|
"homeassistant.config_entries.ConfigEntries.async_forward_entry_setup",
|
|
|
|
return_value=True,
|
|
|
|
) as forward_entry_setup:
|
2021-02-05 15:31:47 +00:00
|
|
|
config_entry = await setup_unifi_integration(hass, aioclient_mock)
|
|
|
|
controller = hass.data[UNIFI_DOMAIN][config_entry.entry_id]
|
2019-10-07 19:55:35 +00:00
|
|
|
|
2020-01-03 18:23:17 +00:00
|
|
|
entry = controller.config_entry
|
2020-04-23 14:48:24 +00:00
|
|
|
assert len(forward_entry_setup.mock_calls) == len(SUPPORTED_PLATFORMS)
|
|
|
|
assert forward_entry_setup.mock_calls[0][1] == (entry, TRACKER_DOMAIN)
|
|
|
|
assert forward_entry_setup.mock_calls[1][1] == (entry, SENSOR_DOMAIN)
|
|
|
|
assert forward_entry_setup.mock_calls[2][1] == (entry, SWITCH_DOMAIN)
|
2019-10-07 19:55:35 +00:00
|
|
|
|
|
|
|
assert controller.host == CONTROLLER_DATA[CONF_HOST]
|
|
|
|
assert controller.site == CONTROLLER_DATA[CONF_SITE_ID]
|
2021-02-05 15:31:47 +00:00
|
|
|
assert controller.site_name == SITE[0]["desc"]
|
|
|
|
assert controller.site_role == SITE[0]["role"]
|
2019-10-07 19:55:35 +00:00
|
|
|
|
2020-04-23 14:48:24 +00:00
|
|
|
assert controller.option_allow_bandwidth_sensors == DEFAULT_ALLOW_BANDWIDTH_SENSORS
|
2020-09-18 17:33:37 +00:00
|
|
|
assert controller.option_allow_uptime_sensors == DEFAULT_ALLOW_UPTIME_SENSORS
|
2020-03-05 05:55:56 +00:00
|
|
|
assert isinstance(controller.option_block_clients, list)
|
2020-04-23 14:48:24 +00:00
|
|
|
assert controller.option_track_clients == DEFAULT_TRACK_CLIENTS
|
|
|
|
assert controller.option_track_devices == DEFAULT_TRACK_DEVICES
|
|
|
|
assert controller.option_track_wired_clients == DEFAULT_TRACK_WIRED_CLIENTS
|
|
|
|
assert controller.option_detection_time == timedelta(seconds=DEFAULT_DETECTION_TIME)
|
2021-01-20 22:58:02 +00:00
|
|
|
assert isinstance(controller.option_ssid_filter, set)
|
2018-10-16 08:35:35 +00:00
|
|
|
|
2019-10-07 19:55:35 +00:00
|
|
|
assert controller.mac is None
|
2018-10-16 08:35:35 +00:00
|
|
|
|
2021-02-01 16:55:16 +00:00
|
|
|
assert controller.signal_reachable == "unifi-reachable-1"
|
|
|
|
assert controller.signal_update == "unifi-update-1"
|
|
|
|
assert controller.signal_remove == "unifi-remove-1"
|
|
|
|
assert controller.signal_options_update == "unifi-options-1"
|
|
|
|
assert controller.signal_heartbeat_missed == "unifi-heartbeat-missed"
|
2018-10-16 08:35:35 +00:00
|
|
|
|
|
|
|
|
2021-02-05 15:31:47 +00:00
|
|
|
async def test_controller_mac(hass, aioclient_mock):
|
2019-10-07 19:55:35 +00:00
|
|
|
"""Test that it is possible to identify controller mac."""
|
2021-02-05 15:31:47 +00:00
|
|
|
config_entry = await setup_unifi_integration(
|
|
|
|
hass, aioclient_mock, clients_response=[CONTROLLER_HOST]
|
|
|
|
)
|
|
|
|
controller = hass.data[UNIFI_DOMAIN][config_entry.entry_id]
|
2020-04-23 14:48:24 +00:00
|
|
|
assert controller.mac == CONTROLLER_HOST["mac"]
|
2019-10-07 19:55:35 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_controller_not_accessible(hass):
|
|
|
|
"""Retry to login gets scheduled when connection fails."""
|
2020-04-23 14:48:24 +00:00
|
|
|
with patch(
|
|
|
|
"homeassistant.components.unifi.controller.get_controller",
|
|
|
|
side_effect=CannotConnect,
|
2020-01-03 18:23:17 +00:00
|
|
|
):
|
|
|
|
await setup_unifi_integration(hass)
|
2020-04-23 14:48:24 +00:00
|
|
|
assert hass.data[UNIFI_DOMAIN] == {}
|
2019-10-07 19:55:35 +00:00
|
|
|
|
|
|
|
|
2021-01-20 21:10:40 +00:00
|
|
|
async def test_controller_trigger_reauth_flow(hass):
|
|
|
|
"""Failed authentication trigger a reauthentication flow."""
|
|
|
|
with patch(
|
|
|
|
"homeassistant.components.unifi.controller.get_controller",
|
|
|
|
side_effect=AuthenticationRequired,
|
|
|
|
), patch.object(hass.config_entries.flow, "async_init") as mock_flow_init:
|
|
|
|
await setup_unifi_integration(hass)
|
|
|
|
mock_flow_init.assert_called_once()
|
|
|
|
assert hass.data[UNIFI_DOMAIN] == {}
|
|
|
|
|
|
|
|
|
2019-10-07 19:55:35 +00:00
|
|
|
async def test_controller_unknown_error(hass):
|
|
|
|
"""Unknown errors are handled."""
|
2020-04-23 14:48:24 +00:00
|
|
|
with patch(
|
|
|
|
"homeassistant.components.unifi.controller.get_controller",
|
|
|
|
side_effect=Exception,
|
|
|
|
):
|
2020-01-03 18:23:17 +00:00
|
|
|
await setup_unifi_integration(hass)
|
2020-04-23 14:48:24 +00:00
|
|
|
assert hass.data[UNIFI_DOMAIN] == {}
|
2019-10-07 19:55:35 +00:00
|
|
|
|
|
|
|
|
2021-02-05 15:31:47 +00:00
|
|
|
async def test_reset_after_successful_setup(hass, aioclient_mock):
|
2019-10-07 19:55:35 +00:00
|
|
|
"""Calling reset when the entry has been setup."""
|
2021-02-05 15:31:47 +00:00
|
|
|
config_entry = await setup_unifi_integration(hass, aioclient_mock)
|
|
|
|
controller = hass.data[UNIFI_DOMAIN][config_entry.entry_id]
|
2018-10-16 08:35:35 +00:00
|
|
|
|
2020-04-19 19:30:06 +00:00
|
|
|
assert len(controller.listeners) == 6
|
2018-10-16 08:35:35 +00:00
|
|
|
|
2019-10-07 19:55:35 +00:00
|
|
|
result = await controller.async_reset()
|
|
|
|
await hass.async_block_till_done()
|
2018-10-16 08:35:35 +00:00
|
|
|
|
2019-10-07 19:55:35 +00:00
|
|
|
assert result is True
|
|
|
|
assert len(controller.listeners) == 0
|
2018-10-16 08:35:35 +00:00
|
|
|
|
2019-10-07 19:55:35 +00:00
|
|
|
|
2021-02-05 15:31:47 +00:00
|
|
|
async def test_wireless_client_event_calls_update_wireless_devices(
|
|
|
|
hass, aioclient_mock
|
|
|
|
):
|
2020-01-31 19:23:25 +00:00
|
|
|
"""Call update_wireless_devices method when receiving wireless client event."""
|
2021-02-05 15:31:47 +00:00
|
|
|
config_entry = await setup_unifi_integration(hass, aioclient_mock)
|
|
|
|
controller = hass.data[UNIFI_DOMAIN][config_entry.entry_id]
|
2018-10-16 08:35:35 +00:00
|
|
|
|
2020-01-31 19:23:25 +00:00
|
|
|
with patch(
|
|
|
|
"homeassistant.components.unifi.controller.UniFiController.update_wireless_clients",
|
|
|
|
return_value=None,
|
|
|
|
) as wireless_clients_mock:
|
|
|
|
controller.api.websocket._data = {
|
|
|
|
"meta": {"rc": "ok", "message": "events"},
|
|
|
|
"data": [
|
|
|
|
{
|
|
|
|
"datetime": "2020-01-20T19:37:04Z",
|
|
|
|
"key": aiounifi.events.WIRELESS_CLIENT_CONNECTED,
|
|
|
|
"msg": "User[11:22:33:44:55:66] has connected to WLAN",
|
|
|
|
"time": 1579549024893,
|
|
|
|
}
|
|
|
|
],
|
|
|
|
}
|
|
|
|
controller.api.session_handler("data")
|
2019-10-07 19:55:35 +00:00
|
|
|
|
2020-01-31 19:23:25 +00:00
|
|
|
assert wireless_clients_mock.assert_called_once
|
2019-10-07 19:55:35 +00:00
|
|
|
|
2018-10-16 08:35:35 +00:00
|
|
|
|
|
|
|
async def test_get_controller(hass):
|
|
|
|
"""Successful call."""
|
2020-04-08 21:19:39 +00:00
|
|
|
with patch("aiounifi.Controller.check_unifi_os", return_value=True), patch(
|
|
|
|
"aiounifi.Controller.login", return_value=True
|
|
|
|
):
|
2020-04-23 14:48:24 +00:00
|
|
|
assert await get_controller(hass, **CONTROLLER_DATA)
|
2018-10-16 08:35:35 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_get_controller_verify_ssl_false(hass):
|
|
|
|
"""Successful call with verify ssl set to false."""
|
|
|
|
controller_data = dict(CONTROLLER_DATA)
|
2019-05-13 08:16:55 +00:00
|
|
|
controller_data[CONF_VERIFY_SSL] = False
|
2020-04-08 21:19:39 +00:00
|
|
|
with patch("aiounifi.Controller.check_unifi_os", return_value=True), patch(
|
|
|
|
"aiounifi.Controller.login", return_value=True
|
|
|
|
):
|
2020-04-23 14:48:24 +00:00
|
|
|
assert await get_controller(hass, **controller_data)
|
2018-10-16 08:35:35 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_get_controller_login_failed(hass):
|
|
|
|
"""Check that get_controller can handle a failed login."""
|
2020-04-08 21:19:39 +00:00
|
|
|
with patch("aiounifi.Controller.check_unifi_os", return_value=True), patch(
|
2020-01-03 18:23:17 +00:00
|
|
|
"aiounifi.Controller.login", side_effect=aiounifi.Unauthorized
|
2020-04-23 14:48:24 +00:00
|
|
|
), pytest.raises(AuthenticationRequired):
|
|
|
|
await get_controller(hass, **CONTROLLER_DATA)
|
2018-10-16 08:35:35 +00:00
|
|
|
|
|
|
|
|
2020-12-10 20:25:50 +00:00
|
|
|
async def test_get_controller_controller_bad_gateway(hass):
|
|
|
|
"""Check that get_controller can handle controller being unavailable."""
|
|
|
|
with patch("aiounifi.Controller.check_unifi_os", return_value=True), patch(
|
|
|
|
"aiounifi.Controller.login", side_effect=aiounifi.BadGateway
|
|
|
|
), pytest.raises(CannotConnect):
|
|
|
|
await get_controller(hass, **CONTROLLER_DATA)
|
|
|
|
|
|
|
|
|
|
|
|
async def test_get_controller_controller_service_unavailable(hass):
|
|
|
|
"""Check that get_controller can handle controller being unavailable."""
|
|
|
|
with patch("aiounifi.Controller.check_unifi_os", return_value=True), patch(
|
|
|
|
"aiounifi.Controller.login", side_effect=aiounifi.ServiceUnavailable
|
|
|
|
), pytest.raises(CannotConnect):
|
|
|
|
await get_controller(hass, **CONTROLLER_DATA)
|
|
|
|
|
|
|
|
|
2018-10-16 08:35:35 +00:00
|
|
|
async def test_get_controller_controller_unavailable(hass):
|
|
|
|
"""Check that get_controller can handle controller being unavailable."""
|
2020-04-08 21:19:39 +00:00
|
|
|
with patch("aiounifi.Controller.check_unifi_os", return_value=True), patch(
|
2020-01-03 18:23:17 +00:00
|
|
|
"aiounifi.Controller.login", side_effect=aiounifi.RequestError
|
2020-04-23 14:48:24 +00:00
|
|
|
), pytest.raises(CannotConnect):
|
|
|
|
await get_controller(hass, **CONTROLLER_DATA)
|
2018-10-16 08:35:35 +00:00
|
|
|
|
|
|
|
|
2021-01-20 21:10:40 +00:00
|
|
|
async def test_get_controller_login_required(hass):
|
|
|
|
"""Check that get_controller can handle unknown errors."""
|
|
|
|
with patch("aiounifi.Controller.check_unifi_os", return_value=True), patch(
|
|
|
|
"aiounifi.Controller.login", side_effect=aiounifi.LoginRequired
|
|
|
|
), pytest.raises(AuthenticationRequired):
|
|
|
|
await get_controller(hass, **CONTROLLER_DATA)
|
|
|
|
|
|
|
|
|
2018-10-16 08:35:35 +00:00
|
|
|
async def test_get_controller_unknown_error(hass):
|
2020-01-31 16:33:00 +00:00
|
|
|
"""Check that get_controller can handle unknown errors."""
|
2020-04-08 21:19:39 +00:00
|
|
|
with patch("aiounifi.Controller.check_unifi_os", return_value=True), patch(
|
2020-01-03 18:23:17 +00:00
|
|
|
"aiounifi.Controller.login", side_effect=aiounifi.AiounifiException
|
2020-04-23 14:48:24 +00:00
|
|
|
), pytest.raises(AuthenticationRequired):
|
|
|
|
await get_controller(hass, **CONTROLLER_DATA)
|