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

507 statements  

« prev     ^ index     » next       coverage.py v7.5.0, created at 2024-12-20 18:03 +0000

1import grpc 

2import pytest 

3from google.protobuf import wrappers_pb2 

4 

5from couchers import errors 

6from couchers.crypto import random_hex 

7from couchers.db import session_scope 

8from couchers.models import Cluster, ClusterRole, ClusterSubscription, Node, Page, PageType, PageVersion, Thread, Upload 

9from couchers.utils import create_polygon_lat_lng, now, to_aware_datetime, to_multi 

10from proto import pages_pb2 

11from tests.test_communities import create_community 

12from tests.test_fixtures import db, generate_user, pages_session, testconfig # noqa 

13 

14 

15@pytest.fixture(autouse=True) 

16def _(testconfig): 

17 pass 

18 

19 

20def test_create_place_errors(db): 

21 user, token = generate_user() 

22 with session_scope() as session: 

23 create_community(session, 0, 2, "Root node", [user], [], None) 

24 

25 with pages_session(token) as api: 

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

27 api.CreatePlace( 

28 pages_pb2.CreatePlaceReq( 

29 title=None, 

30 content="dummy content", 

31 address="dummy address", 

32 location=pages_pb2.Coordinate( 

33 lat=1, 

34 lng=1, 

35 ), 

36 ) 

37 ) 

38 assert e.value.code() == grpc.StatusCode.INVALID_ARGUMENT 

39 assert e.value.details() == errors.MISSING_PAGE_TITLE 

40 

41 with pages_session(token) as api: 

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

43 api.CreatePlace( 

44 pages_pb2.CreatePlaceReq( 

45 title="dummy title", 

46 content=None, 

47 address="dummy address", 

48 location=pages_pb2.Coordinate( 

49 lat=1, 

50 lng=1, 

51 ), 

52 ) 

53 ) 

54 assert e.value.code() == grpc.StatusCode.INVALID_ARGUMENT 

55 assert e.value.details() == errors.MISSING_PAGE_CONTENT 

56 

57 with pages_session(token) as api: 

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

59 api.CreatePlace( 

60 pages_pb2.CreatePlaceReq( 

61 title="dummy title", 

62 content="dummy content", 

63 address=None, 

64 location=pages_pb2.Coordinate( 

65 lat=1, 

66 lng=1, 

67 ), 

68 ) 

69 ) 

70 assert e.value.code() == grpc.StatusCode.INVALID_ARGUMENT 

71 assert e.value.details() == errors.MISSING_PAGE_ADDRESS 

72 

73 with pages_session(token) as api: 

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

75 api.CreatePlace( 

76 pages_pb2.CreatePlaceReq( 

77 title="dummy title", 

78 content="dummy content", 

79 address="dummy address", 

80 ) 

81 ) 

82 assert e.value.code() == grpc.StatusCode.INVALID_ARGUMENT 

83 assert e.value.details() == errors.MISSING_PAGE_LOCATION 

84 

85 

86def test_create_guide_errors(db): 

87 user, token = generate_user() 

88 with session_scope() as session: 

89 c_id = create_community(session, 0, 2, "Root node", [user], [], None).id 

90 

91 with pages_session(token) as api: 

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

93 api.CreateGuide( 

94 pages_pb2.CreateGuideReq( 

95 title=None, 

96 content="dummy content", 

97 parent_community_id=c_id, 

98 ) 

99 ) 

100 assert e.value.code() == grpc.StatusCode.INVALID_ARGUMENT 

101 assert e.value.details() == errors.MISSING_PAGE_TITLE 

102 

103 with pages_session(token) as api: 

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

105 api.CreateGuide( 

106 pages_pb2.CreateGuideReq( 

107 title="dummy title", 

108 content=None, 

109 parent_community_id=c_id, 

110 ) 

111 ) 

112 assert e.value.code() == grpc.StatusCode.INVALID_ARGUMENT 

113 assert e.value.details() == errors.MISSING_PAGE_CONTENT 

114 

115 with pages_session(token) as api: 

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

117 api.CreateGuide( 

118 pages_pb2.CreateGuideReq( 

119 title="dummy title", 

120 content="dummy content", 

121 ) 

122 ) 

123 assert e.value.code() == grpc.StatusCode.INVALID_ARGUMENT 

124 assert e.value.details() == errors.MISSING_PAGE_PARENT 

125 

126 with pages_session(token) as api: 

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

128 api.CreateGuide( 

129 pages_pb2.CreateGuideReq( 

130 title="dummy title", 

131 content="dummy content", 

132 parent_community_id=9999, 

133 ) 

134 ) 

135 assert e.value.code() == grpc.StatusCode.INVALID_ARGUMENT 

136 assert e.value.details() == errors.COMMUNITY_NOT_FOUND 

137 

138 with pages_session(token) as api: 

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

