1
0
mirror of https://github.com/home-assistant/core.git synced 2025-12-24 21:06:19 +00:00

Supplementary fixes to new motionEye integration (#49626)

This commit is contained in:
Dermot Duffy
2021-04-25 06:27:46 -07:00
committed by GitHub
parent 914451d99c
commit 3077363f44
5 changed files with 162 additions and 112 deletions

View File

@@ -12,7 +12,6 @@ from homeassistant import config_entries, data_entry_flow, setup
from homeassistant.components.motioneye.const import (
CONF_ADMIN_PASSWORD,
CONF_ADMIN_USERNAME,
CONF_CONFIG_ENTRY,
CONF_SURVEILLANCE_PASSWORD,
CONF_SURVEILLANCE_USERNAME,
DOMAIN,
@@ -22,6 +21,8 @@ from homeassistant.core import HomeAssistant
from . import TEST_URL, create_mock_motioneye_client, create_mock_motioneye_config_entry
from tests.common import MockConfigEntry
_LOGGER = logging.getLogger(__name__)
@@ -32,7 +33,7 @@ async def test_user_success(hass: HomeAssistant) -> None:
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
assert result["type"] == "form"
assert result["errors"] == {}
assert not result["errors"]
mock_client = create_mock_motioneye_client()
@@ -65,6 +66,7 @@ async def test_user_success(hass: HomeAssistant) -> None:
CONF_SURVEILLANCE_PASSWORD: "surveillance-password",
}
assert len(mock_setup_entry.mock_calls) == 1
assert mock_client.async_client_close.called
async def test_user_invalid_auth(hass: HomeAssistant) -> None:
@@ -92,10 +94,11 @@ async def test_user_invalid_auth(hass: HomeAssistant) -> None:
CONF_SURVEILLANCE_PASSWORD: "surveillance-password",
},
)
await mock_client.async_client_close()
await hass.async_block_till_done()
assert result["type"] == "form"
assert result["errors"] == {"base": "invalid_auth"}
assert mock_client.async_client_close.called
async def test_user_invalid_url(hass: HomeAssistant) -> None:
@@ -105,9 +108,10 @@ async def test_user_invalid_url(hass: HomeAssistant) -> None:
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
mock_client = create_mock_motioneye_client()
with patch(
"homeassistant.components.motioneye.MotionEyeClient",
return_value=create_mock_motioneye_client(),
return_value=mock_client,
):
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
@@ -119,6 +123,7 @@ async def test_user_invalid_url(hass: HomeAssistant) -> None:
CONF_SURVEILLANCE_PASSWORD: "surveillance-password",
},
)
await hass.async_block_till_done()
assert result["type"] == "form"
assert result["errors"] == {"base": "invalid_url"}
@@ -149,10 +154,11 @@ async def test_user_cannot_connect(hass: HomeAssistant) -> None:
CONF_SURVEILLANCE_PASSWORD: "surveillance-password",
},
)
await mock_client.async_client_close()
await hass.async_block_till_done()
assert result["type"] == "form"
assert result["errors"] == {"base": "cannot_connect"}
assert mock_client.async_client_close.called
async def test_user_request_error(hass: HomeAssistant) -> None:
@@ -178,10 +184,11 @@ async def test_user_request_error(hass: HomeAssistant) -> None:
CONF_SURVEILLANCE_PASSWORD: "surveillance-password",
},
)
await mock_client.async_client_close()
await hass.async_block_till_done()
assert result["type"] == "form"
assert result["errors"] == {"base": "unknown"}
assert mock_client.async_client_close.called
async def test_reauth(hass: HomeAssistant) -> None:
@@ -197,11 +204,11 @@ async def test_reauth(hass: HomeAssistant) -> None:
DOMAIN,
context={
"source": config_entries.SOURCE_REAUTH,
CONF_CONFIG_ENTRY: config_entry,
"entry_id": config_entry.entry_id,
},
)
assert result["type"] == "form"
assert result["errors"] == {}
assert not result["errors"]
mock_client = create_mock_motioneye_client()
@@ -226,8 +233,57 @@ async def test_reauth(hass: HomeAssistant) -> None:
)
await hass.async_block_till_done()
assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT
assert result["reason"] == "reauth_successful"
assert config_entry.data == new_data
assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT
assert result["reason"] == "reauth_successful"
assert config_entry.data == new_data
assert len(mock_setup_entry.mock_calls) == 1
assert mock_client.async_client_close.called
async def test_duplicate(hass: HomeAssistant) -> None:
"""Test that a duplicate entry (same URL) is rejected."""
config_data = {
CONF_URL: TEST_URL,
}
# Add an existing entry with the same URL.
existing_entry: MockConfigEntry = MockConfigEntry( # type: ignore[no-untyped-call]
domain=DOMAIN,
data=config_data,
)
existing_entry.add_to_hass(hass) # type: ignore[no-untyped-call]
# Now do the usual config entry process, and verify it is rejected.
create_mock_motioneye_config_entry(hass, data=config_data)
await setup.async_setup_component(hass, "persistent_notification", {})
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
assert result["type"] == "form"
assert not result["errors"]
mock_client = create_mock_motioneye_client()
new_data = {
CONF_URL: TEST_URL,
CONF_ADMIN_USERNAME: "admin-username",
CONF_ADMIN_PASSWORD: "admin-password",
CONF_SURVEILLANCE_USERNAME: "surveillance-username",
CONF_SURVEILLANCE_PASSWORD: "surveillance-password",
}
with patch(
"homeassistant.components.motioneye.MotionEyeClient",
return_value=mock_client,
):
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
new_data,
)
await hass.async_block_till_done()
assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT
assert result["reason"] == "already_configured"
assert mock_client.async_client_close.called