Coverage for app/backend/src/tests/test_screens.py: 100%
100 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
1from datetime import time, timedelta
3from google.protobuf import empty_pb2
5from couchers.db import session_scope
6from couchers.proto import discussions_pb2, events_pb2, messages_pb2, public_trips_pb2, requests_pb2, screens_pb2
7from couchers.utils import today
8from tests.fixtures.api import events as events_api
9from tests.fixtures.api import requests as requests_api
10from tests.fixtures.db import generate_user
11from tests.fixtures.feature_flags import Flags
12from tests.fixtures.sessions import (
13 account_session,
14 discussions_session,
15 public_trips_session,
16 screens_session,
17)
18from tests.test_communities import create_community
20UPCOMING_STATUSES = [
21 messages_pb2.HOST_REQUEST_STATUS_ACCEPTED,
22 messages_pb2.HOST_REQUEST_STATUS_CONFIRMED,
23]
26def test_GetDashboard_matches_individual_rpcs(db, moderator):
27 user1, token1 = generate_user()
28 user2, token2 = generate_user()
30 host_request_id = requests_api.create_host_request(token=token1, host_user_id=user2.id, approve_by=moderator)
31 requests_api.respond_host_request(
32 token=token2, host_request_id=host_request_id, status=messages_pb2.HOST_REQUEST_STATUS_ACCEPTED
33 )
35 # the dashboard response must be identical to fanning out to the individual RPCs with the
36 # same parameters the web frontend uses
37 surfing = requests_api.list_host_requests(
38 token=token1,
39 only_sent=True,
40 only_active=True,
41 status_in=UPCOMING_STATUSES,
42 sort_by=requests_pb2.HOST_REQUEST_SORT_BY_FROM_DATE,
43 number=10,
44 )
45 hosting = requests_api.list_host_requests(
46 token=token1,
47 only_received=True,
48 only_active=True,
49 status_in=UPCOMING_STATUSES,
50 sort_by=requests_pb2.HOST_REQUEST_SORT_BY_FROM_DATE,
51 number=10,
52 )
54 my_events = events_api.list_my_events(token=token1, page_size=9, attending=True, organizing=True)
55 community_events = events_api.list_my_events(
56 token=token1,
57 page_size=9,
58 my_communities=True,
59 my_communities_exclude_global=True,
60 exclude_attending=True,
61 )
63 with discussions_session(token1) as api:
64 discussions = api.ListMyCommunitiesDiscussions(discussions_pb2.ListMyCommunitiesDiscussionsReq(page_size=9))
65 with account_session(token1) as api:
66 reminders = api.GetReminders(empty_pb2.Empty())
67 with public_trips_session(token1) as api:
68 my_public_trips = api.ListPublicTripsByUser(
69 public_trips_pb2.ListPublicTripsByUserReq(
70 user_id=user1.id,
71 page_size=10,
72 statuses_in=[public_trips_pb2.PUBLIC_TRIP_STATUS_SEARCHING_FOR_HOST],
73 ascending=True,
74 )
75 )
76 community_public_trips = api.ListMyCommunitiesPublicTrips(
77 public_trips_pb2.ListMyCommunitiesPublicTripsReq(page_size=10)
78 )
80 with screens_session(token1) as api:
81 res = api.GetDashboard(screens_pb2.GetDashboardReq())
83 assert res.reminders == reminders
84 assert res.surfing == surfing
85 assert res.hosting == hosting
86 assert res.my_events == my_events
87 assert res.community_events == community_events
88 assert res.discussions == discussions
89 assert res.my_public_trips == my_public_trips
90 assert res.community_public_trips == community_public_trips
92 # the surfer sees their upcoming trip under surfing, nothing under hosting
93 assert len(res.surfing.host_requests) == 1
94 assert res.surfing.host_requests[0].host_request_id == host_request_id
95 assert res.surfing.host_requests[0].status == messages_pb2.HOST_REQUEST_STATUS_ACCEPTED
96 assert len(res.hosting.host_requests) == 0
99def test_GetDashboard_buckets_by_role(db, moderator):
100 user1, token1 = generate_user()
101 user2, token2 = generate_user()
103 host_request_id = requests_api.create_host_request(token=token1, host_user_id=user2.id, approve_by=moderator)
104 requests_api.respond_host_request(
105 token=token2, host_request_id=host_request_id, status=messages_pb2.HOST_REQUEST_STATUS_ACCEPTED
106 )
108 # the host sees the upcoming stay under hosting, nothing under surfing
109 with screens_session(token2) as api:
110 res = api.GetDashboard(screens_pb2.GetDashboardReq())
111 assert len(res.hosting.host_requests) == 1
112 assert res.hosting.host_requests[0].host_request_id == host_request_id
113 assert len(res.surfing.host_requests) == 0
116def test_GetDashboard_community_events_excludes_attending(db, moderator):
117 # Pins the exclude_attending parameter the web frontend sends: an event the user is
118 # attending shows under my_events and must not also duplicate into community_events.
119 user1, token1 = generate_user()
120 user2, token2 = generate_user()
122 with session_scope() as session:
123 # community_events excludes global-level communities, so nest down to a subregion
124 world = create_community(session, 0, 100, "World", [user1, user2], [], None)
125 macroregion = create_community(session, 0, 100, "Macroregion", [user1, user2], [], world)
126 region = create_community(session, 0, 100, "Region", [user1, user2], [], macroregion)
127 subregion = create_community(session, 0, 100, "Subregion", [user1, user2], [], region)
128 community_id = subregion.id
130 e_attending = events_api.create_event(token=token2, parent_community_id=community_id, approve_by=moderator).event_id
131 e_community_only = events_api.create_event(
132 token=token2, parent_community_id=community_id, approve_by=moderator
133 ).event_id
135 events_api.set_event_attendance(
136 token=token1, event_id=e_attending, attendance_state=events_pb2.ATTENDANCE_STATE_GOING
137 )
139 with screens_session(token1) as api:
140 res = api.GetDashboard(screens_pb2.GetDashboardReq())
142 # the attended event shows only under my_events, and the unjoined community event only under community_events
143 assert [e.event_id for e in res.my_events.events] == [e_attending]
144 assert [e.event_id for e in res.community_events.events] == [e_community_only]
147def test_GetDashboard_returns_several_pages_per_section(db, moderator):
148 # the client pages through each section locally, so a section returns a batch of items and a
149 # token for that section's own RPC when there are more
150 page_size = 9 # GetDashboard's my_events page size
151 user, token = generate_user()
153 with session_scope() as session:
154 community_id = create_community(session, 0, 100, "Community", [user], [], None).id
156 tomorrow = today() + timedelta(days=1)
157 event_ids = [
158 events_api.create_event(
159 token=token,
160 parent_community_id=community_id,
161 start_date_time=(tomorrow, time(i, 0)),
162 approve_by=moderator,
163 ).event_id
164 for i in range(page_size + 1)
165 ]
167 with screens_session(token) as api:
168 res = api.GetDashboard(screens_pb2.GetDashboardReq())
170 assert [e.event_id for e in res.my_events.events] == event_ids[:page_size]
171 assert res.my_events.next_page_token
174def test_GetDashboard_empty(db):
175 user, token = generate_user()
176 with screens_session(token) as api:
177 res = api.GetDashboard(screens_pb2.GetDashboardReq())
178 assert len(res.surfing.host_requests) == 0
179 assert len(res.hosting.host_requests) == 0
180 assert len(res.my_events.events) == 0
181 assert len(res.community_events.events) == 0
182 assert len(res.discussions.discussions) == 0
183 assert len(res.reminders.reminders) == 0
186def test_GetDashboard_public_trips_behind_flag(db, flags: Flags):
187 user, token = generate_user()
189 with screens_session(token) as api:
190 res = api.GetDashboard(screens_pb2.GetDashboardReq())
191 assert res.HasField("my_public_trips")
192 assert res.HasField("community_public_trips")
194 flags.set_boolean("public_trips_enabled", False)
195 with screens_session(token) as api:
196 res = api.GetDashboard(screens_pb2.GetDashboardReq())
197 assert not res.HasField("my_public_trips")
198 assert not res.HasField("community_public_trips")