From f40b7126642a93a33e810485d9115c05771bdfc9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Albin=20M=C3=A9doc?= Date: Wed, 22 Feb 2023 15:33:43 +0100 Subject: [PATCH] Fix 500 error when getting calendar events (#88276) * Fix 500 error when getting calendar event * Add test for calendar dates in wrong order * Update calendar tests to use f strings --- homeassistant/components/calendar/__init__.py | 2 ++ tests/components/calendar/test_init.py | 23 ++++++++++++++----- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/homeassistant/components/calendar/__init__.py b/homeassistant/components/calendar/__init__.py index 876b90eac9b..390e14d1689 100644 --- a/homeassistant/components/calendar/__init__.py +++ b/homeassistant/components/calendar/__init__.py @@ -386,6 +386,8 @@ class CalendarEventView(http.HomeAssistantView): return web.Response(status=HTTPStatus.BAD_REQUEST) if start_date is None or end_date is None: return web.Response(status=HTTPStatus.BAD_REQUEST) + if start_date > end_date: + return web.Response(status=HTTPStatus.BAD_REQUEST) try: calendar_event_list = await entity.async_get_events( diff --git a/tests/components/calendar/test_init.py b/tests/components/calendar/test_init.py index b5135ccc789..806410c9834 100644 --- a/tests/components/calendar/test_init.py +++ b/tests/components/calendar/test_init.py @@ -28,9 +28,7 @@ async def test_events_http_api( start = dt_util.now() end = start + timedelta(days=1) response = await client.get( - "/api/calendars/calendar.calendar_1?start={}&end={}".format( - start.isoformat(), end.isoformat() - ) + f"/api/calendars/calendar.calendar_1?start={start.isoformat()}&end={end.isoformat()}" ) assert response.status == HTTPStatus.OK events = await response.json() @@ -63,14 +61,27 @@ async def test_events_http_api_error( side_effect=HomeAssistantError("Failure"), ): response = await client.get( - "/api/calendars/calendar.calendar_1?start={}&end={}".format( - start.isoformat(), end.isoformat() - ) + f"/api/calendars/calendar.calendar_1?start={start.isoformat()}&end={end.isoformat()}" ) assert response.status == HTTPStatus.INTERNAL_SERVER_ERROR assert await response.json() == {"message": "Error reading events: Failure"} +async def test_events_http_api_dates_wrong_order( + hass: HomeAssistant, hass_client: ClientSessionGenerator +) -> None: + """Test the calendar demo view.""" + await async_setup_component(hass, "calendar", {"calendar": {"platform": "demo"}}) + await hass.async_block_till_done() + client = await hass_client() + start = dt_util.now() + end = start + timedelta(days=-1) + response = await client.get( + f"/api/calendars/calendar.calendar_1?start={start.isoformat()}&end={end.isoformat()}" + ) + assert response.status == HTTPStatus.BAD_REQUEST + + async def test_calendars_http_api( hass: HomeAssistant, hass_client: ClientSessionGenerator ) -> None: