Coverage for src/tests/test_communities.py: 100%
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1from datetime import timedelta
3import grpc
4import pytest
5from google.protobuf import wrappers_pb2
7from couchers import errors
8from couchers.db import session_scope
9from couchers.materialized_views import refresh_materialized_views
10from couchers.models import (
11 Cluster,
12 ClusterRole,
13 ClusterSubscription,
14 Node,
15 Page,
16 PageType,
17 PageVersion,
18 SignupFlow,
19 Thread,
20)
21from couchers.sql import couchers_select as select
22from couchers.tasks import enforce_community_memberships
23from couchers.utils import Timestamp_from_datetime, create_coordinate, create_polygon_lat_lng, now, to_multi
24from proto import api_pb2, auth_pb2, communities_pb2, discussions_pb2, events_pb2, pages_pb2
25from tests.test_auth import get_session_cookie_token
26from tests.test_fixtures import ( # noqa
27 auth_api_session,
28 communities_session,
29 db,
30 discussions_session,
31 events_session,
32 generate_user,
33 get_user_id_and_token,
34 pages_session,
35 recreate_database,
36 testconfig,
37)
40@pytest.fixture(autouse=True)
41def _(testconfig):
42 pass
45# For testing purposes, restrict ourselves to a 1D-world, consisting of "intervals" that have width 2, and coordinates
46# that are points at (x, 1).
47# we'll stick to EPSG4326, even though it's not ideal, so don't use too large values, but it's around the equator, so
48# mostly fine
51def create_1d_polygon(lb, ub):
52 # given a lower bound and upper bound on x, creates the given interval
53 return create_polygon_lat_lng([[lb, 0], [lb, 2], [ub, 2], [ub, 0], [lb, 0]])
56def create_1d_point(x):
57 return create_coordinate(x, 1)
60def create_community(session, interval_lb, interval_ub, name, admins, extra_members, parent):
61 node = Node(
62 geom=to_multi(create_1d_polygon(interval_lb, interval_ub)),
63 parent_node=parent,
64 )
65 session.add(node)
66 cluster = Cluster(
67 name=f"{name}",
68 description=f"Description for {name}",
69 parent_node=node,
70 is_official_cluster=True,
71 )
72 session.add(cluster)
73 main_page = Page(
74 parent_node=cluster.parent_node,
75 creator_user_id=admins[0].id,
76 owner_cluster=cluster,
77 type=PageType.main_page,
78 thread=Thread(),
79 )
80 session.add(main_page)
81 page_version = PageVersion(
82 page=main_page,
83 editor_user_id=admins[0].id,
84 title=f"Main page for the {name} community",
85 content="There is nothing here yet...",
86 )
87 session.add(page_version)
88 for admin in admins:
89 cluster.cluster_subscriptions.append(
90 ClusterSubscription(
91 user_id=admin.id,
92 role=ClusterRole.admin,
93 )
94 )
95 for member in extra_members:
96 cluster.cluster_subscriptions.append(
97 ClusterSubscription(
98 user_id=member.id,
99 role=ClusterRole.member,
100 )
101 )
102 session.commit()
103 # other members will be added by enforce_community_memberships()
104 return node
107def create_group(session, name, admins, members, parent_community):
108 cluster = Cluster(
109 name=f"{name}",
110 description=f"Description for {name}",
111 parent_node=parent_community,
112 )
113 session.add(cluster)
114 main_page = Page(
115 parent_node=cluster.parent_node,
116 creator_user=admins[0],
117 owner_cluster=cluster,
118 type=PageType.main_page,
119 thread=Thread(),
120 )
121 session.add(main_page)
122 page_version = PageVersion(
123 page=main_page,
124 editor_user=admins[0],
125 title=f"Main page for the {name} community",
126 content="There is nothing here yet...",
127 )
128 session.add(page_version)
129 for admin in admins:
130 cluster.cluster_subscriptions.append(
131 ClusterSubscription(
132 user=admin,
133 role=ClusterRole.admin,
134 )
135 )
136 for member in members:
137 cluster.cluster_subscriptions.append(
138 ClusterSubscription(
139 user=member,
140 role=ClusterRole.member,
141 )
142 )
143 session.commit()
144 return cluster
147def create_place(token, title, content, address, x):
148 with pages_session(token) as api:
149 res = api.CreatePlace(
150 pages_pb2.CreatePlaceReq(
151 title=title,
152 content=content,
153 address=address,
154 location=pages_pb2.Coordinate(
155 lat=x,
156 lng=1,
157 ),
158 )
159 )
162def create_discussion(token, community_id, group_id, title, content):
163 # set group_id or community_id to None
164 with discussions_session(token) as api:
165 res = api.CreateDiscussion(
166 discussions_pb2.CreateDiscussionReq(
167 title=title,
168 content=content,
169 owner_community_id=community_id,
170 owner_group_id=group_id,
171 )
172 )
175def create_event(token, community_id, group_id, title, content, start_td):
176 with events_session(token) as api:
177 res = api.CreateEvent(
178 events_pb2.CreateEventReq(
179 title=title,
180 content=content,
181 offline_information=events_pb2.OfflineEventInformation(
182 address="Near Null Island",
183 lat=0.1,
184 lng=0.2,
185 ),
186 start_time=Timestamp_from_datetime(now() + start_td),
187 end_time=Timestamp_from_datetime(now() + start_td + timedelta(hours=2)),
188 timezone="UTC",
189 )
190 )
191 api.TransferEvent(
192 events_pb2.TransferEventReq(
193 event_id=res.event_id,
194 new_owner_community_id=community_id,
195 new_owner_group_id=group_id,
196 )
197 )
200def get_community_id(session, community_name):
201 return (
202 session.execute(select(Cluster).where(Cluster.is_official_cluster).where(Cluster.name == community_name))
203 .scalar_one()
204 .parent_node_id
205 )
208def get_group_id(session, group_name):
209 return (
210 session.execute(select(Cluster).where(~Cluster.is_official_cluster).where(Cluster.name == group_name))
211 .scalar_one()
212 .id
213 )
216@pytest.fixture(scope="class")
217def testing_communities():
218 recreate_database()
219 user1, token1 = generate_user(username="user1", geom=create_1d_point(1), geom_radius=0.1)
220 user2, token2 = generate_user(username="user2", geom=create_1d_point(2), geom_radius=0.1)
221 user3, token3 = generate_user(username="user3", geom=create_1d_point(3), geom_radius=0.1)
222 user4, token4 = generate_user(username="user4", geom=create_1d_point(8), geom_radius=0.1)
223 user5, token5 = generate_user(username="user5", geom=create_1d_point(6), geom_radius=0.1)
224 user6, token6 = generate_user(username="user6", geom=create_1d_point(65), geom_radius=0.1)
225 user7, token7 = generate_user(username="user7", geom=create_1d_point(80), geom_radius=0.1)
226 user8, token8 = generate_user(username="user8", geom=create_1d_point(51), geom_radius=0.1)
228 with session_scope() as session:
229 w = create_community(session, 0, 100, "Global", [user1, user3, user7], [], None)
230 c1 = create_community(session, 0, 50, "Country 1", [user1, user2], [], w)
231 c1r1 = create_community(session, 0, 10, "Country 1, Region 1", [user1, user2], [], c1)
232 c1r1c1 = create_community(session, 0, 5, "Country 1, Region 1, City 1", [user2], [], c1r1)
233 c1r1c2 = create_community(session, 7, 10, "Country 1, Region 1, City 2", [user4, user5], [user2], c1r1)
234 c1r2 = create_community(session, 20, 25, "Country 1, Region 2", [user2], [], c1)
235 c1r2c1 = create_community(session, 21, 23, "Country 1, Region 2, City 1", [user2], [], c1r2)
236 c2 = create_community(session, 52, 100, "Country 2", [user6, user7], [], w)
237 c2r1 = create_community(session, 52, 71, "Country 2, Region 1", [user6], [user8], c2)
238 c2r1c1 = create_community(session, 53, 70, "Country 2, Region 1, City 1", [user8], [], c2r1)
240 h = create_group(session, "Hitchhikers", [user1, user2], [user5, user8], w)
241 create_group(session, "Country 1, Region 1, Foodies", [user1], [user2, user4], c1r1)
242 create_group(session, "Country 1, Region 1, Skaters", [user2], [user1], c1r1)
243 create_group(session, "Country 1, Region 2, Foodies", [user2], [user4, user5], c1r2)
244 create_group(session, "Country 2, Region 1, Foodies", [user6], [user7], c2r1)
246 create_discussion(token1, w.id, None, "Discussion title 1", "Discussion content 1")
247 create_discussion(token3, w.id, None, "Discussion title 2", "Discussion content 2")
248 create_discussion(token3, w.id, None, "Discussion title 3", "Discussion content 3")
249 create_discussion(token3, w.id, None, "Discussion title 4", "Discussion content 4")
250 create_discussion(token3, w.id, None, "Discussion title 5", "Discussion content 5")
251 create_discussion(token3, w.id, None, "Discussion title 6", "Discussion content 6")
252 create_discussion(token4, c1r1c2.id, None, "Discussion title 7", "Discussion content 7")
253 create_discussion(token5, None, h.id, "Discussion title 8", "Discussion content 8")
254 create_discussion(token1, None, h.id, "Discussion title 9", "Discussion content 9")
255 create_discussion(token2, None, h.id, "Discussion title 10", "Discussion content 10")
256 create_discussion(token3, None, h.id, "Discussion title 11", "Discussion content 11")
257 create_discussion(token4, None, h.id, "Discussion title 12", "Discussion content 12")
258 create_discussion(token5, None, h.id, "Discussion title 13", "Discussion content 13")
259 create_discussion(token8, None, h.id, "Discussion title 14", "Discussion content 14")
261 create_event(token3, c1.id, None, "Event title 1", "Event content 1", timedelta(hours=1))
262 create_event(token1, c1.id, None, "Event title 2", "Event content 2", timedelta(hours=2))
263 create_event(token3, c1.id, None, "Event title 3", "Event content 3", timedelta(hours=3))
264 create_event(token1, c1.id, None, "Event title 4", "Event content 4", timedelta(hours=4))
265 create_event(token3, c1.id, None, "Event title 5", "Event content 5", timedelta(hours=5))
266 create_event(token1, c1.id, None, "Event title 6", "Event content 6", timedelta(hours=6))
267 create_event(token2, None, h.id, "Event title 7", "Event content 7", timedelta(hours=7))
268 create_event(token2, None, h.id, "Event title 8", "Event content 8", timedelta(hours=8))
269 create_event(token2, None, h.id, "Event title 9", "Event content 9", timedelta(hours=9))
270 create_event(token2, None, h.id, "Event title 10", "Event content 10", timedelta(hours=10))
271 create_event(token2, None, h.id, "Event title 11", "Event content 11", timedelta(hours=11))
272 create_event(token2, None, h.id, "Event title 12", "Event content 12", timedelta(hours=12))
274 enforce_community_memberships()
276 create_place(token1, "Country 1, Region 1, Attraction", "Place content", "Somewhere in c1r1", 6)
277 create_place(token2, "Country 1, Region 1, City 1, Attraction 1", "Place content", "Somewhere in c1r1c1", 3)
278 create_place(token2, "Country 1, Region 1, City 1, Attraction 2", "Place content", "Somewhere in c1r1c1", 4)
279 create_place(token8, "Global, Attraction", "Place content", "Somewhere in w", 51.5)
280 create_place(token6, "Country 2, Region 1, Attraction", "Place content", "Somewhere in c2r1", 59)
282 refresh_materialized_views()
284 yield
287class TestCommunities:
288 @staticmethod
289 def test_GetCommunity(testing_communities):
290 with session_scope() as session:
291 user2_id, token2 = get_user_id_and_token(session, "user2")
292 w_id = get_community_id(session, "Global")
293 c1_id = get_community_id(session, "Country 1")
294 c1r1_id = get_community_id(session, "Country 1, Region 1")
295 c1r1c1_id = get_community_id(session, "Country 1, Region 1, City 1")
296 c2_id = get_community_id(session, "Country 2")
298 with communities_session(token2) as api:
299 res = api.GetCommunity(
300 communities_pb2.GetCommunityReq(
301 community_id=w_id,
302 )
303 )
304 assert res.name == "Global"
305 assert res.slug == "global"
306 assert res.description == "Description for Global"
307 assert len(res.parents) == 1
308 assert res.parents[0].HasField("community")
309 assert res.parents[0].community.community_id == w_id
310 assert res.parents[0].community.name == "Global"
311 assert res.parents[0].community.slug == "global"
312 assert res.parents[0].community.description == "Description for Global"
313 assert res.main_page.type == pages_pb2.PAGE_TYPE_MAIN_PAGE
314 assert res.main_page.slug == "main-page-for-the-global-community"
315 assert res.main_page.last_editor_user_id == 1
316 assert res.main_page.creator_user_id == 1
317 assert res.main_page.owner_community_id == w_id
318 assert res.main_page.title == "Main page for the Global community"
319 assert res.main_page.content == "There is nothing here yet..."
320 assert not res.main_page.can_edit
321 assert not res.main_page.can_moderate
322 assert res.main_page.editor_user_ids == [1]
323 assert res.member
324 assert not res.admin
325 assert res.member_count == 8
326 assert res.admin_count == 3
328 res = api.GetCommunity(
329 communities_pb2.GetCommunityReq(
330 community_id=c1r1c1_id,
331 )
332 )
333 assert res.community_id == c1r1c1_id
334 assert res.name == "Country 1, Region 1, City 1"
335 assert res.slug == "country-1-region-1-city-1"
336 assert res.description == "Description for Country 1, Region 1, City 1"
337 assert len(res.parents) == 4
338 assert res.parents[0].HasField("community")
339 assert res.parents[0].community.community_id == w_id
340 assert res.parents[0].community.name == "Global"
341 assert res.parents[0].community.slug == "global"
342 assert res.parents[0].community.description == "Description for Global"
343 assert res.parents[1].HasField("community")
344 assert res.parents[1].community.community_id == c1_id
345 assert res.parents[1].community.name == "Country 1"
346 assert res.parents[1].community.slug == "country-1"
347 assert res.parents[1].community.description == "Description for Country 1"
348 assert res.parents[2].HasField("community")
349 assert res.parents[2].community.community_id == c1r1_id
350 assert res.parents[2].community.name == "Country 1, Region 1"
351 assert res.parents[2].community.slug == "country-1-region-1"
352 assert res.parents[2].community.description == "Description for Country 1, Region 1"
353 assert res.parents[3].HasField("community")
354 assert res.parents[3].community.community_id == c1r1c1_id
355 assert res.parents[3].community.name == "Country 1, Region 1, City 1"
356 assert res.parents[3].community.slug == "country-1-region-1-city-1"
357 assert res.parents[3].community.description == "Description for Country 1, Region 1, City 1"
358 assert res.main_page.type == pages_pb2.PAGE_TYPE_MAIN_PAGE
359 assert res.main_page.slug == "main-page-for-the-country-1-region-1-city-1-community"
360 assert res.main_page.last_editor_user_id == 2
361 assert res.main_page.creator_user_id == 2
362 assert res.main_page.owner_community_id == c1r1c1_id
363 assert res.main_page.title == "Main page for the Country 1, Region 1, City 1 community"
364 assert res.main_page.content == "There is nothing here yet..."
365 assert res.main_page.can_edit
366 assert res.main_page.can_moderate
367 assert res.main_page.editor_user_ids == [2]
368 assert res.member
369 assert res.admin
370 assert res.member_count == 3
371 assert res.admin_count == 1
373 res = api.GetCommunity(
374 communities_pb2.GetCommunityReq(
375 community_id=c2_id,
376 )
377 )
378 assert res.community_id == c2_id
379 assert res.name == "Country 2"
380 assert res.slug == "country-2"
381 assert res.description == "Description for Country 2"
382 assert len(res.parents) == 2
383 assert res.parents[0].HasField("community")
384 assert res.parents[0].community.community_id == w_id
385 assert res.parents[0].community.name == "Global"
386 assert res.parents[0].community.slug == "global"
387 assert res.parents[0].community.description == "Description for Global"
388 assert res.parents[1].HasField("community")
389 assert res.parents[1].community.community_id == c2_id
390 assert res.parents[1].community.name == "Country 2"
391 assert res.parents[1].community.slug == "country-2"
392 assert res.parents[1].community.description == "Description for Country 2"
393 assert res.main_page.type == pages_pb2.PAGE_TYPE_MAIN_PAGE
394 assert res.main_page.slug == "main-page-for-the-country-2-community"
395 assert res.main_page.last_editor_user_id == 6
396 assert res.main_page.creator_user_id == 6
397 assert res.main_page.owner_community_id == c2_id
398 assert res.main_page.title == "Main page for the Country 2 community"
399 assert res.main_page.content == "There is nothing here yet..."
400 assert not res.main_page.can_edit
401 assert not res.main_page.can_moderate
402 assert res.main_page.editor_user_ids == [6]
403 assert not res.member
404 assert not res.admin
405 assert res.member_count == 2
406 assert res.admin_count == 2
408 @staticmethod
409 def test_ListCommunities(testing_communities):
410 with session_scope() as session:
411 user1_id, token1 = get_user_id_and_token(session, "user1")
412 c1_id = get_community_id(session, "Country 1")
413 c1r1_id = get_community_id(session, "Country 1, Region 1")
414 c1r2_id = get_community_id(session, "Country 1, Region 2")
416 with communities_session(token1) as api:
417 res = api.ListCommunities(
418 communities_pb2.ListCommunitiesReq(
419 community_id=c1_id,
420 )
421 )
422 assert [c.community_id for c in res.communities] == [c1r1_id, c1r2_id]
424 @staticmethod
425 def test_ListCommunities_all(testing_communities):
426 with session_scope() as session:
427 user1_id, token1 = get_user_id_and_token(session, "user1")
428 w_id = get_community_id(session, "Global")
429 c1_id = get_community_id(session, "Country 1")
430 c1r1_id = get_community_id(session, "Country 1, Region 1")
431 c1r1c1_id = get_community_id(session, "Country 1, Region 1, City 1")
432 c1r1c2_id = get_community_id(session, "Country 1, Region 1, City 2")
433 c1r2_id = get_community_id(session, "Country 1, Region 2")
434 c1r2c1_id = get_community_id(session, "Country 1, Region 2, City 1")
435 c2_id = get_community_id(session, "Country 2")
436 c2r1_id = get_community_id(session, "Country 2, Region 1")
437 c2r1c1_id = get_community_id(session, "Country 2, Region 1, City 1")
439 with communities_session(token1) as api:
440 res = api.ListCommunities(
441 communities_pb2.ListCommunitiesReq(
442 page_size=5,
443 )
444 )
445 assert [c.community_id for c in res.communities] == [w_id, c1_id, c1r1_id, c1r1c1_id, c1r1c2_id]
446 res = api.ListCommunities(
447 communities_pb2.ListCommunitiesReq(
448 page_size=2,
449 page_token=res.next_page_token,
450 )
451 )
452 assert [c.community_id for c in res.communities] == [c1r2_id, c1r2c1_id]
453 res = api.ListCommunities(
454 communities_pb2.ListCommunitiesReq(
455 page_size=5,
456 page_token=res.next_page_token,
457 )
458 )
459 assert [c.community_id for c in res.communities] == [c2_id, c2r1_id, c2r1c1_id]
461 @staticmethod
462 def test_ListUserCommunities(testing_communities):
463 with session_scope() as session:
464 user2_id, token2 = get_user_id_and_token(session, "user2")
465 w_id = get_community_id(session, "Global")
466 c1_id = get_community_id(session, "Country 1")
467 c1r1_id = get_community_id(session, "Country 1, Region 1")
468 c1r1c1_id = get_community_id(session, "Country 1, Region 1, City 1")
469 c1r1c2_id = get_community_id(session, "Country 1, Region 1, City 2")
470 c1r2_id = get_community_id(session, "Country 1, Region 2")
471 c1r2c1_id = get_community_id(session, "Country 1, Region 2, City 1")
473 # Fetch user2's communities from user2's account
474 with communities_session(token2) as api:
475 res = api.ListUserCommunities(communities_pb2.ListUserCommunitiesReq())
476 assert [c.community_id for c in res.communities] == [
477 w_id,
478 c1_id,
479 c1r1_id,
480 c1r1c1_id,
481 c1r1c2_id,
482 c1r2_id,
483 c1r2c1_id,
484 ]
486 @staticmethod
487 def test_ListOtherUserCommunities(testing_communities):
488 with session_scope() as session:
489 user1_id, token1 = get_user_id_and_token(session, "user1")
490 user2_id, token2 = get_user_id_and_token(session, "user2")
491 w_id = get_community_id(session, "Global")
492 c1_id = get_community_id(session, "Country 1")
493 c1r1_id = get_community_id(session, "Country 1, Region 1")
494 c1r1c1_id = get_community_id(session, "Country 1, Region 1, City 1")
495 c1r1c2_id = get_community_id(session, "Country 1, Region 1, City 2")
496 c1r2_id = get_community_id(session, "Country 1, Region 2")
497 c1r2c1_id = get_community_id(session, "Country 1, Region 2, City 1")
499 # Fetch user2's communities from user1's account
500 with communities_session(token1) as api:
501 res = api.ListUserCommunities(communities_pb2.ListUserCommunitiesReq(user_id=user2_id))
502 assert [c.community_id for c in res.communities] == [
503 w_id,
504 c1_id,
505 c1r1_id,
506 c1r1c1_id,
507 c1r1c2_id,
508 c1r2_id,
509 c1r2c1_id,
510 ]
512 @staticmethod
513 def test_ListGroups(testing_communities):
514 with session_scope() as session:
515 user1_id, token1 = get_user_id_and_token(session, "user1")
516 user5_id, token5 = get_user_id_and_token(session, "user5")
517 w_id = get_community_id(session, "Global")
518 hitchhikers_id = get_group_id(session, "Hitchhikers")
519 c1r1_id = get_community_id(session, "Country 1, Region 1")
520 foodies_id = get_group_id(session, "Country 1, Region 1, Foodies")
521 skaters_id = get_group_id(session, "Country 1, Region 1, Skaters")
523 with communities_session(token1) as api:
524 res = api.ListGroups(
525 communities_pb2.ListGroupsReq(
526 community_id=c1r1_id,
527 )
528 )
529 assert [g.group_id for g in res.groups] == [foodies_id, skaters_id]
531 with communities_session(token5) as api:
532 res = api.ListGroups(
533 communities_pb2.ListGroupsReq(
534 community_id=w_id,
535 )
536 )
537 assert len(res.groups) == 1
538 assert res.groups[0].group_id == hitchhikers_id
540 @staticmethod
541 def test_ListAdmins(testing_communities):
542 with session_scope() as session:
543 user1_id, token1 = get_user_id_and_token(session, "user1")
544 user3_id, token3 = get_user_id_and_token(session, "user3")
545 user4_id, token4 = get_user_id_and_token(session, "user4")
546 user5_id, token5 = get_user_id_and_token(session, "user5")
547 user7_id, token7 = get_user_id_and_token(session, "user7")
548 w_id = get_community_id(session, "Global")
549 c1r1c2_id = get_community_id(session, "Country 1, Region 1, City 2")
551 with communities_session(token1) as api:
552 res = api.ListAdmins(
553 communities_pb2.ListAdminsReq(
554 community_id=w_id,
555 )
556 )
557 assert res.admin_user_ids == [user1_id, user3_id, user7_id]
559 res = api.ListAdmins(
560 communities_pb2.ListAdminsReq(
561 community_id=c1r1c2_id,
562 )
563 )
564 assert res.admin_user_ids == [user4_id, user5_id]
566 @staticmethod
567 def test_ListMembers(testing_communities):
568 with session_scope() as session:
569 user1_id, token1 = get_user_id_and_token(session, "user1")
570 user2_id, token2 = get_user_id_and_token(session, "user2")
571 user3_id, token3 = get_user_id_and_token(session, "user3")
572 user4_id, token4 = get_user_id_and_token(session, "user4")
573 user5_id, token5 = get_user_id_and_token(session, "user5")
574 user6_id, token6 = get_user_id_and_token(session, "user6")
575 user7_id, token7 = get_user_id_and_token(session, "user7")
576 user8_id, token8 = get_user_id_and_token(session, "user8")
577 w_id = get_community_id(session, "Global")
578 c1r1c2_id = get_community_id(session, "Country 1, Region 1, City 2")
580 with communities_session(token1) as api:
581 res = api.ListMembers(
582 communities_pb2.ListMembersReq(
583 community_id=w_id,
584 )
585 )
586 assert res.member_user_ids == [
587 user1_id,
588 user2_id,
589 user3_id,
590 user4_id,
591 user5_id,
592 user6_id,
593 user7_id,
594 user8_id,
595 ]
597 res = api.ListMembers(
598 communities_pb2.ListMembersReq(
599 community_id=c1r1c2_id,
600 )
601 )
602 assert res.member_user_ids == [user2_id, user4_id, user5_id]
604 @staticmethod
605 def test_ListNearbyUsers(testing_communities):
606 with session_scope() as session:
607 user1_id, token1 = get_user_id_and_token(session, "user1")
608 user2_id, token2 = get_user_id_and_token(session, "user2")
609 user3_id, token3 = get_user_id_and_token(session, "user3")
610 user4_id, token4 = get_user_id_and_token(session, "user4")
611 user5_id, token5 = get_user_id_and_token(session, "user5")
612 user6_id, token6 = get_user_id_and_token(session, "user6")
613 user7_id, token7 = get_user_id_and_token(session, "user7")
614 user8_id, token8 = get_user_id_and_token(session, "user8")
615 w_id = get_community_id(session, "Global")
616 c1r1c2_id = get_community_id(session, "Country 1, Region 1, City 2")
618 with communities_session(token1) as api:
619 res = api.ListNearbyUsers(
620 communities_pb2.ListNearbyUsersReq(
621 community_id=w_id,
622 )
623 )
624 assert res.nearby_user_ids == [
625 user1_id,
626 user2_id,
627 user3_id,
628 user4_id,
629 user5_id,
630 user6_id,
631 user7_id,
632 user8_id,
633 ]
635 res = api.ListNearbyUsers(
636 communities_pb2.ListNearbyUsersReq(
637 community_id=c1r1c2_id,
638 )
639 )
640 assert res.nearby_user_ids == [user4_id]
642 @staticmethod
643 def test_ListDiscussions(testing_communities):
644 with session_scope() as session:
645 user1_id, token1 = get_user_id_and_token(session, "user1")
646 w_id = get_community_id(session, "Global")
647 c1r1c2_id = get_community_id(session, "Country 1, Region 1, City 2")
649 with communities_session(token1) as api:
650 res = api.ListDiscussions(
651 communities_pb2.ListDiscussionsReq(
652 community_id=w_id,
653 page_size=3,
654 )
655 )
656 assert [d.title for d in res.discussions] == [
657 "Discussion title 6",
658 "Discussion title 5",
659 "Discussion title 4",
660 ]
661 for d in res.discussions:
662 assert d.thread.thread_id > 0
663 assert d.thread.num_responses == 0
665 res = api.ListDiscussions(
666 communities_pb2.ListDiscussionsReq(
667 community_id=w_id,
668 page_token=res.next_page_token,
669 page_size=2,
670 )
671 )
672 assert [d.title for d in res.discussions] == [
673 "Discussion title 3",
674 "Discussion title 2",
675 ]
676 for d in res.discussions:
677 assert d.thread.thread_id > 0
678 assert d.thread.num_responses == 0
680 res = api.ListDiscussions(
681 communities_pb2.ListDiscussionsReq(
682 community_id=w_id,
683 page_token=res.next_page_token,
684 page_size=2,
685 )
686 )
687 assert [d.title for d in res.discussions] == [
688 "Discussion title 1",
689 ]
690 for d in res.discussions:
691 assert d.thread.thread_id > 0
692 assert d.thread.num_responses == 0
694 res = api.ListDiscussions(
695 communities_pb2.ListDiscussionsReq(
696 community_id=c1r1c2_id,
697 )
698 )
699 assert [d.title for d in res.discussions] == [
700 "Discussion title 7",
701 ]
702 for d in res.discussions:
703 assert d.thread.thread_id > 0
704 assert d.thread.num_responses == 0
706 @staticmethod
707 def test_node_contained_user_ids_association_proxy(testing_communities):
708 with session_scope() as session:
709 c1_id = get_community_id(session, "Country 1")
710 node = session.execute(select(Node).where(Node.id == c1_id)).scalar_one_or_none()
711 assert node.contained_user_ids == [1, 2, 3, 4, 5]
712 assert len(node.contained_user_ids) == len(node.contained_users)
714 @staticmethod
715 def test_ListEvents(testing_communities):
716 with session_scope() as session:
717 user1_id, token1 = get_user_id_and_token(session, "user1")
718 c1_id = get_community_id(session, "Country 1")
720 with communities_session(token1) as api:
721 res = api.ListEvents(
722 communities_pb2.ListEventsReq(
723 community_id=c1_id,
724 page_size=3,
725 )
726 )
727 assert [d.title for d in res.events] == [
728 "Event title 1",
729 "Event title 2",
730 "Event title 3",
731 ]
733 res = api.ListEvents(
734 communities_pb2.ListEventsReq(
735 community_id=c1_id,
736 page_token=res.next_page_token,
737 page_size=2,
738 )
739 )
740 assert [d.title for d in res.events] == [
741 "Event title 4",
742 "Event title 5",
743 ]
745 res = api.ListEvents(
746 communities_pb2.ListEventsReq(
747 community_id=c1_id,
748 page_token=res.next_page_token,
749 page_size=2,
750 )
751 )
752 assert [d.title for d in res.events] == [
753 "Event title 6",
754 ]
755 assert not res.next_page_token
758def test_JoinCommunity_and_LeaveCommunity(testing_communities):
759 # these are separate as they mutate the database
760 with session_scope() as session:
761 # at x=1, inside c1 (country 1)
762 user1_id, token1 = get_user_id_and_token(session, "user1")
763 # at x=51, not inside c1
764 user8_id, token8 = get_user_id_and_token(session, "user8")
765 c1_id = get_community_id(session, "Country 1")
767 with communities_session(token1) as api:
768 assert api.GetCommunity(communities_pb2.GetCommunityReq(community_id=c1_id)).member
770 # user1 is already part of c1, cannot join
771 with pytest.raises(grpc.RpcError) as e:
772 res = api.JoinCommunity(
773 communities_pb2.JoinCommunityReq(
774 community_id=c1_id,
775 )
776 )
777 assert e.value.code() == grpc.StatusCode.FAILED_PRECONDITION
778 assert e.value.details() == errors.ALREADY_IN_COMMUNITY
780 assert api.GetCommunity(communities_pb2.GetCommunityReq(community_id=c1_id)).member
782 # user1 is inside c1, cannot leave
783 with pytest.raises(grpc.RpcError) as e:
784 res = api.LeaveCommunity(
785 communities_pb2.LeaveCommunityReq(
786 community_id=c1_id,
787 )
788 )
789 assert e.value.code() == grpc.StatusCode.FAILED_PRECONDITION
790 assert e.value.details() == errors.CANNOT_LEAVE_CONTAINING_COMMUNITY
792 assert api.GetCommunity(communities_pb2.GetCommunityReq(community_id=c1_id)).member
794 with communities_session(token8) as api:
795 assert not api.GetCommunity(communities_pb2.GetCommunityReq(community_id=c1_id)).member
797 # user8 is not in c1 yet, cannot leave
798 with pytest.raises(grpc.RpcError) as e:
799 res = api.LeaveCommunity(
800 communities_pb2.LeaveCommunityReq(
801 community_id=c1_id,
802 )
803 )
804 assert e.value.code() == grpc.StatusCode.FAILED_PRECONDITION
805 assert e.value.details() == errors.NOT_IN_COMMUNITY
807 assert not api.GetCommunity(communities_pb2.GetCommunityReq(community_id=c1_id)).member
809 # user8 is not in c1 and not part, can join
810 res = api.JoinCommunity(
811 communities_pb2.JoinCommunityReq(
812 community_id=c1_id,
813 )
814 )
816 assert api.GetCommunity(communities_pb2.GetCommunityReq(community_id=c1_id)).member
818 # user8 is not in c1 and but now part, can't join again
819 with pytest.raises(grpc.RpcError) as e:
820 res = api.JoinCommunity(
821 communities_pb2.JoinCommunityReq(
822 community_id=c1_id,
823 )
824 )
825 assert e.value.code() == grpc.StatusCode.FAILED_PRECONDITION
826 assert e.value.details() == errors.ALREADY_IN_COMMUNITY
828 assert api.GetCommunity(communities_pb2.GetCommunityReq(community_id=c1_id)).member
830 # user8 is not in c1 yet, but part of it, can leave
831 res = api.LeaveCommunity(
832 communities_pb2.LeaveCommunityReq(
833 community_id=c1_id,
834 )
835 )
836 assert not api.GetCommunity(communities_pb2.GetCommunityReq(community_id=c1_id)).member
839def test_LeaveCommunity_regression(db):
840 # See github issue #1444, repro:
841 # 1. Join more than one community
842 # 2. Leave one of them
843 # 3. You are no longer in any community
844 # admin
845 user1, token1 = generate_user(username="user1", geom=create_1d_point(200), geom_radius=0.1)
846 # joiner/leaver
847 user2, token2 = generate_user(username="user2", geom=create_1d_point(201), geom_radius=0.1)
849 with session_scope() as session:
850 c0 = create_community(session, 0, 100, "Community 0", [user1], [], None)
851 c1 = create_community(session, 0, 50, "Community 1", [user1], [], c0)
852 c2 = create_community(session, 0, 10, "Community 2", [user1], [], c0)
853 c0_id = c0.id
854 c1_id = c1.id
855 c2_id = c2.id
857 enforce_community_memberships()
859 with communities_session(token1) as api:
860 assert api.GetCommunity(communities_pb2.GetCommunityReq(community_id=c0_id)).member
861 assert api.GetCommunity(communities_pb2.GetCommunityReq(community_id=c1_id)).member
862 assert api.GetCommunity(communities_pb2.GetCommunityReq(community_id=c2_id)).member
864 with communities_session(token2) as api:
865 # first check we're not in any communities
866 assert not api.GetCommunity(communities_pb2.GetCommunityReq(community_id=c0_id)).member
867 assert not api.GetCommunity(communities_pb2.GetCommunityReq(community_id=c1_id)).member
868 assert not api.GetCommunity(communities_pb2.GetCommunityReq(community_id=c2_id)).member
870 # join some communities
871 api.JoinCommunity(communities_pb2.JoinCommunityReq(community_id=c1_id))
872 api.JoinCommunity(communities_pb2.JoinCommunityReq(community_id=c2_id))
874 # check memberships
875 assert not api.GetCommunity(communities_pb2.GetCommunityReq(community_id=c0_id)).member
876 assert api.GetCommunity(communities_pb2.GetCommunityReq(community_id=c1_id)).member
877 assert api.GetCommunity(communities_pb2.GetCommunityReq(community_id=c2_id)).member
879 # leave just c2
880 api.LeaveCommunity(communities_pb2.LeaveCommunityReq(community_id=c2_id))
882 # check memberships
883 assert not api.GetCommunity(communities_pb2.GetCommunityReq(community_id=c0_id)).member
884 assert api.GetCommunity(communities_pb2.GetCommunityReq(community_id=c1_id)).member
885 assert not api.GetCommunity(communities_pb2.GetCommunityReq(community_id=c2_id)).member
888def test_enforce_community_memberships_for_user(testing_communities):
889 """
890 Make sure the user is added to the right communities on signup
891 """
892 with auth_api_session() as (auth_api, metadata_interceptor):
893 res = auth_api.SignupFlow(
894 auth_pb2.SignupFlowReq(
895 basic=auth_pb2.SignupBasic(name="testing", email="email@couchers.org.invalid"),
896 account=auth_pb2.SignupAccount(
897 username="frodo",
898 password="a very insecure password",
899 birthdate="1970-01-01",
900 gender="Bot",
901 hosting_status=api_pb2.HOSTING_STATUS_CAN_HOST,
902 city="Country 1, Region 1, City 2",
903 # lat=8, lng=1 is equivalent to creating this coordinate with create_coordinate(8)
904 lat=8,
905 lng=1,
906 radius=500,
907 accept_tos=True,
908 ),
909 feedback=auth_pb2.ContributorForm(),
910 accept_community_guidelines=wrappers_pb2.BoolValue(value=True),
911 )
912 )
913 with session_scope() as session:
914 email_token = (
915 session.execute(select(SignupFlow).where(SignupFlow.flow_token == res.flow_token)).scalar_one().email_token
916 )
917 with auth_api_session() as (auth_api, metadata_interceptor):
918 res = auth_api.SignupFlow(auth_pb2.SignupFlowReq(email_token=email_token))
919 user_id = res.auth_res.user_id
921 # now check the user is in the right communities
922 with session_scope() as session:
923 w_id = get_community_id(session, "Global")
924 c1_id = get_community_id(session, "Country 1")
925 c1r1_id = get_community_id(session, "Country 1, Region 1")
926 c1r1c2_id = get_community_id(session, "Country 1, Region 1, City 2")
928 token = get_session_cookie_token(metadata_interceptor)
930 with communities_session(token) as api:
931 res = api.ListUserCommunities(communities_pb2.ListUserCommunitiesReq())
932 assert [c.community_id for c in res.communities] == [w_id, c1_id, c1r1_id, c1r1c2_id]
935# TODO: requires transferring of content
937# def test_ListPlaces(db, testing_communities):
938# pass
940# def test_ListGuides(db, testing_communities):
941# pass
943# def test_ListEvents(db, testing_communities):
944# pass