Coverage for app/backend/src/tests/test_schedule_event_occurrences.py: 100%
126 statements
« prev ^ index » next coverage.py v7.17.2, created at 2026-11-10 09:48 +0000
« prev ^ index » next coverage.py v7.17.2, created at 2026-11-10 09:48 +0000
1"""
2Integration tests for the job scheduling event recurrences.
3"""
5from datetime import UTC, date, datetime, time
6from typing import cast
8from google.protobuf import empty_pb2
9from sqlalchemy import func, select, update
11from couchers.context import CouchersContext
12from couchers.db import session_scope
13from couchers.jobs.handlers import schedule_event_occurrences
14from couchers.models import User
15from couchers.proto import events_pb2
16from couchers.servicers.auth import create_session
17from couchers.utils import to_aware_datetime
18from tests.fixtures.api import events as events_api
19from tests.fixtures.db import generate_user
20from tests.fixtures.sessions import _MockCouchersContext
21from tests.fixtures.timewarp import FrozenTimewarp
22from tests.test_communities import create_community
25def _create_user_in_community() -> tuple[User, str]:
26 """
27 CreateEvent needs a community node whose area covers the test location to resolve a timezone.
29 Issues a long-lived session token rather than generate_user()'s default short-lived one: these
30 tests jump the frozen clock forward by a week or more at a time, and a short-lived session
31 expires after 168 hours of inactivity, which a jump that size blows straight through.
32 """
33 user, _ = generate_user()
35 with session_scope() as session:
36 create_community(session, 0, 2, "Community", [user], [], None)
38 live_user = session.execute(select(User).where(User.id == user.id)).scalar_one()
39 context = cast(CouchersContext, _MockCouchersContext())
40 token, _ = create_session(context, session, live_user, True, set_cookie=False)
42 return user, token
45def test_progressive_scheduling_until_end_date(db, frozen_timewarp: FrozenTimewarp):
46 """
47 A weekly recurring event should get scheduled progressively (2 occurrences at a time) until the ends_on date.
48 """
49 user, token = _create_user_in_community()
51 # Set time to Wednesday, January 1, 2020
52 frozen_timewarp.freeze_at(datetime(2020, 1, 1, tzinfo=UTC))
54 # Create a recurring weekly event on Thursday, January 2, 2020, 12:00-13:30 UTC.
55 initial_occurrence = events_api.create_event(
56 token=token, start_date_time=(date(2020, 1, 2), time(12, 0)), end_date_time=(date(2020, 1, 2), time(13, 30))
57 )
58 events_api.x_make_event_recurring(initial_occurrence, rrule_interval=1, ends_on_date=date(2020, 1, 20))
60 occurrences_before_scheduling = events_api.list_event_occurrences(
61 token=token, event_id=initial_occurrence.event_id, past=False
62 ).events
63 assert len(occurrences_before_scheduling) == 1, "No other occurrences should exist yet."
64 assert occurrences_before_scheduling[0].event_id == initial_occurrence.event_id
66 # Schedule recurring occurrences, expect a new occurrence on Thursday, January 9
67 schedule_event_occurrences(empty_pb2.Empty())
69 occurrences_after_scheduling = events_api.list_event_occurrences(
70 token=token, event_id=initial_occurrence.event_id, past=False
71 ).events
72 assert len(occurrences_after_scheduling) == 2
73 assert occurrences_after_scheduling[0].event_id != occurrences_after_scheduling[1].event_id
74 assert to_aware_datetime(occurrences_after_scheduling[0].start_time) == datetime(2020, 1, 2, 12, 0, tzinfo=UTC)
75 assert to_aware_datetime(occurrences_after_scheduling[0].end_time) == datetime(2020, 1, 2, 13, 30, tzinfo=UTC)
76 assert to_aware_datetime(occurrences_after_scheduling[1].start_time) == datetime(2020, 1, 9, 12, 0, tzinfo=UTC)
77 assert to_aware_datetime(occurrences_after_scheduling[1].end_time) == datetime(2020, 1, 9, 13, 30, tzinfo=UTC)
78 assert occurrences_after_scheduling[0].title == occurrences_after_scheduling[1].title
79 assert occurrences_after_scheduling[0].content == occurrences_after_scheduling[1].content
80 assert occurrences_after_scheduling[0].location.address == occurrences_after_scheduling[1].location.address
81 assert occurrences_after_scheduling[0].location.lat == occurrences_after_scheduling[1].location.lat
82 assert occurrences_after_scheduling[0].location.lng == occurrences_after_scheduling[1].location.lng
83 assert occurrences_after_scheduling[0].timezone == occurrences_after_scheduling[1].timezone
84 assert occurrences_after_scheduling[0].thread.thread_id != occurrences_after_scheduling[1].thread.thread_id
86 # Schedule again, expect a no-op (idempotency)
87 schedule_event_occurrences(empty_pb2.Empty())
89 occurrences_after_scheduling_redundantly = events_api.list_event_occurrences(
90 token=token, event_id=initial_occurrence.event_id, past=False
91 ).events
92 assert len(occurrences_after_scheduling_redundantly) == len(occurrences_after_scheduling)
94 # Set time to Friday, January 3, 2020
95 frozen_timewarp.freeze_at(datetime(2020, 1, 3, tzinfo=UTC))
97 # Test that new occurrences get scheduled now that the January 2 occurrence has gone by
98 schedule_event_occurrences(empty_pb2.Empty())
100 occurrences_after_january_3_scheduling = events_api.list_event_occurrences(
101 token=token, event_id=initial_occurrence.event_id, past=False
102 ).events
104 # January 2 has now ended and drops off the upcoming list, so this is January 9 and 16, not 3
105 # occurrences.
106 assert len(occurrences_after_january_3_scheduling) == 2
107 assert to_aware_datetime(occurrences_after_january_3_scheduling[1].start_time) == datetime(
108 2020, 1, 16, 12, 0, tzinfo=UTC
109 )
111 # Set time to Friday, January 10, 2020
112 frozen_timewarp.freeze_at(datetime(2020, 1, 10, tzinfo=UTC))
114 # Test that no occurrence gets scheduled past the ends_on_date of January 20, 2020
115 schedule_event_occurrences(empty_pb2.Empty())
117 occurrences_after_january_10_scheduling = events_api.list_event_occurrences(
118 token=token, event_id=initial_occurrence.event_id, past=False
119 ).events
121 # January 9 has now also ended; January 23 shouldn't be scheduled because it's past the
122 # ends_on_date of January 20, so only January 16 remains upcoming.
123 assert len(occurrences_after_january_10_scheduling) == 1
124 assert to_aware_datetime(occurrences_after_january_10_scheduling[0].start_time) == datetime(
125 2020, 1, 16, 12, 0, tzinfo=UTC
126 )
129def test_biweekly_frequency(db, frozen_timewarp: FrozenTimewarp):
130 """Test biweekly event scheduling."""
131 user, token = _create_user_in_community()
133 # Set time to Wednesday, January 1, 2020
134 frozen_timewarp.freeze_at(datetime(2020, 1, 1, tzinfo=UTC))
136 # Create a recurring biweekly event on Thursday, January 2, 2020, 12:00-13:30 UTC.
137 initial_occurrence = events_api.create_event(
138 token=token, start_date_time=(date(2020, 1, 2), time(12, 0)), end_date_time=(date(2020, 1, 2), time(13, 30))
139 )
140 events_api.x_make_event_recurring(initial_occurrence, rrule_interval=2, ends_on_date=date(2020, 1, 20))
142 schedule_event_occurrences(empty_pb2.Empty())
144 scheduled_occurrences = events_api.list_event_occurrences(
145 token=token, event_id=initial_occurrence.event_id, past=False
146 ).events
147 assert len(scheduled_occurrences) == 2
148 assert to_aware_datetime(scheduled_occurrences[0].start_time) == datetime(2020, 1, 2, 12, 0, tzinfo=UTC)
149 assert to_aware_datetime(scheduled_occurrences[1].start_time) == datetime(
150 2020, 1, 16, 12, 0, tzinfo=UTC
151 ) # 2 weeks later
154def test_no_rescheduling_after_edit_or_cancel(db, frozen_timewarp: FrozenTimewarp):
155 """Editing or cancelling an already-spawned occurrence must not cause it to be rescheduled."""
156 user, token = _create_user_in_community()
158 # Set time to Wednesday, January 1, 2020
159 frozen_timewarp.freeze_at(datetime(2020, 1, 1, tzinfo=UTC))
161 # Create a recurring event on Fridays
162 initial_occurrence = events_api.create_event(
163 token=token, start_date_time=(date(2020, 1, 3), time(12, 0)), end_date_time=(date(2020, 1, 3), time(13, 0))
164 )
165 events_api.x_make_event_recurring(initial_occurrence, rrule_interval=1, ends_on_date=date(2020, 6, 30))
167 schedule_event_occurrences(empty_pb2.Empty())
169 initial_scheduled_occurrences = events_api.list_event_occurrences(
170 token=token, event_id=initial_occurrence.event_id, past=False
171 ).events
172 assert len(initial_scheduled_occurrences) == 2
173 to_cancel, to_move = initial_scheduled_occurrences
174 assert to_aware_datetime(to_cancel.start_time) == datetime(2020, 1, 3, 12, 0, tzinfo=UTC)
175 assert to_aware_datetime(to_move.start_time) == datetime(2020, 1, 10, 12, 0, tzinfo=UTC)
177 # Cancel the January 3 occurrence and move the January 10 one to January 9
178 events_api.cancel_event(token=token, event_id=to_cancel.event_id)
180 events_api.update_event(
181 token=token,
182 event_id=to_move.event_id,
183 start_date_time=(date(2020, 1, 9), time(12, 0)),
184 end_date_time=(date(2020, 1, 9), time(13, 0)),
185 )
187 # Ensure no other occurrence gets scheduled, even though the January 9 occurrence was moved to January 8,
188 # so we're now recurring on Thursdays instead of Fridays, since the next two Thursdays (2 and 8) are earlier
189 # than the last scheduled date (January 9).
190 schedule_event_occurrences(empty_pb2.Empty())
192 scheduled_occurrences_after_changes = events_api.list_event_occurrences(
193 token=token, event_id=initial_occurrence.event_id, past=False, include_cancelled=True
194 ).events
195 assert len(scheduled_occurrences_after_changes) == 2
196 assert to_aware_datetime(scheduled_occurrences_after_changes[0].start_time) == datetime(
197 2020, 1, 3, 12, 0, tzinfo=UTC
198 )
199 assert to_aware_datetime(scheduled_occurrences_after_changes[1].start_time) == datetime(
200 2020, 1, 9, 12, 0, tzinfo=UTC
201 )
204def test_new_occurrence_not_based_on_previous(db, frozen_timewarp: FrozenTimewarp):
205 """The latest occurrence is the one that is used as a template for the next one."""
206 user, token = _create_user_in_community()
208 # Set time to Wednesday, January 1, 2020
209 frozen_timewarp.freeze_at(datetime(2020, 1, 1, tzinfo=UTC))
211 # Schedule on Thursdays
212 initial_occurrence = events_api.create_event(
213 token=token,
214 content="Initial content",
215 start_date_time=(date(2020, 1, 2), time(12, 0)),
216 end_date_time=(date(2020, 1, 2), time(13, 0)),
217 )
218 events_api.x_make_event_recurring(initial_occurrence, rrule_interval=1, ends_on_date=date(2020, 6, 30))
220 # Change the original occurrence
221 initial_occurrence = events_api.update_event(
222 token=token, event_id=initial_occurrence.event_id, content="Updated content"
223 )
225 # New scheduled occurrences should not care
226 schedule_event_occurrences(empty_pb2.Empty())
228 second_occurrence = events_api.list_event_occurrences(
229 token=token, event_id=initial_occurrence.event_id, past=False
230 ).events[-1]
231 assert to_aware_datetime(second_occurrence.start_time) == datetime(2020, 1, 9, 12, 0, tzinfo=UTC)
232 assert second_occurrence.content == "Initial content"
235def test_no_scheduling_after_creator_banned(db, frozen_timewarp: FrozenTimewarp):
236 """Occurrences should stop being scheduled once the event creator is banned."""
237 user, token = _create_user_in_community()
239 # Set time to Wednesday, January 1, 2020
240 frozen_timewarp.freeze_at(datetime(2020, 1, 1, tzinfo=UTC))
242 # Schedule on Thursdays
243 initial_occurrence = events_api.create_event(
244 token=token, start_date_time=(date(2020, 1, 2), time(12, 0)), end_date_time=(date(2020, 1, 2), time(13, 0))
245 )
246 events_api.x_make_event_recurring(initial_occurrence, rrule_interval=1, ends_on_date=date(2020, 6, 30))
248 # Schedules January 9
249 schedule_event_occurrences(empty_pb2.Empty())
251 occurrences_before_ban = events_api.list_event_occurrences(
252 token=token, event_id=initial_occurrence.event_id, past=False
253 ).events
254 assert len(occurrences_before_ban) == 2
256 with session_scope() as session:
257 session.execute(update(User).where(User.id == user.id).values(banned_at=func.now()))
259 # Set time to Friday, January 3, 2020, when January 16 would otherwise get scheduled
260 frozen_timewarp.freeze_at(datetime(2020, 1, 3, tzinfo=UTC))
262 schedule_event_occurrences(empty_pb2.Empty())
264 # Unban so the creator can see their (shadowed) occurrences through the API again
265 with session_scope() as session:
266 session.execute(update(User).where(User.id == user.id).values(banned_at=None))
268 occurrences_after_ban = events_api.list_event_occurrences(
269 token=token, event_id=initial_occurrence.event_id, past=False
270 ).events
272 # January 2 has ended and drops off the upcoming list, and January 16 was never scheduled.
273 assert [to_aware_datetime(occurrence.start_time) for occurrence in occurrences_after_ban] == [
274 datetime(2020, 1, 9, 12, 0, tzinfo=UTC)
275 ]
278def test_organizers_attend_scheduled_occurrences(db, frozen_timewarp: FrozenTimewarp):
279 """Event organizers should be added as attendees of newly scheduled occurrences."""
280 user, token = _create_user_in_community()
281 co_organizer, _ = generate_user()
283 # Set time to Wednesday, January 1, 2020
284 frozen_timewarp.freeze_at(datetime(2020, 1, 1, tzinfo=UTC))
286 # Schedule on Thursdays
287 initial_occurrence = events_api.create_event(
288 token=token, start_date_time=(date(2020, 1, 2), time(12, 0)), end_date_time=(date(2020, 1, 2), time(13, 0))
289 )
290 events_api.x_make_event_recurring(initial_occurrence, rrule_interval=1, ends_on_date=date(2020, 6, 30))
292 events_api.invite_event_organizer(token=token, event_id=initial_occurrence.event_id, user_id=co_organizer.id)
294 # Schedules January 9
295 schedule_event_occurrences(empty_pb2.Empty())
297 scheduled_occurrence = events_api.list_event_occurrences(
298 token=token, event_id=initial_occurrence.event_id, past=False
299 ).events[-1]
300 assert to_aware_datetime(scheduled_occurrence.start_time) == datetime(2020, 1, 9, 12, 0, tzinfo=UTC)
301 assert scheduled_occurrence.attendance_state == events_pb2.ATTENDANCE_STATE_GOING
302 assert scheduled_occurrence.going_count == 2