From d41fe01736eb1b753977f968a677434f6c800de3 Mon Sep 17 00:00:00 2001 From: Allen Porter Date: Wed, 21 Dec 2022 07:36:37 -0800 Subject: [PATCH] Don't allow google calendar create/delete for yaml/search calendars (#83604) * Update tests/components/google/test_calendar.py Co-authored-by: Martin Hjelmare * Don't allow google calendar create/delete for yaml/search calendars * Revert incorrect docstring update Co-authored-by: Martin Hjelmare --- homeassistant/components/google/calendar.py | 4 +- tests/components/google/test_calendar.py | 51 +++++++++++++++++++++ 2 files changed, 54 insertions(+), 1 deletion(-) diff --git a/homeassistant/components/google/calendar.py b/homeassistant/components/google/calendar.py index ac827420d31..e6f31cc54cb 100644 --- a/homeassistant/components/google/calendar.py +++ b/homeassistant/components/google/calendar.py @@ -212,6 +212,7 @@ async def async_setup_entry( # Prefer calendar sync down of resources when possible. However, sync does not work # for search. Also free-busy calendars denormalize recurring events as individual # events which is not efficient for sync + support_write = calendar_item.access_role.is_writer if ( search := data.get(CONF_SEARCH) or calendar_item.access_role == AccessRole.FREE_BUSY_READER @@ -223,6 +224,7 @@ async def async_setup_entry( calendar_id, search, ) + support_write = False else: request_template = SyncEventsRequest( calendar_id=calendar_id, @@ -246,7 +248,7 @@ async def async_setup_entry( generate_entity_id(ENTITY_ID_FORMAT, entity_name, hass=hass), unique_id, entity_enabled, - calendar_item.access_role.is_writer, + support_write, ) ) diff --git a/tests/components/google/test_calendar.py b/tests/components/google/test_calendar.py index 4e70be87cf6..8eeb32295d3 100644 --- a/tests/components/google/test_calendar.py +++ b/tests/components/google/test_calendar.py @@ -1091,6 +1091,57 @@ async def test_readonly_websocket_create( assert result["error"].get("code") == "not_supported" +@pytest.mark.parametrize( + "calendars_config", + [ + [ + { + "cal_id": CALENDAR_ID, + "entities": [ + { + "device_id": "backyard_light", + "name": "Backyard Light", + "search": "#Backyard", + }, + ], + } + ], + ], +) +async def test_readonly_search_calendar( + hass: HomeAssistant, + component_setup: ComponentSetup, + mock_calendars_yaml, + mock_insert_event: Callable[[str, dict[str, Any]], None], + mock_events_list: ApiResult, + aioclient_mock: AiohttpClientMocker, + ws_client: ClientFixture, +) -> None: + """Test calendar configured with yaml/search does not support mutation.""" + mock_events_list({}) + assert await component_setup() + + aioclient_mock.clear_requests() + mock_insert_event( + calendar_id=CALENDAR_ID, + ) + + client = await ws_client() + result = await client.cmd( + "create", + { + "entity_id": TEST_YAML_ENTITY, + "event": { + "summary": "Bastille Day Party", + "dtstart": "1997-07-14T17:00:00+00:00", + "dtend": "1997-07-15T04:00:00+00:00", + }, + }, + ) + assert result.get("error") + assert result["error"].get("code") == "not_supported" + + @pytest.mark.parametrize("calendar_access_role", ["reader", "freeBusyReader"]) async def test_all_day_reader_access(hass, mock_events_list_items, component_setup): """Test that reader / freebusy reader access can load properly."""