Skip to content

Commit

Permalink
Add blink tests (#36672)
Browse files Browse the repository at this point in the history
Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
  • Loading branch information
fronzbot and MartinHjelmare authored Jun 16, 2020
1 parent f8de059 commit a68af0a
Show file tree
Hide file tree
Showing 4 changed files with 214 additions and 1 deletion.
7 changes: 6 additions & 1 deletion .coveragerc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions requirements_test_all.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions tests/components/blink/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""Tests for the Blink component."""
204 changes: 204 additions & 0 deletions tests/components/blink/test_config_flow.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit a68af0a

Please sign in to comment.