140 api.CreateGuide( 

141 pages_pb2.CreateGuideReq( 

142 title="dummy title", 

143 content="dummy content", 

144 address="dummy address", 

145 parent_community_id=c_id, 

146 ) 

147 ) 

148 assert e.value.code() == grpc.StatusCode.INVALID_ARGUMENT 

149 assert e.value.details() == errors.INVALID_GUIDE_LOCATION 

150 

151 with pages_session(token) as api: 

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

153 api.CreateGuide( 

154 pages_pb2.CreateGuideReq( 

155 title="dummy title", 

156 content="dummy content", 

157 location=pages_pb2.Coordinate( 

158 lat=1, 

159 lng=1, 

160 ), 

161 parent_community_id=c_id, 

162 ) 

163 ) 

164 assert e.value.code() == grpc.StatusCode.INVALID_ARGUMENT 

165 assert e.value.details() == errors.INVALID_GUIDE_LOCATION 

166 

167 

168def test_create_page_place(db): 

169 user, token = generate_user() 

170 with session_scope() as session: 

171 c_id = create_community(session, 0, 2, "Root node", [user], [], None).id 

172 

173 with pages_session(token) as api: 

174 time_before = now() 

175 res = api.CreatePlace( 

176 pages_pb2.CreatePlaceReq( 

177 title="dummy !#¤%&/-*' title", 

178 content="dummy content", 

179 address="dummy address", 

180 location=pages_pb2.Coordinate( 

181 lat=1, 

182 lng=1, 

183 ), 

184 ) 

185 ) 

186 

187 assert res.title == "dummy !#¤%&/-*' title" 

188 assert res.type == pages_pb2.PAGE_TYPE_PLACE 

189 assert res.content == "dummy content" 

190 assert res.address == "dummy address" 

191 assert res.location.lat == 1 

192 assert res.location.lng == 1 

193 assert res.slug == "dummy-title" 

194 assert time_before <= to_aware_datetime(res.created) <= now() 

195 assert time_before <= to_aware_datetime(res.last_edited) <= now() 

196 assert res.last_editor_user_id == user.id 

197 assert res.creator_user_id == user.id 

198 assert res.owner_user_id == user.id 

199 assert not res.owner_community_id 

200 assert not res.owner_group_id 

201 assert res.editor_user_ids == [user.id] 

202 assert res.can_edit 

203 assert res.can_moderate 

204 

205 

206def test_create_page_guide(db): 

207 user, token = generate_user() 

208 with session_scope() as session: 

209 c_id = create_community(session, 0, 2, "Root node", [user], [], None).id 

210 

211 with pages_session(token) as api: 

212 time_before = now() 

213 res = api.CreateGuide( 

214 pages_pb2.CreateGuideReq( 

215 title="dummy title", 

216 content="dummy content", 

217 address="dummy address", 

218 location=pages_pb2.Coordinate( 

219 lat=1, 

220 lng=1, 

221 ), 

222 parent_community_id=c_id, 

223 ) 

224 ) 

225 

226 assert res.title == "dummy title" 

227 assert res.type == pages_pb2.PAGE_TYPE_GUIDE 

228 assert res.content == "dummy content" 

229 assert res.address == "dummy address" 

230 assert res.location.lat == 1 

231 assert res.location.lng == 1 

232 assert res.slug == "dummy-title" 

233 assert time_before <= to_aware_datetime(res.created) <= now() 

234 assert time_before <= to_aware_datetime(res.last_edited) <= now() 

235 assert res.last_editor_user_id == user.id 

236 assert res.creator_user_id == user.id 

237 assert res.owner_user_id == user.id 

238 assert not res.owner_community_id 

239 assert not res.owner_group_id 

240 assert res.editor_user_ids == [user.id] 

241 assert res.can_edit 

242 assert res.can_moderate 

243 

244 with pages_session(token) as api: 

245 time_before = now() 

246 res = api.CreateGuide( 

247 pages_pb2.CreateGuideReq( 

248 title="dummy title", 

249 content="dummy content", 

250 parent_community_id=c_id, 

251 ) 

252 ) 

253 

254 assert res.title == "dummy title" 

255 assert res.type == pages_pb2.PAGE_TYPE_GUIDE 

256 assert res.content == "dummy content" 

257 assert not res.address 

258 assert not res.HasField("location") 

259 assert res.slug == "dummy-title" 

260 assert time_before <= to_aware_datetime(res.created) <= now() 

261 assert time_before <= to_aware_datetime(res.last_edited) <= now() 

262 assert res.last_editor_user_id == user.id 

263 assert res.creator_user_id == user.id 

264 assert res.owner_user_id == user.id 

265 assert not res.owner_community_id 

266 assert not res.owner_group_id 

267 assert res.editor_user_ids == [user.id] 

268 assert res.can_edit 

269 assert res.can_moderate 

270 

271 

272def test_get_page(db): 

273 user1, token1 = generate_user() 

274 user2, token2 = generate_user() 

275 with session_scope() as session: 

