Use reauth_confirm in azure_devops (#128349)

This commit is contained in:
epenet 2024-10-14 13:24:08 +02:00 committed by GitHub
parent 7df973648c
commit c4e2e9c4f0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 32 additions and 33 deletions

View File

@ -42,17 +42,6 @@ class AzureDevOpsFlowHandler(ConfigFlow, domain=DOMAIN):
errors=errors or {}, 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: async def _check_setup(self) -> dict[str, str] | None:
"""Check the setup of the flow.""" """Check the setup of the flow."""
errors: dict[str, str] = {} errors: dict[str, str] = {}
@ -106,13 +95,16 @@ class AzureDevOpsFlowHandler(ConfigFlow, domain=DOMAIN):
self.context["title_placeholders"] = { self.context["title_placeholders"] = {
"project_url": f"{self._organization}/{self._project}", "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}") 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() errors = await self._check_setup()
if errors is not None: if errors is None:
return await self._show_reauth_form(errors)
self.hass.config_entries.async_update_entry( self.hass.config_entries.async_update_entry(
self._get_reauth_entry(), self._get_reauth_entry(),
data={ data={
@ -122,6 +114,14 @@ class AzureDevOpsFlowHandler(ConfigFlow, domain=DOMAIN):
}, },
) )
return self.async_abort(reason="reauth_successful") 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 {},
)
def _async_create_entry(self) -> ConfigFlowResult: def _async_create_entry(self) -> ConfigFlowResult:
"""Handle create entry.""" """Handle create entry."""

View File

@ -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.", "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" "title": "Add Azure DevOps Project"
}, },
"reauth": { "reauth_confirm": {
"data": { "data": {
"personal_access_token": "[%key:component::azure_devops::config::step::user::data::personal_access_token%]" "personal_access_token": "[%key:component::azure_devops::config::step::user::data::personal_access_token%]"
}, },

View File

@ -62,7 +62,7 @@ async def test_reauth_authorization_error(
result = await mock_config_entry.start_reauth_flow(hass) result = await mock_config_entry.start_reauth_flow(hass)
assert result["type"] is FlowResultType.FORM 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( result2 = await hass.config_entries.flow.async_configure(
result["flow_id"], result["flow_id"],
@ -71,7 +71,7 @@ async def test_reauth_authorization_error(
await hass.async_block_till_done() await hass.async_block_till_done()
assert result2["type"] is FlowResultType.FORM assert result2["type"] is FlowResultType.FORM
assert result2["step_id"] == "reauth" assert result2["step_id"] == "reauth_confirm"
assert result2["errors"] == {"base": "invalid_auth"} 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) result = await mock_config_entry.start_reauth_flow(hass)
assert result["type"] is FlowResultType.FORM 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( result2 = await hass.config_entries.flow.async_configure(
result["flow_id"], result["flow_id"],
@ -123,7 +123,7 @@ async def test_reauth_connection_error(
await hass.async_block_till_done() await hass.async_block_till_done()
assert result2["type"] is FlowResultType.FORM assert result2["type"] is FlowResultType.FORM
assert result2["step_id"] == "reauth" assert result2["step_id"] == "reauth_confirm"
assert result2["errors"] == {"base": "cannot_connect"} 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) result = await mock_config_entry.start_reauth_flow(hass)
assert result["type"] is FlowResultType.FORM 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( result2 = await hass.config_entries.flow.async_configure(
result["flow_id"], result["flow_id"],
@ -179,7 +179,7 @@ async def test_reauth_project_error(
await hass.async_block_till_done() await hass.async_block_till_done()
assert result2["type"] is FlowResultType.FORM assert result2["type"] is FlowResultType.FORM
assert result2["step_id"] == "reauth" assert result2["step_id"] == "reauth_confirm"
assert result2["errors"] == {"base": "project_error"} assert result2["errors"] == {"base": "project_error"}
@ -197,8 +197,7 @@ async def test_reauth_flow(
result = await mock_config_entry.start_reauth_flow(hass) result = await mock_config_entry.start_reauth_flow(hass)
assert result["type"] is FlowResultType.FORM assert result["type"] is FlowResultType.FORM
assert result["step_id"] == "reauth" assert result["step_id"] == "reauth_confirm"
assert result["errors"] == {"base": "invalid_auth"}
mock_devops_client.authorize.return_value = True mock_devops_client.authorize.return_value = True
mock_devops_client.authorized = True mock_devops_client.authorized = True