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

Code before:
    "username": "test-username",
    "password": "test-password",
}

MOCK_ENTRY_DATA = {
    "host": "https://fake.omada.host",
    "verify_ssl": True,
    "site": "SiteId",
    "username": "test-username",
    "password": "test-password",
}


@pytest.fixture
def mock_config_flow_client() -> Generator[MagicMock]:
    """Mock Omada client for config flow testing."""
    with patch(
        "homeassistant.components.tplink_omada.config_flow.create_omada_client",
        autospec=True,
    ) as client_mock:
        client = client_mock.return_value
        # Set default return values for the client methods
        client.login = AsyncMock(return_value="omada_id")
        client.get_controller_name = AsyncMock(return_value="OC200")
        client.get_sites = AsyncMock(return_value=[])
        yield client


@pytest.fixture
def mock_setup_entry() -> Generator[MagicMock]:
    """Mock async_setup_entry."""
    with patch(
        "homeassistant.components.tplink_omada.async_setup_entry",
        return_value=True,
    ) as mock_setup:
        yield mock_setup


async def test_form_single_site(
    hass: HomeAssistant,
    mock_config_flow_client: MagicMock,
    mock_setup_entry: MagicMock,
) -> None:
    """Test we get the form."""
    result = await hass.config_entries.flow.async_init(
        DOMAIN, context={"source": config_entries.SOURCE_USER}
    )
    assert result["type"] is FlowResultType.FORM
    assert result["errors"] == {}

    mock_config_flow_client.login.return_value = "omada_id"

Review comment (suggestion):
FYI
```py
@pytest.fixture
def mock_omada_client(mock_omada_site_client: AsyncMock) -> Generator[MagicMock]:
    """Mock Omada client."""
    with (patch(
        "homeassistant.components.tplink_omada.create_omada_client",
        autospec=True,
    ) as client_mock, patch(
        "homeassistant.components.tplink_omada.config_flow.create_omada_client",
        new=client_mock,
    )):
        client = client_mock.return_value

        client.get_site_client.return_value = mock_omada_site_client
        client.login = AsyncMock()
        yield client
```
You can merge the existing mock to also cover the config flow with this simple trick (formatting isn't on point)

Code after:

MOCK_ENTRY_DATA = {
    "host": "https://fake.omada.host",
    "verify_ssl": True,
    "site": "SiteId",
    "username": "test-username",
    "password": "test-password",
}


async def test_form_single_site(
    hass: HomeAssistant,
    mock_omada_client: MagicMock,
    mock_setup_entry: MagicMock,
) -> None:
    """Test we get the form."""
    result = await hass.config_entries.flow.async_init(
        DOMAIN, context={"source": SOURCE_USER}
    )
    assert result["type"] is FlowResultType.FORM
    assert result["errors"] == {}

    result = await hass.config_entries.flow.async_configure(
        result["flow_id"],
        MOCK_USER_DATA,
    )

    assert result["type"] is FlowResultType.CREATE_ENTRY
    assert result["title"] == "OC200 (Display Name)"
    assert result["data"] == MOCK_ENTRY_DATA
    assert result["result"].unique_id == "12345"
    assert len(mock_setup_entry.mock_calls) == 1


async def test_form_multiple_sites(
    hass: HomeAssistant,
    mock_omada_client: MagicMock,
    mock_setup_entry: MagicMock,
) -> None:
    """Test we get the form."""
    result = await hass.config_entries.flow.async_init(
        DOMAIN, context={"source": SOURCE_USER}
    )
    assert result["type"] is FlowResultType.FORM
    assert result["step_id"] == "user"
    assert result["errors"] == {}

    mock_omada_client.get_sites.return_value = [
        OmadaSite("Site 1", "first"),
        OmadaSite("Site 2", "second"),
    ]