mirror of
https://github.com/home-assistant/core.git
synced 2025-07-20 19:57:07 +00:00
UniFi - Catch controllers running on UniFi OS that don't have a local user configured (#35060)
This commit is contained in:
parent
221b07595f
commit
a2048b4c7a
@ -32,7 +32,12 @@ from .const import (
|
|||||||
LOGGER,
|
LOGGER,
|
||||||
)
|
)
|
||||||
from .controller import get_controller
|
from .controller import get_controller
|
||||||
from .errors import AlreadyConfigured, AuthenticationRequired, CannotConnect
|
from .errors import (
|
||||||
|
AlreadyConfigured,
|
||||||
|
AuthenticationRequired,
|
||||||
|
CannotConnect,
|
||||||
|
NoLocalUser,
|
||||||
|
)
|
||||||
|
|
||||||
DEFAULT_PORT = 8443
|
DEFAULT_PORT = 8443
|
||||||
DEFAULT_SITE_ID = "default"
|
DEFAULT_SITE_ID = "default"
|
||||||
@ -129,6 +134,8 @@ class UnifiFlowHandler(config_entries.ConfigFlow, domain=UNIFI_DOMAIN):
|
|||||||
|
|
||||||
for site in self.sites.values():
|
for site in self.sites.values():
|
||||||
if desc == site["desc"]:
|
if desc == site["desc"]:
|
||||||
|
if "role" not in site:
|
||||||
|
raise NoLocalUser
|
||||||
self.config[CONF_SITE_ID] = site["name"]
|
self.config[CONF_SITE_ID] = site["name"]
|
||||||
break
|
break
|
||||||
|
|
||||||
@ -147,6 +154,9 @@ class UnifiFlowHandler(config_entries.ConfigFlow, domain=UNIFI_DOMAIN):
|
|||||||
except AlreadyConfigured:
|
except AlreadyConfigured:
|
||||||
return self.async_abort(reason="already_configured")
|
return self.async_abort(reason="already_configured")
|
||||||
|
|
||||||
|
except NoLocalUser:
|
||||||
|
return self.async_abort(reason="no_local_user")
|
||||||
|
|
||||||
if len(self.sites) == 1:
|
if len(self.sites) == 1:
|
||||||
self.desc = next(iter(self.sites.values()))["desc"]
|
self.desc = next(iter(self.sites.values()))["desc"]
|
||||||
return await self.async_step_site(user_input={})
|
return await self.async_step_site(user_input={})
|
||||||
|
@ -22,5 +22,9 @@ class LoginRequired(UnifiException):
|
|||||||
"""Component got logged out."""
|
"""Component got logged out."""
|
||||||
|
|
||||||
|
|
||||||
|
class NoLocalUser(UnifiException):
|
||||||
|
"""No local user."""
|
||||||
|
|
||||||
|
|
||||||
class UserLevel(UnifiException):
|
class UserLevel(UnifiException):
|
||||||
"""User level too low."""
|
"""User level too low."""
|
||||||
|
@ -20,6 +20,7 @@
|
|||||||
},
|
},
|
||||||
"abort": {
|
"abort": {
|
||||||
"already_configured": "Controller site is already configured",
|
"already_configured": "Controller site is already configured",
|
||||||
|
"no_local_user": "No local user found, configure a local account on controller and try again",
|
||||||
"user_privilege": "User needs to be administrator"
|
"user_privilege": "User needs to be administrator"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
"config": {
|
"config": {
|
||||||
"abort": {
|
"abort": {
|
||||||
"already_configured": "Controller site is already configured",
|
"already_configured": "Controller site is already configured",
|
||||||
|
"no_local_user": "No local user found, configure a local account on controller and try again",
|
||||||
"user_privilege": "User needs to be administrator"
|
"user_privilege": "User needs to be administrator"
|
||||||
},
|
},
|
||||||
"error": {
|
"error": {
|
||||||
|
@ -185,6 +185,53 @@ async def test_flow_fails_site_already_configured(hass, aioclient_mock):
|
|||||||
)
|
)
|
||||||
|
|
||||||
assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT
|
assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT
|
||||||
|
assert result["reason"] == "already_configured"
|
||||||
|
|
||||||
|
|
||||||
|
async def test_flow_fails_site_has_no_local_user(hass, aioclient_mock):
|
||||||
|
"""Test config flow."""
|
||||||
|
entry = MockConfigEntry(
|
||||||
|
domain=UNIFI_DOMAIN, data={"controller": {"host": "1.2.3.4", "site": "site_id"}}
|
||||||
|
)
|
||||||
|
entry.add_to_hass(hass)
|
||||||
|
|
||||||
|
result = await hass.config_entries.flow.async_init(
|
||||||
|
UNIFI_DOMAIN, context={"source": "user"}
|
||||||
|
)
|
||||||
|
|
||||||
|
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
|
||||||
|
assert result["step_id"] == "user"
|
||||||
|
|
||||||
|
aioclient_mock.get("https://1.2.3.4:1234", status=302)
|
||||||
|
|
||||||
|
aioclient_mock.post(
|
||||||
|
"https://1.2.3.4:1234/api/login",
|
||||||
|
json={"data": "login successful", "meta": {"rc": "ok"}},
|
||||||
|
headers={"content-type": "application/json"},
|
||||||
|
)
|
||||||
|
|
||||||
|
aioclient_mock.get(
|
||||||
|
"https://1.2.3.4:1234/api/self/sites",
|
||||||
|
json={
|
||||||
|
"data": [{"desc": "Site name", "name": "site_id"}],
|
||||||
|
"meta": {"rc": "ok"},
|
||||||
|
},
|
||||||
|
headers={"content-type": "application/json"},
|
||||||
|
)
|
||||||
|
|
||||||
|
result = await hass.config_entries.flow.async_configure(
|
||||||
|
result["flow_id"],
|
||||||
|
user_input={
|
||||||
|
CONF_HOST: "1.2.3.4",
|
||||||
|
CONF_USERNAME: "username",
|
||||||
|
CONF_PASSWORD: "password",
|
||||||
|
CONF_PORT: 1234,
|
||||||
|
CONF_VERIFY_SSL: True,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT
|
||||||
|
assert result["reason"] == "no_local_user"
|
||||||
|
|
||||||
|
|
||||||
async def test_flow_fails_user_credentials_faulty(hass, aioclient_mock):
|
async def test_flow_fails_user_credentials_faulty(hass, aioclient_mock):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user