mirror of
https://github.com/home-assistant/core.git
synced 2026-09-08 02:35:02 -05:00
Bump ring_doorbell to 0.8.0 and handle new exceptions (#103904)
* Bump ring_doorbell to 0.8.0 and handle the new exceptions * Modify data update tests to not call coordinator internals
This commit is contained in:
@@ -1,13 +1,74 @@
|
||||
"""Configuration for Ring tests."""
|
||||
from collections.abc import Generator
|
||||
import re
|
||||
from unittest.mock import AsyncMock, Mock, patch
|
||||
|
||||
import pytest
|
||||
import requests_mock
|
||||
|
||||
from tests.common import load_fixture
|
||||
from homeassistant.components.ring import DOMAIN
|
||||
from homeassistant.const import CONF_USERNAME
|
||||
from homeassistant.core import HomeAssistant
|
||||
|
||||
from tests.common import MockConfigEntry, load_fixture
|
||||
from tests.components.light.conftest import mock_light_profiles # noqa: F401
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_setup_entry() -> Generator[AsyncMock, None, None]:
|
||||
"""Override async_setup_entry."""
|
||||
with patch(
|
||||
"homeassistant.components.ring.async_setup_entry", return_value=True
|
||||
) as mock_setup_entry:
|
||||
yield mock_setup_entry
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_ring_auth():
|
||||
"""Mock ring_doorbell.Auth."""
|
||||
with patch("ring_doorbell.Auth", autospec=True) as mock_ring_auth:
|
||||
mock_ring_auth.return_value.fetch_token.return_value = {
|
||||
"access_token": "mock-token"
|
||||
}
|
||||
yield mock_ring_auth.return_value
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_ring():
|
||||
"""Mock ring_doorbell.Ring."""
|
||||
with patch("ring_doorbell.Ring", autospec=True) as mock_ring:
|
||||
yield mock_ring.return_value
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_config_entry() -> MockConfigEntry:
|
||||
"""Mock ConfigEntry."""
|
||||
return MockConfigEntry(
|
||||
title="Ring",
|
||||
domain=DOMAIN,
|
||||
data={
|
||||
CONF_USERNAME: "foo@bar.com",
|
||||
"token": {"access_token": "mock-token"},
|
||||
},
|
||||
unique_id="foo@bar.com",
|
||||
)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
async def mock_added_config_entry(
|
||||
hass: HomeAssistant,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
mock_ring_auth: Mock,
|
||||
mock_ring: Mock,
|
||||
) -> MockConfigEntry:
|
||||
"""Mock ConfigEntry that's been added to HA."""
|
||||
mock_config_entry.add_to_hass(hass)
|
||||
await hass.config_entries.async_setup(mock_config_entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
assert DOMAIN in hass.config_entries.async_domains()
|
||||
return mock_config_entry
|
||||
|
||||
|
||||
@pytest.fixture(name="requests_mock")
|
||||
def requests_mock_fixture():
|
||||
"""Fixture to provide a requests mocker."""
|
||||
@@ -52,5 +113,11 @@ def requests_mock_fixture():
|
||||
re.compile(r"https:\/\/api\.ring\.com\/clients_api\/chimes\/\d+\/health"),
|
||||
text=load_fixture("chime_health_attrs.json", "ring"),
|
||||
)
|
||||
|
||||
mock.get(
|
||||
re.compile(
|
||||
r"https:\/\/api\.ring\.com\/clients_api\/dings\/\d+\/share/play"
|
||||
),
|
||||
status_code=200,
|
||||
json={"url": "http://127.0.0.1/foo"},
|
||||
)
|
||||
yield mock
|
||||
|
||||
@@ -1,13 +1,21 @@
|
||||
"""Test the Ring config flow."""
|
||||
from unittest.mock import Mock, patch
|
||||
from unittest.mock import AsyncMock, Mock
|
||||
|
||||
import pytest
|
||||
import ring_doorbell
|
||||
|
||||
from homeassistant import config_entries
|
||||
from homeassistant.components.ring import DOMAIN
|
||||
from homeassistant.components.ring.config_flow import InvalidAuth
|
||||
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.data_entry_flow import FlowResultType
|
||||
|
||||
|
||||
async def test_form(hass: HomeAssistant) -> None:
|
||||
async def test_form(
|
||||
hass: HomeAssistant,
|
||||
mock_setup_entry: AsyncMock,
|
||||
mock_ring_auth: Mock,
|
||||
) -> None:
|
||||
"""Test we get the form."""
|
||||
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
@@ -16,20 +24,11 @@ async def test_form(hass: HomeAssistant) -> None:
|
||||
assert result["type"] == "form"
|
||||
assert result["errors"] == {}
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.ring.config_flow.Auth",
|
||||
return_value=Mock(
|
||||
fetch_token=Mock(return_value={"access_token": "mock-token"})
|
||||
),
|
||||
), patch(
|
||||
"homeassistant.components.ring.async_setup_entry",
|
||||
return_value=True,
|
||||
) as mock_setup_entry:
|
||||
result2 = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
{"username": "hello@home-assistant.io", "password": "test-password"},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
result2 = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
{"username": "hello@home-assistant.io", "password": "test-password"},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert result2["type"] == "create_entry"
|
||||
assert result2["title"] == "hello@home-assistant.io"
|
||||
@@ -40,20 +39,72 @@ async def test_form(hass: HomeAssistant) -> None:
|
||||
assert len(mock_setup_entry.mock_calls) == 1
|
||||
|
||||
|
||||
async def test_form_invalid_auth(hass: HomeAssistant) -> None:
|
||||
@pytest.mark.parametrize(
|
||||
("error_type", "errors_msg"),
|
||||
[
|
||||
(ring_doorbell.AuthenticationError, "invalid_auth"),
|
||||
(Exception, "unknown"),
|
||||
],
|
||||
ids=["invalid-auth", "unknown-error"],
|
||||
)
|
||||
async def test_form_error(
|
||||
hass: HomeAssistant, mock_ring_auth: Mock, error_type, errors_msg
|
||||
) -> None:
|
||||
"""Test we handle invalid auth."""
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
||||
)
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.ring.config_flow.Auth.fetch_token",
|
||||
side_effect=InvalidAuth,
|
||||
):
|
||||
result2 = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
{"username": "hello@home-assistant.io", "password": "test-password"},
|
||||
)
|
||||
mock_ring_auth.fetch_token.side_effect = error_type
|
||||
result2 = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
{"username": "hello@home-assistant.io", "password": "test-password"},
|
||||
)
|
||||
|
||||
assert result2["type"] == "form"
|
||||
assert result2["errors"] == {"base": "invalid_auth"}
|
||||
assert result2["errors"] == {"base": errors_msg}
|
||||
|
||||
|
||||
async def test_form_2fa(
|
||||
hass: HomeAssistant,
|
||||
mock_setup_entry: AsyncMock,
|
||||
mock_ring_auth: Mock,
|
||||
) -> None:
|
||||
"""Test form flow for 2fa."""
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
||||
)
|
||||
assert result["type"] == FlowResultType.FORM
|
||||
assert result["errors"] == {}
|
||||
|
||||
mock_ring_auth.fetch_token.side_effect = ring_doorbell.Requires2FAError
|
||||
result2 = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
{
|
||||
CONF_USERNAME: "foo@bar.com",
|
||||
CONF_PASSWORD: "fake-password",
|
||||
},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
mock_ring_auth.fetch_token.assert_called_once_with(
|
||||
"foo@bar.com", "fake-password", None
|
||||
)
|
||||
|
||||
assert result2["type"] == FlowResultType.FORM
|
||||
assert result2["step_id"] == "2fa"
|
||||
mock_ring_auth.fetch_token.reset_mock(side_effect=True)
|
||||
mock_ring_auth.fetch_token.return_value = "new-foobar"
|
||||
result3 = await hass.config_entries.flow.async_configure(
|
||||
result2["flow_id"],
|
||||
user_input={"2fa": "123456"},
|
||||
)
|
||||
|
||||
mock_ring_auth.fetch_token.assert_called_once_with(
|
||||
"foo@bar.com", "fake-password", "123456"
|
||||
)
|
||||
assert result3["type"] == FlowResultType.CREATE_ENTRY
|
||||
assert result3["title"] == "foo@bar.com"
|
||||
assert result3["data"] == {
|
||||
"username": "foo@bar.com",
|
||||
"token": "new-foobar",
|
||||
}
|
||||
assert len(mock_setup_entry.mock_calls) == 1
|
||||
|
||||
@@ -1,12 +1,20 @@
|
||||
"""The tests for the Ring component."""
|
||||
|
||||
from datetime import timedelta
|
||||
from unittest.mock import patch
|
||||
|
||||
import pytest
|
||||
import requests_mock
|
||||
from ring_doorbell import AuthenticationError, RingError, RingTimeout
|
||||
|
||||
import homeassistant.components.ring as ring
|
||||
from homeassistant.components.ring import DOMAIN
|
||||
from homeassistant.config_entries import ConfigEntryState
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.setup import async_setup_component
|
||||
from homeassistant.util import dt as dt_util
|
||||
|
||||
from tests.common import load_fixture
|
||||
from tests.common import MockConfigEntry, async_fire_time_changed, load_fixture
|
||||
|
||||
|
||||
async def test_setup(hass: HomeAssistant, requests_mock: requests_mock.Mocker) -> None:
|
||||
@@ -32,3 +40,157 @@ async def test_setup(hass: HomeAssistant, requests_mock: requests_mock.Mocker) -
|
||||
"https://api.ring.com/clients_api/doorbots/987652/health",
|
||||
text=load_fixture("doorboot_health_attrs.json", "ring"),
|
||||
)
|
||||
|
||||
|
||||
async def test_auth_failed_on_setup(
|
||||
hass: HomeAssistant,
|
||||
requests_mock: requests_mock.Mocker,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
caplog,
|
||||
) -> None:
|
||||
"""Test auth failure on setup entry."""
|
||||
mock_config_entry.add_to_hass(hass)
|
||||
with patch(
|
||||
"ring_doorbell.Ring.update_data",
|
||||
side_effect=AuthenticationError,
|
||||
):
|
||||
result = await hass.config_entries.async_setup(mock_config_entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
assert result is False
|
||||
assert "Access token is no longer valid. Please set up Ring again" in [
|
||||
record.message for record in caplog.records if record.levelname == "ERROR"
|
||||
]
|
||||
|
||||
assert DOMAIN not in hass.data
|
||||
assert mock_config_entry.state is ConfigEntryState.SETUP_ERROR
|
||||
|
||||
|
||||
async def test_auth_failure_on_global_update(
|
||||
hass: HomeAssistant,
|
||||
requests_mock: requests_mock.Mocker,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
caplog,
|
||||
) -> None:
|
||||
"""Test authentication failure on global data update."""
|
||||
mock_config_entry.add_to_hass(hass)
|
||||
await hass.config_entries.async_setup(mock_config_entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
with patch(
|
||||
"ring_doorbell.Ring.update_devices",
|
||||
side_effect=AuthenticationError,
|
||||
):
|
||||
async_fire_time_changed(hass, dt_util.now() + timedelta(minutes=20))
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert "Ring access token is no longer valid. Set up Ring again" in [
|
||||
record.message for record in caplog.records if record.levelname == "ERROR"
|
||||
]
|
||||
|
||||
assert mock_config_entry.entry_id not in hass.data[DOMAIN]
|
||||
|
||||
|
||||
async def test_auth_failure_on_device_update(
|
||||
hass: HomeAssistant,
|
||||
requests_mock: requests_mock.Mocker,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
caplog,
|
||||
) -> None:
|
||||
"""Test authentication failure on global data update."""
|
||||
mock_config_entry.add_to_hass(hass)
|
||||
await hass.config_entries.async_setup(mock_config_entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
with patch(
|
||||
"ring_doorbell.RingDoorBell.history",
|
||||
side_effect=AuthenticationError,
|
||||
):
|
||||
async_fire_time_changed(hass, dt_util.now() + timedelta(minutes=20))
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert "Ring access token is no longer valid. Set up Ring again" in [
|
||||
record.message for record in caplog.records if record.levelname == "ERROR"
|
||||
]
|
||||
|
||||
assert mock_config_entry.entry_id not in hass.data[DOMAIN]
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("error_type", "log_msg"),
|
||||
[
|
||||
(
|
||||
RingTimeout,
|
||||
"Time out fetching Ring device data",
|
||||
),
|
||||
(
|
||||
RingError,
|
||||
"Error fetching Ring device data: ",
|
||||
),
|
||||
],
|
||||
ids=["timeout-error", "other-error"],
|
||||
)
|
||||
async def test_error_on_global_update(
|
||||
hass: HomeAssistant,
|
||||
requests_mock: requests_mock.Mocker,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
caplog,
|
||||
error_type,
|
||||
log_msg,
|
||||
) -> None:
|
||||
"""Test error on global data update."""
|
||||
mock_config_entry.add_to_hass(hass)
|
||||
await hass.config_entries.async_setup(mock_config_entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
with patch(
|
||||
"ring_doorbell.Ring.update_devices",
|
||||
side_effect=error_type,
|
||||
):
|
||||
async_fire_time_changed(hass, dt_util.now() + timedelta(minutes=20))
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert log_msg in [
|
||||
record.message for record in caplog.records if record.levelname == "WARNING"
|
||||
]
|
||||
|
||||
assert mock_config_entry.entry_id in hass.data[DOMAIN]
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("error_type", "log_msg"),
|
||||
[
|
||||
(
|
||||
RingTimeout,
|
||||
"Time out fetching Ring history data for device aacdef123",
|
||||
),
|
||||
(
|
||||
RingError,
|
||||
"Error fetching Ring history data for device aacdef123: ",
|
||||
),
|
||||
],
|
||||
ids=["timeout-error", "other-error"],
|
||||
)
|
||||
async def test_error_on_device_update(
|
||||
hass: HomeAssistant,
|
||||
requests_mock: requests_mock.Mocker,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
caplog,
|
||||
error_type,
|
||||
log_msg,
|
||||
) -> None:
|
||||
"""Test auth failure on data update."""
|
||||
mock_config_entry.add_to_hass(hass)
|
||||
await hass.config_entries.async_setup(mock_config_entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
with patch(
|
||||
"ring_doorbell.RingDoorBell.history",
|
||||
side_effect=error_type,
|
||||
):
|
||||
async_fire_time_changed(hass, dt_util.now() + timedelta(minutes=20))
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert log_msg in [
|
||||
record.message for record in caplog.records if record.levelname == "WARNING"
|
||||
]
|
||||
assert mock_config_entry.entry_id in hass.data[DOMAIN]
|
||||
|
||||
Reference in New Issue
Block a user