276 c_id = create_community(session, 0, 2, "Root node", [user1], [], None).id 

277 

278 with pages_session(token1) as api: 

279 time_before_create = now() 

280 page_id = api.CreatePlace( 

281 pages_pb2.CreatePlaceReq( 

282 title="dummy title", 

283 content="dummy content", 

284 address="dummy address", 

285 location=pages_pb2.Coordinate( 

286 lat=1, 

287 lng=1, 

288 ), 

289 ) 

290 ).page_id 

291 

292 with pages_session(token2) as api: 

293 time_before_get = now() 

294 res = api.GetPage(pages_pb2.GetPageReq(page_id=page_id)) 

295 assert res.title == "dummy title" 

296 assert res.content == "dummy content" 

297 assert res.address == "dummy address" 

298 assert res.location.lat == 1 

299 assert res.location.lng == 1 

300 assert res.slug == "dummy-title" 

301 assert time_before_create <= to_aware_datetime(res.created) <= time_before_get 

302 assert time_before_create <= to_aware_datetime(res.last_edited) <= time_before_get 

303 assert res.last_editor_user_id == user1.id 

304 assert res.creator_user_id == user1.id 

305 assert res.owner_user_id == user1.id 

306 assert not res.owner_community_id 

307 assert not res.owner_group_id 

308 assert res.editor_user_ids == [user1.id] 

309 assert not res.can_edit 

310 assert not res.can_moderate 

311 assert res.thread.thread_id > 0 

312 assert res.thread.num_responses == 0 

313 

314 

315def test_update_page(db): 

316 user, token = generate_user() 

317 with session_scope() as session: 

318 c_id = create_community(session, 0, 2, "Root node", [user], [], None).id 

319 

320 with pages_session(token) as api: 

321 time_before_create = now() 

322 page_id = api.CreatePlace( 

323 pages_pb2.CreatePlaceReq( 

324 title="dummy title", 

325 content="dummy content", 

326 address="dummy address", 

327 location=pages_pb2.Coordinate( 

328 lat=1, 

329 lng=1, 

330 ), 

331 ) 

332 ).page_id 

333 

334 time_before_update = now() 

335 api.UpdatePage( 

336 pages_pb2.UpdatePageReq( 

337 page_id=page_id, 

338 title=wrappers_pb2.StringValue(value="test title"), 

339 ) 

340 ) 

341 

342 time_before_get = now() 

343 res = api.GetPage(pages_pb2.GetPageReq(page_id=page_id)) 

344 assert res.title == "test title" 

345 assert res.content == "dummy content" 

346 assert res.address == "dummy address" 

347 assert res.location.lat == 1 

348 assert res.location.lng == 1 

349 assert res.slug == "test-title" 

350 assert time_before_create <= to_aware_datetime(res.created) <= time_before_update 

351 assert time_before_update <= to_aware_datetime(res.last_edited) <= time_before_get 

352 assert to_aware_datetime(res.created) <= to_aware_datetime(res.last_edited) 

353 assert res.last_editor_user_id == user.id 

354 assert res.creator_user_id == user.id 

355 assert res.owner_user_id == user.id 

356 assert not res.owner_community_id 

357 assert not res.owner_group_id 

358 assert res.editor_user_ids == [user.id] 

359 assert res.can_edit 

360 assert res.can_moderate 

361 

362 time_before_second_update = now() 

363 api.UpdatePage( 

364 pages_pb2.UpdatePageReq( 

365 page_id=page_id, 

366 content=wrappers_pb2.StringValue(value="test content"), 

367 ) 

368 ) 

369 

370 time_before_second_get = now() 

371 res = api.GetPage(pages_pb2.GetPageReq(page_id=page_id)) 

372 assert res.title == "test title" 

373 assert res.content == "test content" 

374 assert res.address == "dummy address" 

375 assert res.location.lat == 1 

376 assert res.location.lng == 1 

377 assert res.slug == "test-title" 

378 assert time_before_create <= to_aware_datetime(res.created) <= time_before_update 

379 assert time_before_second_update <= to_aware_datetime(res.last_edited) <= time_before_second_get 

380 assert to_aware_datetime(res.created) <= to_aware_datetime(res.last_edited) 

381 assert res.last_editor_user_id == user.id 

382 assert res.creator_user_id == user.id 

383 assert res.owner_user_id == user.id 

384 assert not res.owner_community_id 

385 assert not res.owner_group_id 

386 assert res.editor_user_ids == [user.id] 

387 assert res.can_edit 

388 assert res.can_moderate 

389 assert res.thread.thread_id > 0 

390 assert res.thread.num_responses == 0 

391 

392 time_before_third_update = now() 

393 api.UpdatePage( 

394 pages_pb2.UpdatePageReq( 

395 page_id=page_id, 

396 address=wrappers_pb2.StringValue(value="test address"), 

397 ) 

398 ) 

399 

400 time_before_third_get = now() 

401 res = api.GetPage(pages_pb2.GetPageReq(page_id=page_id)) 

