Skip to content

Commit

Permalink
Always allow a custom offset
Browse files Browse the repository at this point in the history
  • Loading branch information
RealOrangeOne committed Mar 26, 2024
1 parent 67a9ef7 commit 16d7591
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 18 deletions.
1 change: 0 additions & 1 deletion calmerge/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
10 changes: 5 additions & 5 deletions calmerge/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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())
8 changes: 0 additions & 8 deletions tests/calendars.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down
8 changes: 4 additions & 4 deletions tests/test_calendar_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down Expand Up @@ -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},
)

Expand All @@ -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},
)

Expand Down

0 comments on commit 16d7591

Please sign in to comment.