|
1 | 1 | import string |
2 | 2 | import textwrap |
| 3 | +from datetime import timedelta |
3 | 4 |
|
4 | 5 | import grpc |
5 | 6 | import pytest |
|
17 | 18 | User, |
18 | 19 | ) |
19 | 20 | from couchers.models.discussions import CommentVersion, ContentChangeType, ReplyVersion |
20 | | -from couchers.proto import moderation_pb2, threads_pb2 |
| 21 | +from couchers.proto import discussions_pb2, events_pb2, moderation_pb2, threads_pb2 |
21 | 22 | from couchers.servicers.threads import pack_thread_id |
22 | | -from couchers.utils import now |
| 23 | +from couchers.utils import Timestamp_from_datetime, now |
23 | 24 | from tests.fixtures.db import generate_user |
24 | | -from tests.fixtures.sessions import real_moderation_session, threads_session |
| 25 | +from tests.fixtures.sessions import discussions_session, events_session, real_moderation_session, threads_session |
| 26 | +from tests.test_communities import create_community |
25 | 27 |
|
26 | 28 |
|
27 | 29 | @pytest.fixture(autouse=True) |
@@ -539,6 +541,101 @@ def test_delete_reply_creates_version_record(db): |
539 | 541 | assert v.editor_user_id == user.id |
540 | 542 |
|
541 | 543 |
|
| 544 | +def _create_event_and_get_thread_id(organizer, organizer_token: str) -> int: |
| 545 | + """Helper: create an Event via the API, return its (packed) thread_id.""" |
| 546 | + with session_scope() as session: |
| 547 | + create_community(session, 0, 2, "Testing Community", [organizer], [], None) |
| 548 | + |
| 549 | + start_time = now() + timedelta(hours=2) |
| 550 | + end_time = start_time + timedelta(hours=3) |
| 551 | + with events_session(organizer_token) as api: |
| 552 | + res = api.CreateEvent( |
| 553 | + events_pb2.CreateEventReq( |
| 554 | + title="Dummy event", |
| 555 | + content="Dummy content.", |
| 556 | + location=events_pb2.EventLocation( |
| 557 | + address="Near Null Island", |
| 558 | + lat=0.1, |
| 559 | + lng=0.2, |
| 560 | + ), |
| 561 | + start_time=Timestamp_from_datetime(start_time), |
| 562 | + end_time=Timestamp_from_datetime(end_time), |
| 563 | + timezone="UTC", |
| 564 | + ) |
| 565 | + ) |
| 566 | + return int(res.thread.thread_id) |
| 567 | + |
| 568 | + |
| 569 | +def test_post_reply_incomplete_profile_blocked_on_event_comment(db): |
| 570 | + """An incomplete-profile user cannot post a top-level comment on an event's thread.""" |
| 571 | + organizer, organizer_token = generate_user() |
| 572 | + _commenter, commenter_token = generate_user(complete_profile=False) |
| 573 | + |
| 574 | + event_thread_id = _create_event_and_get_thread_id(organizer, organizer_token) |
| 575 | + |
| 576 | + with threads_session(commenter_token) as api: |
| 577 | + with pytest.raises(grpc.RpcError) as e: |
| 578 | + api.PostReply(threads_pb2.PostReplyReq(thread_id=event_thread_id, content="hello")) |
| 579 | + assert e.value.code() == grpc.StatusCode.FAILED_PRECONDITION |
| 580 | + |
| 581 | + |
| 582 | +def test_post_reply_incomplete_profile_blocked_on_event_reply(db): |
| 583 | + """An incomplete-profile user cannot post a nested reply within an event's thread.""" |
| 584 | + organizer, organizer_token = generate_user() |
| 585 | + _commenter, commenter_token = generate_user() |
| 586 | + _replier, replier_token = generate_user(complete_profile=False) |
| 587 | + |
| 588 | + event_thread_id = _create_event_and_get_thread_id(organizer, organizer_token) |
| 589 | + |
| 590 | + with threads_session(commenter_token) as api: |
| 591 | + comment_thread_id = api.PostReply( |
| 592 | + threads_pb2.PostReplyReq(thread_id=event_thread_id, content="top-level comment") |
| 593 | + ).thread_id |
| 594 | + |
| 595 | + with threads_session(replier_token) as api: |
| 596 | + with pytest.raises(grpc.RpcError) as e: |
| 597 | + api.PostReply(threads_pb2.PostReplyReq(thread_id=comment_thread_id, content="a reply")) |
| 598 | + assert e.value.code() == grpc.StatusCode.FAILED_PRECONDITION |
| 599 | + |
| 600 | + |
| 601 | +def test_post_reply_complete_profile_allowed_on_event_comment(db): |
| 602 | + """A complete-profile user can post a comment on an event's thread.""" |
| 603 | + organizer, organizer_token = generate_user() |
| 604 | + _commenter, commenter_token = generate_user() |
| 605 | + |
| 606 | + event_thread_id = _create_event_and_get_thread_id(organizer, organizer_token) |
| 607 | + |
| 608 | + with threads_session(commenter_token) as api: |
| 609 | + comment_thread_id = api.PostReply( |
| 610 | + threads_pb2.PostReplyReq(thread_id=event_thread_id, content="hello") |
| 611 | + ).thread_id |
| 612 | + assert comment_thread_id |
| 613 | + |
| 614 | + |
| 615 | +def test_post_reply_incomplete_profile_blocked_on_discussion_thread(db): |
| 616 | + """An incomplete-profile user cannot post a comment on a discussion thread.""" |
| 617 | + admin, admin_token = generate_user() |
| 618 | + _commenter, commenter_token = generate_user(complete_profile=False) |
| 619 | + |
| 620 | + with session_scope() as session: |
| 621 | + community_id = create_community(session, 0, 1, "Testing Community", [admin], [], None).id |
| 622 | + |
| 623 | + with discussions_session(admin_token) as api: |
| 624 | + discussion = api.CreateDiscussion( |
| 625 | + discussions_pb2.CreateDiscussionReq( |
| 626 | + title="A discussion", |
| 627 | + content="Content", |
| 628 | + owner_community_id=community_id, |
| 629 | + ) |
| 630 | + ) |
| 631 | + discussion_thread_id = discussion.thread.thread_id |
| 632 | + |
| 633 | + with threads_session(commenter_token) as api: |
| 634 | + with pytest.raises(grpc.RpcError) as e: |
| 635 | + api.PostReply(threads_pb2.PostReplyReq(thread_id=discussion_thread_id, content="hello")) |
| 636 | + assert e.value.code() == grpc.StatusCode.FAILED_PRECONDITION |
| 637 | + |
| 638 | + |
542 | 639 | def test_edit_comment_multiple_edits_creates_multiple_version_records(db): |
543 | 640 | user, token = generate_user() |
544 | 641 | _, comment_thread_id = _make_thread_and_comment(token, content="v1") |
|
0 commit comments