402 assert res.title == "test title" 

403 assert res.content == "test content" 

404 assert res.address == "test address" 

405 assert res.location.lat == 1 

406 assert res.location.lng == 1 

407 assert res.slug == "test-title" 

408 assert time_before_create <= to_aware_datetime(res.created) <= time_before_update 

409 assert time_before_third_update <= to_aware_datetime(res.last_edited) <= time_before_third_get 

410 assert to_aware_datetime(res.created) <= to_aware_datetime(res.last_edited) 

411 assert res.last_editor_user_id == user.id 

412 assert res.creator_user_id == user.id 

413 assert res.owner_user_id == user.id 

414 assert not res.owner_community_id 

415 assert not res.owner_group_id 

416 assert res.editor_user_ids == [user.id] 

417 assert res.can_edit 

418 assert res.can_moderate 

419 assert res.thread.thread_id > 0 

420 assert res.thread.num_responses == 0 

421 

422 time_before_fourth_update = now() 

423 api.UpdatePage( 

424 pages_pb2.UpdatePageReq( 

425 page_id=page_id, 

426 location=pages_pb2.Coordinate( 

427 lat=3, 

428 lng=1.222, 

429 ), 

430 ) 

431 ) 

432 

433 time_before_fourth_get = now() 

434 res = api.GetPage(pages_pb2.GetPageReq(page_id=page_id)) 

435 assert res.title == "test title" 

436 assert res.content == "test content" 

437 assert res.address == "test address" 

438 assert res.location.lat == 3 

439 assert res.location.lng == 1.222 

440 assert res.slug == "test-title" 

441 assert time_before_create <= to_aware_datetime(res.created) <= time_before_update 

442 assert time_before_fourth_update <= to_aware_datetime(res.last_edited) <= time_before_fourth_get 

443 assert to_aware_datetime(res.created) <= to_aware_datetime(res.last_edited) 

444 assert res.last_editor_user_id == user.id 

445 assert res.creator_user_id == user.id 

446 assert res.owner_user_id == user.id 

447 assert not res.owner_community_id 

448 assert not res.owner_group_id 

449 assert res.editor_user_ids == [user.id] 

450 assert res.can_edit 

451 assert res.can_moderate 

452 assert res.thread.thread_id > 0 

453 assert res.thread.num_responses == 0 

454 

455 

456def test_owner_not_moderator(db): 

457 """ 

458 You can be the owner of content yet not have moderation rights 

459 """ 

460 user1, token1 = generate_user() 

461 user2, token2 = generate_user() 

462 with session_scope() as session: 

463 c_id = create_community(session, 0, 2, "Root node", [user1], [], None).id 

464 

465 # user2 makes page, is owner but not moderator, so can edit, not moderate 

466 with pages_session(token2) as api: 

467 res = api.CreatePlace( 

468 pages_pb2.CreatePlaceReq( 

469 title="dummy title", 

470 content="dummy content", 

471 address="dummy address", 

472 location=pages_pb2.Coordinate( 

473 lat=1, 

474 lng=1, 

475 ), 

476 ) 

477 ) 

478 assert res.title == "dummy title" 

479 assert res.content == "dummy content" 

480 assert res.address == "dummy address" 

481 assert res.location.lat == 1 

482 assert res.location.lng == 1 

483 assert res.slug == "dummy-title" 

484 assert res.last_editor_user_id == user2.id 

485 assert res.creator_user_id == user2.id 

486 assert res.owner_user_id == user2.id 

487 assert not res.owner_community_id 

488 assert not res.owner_group_id 

489 assert res.editor_user_ids == [user2.id] 

490 assert res.can_edit 

491 assert not res.can_moderate 

492 

493 page_id = res.page_id 

494 

495 # user1 is not owner so can't edit but can moderate 

496 with pages_session(token1) as api: 

497 res = api.GetPage(pages_pb2.GetPageReq(page_id=page_id)) 

498 assert res.title == "dummy title" 

499 assert res.content == "dummy content" 

500 assert res.address == "dummy address" 

501 assert res.location.lat == 1 

502 assert res.location.lng == 1 

503 assert res.slug == "dummy-title" 

504 assert res.last_editor_user_id == user2.id 

505 assert res.creator_user_id == user2.id 

506 assert res.owner_user_id == user2.id 

507 assert not res.owner_community_id 

508 assert not res.owner_group_id 

509 assert res.editor_user_ids == [user2.id] 

510 assert res.can_edit 

511 assert res.can_moderate 

512 assert res.thread.thread_id > 0 

513 assert res.thread.num_responses == 0 

514 

515 

516def test_update_page_errors(db): 

517 user, token = generate_user() 

518 with session_scope() as session: 

519 c_id = create_community(session, 0, 2, "Root node", [user], [], None).id 

520 

521 with pages_session(token) as api: 

522 page_id = api.CreatePlace( 

523 pages_pb2.CreatePlaceReq( 

524 title="dummy title", 

525 content="dummy content", 

526 address="dummy address", 

527 location=pages_pb2.Coordinate( 

528 lat=1, 

529 lng=1, 

530 ), 

531 ) 

532 ).page_id 

533 

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

535 api.UpdatePage( 

536 pages_pb2.UpdatePageReq( 

537 page_id=page_id, 

538 title=wrappers_pb2.StringValue(value=""), 

539 ) 

540 ) 

541 assert e.value.code() == grpc.StatusCode.INVALID_ARGUMENT 

542 assert e.value.details() == errors.MISSING_PAGE_TITLE 

543 

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

545 api.UpdatePage( 

546 pages_pb2.UpdatePageReq( 

547 page_id=page_id, 

548 content=wrappers_pb2.StringValue(value=""), 

549 ) 

550 ) 

551 assert e.value.code() == grpc.StatusCode.INVALID_ARGUMENT 

552 assert e.value.details() == errors.MISSING_PAGE_CONTENT 

553 

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

555 api.UpdatePage( 

556 pages_pb2.UpdatePageReq( 

557 page_id=page_id, 

558 address=wrappers_pb2.StringValue(value=""), 

559 ) 

560 ) 

561 assert e.value.code() == grpc.StatusCode.INVALID_ARGUMENT 

562 assert e.value.details() == errors.MISSING_PAGE_ADDRESS 

563 

564 

565def test_page_transfer(db): 

566 # transfers the pages 

567 user1, token1 = generate_user() 

568 # admin of the community/group 

569 user2, token2 = generate_user() 

570 # member of the community/group, shouldn't ever have edit access to anything 

571 user3, token3 = generate_user() 

572 with session_scope() as session: 

573 # create a community 

574 node = Node(geom=to_multi(create_polygon_lat_lng([[0, 0], [0, 2], [2, 2], [2, 0], [0, 0]]))) 

575 session.add(node) 

576 community_cluster = Cluster( 

577 name="Testing Community", 

578 description="Description for testing community", 

579 parent_node=node, 

580 is_official_cluster=True, 

581 ) 

582 session.add(community_cluster) 

583 main_page = Page( 

584 parent_node=community_cluster.parent_node, 

585 creator_user_id=user2.id, 

586 owner_cluster=community_cluster, 

587 type=PageType.main_page, 

588 thread=Thread(), 

589 ) 

590 session.add(main_page) 

591 session.add( 

592 PageVersion( 

593 page=main_page, 

594 editor_user_id=user2.id, 

595 title="Main page for the testing community", 

596 content="Empty.", 

597 ) 

598 ) 

599 community_cluster.cluster_subscriptions.append( 

600 ClusterSubscription( 

601 user_id=user2.id, 

602 role=ClusterRole.admin, 

603 ) 

604 ) 

605 community_cluster.cluster_subscriptions.append( 

606 ClusterSubscription( 

607 user_id=user3.id, 

608 role=ClusterRole.member, 

609 ) 

610 ) 

611 

612 # create a group 

613 group_cluster = Cluster( 

614 name="Testing Group", 

615 description="Description for testing group", 

616 parent_node=node, 

617 ) 

618 session.add(group_cluster) 

619 main_page = Page( 

620 parent_node=group_cluster.parent_node, 

621 creator_user_id=user2.id, 

622 owner_cluster=group_cluster, 

623 type=PageType.main_page, 

624 thread=Thread(), 

625 ) 

626 session.add(main_page) 

627 session.add( 

628 PageVersion( 

629 page=main_page, 

630 editor_user_id=user2.id, 

631 title="Main page for the testing community", 

632 content="Empty.", 

633 ) 

634 ) 

635 group_cluster.cluster_subscriptions.append( 

636 ClusterSubscription( 

637 user_id=user2.id, 

638 role=ClusterRole.admin, 

639 ) 

640 ) 

641 group_cluster.cluster_subscriptions.append( 

642 ClusterSubscription( 

643 user_id=user3.id, 

644 role=ClusterRole.member, 

645 ) 

646 ) 

647 session.flush() 

648 

649 community_id = node.id 

650 community_cluster_id = community_cluster.id 

651 group_id = group_cluster.id 

652 

653 with pages_session(token1) as api: 

654 create_page_req = pages_pb2.CreatePlaceReq( 

655 title="title", 

656 content="content", 

657 address="address", 

658 location=pages_pb2.Coordinate( 

659 lat=1, 

660 lng=1, 

661 ), 

662 ) 

663 

664 # transfer should work fine to a community 

665 page1 = api.CreatePlace(create_page_req) 

666 assert page1.owner_user_id == user1.id 

667 assert page1.can_edit 

668 assert not page1.can_moderate 

669 

670 with pages_session(token2) as api: 

671 assert api.GetPage(pages_pb2.GetPageReq(page_id=page1.page_id)).can_edit 

672 assert api.GetPage(pages_pb2.GetPageReq(page_id=page1.page_id)).can_moderate 

