Test the google tasks api connection in setup (#132657)

Improve google tasks setup
This commit is contained in:
Allen Porter
2024-12-10 19:01:50 -08:00
committed by GitHub
parent 77debcbe8b
commit 355e80aa56
6 changed files with 115 additions and 73 deletions
+39 -1
View File
@@ -1,10 +1,12 @@
"""Test fixtures for Google Tasks."""
from collections.abc import Awaitable, Callable
import json
import time
from typing import Any
from unittest.mock import patch
from unittest.mock import Mock, patch
from httplib2 import Response
import pytest
from homeassistant.components.application_credentials import (
@@ -24,6 +26,14 @@ FAKE_ACCESS_TOKEN = "some-access-token"
FAKE_REFRESH_TOKEN = "some-refresh-token"
FAKE_AUTH_IMPL = "conftest-imported-cred"
TASK_LIST = {
"id": "task-list-id-1",
"title": "My tasks",
}
LIST_TASK_LIST_RESPONSE = {
"items": [TASK_LIST],
}
@pytest.fixture
def platforms() -> list[Platform]:
@@ -89,3 +99,31 @@ async def mock_integration_setup(
return result
return run
@pytest.fixture(name="api_responses")
def mock_api_responses() -> list[dict | list]:
"""Fixture forcreate_response_object API responses to return during test."""
return []
def create_response_object(api_response: dict | list) -> tuple[Response, bytes]:
"""Create an http response."""
return (
Response({"Content-Type": "application/json"}),
json.dumps(api_response).encode(),
)
@pytest.fixture(name="response_handler")
def mock_response_handler(api_responses: list[dict | list]) -> list:
"""Create a mock http2lib response handler."""
return [create_response_object(api_response) for api_response in api_responses]
@pytest.fixture
def mock_http_response(response_handler: list | Callable) -> Mock:
"""Fixture to fake out http2lib responses."""
with patch("httplib2.Http.request", side_effect=response_handler) as mock_response:
yield mock_response