Coverage for src/tests/test_communities.py: 100%

538 statements  

« prev     ^ index     » next       coverage.py v7.5.0, created at 2024-07-22 17:19 +0000

1from datetime import timedelta 

2 

3import grpc 

4import pytest 

5from google.protobuf import wrappers_pb2 

6 

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) 

38 

39 

40@pytest.fixture(autouse=True) 

41def _(testconfig): 

42 pass 

43 

44 

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 

49 

50 

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]]) 

54 

55 

56def create_1d_point(x): 

57 return create_coordinate(x, 1) 

58 

59 

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 

105 

106 

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 

145 

146 

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 ) 

160 

161 

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 ) 

173 

174 

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 ) 

198 

199 

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 ) 

206 

207 

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 ) 

214 

215 

216@pytest.fixture(scope="class") 

217def testing_communities(testconfig): 

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) 

227 

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) 

239 

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) 

245 

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") 

260 

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)) 

273 

274 enforce_community_memberships() 

275 

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) 

281 

282 refresh_materialized_views() 

283 

284 yield 

285 

286 

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") 

297 

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 

327 

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 

372 

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 

407 

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") 

415 

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] 

423 

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") 

438 

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] 

460 

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") 

472 

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 ] 

485 

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") 

498 

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 ] 

511 

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") 

522 

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] 

530 

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 

539 

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") 

550 

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] 

558 

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] 

565 

566 @staticmethod 

567 def test_AddAdmin(testing_communities): 

568 with session_scope() as session: 

569 user4_id, token4 = get_user_id_and_token(session, "user4") 

570 user5_id, _ = get_user_id_and_token(session, "user5") 

571 user2_id, _ = get_user_id_and_token(session, "user2") 

572 user8_id, token8 = get_user_id_and_token(session, "user8") 

573 node_id = get_community_id(session, "Country 1, Region 1, City 2") 

574 

575 with communities_session(token8) as api: 

576 with pytest.raises(grpc.RpcError) as err: 

577 api.AddAdmin(communities_pb2.AddAdminReq(community_id=node_id, user_id=user2_id)) 

578 assert err.value.code() == grpc.StatusCode.FAILED_PRECONDITION 

579 assert err.value.details() == errors.NODE_MODERATE_PERMISSION_DENIED 

580 

581 with communities_session(token4) as api: 

582 res = api.ListAdmins(communities_pb2.ListAdminsReq(community_id=node_id)) 

583 assert res.admin_user_ids == [user4_id, user5_id] 

584 

585 with pytest.raises(grpc.RpcError) as err: 

586 api.AddAdmin(communities_pb2.AddAdminReq(community_id=node_id, user_id=user8_id)) 

587 assert err.value.code() == grpc.StatusCode.FAILED_PRECONDITION 

588 assert err.value.details() == errors.USER_NOT_MEMBER 

589 

590 with pytest.raises(grpc.RpcError) as err: 

591 api.AddAdmin(communities_pb2.AddAdminReq(community_id=node_id, user_id=user5_id)) 

592 assert err.value.code() == grpc.StatusCode.FAILED_PRECONDITION 

593 assert err.value.details() == errors.USER_ALREADY_ADMIN 

594 

595 api.AddAdmin(communities_pb2.AddAdminReq(community_id=node_id, user_id=user2_id)) 

596 res = api.ListAdmins(communities_pb2.ListAdminsReq(community_id=node_id)) 

597 assert res.admin_user_ids == [user2_id, user4_id, user5_id] 

598 # Cleanup because database changes do not roll back 

599 api.RemoveAdmin(communities_pb2.RemoveAdminReq(community_id=node_id, user_id=user2_id)) 

600 

601 @staticmethod 

602 def test_RemoveAdmin(testing_communities): 

603 with session_scope() as session: 

604 user4_id, token4 = get_user_id_and_token(session, "user4") 

605 user5_id, _ = get_user_id_and_token(session, "user5") 

606 user2_id, _ = get_user_id_and_token(session, "user2") 

607 user8_id, token8 = get_user_id_and_token(session, "user8") 

608 node_id = get_community_id(session, "Country 1, Region 1, City 2") 

609 

610 with communities_session(token8) as api: 

611 with pytest.raises(grpc.RpcError) as err: 

612 api.AddAdmin(communities_pb2.AddAdminReq(community_id=node_id, user_id=user2_id)) 

613 assert err.value.code() == grpc.StatusCode.FAILED_PRECONDITION 

614 assert err.value.details() == errors.NODE_MODERATE_PERMISSION_DENIED 

615 

616 with communities_session(token4) as api: 

617 res = api.ListAdmins(communities_pb2.ListAdminsReq(community_id=node_id)) 

618 assert res.admin_user_ids == [user4_id, user5_id] 

619 

620 with pytest.raises(grpc.RpcError) as err: 

621 api.RemoveAdmin(communities_pb2.RemoveAdminReq(community_id=node_id, user_id=user8_id)) 

622 assert err.value.code() == grpc.StatusCode.FAILED_PRECONDITION 

623 assert err.value.details() == errors.USER_NOT_MEMBER 

624 

625 with pytest.raises(grpc.RpcError) as err: 

626 api.RemoveAdmin(communities_pb2.RemoveAdminReq(community_id=node_id, user_id=user2_id)) 

627 assert err.value.code() == grpc.StatusCode.FAILED_PRECONDITION 

628 assert err.value.details() == errors.USER_NOT_ADMIN 

629 

630 api.RemoveAdmin(communities_pb2.RemoveAdminReq(community_id=node_id, user_id=user5_id)) 

631 res = api.ListAdmins(communities_pb2.ListAdminsReq(community_id=node_id)) 

632 assert res.admin_user_ids == [user4_id] 

633 # Cleanup because database changes do not roll back 

634 api.AddAdmin(communities_pb2.AddAdminReq(community_id=node_id, user_id=user5_id)) 

635 

636 @staticmethod 

637 def test_ListMembers(testing_communities): 

638 with session_scope() as session: 

639 user1_id, token1 = get_user_id_and_token(session, "user1") 

640 user2_id, token2 = get_user_id_and_token(session, "user2") 

641 user3_id, token3 = get_user_id_and_token(session, "user3") 

642 user4_id, token4 = get_user_id_and_token(session, "user4") 

643 user5_id, token5 = get_user_id_and_token(session, "user5") 

644 user6_id, token6 = get_user_id_and_token(session, "user6") 

645 user7_id, token7 = get_user_id_and_token(session, "user7") 

646 user8_id, token8 = get_user_id_and_token(session, "user8") 

647 w_id = get_community_id(session, "Global") 

648 c1r1c2_id = get_community_id(session, "Country 1, Region 1, City 2") 

649 

650 with communities_session(token1) as api: 

651 res = api.ListMembers( 

652 communities_pb2.ListMembersReq( 

653 community_id=w_id, 

654 ) 

655 ) 

656 assert res.member_user_ids == [ 

657 user1_id, 

658 user2_id, 

659 user3_id, 

660 user4_id, 

661 user5_id, 

662 user6_id, 

663 user7_id, 

664 user8_id, 

665 ] 

666 

667 res = api.ListMembers( 

668 communities_pb2.ListMembersReq( 

669 community_id=c1r1c2_id, 

670 ) 

671 ) 

672 assert res.member_user_ids == [user2_id, user4_id, user5_id] 

673 

674 @staticmethod 

675 def test_ListNearbyUsers(testing_communities): 

676 with session_scope() as session: 

677 user1_id, token1 = get_user_id_and_token(session, "user1") 

678 user2_id, token2 = get_user_id_and_token(session, "user2") 

679 user3_id, token3 = get_user_id_and_token(session, "user3") 

680 user4_id, token4 = get_user_id_and_token(session, "user4") 

681 user5_id, token5 = get_user_id_and_token(session, "user5") 

682 user6_id, token6 = get_user_id_and_token(session, "user6") 

683 user7_id, token7 = get_user_id_and_token(session, "user7") 

684 user8_id, token8 = get_user_id_and_token(session, "user8") 

685 w_id = get_community_id(session, "Global") 

686 c1r1c2_id = get_community_id(session, "Country 1, Region 1, City 2") 

687 

688 with communities_session(token1) as api: 

689 res = api.ListNearbyUsers( 

690 communities_pb2.ListNearbyUsersReq( 

691 community_id=w_id, 

692 ) 

693 ) 

694 assert res.nearby_user_ids == [ 

695 user1_id, 

696 user2_id, 

697 user3_id, 

698 user4_id, 

699 user5_id, 

700 user6_id, 

701 user7_id, 

702 user8_id, 

703 ] 

704 

705 res = api.ListNearbyUsers( 

706 communities_pb2.ListNearbyUsersReq( 

707 community_id=c1r1c2_id, 

708 ) 

709 ) 

710 assert res.nearby_user_ids == [user4_id] 

711 

712 @staticmethod 

713 def test_ListDiscussions(testing_communities): 

714 with session_scope() as session: 

715 user1_id, token1 = get_user_id_and_token(session, "user1") 

716 w_id = get_community_id(session, "Global") 

717 c1r1c2_id = get_community_id(session, "Country 1, Region 1, City 2") 

718 

719 with communities_session(token1) as api: 

720 res = api.ListDiscussions( 

721 communities_pb2.ListDiscussionsReq( 

722 community_id=w_id, 

723 page_size=3, 

724 ) 

725 ) 

726 assert [d.title for d in res.discussions] == [ 

727 "Discussion title 6", 

728 "Discussion title 5", 

729 "Discussion title 4", 

730 ] 

731 for d in res.discussions: 

732 assert d.thread.thread_id > 0 

733 assert d.thread.num_responses == 0 

734 

735 res = api.ListDiscussions( 

736 communities_pb2.ListDiscussionsReq( 

737 community_id=w_id, 

738 page_token=res.next_page_token, 

739 page_size=2, 

740 ) 

741 ) 

742 assert [d.title for d in res.discussions] == [ 

743 "Discussion title 3", 

744 "Discussion title 2", 

745 ] 

746 for d in res.discussions: 

747 assert d.thread.thread_id > 0 

748 assert d.thread.num_responses == 0 

749 

750 res = api.ListDiscussions( 

751 communities_pb2.ListDiscussionsReq( 

752 community_id=w_id, 

753 page_token=res.next_page_token, 

754 page_size=2, 

755 ) 

756 ) 

757 assert [d.title for d in res.discussions] == [ 

758 "Discussion title 1", 

759 ] 

760 for d in res.discussions: 

761 assert d.thread.thread_id > 0 

762 assert d.thread.num_responses == 0 

763 

764 res = api.ListDiscussions( 

765 communities_pb2.ListDiscussionsReq( 

766 community_id=c1r1c2_id, 

767 ) 

768 ) 

769 assert [d.title for d in res.discussions] == [ 

770 "Discussion title 7", 

771 ] 

772 for d in res.discussions: 

773 assert d.thread.thread_id > 0 

774 assert d.thread.num_responses == 0 

775 

776 @staticmethod 

777 def test_node_contained_user_ids_association_proxy(testing_communities): 

778 with session_scope() as session: 

779 c1_id = get_community_id(session, "Country 1") 

780 node = session.execute(select(Node).where(Node.id == c1_id)).scalar_one_or_none() 

781 assert node.contained_user_ids == [1, 2, 3, 4, 5] 

782 assert len(node.contained_user_ids) == len(node.contained_users) 

783 

784 @staticmethod 

785 def test_ListEvents(testing_communities): 

786 with session_scope() as session: 

787 user1_id, token1 = get_user_id_and_token(session, "user1") 

788 c1_id = get_community_id(session, "Country 1") 

789 

790 with communities_session(token1) as api: 

791 res = api.ListEvents( 

792 communities_pb2.ListEventsReq( 

793 community_id=c1_id, 

794 page_size=3, 

795 ) 

796 ) 

797 assert [d.title for d in res.events] == [ 

798 "Event title 1", 

799 "Event title 2", 

800 "Event title 3", 

801 ] 

802 

803 res = api.ListEvents( 

804 communities_pb2.ListEventsReq( 

805 community_id=c1_id, 

806 page_token=res.next_page_token, 

807 page_size=2, 

808 ) 

809 ) 

810 assert [d.title for d in res.events] == [ 

811 "Event title 4", 

812 "Event title 5", 

813 ] 

814 

815 res = api.ListEvents( 

816 communities_pb2.ListEventsReq( 

817 community_id=c1_id, 

818 page_token=res.next_page_token, 

819 page_size=2, 

820 ) 

821 ) 

822 assert [d.title for d in res.events] == [ 

823 "Event title 6", 

824 ] 

825 assert not res.next_page_token 

826 

827 

828def test_JoinCommunity_and_LeaveCommunity(testing_communities): 

829 # these are separate as they mutate the database 

830 with session_scope() as session: 

831 # at x=1, inside c1 (country 1) 

832 user1_id, token1 = get_user_id_and_token(session, "user1") 

833 # at x=51, not inside c1 

834 user8_id, token8 = get_user_id_and_token(session, "user8") 

835 c1_id = get_community_id(session, "Country 1") 

836 

837 with communities_session(token1) as api: 

838 assert api.GetCommunity(communities_pb2.GetCommunityReq(community_id=c1_id)).member 

839 

840 # user1 is already part of c1, cannot join 

841 with pytest.raises(grpc.RpcError) as e: 

842 res = api.JoinCommunity( 

843 communities_pb2.JoinCommunityReq( 

844 community_id=c1_id, 

845 ) 

846 ) 

847 assert e.value.code() == grpc.StatusCode.FAILED_PRECONDITION 

848 assert e.value.details() == errors.ALREADY_IN_COMMUNITY 

849 

850 assert api.GetCommunity(communities_pb2.GetCommunityReq(community_id=c1_id)).member 

851 

852 # user1 is inside c1, cannot leave 

853 with pytest.raises(grpc.RpcError) as e: 

854 res = api.LeaveCommunity( 

855 communities_pb2.LeaveCommunityReq( 

856 community_id=c1_id, 

857 ) 

858 ) 

859 assert e.value.code() == grpc.StatusCode.FAILED_PRECONDITION 

860 assert e.value.details() == errors.CANNOT_LEAVE_CONTAINING_COMMUNITY 

861 

862 assert api.GetCommunity(communities_pb2.GetCommunityReq(community_id=c1_id)).member 

863 

864 with communities_session(token8) as api: 

865 assert not api.GetCommunity(communities_pb2.GetCommunityReq(community_id=c1_id)).member 

866 

867 # user8 is not in c1 yet, cannot leave 

868 with pytest.raises(grpc.RpcError) as e: 

869 res = api.LeaveCommunity( 

870 communities_pb2.LeaveCommunityReq( 

871 community_id=c1_id, 

872 ) 

873 ) 

874 assert e.value.code() == grpc.StatusCode.FAILED_PRECONDITION 

875 assert e.value.details() == errors.NOT_IN_COMMUNITY 

876 

877 assert not api.GetCommunity(communities_pb2.GetCommunityReq(community_id=c1_id)).member 

878 

879 # user8 is not in c1 and not part, can join 

880 res = api.JoinCommunity( 

881 communities_pb2.JoinCommunityReq( 

882 community_id=c1_id, 

883 ) 

884 ) 

885 

886 assert api.GetCommunity(communities_pb2.GetCommunityReq(community_id=c1_id)).member 

887 

888 # user8 is not in c1 and but now part, can't join again 

889 with pytest.raises(grpc.RpcError) as e: 

890 res = api.JoinCommunity( 

891 communities_pb2.JoinCommunityReq( 

892 community_id=c1_id, 

893 ) 

894 ) 

895 assert e.value.code() == grpc.StatusCode.FAILED_PRECONDITION 

896 assert e.value.details() == errors.ALREADY_IN_COMMUNITY 

897 

898 assert api.GetCommunity(communities_pb2.GetCommunityReq(community_id=c1_id)).member 

899 

900 # user8 is not in c1 yet, but part of it, can leave 

901 res = api.LeaveCommunity( 

902 communities_pb2.LeaveCommunityReq( 

903 community_id=c1_id, 

904 ) 

905 ) 

906 assert not api.GetCommunity(communities_pb2.GetCommunityReq(community_id=c1_id)).member 

907 

908 

909def test_LeaveCommunity_regression(db): 

910 # See github issue #1444, repro: 

911 # 1. Join more than one community 

912 # 2. Leave one of them 

913 # 3. You are no longer in any community 

914 # admin 

915 user1, token1 = generate_user(username="user1", geom=create_1d_point(200), geom_radius=0.1) 

916 # joiner/leaver 

917 user2, token2 = generate_user(username="user2", geom=create_1d_point(201), geom_radius=0.1) 

918 

919 with session_scope() as session: 

920 c0 = create_community(session, 0, 100, "Community 0", [user1], [], None) 

921 c1 = create_community(session, 0, 50, "Community 1", [user1], [], c0) 

922 c2 = create_community(session, 0, 10, "Community 2", [user1], [], c0) 

923 c0_id = c0.id 

924 c1_id = c1.id 

925 c2_id = c2.id 

926 

927 enforce_community_memberships() 

928 

929 with communities_session(token1) as api: 

930 assert api.GetCommunity(communities_pb2.GetCommunityReq(community_id=c0_id)).member 

931 assert api.GetCommunity(communities_pb2.GetCommunityReq(community_id=c1_id)).member 

932 assert api.GetCommunity(communities_pb2.GetCommunityReq(community_id=c2_id)).member 

933 

934 with communities_session(token2) as api: 

935 # first check we're not in any communities 

936 assert not api.GetCommunity(communities_pb2.GetCommunityReq(community_id=c0_id)).member 

937 assert not api.GetCommunity(communities_pb2.GetCommunityReq(community_id=c1_id)).member 

938 assert not api.GetCommunity(communities_pb2.GetCommunityReq(community_id=c2_id)).member 

939 

940 # join some communities 

941 api.JoinCommunity(communities_pb2.JoinCommunityReq(community_id=c1_id)) 

942 api.JoinCommunity(communities_pb2.JoinCommunityReq(community_id=c2_id)) 

943 

944 # check memberships 

945 assert not api.GetCommunity(communities_pb2.GetCommunityReq(community_id=c0_id)).member 

946 assert api.GetCommunity(communities_pb2.GetCommunityReq(community_id=c1_id)).member 

947 assert api.GetCommunity(communities_pb2.GetCommunityReq(community_id=c2_id)).member 

948 

949 # leave just c2 

950 api.LeaveCommunity(communities_pb2.LeaveCommunityReq(community_id=c2_id)) 

951 

952 # check memberships 

953 assert not api.GetCommunity(communities_pb2.GetCommunityReq(community_id=c0_id)).member 

954 assert api.GetCommunity(communities_pb2.GetCommunityReq(community_id=c1_id)).member 

955 assert not api.GetCommunity(communities_pb2.GetCommunityReq(community_id=c2_id)).member 

956 

957 

958def test_enforce_community_memberships_for_user(testing_communities): 

959 """ 

960 Make sure the user is added to the right communities on signup 

961 """ 

962 with auth_api_session() as (auth_api, metadata_interceptor): 

963 res = auth_api.SignupFlow( 

964 auth_pb2.SignupFlowReq( 

965 basic=auth_pb2.SignupBasic(name="testing", email="email@couchers.org.invalid"), 

966 account=auth_pb2.SignupAccount( 

967 username="frodo", 

968 password="a very insecure password", 

969 birthdate="1970-01-01", 

970 gender="Bot", 

971 hosting_status=api_pb2.HOSTING_STATUS_CAN_HOST, 

972 city="Country 1, Region 1, City 2", 

973 # lat=8, lng=1 is equivalent to creating this coordinate with create_coordinate(8) 

974 lat=8, 

975 lng=1, 

976 radius=500, 

977 accept_tos=True, 

978 ), 

979 feedback=auth_pb2.ContributorForm(), 

980 accept_community_guidelines=wrappers_pb2.BoolValue(value=True), 

981 ) 

982 ) 

983 with session_scope() as session: 

984 email_token = ( 

985 session.execute(select(SignupFlow).where(SignupFlow.flow_token == res.flow_token)).scalar_one().email_token 

986 ) 

987 with auth_api_session() as (auth_api, metadata_interceptor): 

988 res = auth_api.SignupFlow(auth_pb2.SignupFlowReq(email_token=email_token)) 

989 user_id = res.auth_res.user_id 

990 

991 # now check the user is in the right communities 

992 with session_scope() as session: 

993 w_id = get_community_id(session, "Global") 

994 c1_id = get_community_id(session, "Country 1") 

995 c1r1_id = get_community_id(session, "Country 1, Region 1") 

996 c1r1c2_id = get_community_id(session, "Country 1, Region 1, City 2") 

997 

998 token = get_session_cookie_token(metadata_interceptor) 

999 

1000 with communities_session(token) as api: 

1001 res = api.ListUserCommunities(communities_pb2.ListUserCommunitiesReq()) 

1002 assert [c.community_id for c in res.communities] == [w_id, c1_id, c1r1_id, c1r1c2_id] 

1003 

1004 

1005# TODO: requires transferring of content 

1006 

1007# def test_ListPlaces(db, testing_communities): 

1008# pass 

1009 

1010# def test_ListGuides(db, testing_communities): 

1011# pass 

1012 

1013# def test_ListEvents(db, testing_communities): 

1014# pass