Coverage for app/backend/src/tests/test_screens.py: 100%
117 statements
« prev ^ index » next coverage.py v7.17.5, created at 2026-11-25 11:02 +0000
« prev ^ index » next coverage.py v7.17.5, created at 2026-11-25 11:02 +0000
1from datetime import time, timedelta
3from google.protobuf import empty_pb2
5from couchers.db import session_scope
6from couchers.proto import (
7 communities_pb2,
8 events_pb2,
9 messages_pb2,
10 public_trips_pb2,
11 requests_pb2,
12 screens_pb2,
13)
14from couchers.utils import today
15from tests.fixtures.api import discussions as discussions_api
16from tests.fixtures.api import events as events_api
17from tests.fixtures.api import public_trips as public_trips_api
18from tests.fixtures.api import requests as requests_api
19from tests.fixtures.db import generate_user
20from tests.fixtures.feature_flags import Flags
21from tests.fixtures.sessions import (
22 account_session,
23 communities_session,
24 screens_session,
25)
26from tests.test_communities import create_community
28UPCOMING_STATUSES = [
29 messages_pb2.HOST_REQUEST_STATUS_ACCEPTED,
30 messages_pb2.HOST_REQUEST_STATUS_CONFIRMED,
31]
34def test_GetDashboard_matches_individual_rpcs(db, moderator):
35 surfer_user, surfer_token = generate_user()
36 host_user, host_token = generate_user()
38 host_request_id = requests_api.create_host_request(
39 token=surfer_token, host_user_id=host_user.id, approve_by=moderator
40 )
41 requests_api.respond_host_request(
42 token=host_token, host_request_id=host_request_id, status=messages_pb2.HOST_REQUEST_STATUS_ACCEPTED
43 )
45 # the dashboard response must be identical to fanning out to the individual RPCs with the
46 # same parameters the web frontend uses
47 surfing = requests_api.list_host_requests(
48 token=surfer_token,
49 only_sent=True,
50 only_active=True,
51 status_in=UPCOMING_STATUSES,
52 sort_by=requests_pb2.HOST_REQUEST_SORT_BY_FROM_DATE,
53 number=10,
54 )
55 hosting = requests_api.list_host_requests(
56 token=surfer_token,
57 only_received=True,
58 only_active=True,
59 status_in=UPCOMING_STATUSES,
60 sort_by=requests_pb2.HOST_REQUEST_SORT_BY_FROM_DATE,
61 number=10,
62 )
64 my_events = events_api.list_my_events(token=surfer_token, page_size=9, attending=True, organizing=True)
65 community_events = events_api.list_my_events(
66 token=surfer_token,
67 page_size=9,
68 my_communities=True,
69 my_communities_exclude_global=True,
70 )
72 discussions = discussions_api.list_my_communities_discussions(token=surfer_token, page_size=9)
73 with account_session(surfer_token) as api:
74 reminders = api.GetReminders(empty_pb2.Empty())
75 my_public_trips = public_trips_api.list_public_trips_by_user(
76 token=surfer_token,
77 user_id=surfer_user.id,
78 page_size=10,
79 statuses_in=[public_trips_pb2.PUBLIC_TRIP_STATUS_SEARCHING_FOR_HOST],
80 ascending=True,
81 )
82 community_public_trips = public_trips_api.list_my_communities_public_trips(token=surfer_token, page_size=10)
84 with communities_session(surfer_token) as api:
85 my_communities = api.ListUserCommunities(communities_pb2.ListUserCommunitiesReq(page_size=25))
87 with screens_session(surfer_token) as api:
88 res = api.GetDashboard(screens_pb2.GetDashboardReq())
90 assert res.reminders == reminders
91 assert res.surfing == surfing
92 assert res.hosting == hosting
93 assert res.my_events == my_events
94 assert res.community_events == community_events
95 assert res.discussions == discussions
96 assert res.my_public_trips == my_public_trips
97 assert res.community_public_trips == community_public_trips
98 assert res.my_communities == my_communities
100 # the surfer sees their upcoming trip under surfing, nothing under hosting
101 assert len(res.surfing.host_requests) == 1
102 assert res.surfing.host_requests[0].host_request_id == host_request_id
103 assert res.surfing.host_requests[0].status == messages_pb2.HOST_REQUEST_STATUS_ACCEPTED
104 assert len(res.hosting.host_requests) == 0
107def test_GetDashboard_buckets_by_role(db, moderator):
108 surfer_user, surfer_token = generate_user()
109 host_user, host_token = generate_user()
111 host_request_id = requests_api.create_host_request(
112 token=surfer_token, host_user_id=host_user.id, approve_by=moderator
113 )
114 requests_api.respond_host_request(
115 token=host_token, host_request_id=host_request_id, status=messages_pb2.HOST_REQUEST_STATUS_ACCEPTED
116 )
118 # the host sees the upcoming stay under hosting, nothing under surfing
119 with screens_session(host_token) as api:
120 res = api.GetDashboard(screens_pb2.GetDashboardReq())
121 assert len(res.hosting.host_requests) == 1
122 assert res.hosting.host_requests[0].host_request_id == host_request_id
123 assert len(res.surfing.host_requests) == 0
126def test_GetDashboard_community_events_includes_own_events(db, moderator):
127 # Users' own events also show under community_events, so they can see they're published
128 attendee_user, attendee_token = generate_user()
129 organizer_user, organizer_token = generate_user()
131 with session_scope() as session:
132 # community_events excludes world and macroregion communities, so nest down to a region
133 world = create_community(session, 0, 100, "World", [attendee_user, organizer_user], [], None)
134 macroregion = create_community(session, 0, 100, "Macroregion", [attendee_user, organizer_user], [], world)
135 region = create_community(session, 0, 100, "Region", [attendee_user, organizer_user], [], macroregion)
136 community_id = region.id
138 e_attending = events_api.create_event(
139 token=organizer_token, parent_community_id=community_id, approve_by=moderator
140 ).event_id
141 e_community_only = events_api.create_event(
142 token=organizer_token, parent_community_id=community_id, approve_by=moderator
143 ).event_id
145 events_api.set_event_attendance(
146 token=attendee_token, event_id=e_attending, attendance_state=events_pb2.ATTENDANCE_STATE_GOING
147 )
149 with screens_session(attendee_token) as api:
150 res = api.GetDashboard(screens_pb2.GetDashboardReq())
151 assert [e.event_id for e in res.my_events.events] == [e_attending]
152 assert [e.event_id for e in res.community_events.events] == [e_attending, e_community_only]
154 with screens_session(organizer_token) as api:
155 res = api.GetDashboard(screens_pb2.GetDashboardReq())
156 assert [e.event_id for e in res.community_events.events] == [e_attending, e_community_only]
159def test_GetDashboard_my_communities(db):
160 user, token = generate_user()
161 other_user, _ = generate_user()
163 with session_scope() as session:
164 world = create_community(session, 0, 100, "World", [user], [], None)
165 joined = create_community(session, 0, 50, "Joined", [user], [], world)
166 create_community(session, 0, 25, "Not joined", [other_user], [], joined)
167 expected_ids = [joined.id, world.id]
169 with screens_session(token) as api:
170 res = api.GetDashboard(screens_pb2.GetDashboardReq())
172 assert [c.community_id for c in res.my_communities.communities] == expected_ids
175def test_GetDashboard_returns_several_pages_per_section(db, moderator):
176 # the client pages through each section locally, so a section returns a batch of items and a
177 # token for that section's own RPC when there are more
178 page_size = 9 # GetDashboard's my_events page size
179 user, token = generate_user()
181 with session_scope() as session:
182 community_id = create_community(session, 0, 100, "Community", [user], [], None).id
184 tomorrow = today() + timedelta(days=1)
185 event_ids = [
186 events_api.create_event(
187 token=token,
188 parent_community_id=community_id,
189 start_date_time=(tomorrow, time(i, 0)),
190 approve_by=moderator,
191 ).event_id
192 for i in range(page_size + 1)
193 ]
195 with screens_session(token) as api:
196 res = api.GetDashboard(screens_pb2.GetDashboardReq())
198 assert [e.event_id for e in res.my_events.events] == event_ids[:page_size]
199 assert res.my_events.next_page_token
202def test_GetDashboard_empty(db):
203 user, token = generate_user()
204 with screens_session(token) as api:
205 res = api.GetDashboard(screens_pb2.GetDashboardReq())
206 assert len(res.surfing.host_requests) == 0
207 assert len(res.hosting.host_requests) == 0
208 assert len(res.my_events.events) == 0
209 assert len(res.community_events.events) == 0
210 assert len(res.discussions.discussions) == 0
211 assert len(res.reminders.reminders) == 0
212 assert len(res.my_communities.communities) == 0
215def test_GetDashboard_public_trips_behind_flag(db, flags: Flags):
216 user, token = generate_user()
218 with screens_session(token) as api:
219 res = api.GetDashboard(screens_pb2.GetDashboardReq())
220 assert res.HasField("my_public_trips")
221 assert res.HasField("community_public_trips")
223 flags.set_boolean("public_trips_enabled", False)
224 with screens_session(token) as api:
225 res = api.GetDashboard(screens_pb2.GetDashboardReq())
226 assert not res.HasField("my_public_trips")
227 assert not res.HasField("community_public_trips")