Improve add user error messages (#120909)
This commit is contained in:
parent
001ee0cc0b
commit
a0b604f98c
4 changed files with 32 additions and 33 deletions
|
@ -55,13 +55,6 @@ class InvalidUser(HomeAssistantError):
|
|||
Will not be raised when validating authentication.
|
||||
"""
|
||||
|
||||
|
||||
class InvalidUsername(InvalidUser):
|
||||
"""Raised when invalid username is specified.
|
||||
|
||||
Will not be raised when validating authentication.
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
*args: object,
|
||||
|
@ -77,6 +70,13 @@ class InvalidUsername(InvalidUser):
|
|||
)
|
||||
|
||||
|
||||
class InvalidUsername(InvalidUser):
|
||||
"""Raised when invalid username is specified.
|
||||
|
||||
Will not be raised when validating authentication.
|
||||
"""
|
||||
|
||||
|
||||
class Data:
|
||||
"""Hold the user data."""
|
||||
|
||||
|
@ -216,7 +216,7 @@ class Data:
|
|||
break
|
||||
|
||||
if index is None:
|
||||
raise InvalidUser
|
||||
raise InvalidUser(translation_key="user_not_found")
|
||||
|
||||
self.users.pop(index)
|
||||
|
||||
|
@ -232,7 +232,7 @@ class Data:
|
|||
user["password"] = self.hash_password(new_password, True).decode()
|
||||
break
|
||||
else:
|
||||
raise InvalidUser
|
||||
raise InvalidUser(translation_key="user_not_found")
|
||||
|
||||
@callback
|
||||
def _validate_new_username(self, new_username: str) -> None:
|
||||
|
@ -275,7 +275,7 @@ class Data:
|
|||
self._async_check_for_not_normalized_usernames(self._data)
|
||||
break
|
||||
else:
|
||||
raise InvalidUser
|
||||
raise InvalidUser(translation_key="user_not_found")
|
||||
|
||||
async def async_save(self) -> None:
|
||||
"""Save data."""
|
||||
|
|
|
@ -37,7 +37,10 @@
|
|||
"message": "Username \"{username}\" already exists"
|
||||
},
|
||||
"username_not_normalized": {
|
||||
"message": "Username \"{new_username}\" is not normalized"
|
||||
"message": "Username \"{new_username}\" is not normalized. Please make sure the username is lowercase and does not contain any whitespace."
|
||||
},
|
||||
"user_not_found": {
|
||||
"message": "User not found"
|
||||
}
|
||||
},
|
||||
"issues": {
|
||||
|
|
|
@ -53,11 +53,7 @@ async def websocket_create(
|
|||
)
|
||||
return
|
||||
|
||||
try:
|
||||
await provider.async_add_auth(msg["username"], msg["password"])
|
||||
except auth_ha.InvalidUser:
|
||||
connection.send_error(msg["id"], "username_exists", "Username already exists")
|
||||
return
|
||||
|
||||
credentials = await provider.async_get_or_create_credentials(
|
||||
{"username": msg["username"]}
|
||||
|
@ -94,13 +90,7 @@ async def websocket_delete(
|
|||
connection.send_result(msg["id"])
|
||||
return
|
||||
|
||||
try:
|
||||
await provider.async_remove_auth(msg["username"])
|
||||
except auth_ha.InvalidUser:
|
||||
connection.send_error(
|
||||
msg["id"], "auth_not_found", "Given username was not found."
|
||||
)
|
||||
return
|
||||
|
||||
connection.send_result(msg["id"])
|
||||
|
||||
|
@ -187,14 +177,8 @@ async def websocket_admin_change_password(
|
|||
)
|
||||
return
|
||||
|
||||
try:
|
||||
await provider.async_change_password(username, msg["password"])
|
||||
connection.send_result(msg["id"])
|
||||
except auth_ha.InvalidUser:
|
||||
connection.send_error(
|
||||
msg["id"], "credentials_not_found", "Credentials not found"
|
||||
)
|
||||
return
|
||||
|
||||
|
||||
@websocket_api.websocket_command(
|
||||
|
|
|
@ -183,7 +183,13 @@ async def test_create_auth_duplicate_username(
|
|||
|
||||
result = await client.receive_json()
|
||||
assert not result["success"], result
|
||||
assert result["error"]["code"] == "username_exists"
|
||||
assert result["error"] == {
|
||||
"code": "home_assistant_error",
|
||||
"message": "username_already_exists",
|
||||
"translation_key": "username_already_exists",
|
||||
"translation_placeholders": {"username": "test-user"},
|
||||
"translation_domain": "auth",
|
||||
}
|
||||
|
||||
|
||||
async def test_delete_removes_just_auth(
|
||||
|
@ -282,7 +288,13 @@ async def test_delete_unknown_auth(
|
|||
|
||||
result = await client.receive_json()
|
||||
assert not result["success"], result
|
||||
assert result["error"]["code"] == "auth_not_found"
|
||||
assert result["error"] == {
|
||||
"code": "home_assistant_error",
|
||||
"message": "user_not_found",
|
||||
"translation_key": "user_not_found",
|
||||
"translation_placeholders": None,
|
||||
"translation_domain": "auth",
|
||||
}
|
||||
|
||||
|
||||
async def test_change_password(
|
||||
|
|
Loading…
Add table
Reference in a new issue