Skip to content

Commit 0f8cbc4

Browse files
Test tweaks
1 parent f1be79d commit 0f8cbc4

3 files changed

Lines changed: 37 additions & 36 deletions

File tree

app/backend/src/couchers/email/calendar_events.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,6 @@ def create_event_ics_event(event: events_pb2.Event, loc_context: LocalizationCon
9797

9898
ics_event.begin = to_aware_datetime(event.start_time, event.timezone)
9999
ics_event.end = to_aware_datetime(event.start_time, event.timezone)
100-
ics_event.timezone = event.timezone
101100

102101
ics_event.location = event.location
103102
url = urls.event_link(occurrence_id=event.event_id, slug=event.slug)

app/backend/src/tests/test_calendar_events.py

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -27,23 +27,26 @@ def test_host_request_ics_content():
2727
ics: str = create_host_request_ics_calendar(
2828
host_request, other_name="Bob", hosting=True, loc_context=LocalizationContext.en_utc()
2929
).serialize()
30-
assert _normalize_ics(ics) == _normalize_ics("""
30+
assert _assert_ics_matches_pattern(
31+
ics,
32+
"""
3133
BEGIN:VCALENDAR
3234
VERSION:2.0
3335
PRODID:-//Couchers.org//Couchers//EN
3436
BEGIN:VEVENT
35-
SEQUENCE:<stripped>
37+
SEQUENCE:***
3638
DTSTART;VALUE=DATE:20000101
3739
DTEND;VALUE=DATE:20000103
38-
DESCRIPTION:<stripped>/42
40+
DESCRIPTION:***/42
3941
LOCATION:New York
4042
SUMMARY:Hosting Bob
41-
UID:host_request.42@<stripped>
42-
URL:<stripped>/42
43+
UID:host_request.42@***
44+
URL:***/42
4345
END:VEVENT
4446
METHOD:PUBLISH
4547
END:VCALENDAR
46-
""")
48+
""",
49+
)
4750

4851

4952
def test_host_request_cancelled_ics_content():
@@ -58,24 +61,27 @@ def test_host_request_cancelled_ics_content():
5861
ics: str = create_host_request_ics_calendar(
5962
host_request, other_name="Bob", hosting=True, loc_context=LocalizationContext.en_utc()
6063
).serialize()
61-
assert _normalize_ics(ics) == _normalize_ics("""
64+
assert _assert_ics_matches_pattern(
65+
ics,
66+
"""
6267
BEGIN:VCALENDAR
6368
VERSION:2.0
6469
PRODID:-//Couchers.org//Couchers//EN
6570
BEGIN:VEVENT
66-
SEQUENCE:<stripped>
71+
SEQUENCE:***
6772
DTSTART;VALUE=DATE:20000101
6873
DTEND;VALUE=DATE:20000103
69-
DESCRIPTION:<stripped>/42
74+
DESCRIPTION:***/42
7075
LOCATION:New York
7176
STATUS:CANCELLED
7277
SUMMARY:Cancelled: Hosting Bob
73-
UID:host_request.42@<stripped>
74-
URL:<stripped>/42
78+
UID:host_request.42@***
79+
URL:***/42
7580
END:VEVENT
7681
METHOD:PUBLISH
7782
END:VCALENDAR
78-
""")
83+
""",
84+
)
7985

8086

8187
def test_event_ics_content():
@@ -84,47 +90,41 @@ def test_event_ics_content():
8490
title="Event Title",
8591
slug="event-slug",
8692
content="Event description",
87-
start_time=Timestamp_from_datetime(datetime(2000, 1, 1, tzinfo=UTC)),
88-
end_time=Timestamp_from_datetime(datetime(2000, 1, 2, tzinfo=UTC)),
93+
start_time=Timestamp_from_datetime(datetime(2000, 1, 1, 12, 0, tzinfo=UTC)),
94+
end_time=Timestamp_from_datetime(datetime(2000, 1, 2, 12, 0, tzinfo=UTC)),
8995
location=events_pb2.EventLocation(address="City, Country", lat=0.1, lng=0.1),
9096
)
9197

9298
ics: str = create_event_ics_calendar(event, loc_context=LocalizationContext.en_utc()).serialize()
93-
assert _normalize_ics(ics) == _normalize_ics("""
99+
assert _assert_ics_matches_pattern(
100+
ics,
101+
"""
94102
BEGIN:VCALENDAR
95103
VERSION:2.0
96104
PRODID:-//Couchers.org//Couchers//EN
97105
BEGIN:VEVENT
98-
SEQUENCE:<stripped>
99-
DTSTART;VALUE=DATE:20000101
100-
DTEND;VALUE=DATE:20000102
106+
SEQUENCE:***
107+
DTSTART;20000101T120000Z
108+
DTEND;20000102T120000Z
101109
DESCRIPTION:Event description
102110
LOCATION:City, Country
103111
STATUS:CANCELLED
104112
SUMMARY:Event Title
105-
UID:event.42@<stripped>
106-
URL:<stripped>/42
113+
UID:event.42@***
114+
URL:***/42
107115
END:VEVENT
108116
METHOD:PUBLISH
109117
END:VCALENDAR
110-
""")
111-
112-
113-
def _normalize_ics(ics: str) -> str:
114-
# Normalize whitespace:
115-
# - The ics library produces '\r\n', in-code literals are '\n'
116-
# - In-code literals have start/end newlines and indentation.
117-
ics = ics.replace("\r\n", "\n").strip()
118-
119-
# Strip the domain in the UID, which depends on environment variables
120-
ics = re.sub(
121-
r"^UID:.*@(.*)$", lambda match: match[0].removesuffix(match[1]) + "<stripped>", ics, flags=re.MULTILINE
118+
""",
122119
)
123120

124-
# Strip the domain in the URL and DESCRIPTION, which depends on environment variables
125-
ics = re.sub(r"^(DESCRIPTION|URL):(.*)/(\d+)", r"\1:<stripped>/\3", ics, flags=re.MULTILINE)
126121

127-
return ics
122+
def _assert_ics_matches_pattern(actual: str, expected_pattern: str) -> str:
123+
actual = actual.replace("\r\n", "\n").strip()
124+
expected_pattern = expected_pattern.strip()
125+
126+
expected_pattern = re.escape(expected_pattern).replace("\*\*\*", ".*?")
127+
return re.fullmatch(expected_pattern, actual)
128128

129129

130130
def test_host_request_attachments(db, email_collector: EmailCollector, moderator: Moderator):

app/backend/src/tests/test_events.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1985,6 +1985,8 @@ def test_GetEventCalendarFile(db, moderator: Moderator):
19851985
assert ics_event.name == "Dummy Title"
19861986
assert ics_event.description == "Dummy content."
19871987
assert ics_event.location == "Near Null Island"
1988+
assert ics_event.begin.isoformat() == start_time.isoformat()
1989+
assert ics_event.end.isoformat() == end_time.isoformat()
19881990
assert ics_event.status != "CANCELLED"
19891991

19901992
api.CancelEvent(events_pb2.CancelEventReq(event_id=event_id))

0 commit comments

Comments
 (0)