|
| 1 | +""" |
| 2 | +Tests for the /commitfests.ics calendar feed. |
| 3 | +""" |
| 4 | + |
| 5 | +from __future__ import annotations |
| 6 | + |
| 7 | +from datetime import timedelta |
| 8 | +from typing import TYPE_CHECKING, cast |
| 9 | + |
| 10 | +import pytest |
| 11 | +from icalendar import Calendar, Event |
| 12 | + |
| 13 | +if TYPE_CHECKING: |
| 14 | + from pgcommitfest.commitfest.models import CommitFest |
| 15 | + |
| 16 | +pytestmark = pytest.mark.django_db |
| 17 | + |
| 18 | + |
| 19 | +def _get_calendar(client) -> Calendar: |
| 20 | + response = client.get("/commitfests.ics") |
| 21 | + assert response.status_code == 200 |
| 22 | + assert response["Content-Type"] == "text/calendar; charset=utf-8" |
| 23 | + return Calendar.from_ical(response.content) |
| 24 | + |
| 25 | + |
| 26 | +def _get_events(calendar: Calendar) -> list[Event]: |
| 27 | + return cast("list[Event]", calendar.walk("VEVENT")) |
| 28 | + |
| 29 | + |
| 30 | +def test_commitfests_ics_empty(client): |
| 31 | + """ |
| 32 | + With no commitfests, we still get a valid, empty calendar. |
| 33 | + """ |
| 34 | + calendar = _get_calendar(client) |
| 35 | + |
| 36 | + assert len(_get_events(calendar)) == 0 |
| 37 | + |
| 38 | + |
| 39 | +def test_commitfests_ics_contains_all_commitfests(client, commitfests: dict[str, CommitFest]): |
| 40 | + """ |
| 41 | + Every commitfest, regardless of status, shows up as a VEVENT with the right |
| 42 | + dates, DTEND being the day after enddate (exclusive). |
| 43 | + """ |
| 44 | + calendar = _get_calendar(client) |
| 45 | + |
| 46 | + events = _get_events(calendar) |
| 47 | + assert len(events) == len(commitfests) |
| 48 | + |
| 49 | + events_by_uid = {cast("str", event.uid): event for event in events} |
| 50 | + |
| 51 | + for cf in commitfests.values(): |
| 52 | + event = events_by_uid[f"commitfest-{cf.name}@commitfest.postgresql.org"] |
| 53 | + assert event.summary == f"Commitfest {cf.name}" |
| 54 | + assert event.url == f"https://commitfest.postgresql.org/{cf.pk}/" |
| 55 | + assert event.description == f"Status: {cf.statusstring}" |
| 56 | + assert event.start == cf.startdate |
| 57 | + assert event.end == cf.enddate + timedelta(days=1) |
0 commit comments