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:
sdb9696 2023-11-14 07:15:19 +00:00 committed by GitHub
parent 7ca264e746
commit cf6c72fdbd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 333 additions and 54 deletions

View file

@ -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]