2019-05-05 16:38:55 +00:00
|
|
|
"""Test configuration and mocks for the google integration."""
|
2022-02-26 23:17:02 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2022-02-01 02:14:49 +00:00
|
|
|
from collections.abc import Callable
|
2022-02-26 23:17:02 +00:00
|
|
|
import datetime
|
2022-02-01 02:14:49 +00:00
|
|
|
from typing import Any, Generator, TypeVar
|
|
|
|
from unittest.mock import Mock, patch
|
2019-05-05 16:38:55 +00:00
|
|
|
|
2022-02-27 00:19:45 +00:00
|
|
|
from googleapiclient import discovery as google_discovery
|
2022-02-26 23:17:02 +00:00
|
|
|
from oauth2client.client import Credentials, OAuth2Credentials
|
2021-01-01 21:31:56 +00:00
|
|
|
import pytest
|
2020-05-03 18:27:19 +00:00
|
|
|
|
2022-02-26 23:17:02 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
|
|
|
from homeassistant.util.dt import utcnow
|
2022-02-01 02:14:49 +00:00
|
|
|
|
|
|
|
ApiResult = Callable[[dict[str, Any]], None]
|
|
|
|
T = TypeVar("T")
|
|
|
|
YieldFixture = Generator[T, None, None]
|
|
|
|
|
|
|
|
|
|
|
|
CALENDAR_ID = "qwertyuiopasdfghjklzxcvbnm@import.calendar.google.com"
|
2019-05-05 16:38:55 +00:00
|
|
|
TEST_CALENDAR = {
|
2022-02-01 02:14:49 +00:00
|
|
|
"id": CALENDAR_ID,
|
2019-07-31 19:25:30 +00:00
|
|
|
"etag": '"3584134138943410"',
|
|
|
|
"timeZone": "UTC",
|
|
|
|
"accessRole": "reader",
|
|
|
|
"foregroundColor": "#000000",
|
|
|
|
"selected": True,
|
|
|
|
"kind": "calendar#calendarListEntry",
|
|
|
|
"backgroundColor": "#16a765",
|
|
|
|
"description": "Test Calendar",
|
|
|
|
"summary": "We are, we are, a... Test Calendar",
|
|
|
|
"colorId": "8",
|
|
|
|
"defaultReminders": [],
|
|
|
|
"track": True,
|
2019-05-05 16:38:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def test_calendar():
|
|
|
|
"""Return a test calendar."""
|
|
|
|
return TEST_CALENDAR
|
|
|
|
|
|
|
|
|
2022-02-26 23:17:02 +00:00
|
|
|
class FakeStorage:
|
|
|
|
"""A fake storage object for persiting creds."""
|
|
|
|
|
|
|
|
def __init__(self) -> None:
|
|
|
|
"""Initialize FakeStorage."""
|
|
|
|
self._creds: Credentials | None = None
|
|
|
|
|
|
|
|
def get(self) -> Credentials | None:
|
|
|
|
"""Get credentials from storage."""
|
|
|
|
return self._creds
|
|
|
|
|
|
|
|
def put(self, creds: Credentials) -> None:
|
|
|
|
"""Put credentials in storage."""
|
|
|
|
self._creds = creds
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
async def token_scopes() -> list[str]:
|
|
|
|
"""Fixture for scopes used during test."""
|
|
|
|
return ["https://www.googleapis.com/auth/calendar"]
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
async def creds(token_scopes: list[str]) -> OAuth2Credentials:
|
|
|
|
"""Fixture that defines creds used in the test."""
|
|
|
|
token_expiry = utcnow() + datetime.timedelta(days=7)
|
|
|
|
return OAuth2Credentials(
|
|
|
|
access_token="ACCESS_TOKEN",
|
|
|
|
client_id="client-id",
|
|
|
|
client_secret="client-secret",
|
|
|
|
refresh_token="REFRESH_TOKEN",
|
|
|
|
token_expiry=token_expiry,
|
|
|
|
token_uri="http://example.com",
|
|
|
|
user_agent="n/a",
|
|
|
|
scopes=token_scopes,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
|
|
async def storage() -> YieldFixture[FakeStorage]:
|
|
|
|
"""Fixture to populate an existing token file for read on startup."""
|
|
|
|
storage = FakeStorage()
|
|
|
|
with patch("homeassistant.components.google.Storage", return_value=storage):
|
|
|
|
yield storage
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
async def mock_token_read(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
creds: OAuth2Credentials,
|
|
|
|
storage: FakeStorage,
|
|
|
|
) -> None:
|
|
|
|
"""Fixture to populate an existing token file for read on startup."""
|
|
|
|
storage.put(creds)
|
|
|
|
|
|
|
|
|
2022-02-27 00:19:45 +00:00
|
|
|
@pytest.fixture(autouse=True)
|
|
|
|
def calendar_resource() -> YieldFixture[google_discovery.Resource]:
|
|
|
|
"""Fixture to mock out the Google discovery API."""
|
|
|
|
with patch("homeassistant.components.google.api.google_discovery.build") as mock:
|
|
|
|
yield mock
|
|
|
|
|
|
|
|
|
2022-02-01 02:14:49 +00:00
|
|
|
@pytest.fixture
|
|
|
|
def mock_events_list(
|
2022-02-27 00:19:45 +00:00
|
|
|
calendar_resource: google_discovery.Resource,
|
2022-02-01 02:14:49 +00:00
|
|
|
) -> Callable[[dict[str, Any]], None]:
|
|
|
|
"""Fixture to construct a fake event list API response."""
|
|
|
|
|
|
|
|
def _put_result(response: dict[str, Any]) -> None:
|
2022-02-27 00:19:45 +00:00
|
|
|
calendar_resource.return_value.events.return_value.list.return_value.execute.return_value = (
|
2022-02-01 02:14:49 +00:00
|
|
|
response
|
|
|
|
)
|
|
|
|
return
|
|
|
|
|
|
|
|
return _put_result
|
|
|
|
|
|
|
|
|
2022-02-27 18:58:00 +00:00
|
|
|
@pytest.fixture
|
|
|
|
def mock_events_list_items(
|
|
|
|
mock_events_list: Callable[[dict[str, Any]], None]
|
|
|
|
) -> Callable[list[[dict[str, Any]]], None]:
|
|
|
|
"""Fixture to construct an API response containing event items."""
|
|
|
|
|
|
|
|
def _put_items(items: list[dict[str, Any]]) -> None:
|
|
|
|
mock_events_list({"items": items})
|
|
|
|
return
|
|
|
|
|
|
|
|
return _put_items
|
|
|
|
|
|
|
|
|
2022-02-01 02:14:49 +00:00
|
|
|
@pytest.fixture
|
|
|
|
def mock_calendars_list(
|
2022-02-27 00:19:45 +00:00
|
|
|
calendar_resource: google_discovery.Resource,
|
2022-02-01 02:14:49 +00:00
|
|
|
) -> ApiResult:
|
|
|
|
"""Fixture to construct a fake calendar list API response."""
|
|
|
|
|
|
|
|
def _put_result(response: dict[str, Any]) -> None:
|
2022-02-27 00:19:45 +00:00
|
|
|
calendar_resource.return_value.calendarList.return_value.list.return_value.execute.return_value = (
|
2022-02-01 02:14:49 +00:00
|
|
|
response
|
|
|
|
)
|
|
|
|
return
|
|
|
|
|
|
|
|
return _put_result
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def mock_insert_event(
|
2022-02-27 00:19:45 +00:00
|
|
|
calendar_resource: google_discovery.Resource,
|
2022-02-01 02:14:49 +00:00
|
|
|
) -> Mock:
|
|
|
|
"""Fixture to create a mock to capture new events added to the API."""
|
|
|
|
insert_mock = Mock()
|
2022-02-27 00:19:45 +00:00
|
|
|
calendar_resource.return_value.events.return_value.insert = insert_mock
|
2022-02-01 02:14:49 +00:00
|
|
|
return insert_mock
|