Handle errors response to be None (#60679)
Co-authored-by: Philip Allgaier <mail@spacegaier.de> Co-authored-by: Franck Nijhof <git@frenck.dev>
This commit is contained in:
parent
e5e1e7b7e0
commit
72d8882c79
3 changed files with 48 additions and 28 deletions
|
@ -102,7 +102,7 @@ class ExampleLoginFlow(LoginFlow):
|
||||||
self, user_input: dict[str, str] | None = None
|
self, user_input: dict[str, str] | None = None
|
||||||
) -> FlowResult:
|
) -> FlowResult:
|
||||||
"""Handle the step of the form."""
|
"""Handle the step of the form."""
|
||||||
errors = {}
|
errors = None
|
||||||
|
|
||||||
if user_input is not None:
|
if user_input is not None:
|
||||||
try:
|
try:
|
||||||
|
@ -110,7 +110,7 @@ class ExampleLoginFlow(LoginFlow):
|
||||||
user_input["username"], user_input["password"]
|
user_input["username"], user_input["password"]
|
||||||
)
|
)
|
||||||
except InvalidAuthError:
|
except InvalidAuthError:
|
||||||
errors["base"] = "invalid_auth"
|
errors = {"base": "invalid_auth"}
|
||||||
|
|
||||||
if not errors:
|
if not errors:
|
||||||
user_input.pop("password")
|
user_input.pop("password")
|
||||||
|
|
|
@ -154,14 +154,18 @@ class LoginFlowBaseView(HomeAssistantView):
|
||||||
async def _async_flow_result_to_response(self, request, client_id, result):
|
async def _async_flow_result_to_response(self, request, client_id, result):
|
||||||
"""Convert the flow result to a response."""
|
"""Convert the flow result to a response."""
|
||||||
if result["type"] != data_entry_flow.RESULT_TYPE_CREATE_ENTRY:
|
if result["type"] != data_entry_flow.RESULT_TYPE_CREATE_ENTRY:
|
||||||
if result["type"] == data_entry_flow.RESULT_TYPE_FORM:
|
# @log_invalid_auth does not work here since it returns HTTP 200.
|
||||||
# @log_invalid_auth does not work here since it returns HTTP 200
|
# We need to manually log failed login attempts.
|
||||||
# need manually log failed login attempts
|
if (
|
||||||
if result.get("errors", {}).get("base") in (
|
result["type"] == data_entry_flow.RESULT_TYPE_FORM
|
||||||
|
and (errors := result.get("errors"))
|
||||||
|
and errors.get("base")
|
||||||
|
in (
|
||||||
"invalid_auth",
|
"invalid_auth",
|
||||||
"invalid_code",
|
"invalid_code",
|
||||||
):
|
)
|
||||||
await process_wrong_login(request)
|
):
|
||||||
|
await process_wrong_login(request)
|
||||||
return self.json(_prepare_result_json(result))
|
return self.json(_prepare_result_json(result))
|
||||||
|
|
||||||
result.pop("data")
|
result.pop("data")
|
||||||
|
|
|
@ -54,33 +54,41 @@ async def test_invalid_username_password(hass, aiohttp_client):
|
||||||
step = await resp.json()
|
step = await resp.json()
|
||||||
|
|
||||||
# Incorrect username
|
# Incorrect username
|
||||||
resp = await client.post(
|
with patch(
|
||||||
f"/auth/login_flow/{step['flow_id']}",
|
"homeassistant.components.auth.login_flow.process_wrong_login"
|
||||||
json={
|
) as mock_process_wrong_login:
|
||||||
"client_id": CLIENT_ID,
|
resp = await client.post(
|
||||||
"username": "wrong-user",
|
f"/auth/login_flow/{step['flow_id']}",
|
||||||
"password": "test-pass",
|
json={
|
||||||
},
|
"client_id": CLIENT_ID,
|
||||||
)
|
"username": "wrong-user",
|
||||||
|
"password": "test-pass",
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
assert resp.status == HTTPStatus.OK
|
assert resp.status == HTTPStatus.OK
|
||||||
step = await resp.json()
|
step = await resp.json()
|
||||||
|
assert len(mock_process_wrong_login.mock_calls) == 1
|
||||||
|
|
||||||
assert step["step_id"] == "init"
|
assert step["step_id"] == "init"
|
||||||
assert step["errors"]["base"] == "invalid_auth"
|
assert step["errors"]["base"] == "invalid_auth"
|
||||||
|
|
||||||
# Incorrect password
|
# Incorrect password
|
||||||
resp = await client.post(
|
with patch(
|
||||||
f"/auth/login_flow/{step['flow_id']}",
|
"homeassistant.components.auth.login_flow.process_wrong_login"
|
||||||
json={
|
) as mock_process_wrong_login:
|
||||||
"client_id": CLIENT_ID,
|
resp = await client.post(
|
||||||
"username": "test-user",
|
f"/auth/login_flow/{step['flow_id']}",
|
||||||
"password": "wrong-pass",
|
json={
|
||||||
},
|
"client_id": CLIENT_ID,
|
||||||
)
|
"username": "test-user",
|
||||||
|
"password": "wrong-pass",
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
assert resp.status == HTTPStatus.OK
|
assert resp.status == HTTPStatus.OK
|
||||||
step = await resp.json()
|
step = await resp.json()
|
||||||
|
assert len(mock_process_wrong_login.mock_calls) == 1
|
||||||
|
|
||||||
assert step["step_id"] == "init"
|
assert step["step_id"] == "init"
|
||||||
assert step["errors"]["base"] == "invalid_auth"
|
assert step["errors"]["base"] == "invalid_auth"
|
||||||
|
@ -105,15 +113,23 @@ async def test_login_exist_user(hass, aiohttp_client):
|
||||||
assert resp.status == HTTPStatus.OK
|
assert resp.status == HTTPStatus.OK
|
||||||
step = await resp.json()
|
step = await resp.json()
|
||||||
|
|
||||||
resp = await client.post(
|
with patch(
|
||||||
f"/auth/login_flow/{step['flow_id']}",
|
"homeassistant.components.auth.login_flow.process_success_login"
|
||||||
json={"client_id": CLIENT_ID, "username": "test-user", "password": "test-pass"},
|
) as mock_process_success_login:
|
||||||
)
|
resp = await client.post(
|
||||||
|
f"/auth/login_flow/{step['flow_id']}",
|
||||||
|
json={
|
||||||
|
"client_id": CLIENT_ID,
|
||||||
|
"username": "test-user",
|
||||||
|
"password": "test-pass",
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
assert resp.status == HTTPStatus.OK
|
assert resp.status == HTTPStatus.OK
|
||||||
step = await resp.json()
|
step = await resp.json()
|
||||||
assert step["type"] == "create_entry"
|
assert step["type"] == "create_entry"
|
||||||
assert len(step["result"]) > 1
|
assert len(step["result"]) > 1
|
||||||
|
assert len(mock_process_success_login.mock_calls) == 1
|
||||||
|
|
||||||
|
|
||||||
async def test_login_local_only_user(hass, aiohttp_client):
|
async def test_login_local_only_user(hass, aiohttp_client):
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue