Coverage for app/backend/src/tests/fixtures/api/communities.py: 100%
71 statements
« prev ^ index » next coverage.py v7.17.5, created at 2026-11-27 22:43 +0000
« prev ^ index » next coverage.py v7.17.5, created at 2026-11-27 22:43 +0000
1from contextlib import contextmanager
2from typing import cast
4import grpc
6from couchers.proto import communities_pb2, communities_pb2_grpc
7from couchers.servicers.communities import Communities
8from tests.fixtures.sessions import FakeChannel
11@contextmanager
12def _communities_session(token: str):
13 channel = FakeChannel(token)
14 communities_pb2_grpc.add_CommunitiesServicer_to_server(Communities(), cast(grpc.Server, channel))
15 yield communities_pb2_grpc.CommunitiesStub(cast(grpc.Channel, channel))
18def get_community(*, token: str, community_id: int) -> communities_pb2.Community:
19 with _communities_session(token) as session:
20 res: communities_pb2.Community = session.GetCommunity(
21 communities_pb2.GetCommunityReq(community_id=community_id)
22 )
23 return res
26def list_communities(
27 *,
28 token: str,
29 # None lists all communities.
30 community_id: int | None = None,
31 page_size: int | None = None,
32 page_token: str | None = None,
33) -> communities_pb2.ListCommunitiesRes:
34 with _communities_session(token) as session:
35 res: communities_pb2.ListCommunitiesRes = session.ListCommunities(
36 communities_pb2.ListCommunitiesReq(community_id=community_id, page_size=page_size, page_token=page_token)
37 )
38 return res
41def list_user_communities(
42 *,
43 token: str,
44 # None lists the communities of the calling user.
45 user_id: int | None = None,
46 page_size: int | None = None,
47 page_token: str | None = None,
48) -> communities_pb2.ListUserCommunitiesRes:
49 with _communities_session(token) as session:
50 res: communities_pb2.ListUserCommunitiesRes = session.ListUserCommunities(
51 communities_pb2.ListUserCommunitiesReq(user_id=user_id, page_size=page_size, page_token=page_token)
52 )
53 return res
56def list_groups(
57 *, token: str, community_id: int, page_size: int | None = None, page_token: str | None = None
58) -> communities_pb2.ListGroupsRes:
59 with _communities_session(token) as session:
60 res: communities_pb2.ListGroupsRes = session.ListGroups(
61 communities_pb2.ListGroupsReq(community_id=community_id, page_size=page_size, page_token=page_token)
62 )
63 return res
66def list_admins(
67 *, token: str, community_id: int, page_size: int | None = None, page_token: str | None = None
68) -> communities_pb2.ListAdminsRes:
69 with _communities_session(token) as session:
70 res: communities_pb2.ListAdminsRes = session.ListAdmins(
71 communities_pb2.ListAdminsReq(community_id=community_id, page_size=page_size, page_token=page_token)
72 )
73 return res
76def add_admin(*, token: str, community_id: int, user_id: int) -> None:
77 with _communities_session(token) as session:
78 session.AddAdmin(communities_pb2.AddAdminReq(community_id=community_id, user_id=user_id))
81def remove_admin(*, token: str, community_id: int, user_id: int) -> None:
82 with _communities_session(token) as session:
83 session.RemoveAdmin(communities_pb2.RemoveAdminReq(community_id=community_id, user_id=user_id))
86def list_members(
87 *, token: str, community_id: int, page_size: int | None = None, page_token: str | None = None
88) -> communities_pb2.ListMembersRes:
89 with _communities_session(token) as session:
90 res: communities_pb2.ListMembersRes = session.ListMembers(
91 communities_pb2.ListMembersReq(community_id=community_id, page_size=page_size, page_token=page_token)
92 )
93 return res
96def list_nearby_users(
97 *, token: str, community_id: int, page_size: int | None = None, page_token: str | None = None
98) -> communities_pb2.ListNearbyUsersRes:
99 with _communities_session(token) as session:
100 res: communities_pb2.ListNearbyUsersRes = session.ListNearbyUsers(
101 communities_pb2.ListNearbyUsersReq(community_id=community_id, page_size=page_size, page_token=page_token)
102 )
103 return res
106def list_events(
107 *,
108 token: str,
109 community_id: int,
110 past: bool = False,
111 include_parents: bool = False,
112 page_size: int | None = None,
113 page_token: str | None = None,
114) -> communities_pb2.ListEventsRes:
115 with _communities_session(token) as session:
116 res: communities_pb2.ListEventsRes = session.ListEvents(
117 communities_pb2.ListEventsReq(
118 community_id=community_id,
119 past=past,
120 include_parents=include_parents,
121 page_size=page_size,
122 page_token=page_token,
123 )
124 )
125 return res
128def list_discussions(
129 *, token: str, community_id: int, page_size: int | None = None, page_token: str | None = None
130) -> communities_pb2.ListDiscussionsRes:
131 with _communities_session(token) as session:
132 res: communities_pb2.ListDiscussionsRes = session.ListDiscussions(
133 communities_pb2.ListDiscussionsReq(community_id=community_id, page_size=page_size, page_token=page_token)
134 )
135 return res
138def join_community(*, token: str, community_id: int) -> None:
139 with _communities_session(token) as session:
140 session.JoinCommunity(communities_pb2.JoinCommunityReq(community_id=community_id))
143def leave_community(*, token: str, community_id: int) -> None:
144 with _communities_session(token) as session:
145 session.LeaveCommunity(communities_pb2.LeaveCommunityReq(community_id=community_id))
148def search_communities(*, token: str, query: str, page_size: int | None = None) -> communities_pb2.SearchCommunitiesRes:
149 with _communities_session(token) as session:
150 res: communities_pb2.SearchCommunitiesRes = session.SearchCommunities(
151 communities_pb2.SearchCommunitiesReq(query=query, page_size=page_size)
152 )
153 return res
156def list_all_communities(*, token: str) -> communities_pb2.ListAllCommunitiesRes:
157 with _communities_session(token) as session:
158 res: communities_pb2.ListAllCommunitiesRes = session.ListAllCommunities(communities_pb2.ListAllCommunitiesReq())
159 return res
162def list_recent_communities(*, token: str, page_size: int | None = None) -> communities_pb2.ListRecentCommunitiesRes:
163 with _communities_session(token) as session:
164 res: communities_pb2.ListRecentCommunitiesRes = session.ListRecentCommunities(
165 communities_pb2.ListRecentCommunitiesReq(page_size=page_size)
166 )
167 return res