From 16d75916787555fe1dde841324db4f3e07a88c89 Mon Sep 17 00:00:00 2001 From: Jake Howard Date: Tue, 26 Mar 2024 12:24:09 +0000 Subject: [PATCH] Always allow a custom offset --- calmerge/config.py | 1 - calmerge/views.py | 10 +++++----- tests/calendars.toml | 8 -------- tests/test_calendar_view.py | 8 ++++---- 4 files changed, 9 insertions(+), 18 deletions(-) diff --git a/calmerge/config.py b/calmerge/config.py index e431304..3af5e62 100644 --- a/calmerge/config.py +++ b/calmerge/config.py @@ -37,7 +37,6 @@ class CalendarConfig(BaseModel): name: str urls: list[HttpUrl] offset_days: int = Field(default=0, le=MAX_OFFSET, ge=-MAX_OFFSET) - allow_custom_offset: bool = False auth: AuthConfig | None = None @field_validator("urls") diff --git a/calmerge/views.py b/calmerge/views.py index bebebac..b127d03 100644 --- a/calmerge/views.py +++ b/calmerge/views.py @@ -24,8 +24,10 @@ async def calendar(request: web.Request) -> web.Response: calendar = await fetch_merged_calendar(calendar_config) - if calendar_config.allow_custom_offset: - offset_days = try_parse_int(request.query.get("offset_days", "")) + offset_days = calendar_config.offset_days + + if custom_offset_days := request.query.get("offset_days", ""): + offset_days = try_parse_int(custom_offset_days) if offset_days is None: raise web.HTTPBadRequest(reason="offset_days is invalid") @@ -34,9 +36,7 @@ async def calendar(request: web.Request) -> web.Response: reason=f"offset_days is too large (must be between -{MAX_OFFSET} and {MAX_OFFSET})" ) - offset_calendar(calendar, offset_days) - - elif offset_days := calendar_config.offset_days: + if offset_days is not None: offset_calendar(calendar, offset_days) return web.Response(body=calendar.to_ical()) diff --git a/tests/calendars.toml b/tests/calendars.toml index 5484beb..5b1166b 100644 --- a/tests/calendars.toml +++ b/tests/calendars.toml @@ -11,14 +11,6 @@ urls = [ ] offset_days = 365 -[[calendar]] -name = "python-custom-offset" -urls = [ - "https://endoflife.date/calendar/python.ics", -] -offset_days = 10 -allow_custom_offset = true - [[calendar]] name = "python-authed" urls = [ diff --git a/tests/test_calendar_view.py b/tests/test_calendar_view.py index 8d4c2fe..6fcfd10 100644 --- a/tests/test_calendar_view.py +++ b/tests/test_calendar_view.py @@ -83,7 +83,7 @@ async def test_offset_calendar_matches(client: TestClient) -> None: @pytest.mark.parametrize("offset", [100, -100, MAX_OFFSET, -MAX_OFFSET]) async def test_custom_offset(client: TestClient, offset: int) -> None: offset_response = await client.get( - "/python-custom-offset.ics", + "/python-offset.ics", params={"offset_days": offset}, ) offset_calendar = icalendar.Calendar.from_ical(await offset_response.text()) @@ -115,7 +115,7 @@ async def test_custom_offset(client: TestClient, offset: int) -> None: @pytest.mark.parametrize("offset", [MAX_OFFSET + 1, -MAX_OFFSET - 1]) async def test_out_of_bounds_custom_offset(client: TestClient, offset: int) -> None: response = await client.get( - "/python-custom-offset.ics", + "/python-offset.ics", params={"offset_days": offset}, ) @@ -126,10 +126,10 @@ async def test_out_of_bounds_custom_offset(client: TestClient, offset: int) -> N ) -@pytest.mark.parametrize("offset", ["invalid", "", "\0"]) +@pytest.mark.parametrize("offset", ["invalid", "\0"]) async def test_invalid_offset(client: TestClient, offset: str) -> None: response = await client.get( - "/python-custom-offset.ics", + "/python-offset.ics", params={"offset_days": offset}, )