From 745df277a0ae0875228fa311d79c887cc9609ea2 Mon Sep 17 00:00:00 2001 From: Aaron Godfrey Date: Sun, 26 Mar 2023 13:08:36 -0700 Subject: [PATCH] Fix Todoist end date for all day event (#89837) --- homeassistant/components/todoist/calendar.py | 2 +- tests/components/todoist/test_calendar.py | 56 +++++++++++++++++++- 2 files changed, 56 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/todoist/calendar.py b/homeassistant/components/todoist/calendar.py index 8fdafee6cfd..02459b429c4 100644 --- a/homeassistant/components/todoist/calendar.py +++ b/homeassistant/components/todoist/calendar.py @@ -612,7 +612,7 @@ class TodoistProjectData: event = CalendarEvent( summary=task.content, start=due_date_value, - end=due_date_value, + end=due_date_value + timedelta(days=1), ) events.append(event) return events diff --git a/tests/components/todoist/test_calendar.py b/tests/components/todoist/test_calendar.py index 82eff0d7553..adf0f8a14b0 100644 --- a/tests/components/todoist/test_calendar.py +++ b/tests/components/todoist/test_calendar.py @@ -1,6 +1,8 @@ """Unit tests for the Todoist calendar platform.""" -from datetime import datetime +from datetime import datetime, timedelta +from http import HTTPStatus from unittest.mock import AsyncMock, patch +import urllib import pytest from todoist_api_python.models import Due, Label, Project, Task @@ -11,6 +13,9 @@ from homeassistant.const import CONF_TOKEN from homeassistant.core import HomeAssistant from homeassistant.helpers import entity_registry as er from homeassistant.helpers.entity_component import async_update_entity +from homeassistant.util import dt + +from tests.typing import ClientSessionGenerator @pytest.fixture(name="task") @@ -68,6 +73,11 @@ def mock_api(task) -> AsyncMock: return api +def get_events_url(entity: str, start: str, end: str) -> str: + """Create a url to get events during the specified time range.""" + return f"/api/calendars/{entity}?start={urllib.parse.quote(start)}&end={urllib.parse.quote(end)}" + + @patch("homeassistant.components.todoist.calendar.TodoistAPIAsync") async def test_calendar_entity_unique_id( todoist_api, hass: HomeAssistant, api, entity_registry: er.EntityRegistry @@ -139,3 +149,47 @@ async def test_calendar_custom_project_unique_id( state = hass.states.get("calendar.all_projects") assert state.state == "off" + + +@patch("homeassistant.components.todoist.calendar.TodoistAPIAsync") +async def test_all_day_event( + todoist_api, hass: HomeAssistant, hass_client: ClientSessionGenerator, api +) -> None: + """Test for an all day calendar event.""" + todoist_api.return_value = api + assert await setup.async_setup_component( + hass, + "calendar", + { + "calendar": { + "platform": DOMAIN, + CONF_TOKEN: "token", + "custom_projects": [{"name": "All projects", "labels": ["Label1"]}], + } + }, + ) + await hass.async_block_till_done() + + await async_update_entity(hass, "calendar.all_projects") + client = await hass_client() + start = dt.now() - timedelta(days=1) + end = dt.now() + timedelta(days=1) + response = await client.get( + get_events_url("calendar.all_projects", start.isoformat(), end.isoformat()) + ) + assert response.status == HTTPStatus.OK + events = await response.json() + + expected = [ + { + "start": {"date": dt.now().strftime("%Y-%m-%d")}, + "end": {"date": (dt.now() + timedelta(days=1)).strftime("%Y-%m-%d")}, + "summary": "A task", + "description": None, + "location": None, + "uid": None, + "recurrence_id": None, + "rrule": None, + } + ] + assert events == expected