mirror of
https://github.com/home-assistant/core.git
synced 2025-07-31 19:25:12 +02:00
strengthen config_flow test
This commit is contained in:
@@ -5,6 +5,26 @@ from unittest.mock import AsyncMock, patch
|
||||
|
||||
import pytest
|
||||
|
||||
from homeassistant.components.playstation_network.const import CONF_NPSSO, DOMAIN
|
||||
|
||||
from tests.common import MockConfigEntry
|
||||
|
||||
NPSSO_TOKEN: str = "npsso-token"
|
||||
PSN_ID: str = "my-psn-id"
|
||||
|
||||
|
||||
@pytest.fixture(name="config_entry")
|
||||
def mock_config_entry() -> MockConfigEntry:
|
||||
"""Mock PlayStation Network configuration entry."""
|
||||
return MockConfigEntry(
|
||||
domain=DOMAIN,
|
||||
title="test-user",
|
||||
data={
|
||||
CONF_NPSSO: NPSSO_TOKEN,
|
||||
},
|
||||
unique_id=PSN_ID,
|
||||
)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_setup_entry() -> Generator[AsyncMock]:
|
||||
|
@@ -10,14 +10,21 @@ from homeassistant.components.playstation_network.config_flow import (
|
||||
PSNAWPNotFound,
|
||||
)
|
||||
from homeassistant.components.playstation_network.const import CONF_NPSSO, DOMAIN
|
||||
from homeassistant.config_entries import SOURCE_USER
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.data_entry_flow import FlowResultType
|
||||
|
||||
from .conftest import NPSSO_TOKEN, PSN_ID
|
||||
|
||||
from tests.common import MockConfigEntry
|
||||
|
||||
MOCK_DATA_ADVANCED_STEP = {CONF_NPSSO: NPSSO_TOKEN}
|
||||
|
||||
|
||||
class mockUser:
|
||||
"""Mock User class."""
|
||||
|
||||
account_id = "1234"
|
||||
account_id = PSN_ID
|
||||
online_id = "testuser"
|
||||
|
||||
|
||||
@@ -52,6 +59,35 @@ async def test_form_success(hass: HomeAssistant, npsso) -> None:
|
||||
}
|
||||
|
||||
|
||||
async def test_form_already_configured(
|
||||
hass: HomeAssistant,
|
||||
config_entry: MockConfigEntry,
|
||||
) -> None:
|
||||
"""Test we abort form login when entry is already configured."""
|
||||
|
||||
config_entry.add_to_hass(hass)
|
||||
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": SOURCE_USER}
|
||||
)
|
||||
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["step_id"] == "user"
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.playstation_network.config_flow.PlaystationNetwork.get_user",
|
||||
return_value=mockUser(),
|
||||
):
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
{CONF_NPSSO: NPSSO_TOKEN},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert result["type"] is FlowResultType.ABORT
|
||||
assert result["reason"] == "already_configured"
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("raise_error", "text_error"),
|
||||
[
|
||||
@@ -61,7 +97,10 @@ async def test_form_success(hass: HomeAssistant, npsso) -> None:
|
||||
],
|
||||
)
|
||||
async def test_form_failures(hass: HomeAssistant, raise_error, text_error) -> None:
|
||||
"""Test we handle a connection error."""
|
||||
"""Test we handle a connection error.
|
||||
|
||||
First we generate an error and after fixing it, we are still able to submit.
|
||||
"""
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
||||
)
|
||||
@@ -75,8 +114,29 @@ async def test_form_failures(hass: HomeAssistant, raise_error, text_error) -> No
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN,
|
||||
context={"source": config_entries.SOURCE_USER},
|
||||
data={CONF_NPSSO: "TEST_NPSSO_TOKEN"},
|
||||
data={CONF_NPSSO: NPSSO_TOKEN},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert result["errors"] == {"base": text_error}
|
||||
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
||||
)
|
||||
assert result["type"] == FlowResultType.FORM
|
||||
assert result["errors"] == {}
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.playstation_network.config_flow.PlaystationNetwork.get_user",
|
||||
return_value=mockUser(),
|
||||
):
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
{CONF_NPSSO: NPSSO_TOKEN},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert result["type"] == FlowResultType.CREATE_ENTRY
|
||||
assert result["data"] == {
|
||||
CONF_NPSSO: NPSSO_TOKEN,
|
||||
}
|
||||
|
Reference in New Issue
Block a user