async def _async_flow_result_to_response( self, request: web.Request, client_id: str, result: data_entry_flow.FlowResult, ) -> web.Response: """Convert the flow result to a response.""" if result["type"] != data_entry_flow.FlowResultType.CREATE_ENTRY: # @log_invalid_auth does not work here since it returns HTTP 200. # We need to manually log failed login attempts. if (result["type"] == data_entry_flow.FlowResultType.FORM and (errors := result.get("errors")) and errors.get("base") in ( "invalid_auth", "invalid_code", )): await process_wrong_login(request) return self.json(_prepare_result_json(result))
async def complete_external_step( self, result: data_entry_flow.FlowResult) -> data_entry_flow.FlowResult: """Fixture method to complete the OAuth flow and return the completed result.""" client = await self.hass_client() state = config_entry_oauth2_flow._encode_jwt( self.hass, { "flow_id": result["flow_id"], "redirect_uri": "https://example.com/auth/external/callback", }, ) assert result["url"] == ( f"{AUTHORIZE_URL}?response_type=code&client_id={self.client_id}" "&redirect_uri=https://example.com/auth/external/callback" f"&state={state}") resp = await client.get( f"/auth/external/callback?code=abcd&state={state}") assert resp.status == 200 assert resp.headers["content-type"] == "text/html; charset=utf-8" self.aioclient_mock.post( TOKEN_URL, json={ "refresh_token": REFRESH_TOKEN, "access_token": ACCESS_TOKEN, "type": "bearer", "expires_in": 60, }, ) result = await self.hass.config_entries.flow.async_configure( result["flow_id"]) assert result.get("type") == data_entry_flow.RESULT_TYPE_CREATE_ENTRY assert result.get("title") == self.client_id assert "data" in result assert "token" in result["data"] return result