UniFi - Add block network access control to config option (#32004)

* Add block network access control to config option

* Clean up
This commit is contained in:
Paulus Schoutsen authored and GitHub committed 2020-03-04 21:55:56 -08:00
1 parent 1615a5ee81
commit d216c1f2ac
9 files changed
+314 -104

No files matched your search

+78 -26
View File
@@ -5,7 +5,18 @@ from asynctest import patch
from homeassistant import data_entry_flow
from homeassistant.components import unifi
from homeassistant.components.unifi import config_flow
from homeassistant.components.unifi.const import CONF_CONTROLLER, CONF_SITE_ID
from homeassistant.components.unifi.config_flow import CONF_NEW_CLIENT
from homeassistant.components.unifi.const import (
CONF_ALLOW_BANDWIDTH_SENSORS,
CONF_BLOCK_CLIENT,
CONF_CONTROLLER,
CONF_DETECTION_TIME,
CONF_SITE_ID,
CONF_SSID_FILTER,
CONF_TRACK_CLIENTS,
CONF_TRACK_DEVICES,
CONF_TRACK_WIRED_CLIENTS,
)
from homeassistant.const import (
CONF_HOST,
CONF_PASSWORD,
@@ -18,6 +29,8 @@ from .test_controller import setup_unifi_integration
from tests.common import MockConfigEntry
CLIENTS = [{"mac": "00:00:00:00:00:01"}]
WLANS = [{"name": "SSID 1"}, {"name": "SSID 2"}]
@@ -28,7 +41,7 @@ async def test_flow_works(hass, aioclient_mock, mock_discovery):
config_flow.DOMAIN, context={"source": "user"}
)
assert result["type"] == "form"
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
assert result["step_id"] == "user"
assert result["data_schema"]({CONF_USERNAME: "", CONF_PASSWORD: ""}) == {
CONF_HOST: "unifi",
@@ -64,7 +77,7 @@ async def test_flow_works(hass, aioclient_mock, mock_discovery):
},
)
assert result["type"] == "create_entry"
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
assert result["title"] == "Site name"
assert result["data"] == {
CONF_CONTROLLER: {
@@ -84,7 +97,7 @@ async def test_flow_works_multiple_sites(hass, aioclient_mock):
config_flow.DOMAIN, context={"source": "user"}
)
assert result["type"] == "form"
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
assert result["step_id"] == "user"
aioclient_mock.post(
@@ -116,7 +129,7 @@ async def test_flow_works_multiple_sites(hass, aioclient_mock):
},
)
assert result["type"] == "form"
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
assert result["step_id"] == "site"
assert result["data_schema"]({"site": "site name"})
assert result["data_schema"]({"site": "site2 name"})
@@ -133,7 +146,7 @@ async def test_flow_fails_site_already_configured(hass, aioclient_mock):
config_flow.DOMAIN, context={"source": "user"}
)
assert result["type"] == "form"
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
assert result["step_id"] == "user"
aioclient_mock.post(
@@ -162,7 +175,7 @@ async def test_flow_fails_site_already_configured(hass, aioclient_mock):
},
)
assert result["type"] == "abort"
assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT
async def test_flow_fails_user_credentials_faulty(hass, aioclient_mock):
@@ -171,7 +184,7 @@ async def test_flow_fails_user_credentials_faulty(hass, aioclient_mock):
config_flow.DOMAIN, context={"source": "user"}
)
assert result["type"] == "form"
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
assert result["step_id"] == "user"
with patch("aiounifi.Controller.login", side_effect=aiounifi.errors.Unauthorized):
@@ -186,7 +199,7 @@ async def test_flow_fails_user_credentials_faulty(hass, aioclient_mock):
},
)
assert result["type"] == "form"
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
assert result["errors"] == {"base": "faulty_credentials"}
@@ -196,7 +209,7 @@ async def test_flow_fails_controller_unavailable(hass, aioclient_mock):
config_flow.DOMAIN, context={"source": "user"}
)
assert result["type"] == "form"
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
assert result["step_id"] == "user"
with patch("aiounifi.Controller.login", side_effect=aiounifi.errors.RequestError):
@@ -211,7 +224,7 @@ async def test_flow_fails_controller_unavailable(hass, aioclient_mock):
},
)
assert result["type"] == "form"
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
assert result["errors"] == {"base": "service_unavailable"}
@@ -221,7 +234,7 @@ async def test_flow_fails_unknown_problem(hass, aioclient_mock):
config_flow.DOMAIN, context={"source": "user"}
)
assert result["type"] == "form"
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
assert result["step_id"] == "user"
with patch("aiounifi.Controller.login", side_effect=Exception):
@@ -236,12 +249,14 @@ async def test_flow_fails_unknown_problem(hass, aioclient_mock):
},
)
assert result["type"] == "abort"
assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT
async def test_option_flow(hass):
"""Test config flow options."""
controller = await setup_unifi_integration(hass, wlans_response=WLANS)
controller = await setup_unifi_integration(
hass, clients_response=CLIENTS, wlans_response=WLANS
)
result = await hass.config_entries.options.async_init(
controller.config_entry.entry_id
@@ -253,27 +268,64 @@ async def test_option_flow(hass):
result = await hass.config_entries.options.async_configure(
result["flow_id"],
user_input={
config_flow.CONF_TRACK_CLIENTS: False,
config_flow.CONF_TRACK_WIRED_CLIENTS: False,
config_flow.CONF_TRACK_DEVICES: False,
config_flow.CONF_SSID_FILTER: ["SSID 1"],
config_flow.CONF_DETECTION_TIME: 100,
CONF_TRACK_CLIENTS: False,
CONF_TRACK_WIRED_CLIENTS: False,
CONF_TRACK_DEVICES: False,
CONF_SSID_FILTER: ["SSID 1"],
CONF_DETECTION_TIME: 100,
},
)
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
assert result["step_id"] == "client_control"
clients_to_block = hass.config_entries.options._progress[result["flow_id"]].options[
CONF_BLOCK_CLIENT
]
result = await hass.config_entries.options.async_configure(
result["flow_id"],
user_input={
CONF_BLOCK_CLIENT: clients_to_block,
CONF_NEW_CLIENT: "00:00:00:00:00:01",
},
)
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
assert result["step_id"] == "client_control"
result = await hass.config_entries.options.async_configure(
result["flow_id"],
user_input={
CONF_BLOCK_CLIENT: clients_to_block,
CONF_NEW_CLIENT: "00:00:00:00:00:02",
},
)
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
assert result["step_id"] == "client_control"
assert result["errors"] == {"base": "unknown_client_mac"}
clients_to_block = hass.config_entries.options._progress[result["flow_id"]].options[
CONF_BLOCK_CLIENT
]
result = await hass.config_entries.options.async_configure(
result["flow_id"], user_input={CONF_BLOCK_CLIENT: clients_to_block},
)
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
assert result["step_id"] == "statistics_sensors"
result = await hass.config_entries.options.async_configure(
result["flow_id"], user_input={config_flow.CONF_ALLOW_BANDWIDTH_SENSORS: True}
result["flow_id"], user_input={CONF_ALLOW_BANDWIDTH_SENSORS: True}
)
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
assert result["data"] == {
config_flow.CONF_TRACK_CLIENTS: False,
config_flow.CONF_TRACK_WIRED_CLIENTS: False,
config_flow.CONF_TRACK_DEVICES: False,
config_flow.CONF_DETECTION_TIME: 100,
config_flow.CONF_SSID_FILTER: ["SSID 1"],
config_flow.CONF_ALLOW_BANDWIDTH_SENSORS: True,
CONF_TRACK_CLIENTS: False,
CONF_TRACK_WIRED_CLIENTS: False,
CONF_TRACK_DEVICES: False,
CONF_DETECTION_TIME: 100,
CONF_SSID_FILTER: ["SSID 1"],
CONF_BLOCK_CLIENT: ["00:00:00:00:00:01"],
CONF_ALLOW_BANDWIDTH_SENSORS: True,
}