mirror of
https://github.com/home-assistant/core.git
synced 2025-04-23 08:47:57 +00:00
Add blink tests (#36672)
Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
This commit is contained in:
parent
f8de0594b9
commit
a68af0a3a9
@ -80,7 +80,12 @@ omit =
|
||||
homeassistant/components/bh1750/sensor.py
|
||||
homeassistant/components/bitcoin/sensor.py
|
||||
homeassistant/components/bizkaibus/sensor.py
|
||||
homeassistant/components/blink/*
|
||||
homeassistant/components/blink/__init__.py
|
||||
homeassistant/components/blink/alarm_control_panel.py
|
||||
homeassistant/components/blink/binary_sensor.py
|
||||
homeassistant/components/blink/camera.py
|
||||
homeassistant/components/blink/const.py
|
||||
homeassistant/components/blink/sensor.py
|
||||
homeassistant/components/blinksticklight/light.py
|
||||
homeassistant/components/blinkt/light.py
|
||||
homeassistant/components/blockchain/sensor.py
|
||||
|
@ -161,6 +161,9 @@ bellows==0.17.0
|
||||
# homeassistant.components.blebox
|
||||
blebox_uniapi==1.3.2
|
||||
|
||||
# homeassistant.components.blink
|
||||
blinkpy==0.15.0
|
||||
|
||||
# homeassistant.components.bom
|
||||
bomradarloop==0.1.4
|
||||
|
||||
|
1
tests/components/blink/__init__.py
Normal file
1
tests/components/blink/__init__.py
Normal file
@ -0,0 +1 @@
|
||||
"""Tests for the Blink component."""
|
204
tests/components/blink/test_config_flow.py
Normal file
204
tests/components/blink/test_config_flow.py
Normal file
@ -0,0 +1,204 @@
|
||||
"""Test the Blink config flow."""
|
||||
from homeassistant import config_entries, data_entry_flow, setup
|
||||
from homeassistant.components.blink import DOMAIN
|
||||
|
||||
from tests.async_mock import Mock, patch
|
||||
from tests.common import MockConfigEntry
|
||||
|
||||
|
||||
async def test_form(hass):
|
||||
"""Test we get the form."""
|
||||
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 result["errors"] == {}
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.blink.config_flow.Blink",
|
||||
return_value=Mock(
|
||||
get_auth_token=Mock(return_value=True),
|
||||
key_required=False,
|
||||
login_response={},
|
||||
),
|
||||
), patch(
|
||||
"homeassistant.components.blink.async_setup", return_value=True
|
||||
) as mock_setup, patch(
|
||||
"homeassistant.components.blink.async_setup_entry", return_value=True,
|
||||
) as mock_setup_entry:
|
||||
result2 = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"], {"username": "blink@example.com", "password": "example"},
|
||||
)
|
||||
|
||||
assert result2["type"] == "create_entry"
|
||||
assert result2["title"] == "blink"
|
||||
assert result2["result"].unique_id == "blink@example.com"
|
||||
assert result2["data"] == {
|
||||
"username": "blink@example.com",
|
||||
"password": "example",
|
||||
"login_response": {},
|
||||
}
|
||||
await hass.async_block_till_done()
|
||||
assert len(mock_setup.mock_calls) == 1
|
||||
assert len(mock_setup_entry.mock_calls) == 1
|
||||
|
||||
|
||||
async def test_import(hass):
|
||||
"""Test we import the config."""
|
||||
with patch(
|
||||
"homeassistant.components.blink.config_flow.Blink",
|
||||
return_value=Mock(
|
||||
get_auth_token=Mock(return_value=True),
|
||||
key_required=False,
|
||||
login_response={},
|
||||
),
|
||||
), patch(
|
||||
"homeassistant.components.blink.async_setup_entry", return_value=True,
|
||||
) as mock_setup_entry:
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN,
|
||||
context={"source": config_entries.SOURCE_IMPORT},
|
||||
data={
|
||||
"username": "blink@example.com",
|
||||
"password": "example",
|
||||
"scan_interval": 10,
|
||||
},
|
||||
)
|
||||
|
||||
assert result["type"] == "create_entry"
|
||||
assert result["title"] == "blink"
|
||||
assert result["result"].unique_id == "blink@example.com"
|
||||
assert result["data"] == {
|
||||
"username": "blink@example.com",
|
||||
"password": "example",
|
||||
"scan_interval": 10,
|
||||
"login_response": {},
|
||||
}
|
||||
await hass.async_block_till_done()
|
||||
assert len(mock_setup_entry.mock_calls) == 1
|
||||
|
||||
|
||||
async def test_form_2fa(hass):
|
||||
"""Test we get the 2fa form."""
|
||||
await setup.async_setup_component(hass, "persistent_notification", {})
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
||||
)
|
||||
|
||||
mock_blink = Mock(
|
||||
get_auth_token=Mock(return_value=True),
|
||||
key_required=True,
|
||||
login_response={},
|
||||
login_handler=Mock(send_auth_key=Mock(return_value=True)),
|
||||
)
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.blink.config_flow.Blink", return_value=mock_blink
|
||||
), patch(
|
||||
"homeassistant.components.blink.async_setup", return_value=True
|
||||
) as mock_setup:
|
||||
result2 = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"], {"username": "blink@example.com", "password": "example"}
|
||||
)
|
||||
|
||||
assert result2["type"] == "form"
|
||||
assert result2["step_id"] == "2fa"
|
||||
|
||||
mock_blink.key_required = False
|
||||
with patch(
|
||||
"homeassistant.components.blink.config_flow.Blink", return_value=mock_blink
|
||||
), patch(
|
||||
"homeassistant.components.blink.async_setup", return_value=True
|
||||
) as mock_setup, patch(
|
||||
"homeassistant.components.blink.async_setup_entry", return_value=True
|
||||
) as mock_setup_entry:
|
||||
result3 = await hass.config_entries.flow.async_configure(
|
||||
result2["flow_id"], {"pin": "1234"}
|
||||
)
|
||||
|
||||
assert result3["type"] == "create_entry"
|
||||
assert result3["title"] == "blink"
|
||||
assert result3["result"].unique_id == "blink@example.com"
|
||||
await hass.async_block_till_done()
|
||||
assert len(mock_setup.mock_calls) == 1
|
||||
assert len(mock_setup_entry.mock_calls) == 1
|
||||
|
||||
|
||||
async def test_form_invalid_auth(hass):
|
||||
"""Test we handle invalid auth."""
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
||||
)
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.blink.config_flow.Blink.get_auth_token",
|
||||
return_value=None,
|
||||
):
|
||||
result2 = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"], {"username": "blink@example.com", "password": "example"}
|
||||
)
|
||||
|
||||
assert result2["type"] == "form"
|
||||
assert result2["errors"] == {"base": "invalid_auth"}
|
||||
|
||||
|
||||
async def test_form_unknown_error(hass):
|
||||
"""Test we handle unknown error at startup."""
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
||||
)
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.blink.config_flow.Blink.get_auth_token",
|
||||
return_value=None,
|
||||
), patch(
|
||||
"homeassistant.components.blink.config_flow.validate_input",
|
||||
side_effect=KeyError,
|
||||
):
|
||||
result2 = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"], {"username": "blink@example.com", "password": "example"}
|
||||
)
|
||||
|
||||
assert result2["type"] == "form"
|
||||
assert result2["errors"] == {"base": "unknown"}
|
||||
|
||||
|
||||
async def test_options_flow(hass):
|
||||
"""Test config flow options."""
|
||||
config_entry = MockConfigEntry(
|
||||
domain=DOMAIN,
|
||||
data={
|
||||
"username": "blink@example.com",
|
||||
"password": "example",
|
||||
"login_response": {},
|
||||
},
|
||||
options={},
|
||||
entry_id=1,
|
||||
)
|
||||
config_entry.add_to_hass(hass)
|
||||
|
||||
mock_blink = Mock(
|
||||
login_handler=True,
|
||||
setup_params=Mock(return_value=True),
|
||||
setup_post_verify=Mock(return_value=True),
|
||||
)
|
||||
|
||||
with patch("homeassistant.components.blink.Blink", return_value=mock_blink):
|
||||
await hass.config_entries.async_setup(config_entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
result = await hass.config_entries.options.async_init(
|
||||
config_entry.entry_id, context={"show_advanced_options": False}
|
||||
)
|
||||
|
||||
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
|
||||
assert result["step_id"] == "simple_options"
|
||||
|
||||
result = await hass.config_entries.options.async_configure(
|
||||
result["flow_id"], user_input={"scan_interval": 5},
|
||||
)
|
||||
|
||||
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
|
||||
assert result["data"] == {"scan_interval": 5}
|
||||
assert mock_blink.refresh_rate == 5
|
Loading…
x
Reference in New Issue
Block a user