673 

674 with pages_session(token3) as api: 

675 assert not api.GetPage(pages_pb2.GetPageReq(page_id=page1.page_id)).can_edit 

676 assert not api.GetPage(pages_pb2.GetPageReq(page_id=page1.page_id)).can_moderate 

677 

678 with pages_session(token1) as api: 

679 page1 = api.TransferPage( 

680 pages_pb2.TransferPageReq( 

681 page_id=page1.page_id, 

682 new_owner_community_id=community_id, 

683 ) 

684 ) 

685 assert page1.owner_community_id == community_id 

686 assert not page1.can_edit 

687 assert not page1.can_moderate 

688 

689 with pages_session(token2) as api: 

690 assert api.GetPage(pages_pb2.GetPageReq(page_id=page1.page_id)).can_edit 

691 assert api.GetPage(pages_pb2.GetPageReq(page_id=page1.page_id)).can_moderate 

692 

693 with pages_session(token3) as api: 

694 assert not api.GetPage(pages_pb2.GetPageReq(page_id=page1.page_id)).can_edit 

695 assert not api.GetPage(pages_pb2.GetPageReq(page_id=page1.page_id)).can_moderate 

696 

697 with pages_session(token1) as api: 

698 # now we're no longer the owner, can't transfer 

699 page1 = api.GetPage(pages_pb2.GetPageReq(page_id=page1.page_id)) 

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

701 api.TransferPage( 

702 pages_pb2.TransferPageReq( 

703 page_id=page1.page_id, 

704 new_owner_group_id=group_id, 

705 ) 

706 ) 

707 assert e.value.code() == grpc.StatusCode.PERMISSION_DENIED 

708 assert e.value.details() == errors.PAGE_TRANSFER_PERMISSION_DENIED 

709 page1 = api.GetPage(pages_pb2.GetPageReq(page_id=page1.page_id)) 

710 assert page1.owner_community_id == community_id 

711 assert not page1.can_edit 

712 assert not page1.can_moderate 

713 

714 with pages_session(token2) as api: 

715 assert api.GetPage(pages_pb2.GetPageReq(page_id=page1.page_id)).can_edit 

716 assert api.GetPage(pages_pb2.GetPageReq(page_id=page1.page_id)).can_moderate 

717 

718 with pages_session(token3) as api: 

719 assert not api.GetPage(pages_pb2.GetPageReq(page_id=page1.page_id)).can_edit 

720 assert not api.GetPage(pages_pb2.GetPageReq(page_id=page1.page_id)).can_moderate 

721 

722 with pages_session(token1) as api: 

723 # try a new page, just for fun 

724 page2 = api.CreatePlace(create_page_req) 

725 assert page2.owner_user_id == user1.id 

726 

727 page2 = api.TransferPage( 

728 pages_pb2.TransferPageReq( 

729 page_id=page2.page_id, 

730 new_owner_community_id=community_id, 

731 ) 

732 ) 

733 assert page2.owner_community_id == community_id 

734 

735 with pages_session(token2) as api: 

736 assert api.GetPage(pages_pb2.GetPageReq(page_id=page2.page_id)).can_edit 

737 assert api.GetPage(pages_pb2.GetPageReq(page_id=page2.page_id)).can_moderate 

738 

739 with pages_session(token3) as api: 

740 assert not api.GetPage(pages_pb2.GetPageReq(page_id=page2.page_id)).can_edit 

741 assert not api.GetPage(pages_pb2.GetPageReq(page_id=page2.page_id)).can_moderate 

742 

743 with pages_session(token1) as api: 

744 # can't transfer a page to an official cluster, only through community 

745 page3 = api.CreatePlace(create_page_req) 

746 assert page3.owner_user_id == user1.id 

747 

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

749 api.TransferPage( 

750 pages_pb2.TransferPageReq( 

751 page_id=page3.page_id, 

752 new_owner_community_id=community_cluster_id, 

753 ) 

754 ) 

755 assert e.value.code() == grpc.StatusCode.NOT_FOUND 

756 assert e.value.details() == errors.GROUP_OR_COMMUNITY_NOT_FOUND 

757 page3 = api.GetPage(pages_pb2.GetPageReq(page_id=page3.page_id)) 

758 assert page3.owner_user_id == user1.id 

759 

760 # can transfer to group 

761 page4 = api.CreatePlace(create_page_req) 

762 assert page4.owner_user_id == user1.id 

763 assert page4.can_edit 

764 assert not page4.can_moderate 

765 

766 with pages_session(token2) as api: 

767 assert api.GetPage(pages_pb2.GetPageReq(page_id=page4.page_id)).can_edit 

768 assert api.GetPage(pages_pb2.GetPageReq(page_id=page4.page_id)).can_moderate 

769 

770 with pages_session(token3) as api: 

771 assert not api.GetPage(pages_pb2.GetPageReq(page_id=page4.page_id)).can_edit 

