Repository: home-assistant/core
File: tests/components/onedrive_for_business/test_config_flow.py (Python)

Code before:

@pytest.mark.usefixtures("current_request_with_host")
async def test_reauth_flow(
    hass: HomeAssistant,
    hass_client_no_auth: ClientSessionGenerator,
    aioclient_mock: AiohttpClientMocker,
    mock_setup_entry: AsyncMock,
    mock_config_entry: MockConfigEntry,
) -> None:
    """Test that the reauth flow works."""

    await setup_integration(hass, mock_config_entry)

    result = await mock_config_entry.start_reauth_flow(hass)

    assert result["type"] is FlowResultType.FORM
    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"
    )
    result = await hass.config_entries.flow.async_configure(result["flow_id"])

    assert result["type"] is FlowResultType.ABORT
    assert result["reason"] == "reauth_successful"


@pytest.mark.usefixtures("current_request_with_host")
async def test_reauth_flow_id_changed(
    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 reauth flow fails on a different drive id."""

    await setup_integration(hass, mock_config_entry)

    mock_drive.id = "other_drive_id"

    result = await mock_config_entry.start_reauth_flow(hass)

    assert result["type"] is FlowResultType.FORM
    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"

Review comment (suggestion):
We should check our "other" configuration data is still in the entry at this point...

Reading through the code, I'm not sure, but I think it might have gotten lost.

Something like: 

```
assert mock_config_entry.data[CONF_TENANT_ID] == "mock-tenant-id"
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["token"]["access_token"] == "mock-access-token"
```

Code after:

@pytest.mark.usefixtures("current_request_with_host")
async def test_reauth_flow(
    hass: HomeAssistant,
    hass_client_no_auth: ClientSessionGenerator,
    aioclient_mock: AiohttpClientMocker,
    mock_setup_entry: AsyncMock,
    mock_config_entry: MockConfigEntry,
) -> None:
    """Test that the reauth flow works."""

    await setup_integration(hass, mock_config_entry)

    result = await mock_config_entry.start_reauth_flow(hass)

    assert result["type"] is FlowResultType.FORM
    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"
    )
    result = await hass.config_entries.flow.async_configure(result["flow_id"])

    assert result["type"] is FlowResultType.ABORT
    assert result["reason"] == "reauth_successful"
    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"
    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"


@pytest.mark.usefixtures("current_request_with_host")
async def test_reauth_flow_id_changed(
    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 reauth flow fails on a different drive id."""

    await setup_integration(hass, mock_config_entry)

    mock_drive.id = "other_drive_id"

    result = await mock_config_entry.start_reauth_flow(hass)

    assert result["type"] is FlowResultType.FORM