From c4e2e9c4f017b5aef573849629742c5c116cb7eb Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Mon, 14 Oct 2024 13:24:08 +0200 Subject: [PATCH] Use reauth_confirm in azure_devops (#128349) --- .../components/azure_devops/config_flow.py | 48 +++++++++---------- .../components/azure_devops/strings.json | 2 +- .../azure_devops/test_config_flow.py | 15 +++--- 3 files changed, 32 insertions(+), 33 deletions(-) diff --git a/homeassistant/components/azure_devops/config_flow.py b/homeassistant/components/azure_devops/config_flow.py index 995f9c5f5a1..13666343e1d 100644 --- a/homeassistant/components/azure_devops/config_flow.py +++ b/homeassistant/components/azure_devops/config_flow.py @@ -42,17 +42,6 @@ class AzureDevOpsFlowHandler(ConfigFlow, domain=DOMAIN): errors=errors or {}, ) - async def _show_reauth_form(self, errors: dict[str, str]) -> ConfigFlowResult: - """Show the reauth form to the user.""" - return self.async_show_form( - step_id="reauth", - description_placeholders={ - "project_url": f"{self._organization}/{self._project}" - }, - data_schema=vol.Schema({vol.Required(CONF_PAT): str}), - errors=errors or {}, - ) - async def _check_setup(self) -> dict[str, str] | None: """Check the setup of the flow.""" errors: dict[str, str] = {} @@ -106,22 +95,33 @@ class AzureDevOpsFlowHandler(ConfigFlow, domain=DOMAIN): self.context["title_placeholders"] = { "project_url": f"{self._organization}/{self._project}", } + return await self.async_step_reauth_confirm() - await self.async_set_unique_id(f"{self._organization}_{self._project}") - - errors = await self._check_setup() - if errors is not None: - return await self._show_reauth_form(errors) - - self.hass.config_entries.async_update_entry( - self._get_reauth_entry(), - data={ - CONF_ORG: self._organization, - CONF_PROJECT: self._project, - CONF_PAT: self._pat, + async def async_step_reauth_confirm( + self, user_input: dict[str, str] | None = None + ) -> ConfigFlowResult: + """Handle configuration by re-auth.""" + errors: dict[str, str] | None = None + if user_input is not None: + errors = await self._check_setup() + if errors is None: + self.hass.config_entries.async_update_entry( + self._get_reauth_entry(), + data={ + CONF_ORG: self._organization, + CONF_PROJECT: self._project, + CONF_PAT: self._pat, + }, + ) + return self.async_abort(reason="reauth_successful") + return self.async_show_form( + step_id="reauth_confirm", + description_placeholders={ + "project_url": f"{self._organization}/{self._project}" }, + data_schema=vol.Schema({vol.Required(CONF_PAT): str}), + errors=errors or {}, ) - return self.async_abort(reason="reauth_successful") def _async_create_entry(self) -> ConfigFlowResult: """Handle create entry.""" diff --git a/homeassistant/components/azure_devops/strings.json b/homeassistant/components/azure_devops/strings.json index c5304270396..f5fe5cd06a7 100644 --- a/homeassistant/components/azure_devops/strings.json +++ b/homeassistant/components/azure_devops/strings.json @@ -16,7 +16,7 @@ "description": "Set up an Azure DevOps instance to access your project. A Personal Access Token is only required for a private project.", "title": "Add Azure DevOps Project" }, - "reauth": { + "reauth_confirm": { "data": { "personal_access_token": "[%key:component::azure_devops::config::step::user::data::personal_access_token%]" }, diff --git a/tests/components/azure_devops/test_config_flow.py b/tests/components/azure_devops/test_config_flow.py index 9ebc9991939..577067d5744 100644 --- a/tests/components/azure_devops/test_config_flow.py +++ b/tests/components/azure_devops/test_config_flow.py @@ -62,7 +62,7 @@ async def test_reauth_authorization_error( result = await mock_config_entry.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM - assert result["step_id"] == "reauth" + assert result["step_id"] == "reauth_confirm" result2 = await hass.config_entries.flow.async_configure( result["flow_id"], @@ -71,7 +71,7 @@ async def test_reauth_authorization_error( await hass.async_block_till_done() assert result2["type"] is FlowResultType.FORM - assert result2["step_id"] == "reauth" + assert result2["step_id"] == "reauth_confirm" assert result2["errors"] == {"base": "invalid_auth"} @@ -114,7 +114,7 @@ async def test_reauth_connection_error( result = await mock_config_entry.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM - assert result["step_id"] == "reauth" + assert result["step_id"] == "reauth_confirm" result2 = await hass.config_entries.flow.async_configure( result["flow_id"], @@ -123,7 +123,7 @@ async def test_reauth_connection_error( await hass.async_block_till_done() assert result2["type"] is FlowResultType.FORM - assert result2["step_id"] == "reauth" + assert result2["step_id"] == "reauth_confirm" assert result2["errors"] == {"base": "cannot_connect"} @@ -170,7 +170,7 @@ async def test_reauth_project_error( result = await mock_config_entry.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM - assert result["step_id"] == "reauth" + assert result["step_id"] == "reauth_confirm" result2 = await hass.config_entries.flow.async_configure( result["flow_id"], @@ -179,7 +179,7 @@ async def test_reauth_project_error( await hass.async_block_till_done() assert result2["type"] is FlowResultType.FORM - assert result2["step_id"] == "reauth" + assert result2["step_id"] == "reauth_confirm" assert result2["errors"] == {"base": "project_error"} @@ -197,8 +197,7 @@ async def test_reauth_flow( result = await mock_config_entry.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM - assert result["step_id"] == "reauth" - assert result["errors"] == {"base": "invalid_auth"} + assert result["step_id"] == "reauth_confirm" mock_devops_client.authorize.return_value = True mock_devops_client.authorized = True