772 assert not api.GetPage(pages_pb2.GetPageReq(page_id=page4.page_id)).can_moderate 

773 

774 with pages_session(token1) as api: 

775 page4 = api.TransferPage( 

776 pages_pb2.TransferPageReq( 

777 page_id=page4.page_id, 

778 new_owner_group_id=group_id, 

779 ) 

780 ) 

781 assert page4.owner_group_id == group_id 

782 assert not page4.can_edit 

783 assert not page4.can_moderate 

784 

785 with pages_session(token2) as api: 

786 assert api.GetPage(pages_pb2.GetPageReq(page_id=page4.page_id)).can_edit 

787 assert api.GetPage(pages_pb2.GetPageReq(page_id=page4.page_id)).can_moderate 

788 

789 with pages_session(token3) as api: 

790 assert not api.GetPage(pages_pb2.GetPageReq(page_id=page4.page_id)).can_edit 

791 assert not api.GetPage(pages_pb2.GetPageReq(page_id=page4.page_id)).can_moderate 

792 

793 with pages_session(token1) as api: 

794 # now we're no longer the owner, can't transfer 

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

796 api.TransferPage( 

797 pages_pb2.TransferPageReq( 

798 page_id=page4.page_id, 

799 new_owner_community_id=community_id, 

800 ) 

801 ) 

802 assert e.value.code() == grpc.StatusCode.PERMISSION_DENIED 

803 assert e.value.details() == errors.PAGE_TRANSFER_PERMISSION_DENIED 

804 page4 = api.GetPage(pages_pb2.GetPageReq(page_id=page4.page_id)) 

805 assert page4.owner_group_id == group_id 

806 

807 

808def test_page_photo(db): 

809 user, token = generate_user() 

810 with session_scope() as session: 

811 c_id = create_community(session, 0, 2, "Root node", [user], [], None).id 

812 

813 with pages_session(token) as api: 

814 key1 = random_hex(32) 

815 filename1 = random_hex(32) 

816 

817 with session_scope() as session: 

818 session.add( 

819 Upload( 

820 key=key1, 

821 filename=filename1, 

822 creator_user_id=user.id, 

823 ) 

824 ) 

825 

826 res = api.CreatePlace( 

827 pages_pb2.CreatePlaceReq( 

828 title="dummy title", 

829 content="dummy content", 

830 photo_key=key1, 

831 address="dummy address", 

832 location=pages_pb2.Coordinate( 

833 lat=1, 

834 lng=1, 

835 ), 

836 ) 

837 ) 

838 

839 assert filename1 in res.photo_url 

840 

841 # can't create with non-existent photo 

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

843 api.CreatePlace( 

844 pages_pb2.CreatePlaceReq( 

845 title="dummy title", 

846 content="dummy content", 

847 photo_key="nonexisten", 

848 address="dummy address", 

849 location=pages_pb2.Coordinate( 

850 lat=1, 

851 lng=1, 

852 ), 

853 ) 

854 ) 

855 assert e.value.code() == grpc.StatusCode.INVALID_ARGUMENT 

856 assert e.value.details() == errors.PHOTO_NOT_FOUND 

857 

858 # can create page with no photo 

859 res = api.CreatePlace( 

860 pages_pb2.CreatePlaceReq( 

861 title="dummy title", 

862 content="dummy content", 

863 address="dummy address", 

864 location=pages_pb2.Coordinate( 

865 lat=1, 

866 lng=1, 

867 ), 

868 ) 

869 ) 

870 

871 assert res.photo_url == "" 

872 

873 page_id = res.page_id 

874 

875 # can't set it to non-existent stuff 

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

877 api.UpdatePage( 

878 pages_pb2.UpdatePageReq( 

879 page_id=page_id, 

880 photo_key=wrappers_pb2.StringValue(value="non-existent id"), 

881 ) 

882 ) 

883 assert e.value.code() == grpc.StatusCode.INVALID_ARGUMENT 

884 assert e.value.details() == errors.PHOTO_NOT_FOUND 

885 

886 key2 = random_hex(32) 

887 filename2 = random_hex(32) 

888 

889 with session_scope() as session: 

890 session.add( 

891 Upload( 

892 key=key2, 

893 filename=filename2, 

894 creator_user_id=user.id, 

895 ) 

896 ) 

897 

898 res = api.UpdatePage( 

899 pages_pb2.UpdatePageReq( 

900 page_id=page_id, 

901 photo_key=wrappers_pb2.StringValue(value=key2), 

902 ) 

903 ) 

904 

905 assert filename2 in res.photo_url 

906 

907 # can unset it 

908 

909 res = api.UpdatePage( 

910 pages_pb2.UpdatePageReq( 

911 page_id=page_id, 

912 photo_key=wrappers_pb2.StringValue(value=""), 

913 ) 

914 ) 

915 

916 assert res.photo_url == "" 

917 

918 

919def test_list_user_places(db): 

920 user1, token1 = generate_user() 

921 with session_scope() as session: 

