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:
Paulus Schoutsen 2021-12-01 04:51:10 -08:00 committed by GitHub
parent e5e1e7b7e0
commit 72d8882c79
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 48 additions and 28 deletions

View file

@ -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")

View file

@ -154,12 +154,16 @@ 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))

View file

@ -54,6 +54,9 @@ async def test_invalid_username_password(hass, aiohttp_client):
step = await resp.json() step = await resp.json()
# Incorrect username # Incorrect username
with patch(
"homeassistant.components.auth.login_flow.process_wrong_login"
) as mock_process_wrong_login:
resp = await client.post( resp = await client.post(
f"/auth/login_flow/{step['flow_id']}", f"/auth/login_flow/{step['flow_id']}",
json={ json={
@ -65,11 +68,15 @@ async def test_invalid_username_password(hass, aiohttp_client):
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
with patch(
"homeassistant.components.auth.login_flow.process_wrong_login"
) as mock_process_wrong_login:
resp = await client.post( resp = await client.post(
f"/auth/login_flow/{step['flow_id']}", f"/auth/login_flow/{step['flow_id']}",
json={ json={
@ -81,6 +88,7 @@ async def test_invalid_username_password(hass, aiohttp_client):
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()
with patch(
"homeassistant.components.auth.login_flow.process_success_login"
) as mock_process_success_login:
resp = await client.post( resp = await client.post(
f"/auth/login_flow/{step['flow_id']}", f"/auth/login_flow/{step['flow_id']}",
json={"client_id": CLIENT_ID, "username": "test-user", "password": "test-pass"}, 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):