From d56dd63bfdefa28c8fefa6bf801ec32c72e1691a Mon Sep 17 00:00:00 2001 From: Mario Fetka Date: Thu, 23 Jul 2026 16:41:27 +0200 Subject: [PATCH] Decode stored calendar documents under Python 3 --- src/www/bongo_web/calendar_store.py | 2 ++ src/www/tests/test_calendar_store.py | 15 ++++++++++++++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/www/bongo_web/calendar_store.py b/src/www/bongo_web/calendar_store.py index fec7fb7..48f3282 100644 --- a/src/www/bongo_web/calendar_store.py +++ b/src/www/bongo_web/calendar_store.py @@ -77,6 +77,8 @@ def open_store(user, store_cookie): def json_to_ical(document): from libbongo._calendar import bongojson, cal + if isinstance(document, bytes): + document = document.decode("utf-8", "strict") value = bongojson.ParseString(document.strip()) if not value: raise CalendarNotFound("invalid stored calendar object") diff --git a/src/www/tests/test_calendar_store.py b/src/www/tests/test_calendar_store.py index 2f4c117..7d732d2 100644 --- a/src/www/tests/test_calendar_store.py +++ b/src/www/tests/test_calendar_store.py @@ -20,11 +20,13 @@ # ****************************************************************************/ import unittest +from types import SimpleNamespace +from unittest import mock from bongo_web.calendar_store import (CalendarNotFound, CalendarPathError, PreconditionFailed, delete_event, etag_matches, event_href, event_to_dict, - ical_to_json, make_event, + ical_to_json, json_to_ical, make_event, list_calendar_events, list_calendars, list_events, make_etag, parse_calendar_path, write_event) @@ -117,6 +119,17 @@ class CalendarPathTests(unittest.TestCase): with self.subTest(data=data), self.assertRaises(ValueError): ical_to_json(data) + def test_stored_json_bytes_are_accepted(self): + parsed = [] + calendar_module = SimpleNamespace( + bongojson=SimpleNamespace( + ParseString=lambda value: parsed.append(value) or object()), + cal=SimpleNamespace(JsonToIcal=lambda value: "ICAL")) + with mock.patch.dict( + "sys.modules", {"libbongo._calendar": calendar_module}): + self.assertEqual(json_to_ical(b' {"event": "bytes"} '), b"ICAL") + self.assertEqual(parsed, ['{"event": "bytes"}']) + class CalendarMutationTests(unittest.TestCase): def factory(self, store):