922 c_id = create_community(session, 0, 2, "Root node", [user1], [], None).id 

923 

924 with pages_session(token1) as api: 

925 place1_id = api.CreatePlace( 

926 pages_pb2.CreatePlaceReq( 

927 title="dummy title", 

928 content="dummy content", 

929 address="dummy address", 

930 location=pages_pb2.Coordinate( 

931 lat=1, 

932 lng=1, 

933 ), 

934 ) 

935 ).page_id 

936 

937 place2_id = api.CreatePlace( 

938 pages_pb2.CreatePlaceReq( 

939 title="dummy title 2", 

940 content="dummy content 2", 

941 address="dummy address 2", 

942 location=pages_pb2.Coordinate( 

943 lat=1, 

944 lng=1, 

945 ), 

946 ) 

947 ).page_id 

948 

949 with pages_session(token1) as api: 

950 res = api.ListUserPlaces(pages_pb2.ListUserPlacesReq()) 

951 assert [p.page_id for p in res.places] == [place1_id, place2_id] 

952 

953 

954def test_list_other_user_places(db): 

955 user1, token1 = generate_user() 

956 user2, token2 = generate_user() 

957 with session_scope() as session: 

958 c_id = create_community(session, 0, 2, "Root node", [user1], [], None).id 

959 

960 with pages_session(token1) as api: 

961 place1_id = api.CreatePlace( 

962 pages_pb2.CreatePlaceReq( 

963 title="dummy title", 

964 content="dummy content", 

965 address="dummy address", 

966 location=pages_pb2.Coordinate( 

967 lat=1, 

968 lng=1, 

969 ), 

970 ) 

971 ).page_id 

972 

973 place2_id = api.CreatePlace( 

974 pages_pb2.CreatePlaceReq( 

975 title="dummy title 2", 

976 content="dummy content 2", 

977 address="dummy address 2", 

978 location=pages_pb2.Coordinate( 

979 lat=1, 

980 lng=1, 

981 ), 

982 ) 

983 ).page_id 

984 

985 with pages_session(token2) as api: 

986 res = api.ListUserPlaces(pages_pb2.ListUserPlacesReq(user_id=user1.id)) 

987 assert [p.page_id for p in res.places] == [place1_id, place2_id] 

988 

989 

990def test_list_user_guides(db): 

991 user1, token1 = generate_user() 

992 with session_scope() as session: 

993 c_id = create_community(session, 0, 2, "Root node", [user1], [], None).id 

994 

995 with pages_session(token1) as api: 

996 guide1_id = api.CreateGuide( 

997 pages_pb2.CreateGuideReq( 

998 title="dummy title", 

999 content="dummy content", 

1000 address="dummy address", 

1001 location=pages_pb2.Coordinate( 

1002 lat=1, 

1003 lng=1, 

1004 ), 

1005 parent_community_id=c_id, 

1006 ) 

1007 ).page_id 

1008 

1009 guide2_id = api.CreateGuide( 

1010 pages_pb2.CreateGuideReq( 

1011 title="dummy title 2", 

1012 content="dummy content 2", 

1013 address="dummy address 2", 

1014 location=pages_pb2.Coordinate( 

1015 lat=1, 

1016 lng=1, 

1017 ), 

1018 parent_community_id=c_id, 

1019 ) 

1020 ).page_id 

1021 

1022 with pages_session(token1) as api: 

1023 res = api.ListUserGuides(pages_pb2.ListUserGuidesReq()) 

1024 assert [p.page_id for p in res.guides] == [guide1_id, guide2_id] 

1025 

1026 

1027def test_list_other_user_guides(db): 

1028 user1, token1 = generate_user() 

1029 user2, token2 = generate_user() 

1030 with session_scope() as session: 

1031 c_id = create_community(session, 0, 2, "Root node", [user1], [], None).id 

1032 

1033 with pages_session(token1) as api: 

1034 guide1_id = api.CreateGuide( 

1035 pages_pb2.CreateGuideReq( 

1036 title="dummy title", 

1037 content="dummy content", 

1038 address="dummy address", 

1039 location=pages_pb2.Coordinate( 

1040 lat=1, 

1041 lng=1, 

1042 ), 

1043 parent_community_id=c_id, 

1044 ) 

1045 ).page_id 

1046 

1047 guide2_id = api.CreateGuide( 

1048 pages_pb2.CreateGuideReq( 

1049 title="dummy title 2", 

1050 content="dummy content 2", 

1051 address="dummy address 2", 

1052 location=pages_pb2.Coordinate( 

1053 lat=1, 

1054 lng=1, 

1055 ), 

1056 parent_community_id=c_id, 

1057 ) 

1058 ).page_id 

1059 

1060 with pages_session(token2) as api: 

1061 res = api.ListUserGuides(pages_pb2.ListUserGuidesReq(user_id=user1.id)) 

1062 assert [p.page_id for p in res.guides] == [guide1_id, guide2_id]