def test_ListOtherUserCommunities(testing_communities): with session_scope() as session: user1_id, token1 = get_user_id_and_token(session, "user1") user2_id, token2 = get_user_id_and_token(session, "user2") w_id = get_community_id(session, "World") c1_id = get_community_id(session, "Country 1") c1r1_id = get_community_id(session, "Country 1, Region 1") c1r1c1_id = get_community_id(session, "Country 1, Region 1, City 1") c1r1c2_id = get_community_id(session, "Country 1, Region 1, City 2") c1r2_id = get_community_id(session, "Country 1, Region 2") c1r2c1_id = get_community_id(session, "Country 1, Region 2, City 1") # Fetch user2's communities from user1's account with communities_session(token1) as api: res = api.ListUserCommunities( communities_pb2.ListUserCommunitiesReq(user_id=user2_id)) assert [c.community_id for c in res.communities] == [ w_id, c1_id, c1r1_id, c1r1c1_id, c1r1c2_id, c1r2_id, c1r2c1_id, ]
def test_ListNearbyUsers(testing_communities): with session_scope() as session: user1_id, token1 = get_user_id_and_token(session, "user1") user2_id, token2 = get_user_id_and_token(session, "user2") user3_id, token3 = get_user_id_and_token(session, "user3") user4_id, token4 = get_user_id_and_token(session, "user4") user5_id, token5 = get_user_id_and_token(session, "user5") user6_id, token6 = get_user_id_and_token(session, "user6") user7_id, token7 = get_user_id_and_token(session, "user7") user8_id, token8 = get_user_id_and_token(session, "user8") w_id = get_community_id(session, "World") c1r1c2_id = get_community_id(session, "Country 1, Region 1, City 2") with communities_session(token1) as api: res = api.ListNearbyUsers( communities_pb2.ListNearbyUsersReq(community_id=w_id, )) assert res.nearby_user_ids == [ user1_id, user2_id, user3_id, user4_id, user5_id, user6_id, user7_id, user8_id, ] res = api.ListNearbyUsers( communities_pb2.ListNearbyUsersReq(community_id=c1r1c2_id, )) assert res.nearby_user_ids == [user4_id]
def test_ListDiscussions(testing_communities): with session_scope() as session: user1_id, token1 = get_user_id_and_token(session, "user1") w_id = get_community_id(session, "Global") c1r1c2_id = get_community_id(session, "Country 1, Region 1, City 2") with communities_session(token1) as api: res = api.ListDiscussions( communities_pb2.ListDiscussionsReq( community_id=w_id, page_size=3, )) assert [d.title for d in res.discussions] == [ "Discussion title 6", "Discussion title 5", "Discussion title 4", ] for d in res.discussions: assert d.thread.thread_id > 0 assert d.thread.num_responses == 0 res = api.ListDiscussions( communities_pb2.ListDiscussionsReq( community_id=w_id, page_token=res.next_page_token, page_size=2, )) assert [d.title for d in res.discussions] == [ "Discussion title 3", "Discussion title 2", ] for d in res.discussions: assert d.thread.thread_id > 0 assert d.thread.num_responses == 0 res = api.ListDiscussions( communities_pb2.ListDiscussionsReq( community_id=w_id, page_token=res.next_page_token, page_size=2, )) assert [d.title for d in res.discussions] == [ "Discussion title 1", ] for d in res.discussions: assert d.thread.thread_id > 0 assert d.thread.num_responses == 0 res = api.ListDiscussions( communities_pb2.ListDiscussionsReq(community_id=c1r1c2_id, )) assert [d.title for d in res.discussions] == [ "Discussion title 7", ] for d in res.discussions: assert d.thread.thread_id > 0 assert d.thread.num_responses == 0
def test_ListGroups(testing_communities): with session_scope() as session: user1_id, token1 = get_user_id_and_token(session, "user1") user5_id, token5 = get_user_id_and_token(session, "user5") w_id = get_community_id(session, "World") hitchhikers_id = get_group_id(session, "Hitchhikers") c1r1_id = get_community_id(session, "Country 1, Region 1") foodies_id = get_group_id(session, "Country 1, Region 1, Foodies") skaters_id = get_group_id(session, "Country 1, Region 1, Skaters") with communities_session(token1) as api: res = api.ListGroups( communities_pb2.ListGroupsReq(community_id=c1r1_id, )) assert [g.group_id for g in res.groups] == [foodies_id, skaters_id] with communities_session(token5) as api: res = api.ListGroups( communities_pb2.ListGroupsReq(community_id=w_id, )) assert len(res.groups) == 1 assert res.groups[0].group_id == hitchhikers_id
def test_ListCommunities(testing_communities): with session_scope() as session: user1_id, token1 = get_user_id_and_token(session, "user1") c1_id = get_community_id(session, "Country 1") c1r1_id = get_community_id(session, "Country 1, Region 1") c1r2_id = get_community_id(session, "Country 1, Region 2") with communities_session(token1) as api: res = api.ListCommunities( communities_pb2.ListCommunitiesReq(community_id=c1_id, )) assert [c.community_id for c in res.communities] == [c1r1_id, c1r2_id]
def test_enforce_community_memberships_for_user(testing_communities): """ Make sure the user is added to the right communities on signup """ with auth_api_session() as (auth_api, metadata_interceptor): res = auth_api.SignupFlow( auth_pb2.SignupFlowReq( basic=auth_pb2.SignupBasic(name="testing", email="*****@*****.**"), account=auth_pb2.SignupAccount( username="******", password="******", birthdate="1970-01-01", gender="Bot", hosting_status=api_pb2.HOSTING_STATUS_CAN_HOST, city="Country 1, Region 1, City 2", # lat=8, lng=1 is equivalent to creating this coordinate with create_coordinate(8) lat=8, lng=1, radius=500, accept_tos=True, ), feedback=auth_pb2.ContributorForm(), accept_community_guidelines=wrappers_pb2.BoolValue(value=True), )) with session_scope() as session: email_token = (session.execute( select(SignupFlow).where(SignupFlow.flow_token == res.flow_token)).scalar_one().email_token) with auth_api_session() as (auth_api, metadata_interceptor): res = auth_api.SignupFlow( auth_pb2.SignupFlowReq(email_token=email_token)) user_id = res.auth_res.user_id # now check the user is in the right communities with session_scope() as session: w_id = get_community_id(session, "Global") c1_id = get_community_id(session, "Country 1") c1r1_id = get_community_id(session, "Country 1, Region 1") c1r1c2_id = get_community_id(session, "Country 1, Region 1, City 2") token = get_session_cookie_token(metadata_interceptor) with communities_session(token) as api: res = api.ListUserCommunities(communities_pb2.ListUserCommunitiesReq()) assert [c.community_id for c in res.communities] == [w_id, c1_id, c1r1_id, c1r1c2_id]
def test_ListAdmins(testing_communities): with session_scope() as session: user1_id, token1 = get_user_id_and_token(session, "user1") user3_id, token3 = get_user_id_and_token(session, "user3") user4_id, token4 = get_user_id_and_token(session, "user4") user5_id, token5 = get_user_id_and_token(session, "user5") user7_id, token7 = get_user_id_and_token(session, "user7") w_id = get_community_id(session, "Global") c1r1c2_id = get_community_id(session, "Country 1, Region 1, City 2") with communities_session(token1) as api: res = api.ListAdmins( communities_pb2.ListAdminsReq(community_id=w_id, )) assert res.admin_user_ids == [user1_id, user3_id, user7_id] res = api.ListAdmins( communities_pb2.ListAdminsReq(community_id=c1r1c2_id, )) assert res.admin_user_ids == [user4_id, user5_id]
def test_ListEvents(testing_communities): with session_scope() as session: user1_id, token1 = get_user_id_and_token(session, "user1") c1_id = get_community_id(session, "Country 1") with communities_session(token1) as api: res = api.ListEvents( communities_pb2.ListEventsReq( community_id=c1_id, page_size=3, )) assert [d.title for d in res.events] == [ "Event title 1", "Event title 2", "Event title 3", ] res = api.ListEvents( communities_pb2.ListEventsReq( community_id=c1_id, page_token=res.next_page_token, page_size=2, )) assert [d.title for d in res.events] == [ "Event title 4", "Event title 5", ] res = api.ListEvents( communities_pb2.ListEventsReq( community_id=c1_id, page_token=res.next_page_token, page_size=2, )) assert [d.title for d in res.events] == [ "Event title 6", ] assert not res.next_page_token
def test_ListCommunities_all(testing_communities): with session_scope() as session: user1_id, token1 = get_user_id_and_token(session, "user1") w_id = get_community_id(session, "Global") c1_id = get_community_id(session, "Country 1") c1r1_id = get_community_id(session, "Country 1, Region 1") c1r1c1_id = get_community_id(session, "Country 1, Region 1, City 1") c1r1c2_id = get_community_id(session, "Country 1, Region 1, City 2") c1r2_id = get_community_id(session, "Country 1, Region 2") c1r2c1_id = get_community_id(session, "Country 1, Region 2, City 1") c2_id = get_community_id(session, "Country 2") c2r1_id = get_community_id(session, "Country 2, Region 1") c2r1c1_id = get_community_id(session, "Country 2, Region 1, City 1") with communities_session(token1) as api: res = api.ListCommunities( communities_pb2.ListCommunitiesReq(page_size=5, )) assert [c.community_id for c in res.communities ] == [w_id, c1_id, c1r1_id, c1r1c1_id, c1r1c2_id] res = api.ListCommunities( communities_pb2.ListCommunitiesReq( page_size=2, page_token=res.next_page_token, )) assert [c.community_id for c in res.communities] == [c1r2_id, c1r2c1_id] res = api.ListCommunities( communities_pb2.ListCommunitiesReq( page_size=5, page_token=res.next_page_token, )) assert [c.community_id for c in res.communities] == [c2_id, c2r1_id, c2r1c1_id]
def test_JoinCommunity_and_LeaveCommunity(testing_communities): # these are separate as they mutate the database with session_scope() as session: # at x=1, inside c1 (country 1) user1_id, token1 = get_user_id_and_token(session, "user1") # at x=51, not inside c1 user8_id, token8 = get_user_id_and_token(session, "user8") c1_id = get_community_id(session, "Country 1") with communities_session(token1) as api: assert api.GetCommunity( communities_pb2.GetCommunityReq(community_id=c1_id)).member # user1 is already part of c1, cannot join with pytest.raises(grpc.RpcError) as e: res = api.JoinCommunity( communities_pb2.JoinCommunityReq(community_id=c1_id, )) assert e.value.code() == grpc.StatusCode.FAILED_PRECONDITION assert e.value.details() == errors.ALREADY_IN_COMMUNITY assert api.GetCommunity( communities_pb2.GetCommunityReq(community_id=c1_id)).member # user1 is inside c1, cannot leave with pytest.raises(grpc.RpcError) as e: res = api.LeaveCommunity( communities_pb2.LeaveCommunityReq(community_id=c1_id, )) assert e.value.code() == grpc.StatusCode.FAILED_PRECONDITION assert e.value.details() == errors.CANNOT_LEAVE_CONTAINING_COMMUNITY assert api.GetCommunity( communities_pb2.GetCommunityReq(community_id=c1_id)).member with communities_session(token8) as api: assert not api.GetCommunity( communities_pb2.GetCommunityReq(community_id=c1_id)).member # user8 is not in c1 yet, cannot leave with pytest.raises(grpc.RpcError) as e: res = api.LeaveCommunity( communities_pb2.LeaveCommunityReq(community_id=c1_id, )) assert e.value.code() == grpc.StatusCode.FAILED_PRECONDITION assert e.value.details() == errors.NOT_IN_COMMUNITY assert not api.GetCommunity( communities_pb2.GetCommunityReq(community_id=c1_id)).member # user8 is not in c1 and not part, can join res = api.JoinCommunity( communities_pb2.JoinCommunityReq(community_id=c1_id, )) assert api.GetCommunity( communities_pb2.GetCommunityReq(community_id=c1_id)).member # user8 is not in c1 and but now part, can't join again with pytest.raises(grpc.RpcError) as e: res = api.JoinCommunity( communities_pb2.JoinCommunityReq(community_id=c1_id, )) assert e.value.code() == grpc.StatusCode.FAILED_PRECONDITION assert e.value.details() == errors.ALREADY_IN_COMMUNITY assert api.GetCommunity( communities_pb2.GetCommunityReq(community_id=c1_id)).member # user8 is not in c1 yet, but part of it, can leave res = api.LeaveCommunity( communities_pb2.LeaveCommunityReq(community_id=c1_id, )) assert not api.GetCommunity( communities_pb2.GetCommunityReq(community_id=c1_id)).member
def test_GetCommunity(testing_communities): with session_scope() as session: user2_id, token2 = get_user_id_and_token(session, "user2") w_id = get_community_id(session, "World") c1_id = get_community_id(session, "Country 1") c1r1_id = get_community_id(session, "Country 1, Region 1") c1r1c1_id = get_community_id(session, "Country 1, Region 1, City 1") c2_id = get_community_id(session, "Country 2") with communities_session(token2) as api: res = api.GetCommunity( communities_pb2.GetCommunityReq(community_id=w_id, )) assert res.name == "World" assert res.slug == "world" assert res.description == "Description for World" assert len(res.parents) == 1 assert res.parents[0].HasField("community") assert res.parents[0].community.community_id == w_id assert res.parents[0].community.name == "World" assert res.parents[0].community.slug == "world" assert res.parents[ 0].community.description == "Description for World" assert res.main_page.type == pages_pb2.PAGE_TYPE_MAIN_PAGE assert res.main_page.slug == "main-page-for-the-world-community" assert res.main_page.last_editor_user_id == 1 assert res.main_page.creator_user_id == 1 assert res.main_page.owner_community_id == w_id assert res.main_page.title == "Main page for the World community" assert res.main_page.content == "There is nothing here yet..." assert not res.main_page.can_edit assert not res.main_page.can_moderate assert res.main_page.editor_user_ids == [1] assert res.member assert not res.admin assert res.member_count == 8 assert res.admin_count == 3 res = api.GetCommunity( communities_pb2.GetCommunityReq(community_id=c1r1c1_id, )) assert res.community_id == c1r1c1_id assert res.name == "Country 1, Region 1, City 1" assert res.slug == "country-1-region-1-city-1" assert res.description == "Description for Country 1, Region 1, City 1" assert len(res.parents) == 4 assert res.parents[0].HasField("community") assert res.parents[0].community.community_id == w_id assert res.parents[0].community.name == "World" assert res.parents[0].community.slug == "world" assert res.parents[ 0].community.description == "Description for World" assert res.parents[1].HasField("community") assert res.parents[1].community.community_id == c1_id assert res.parents[1].community.name == "Country 1" assert res.parents[1].community.slug == "country-1" assert res.parents[ 1].community.description == "Description for Country 1" assert res.parents[2].HasField("community") assert res.parents[2].community.community_id == c1r1_id assert res.parents[2].community.name == "Country 1, Region 1" assert res.parents[2].community.slug == "country-1-region-1" assert res.parents[ 2].community.description == "Description for Country 1, Region 1" assert res.parents[3].HasField("community") assert res.parents[3].community.community_id == c1r1c1_id assert res.parents[ 3].community.name == "Country 1, Region 1, City 1" assert res.parents[3].community.slug == "country-1-region-1-city-1" assert res.parents[ 3].community.description == "Description for Country 1, Region 1, City 1" assert res.main_page.type == pages_pb2.PAGE_TYPE_MAIN_PAGE assert res.main_page.slug == "main-page-for-the-country-1-region-1-city-1-community" assert res.main_page.last_editor_user_id == 2 assert res.main_page.creator_user_id == 2 assert res.main_page.owner_community_id == c1r1c1_id assert res.main_page.title == "Main page for the Country 1, Region 1, City 1 community" assert res.main_page.content == "There is nothing here yet..." assert res.main_page.can_edit assert res.main_page.can_moderate assert res.main_page.editor_user_ids == [2] assert res.member assert res.admin assert res.member_count == 3 assert res.admin_count == 1 res = api.GetCommunity( communities_pb2.GetCommunityReq(community_id=c2_id, )) assert res.community_id == c2_id assert res.name == "Country 2" assert res.slug == "country-2" assert res.description == "Description for Country 2" assert len(res.parents) == 2 assert res.parents[0].HasField("community") assert res.parents[0].community.community_id == w_id assert res.parents[0].community.name == "World" assert res.parents[0].community.slug == "world" assert res.parents[ 0].community.description == "Description for World" assert res.parents[1].HasField("community") assert res.parents[1].community.community_id == c2_id assert res.parents[1].community.name == "Country 2" assert res.parents[1].community.slug == "country-2" assert res.parents[ 1].community.description == "Description for Country 2" assert res.main_page.type == pages_pb2.PAGE_TYPE_MAIN_PAGE assert res.main_page.slug == "main-page-for-the-country-2-community" assert res.main_page.last_editor_user_id == 6 assert res.main_page.creator_user_id == 6 assert res.main_page.owner_community_id == c2_id assert res.main_page.title == "Main page for the Country 2 community" assert res.main_page.content == "There is nothing here yet..." assert not res.main_page.can_edit assert not res.main_page.can_moderate assert res.main_page.editor_user_ids == [6] assert not res.member assert not res.admin assert res.member_count == 2 assert res.admin_count == 2
def test_LeaveCommunity_regression(db): # See github issue #1444, repro: # 1. Join more than one community # 2. Leave one of them # 3. You are no longer in any community # admin user1, token1 = generate_user(username="******", geom=create_1d_point(200), geom_radius=0.1) # joiner/leaver user2, token2 = generate_user(username="******", geom=create_1d_point(201), geom_radius=0.1) with session_scope() as session: c0 = create_community(session, 0, 100, "Community 0", [user1], [], None) c1 = create_community(session, 0, 50, "Community 1", [user1], [], c0) c2 = create_community(session, 0, 10, "Community 2", [user1], [], c0) c0_id = c0.id c1_id = c1.id c2_id = c2.id enforce_community_memberships() with communities_session(token1) as api: assert api.GetCommunity( communities_pb2.GetCommunityReq(community_id=c0_id)).member assert api.GetCommunity( communities_pb2.GetCommunityReq(community_id=c1_id)).member assert api.GetCommunity( communities_pb2.GetCommunityReq(community_id=c2_id)).member with communities_session(token2) as api: # first check we're not in any communities assert not api.GetCommunity( communities_pb2.GetCommunityReq(community_id=c0_id)).member assert not api.GetCommunity( communities_pb2.GetCommunityReq(community_id=c1_id)).member assert not api.GetCommunity( communities_pb2.GetCommunityReq(community_id=c2_id)).member # join some communities api.JoinCommunity(communities_pb2.JoinCommunityReq(community_id=c1_id)) api.JoinCommunity(communities_pb2.JoinCommunityReq(community_id=c2_id)) # check memberships assert not api.GetCommunity( communities_pb2.GetCommunityReq(community_id=c0_id)).member assert api.GetCommunity( communities_pb2.GetCommunityReq(community_id=c1_id)).member assert api.GetCommunity( communities_pb2.GetCommunityReq(community_id=c2_id)).member # leave just c2 api.LeaveCommunity( communities_pb2.LeaveCommunityReq(community_id=c2_id)) # check memberships assert not api.GetCommunity( communities_pb2.GetCommunityReq(community_id=c0_id)).member assert api.GetCommunity( communities_pb2.GetCommunityReq(community_id=c1_id)).member assert not api.GetCommunity( communities_pb2.GetCommunityReq(community_id=c2_id)).member