Add reconfiguration to onedrive_for_business (#163054)

This commit is contained in:
Josef Zweck
2026-02-15 13:32:05 +01:00
committed by GitHub
parent 40890419bb
commit 11fe11cc03
5 changed files with 166 additions and 16 deletions
@@ -33,7 +33,7 @@ from homeassistant.components.onedrive_for_business.const import (
from homeassistant.core import HomeAssistant
from homeassistant.setup import async_setup_component
from .const import BACKUP_METADATA, CLIENT_ID, CLIENT_SECRET, IDENTITY_SET
from .const import BACKUP_METADATA, CLIENT_ID, CLIENT_SECRET, IDENTITY_SET, TENANT_ID
from tests.common import MockConfigEntry
@@ -77,7 +77,7 @@ def mock_config_entry(expires_at: int, scopes: list[str]) -> MockConfigEntry:
},
CONF_FOLDER_PATH: "backups/home_assistant",
CONF_FOLDER_ID: "my_folder_id",
CONF_TENANT_ID: "mock-tenant-id",
CONF_TENANT_ID: TENANT_ID,
},
unique_id="mock_drive_id",
)
@@ -35,7 +35,6 @@ async def _do_get_token(
result: ConfigFlowResult,
hass_client_no_auth: ClientSessionGenerator,
aioclient_mock: AiohttpClientMocker,
tenant_id: str = TENANT_ID,
) -> None:
state = config_entry_oauth2_flow._encode_jwt(
hass,
@@ -46,8 +45,8 @@ async def _do_get_token(
)
scope = "Files.ReadWrite.All+offline_access+openid"
authorize_url = OAUTH2_AUTHORIZE.format(tenant_id=tenant_id)
token_url = OAUTH2_TOKEN.format(tenant_id=tenant_id)
authorize_url = OAUTH2_AUTHORIZE.format(tenant_id=TENANT_ID)
token_url = OAUTH2_TOKEN.format(tenant_id=TENANT_ID)
assert result["url"] == (
f"{authorize_url}?response_type=code&client_id={CLIENT_ID}"
@@ -266,6 +265,99 @@ async def test_already_configured(
assert result["reason"] == "already_configured"
@pytest.mark.usefixtures("current_request_with_host")
async def test_reconfigure_flow(
hass: HomeAssistant,
hass_client_no_auth: ClientSessionGenerator,
aioclient_mock: AiohttpClientMocker,
mock_onedrive_client: MagicMock,
mock_config_entry: MockConfigEntry,
mock_setup_entry: AsyncMock,
) -> None:
"""Test reconfigure flow."""
await setup_integration(hass, mock_config_entry)
result = await mock_config_entry.start_reconfigure_flow(hass)
await _do_get_token(hass, result, hass_client_no_auth, aioclient_mock)
result = await hass.config_entries.flow.async_configure(result["flow_id"])
assert result["type"] is FlowResultType.FORM
assert result["step_id"] == "reconfigure_folder"
result = await hass.config_entries.flow.async_configure(
result["flow_id"], {CONF_FOLDER_PATH: "new/folder/path"}
)
assert result["type"] is FlowResultType.ABORT
assert result["reason"] == "reconfigure_successful"
assert mock_config_entry.data[CONF_FOLDER_PATH] == "new/folder/path"
assert mock_config_entry.data[CONF_FOLDER_ID] == "my_folder_id"
assert mock_config_entry.data[CONF_TOKEN][CONF_ACCESS_TOKEN] == "mock-access-token"
assert mock_config_entry.data[CONF_TOKEN]["refresh_token"] == "mock-refresh-token"
@pytest.mark.usefixtures("current_request_with_host")
async def test_reconfigure_flow_error(
hass: HomeAssistant,
hass_client_no_auth: ClientSessionGenerator,
aioclient_mock: AiohttpClientMocker,
mock_onedrive_client: MagicMock,
mock_config_entry: MockConfigEntry,
mock_setup_entry: AsyncMock,
) -> None:
"""Test reconfigure flow errors."""
mock_config_entry.add_to_hass(hass)
await hass.async_block_till_done()
result = await mock_config_entry.start_reconfigure_flow(hass)
await _do_get_token(hass, result, hass_client_no_auth, aioclient_mock)
result = await hass.config_entries.flow.async_configure(result["flow_id"])
assert result["type"] is FlowResultType.FORM
assert result["step_id"] == "reconfigure_folder"
mock_onedrive_client.create_folder.side_effect = OneDriveException()
result = await hass.config_entries.flow.async_configure(
result["flow_id"], {CONF_FOLDER_PATH: "new/folder/path"}
)
assert result["type"] is FlowResultType.FORM
assert result["step_id"] == "reconfigure_folder"
assert result["errors"] == {"base": "folder_creation_error"}
# clear side effect and try again
mock_onedrive_client.create_folder.side_effect = None
result = await hass.config_entries.flow.async_configure(
result["flow_id"], {CONF_FOLDER_PATH: "new/folder/path"}
)
assert result["type"] is FlowResultType.ABORT
assert result["reason"] == "reconfigure_successful"
assert mock_config_entry.data[CONF_FOLDER_PATH] == "new/folder/path"
assert mock_config_entry.data[CONF_TOKEN][CONF_ACCESS_TOKEN] == "mock-access-token"
assert mock_config_entry.data[CONF_TOKEN]["refresh_token"] == "mock-refresh-token"
@pytest.mark.usefixtures("current_request_with_host")
async def test_reconfigure_flow_wrong_drive(
hass: HomeAssistant,
hass_client_no_auth: ClientSessionGenerator,
aioclient_mock: AiohttpClientMocker,
mock_setup_entry: AsyncMock,
mock_config_entry: MockConfigEntry,
mock_drive: Drive,
) -> None:
"""Test that the reconfigure flow fails on a different drive id."""
mock_drive.id = "other_drive_id"
mock_config_entry.add_to_hass(hass)
await hass.async_block_till_done()
result = await mock_config_entry.start_reconfigure_flow(hass)
await _do_get_token(hass, result, hass_client_no_auth, aioclient_mock)
@pytest.mark.usefixtures("current_request_with_host")
async def test_reauth_flow(
hass: HomeAssistant,
@@ -284,9 +376,7 @@ async def test_reauth_flow(
assert result["step_id"] == "reauth_confirm"
result = await hass.config_entries.flow.async_configure(result["flow_id"], {})
await _do_get_token(
hass, result, hass_client_no_auth, aioclient_mock, "mock-tenant-id"
)
await _do_get_token(hass, result, hass_client_no_auth, aioclient_mock)
result = await hass.config_entries.flow.async_configure(result["flow_id"])
assert result["type"] is FlowResultType.ABORT
@@ -295,7 +385,7 @@ async def test_reauth_flow(
assert mock_config_entry.data[CONF_TOKEN]["refresh_token"] == "mock-refresh-token"
assert mock_config_entry.data[CONF_FOLDER_PATH] == "backups/home_assistant"
assert mock_config_entry.data[CONF_FOLDER_ID] == "my_folder_id"
assert mock_config_entry.data[CONF_TENANT_ID] == "mock-tenant-id"
assert mock_config_entry.data[CONF_TENANT_ID] == TENANT_ID
@pytest.mark.usefixtures("current_request_with_host")
@@ -319,9 +409,7 @@ async def test_reauth_flow_id_changed(
assert result["step_id"] == "reauth_confirm"
result = await hass.config_entries.flow.async_configure(result["flow_id"], {})
await _do_get_token(
hass, result, hass_client_no_auth, aioclient_mock, "mock-tenant-id"
)
await _do_get_token(hass, result, hass_client_no_auth, aioclient_mock)
result = await hass.config_entries.flow.async_configure(result["flow_id"])
assert result["type"] is FlowResultType.ABORT
@@ -330,4 +418,4 @@ async def test_reauth_flow_id_changed(
assert mock_config_entry.data[CONF_TOKEN]["refresh_token"] == "mock-refresh-token"
assert mock_config_entry.data[CONF_FOLDER_PATH] == "backups/home_assistant"
assert mock_config_entry.data[CONF_FOLDER_ID] == "my_folder_id"
assert mock_config_entry.data[CONF_TENANT_ID] == "mock-tenant-id"
assert mock_config_entry.data[CONF_TENANT_ID] == TENANT_ID