def create_1d_polygon(lb, ub): # given a lower bound and upper bound on x, creates the given interval return create_polygon_lat_lng([[lb, 0], [lb, 2], [ub, 2], [ub, 0], [lb, 0]])
def test_page_transfer(db): # transfers the pages user1, token1 = generate_user() # admin of the community/group user2, token2 = generate_user() # member of the community/group, shouldn't ever have edit access to anything user3, token3 = generate_user() with session_scope() as session: # create a community node = Node(geom=to_multi( create_polygon_lat_lng([[0, 0], [0, 2], [2, 2], [2, 0], [0, 0]]))) session.add(node) community_cluster = Cluster( name=f"Testing Community", description=f"Description for testing community", parent_node=node, is_official_cluster=True, ) session.add(community_cluster) main_page = Page( parent_node=community_cluster.parent_node, creator_user_id=user2.id, owner_cluster=community_cluster, type=PageType.main_page, thread=Thread(), ) session.add(main_page) session.add( PageVersion( page=main_page, editor_user_id=user2.id, title=f"Main page for the testing community", content="Empty.", )) community_cluster.cluster_subscriptions.append( ClusterSubscription( user_id=user2.id, role=ClusterRole.admin, )) community_cluster.cluster_subscriptions.append( ClusterSubscription( user_id=user3.id, role=ClusterRole.member, )) # create a group group_cluster = Cluster( name=f"Testing Group", description=f"Description for testing group", parent_node=node, ) session.add(group_cluster) main_page = Page( parent_node=group_cluster.parent_node, creator_user_id=user2.id, owner_cluster=group_cluster, type=PageType.main_page, thread=Thread(), ) session.add(main_page) session.add( PageVersion( page=main_page, editor_user_id=user2.id, title=f"Main page for the testing community", content="Empty.", )) group_cluster.cluster_subscriptions.append( ClusterSubscription( user_id=user2.id, role=ClusterRole.admin, )) group_cluster.cluster_subscriptions.append( ClusterSubscription( user_id=user3.id, role=ClusterRole.member, )) session.flush() community_id = node.id community_cluster_id = community_cluster.id group_id = group_cluster.id with pages_session(token1) as api: create_page_req = pages_pb2.CreatePlaceReq( title="title", content="content", address="address", location=pages_pb2.Coordinate( lat=1, lng=1, ), ) # transfer should work fine to a community page1 = api.CreatePlace(create_page_req) assert page1.owner_user_id == user1.id assert page1.can_edit assert not page1.can_moderate with pages_session(token2) as api: assert not api.GetPage( pages_pb2.GetPageReq(page_id=page1.page_id)).can_edit assert api.GetPage( pages_pb2.GetPageReq(page_id=page1.page_id)).can_moderate with pages_session(token3) as api: assert not api.GetPage( pages_pb2.GetPageReq(page_id=page1.page_id)).can_edit assert not api.GetPage( pages_pb2.GetPageReq(page_id=page1.page_id)).can_moderate with pages_session(token1) as api: page1 = api.TransferPage( pages_pb2.TransferPageReq( page_id=page1.page_id, new_owner_community_id=community_id, )) assert page1.owner_community_id == community_id assert not page1.can_edit assert not page1.can_moderate with pages_session(token2) as api: assert api.GetPage( pages_pb2.GetPageReq(page_id=page1.page_id)).can_edit assert api.GetPage( pages_pb2.GetPageReq(page_id=page1.page_id)).can_moderate with pages_session(token3) as api: assert not api.GetPage( pages_pb2.GetPageReq(page_id=page1.page_id)).can_edit assert not api.GetPage( pages_pb2.GetPageReq(page_id=page1.page_id)).can_moderate with pages_session(token1) as api: # now we're no longer the owner, can't transfer page1 = api.GetPage(pages_pb2.GetPageReq(page_id=page1.page_id)) with pytest.raises(grpc.RpcError) as e: api.TransferPage( pages_pb2.TransferPageReq( page_id=page1.page_id, new_owner_group_id=group_id, )) assert e.value.code() == grpc.StatusCode.PERMISSION_DENIED assert e.value.details() == errors.PAGE_TRANSFER_PERMISSION_DENIED page1 = api.GetPage(pages_pb2.GetPageReq(page_id=page1.page_id)) assert page1.owner_community_id == community_id assert not page1.can_edit assert not page1.can_moderate with pages_session(token2) as api: assert api.GetPage( pages_pb2.GetPageReq(page_id=page1.page_id)).can_edit assert api.GetPage( pages_pb2.GetPageReq(page_id=page1.page_id)).can_moderate with pages_session(token3) as api: assert not api.GetPage( pages_pb2.GetPageReq(page_id=page1.page_id)).can_edit assert not api.GetPage( pages_pb2.GetPageReq(page_id=page1.page_id)).can_moderate with pages_session(token1) as api: # try a new page, just for fun page2 = api.CreatePlace(create_page_req) assert page2.owner_user_id == user1.id page2 = api.TransferPage( pages_pb2.TransferPageReq( page_id=page2.page_id, new_owner_community_id=community_id, )) assert page2.owner_community_id == community_id with pages_session(token2) as api: assert api.GetPage( pages_pb2.GetPageReq(page_id=page2.page_id)).can_edit assert api.GetPage( pages_pb2.GetPageReq(page_id=page2.page_id)).can_moderate with pages_session(token3) as api: assert not api.GetPage( pages_pb2.GetPageReq(page_id=page2.page_id)).can_edit assert not api.GetPage( pages_pb2.GetPageReq(page_id=page2.page_id)).can_moderate with pages_session(token1) as api: # can't transfer a page to an official cluster, only through community page3 = api.CreatePlace(create_page_req) assert page3.owner_user_id == user1.id with pytest.raises(grpc.RpcError) as e: api.TransferPage( pages_pb2.TransferPageReq( page_id=page3.page_id, new_owner_community_id=community_cluster_id, )) assert e.value.code() == grpc.StatusCode.NOT_FOUND assert e.value.details() == errors.GROUP_OR_COMMUNITY_NOT_FOUND page3 = api.GetPage(pages_pb2.GetPageReq(page_id=page3.page_id)) assert page3.owner_user_id == user1.id # can transfer to group page4 = api.CreatePlace(create_page_req) assert page4.owner_user_id == user1.id assert page4.can_edit assert not page4.can_moderate with pages_session(token2) as api: assert not api.GetPage( pages_pb2.GetPageReq(page_id=page4.page_id)).can_edit assert api.GetPage( pages_pb2.GetPageReq(page_id=page4.page_id)).can_moderate with pages_session(token3) as api: assert not api.GetPage( pages_pb2.GetPageReq(page_id=page4.page_id)).can_edit assert not api.GetPage( pages_pb2.GetPageReq(page_id=page4.page_id)).can_moderate with pages_session(token1) as api: page4 = api.TransferPage( pages_pb2.TransferPageReq( page_id=page4.page_id, new_owner_group_id=group_id, )) assert page4.owner_group_id == group_id assert not page4.can_edit assert not page4.can_moderate with pages_session(token2) as api: assert api.GetPage( pages_pb2.GetPageReq(page_id=page4.page_id)).can_edit assert api.GetPage( pages_pb2.GetPageReq(page_id=page4.page_id)).can_moderate with pages_session(token3) as api: assert not api.GetPage( pages_pb2.GetPageReq(page_id=page4.page_id)).can_edit assert not api.GetPage( pages_pb2.GetPageReq(page_id=page4.page_id)).can_moderate with pages_session(token1) as api: # now we're no longer the owner, can't transfer with pytest.raises(grpc.RpcError) as e: api.TransferPage( pages_pb2.TransferPageReq( page_id=page4.page_id, new_owner_community_id=community_id, )) assert e.value.code() == grpc.StatusCode.PERMISSION_DENIED assert e.value.details() == errors.PAGE_TRANSFER_PERMISSION_DENIED page4 = api.GetPage(pages_pb2.GetPageReq(page_id=page4.page_id)) assert page4.owner_group_id == group_id
def test_page_constraints(db): user, token = generate_user() with session_scope() as session: c_id = create_community(session, 0, 2, "Root node", [user], [], None).id # check we can't create a page without an owner with pytest.raises(IntegrityError) as e: with session_scope() as session: page = Page( parent_node_id=c_id, # note no owner creator_user_id=user.id, type=PageType.guide, thread=Thread(), ) session.add(page) session.add( PageVersion( page=page, editor_user_id=user.id, title=f"Title", content="Content", )) assert "violates check constraint" in str(e.value) assert "one_owner" in str(e.value) with session_scope() as session: node = Node(geom=to_multi( create_polygon_lat_lng([[0, 0], [0, 2], [2, 2], [2, 0], [0, 0]]))) session.add(node) cluster = Cluster( name=f"Testing Community", description=f"Description for testing community", parent_node=node, ) session.add(cluster) session.flush() cluster_parent_id = cluster.parent_node_id cluster_id = cluster.id # check we can't create a page with two owners with pytest.raises(IntegrityError) as e: with session_scope() as session: page = Page( parent_node_id=cluster_parent_id, creator_user_id=user.id, owner_cluster_id=cluster_id, owner_user_id=user.id, type=PageType.guide, thread=Thread(), ) session.add(page) session.add( PageVersion( page=page, editor_user_id=user.id, title=f"Title", content="Content", )) assert "violates check constraint" in str(e.value) assert "one_owner" in str(e.value) # main page must be owned by the right cluster with pytest.raises(IntegrityError) as e: with session_scope() as session: main_page = Page( parent_node_id=cluster_parent_id, # note owner is not cluster creator_user_id=user.id, owner_user_id=user.id, type=PageType.main_page, thread=Thread(), ) session.add(main_page) session.add( PageVersion( page=main_page, editor_user_id=user.id, title=f"Main page for the testing community", content="Empty.", )) assert "violates check constraint" in str(e.value) assert "main_page_owned_by_cluster" in str(e.value) # can only have one main page with pytest.raises(IntegrityError) as e: with session_scope() as session: main_page1 = Page( parent_node_id=cluster_parent_id, creator_user_id=user.id, owner_cluster_id=cluster_id, type=PageType.main_page, thread=Thread(), ) session.add(main_page1) session.add( PageVersion( page=main_page1, editor_user_id=user.id, title=f"Main page 1 for the testing community", content="Empty.", )) main_page2 = Page( parent_node_id=cluster_parent_id, creator_user_id=user.id, owner_cluster_id=cluster_id, type=PageType.main_page, thread=Thread(), ) session.add(main_page2) session.add( PageVersion( page=main_page2, editor_user_id=user.id, title=f"Main page 2 for the testing community", content="Empty.", )) assert "violates unique constraint" in str(e.value) assert "ix_pages_owner_cluster_id_type" in str(e.value)
def test_create_and_get_discussion(db): generate_user() user, token = generate_user() generate_user() generate_user() with session_scope() as session: node = Node(geom=to_multi( create_polygon_lat_lng([[0, 0], [0, 1], [1, 1], [1, 0], [0, 0]]))) session.add(node) community_cluster = Cluster( name=f"Testing Community", description=f"Description for testing community", parent_node=node, is_official_cluster=True, ) session.add(community_cluster) main_page = Page( parent_node=community_cluster.parent_node, creator_user_id=user.id, owner_cluster=community_cluster, type=PageType.main_page, thread=Thread(), ) session.add(main_page) session.add( PageVersion( page=main_page, editor_user_id=user.id, title=f"Main page for the testing community", content="Empty.", )) # create a group group_cluster = Cluster( name=f"Testing Group", description=f"Description for testing group", parent_node=node, ) session.add(group_cluster) main_page = Page( parent_node=group_cluster.parent_node, creator_user_id=user.id, owner_cluster=group_cluster, type=PageType.main_page, thread=Thread(), ) session.add(main_page) session.add( PageVersion( page=main_page, editor_user_id=user.id, title=f"Main page for the testing community", content="Empty.", )) session.flush() community_id = node.id community_cluster_id = community_cluster.id group_id = group_cluster.id with discussions_session(token) as api: time_before_create = now() res = api.CreateDiscussion( discussions_pb2.CreateDiscussionReq( title="dummy title", content="dummy content", owner_community_id=community_id, )) time_after_create = now() assert res.title == "dummy title" assert res.content == "dummy content" assert res.slug == "dummy-title" assert time_before_create < to_aware_datetime( res.created) < time_after_create assert res.creator_user_id == user.id assert res.owner_community_id == community_id discussion_id = res.discussion_id with discussions_session(token) as api: res = api.GetDiscussion( discussions_pb2.GetDiscussionReq(discussion_id=discussion_id, )) assert res.title == "dummy title" assert res.content == "dummy content" assert res.slug == "dummy-title" assert time_before_create < to_aware_datetime( res.created) < time_after_create assert res.creator_user_id == user.id assert res.owner_community_id == community_id with discussions_session(token) as api: time_before_create = now() res = api.CreateDiscussion( discussions_pb2.CreateDiscussionReq( title="dummy title", content="dummy content", owner_group_id=group_id, )) time_after_create = now() assert res.title == "dummy title" assert res.content == "dummy content" assert res.slug == "dummy-title" assert time_before_create < to_aware_datetime( res.created) < time_after_create assert res.creator_user_id == user.id assert res.owner_group_id == group_id discussion_id = res.discussion_id with discussions_session(token) as api: res = api.GetDiscussion( discussions_pb2.GetDiscussionReq(discussion_id=discussion_id, )) assert res.title == "dummy title" assert res.content == "dummy content" assert res.slug == "dummy-title" assert time_before_create < to_aware_datetime( res.created) < time_after_create assert res.creator_user_id == user.id assert res.owner_group_id == group_id