mirror of
https://github.com/home-assistant/core.git
synced 2026-09-16 21:48:38 +01:00
88 lines
2.8 KiB
Python
88 lines
2.8 KiB
Python
"""Unit tests for the CalDav integration."""
|
|
|
|
from functools import partial
|
|
from unittest.mock import patch
|
|
|
|
from caldav.lib.error import AuthorizationError, DAVError
|
|
from caldav.lib.http_sync import requests as caldav_requests
|
|
import pytest
|
|
|
|
from homeassistant.config_entries import ConfigEntryState
|
|
from homeassistant.core import HomeAssistant
|
|
|
|
from tests.common import MockConfigEntry
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
async def mock_add_to_hass(hass: HomeAssistant, config_entry: MockConfigEntry) -> None:
|
|
"""Fixture to add the ConfigEntry."""
|
|
config_entry.add_to_hass(hass)
|
|
|
|
|
|
async def test_load_unload(
|
|
hass: HomeAssistant,
|
|
config_entry: MockConfigEntry,
|
|
) -> None:
|
|
"""Test loading and unloading of the config entry."""
|
|
assert config_entry.state is ConfigEntryState.NOT_LOADED
|
|
|
|
with (
|
|
patch("homeassistant.components.caldav.DAVClient") as mock_client,
|
|
patch.object(
|
|
hass,
|
|
"async_add_executor_job",
|
|
wraps=hass.async_add_executor_job,
|
|
) as mock_add_executor_job,
|
|
):
|
|
await hass.config_entries.async_setup(config_entry.entry_id)
|
|
|
|
assert config_entry.state is ConfigEntryState.LOADED
|
|
assert any(
|
|
isinstance(call.args[0], partial) and call.args[0].func is mock_client
|
|
for call in mock_add_executor_job.call_args_list
|
|
)
|
|
|
|
assert await hass.config_entries.async_unload(config_entry.entry_id)
|
|
assert config_entry.state is ConfigEntryState.NOT_LOADED
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("side_effect", "expected_state", "expected_flows"),
|
|
[
|
|
(Exception(), ConfigEntryState.SETUP_ERROR, []),
|
|
(
|
|
caldav_requests.exceptions.ConnectionError(),
|
|
ConfigEntryState.SETUP_RETRY,
|
|
[],
|
|
),
|
|
(caldav_requests.exceptions.Timeout(), ConfigEntryState.SETUP_RETRY, []),
|
|
(DAVError(), ConfigEntryState.SETUP_RETRY, []),
|
|
(
|
|
AuthorizationError(reason="Unauthorized"),
|
|
ConfigEntryState.SETUP_ERROR,
|
|
["reauth_confirm"],
|
|
),
|
|
(AuthorizationError(reason="Other"), ConfigEntryState.SETUP_ERROR, []),
|
|
],
|
|
)
|
|
async def test_client_failure(
|
|
hass: HomeAssistant,
|
|
config_entry: MockConfigEntry,
|
|
side_effect: Exception,
|
|
expected_state: ConfigEntryState,
|
|
expected_flows: list[str],
|
|
) -> None:
|
|
"""Test CalDAV client failures in setup."""
|
|
|
|
assert config_entry.state is ConfigEntryState.NOT_LOADED
|
|
|
|
with patch("homeassistant.components.caldav.DAVClient") as mock_client:
|
|
mock_client.return_value.get_principal.side_effect = side_effect
|
|
await hass.config_entries.async_setup(config_entry.entry_id)
|
|
await hass.async_block_till_done()
|
|
|
|
assert config_entry.state is expected_state
|
|
|
|
flows = hass.config_entries.flow.async_progress()
|
|
assert [flow.get("step_id") for flow in flows] == expected_flows
|