mirror of
https://github.com/home-assistant/core.git
synced 2025-07-22 12:47:08 +00:00
Improve Minecraft Server config flow tests (#139251)
This commit is contained in:
parent
d45fce86a9
commit
664e09790c
@ -7,12 +7,7 @@ rules:
|
||||
brands: done
|
||||
common-modules: done
|
||||
config-flow: done
|
||||
config-flow-test-coverage:
|
||||
status: todo
|
||||
comment: |
|
||||
Merge test_show_config_form with full flow test.
|
||||
Move full flow test to the top of all tests.
|
||||
All test cases should end in either CREATE_ENTRY or ABORT.
|
||||
config-flow-test-coverage: done
|
||||
dependency-transparency: done
|
||||
docs-actions:
|
||||
status: exempt
|
||||
|
@ -26,8 +26,8 @@ USER_INPUT = {
|
||||
}
|
||||
|
||||
|
||||
async def test_show_config_form(hass: HomeAssistant) -> None:
|
||||
"""Test if initial configuration form is shown."""
|
||||
async def test_full_flow_java(hass: HomeAssistant) -> None:
|
||||
"""Test config entry in case of a successful connection to a Java Edition server."""
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": SOURCE_USER}
|
||||
)
|
||||
@ -35,96 +35,6 @@ async def test_show_config_form(hass: HomeAssistant) -> None:
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["step_id"] == "user"
|
||||
|
||||
|
||||
async def test_service_already_configured(
|
||||
hass: HomeAssistant, bedrock_mock_config_entry: MockConfigEntry
|
||||
) -> None:
|
||||
"""Test config flow abort if service is already configured."""
|
||||
bedrock_mock_config_entry.add_to_hass(hass)
|
||||
|
||||
with (
|
||||
patch(
|
||||
"homeassistant.components.minecraft_server.api.BedrockServer.lookup",
|
||||
return_value=BedrockServer(host=TEST_HOST, port=TEST_PORT),
|
||||
),
|
||||
patch(
|
||||
"homeassistant.components.minecraft_server.api.BedrockServer.async_status",
|
||||
return_value=TEST_BEDROCK_STATUS_RESPONSE,
|
||||
),
|
||||
):
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": SOURCE_USER}, data=USER_INPUT
|
||||
)
|
||||
assert result["type"] is FlowResultType.ABORT
|
||||
assert result["reason"] == "already_configured"
|
||||
|
||||
|
||||
async def test_address_validation_failure(hass: HomeAssistant) -> None:
|
||||
"""Test error in case of a failed connection."""
|
||||
with (
|
||||
patch(
|
||||
"homeassistant.components.minecraft_server.api.BedrockServer.lookup",
|
||||
side_effect=ValueError,
|
||||
),
|
||||
patch(
|
||||
"homeassistant.components.minecraft_server.api.JavaServer.async_lookup",
|
||||
side_effect=ValueError,
|
||||
),
|
||||
):
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": SOURCE_USER}, data=USER_INPUT
|
||||
)
|
||||
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["errors"] == {"base": "cannot_connect"}
|
||||
|
||||
|
||||
async def test_java_connection_failure(hass: HomeAssistant) -> None:
|
||||
"""Test error in case of a failed connection to a Java Edition server."""
|
||||
with (
|
||||
patch(
|
||||
"homeassistant.components.minecraft_server.api.BedrockServer.lookup",
|
||||
side_effect=ValueError,
|
||||
),
|
||||
patch(
|
||||
"homeassistant.components.minecraft_server.api.JavaServer.async_lookup",
|
||||
return_value=JavaServer(host=TEST_HOST, port=TEST_PORT),
|
||||
),
|
||||
patch(
|
||||
"homeassistant.components.minecraft_server.api.JavaServer.async_status",
|
||||
side_effect=OSError,
|
||||
),
|
||||
):
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": SOURCE_USER}, data=USER_INPUT
|
||||
)
|
||||
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["errors"] == {"base": "cannot_connect"}
|
||||
|
||||
|
||||
async def test_bedrock_connection_failure(hass: HomeAssistant) -> None:
|
||||
"""Test error in case of a failed connection to a Bedrock Edition server."""
|
||||
with (
|
||||
patch(
|
||||
"homeassistant.components.minecraft_server.api.BedrockServer.lookup",
|
||||
return_value=BedrockServer(host=TEST_HOST, port=TEST_PORT),
|
||||
),
|
||||
patch(
|
||||
"homeassistant.components.minecraft_server.api.BedrockServer.async_status",
|
||||
side_effect=OSError,
|
||||
),
|
||||
):
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": SOURCE_USER}, data=USER_INPUT
|
||||
)
|
||||
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["errors"] == {"base": "cannot_connect"}
|
||||
|
||||
|
||||
async def test_java_connection(hass: HomeAssistant) -> None:
|
||||
"""Test config entry in case of a successful connection to a Java Edition server."""
|
||||
with (
|
||||
patch(
|
||||
"homeassistant.components.minecraft_server.api.BedrockServer.lookup",
|
||||
@ -149,8 +59,15 @@ async def test_java_connection(hass: HomeAssistant) -> None:
|
||||
assert result["data"][CONF_TYPE] == MinecraftServerType.JAVA_EDITION
|
||||
|
||||
|
||||
async def test_bedrock_connection(hass: HomeAssistant) -> None:
|
||||
async def test_full_flow_bedrock(hass: HomeAssistant) -> None:
|
||||
"""Test config entry in case of a successful connection to a Bedrock Edition server."""
|
||||
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.minecraft_server.api.BedrockServer.lookup",
|
||||
@ -171,8 +88,12 @@ async def test_bedrock_connection(hass: HomeAssistant) -> None:
|
||||
assert result["data"][CONF_TYPE] == MinecraftServerType.BEDROCK_EDITION
|
||||
|
||||
|
||||
async def test_recovery(hass: HomeAssistant) -> None:
|
||||
"""Test config flow recovery (successful connection after a failed connection)."""
|
||||
async def test_service_already_configured_java(
|
||||
hass: HomeAssistant, java_mock_config_entry: MockConfigEntry
|
||||
) -> None:
|
||||
"""Test config flow abort if a Java Edition server is already configured."""
|
||||
java_mock_config_entry.add_to_hass(hass)
|
||||
|
||||
with (
|
||||
patch(
|
||||
"homeassistant.components.minecraft_server.api.BedrockServer.lookup",
|
||||
@ -180,8 +101,99 @@ async def test_recovery(hass: HomeAssistant) -> None:
|
||||
),
|
||||
patch(
|
||||
"homeassistant.components.minecraft_server.api.JavaServer.async_lookup",
|
||||
return_value=JavaServer(host=TEST_HOST, port=TEST_PORT),
|
||||
),
|
||||
patch(
|
||||
"homeassistant.components.minecraft_server.api.JavaServer.async_status",
|
||||
return_value=TEST_JAVA_STATUS_RESPONSE,
|
||||
),
|
||||
):
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": SOURCE_USER}, data=USER_INPUT
|
||||
)
|
||||
assert result["type"] is FlowResultType.ABORT
|
||||
assert result["reason"] == "already_configured"
|
||||
|
||||
|
||||
async def test_service_already_configured_bedrock(
|
||||
hass: HomeAssistant, bedrock_mock_config_entry: MockConfigEntry
|
||||
) -> None:
|
||||
"""Test config flow abort if a Bedrock Edition server is already configured."""
|
||||
bedrock_mock_config_entry.add_to_hass(hass)
|
||||
|
||||
with (
|
||||
patch(
|
||||
"homeassistant.components.minecraft_server.api.BedrockServer.lookup",
|
||||
return_value=BedrockServer(host=TEST_HOST, port=TEST_PORT),
|
||||
),
|
||||
patch(
|
||||
"homeassistant.components.minecraft_server.api.BedrockServer.async_status",
|
||||
return_value=TEST_BEDROCK_STATUS_RESPONSE,
|
||||
),
|
||||
):
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": SOURCE_USER}, data=USER_INPUT
|
||||
)
|
||||
assert result["type"] is FlowResultType.ABORT
|
||||
assert result["reason"] == "already_configured"
|
||||
|
||||
|
||||
async def test_recovery_java(hass: HomeAssistant) -> None:
|
||||
"""Test config flow recovery with a Java Edition server (successful connection after a failed connection)."""
|
||||
with (
|
||||
patch(
|
||||
"homeassistant.components.minecraft_server.api.BedrockServer.lookup",
|
||||
side_effect=ValueError,
|
||||
),
|
||||
patch(
|
||||
"homeassistant.components.minecraft_server.api.JavaServer.async_lookup",
|
||||
return_value=JavaServer(host=TEST_HOST, port=TEST_PORT),
|
||||
),
|
||||
patch(
|
||||
"homeassistant.components.minecraft_server.api.JavaServer.async_status",
|
||||
side_effect=OSError,
|
||||
),
|
||||
):
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": SOURCE_USER}, data=USER_INPUT
|
||||
)
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["errors"] == {"base": "cannot_connect"}
|
||||
|
||||
with (
|
||||
patch(
|
||||
"homeassistant.components.minecraft_server.api.BedrockServer.lookup",
|
||||
side_effect=ValueError,
|
||||
),
|
||||
patch(
|
||||
"homeassistant.components.minecraft_server.api.JavaServer.async_lookup",
|
||||
return_value=JavaServer(host=TEST_HOST, port=TEST_PORT),
|
||||
),
|
||||
patch(
|
||||
"homeassistant.components.minecraft_server.api.JavaServer.async_status",
|
||||
return_value=TEST_JAVA_STATUS_RESPONSE,
|
||||
),
|
||||
):
|
||||
result2 = await hass.config_entries.flow.async_configure(
|
||||
flow_id=result["flow_id"], user_input=USER_INPUT
|
||||
)
|
||||
assert result2["type"] is FlowResultType.CREATE_ENTRY
|
||||
assert result2["title"] == USER_INPUT[CONF_ADDRESS]
|
||||
assert result2["data"][CONF_ADDRESS] == TEST_ADDRESS
|
||||
assert result2["data"][CONF_TYPE] == MinecraftServerType.JAVA_EDITION
|
||||
|
||||
|
||||
async def test_recovery_bedrock(hass: HomeAssistant) -> None:
|
||||
"""Test config flow recovery with a Bedrock Edition server (successful connection after a failed connection)."""
|
||||
with (
|
||||
patch(
|
||||
"homeassistant.components.minecraft_server.api.BedrockServer.lookup",
|
||||
return_value=BedrockServer(host=TEST_HOST, port=TEST_PORT),
|
||||
),
|
||||
patch(
|
||||
"homeassistant.components.minecraft_server.api.BedrockServer.async_status",
|
||||
side_effect=OSError,
|
||||
),
|
||||
):
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": SOURCE_USER}, data=USER_INPUT
|
||||
|
Loading…
x
Reference in New Issue
Block a user